4 * Stack trace management functions
6 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 #include <linux/sched/task_stack.h>
9 #include <linux/sched/debug.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/export.h>
13 #include <linux/kallsyms.h>
14 #include <linux/stacktrace.h>
17 * stack_trace_print - Print the entries in the stack trace
18 * @entries: Pointer to storage array
19 * @nr_entries: Number of entries in the storage array
20 * @spaces: Number of leading spaces to print
22 void stack_trace_print(unsigned long *entries, unsigned int nr_entries,
27 if (WARN_ON(!entries))
30 for (i = 0; i < nr_entries; i++)
31 printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
33 EXPORT_SYMBOL_GPL(stack_trace_print);
36 * stack_trace_snprint - Print the entries in the stack trace into a buffer
37 * @buf: Pointer to the print buffer
38 * @size: Size of the print buffer
39 * @entries: Pointer to storage array
40 * @nr_entries: Number of entries in the storage array
41 * @spaces: Number of leading spaces to print
43 * Return: Number of bytes printed.
45 int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
46 unsigned int nr_entries, int spaces)
48 unsigned int generated, i, total = 0;
50 if (WARN_ON(!entries))
53 for (i = 0; i < nr_entries && size; i++) {
54 generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
58 if (generated >= size) {
69 EXPORT_SYMBOL_GPL(stack_trace_snprint);
71 #ifdef CONFIG_ARCH_STACKWALK
73 struct stacktrace_cookie {
80 static bool stack_trace_consume_entry(void *cookie, unsigned long addr,
83 struct stacktrace_cookie *c = cookie;
85 if (c->len >= c->size)
92 c->store[c->len++] = addr;
93 return c->len < c->size;
96 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, reliable);
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 = {
146 if (!try_get_task_stack(tsk))
149 arch_stack_walk(consume_entry, &c, tsk, NULL);
155 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
156 * @regs: Pointer to pt_regs to examine
157 * @store: Pointer to storage array
158 * @size: Size of the storage array
159 * @skipnr: Number of entries to skip at the start of the stack trace
161 * Return: Number of trace entries stored.
163 unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
164 unsigned int size, unsigned int skipnr)
166 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
167 struct stacktrace_cookie c = {
173 arch_stack_walk(consume_entry, &c, current, regs);
177 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
179 * stack_trace_save_tsk_reliable - Save task stack with verification
180 * @tsk: Pointer to the task to examine
181 * @store: Pointer to storage array
182 * @size: Size of the storage array
184 * Return: An error if it detects any unreliable features of the
185 * stack. Otherwise it guarantees that the stack trace is
186 * reliable and returns the number of entries stored.
188 * If the task is not 'current', the caller *must* ensure the task is inactive.
190 int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
193 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
194 struct stacktrace_cookie c = {
201 * If the task doesn't have a stack (e.g., a zombie), the stack is
204 if (!try_get_task_stack(tsk))
207 ret = arch_stack_walk_reliable(consume_entry, &c, tsk);
213 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
215 * stack_trace_save_user - Save a user space stack trace into a storage array
216 * @store: Pointer to storage array
217 * @size: Size of the storage array
219 * Return: Number of trace entries stored.
221 unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
223 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
224 struct stacktrace_cookie c = {
229 /* Trace user stack if not a kernel thread */
233 arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
238 #else /* CONFIG_ARCH_STACKWALK */
241 * Architectures that do not implement save_stack_trace_*()
242 * get these weak aliases and once-per-bootup warnings
243 * (whenever this facility is utilized - for example by procfs):
246 save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
248 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
252 save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
254 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
258 save_stack_trace_tsk_reliable(struct task_struct *tsk,
259 struct stack_trace *trace)
261 WARN_ONCE(1, KERN_INFO "save_stack_tsk_reliable() not implemented yet.\n");
266 * stack_trace_save - Save a stack trace into a storage array
267 * @store: Pointer to storage array
268 * @size: Size of the storage array
269 * @skipnr: Number of entries to skip at the start of the stack trace
271 * Return: Number of trace entries stored
273 unsigned int stack_trace_save(unsigned long *store, unsigned int size,
276 struct stack_trace trace = {
282 save_stack_trace(&trace);
283 return trace.nr_entries;
285 EXPORT_SYMBOL_GPL(stack_trace_save);
288 * stack_trace_save_tsk - Save a task stack trace into a storage array
289 * @task: The task to examine
290 * @store: Pointer to storage array
291 * @size: Size of the storage array
292 * @skipnr: Number of entries to skip at the start of the stack trace
294 * Return: Number of trace entries stored
296 unsigned int stack_trace_save_tsk(struct task_struct *task,
297 unsigned long *store, unsigned int size,
300 struct stack_trace trace = {
306 save_stack_trace_tsk(task, &trace);
307 return trace.nr_entries;
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
317 * Return: Number of trace entries stored
319 unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
320 unsigned int size, unsigned int skipnr)
322 struct stack_trace trace = {
328 save_stack_trace_regs(regs, &trace);
329 return trace.nr_entries;
332 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
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
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.
343 * If the task is not 'current', the caller *must* ensure the task is inactive.
345 int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
348 struct stack_trace trace = {
352 int ret = save_stack_trace_tsk_reliable(tsk, &trace);
354 return ret ? ret : trace.nr_entries;
358 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
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
364 * Return: Number of trace entries stored
366 unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
368 struct stack_trace trace = {
373 save_stack_trace_user(&trace);
374 return trace.nr_entries;
376 #endif /* CONFIG_USER_STACKTRACE_SUPPORT */
378 #endif /* !CONFIG_ARCH_STACKWALK */