1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008 ARM Limited
4 * Copyright (C) 2014 Regents of the University of California
7 #include <linux/export.h>
8 #include <linux/kallsyms.h>
9 #include <linux/sched.h>
10 #include <linux/sched/debug.h>
11 #include <linux/sched/task_stack.h>
12 #include <linux/stacktrace.h>
13 #include <linux/ftrace.h>
15 #ifdef CONFIG_FRAME_POINTER
22 register unsigned long sp_in_global
__asm__("sp");
24 void notrace
walk_stackframe(struct task_struct
*task
, struct pt_regs
*regs
,
25 bool (*fn
)(unsigned long, void *), void *arg
)
27 unsigned long fp
, sp
, pc
;
30 fp
= frame_pointer(regs
);
31 sp
= user_stack_pointer(regs
);
32 pc
= instruction_pointer(regs
);
33 } else if (task
== NULL
|| task
== current
) {
34 const register unsigned long current_sp
= sp_in_global
;
35 fp
= (unsigned long)__builtin_frame_address(0);
37 pc
= (unsigned long)walk_stackframe
;
39 /* task blocked in __switch_to */
40 fp
= task
->thread
.s
[0];
46 unsigned long low
, high
;
47 struct stackframe
*frame
;
49 if (unlikely(!__kernel_text_address(pc
) || fn(pc
, arg
)))
52 /* Validate frame pointer */
53 low
= sp
+ sizeof(struct stackframe
);
54 high
= ALIGN(sp
, THREAD_SIZE
);
55 if (unlikely(fp
< low
|| fp
> high
|| fp
& 0x7))
57 /* Unwind stack frame */
58 frame
= (struct stackframe
*)fp
- 1;
61 pc
= ftrace_graph_ret_addr(current
, NULL
, frame
->ra
,
62 (unsigned long *)(fp
- 8));
66 #else /* !CONFIG_FRAME_POINTER */
68 static void notrace
walk_stackframe(struct task_struct
*task
,
69 struct pt_regs
*regs
, bool (*fn
)(unsigned long, void *), void *arg
)
75 sp
= user_stack_pointer(regs
);
76 pc
= instruction_pointer(regs
);
77 } else if (task
== NULL
|| task
== current
) {
79 pc
= (unsigned long)walk_stackframe
;
81 /* task blocked in __switch_to */
86 if (unlikely(sp
& 0x7))
89 ksp
= (unsigned long *)sp
;
90 while (!kstack_end(ksp
)) {
91 if (__kernel_text_address(pc
) && unlikely(fn(pc
, arg
)))
97 #endif /* CONFIG_FRAME_POINTER */
100 static bool print_trace_address(unsigned long pc
, void *arg
)
106 void show_stack(struct task_struct
*task
, unsigned long *sp
)
108 pr_cont("Call Trace:\n");
109 walk_stackframe(task
, NULL
, print_trace_address
, NULL
);
113 static bool save_wchan(unsigned long pc
, void *arg
)
115 if (!in_sched_functions(pc
)) {
116 unsigned long *p
= arg
;
123 unsigned long get_wchan(struct task_struct
*task
)
125 unsigned long pc
= 0;
127 if (likely(task
&& task
!= current
&& task
->state
!= TASK_RUNNING
))
128 walk_stackframe(task
, NULL
, save_wchan
, &pc
);
133 #ifdef CONFIG_STACKTRACE
135 static bool __save_trace(unsigned long pc
, void *arg
, bool nosched
)
137 struct stack_trace
*trace
= arg
;
139 if (unlikely(nosched
&& in_sched_functions(pc
)))
141 if (unlikely(trace
->skip
> 0)) {
146 trace
->entries
[trace
->nr_entries
++] = pc
;
147 return (trace
->nr_entries
>= trace
->max_entries
);
150 static bool save_trace(unsigned long pc
, void *arg
)
152 return __save_trace(pc
, arg
, false);
156 * Save stack-backtrace addresses into a stack_trace buffer.
158 void save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
160 walk_stackframe(tsk
, NULL
, save_trace
, trace
);
162 EXPORT_SYMBOL_GPL(save_stack_trace_tsk
);
164 void save_stack_trace(struct stack_trace
*trace
)
166 save_stack_trace_tsk(NULL
, trace
);
168 EXPORT_SYMBOL_GPL(save_stack_trace
);
170 #endif /* CONFIG_STACKTRACE */