2 * Stack trace management functions
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6 #include <linux/sched.h>
7 #include <linux/sched/debug.h>
8 #include <linux/sched/task_stack.h>
9 #include <linux/stacktrace.h>
10 #include <linux/export.h>
11 #include <asm/stacktrace.h>
14 * Save stack-backtrace addresses into a stack_trace buffer:
16 static void save_raw_context_stack(struct stack_trace
*trace
,
17 unsigned long reg29
, int savesched
)
19 unsigned long *sp
= (unsigned long *)reg29
;
22 while (!kstack_end(sp
)) {
24 if (__kernel_text_address(addr
) &&
25 (savesched
|| !in_sched_functions(addr
))) {
29 trace
->entries
[trace
->nr_entries
++] = addr
;
30 if (trace
->nr_entries
>= trace
->max_entries
)
36 static void save_context_stack(struct stack_trace
*trace
,
37 struct task_struct
*tsk
, struct pt_regs
*regs
, int savesched
)
39 unsigned long sp
= regs
->regs
[29];
40 #ifdef CONFIG_KALLSYMS
41 unsigned long ra
= regs
->regs
[31];
42 unsigned long pc
= regs
->cp0_epc
;
44 if (raw_show_trace
|| !__kernel_text_address(pc
)) {
45 unsigned long stack_page
=
46 (unsigned long)task_stack_page(tsk
);
47 if (stack_page
&& sp
>= stack_page
&&
48 sp
<= stack_page
+ THREAD_SIZE
- 32)
49 save_raw_context_stack(trace
, sp
, savesched
);
53 if (savesched
|| !in_sched_functions(pc
)) {
57 trace
->entries
[trace
->nr_entries
++] = pc
;
58 if (trace
->nr_entries
>= trace
->max_entries
)
61 pc
= unwind_stack(tsk
, &sp
, pc
, &ra
);
64 save_raw_context_stack(trace
, sp
, savesched
);
69 * Save stack-backtrace addresses into a stack_trace buffer.
71 void save_stack_trace(struct stack_trace
*trace
)
73 save_stack_trace_tsk(current
, trace
);
75 EXPORT_SYMBOL_GPL(save_stack_trace
);
77 void save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
79 struct pt_regs dummyregs
;
80 struct pt_regs
*regs
= &dummyregs
;
82 WARN_ON(trace
->nr_entries
|| !trace
->max_entries
);
85 regs
->regs
[29] = tsk
->thread
.reg29
;
87 regs
->cp0_epc
= tsk
->thread
.reg31
;
89 prepare_frametrace(regs
);
90 save_context_stack(trace
, tsk
, regs
, tsk
== current
);
92 EXPORT_SYMBOL_GPL(save_stack_trace_tsk
);