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>
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
23 void stack_trace_print(const unsigned long *entries
, unsigned int nr_entries
,
28 if (WARN_ON(!entries
))
31 for (i
= 0; i
< nr_entries
; i
++)
32 printk("%*c%pS\n", 1 + spaces
, ' ', (void *)entries
[i
]);
34 EXPORT_SYMBOL_GPL(stack_trace_print
);
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
44 * Return: Number of bytes printed.
46 int stack_trace_snprint(char *buf
, size_t size
, const unsigned long *entries
,
47 unsigned int nr_entries
, int spaces
)
49 unsigned int generated
, i
, total
= 0;
51 if (WARN_ON(!entries
))
54 for (i
= 0; i
< nr_entries
&& size
; i
++) {
55 generated
= snprintf(buf
, size
, "%*c%pS\n", 1 + spaces
, ' ',
59 if (generated
>= size
) {
70 EXPORT_SYMBOL_GPL(stack_trace_snprint
);
72 #ifdef CONFIG_ARCH_STACKWALK
74 struct stacktrace_cookie
{
81 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
,
100 if (in_sched_functions(addr
))
102 return stack_trace_consume_entry(cookie
, addr
, reliable
);
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
111 * Return: Number of trace entries stored.
113 unsigned int stack_trace_save(unsigned long *store
, unsigned int size
,
116 stack_trace_consume_fn consume_entry
= stack_trace_consume_entry
;
117 struct stacktrace_cookie c
= {
123 arch_stack_walk(consume_entry
, &c
, current
, NULL
);
126 EXPORT_SYMBOL_GPL(stack_trace_save
);
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
135 * Return: Number of trace entries stored.
137 unsigned int stack_trace_save_tsk(struct task_struct
*tsk
, unsigned long *store
,
138 unsigned int size
, unsigned int skipnr
)
140 stack_trace_consume_fn consume_entry
= stack_trace_consume_entry_nosched
;
141 struct stacktrace_cookie c
= {
144 /* skip this function if they are tracing us */
145 .skip
= skipnr
+ (current
== tsk
),
148 if (!try_get_task_stack(tsk
))
151 arch_stack_walk(consume_entry
, &c
, tsk
, NULL
);
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
163 * Return: Number of trace entries stored.
165 unsigned int stack_trace_save_regs(struct pt_regs
*regs
, unsigned long *store
,
166 unsigned int size
, unsigned int skipnr
)
168 stack_trace_consume_fn consume_entry
= stack_trace_consume_entry
;
169 struct stacktrace_cookie c
= {
175 arch_stack_walk(consume_entry
, &c
, current
, regs
);
179 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
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
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.
190 * If the task is not 'current', the caller *must* ensure the task is inactive.
192 int stack_trace_save_tsk_reliable(struct task_struct
*tsk
, unsigned long *store
,
195 stack_trace_consume_fn consume_entry
= stack_trace_consume_entry
;
196 struct stacktrace_cookie c
= {
203 * If the task doesn't have a stack (e.g., a zombie), the stack is
206 if (!try_get_task_stack(tsk
))
209 ret
= arch_stack_walk_reliable(consume_entry
, &c
, tsk
);
211 return ret
? ret
: c
.len
;
215 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
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
221 * Return: Number of trace entries stored.
223 unsigned int stack_trace_save_user(unsigned long *store
, unsigned int size
)
225 stack_trace_consume_fn consume_entry
= stack_trace_consume_entry
;
226 struct stacktrace_cookie c
= {
232 /* Trace user stack if not a kernel thread */
233 if (current
->flags
& PF_KTHREAD
)
236 fs
= force_uaccess_begin();
237 arch_stack_walk_user(consume_entry
, &c
, task_pt_regs(current
));
238 force_uaccess_end(fs
);
244 #else /* CONFIG_ARCH_STACKWALK */
247 * Architectures that do not implement save_stack_trace_*()
248 * get these weak aliases and once-per-bootup warnings
249 * (whenever this facility is utilized - for example by procfs):
252 save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
254 WARN_ONCE(1, KERN_INFO
"save_stack_trace_tsk() not implemented yet.\n");
258 save_stack_trace_regs(struct pt_regs
*regs
, struct stack_trace
*trace
)
260 WARN_ONCE(1, KERN_INFO
"save_stack_trace_regs() not implemented yet.\n");
264 * stack_trace_save - Save a stack trace into a storage array
265 * @store: Pointer to storage array
266 * @size: Size of the storage array
267 * @skipnr: Number of entries to skip at the start of the stack trace
269 * Return: Number of trace entries stored
271 unsigned int stack_trace_save(unsigned long *store
, unsigned int size
,
274 struct stack_trace trace
= {
280 save_stack_trace(&trace
);
281 return trace
.nr_entries
;
283 EXPORT_SYMBOL_GPL(stack_trace_save
);
286 * stack_trace_save_tsk - Save a task stack trace into a storage array
287 * @task: The task to examine
288 * @store: Pointer to storage array
289 * @size: Size of the storage array
290 * @skipnr: Number of entries to skip at the start of the stack trace
292 * Return: Number of trace entries stored
294 unsigned int stack_trace_save_tsk(struct task_struct
*task
,
295 unsigned long *store
, unsigned int size
,
298 struct stack_trace trace
= {
301 /* skip this function if they are tracing us */
302 .skip
= skipnr
+ (current
== task
),
305 save_stack_trace_tsk(task
, &trace
);
306 return trace
.nr_entries
;
310 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
311 * @regs: Pointer to pt_regs to examine
312 * @store: Pointer to storage array
313 * @size: Size of the storage array
314 * @skipnr: Number of entries to skip at the start of the stack trace
316 * Return: Number of trace entries stored
318 unsigned int stack_trace_save_regs(struct pt_regs
*regs
, unsigned long *store
,
319 unsigned int size
, unsigned int skipnr
)
321 struct stack_trace trace
= {
327 save_stack_trace_regs(regs
, &trace
);
328 return trace
.nr_entries
;
331 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
333 * stack_trace_save_tsk_reliable - Save task stack with verification
334 * @tsk: Pointer to the task to examine
335 * @store: Pointer to storage array
336 * @size: Size of the storage array
338 * Return: An error if it detects any unreliable features of the
339 * stack. Otherwise it guarantees that the stack trace is
340 * reliable and returns the number of entries stored.
342 * If the task is not 'current', the caller *must* ensure the task is inactive.
344 int stack_trace_save_tsk_reliable(struct task_struct
*tsk
, unsigned long *store
,
347 struct stack_trace trace
= {
351 int ret
= save_stack_trace_tsk_reliable(tsk
, &trace
);
353 return ret
? ret
: trace
.nr_entries
;
357 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
359 * stack_trace_save_user - Save a user space stack trace into a storage array
360 * @store: Pointer to storage array
361 * @size: Size of the storage array
363 * Return: Number of trace entries stored
365 unsigned int stack_trace_save_user(unsigned long *store
, unsigned int size
)
367 struct stack_trace trace
= {
372 save_stack_trace_user(&trace
);
373 return trace
.nr_entries
;
375 #endif /* CONFIG_USER_STACKTRACE_SUPPORT */
377 #endif /* !CONFIG_ARCH_STACKWALK */