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