Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 2 | /* |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 3 | * Stack trace management functions |
| 4 | * |
| 5 | * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp> |
| 6 | */ |
| 7 | #include <linux/sched.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 8 | #include <linux/sched/debug.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 9 | #include <linux/sched/task_stack.h> |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 10 | #include <linux/stacktrace.h> |
Paul Gortmaker | 73bc256 | 2011-07-23 16:30:40 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 12 | #include <asm/stacktrace.h> |
| 13 | |
| 14 | /* |
| 15 | * Save stack-backtrace addresses into a stack_trace buffer: |
| 16 | */ |
| 17 | static void save_raw_context_stack(struct stack_trace *trace, |
Aaro Koskinen | e1e1611 | 2015-10-23 15:39:02 +0300 | [diff] [blame] | 18 | unsigned long reg29, int savesched) |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 19 | { |
| 20 | unsigned long *sp = (unsigned long *)reg29; |
| 21 | unsigned long addr; |
| 22 | |
| 23 | while (!kstack_end(sp)) { |
| 24 | addr = *sp++; |
Aaro Koskinen | e1e1611 | 2015-10-23 15:39:02 +0300 | [diff] [blame] | 25 | if (__kernel_text_address(addr) && |
| 26 | (savesched || !in_sched_functions(addr))) { |
Atsushi Nemoto | 2312669 | 2006-09-28 19:15:33 +0900 | [diff] [blame] | 27 | if (trace->skip > 0) |
| 28 | trace->skip--; |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 29 | else |
Atsushi Nemoto | 2312669 | 2006-09-28 19:15:33 +0900 | [diff] [blame] | 30 | trace->entries[trace->nr_entries++] = addr; |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 31 | if (trace->nr_entries >= trace->max_entries) |
| 32 | break; |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 37 | static void save_context_stack(struct stack_trace *trace, |
Aaro Koskinen | e1e1611 | 2015-10-23 15:39:02 +0300 | [diff] [blame] | 38 | struct task_struct *tsk, struct pt_regs *regs, int savesched) |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 39 | { |
| 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 Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 44 | |
| 45 | if (raw_show_trace || !__kernel_text_address(pc)) { |
Atsushi Nemoto | 1924600 | 2006-09-29 18:02:51 +0900 | [diff] [blame] | 46 | unsigned long stack_page = |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 47 | (unsigned long)task_stack_page(tsk); |
Atsushi Nemoto | 2312669 | 2006-09-28 19:15:33 +0900 | [diff] [blame] | 48 | if (stack_page && sp >= stack_page && |
| 49 | sp <= stack_page + THREAD_SIZE - 32) |
Aaro Koskinen | e1e1611 | 2015-10-23 15:39:02 +0300 | [diff] [blame] | 50 | save_raw_context_stack(trace, sp, savesched); |
Atsushi Nemoto | 1924600 | 2006-09-29 18:02:51 +0900 | [diff] [blame] | 51 | return; |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 52 | } |
| 53 | do { |
Aaro Koskinen | e1e1611 | 2015-10-23 15:39:02 +0300 | [diff] [blame] | 54 | 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 Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 62 | pc = unwind_stack(tsk, &sp, pc, &ra); |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 63 | } while (pc); |
| 64 | #else |
Aaro Koskinen | e1e1611 | 2015-10-23 15:39:02 +0300 | [diff] [blame] | 65 | save_raw_context_stack(trace, sp, savesched); |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 66 | #endif |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Save stack-backtrace addresses into a stack_trace buffer. |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 71 | */ |
Christoph Hellwig | ab1b6f0 | 2007-05-08 00:23:29 -0700 | [diff] [blame] | 72 | void save_stack_trace(struct stack_trace *trace) |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 73 | { |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 74 | save_stack_trace_tsk(current, trace); |
| 75 | } |
| 76 | EXPORT_SYMBOL_GPL(save_stack_trace); |
| 77 | |
| 78 | void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) |
| 79 | { |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 80 | struct pt_regs dummyregs; |
| 81 | struct pt_regs *regs = &dummyregs; |
| 82 | |
| 83 | WARN_ON(trace->nr_entries || !trace->max_entries); |
| 84 | |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 85 | 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 Koskinen | e1e1611 | 2015-10-23 15:39:02 +0300 | [diff] [blame] | 91 | save_context_stack(trace, tsk, regs, tsk == current); |
Atsushi Nemoto | 1df0f0f | 2006-09-26 23:44:01 +0900 | [diff] [blame] | 92 | } |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 93 | EXPORT_SYMBOL_GPL(save_stack_trace_tsk); |