1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only >> 2 /* >> 3 * Stack trace management functions >> 4 * >> 5 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp> >> 6 */ 2 #include <linux/sched.h> 7 #include <linux/sched.h> 3 #include <linux/sched/debug.h> 8 #include <linux/sched/debug.h> >> 9 #include <linux/sched/task_stack.h> 4 #include <linux/stacktrace.h> 10 #include <linux/stacktrace.h> 5 #include <linux/thread_info.h> << 6 #include <linux/ftrace.h> << 7 #include <linux/export.h> 11 #include <linux/export.h> 8 #include <asm/ptrace.h> << 9 #include <asm/stacktrace.h> 12 #include <asm/stacktrace.h> 10 13 11 #include "kstack.h" !! 14 /* 12 !! 15 * Save stack-backtrace addresses into a stack_trace buffer: 13 static void __save_stack_trace(struct thread_i !! 16 */ 14 struct stack_tr !! 17 static void save_raw_context_stack(struct stack_trace *trace, 15 bool skip_sched !! 18 unsigned long reg29, int savesched) 16 { 19 { 17 unsigned long ksp, fp; !! 20 unsigned long *sp = (unsigned long *)reg29; 18 #ifdef CONFIG_FUNCTION_GRAPH_TRACER !! 21 unsigned long addr; 19 struct task_struct *t; << 20 int graph = 0; << 21 #endif << 22 22 23 if (tp == current_thread_info()) { !! 23 while (!kstack_end(sp)) { 24 stack_trace_flush(); !! 24 addr = *sp++; 25 __asm__ __volatile__("mov %%fp !! 25 if (__kernel_text_address(addr) && 26 } else { !! 26 (savesched || !in_sched_functions(addr))) { 27 ksp = tp->ksp; !! 27 if (trace->skip > 0) >> 28 trace->skip--; >> 29 else >> 30 trace->entries[trace->nr_entries++] = addr; >> 31 if (trace->nr_entries >= trace->max_entries) >> 32 break; >> 33 } 28 } 34 } >> 35 } 29 36 30 fp = ksp + STACK_BIAS; !! 37 static void save_context_stack(struct stack_trace *trace, 31 #ifdef CONFIG_FUNCTION_GRAPH_TRACER !! 38 struct task_struct *tsk, struct pt_regs *regs, int savesched) 32 t = tp->task; !! 39 { 33 #endif !! 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; >> 44 >> 45 if (raw_show_trace || !__kernel_text_address(pc)) { >> 46 unsigned long stack_page = >> 47 (unsigned long)task_stack_page(tsk); >> 48 if (stack_page && sp >= stack_page && >> 49 sp <= stack_page + THREAD_SIZE - 32) >> 50 save_raw_context_stack(trace, sp, savesched); >> 51 return; >> 52 } 34 do { 53 do { 35 struct sparc_stackf *sf; !! 54 if (savesched || !in_sched_functions(pc)) { 36 struct pt_regs *regs; !! 55 if (trace->skip > 0) 37 unsigned long pc; !! 56 trace->skip--; 38 !! 57 else 39 if (!kstack_valid(tp, fp)) !! 58 trace->entries[trace->nr_entries++] = pc; 40 break; !! 59 if (trace->nr_entries >= trace->max_entries) 41 << 42 sf = (struct sparc_stackf *) f << 43 regs = (struct pt_regs *) (sf << 44 << 45 if (kstack_is_trap_frame(tp, r << 46 if (!(regs->tstate & T << 47 break; 60 break; 48 pc = regs->tpc; << 49 fp = regs->u_regs[UREG << 50 } else { << 51 pc = sf->callers_pc; << 52 fp = (unsigned long)sf << 53 } 61 } 54 !! 62 pc = unwind_stack(tsk, &sp, pc, &ra); 55 if (trace->skip > 0) !! 63 } while (pc); 56 trace->skip--; !! 64 #else 57 else if (!skip_sched || !in_sc !! 65 save_raw_context_stack(trace, sp, savesched); 58 trace->entries[trace-> << 59 #ifdef CONFIG_FUNCTION_GRAPH_TRACER << 60 if ((pc + 8UL) == (uns << 61 struct ftrace_ << 62 ret_stack = ft << 63 << 64 if (ret_stack) << 65 pc = r << 66 if (tr << 67 tr << 68 << 69 graph+ << 70 } << 71 } << 72 #endif 66 #endif 73 } << 74 } while (trace->nr_entries < trace->ma << 75 } 67 } 76 68 >> 69 /* >> 70 * Save stack-backtrace addresses into a stack_trace buffer. >> 71 */ 77 void save_stack_trace(struct stack_trace *trac 72 void save_stack_trace(struct stack_trace *trace) 78 { 73 { 79 __save_stack_trace(current_thread_info !! 74 save_stack_trace_tsk(current, trace); 80 } 75 } 81 EXPORT_SYMBOL_GPL(save_stack_trace); 76 EXPORT_SYMBOL_GPL(save_stack_trace); 82 77 83 void save_stack_trace_tsk(struct task_struct * 78 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) 84 { 79 { 85 struct thread_info *tp = task_thread_i !! 80 struct pt_regs dummyregs; >> 81 struct pt_regs *regs = &dummyregs; >> 82 >> 83 WARN_ON(trace->nr_entries || !trace->max_entries); 86 84 87 __save_stack_trace(tp, trace, true); !! 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); >> 91 save_context_stack(trace, tsk, regs, tsk == current); 88 } 92 } 89 EXPORT_SYMBOL_GPL(save_stack_trace_tsk); 93 EXPORT_SYMBOL_GPL(save_stack_trace_tsk); 90 94
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.