4 * Stack trace management functions
6 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/export.h>
11 #include <linux/kallsyms.h>
12 #include <linux/stacktrace.h>
15 * stack_trace_print - Print the entries in the stack trace
16 * @entries: Pointer to storage array
17 * @nr_entries: Number of entries in the storage array
18 * @spaces: Number of leading spaces to print
20 void stack_trace_print(unsigned long *entries, unsigned int nr_entries,
25 if (WARN_ON(!entries))
28 for (i = 0; i < nr_entries; i++)
29 printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
31 EXPORT_SYMBOL_GPL(stack_trace_print);
33 void print_stack_trace(struct stack_trace *trace, int spaces)
35 stack_trace_print(trace->entries, trace->nr_entries, spaces);
37 EXPORT_SYMBOL_GPL(print_stack_trace);
40 * stack_trace_snprint - Print the entries in the stack trace into a buffer
41 * @buf: Pointer to the print buffer
42 * @size: Size of the print buffer
43 * @entries: Pointer to storage array
44 * @nr_entries: Number of entries in the storage array
45 * @spaces: Number of leading spaces to print
47 * Return: Number of bytes printed.
49 int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
50 unsigned int nr_entries, int spaces)
52 unsigned int generated, i, total = 0;
54 if (WARN_ON(!entries))
57 for (i = 0; i < nr_entries && size; i++) {
58 generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
62 if (generated >= size) {
73 EXPORT_SYMBOL_GPL(stack_trace_snprint);
75 int snprint_stack_trace(char *buf, size_t size,
76 struct stack_trace *trace, int spaces)
78 return stack_trace_snprint(buf, size, trace->entries,
79 trace->nr_entries, spaces);
81 EXPORT_SYMBOL_GPL(snprint_stack_trace);
84 * Architectures that do not implement save_stack_trace_*()
85 * get these weak aliases and once-per-bootup warnings
86 * (whenever this facility is utilized - for example by procfs):
89 save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
91 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
95 save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
97 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
101 save_stack_trace_tsk_reliable(struct task_struct *tsk,
102 struct stack_trace *trace)
104 WARN_ONCE(1, KERN_INFO "save_stack_tsk_reliable() not implemented yet.\n");
109 * stack_trace_save - Save a stack trace into a storage array
110 * @store: Pointer to storage array
111 * @size: Size of the storage array
112 * @skipnr: Number of entries to skip at the start of the stack trace
114 * Return: Number of trace entries stored
116 unsigned int stack_trace_save(unsigned long *store, unsigned int size,
119 struct stack_trace trace = {
125 save_stack_trace(&trace);
126 return trace.nr_entries;
128 EXPORT_SYMBOL_GPL(stack_trace_save);
131 * stack_trace_save_tsk - Save a task stack trace into a storage array
132 * @task: The task to examine
133 * @store: Pointer to storage array
134 * @size: Size of the storage array
135 * @skipnr: Number of entries to skip at the start of the stack trace
137 * Return: Number of trace entries stored
139 unsigned int stack_trace_save_tsk(struct task_struct *task,
140 unsigned long *store, unsigned int size,
143 struct stack_trace trace = {
149 save_stack_trace_tsk(task, &trace);
150 return trace.nr_entries;
154 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
155 * @regs: Pointer to pt_regs to examine
156 * @store: Pointer to storage array
157 * @size: Size of the storage array
158 * @skipnr: Number of entries to skip at the start of the stack trace
160 * Return: Number of trace entries stored
162 unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
163 unsigned int size, unsigned int skipnr)
165 struct stack_trace trace = {
171 save_stack_trace_regs(regs, &trace);
172 return trace.nr_entries;
175 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
177 * stack_trace_save_tsk_reliable - Save task stack with verification
178 * @tsk: Pointer to the task to examine
179 * @store: Pointer to storage array
180 * @size: Size of the storage array
182 * Return: An error if it detects any unreliable features of the
183 * stack. Otherwise it guarantees that the stack trace is
184 * reliable and returns the number of entries stored.
186 * If the task is not 'current', the caller *must* ensure the task is inactive.
188 int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
191 struct stack_trace trace = {
195 int ret = save_stack_trace_tsk_reliable(tsk, &trace);
197 return ret ? ret : trace.nr_entries;
201 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
203 * stack_trace_save_user - Save a user space stack trace into a storage array
204 * @store: Pointer to storage array
205 * @size: Size of the storage array
207 * Return: Number of trace entries stored
209 unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
211 struct stack_trace trace = {
216 save_stack_trace_user(&trace);
217 return trace.nr_entries;
219 #endif /* CONFIG_USER_STACKTRACE_SUPPORT */