2 * Copyright (C) 2008 ARM Limited
3 * Copyright (C) 2014 Regents of the University of California
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/export.h>
16 #include <linux/kallsyms.h>
17 #include <linux/sched.h>
18 #include <linux/sched/debug.h>
19 #include <linux/sched/task_stack.h>
20 #include <linux/stacktrace.h>
22 #ifdef CONFIG_FRAME_POINTER
29 static void notrace
walk_stackframe(struct task_struct
*task
,
30 struct pt_regs
*regs
, bool (*fn
)(unsigned long, void *), void *arg
)
32 unsigned long fp
, sp
, pc
;
38 } else if (task
== NULL
|| task
== current
) {
39 const register unsigned long current_sp
__asm__ ("sp");
40 fp
= (unsigned long)__builtin_frame_address(0);
42 pc
= (unsigned long)walk_stackframe
;
44 /* task blocked in __switch_to */
45 fp
= task
->thread
.s
[0];
51 unsigned long low
, high
;
52 struct stackframe
*frame
;
54 if (unlikely(!__kernel_text_address(pc
) || fn(pc
, arg
)))
57 /* Validate frame pointer */
58 low
= sp
+ sizeof(struct stackframe
);
59 high
= ALIGN(sp
, THREAD_SIZE
);
60 if (unlikely(fp
< low
|| fp
> high
|| fp
& 0x7))
62 /* Unwind stack frame */
63 frame
= (struct stackframe
*)fp
- 1;
70 #else /* !CONFIG_FRAME_POINTER */
72 static void notrace
walk_stackframe(struct task_struct
*task
,
73 struct pt_regs
*regs
, bool (*fn
)(unsigned long, void *), void *arg
)
81 } else if (task
== NULL
|| task
== current
) {
82 const register unsigned long current_sp
__asm__ ("sp");
84 pc
= (unsigned long)walk_stackframe
;
86 /* task blocked in __switch_to */
91 if (unlikely(sp
& 0x7))
94 ksp
= (unsigned long *)sp
;
95 while (!kstack_end(ksp
)) {
96 if (__kernel_text_address(pc
) && unlikely(fn(pc
, arg
)))
102 #endif /* CONFIG_FRAME_POINTER */
105 static bool print_trace_address(unsigned long pc
, void *arg
)
111 void show_stack(struct task_struct
*task
, unsigned long *sp
)
113 pr_cont("Call Trace:\n");
114 walk_stackframe(task
, NULL
, print_trace_address
, NULL
);
118 static bool save_wchan(unsigned long pc
, void *arg
)
120 if (!in_sched_functions(pc
)) {
121 unsigned long *p
= arg
;
128 unsigned long get_wchan(struct task_struct
*task
)
130 unsigned long pc
= 0;
132 if (likely(task
&& task
!= current
&& task
->state
!= TASK_RUNNING
))
133 walk_stackframe(task
, NULL
, save_wchan
, &pc
);
138 #ifdef CONFIG_STACKTRACE
140 static bool __save_trace(unsigned long pc
, void *arg
, bool nosched
)
142 struct stack_trace
*trace
= arg
;
144 if (unlikely(nosched
&& in_sched_functions(pc
)))
146 if (unlikely(trace
->skip
> 0)) {
151 trace
->entries
[trace
->nr_entries
++] = pc
;
152 return (trace
->nr_entries
>= trace
->max_entries
);
155 static bool save_trace(unsigned long pc
, void *arg
)
157 return __save_trace(pc
, arg
, false);
161 * Save stack-backtrace addresses into a stack_trace buffer.
163 void save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
165 walk_stackframe(tsk
, NULL
, save_trace
, trace
);
166 if (trace
->nr_entries
< trace
->max_entries
)
167 trace
->entries
[trace
->nr_entries
++] = ULONG_MAX
;
169 EXPORT_SYMBOL_GPL(save_stack_trace_tsk
);
171 void save_stack_trace(struct stack_trace
*trace
)
173 save_stack_trace_tsk(NULL
, trace
);
175 EXPORT_SYMBOL_GPL(save_stack_trace
);
177 #endif /* CONFIG_STACKTRACE */