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 void notrace
walk_stackframe(struct task_struct
*task
, struct pt_regs
*regs
,
23 bool (*fn
)(unsigned long, void *), void *arg
)
25 unsigned long fp
, sp
, pc
;
28 fp
= frame_pointer(regs
);
29 sp
= user_stack_pointer(regs
);
30 pc
= instruction_pointer(regs
);
31 } else if (task
== NULL
|| task
== current
) {
32 const register unsigned long current_sp
__asm__ ("sp");
33 fp
= (unsigned long)__builtin_frame_address(0);
35 pc
= (unsigned long)walk_stackframe
;
37 /* task blocked in __switch_to */
38 fp
= task
->thread
.s
[0];
44 unsigned long low
, high
;
45 struct stackframe
*frame
;
47 if (unlikely(!__kernel_text_address(pc
) || fn(pc
, arg
)))
50 /* Validate frame pointer */
51 low
= sp
+ sizeof(struct stackframe
);
52 high
= ALIGN(sp
, THREAD_SIZE
);
53 if (unlikely(fp
< low
|| fp
> high
|| fp
& 0x7))
55 /* Unwind stack frame */
56 frame
= (struct stackframe
*)fp
- 1;
59 pc
= ftrace_graph_ret_addr(current
, NULL
, frame
->ra
,
60 (unsigned long *)(fp
- 8));
64 #else /* !CONFIG_FRAME_POINTER */
66 static void notrace
walk_stackframe(struct task_struct
*task
,
67 struct pt_regs
*regs
, bool (*fn
)(unsigned long, void *), void *arg
)
73 sp
= user_stack_pointer(regs
);
74 pc
= instruction_pointer(regs
);
75 } else if (task
== NULL
|| task
== current
) {
76 const register unsigned long current_sp
__asm__ ("sp");
78 pc
= (unsigned long)walk_stackframe
;
80 /* task blocked in __switch_to */
85 if (unlikely(sp
& 0x7))
88 ksp
= (unsigned long *)sp
;
89 while (!kstack_end(ksp
)) {
90 if (__kernel_text_address(pc
) && unlikely(fn(pc
, arg
)))
96 #endif /* CONFIG_FRAME_POINTER */
99 static bool print_trace_address(unsigned long pc
, void *arg
)
105 void show_stack(struct task_struct
*task
, unsigned long *sp
)
107 pr_cont("Call Trace:\n");
108 walk_stackframe(task
, NULL
, print_trace_address
, NULL
);
112 static bool save_wchan(unsigned long pc
, void *arg
)
114 if (!in_sched_functions(pc
)) {
115 unsigned long *p
= arg
;
122 unsigned long get_wchan(struct task_struct
*task
)
124 unsigned long pc
= 0;
126 if (likely(task
&& task
!= current
&& task
->state
!= TASK_RUNNING
))
127 walk_stackframe(task
, NULL
, save_wchan
, &pc
);
132 #ifdef CONFIG_STACKTRACE
134 static bool __save_trace(unsigned long pc
, void *arg
, bool nosched
)
136 struct stack_trace
*trace
= arg
;
138 if (unlikely(nosched
&& in_sched_functions(pc
)))
140 if (unlikely(trace
->skip
> 0)) {
145 trace
->entries
[trace
->nr_entries
++] = pc
;
146 return (trace
->nr_entries
>= trace
->max_entries
);
149 static bool save_trace(unsigned long pc
, void *arg
)
151 return __save_trace(pc
, arg
, false);
155 * Save stack-backtrace addresses into a stack_trace buffer.
157 void save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
159 walk_stackframe(tsk
, NULL
, save_trace
, trace
);
161 EXPORT_SYMBOL_GPL(save_stack_trace_tsk
);
163 void save_stack_trace(struct stack_trace
*trace
)
165 save_stack_trace_tsk(NULL
, trace
);
167 EXPORT_SYMBOL_GPL(save_stack_trace
);
169 #endif /* CONFIG_STACKTRACE */