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)
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)
98 if (in_sched_functions(addr))
100 return stack_trace_consume_entry(cookie, addr);
104 * stack_trace_save - Save a stack trace into a storage array
105 * @store: Pointer to storage array
106 * @size: Size of the storage array
107 * @skipnr: Number of entries to skip at the start of the stack trace
109 * Return: Number of trace entries stored.
111 unsigned int stack_trace_save(unsigned long *store, unsigned int size,
114 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
115 struct stacktrace_cookie c = {
121 arch_stack_walk(consume_entry, &c, current, NULL);
124 EXPORT_SYMBOL_GPL(stack_trace_save);
127 * stack_trace_save_tsk - Save a task stack trace into a storage array
128 * @task: The task to examine
129 * @store: Pointer to storage array
130 * @size: Size of the storage array
131 * @skipnr: Number of entries to skip at the start of the stack trace
133 * Return: Number of trace entries stored.
135 unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,
136 unsigned int size, unsigned int skipnr)
138 stack_trace_consume_fn consume_entry = stack_trace_consume_entry_nosched;
139 struct stacktrace_cookie c = {
142 /* skip this function if they are tracing us */
143 .skip = skipnr + (current == tsk),
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);
209 return ret ? ret : c.len;
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 = {
230 /* Trace user stack if not a kernel thread */
231 if (current->flags & PF_KTHREAD)
234 fs = force_uaccess_begin();
235 arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
236 force_uaccess_end(fs);
242 #else /* CONFIG_ARCH_STACKWALK */
245 * Architectures that do not implement save_stack_trace_*()
246 * get these weak aliases and once-per-bootup warnings
247 * (whenever this facility is utilized - for example by procfs):
250 save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
252 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
256 save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
258 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
262 * stack_trace_save - Save a stack trace into a storage array
263 * @store: Pointer to storage array
264 * @size: Size of the storage array
265 * @skipnr: Number of entries to skip at the start of the stack trace
267 * Return: Number of trace entries stored
269 unsigned int stack_trace_save(unsigned long *store, unsigned int size,
272 struct stack_trace trace = {
278 save_stack_trace(&trace);
279 return trace.nr_entries;
281 EXPORT_SYMBOL_GPL(stack_trace_save);
284 * stack_trace_save_tsk - Save a task stack trace into a storage array
285 * @task: The task to examine
286 * @store: Pointer to storage array
287 * @size: Size of the storage array
288 * @skipnr: Number of entries to skip at the start of the stack trace
290 * Return: Number of trace entries stored
292 unsigned int stack_trace_save_tsk(struct task_struct *task,
293 unsigned long *store, unsigned int size,
296 struct stack_trace trace = {
299 /* skip this function if they are tracing us */
300 .skip = skipnr + (current == task),
303 save_stack_trace_tsk(task, &trace);
304 return trace.nr_entries;
308 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
309 * @regs: Pointer to pt_regs to examine
310 * @store: Pointer to storage array
311 * @size: Size of the storage array
312 * @skipnr: Number of entries to skip at the start of the stack trace
314 * Return: Number of trace entries stored
316 unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
317 unsigned int size, unsigned int skipnr)
319 struct stack_trace trace = {
325 save_stack_trace_regs(regs, &trace);
326 return trace.nr_entries;
329 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
331 * stack_trace_save_tsk_reliable - Save task stack with verification
332 * @tsk: Pointer to the task to examine
333 * @store: Pointer to storage array
334 * @size: Size of the storage array
336 * Return: An error if it detects any unreliable features of the
337 * stack. Otherwise it guarantees that the stack trace is
338 * reliable and returns the number of entries stored.
340 * If the task is not 'current', the caller *must* ensure the task is inactive.
342 int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
345 struct stack_trace trace = {
349 int ret = save_stack_trace_tsk_reliable(tsk, &trace);
351 return ret ? ret : trace.nr_entries;
355 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
357 * stack_trace_save_user - Save a user space stack trace into a storage array
358 * @store: Pointer to storage array
359 * @size: Size of the storage array
361 * Return: Number of trace entries stored
363 unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
365 struct stack_trace trace = {
370 save_stack_trace_user(&trace);
371 return trace.nr_entries;
373 #endif /* CONFIG_USER_STACKTRACE_SUPPORT */
375 #endif /* !CONFIG_ARCH_STACKWALK */