blob: b38333b3bc181f2d33db80b2c1c75fa92550ca95 [file] [log] [blame]
Ingo Molnar8637c092006-07-03 00:24:38 -07001/*
2 * kernel/stacktrace.c
3 *
4 * Stack trace management functions
5 *
6 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7 */
8#include <linux/sched.h>
Ingo Molnar9212ddb2008-12-25 11:21:20 +01009#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040010#include <linux/export.h>
Ingo Molnar8637c092006-07-03 00:24:38 -070011#include <linux/kallsyms.h>
12#include <linux/stacktrace.h>
13
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020014/**
15 * stack_trace_print - Print the entries in the stack trace
16 * @entries: Pointer to storage array
17 * @nr_entries: Number of entries in the storage array
18 * @spaces: Number of leading spaces to print
19 */
20void stack_trace_print(unsigned long *entries, unsigned int nr_entries,
21 int spaces)
Ingo Molnar8637c092006-07-03 00:24:38 -070022{
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020023 unsigned int i;
Ingo Molnar8637c092006-07-03 00:24:38 -070024
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020025 if (WARN_ON(!entries))
Johannes Bergbfeeeeb2008-05-12 21:21:14 +020026 return;
27
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020028 for (i = 0; i < nr_entries; i++)
29 printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
30}
31EXPORT_SYMBOL_GPL(stack_trace_print);
32
33void print_stack_trace(struct stack_trace *trace, int spaces)
34{
35 stack_trace_print(trace->entries, trace->nr_entries, spaces);
Ingo Molnar8637c092006-07-03 00:24:38 -070036}
Ingo Molnar85946982008-06-27 21:20:17 +020037EXPORT_SYMBOL_GPL(print_stack_trace);
Ingo Molnar8637c092006-07-03 00:24:38 -070038
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020039/**
40 * stack_trace_snprint - Print the entries in the stack trace into a buffer
41 * @buf: Pointer to the print buffer
42 * @size: Size of the print buffer
43 * @entries: Pointer to storage array
44 * @nr_entries: Number of entries in the storage array
45 * @spaces: Number of leading spaces to print
46 *
47 * Return: Number of bytes printed.
48 */
49int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
50 unsigned int nr_entries, int spaces)
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080051{
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020052 unsigned int generated, i, total = 0;
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080053
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020054 if (WARN_ON(!entries))
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080055 return 0;
56
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020057 for (i = 0; i < nr_entries && size; i++) {
Omar Sandovalbfeda412017-02-07 15:33:20 -080058 generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020059 (void *)entries[i]);
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080060
61 total += generated;
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080062 if (generated >= size) {
63 buf += size;
64 size = 0;
65 } else {
66 buf += generated;
67 size -= generated;
68 }
69 }
70
71 return total;
72}
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020073EXPORT_SYMBOL_GPL(stack_trace_snprint);
74
75int snprint_stack_trace(char *buf, size_t size,
76 struct stack_trace *trace, int spaces)
77{
78 return stack_trace_snprint(buf, size, trace->entries,
79 trace->nr_entries, spaces);
80}
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080081EXPORT_SYMBOL_GPL(snprint_stack_trace);
82
Ingo Molnar9212ddb2008-12-25 11:21:20 +010083/*
Josh Poimboeufaf085d92017-02-13 19:42:28 -060084 * Architectures that do not implement save_stack_trace_*()
85 * get these weak aliases and once-per-bootup warnings
Masami Hiramatsuc624d332011-06-08 16:09:27 +090086 * (whenever this facility is utilized - for example by procfs):
Ingo Molnar9212ddb2008-12-25 11:21:20 +010087 */
88__weak void
89save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
90{
91 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
92}
Masami Hiramatsuc624d332011-06-08 16:09:27 +090093
94__weak void
95save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
96{
97 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
98}
Josh Poimboeufaf085d92017-02-13 19:42:28 -060099
100__weak int
101save_stack_trace_tsk_reliable(struct task_struct *tsk,
102 struct stack_trace *trace)
103{
104 WARN_ONCE(1, KERN_INFO "save_stack_tsk_reliable() not implemented yet.\n");
105 return -ENOSYS;
106}
Thomas Gleixnere9b98e12019-04-25 11:44:55 +0200107
108/**
109 * stack_trace_save - Save a stack trace into a storage array
110 * @store: Pointer to storage array
111 * @size: Size of the storage array
112 * @skipnr: Number of entries to skip at the start of the stack trace
113 *
114 * Return: Number of trace entries stored
115 */
116unsigned int stack_trace_save(unsigned long *store, unsigned int size,
117 unsigned int skipnr)
118{
119 struct stack_trace trace = {
120 .entries = store,
121 .max_entries = size,
122 .skip = skipnr + 1,
123 };
124
125 save_stack_trace(&trace);
126 return trace.nr_entries;
127}
128EXPORT_SYMBOL_GPL(stack_trace_save);
129
130/**
131 * stack_trace_save_tsk - Save a task stack trace into a storage array
132 * @task: The task to examine
133 * @store: Pointer to storage array
134 * @size: Size of the storage array
135 * @skipnr: Number of entries to skip at the start of the stack trace
136 *
137 * Return: Number of trace entries stored
138 */
139unsigned int stack_trace_save_tsk(struct task_struct *task,
140 unsigned long *store, unsigned int size,
141 unsigned int skipnr)
142{
143 struct stack_trace trace = {
144 .entries = store,
145 .max_entries = size,
146 .skip = skipnr + 1,
147 };
148
149 save_stack_trace_tsk(task, &trace);
150 return trace.nr_entries;
151}
152
153/**
154 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
155 * @regs: Pointer to pt_regs to examine
156 * @store: Pointer to storage array
157 * @size: Size of the storage array
158 * @skipnr: Number of entries to skip at the start of the stack trace
159 *
160 * Return: Number of trace entries stored
161 */
162unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
163 unsigned int size, unsigned int skipnr)
164{
165 struct stack_trace trace = {
166 .entries = store,
167 .max_entries = size,
168 .skip = skipnr,
169 };
170
171 save_stack_trace_regs(regs, &trace);
172 return trace.nr_entries;
173}
174
175#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
176/**
177 * stack_trace_save_tsk_reliable - Save task stack with verification
178 * @tsk: Pointer to the task to examine
179 * @store: Pointer to storage array
180 * @size: Size of the storage array
181 *
182 * Return: An error if it detects any unreliable features of the
183 * stack. Otherwise it guarantees that the stack trace is
184 * reliable and returns the number of entries stored.
185 *
186 * If the task is not 'current', the caller *must* ensure the task is inactive.
187 */
188int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
189 unsigned int size)
190{
191 struct stack_trace trace = {
192 .entries = store,
193 .max_entries = size,
194 };
195 int ret = save_stack_trace_tsk_reliable(tsk, &trace);
196
197 return ret ? ret : trace.nr_entries;
198}
199#endif
200
201#ifdef CONFIG_USER_STACKTRACE_SUPPORT
202/**
203 * stack_trace_save_user - Save a user space stack trace into a storage array
204 * @store: Pointer to storage array
205 * @size: Size of the storage array
206 *
207 * Return: Number of trace entries stored
208 */
209unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
210{
211 struct stack_trace trace = {
212 .entries = store,
213 .max_entries = size,
214 };
215
216 save_stack_trace_user(&trace);
217 return trace.nr_entries;
218}
219#endif /* CONFIG_USER_STACKTRACE_SUPPORT */