blob: 6d1f68b7e528a3f48c97f0a1e5c1a572d8ed8e11 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Ingo Molnar8637c092006-07-03 00:24:38 -07002/*
3 * kernel/stacktrace.c
4 *
5 * Stack trace management functions
6 *
7 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 */
Thomas Gleixner214d8ca2019-04-25 11:45:21 +02009#include <linux/sched/task_stack.h>
10#include <linux/sched/debug.h>
Ingo Molnar8637c092006-07-03 00:24:38 -070011#include <linux/sched.h>
Ingo Molnar9212ddb2008-12-25 11:21:20 +010012#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040013#include <linux/export.h>
Ingo Molnar8637c092006-07-03 00:24:38 -070014#include <linux/kallsyms.h>
15#include <linux/stacktrace.h>
16
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020017/**
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
22 */
Bart Van Asschea2970422019-07-22 11:24:41 -070023void stack_trace_print(const unsigned long *entries, unsigned int nr_entries,
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020024 int spaces)
Ingo Molnar8637c092006-07-03 00:24:38 -070025{
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020026 unsigned int i;
Ingo Molnar8637c092006-07-03 00:24:38 -070027
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020028 if (WARN_ON(!entries))
Johannes Bergbfeeeeb2008-05-12 21:21:14 +020029 return;
30
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020031 for (i = 0; i < nr_entries; i++)
32 printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
33}
34EXPORT_SYMBOL_GPL(stack_trace_print);
35
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020036/**
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
43 *
44 * Return: Number of bytes printed.
45 */
Bart Van Asschea2970422019-07-22 11:24:41 -070046int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020047 unsigned int nr_entries, int spaces)
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080048{
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020049 unsigned int generated, i, total = 0;
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080050
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020051 if (WARN_ON(!entries))
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080052 return 0;
53
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020054 for (i = 0; i < nr_entries && size; i++) {
Omar Sandovalbfeda412017-02-07 15:33:20 -080055 generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020056 (void *)entries[i]);
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080057
58 total += generated;
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080059 if (generated >= size) {
60 buf += size;
61 size = 0;
62 } else {
63 buf += generated;
64 size -= generated;
65 }
66 }
67
68 return total;
69}
Thomas Gleixnere9b98e12019-04-25 11:44:55 +020070EXPORT_SYMBOL_GPL(stack_trace_snprint);
71
Thomas Gleixner214d8ca2019-04-25 11:45:21 +020072#ifdef CONFIG_ARCH_STACKWALK
73
74struct stacktrace_cookie {
75 unsigned long *store;
76 unsigned int size;
77 unsigned int skip;
78 unsigned int len;
79};
80
81static bool stack_trace_consume_entry(void *cookie, unsigned long addr,
82 bool reliable)
83{
84 struct stacktrace_cookie *c = cookie;
85
86 if (c->len >= c->size)
87 return false;
88
89 if (c->skip > 0) {
90 c->skip--;
91 return true;
92 }
93 c->store[c->len++] = addr;
94 return c->len < c->size;
95}
96
97static bool stack_trace_consume_entry_nosched(void *cookie, unsigned long addr,
98 bool reliable)
99{
100 if (in_sched_functions(addr))
101 return true;
102 return stack_trace_consume_entry(cookie, addr, reliable);
103}
104
105/**
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
110 *
111 * Return: Number of trace entries stored.
112 */
113unsigned int stack_trace_save(unsigned long *store, unsigned int size,
114 unsigned int skipnr)
115{
116 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
117 struct stacktrace_cookie c = {
118 .store = store,
119 .size = size,
120 .skip = skipnr + 1,
121 };
122
123 arch_stack_walk(consume_entry, &c, current, NULL);
124 return c.len;
125}
126EXPORT_SYMBOL_GPL(stack_trace_save);
127
128/**
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
134 *
135 * Return: Number of trace entries stored.
136 */
137unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,
138 unsigned int size, unsigned int skipnr)
139{
140 stack_trace_consume_fn consume_entry = stack_trace_consume_entry_nosched;
141 struct stacktrace_cookie c = {
142 .store = store,
143 .size = size,
144 .skip = skipnr + 1,
145 };
146
147 if (!try_get_task_stack(tsk))
148 return 0;
149
150 arch_stack_walk(consume_entry, &c, tsk, NULL);
151 put_task_stack(tsk);
152 return c.len;
153}
154
155/**
156 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
157 * @regs: Pointer to pt_regs to examine
158 * @store: Pointer to storage array
159 * @size: Size of the storage array
160 * @skipnr: Number of entries to skip at the start of the stack trace
161 *
162 * Return: Number of trace entries stored.
163 */
164unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
165 unsigned int size, unsigned int skipnr)
166{
167 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
168 struct stacktrace_cookie c = {
169 .store = store,
170 .size = size,
171 .skip = skipnr,
172 };
173
174 arch_stack_walk(consume_entry, &c, current, regs);
175 return c.len;
176}
177
178#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
179/**
180 * stack_trace_save_tsk_reliable - Save task stack with verification
181 * @tsk: Pointer to the task to examine
182 * @store: Pointer to storage array
183 * @size: Size of the storage array
184 *
185 * Return: An error if it detects any unreliable features of the
186 * stack. Otherwise it guarantees that the stack trace is
187 * reliable and returns the number of entries stored.
188 *
189 * If the task is not 'current', the caller *must* ensure the task is inactive.
190 */
191int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
192 unsigned int size)
193{
194 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
195 struct stacktrace_cookie c = {
196 .store = store,
197 .size = size,
198 };
199 int ret;
200
201 /*
202 * If the task doesn't have a stack (e.g., a zombie), the stack is
203 * "reliably" empty.
204 */
205 if (!try_get_task_stack(tsk))
206 return 0;
207
208 ret = arch_stack_walk_reliable(consume_entry, &c, tsk);
209 put_task_stack(tsk);
Joe Lawrence7eaf51a2019-05-17 14:51:17 -0400210 return ret ? ret : c.len;
Thomas Gleixner214d8ca2019-04-25 11:45:21 +0200211}
212#endif
213
214#ifdef CONFIG_USER_STACKTRACE_SUPPORT
215/**
216 * stack_trace_save_user - Save a user space stack trace into a storage array
217 * @store: Pointer to storage array
218 * @size: Size of the storage array
219 *
220 * Return: Number of trace entries stored.
221 */
222unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
223{
224 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
225 struct stacktrace_cookie c = {
226 .store = store,
227 .size = size,
228 };
Peter Zijlstracac9b9a2019-07-18 10:47:47 +0200229 mm_segment_t fs;
Thomas Gleixner214d8ca2019-04-25 11:45:21 +0200230
231 /* Trace user stack if not a kernel thread */
Thomas Gleixner7e8e6812019-07-02 17:53:35 +0200232 if (current->flags & PF_KTHREAD)
Thomas Gleixner214d8ca2019-04-25 11:45:21 +0200233 return 0;
234
Peter Zijlstracac9b9a2019-07-18 10:47:47 +0200235 fs = get_fs();
236 set_fs(USER_DS);
Thomas Gleixner214d8ca2019-04-25 11:45:21 +0200237 arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
Peter Zijlstracac9b9a2019-07-18 10:47:47 +0200238 set_fs(fs);
239
Thomas Gleixner214d8ca2019-04-25 11:45:21 +0200240 return c.len;
241}
242#endif
243
244#else /* CONFIG_ARCH_STACKWALK */
245
Ingo Molnar9212ddb2008-12-25 11:21:20 +0100246/*
Josh Poimboeufaf085d92017-02-13 19:42:28 -0600247 * Architectures that do not implement save_stack_trace_*()
248 * get these weak aliases and once-per-bootup warnings
Masami Hiramatsuc624d332011-06-08 16:09:27 +0900249 * (whenever this facility is utilized - for example by procfs):
Ingo Molnar9212ddb2008-12-25 11:21:20 +0100250 */
251__weak void
252save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
253{
254 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
255}
Masami Hiramatsuc624d332011-06-08 16:09:27 +0900256
257__weak void
258save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
259{
260 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
261}
Josh Poimboeufaf085d92017-02-13 19:42:28 -0600262
Thomas Gleixnere9b98e12019-04-25 11:44:55 +0200263/**
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
268 *
269 * Return: Number of trace entries stored
270 */
271unsigned int stack_trace_save(unsigned long *store, unsigned int size,
272 unsigned int skipnr)
273{
274 struct stack_trace trace = {
275 .entries = store,
276 .max_entries = size,
277 .skip = skipnr + 1,
278 };
279
280 save_stack_trace(&trace);
281 return trace.nr_entries;
282}
283EXPORT_SYMBOL_GPL(stack_trace_save);
284
285/**
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
291 *
292 * Return: Number of trace entries stored
293 */
294unsigned int stack_trace_save_tsk(struct task_struct *task,
295 unsigned long *store, unsigned int size,
296 unsigned int skipnr)
297{
298 struct stack_trace trace = {
299 .entries = store,
300 .max_entries = size,
301 .skip = skipnr + 1,
302 };
303
304 save_stack_trace_tsk(task, &trace);
305 return trace.nr_entries;
306}
307
308/**
309 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
310 * @regs: Pointer to pt_regs to examine
311 * @store: Pointer to storage array
312 * @size: Size of the storage array
313 * @skipnr: Number of entries to skip at the start of the stack trace
314 *
315 * Return: Number of trace entries stored
316 */
317unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
318 unsigned int size, unsigned int skipnr)
319{
320 struct stack_trace trace = {
321 .entries = store,
322 .max_entries = size,
323 .skip = skipnr,
324 };
325
326 save_stack_trace_regs(regs, &trace);
327 return trace.nr_entries;
328}
329
330#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
331/**
332 * stack_trace_save_tsk_reliable - Save task stack with verification
333 * @tsk: Pointer to the task to examine
334 * @store: Pointer to storage array
335 * @size: Size of the storage array
336 *
337 * Return: An error if it detects any unreliable features of the
338 * stack. Otherwise it guarantees that the stack trace is
339 * reliable and returns the number of entries stored.
340 *
341 * If the task is not 'current', the caller *must* ensure the task is inactive.
342 */
343int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
344 unsigned int size)
345{
346 struct stack_trace trace = {
347 .entries = store,
348 .max_entries = size,
349 };
350 int ret = save_stack_trace_tsk_reliable(tsk, &trace);
351
352 return ret ? ret : trace.nr_entries;
353}
354#endif
355
356#ifdef CONFIG_USER_STACKTRACE_SUPPORT
357/**
358 * stack_trace_save_user - Save a user space stack trace into a storage array
359 * @store: Pointer to storage array
360 * @size: Size of the storage array
361 *
362 * Return: Number of trace entries stored
363 */
364unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
365{
366 struct stack_trace trace = {
367 .entries = store,
368 .max_entries = size,
369 };
370
371 save_stack_trace_user(&trace);
372 return trace.nr_entries;
373}
374#endif /* CONFIG_USER_STACKTRACE_SUPPORT */
Thomas Gleixner214d8ca2019-04-25 11:45:21 +0200375
376#endif /* !CONFIG_ARCH_STACKWALK */