Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * kernel/stacktrace.c
4 *
5 * Stack trace management functions
6 *
7 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 */
9#include <linux/sched/task_stack.h>
10#include <linux/sched/debug.h>
11#include <linux/sched.h>
12#include <linux/kernel.h>
13#include <linux/export.h>
14#include <linux/kallsyms.h>
15#include <linux/stacktrace.h>
16
17/**
18 * stack_trace_print - Print the entries in the stack trace
19 * @entries: Pointer to storage array
20 * @nr_entries: Number of entries in the storage array
21 * @spaces: Number of leading spaces to print
22 */
23void stack_trace_print(const unsigned long *entries, unsigned int nr_entries,
24 int spaces)
25{
26 unsigned int i;
27
28 if (WARN_ON(!entries))
29 return;
30
31 for (i = 0; i < nr_entries; i++)
32 printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
33}
34EXPORT_SYMBOL_GPL(stack_trace_print);
35
36/**
37 * stack_trace_snprint - Print the entries in the stack trace into a buffer
38 * @buf: Pointer to the print buffer
39 * @size: Size of the print buffer
40 * @entries: Pointer to storage array
41 * @nr_entries: Number of entries in the storage array
42 * @spaces: Number of leading spaces to print
43 *
44 * Return: Number of bytes printed.
45 */
46int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
47 unsigned int nr_entries, int spaces)
48{
49 unsigned int generated, i, total = 0;
50
51 if (WARN_ON(!entries))
52 return 0;
53
54 for (i = 0; i < nr_entries && size; i++) {
55 generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
56 (void *)entries[i]);
57
58 total += generated;
59 if (generated >= size) {
60 buf += size;
61 size = 0;
62 } else {
63 buf += generated;
64 size -= generated;
65 }
66 }
67
68 return total;
69}
70EXPORT_SYMBOL_GPL(stack_trace_snprint);
71
72#ifdef CONFIG_ARCH_STACKWALK
73
74struct stacktrace_cookie {
75 unsigned long *store;
76 unsigned int size;
77 unsigned int skip;
78 unsigned int len;
79};
80
81static bool stack_trace_consume_entry(void *cookie, unsigned long addr,
82 bool reliable)
83{
84 struct stacktrace_cookie *c = cookie;
85
86 if (c->len >= c->size)
87 return false;
88
89 if (c->skip > 0) {
90 c->skip--;
91 return true;
92 }
93 c->store[c->len++] = addr;
94 return c->len < c->size;
95}
96
97static bool stack_trace_consume_entry_nosched(void *cookie, unsigned long addr,
98 bool reliable)
99{
100 if (in_sched_functions(addr))
101 return true;
102 return stack_trace_consume_entry(cookie, addr, reliable);
103}
104
105/**
106 * stack_trace_save - Save a stack trace into a storage array
107 * @store: Pointer to storage array
108 * @size: Size of the storage array
109 * @skipnr: Number of entries to skip at the start of the stack trace
110 *
111 * Return: Number of trace entries stored.
112 */
113unsigned int stack_trace_save(unsigned long *store, unsigned int size,
114 unsigned int skipnr)
115{
116 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
117 struct stacktrace_cookie c = {
118 .store = store,
119 .size = size,
120 .skip = skipnr + 1,
121 };
122
123 arch_stack_walk(consume_entry, &c, current, NULL);
124 return c.len;
125}
126EXPORT_SYMBOL_GPL(stack_trace_save);
127
128/**
129 * stack_trace_save_tsk - Save a task stack trace into a storage array
130 * @task: The task to examine
131 * @store: Pointer to storage array
132 * @size: Size of the storage array
133 * @skipnr: Number of entries to skip at the start of the stack trace
134 *
135 * Return: Number of trace entries stored.
136 */
137unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,
138 unsigned int size, unsigned int skipnr)
139{
140 stack_trace_consume_fn consume_entry = stack_trace_consume_entry_nosched;
141 struct stacktrace_cookie c = {
142 .store = store,
143 .size = size,
144 /* skip this function if they are tracing us */
145 .skip = skipnr + !!(current == tsk),
146 };
147
148 if (!try_get_task_stack(tsk))
149 return 0;
150
151 arch_stack_walk(consume_entry, &c, tsk, NULL);
152 put_task_stack(tsk);
153 return c.len;
154}
155
156/**
157 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
158 * @regs: Pointer to pt_regs to examine
159 * @store: Pointer to storage array
160 * @size: Size of the storage array
161 * @skipnr: Number of entries to skip at the start of the stack trace
162 *
163 * Return: Number of trace entries stored.
164 */
165unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
166 unsigned int size, unsigned int skipnr)
167{
168 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
169 struct stacktrace_cookie c = {
170 .store = store,
171 .size = size,
172 .skip = skipnr,
173 };
174
175 arch_stack_walk(consume_entry, &c, current, regs);
176 return c.len;
177}
178
179#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
180/**
181 * stack_trace_save_tsk_reliable - Save task stack with verification
182 * @tsk: Pointer to the task to examine
183 * @store: Pointer to storage array
184 * @size: Size of the storage array
185 *
186 * Return: An error if it detects any unreliable features of the
187 * stack. Otherwise it guarantees that the stack trace is
188 * reliable and returns the number of entries stored.
189 *
190 * If the task is not 'current', the caller *must* ensure the task is inactive.
191 */
192int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
193 unsigned int size)
194{
195 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
196 struct stacktrace_cookie c = {
197 .store = store,
198 .size = size,
199 };
200 int ret;
201
202 /*
203 * If the task doesn't have a stack (e.g., a zombie), the stack is
204 * "reliably" empty.
205 */
206 if (!try_get_task_stack(tsk))
207 return 0;
208
209 ret = arch_stack_walk_reliable(consume_entry, &c, tsk);
210 put_task_stack(tsk);
211 return ret ? ret : c.len;
212}
213#endif
214
215#ifdef CONFIG_USER_STACKTRACE_SUPPORT
216/**
217 * stack_trace_save_user - Save a user space stack trace into a storage array
218 * @store: Pointer to storage array
219 * @size: Size of the storage array
220 *
221 * Return: Number of trace entries stored.
222 */
223unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
224{
225 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
226 struct stacktrace_cookie c = {
227 .store = store,
228 .size = size,
229 };
230 mm_segment_t fs;
231
232 /* Trace user stack if not a kernel thread */
233 if (current->flags & PF_KTHREAD)
234 return 0;
235
236 fs = get_fs();
237 set_fs(USER_DS);
238 arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
239 set_fs(fs);
240
241 return c.len;
242}
243#endif
244
245#else /* CONFIG_ARCH_STACKWALK */
246
247/*
248 * Architectures that do not implement save_stack_trace_*()
249 * get these weak aliases and once-per-bootup warnings
250 * (whenever this facility is utilized - for example by procfs):
251 */
252__weak void
253save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
254{
255 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
256}
257
258__weak void
259save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
260{
261 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
262}
263
264/**
265 * stack_trace_save - Save a stack trace into a storage array
266 * @store: Pointer to storage array
267 * @size: Size of the storage array
268 * @skipnr: Number of entries to skip at the start of the stack trace
269 *
270 * Return: Number of trace entries stored
271 */
272unsigned int stack_trace_save(unsigned long *store, unsigned int size,
273 unsigned int skipnr)
274{
275 struct stack_trace trace = {
276 .entries = store,
277 .max_entries = size,
278 .skip = skipnr + 1,
279 };
280
281 save_stack_trace(&trace);
282 return trace.nr_entries;
283}
284EXPORT_SYMBOL_GPL(stack_trace_save);
285
286/**
287 * stack_trace_save_tsk - Save a task stack trace into a storage array
288 * @task: The task to examine
289 * @store: Pointer to storage array
290 * @size: Size of the storage array
291 * @skipnr: Number of entries to skip at the start of the stack trace
292 *
293 * Return: Number of trace entries stored
294 */
295unsigned int stack_trace_save_tsk(struct task_struct *task,
296 unsigned long *store, unsigned int size,
297 unsigned int skipnr)
298{
299 struct stack_trace trace = {
300 .entries = store,
301 .max_entries = size,
302 /* skip this function if they are tracing us */
303 .skip = skipnr + !!(current == task),
304 };
305
306 save_stack_trace_tsk(task, &trace);
307 return trace.nr_entries;
308}
309
310/**
311 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
312 * @regs: Pointer to pt_regs to examine
313 * @store: Pointer to storage array
314 * @size: Size of the storage array
315 * @skipnr: Number of entries to skip at the start of the stack trace
316 *
317 * Return: Number of trace entries stored
318 */
319unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
320 unsigned int size, unsigned int skipnr)
321{
322 struct stack_trace trace = {
323 .entries = store,
324 .max_entries = size,
325 .skip = skipnr,
326 };
327
328 save_stack_trace_regs(regs, &trace);
329 return trace.nr_entries;
330}
331
332#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
333/**
334 * stack_trace_save_tsk_reliable - Save task stack with verification
335 * @tsk: Pointer to the task to examine
336 * @store: Pointer to storage array
337 * @size: Size of the storage array
338 *
339 * Return: An error if it detects any unreliable features of the
340 * stack. Otherwise it guarantees that the stack trace is
341 * reliable and returns the number of entries stored.
342 *
343 * If the task is not 'current', the caller *must* ensure the task is inactive.
344 */
345int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
346 unsigned int size)
347{
348 struct stack_trace trace = {
349 .entries = store,
350 .max_entries = size,
351 };
352 int ret = save_stack_trace_tsk_reliable(tsk, &trace);
353
354 return ret ? ret : trace.nr_entries;
355}
356#endif
357
358#ifdef CONFIG_USER_STACKTRACE_SUPPORT
359/**
360 * stack_trace_save_user - Save a user space stack trace into a storage array
361 * @store: Pointer to storage array
362 * @size: Size of the storage array
363 *
364 * Return: Number of trace entries stored
365 */
366unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
367{
368 struct stack_trace trace = {
369 .entries = store,
370 .max_entries = size,
371 };
372
373 save_stack_trace_user(&trace);
374 return trace.nr_entries;
375}
376#endif /* CONFIG_USER_STACKTRACE_SUPPORT */
377
378#endif /* !CONFIG_ARCH_STACKWALK */
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * kernel/stacktrace.c
4 *
5 * Stack trace management functions
6 *
7 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 */
9#include <linux/sched/task_stack.h>
10#include <linux/sched/debug.h>
11#include <linux/sched.h>
12#include <linux/kernel.h>
13#include <linux/export.h>
14#include <linux/kallsyms.h>
15#include <linux/stacktrace.h>
16#include <linux/interrupt.h>
17
18/**
19 * stack_trace_print - Print the entries in the stack trace
20 * @entries: Pointer to storage array
21 * @nr_entries: Number of entries in the storage array
22 * @spaces: Number of leading spaces to print
23 */
24void stack_trace_print(const unsigned long *entries, unsigned int nr_entries,
25 int spaces)
26{
27 unsigned int i;
28
29 if (WARN_ON(!entries))
30 return;
31
32 for (i = 0; i < nr_entries; i++)
33 printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
34}
35EXPORT_SYMBOL_GPL(stack_trace_print);
36
37/**
38 * stack_trace_snprint - Print the entries in the stack trace into a buffer
39 * @buf: Pointer to the print buffer
40 * @size: Size of the print buffer
41 * @entries: Pointer to storage array
42 * @nr_entries: Number of entries in the storage array
43 * @spaces: Number of leading spaces to print
44 *
45 * Return: Number of bytes printed.
46 */
47int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
48 unsigned int nr_entries, int spaces)
49{
50 unsigned int generated, i, total = 0;
51
52 if (WARN_ON(!entries))
53 return 0;
54
55 for (i = 0; i < nr_entries && size; i++) {
56 generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
57 (void *)entries[i]);
58
59 total += generated;
60 if (generated >= size) {
61 buf += size;
62 size = 0;
63 } else {
64 buf += generated;
65 size -= generated;
66 }
67 }
68
69 return total;
70}
71EXPORT_SYMBOL_GPL(stack_trace_snprint);
72
73#ifdef CONFIG_ARCH_STACKWALK
74
75struct stacktrace_cookie {
76 unsigned long *store;
77 unsigned int size;
78 unsigned int skip;
79 unsigned int len;
80};
81
82static bool stack_trace_consume_entry(void *cookie, unsigned long addr)
83{
84 struct stacktrace_cookie *c = cookie;
85
86 if (c->len >= c->size)
87 return false;
88
89 if (c->skip > 0) {
90 c->skip--;
91 return true;
92 }
93 c->store[c->len++] = addr;
94 return c->len < c->size;
95}
96
97static bool stack_trace_consume_entry_nosched(void *cookie, unsigned long addr)
98{
99 if (in_sched_functions(addr))
100 return true;
101 return stack_trace_consume_entry(cookie, addr);
102}
103
104/**
105 * stack_trace_save - Save a stack trace into a storage array
106 * @store: Pointer to storage array
107 * @size: Size of the storage array
108 * @skipnr: Number of entries to skip at the start of the stack trace
109 *
110 * Return: Number of trace entries stored.
111 */
112unsigned int stack_trace_save(unsigned long *store, unsigned int size,
113 unsigned int skipnr)
114{
115 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
116 struct stacktrace_cookie c = {
117 .store = store,
118 .size = size,
119 .skip = skipnr + 1,
120 };
121
122 arch_stack_walk(consume_entry, &c, current, NULL);
123 return c.len;
124}
125EXPORT_SYMBOL_GPL(stack_trace_save);
126
127/**
128 * stack_trace_save_tsk - Save a task stack trace into a storage array
129 * @task: The task to examine
130 * @store: Pointer to storage array
131 * @size: Size of the storage array
132 * @skipnr: Number of entries to skip at the start of the stack trace
133 *
134 * Return: Number of trace entries stored.
135 */
136unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,
137 unsigned int size, unsigned int skipnr)
138{
139 stack_trace_consume_fn consume_entry = stack_trace_consume_entry_nosched;
140 struct stacktrace_cookie c = {
141 .store = store,
142 .size = size,
143 /* skip this function if they are tracing us */
144 .skip = skipnr + (current == tsk),
145 };
146
147 if (!try_get_task_stack(tsk))
148 return 0;
149
150 arch_stack_walk(consume_entry, &c, tsk, NULL);
151 put_task_stack(tsk);
152 return c.len;
153}
154
155/**
156 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
157 * @regs: Pointer to pt_regs to examine
158 * @store: Pointer to storage array
159 * @size: Size of the storage array
160 * @skipnr: Number of entries to skip at the start of the stack trace
161 *
162 * Return: Number of trace entries stored.
163 */
164unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
165 unsigned int size, unsigned int skipnr)
166{
167 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
168 struct stacktrace_cookie c = {
169 .store = store,
170 .size = size,
171 .skip = skipnr,
172 };
173
174 arch_stack_walk(consume_entry, &c, current, regs);
175 return c.len;
176}
177
178#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
179/**
180 * stack_trace_save_tsk_reliable - Save task stack with verification
181 * @tsk: Pointer to the task to examine
182 * @store: Pointer to storage array
183 * @size: Size of the storage array
184 *
185 * Return: An error if it detects any unreliable features of the
186 * stack. Otherwise it guarantees that the stack trace is
187 * reliable and returns the number of entries stored.
188 *
189 * If the task is not 'current', the caller *must* ensure the task is inactive.
190 */
191int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
192 unsigned int size)
193{
194 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
195 struct stacktrace_cookie c = {
196 .store = store,
197 .size = size,
198 };
199 int ret;
200
201 /*
202 * If the task doesn't have a stack (e.g., a zombie), the stack is
203 * "reliably" empty.
204 */
205 if (!try_get_task_stack(tsk))
206 return 0;
207
208 ret = arch_stack_walk_reliable(consume_entry, &c, tsk);
209 put_task_stack(tsk);
210 return ret ? ret : c.len;
211}
212#endif
213
214#ifdef CONFIG_USER_STACKTRACE_SUPPORT
215/**
216 * stack_trace_save_user - Save a user space stack trace into a storage array
217 * @store: Pointer to storage array
218 * @size: Size of the storage array
219 *
220 * Return: Number of trace entries stored.
221 */
222unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
223{
224 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
225 struct stacktrace_cookie c = {
226 .store = store,
227 .size = size,
228 };
229
230 /* Trace user stack if not a kernel thread */
231 if (current->flags & PF_KTHREAD)
232 return 0;
233
234 arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
235
236 return c.len;
237}
238#endif
239
240#else /* CONFIG_ARCH_STACKWALK */
241
242/*
243 * Architectures that do not implement save_stack_trace_*()
244 * get these weak aliases and once-per-bootup warnings
245 * (whenever this facility is utilized - for example by procfs):
246 */
247__weak void
248save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
249{
250 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
251}
252
253__weak void
254save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
255{
256 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
257}
258
259/**
260 * stack_trace_save - Save a stack trace into a storage array
261 * @store: Pointer to storage array
262 * @size: Size of the storage array
263 * @skipnr: Number of entries to skip at the start of the stack trace
264 *
265 * Return: Number of trace entries stored
266 */
267unsigned int stack_trace_save(unsigned long *store, unsigned int size,
268 unsigned int skipnr)
269{
270 struct stack_trace trace = {
271 .entries = store,
272 .max_entries = size,
273 .skip = skipnr + 1,
274 };
275
276 save_stack_trace(&trace);
277 return trace.nr_entries;
278}
279EXPORT_SYMBOL_GPL(stack_trace_save);
280
281/**
282 * stack_trace_save_tsk - Save a task stack trace into a storage array
283 * @task: The task to examine
284 * @store: Pointer to storage array
285 * @size: Size of the storage array
286 * @skipnr: Number of entries to skip at the start of the stack trace
287 *
288 * Return: Number of trace entries stored
289 */
290unsigned int stack_trace_save_tsk(struct task_struct *task,
291 unsigned long *store, unsigned int size,
292 unsigned int skipnr)
293{
294 struct stack_trace trace = {
295 .entries = store,
296 .max_entries = size,
297 /* skip this function if they are tracing us */
298 .skip = skipnr + (current == task),
299 };
300
301 save_stack_trace_tsk(task, &trace);
302 return trace.nr_entries;
303}
304
305/**
306 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
307 * @regs: Pointer to pt_regs to examine
308 * @store: Pointer to storage array
309 * @size: Size of the storage array
310 * @skipnr: Number of entries to skip at the start of the stack trace
311 *
312 * Return: Number of trace entries stored
313 */
314unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
315 unsigned int size, unsigned int skipnr)
316{
317 struct stack_trace trace = {
318 .entries = store,
319 .max_entries = size,
320 .skip = skipnr,
321 };
322
323 save_stack_trace_regs(regs, &trace);
324 return trace.nr_entries;
325}
326
327#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
328/**
329 * stack_trace_save_tsk_reliable - Save task stack with verification
330 * @tsk: Pointer to the task to examine
331 * @store: Pointer to storage array
332 * @size: Size of the storage array
333 *
334 * Return: An error if it detects any unreliable features of the
335 * stack. Otherwise it guarantees that the stack trace is
336 * reliable and returns the number of entries stored.
337 *
338 * If the task is not 'current', the caller *must* ensure the task is inactive.
339 */
340int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
341 unsigned int size)
342{
343 struct stack_trace trace = {
344 .entries = store,
345 .max_entries = size,
346 };
347 int ret = save_stack_trace_tsk_reliable(tsk, &trace);
348
349 return ret ? ret : trace.nr_entries;
350}
351#endif
352
353#ifdef CONFIG_USER_STACKTRACE_SUPPORT
354/**
355 * stack_trace_save_user - Save a user space stack trace into a storage array
356 * @store: Pointer to storage array
357 * @size: Size of the storage array
358 *
359 * Return: Number of trace entries stored
360 */
361unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
362{
363 struct stack_trace trace = {
364 .entries = store,
365 .max_entries = size,
366 };
367
368 save_stack_trace_user(&trace);
369 return trace.nr_entries;
370}
371#endif /* CONFIG_USER_STACKTRACE_SUPPORT */
372
373#endif /* !CONFIG_ARCH_STACKWALK */
374
375static inline bool in_irqentry_text(unsigned long ptr)
376{
377 return (ptr >= (unsigned long)&__irqentry_text_start &&
378 ptr < (unsigned long)&__irqentry_text_end) ||
379 (ptr >= (unsigned long)&__softirqentry_text_start &&
380 ptr < (unsigned long)&__softirqentry_text_end);
381}
382
383/**
384 * filter_irq_stacks - Find first IRQ stack entry in trace
385 * @entries: Pointer to stack trace array
386 * @nr_entries: Number of entries in the storage array
387 *
388 * Return: Number of trace entries until IRQ stack starts.
389 */
390unsigned int filter_irq_stacks(unsigned long *entries, unsigned int nr_entries)
391{
392 unsigned int i;
393
394 for (i = 0; i < nr_entries; i++) {
395 if (in_irqentry_text(entries[i])) {
396 /* Include the irqentry function into the stack. */
397 return i + 1;
398 }
399 }
400 return nr_entries;
401}
402EXPORT_SYMBOL_GPL(filter_irq_stacks);