blob: f2e72094051bda1c9b3cbdda751e2580bdbd790a [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +09002/*
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +09003 * Stack trace management functions
4 *
5 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6 */
7#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +01008#include <linux/sched/debug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +01009#include <linux/sched/task_stack.h>
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090010#include <linux/stacktrace.h>
Paul Gortmaker73bc2562011-07-23 16:30:40 -040011#include <linux/export.h>
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090012#include <asm/stacktrace.h>
13
14/*
15 * Save stack-backtrace addresses into a stack_trace buffer:
16 */
17static void save_raw_context_stack(struct stack_trace *trace,
Aaro Koskinene1e16112015-10-23 15:39:02 +030018 unsigned long reg29, int savesched)
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090019{
20 unsigned long *sp = (unsigned long *)reg29;
21 unsigned long addr;
22
23 while (!kstack_end(sp)) {
24 addr = *sp++;
Aaro Koskinene1e16112015-10-23 15:39:02 +030025 if (__kernel_text_address(addr) &&
26 (savesched || !in_sched_functions(addr))) {
Atsushi Nemoto23126692006-09-28 19:15:33 +090027 if (trace->skip > 0)
28 trace->skip--;
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090029 else
Atsushi Nemoto23126692006-09-28 19:15:33 +090030 trace->entries[trace->nr_entries++] = addr;
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090031 if (trace->nr_entries >= trace->max_entries)
32 break;
33 }
34 }
35}
36
Ken Chen2ec220e2008-11-10 11:26:08 +030037static void save_context_stack(struct stack_trace *trace,
Aaro Koskinene1e16112015-10-23 15:39:02 +030038 struct task_struct *tsk, struct pt_regs *regs, int savesched)
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090039{
40 unsigned long sp = regs->regs[29];
41#ifdef CONFIG_KALLSYMS
42 unsigned long ra = regs->regs[31];
43 unsigned long pc = regs->cp0_epc;
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090044
45 if (raw_show_trace || !__kernel_text_address(pc)) {
Atsushi Nemoto19246002006-09-29 18:02:51 +090046 unsigned long stack_page =
Ken Chen2ec220e2008-11-10 11:26:08 +030047 (unsigned long)task_stack_page(tsk);
Atsushi Nemoto23126692006-09-28 19:15:33 +090048 if (stack_page && sp >= stack_page &&
49 sp <= stack_page + THREAD_SIZE - 32)
Aaro Koskinene1e16112015-10-23 15:39:02 +030050 save_raw_context_stack(trace, sp, savesched);
Atsushi Nemoto19246002006-09-29 18:02:51 +090051 return;
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090052 }
53 do {
Aaro Koskinene1e16112015-10-23 15:39:02 +030054 if (savesched || !in_sched_functions(pc)) {
55 if (trace->skip > 0)
56 trace->skip--;
57 else
58 trace->entries[trace->nr_entries++] = pc;
59 if (trace->nr_entries >= trace->max_entries)
60 break;
61 }
Ken Chen2ec220e2008-11-10 11:26:08 +030062 pc = unwind_stack(tsk, &sp, pc, &ra);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090063 } while (pc);
64#else
Aaro Koskinene1e16112015-10-23 15:39:02 +030065 save_raw_context_stack(trace, sp, savesched);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090066#endif
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090067}
68
69/*
70 * Save stack-backtrace addresses into a stack_trace buffer.
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090071 */
Christoph Hellwigab1b6f02007-05-08 00:23:29 -070072void save_stack_trace(struct stack_trace *trace)
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090073{
Ken Chen2ec220e2008-11-10 11:26:08 +030074 save_stack_trace_tsk(current, trace);
75}
76EXPORT_SYMBOL_GPL(save_stack_trace);
77
78void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
79{
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090080 struct pt_regs dummyregs;
81 struct pt_regs *regs = &dummyregs;
82
83 WARN_ON(trace->nr_entries || !trace->max_entries);
84
Ken Chen2ec220e2008-11-10 11:26:08 +030085 if (tsk != current) {
86 regs->regs[29] = tsk->thread.reg29;
87 regs->regs[31] = 0;
88 regs->cp0_epc = tsk->thread.reg31;
89 } else
90 prepare_frametrace(regs);
Aaro Koskinene1e16112015-10-23 15:39:02 +030091 save_context_stack(trace, tsk, regs, tsk == current);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090092}
Ken Chen2ec220e2008-11-10 11:26:08 +030093EXPORT_SYMBOL_GPL(save_stack_trace_tsk);