blob: dd55312f3fe9c6f42dfe3e43151830675804f96e [file] [log] [blame]
Ingo Molnar8637c092006-07-03 00:24:38 -07001/*
2 * kernel/stacktrace.c
3 *
4 * Stack trace management functions
5 *
6 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7 */
8#include <linux/sched.h>
Ingo Molnar9212ddb2008-12-25 11:21:20 +01009#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040010#include <linux/export.h>
Ingo Molnar8637c092006-07-03 00:24:38 -070011#include <linux/kallsyms.h>
12#include <linux/stacktrace.h>
13
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020014/**
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
19 */
20void stack_trace_print(unsigned long *entries, unsigned int nr_entries,
21 int spaces)
Ingo Molnar8637c092006-07-03 00:24:38 -070022{
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020023 unsigned int i;
Ingo Molnar8637c092006-07-03 00:24:38 -070024
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020025 if (WARN_ON(!entries))
Johannes Bergbfeeeeb2008-05-12 21:21:14 +020026 return;
27
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020028 for (i = 0; i < nr_entries; i++)
29 printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
30}
31EXPORT_SYMBOL_GPL(stack_trace_print);
32
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020033/**
34 * stack_trace_snprint - Print the entries in the stack trace into a buffer
35 * @buf: Pointer to the print buffer
36 * @size: Size of the print buffer
37 * @entries: Pointer to storage array
38 * @nr_entries: Number of entries in the storage array
39 * @spaces: Number of leading spaces to print
40 *
41 * Return: Number of bytes printed.
42 */
43int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
44 unsigned int nr_entries, int spaces)
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080045{
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020046 unsigned int generated, i, total = 0;
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080047
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020048 if (WARN_ON(!entries))
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080049 return 0;
50
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020051 for (i = 0; i < nr_entries && size; i++) {
Omar Sandovalbfeda412017-02-07 15:33:20 -080052 generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020053 (void *)entries[i]);
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080054
55 total += generated;
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080056 if (generated >= size) {
57 buf += size;
58 size = 0;
59 } else {
60 buf += generated;
61 size -= generated;
62 }
63 }
64
65 return total;
66}
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020067EXPORT_SYMBOL_GPL(stack_trace_snprint);
68
Ingo Molnar9212ddb2008-12-25 11:21:20 +010069/*
Josh Poimboeufaf085d92017-02-13 19:42:28 -060070 * Architectures that do not implement save_stack_trace_*()
71 * get these weak aliases and once-per-bootup warnings
Masami Hiramatsuc624d332011-06-08 16:09:27 +090072 * (whenever this facility is utilized - for example by procfs):
Ingo Molnar9212ddb2008-12-25 11:21:20 +010073 */
74__weak void
75save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
76{
77 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
78}
Masami Hiramatsuc624d332011-06-08 16:09:27 +090079
80__weak void
81save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
82{
83 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
84}
Josh Poimboeufaf085d92017-02-13 19:42:28 -060085
86__weak int
87save_stack_trace_tsk_reliable(struct task_struct *tsk,
88 struct stack_trace *trace)
89{
90 WARN_ONCE(1, KERN_INFO "save_stack_tsk_reliable() not implemented yet.\n");
91 return -ENOSYS;
92}
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020093
94/**
95 * stack_trace_save - Save a stack trace into a storage array
96 * @store: Pointer to storage array
97 * @size: Size of the storage array
98 * @skipnr: Number of entries to skip at the start of the stack trace
99 *
100 * Return: Number of trace entries stored
101 */
102unsigned int stack_trace_save(unsigned long *store, unsigned int size,
103 unsigned int skipnr)
104{
105 struct stack_trace trace = {
106 .entries = store,
107 .max_entries = size,
108 .skip = skipnr + 1,
109 };
110
111 save_stack_trace(&trace);
112 return trace.nr_entries;
113}
114EXPORT_SYMBOL_GPL(stack_trace_save);
115
116/**
117 * stack_trace_save_tsk - Save a task stack trace into a storage array
118 * @task: The task to examine
119 * @store: Pointer to storage array
120 * @size: Size of the storage array
121 * @skipnr: Number of entries to skip at the start of the stack trace
122 *
123 * Return: Number of trace entries stored
124 */
125unsigned int stack_trace_save_tsk(struct task_struct *task,
126 unsigned long *store, unsigned int size,
127 unsigned int skipnr)
128{
129 struct stack_trace trace = {
130 .entries = store,
131 .max_entries = size,
132 .skip = skipnr + 1,
133 };
134
135 save_stack_trace_tsk(task, &trace);
136 return trace.nr_entries;
137}
138
139/**
140 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
141 * @regs: Pointer to pt_regs to examine
142 * @store: Pointer to storage array
143 * @size: Size of the storage array
144 * @skipnr: Number of entries to skip at the start of the stack trace
145 *
146 * Return: Number of trace entries stored
147 */
148unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
149 unsigned int size, unsigned int skipnr)
150{
151 struct stack_trace trace = {
152 .entries = store,
153 .max_entries = size,
154 .skip = skipnr,
155 };
156
157 save_stack_trace_regs(regs, &trace);
158 return trace.nr_entries;
159}
160
161#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
162/**
163 * stack_trace_save_tsk_reliable - Save task stack with verification
164 * @tsk: Pointer to the task to examine
165 * @store: Pointer to storage array
166 * @size: Size of the storage array
167 *
168 * Return: An error if it detects any unreliable features of the
169 * stack. Otherwise it guarantees that the stack trace is
170 * reliable and returns the number of entries stored.
171 *
172 * If the task is not 'current', the caller *must* ensure the task is inactive.
173 */
174int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
175 unsigned int size)
176{
177 struct stack_trace trace = {
178 .entries = store,
179 .max_entries = size,
180 };
181 int ret = save_stack_trace_tsk_reliable(tsk, &trace);
182
183 return ret ? ret : trace.nr_entries;
184}
185#endif
186
187#ifdef CONFIG_USER_STACKTRACE_SUPPORT
188/**
189 * stack_trace_save_user - Save a user space stack trace into a storage array
190 * @store: Pointer to storage array
191 * @size: Size of the storage array
192 *
193 * Return: Number of trace entries stored
194 */
195unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
196{
197 struct stack_trace trace = {
198 .entries = store,
199 .max_entries = size,
200 };
201
202 save_stack_trace_user(&trace);
203 return trace.nr_entries;
204}
205#endif /* CONFIG_USER_STACKTRACE_SUPPORT */