1 // SPDX-License-Identifier: GPL-2.0-only
5 * Stack trace management functions
7 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
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>
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
24 void stack_trace_print(const unsigned long *entries
, unsigned int nr_entries
,
29 if (WARN_ON(!entries
))
32 for (i
= 0; i
< nr_entries
; i
++)
33 printk("%*c%pS\n", 1 + spaces
, ' ', (void *)entries
[i
]);
35 EXPORT_SYMBOL_GPL(stack_trace_print
);
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
45 * Return: Number of bytes printed.
47 int stack_trace_snprint(char *buf
, size_t size
, const unsigned long *entries
,
48 unsigned int nr_entries
, int spaces
)
50 unsigned int generated
, i
, total
= 0;
52 if (WARN_ON(!entries
))
55 for (i
= 0; i
< nr_entries
&& size
; i
++) {
56 generated
= snprintf(buf
, size
, "%*c%pS\n", 1 + spaces
, ' ',
60 if (generated
>= size
) {
71 EXPORT_SYMBOL_GPL(stack_trace_snprint
);
73 #ifdef CONFIG_ARCH_STACKWALK
75 struct stacktrace_cookie
{
82 static bool stack_trace_consume_entry(void *cookie
, unsigned long addr
)
84 struct stacktrace_cookie
*c
= cookie
;
86 if (c
->len
>= c
->size
)
93 c
->store
[c
->len
++] = addr
;
94 return c
->len
< c
->size
;
97 static bool stack_trace_consume_entry_nosched(void *cookie
, unsigned long addr
)
99 if (in_sched_functions(addr
))
101 return stack_trace_consume_entry(cookie
, addr
);
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
110 * Return: Number of trace entries stored.
112 unsigned int stack_trace_save(unsigned long *store
, unsigned int size
,
115 stack_trace_consume_fn consume_entry
= stack_trace_consume_entry
;
116 struct stacktrace_cookie c
= {
122 arch_stack_walk(consume_entry
, &c
, current
, NULL
);
125 EXPORT_SYMBOL_GPL(stack_trace_save
);
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
134 * Return: Number of trace entries stored.
136 unsigned int stack_trace_save_tsk(struct task_struct
*tsk
, unsigned long *store
,
137 unsigned int size
, unsigned int skipnr
)
139 stack_trace_consume_fn consume_entry
= stack_trace_consume_entry_nosched
;
140 struct stacktrace_cookie c
= {
143 /* skip this function if they are tracing us */
144 .skip
= skipnr
+ (current
== tsk
),
147 if (!try_get_task_stack(tsk
))
150 arch_stack_walk(consume_entry
, &c
, tsk
, NULL
);
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
162 * Return: Number of trace entries stored.
164 unsigned int stack_trace_save_regs(struct pt_regs
*regs
, unsigned long *store
,
165 unsigned int size
, unsigned int skipnr
)
167 stack_trace_consume_fn consume_entry
= stack_trace_consume_entry
;
168 struct stacktrace_cookie c
= {
174 arch_stack_walk(consume_entry
, &c
, current
, regs
);
178 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
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
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.
189 * If the task is not 'current', the caller *must* ensure the task is inactive.
191 int stack_trace_save_tsk_reliable(struct task_struct
*tsk
, unsigned long *store
,
194 stack_trace_consume_fn consume_entry
= stack_trace_consume_entry
;
195 struct stacktrace_cookie c
= {
202 * If the task doesn't have a stack (e.g., a zombie), the stack is
205 if (!try_get_task_stack(tsk
))
208 ret
= arch_stack_walk_reliable(consume_entry
, &c
, tsk
);
210 return ret
? ret
: c
.len
;
214 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
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
220 * Return: Number of trace entries stored.
222 unsigned int stack_trace_save_user(unsigned long *store
, unsigned int size
)
224 stack_trace_consume_fn consume_entry
= stack_trace_consume_entry
;
225 struct stacktrace_cookie c
= {
230 /* Trace user stack if not a kernel thread */
231 if (current
->flags
& PF_KTHREAD
)
234 arch_stack_walk_user(consume_entry
, &c
, task_pt_regs(current
));
240 #else /* CONFIG_ARCH_STACKWALK */
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):
248 save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
250 WARN_ONCE(1, KERN_INFO
"save_stack_trace_tsk() not implemented yet.\n");
254 save_stack_trace_regs(struct pt_regs
*regs
, struct stack_trace
*trace
)
256 WARN_ONCE(1, KERN_INFO
"save_stack_trace_regs() not implemented yet.\n");
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
265 * Return: Number of trace entries stored
267 unsigned int stack_trace_save(unsigned long *store
, unsigned int size
,
270 struct stack_trace trace
= {
276 save_stack_trace(&trace
);
277 return trace
.nr_entries
;
279 EXPORT_SYMBOL_GPL(stack_trace_save
);
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
288 * Return: Number of trace entries stored
290 unsigned int stack_trace_save_tsk(struct task_struct
*task
,
291 unsigned long *store
, unsigned int size
,
294 struct stack_trace trace
= {
297 /* skip this function if they are tracing us */
298 .skip
= skipnr
+ (current
== task
),
301 save_stack_trace_tsk(task
, &trace
);
302 return trace
.nr_entries
;
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
312 * Return: Number of trace entries stored
314 unsigned int stack_trace_save_regs(struct pt_regs
*regs
, unsigned long *store
,
315 unsigned int size
, unsigned int skipnr
)
317 struct stack_trace trace
= {
323 save_stack_trace_regs(regs
, &trace
);
324 return trace
.nr_entries
;
327 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
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
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.
338 * If the task is not 'current', the caller *must* ensure the task is inactive.
340 int stack_trace_save_tsk_reliable(struct task_struct
*tsk
, unsigned long *store
,
343 struct stack_trace trace
= {
347 int ret
= save_stack_trace_tsk_reliable(tsk
, &trace
);
349 return ret
? ret
: trace
.nr_entries
;
353 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
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
359 * Return: Number of trace entries stored
361 unsigned int stack_trace_save_user(unsigned long *store
, unsigned int size
)
363 struct stack_trace trace
= {
368 save_stack_trace_user(&trace
);
369 return trace
.nr_entries
;
371 #endif /* CONFIG_USER_STACKTRACE_SUPPORT */
373 #endif /* !CONFIG_ARCH_STACKWALK */
375 static inline bool in_irqentry_text(unsigned long ptr
)
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
);
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
388 * Return: Number of trace entries until IRQ stack starts.
390 unsigned int filter_irq_stacks(unsigned long *entries
, unsigned int nr_entries
)
394 for (i
= 0; i
< nr_entries
; i
++) {
395 if (in_irqentry_text(entries
[i
])) {
396 /* Include the irqentry function into the stack. */
402 EXPORT_SYMBOL_GPL(filter_irq_stacks
);