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>
21 #include <linux/ftrace.h>
23 #ifdef CONFIG_FRAME_POINTER
30 static void notrace
walk_stackframe(struct task_struct
*task
,
31 struct pt_regs
*regs
, bool (*fn
)(unsigned long, void *), void *arg
)
33 unsigned long fp
, sp
, pc
;
39 } else if (task
== NULL
|| task
== current
) {
40 const register unsigned long current_sp
__asm__ ("sp");
41 fp
= (unsigned long)__builtin_frame_address(0);
43 pc
= (unsigned long)walk_stackframe
;
45 /* task blocked in __switch_to */
46 fp
= task
->thread
.s
[0];
52 unsigned long low
, high
;
53 struct stackframe
*frame
;
55 if (unlikely(!__kernel_text_address(pc
) || fn(pc
, arg
)))
58 /* Validate frame pointer */
59 low
= sp
+ sizeof(struct stackframe
);
60 high
= ALIGN(sp
, THREAD_SIZE
);
61 if (unlikely(fp
< low
|| fp
> high
|| fp
& 0x7))
63 /* Unwind stack frame */
64 frame
= (struct stackframe
*)fp
- 1;
67 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
68 pc
= ftrace_graph_ret_addr(current
, NULL
, frame
->ra
,
69 (unsigned long *)(fp
- 8));
76 #else /* !CONFIG_FRAME_POINTER */
78 static void notrace
walk_stackframe(struct task_struct
*task
,
79 struct pt_regs
*regs
, bool (*fn
)(unsigned long, void *), void *arg
)
87 } else if (task
== NULL
|| task
== current
) {
88 const register unsigned long current_sp
__asm__ ("sp");
90 pc
= (unsigned long)walk_stackframe
;
92 /* task blocked in __switch_to */
97 if (unlikely(sp
& 0x7))
100 ksp
= (unsigned long *)sp
;
101 while (!kstack_end(ksp
)) {
102 if (__kernel_text_address(pc
) && unlikely(fn(pc
, arg
)))
108 #endif /* CONFIG_FRAME_POINTER */
111 static bool print_trace_address(unsigned long pc
, void *arg
)
117 void show_stack(struct task_struct
*task
, unsigned long *sp
)
119 pr_cont("Call Trace:\n");
120 walk_stackframe(task
, NULL
, print_trace_address
, NULL
);
124 static bool save_wchan(unsigned long pc
, void *arg
)
126 if (!in_sched_functions(pc
)) {
127 unsigned long *p
= arg
;
134 unsigned long get_wchan(struct task_struct
*task
)
136 unsigned long pc
= 0;
138 if (likely(task
&& task
!= current
&& task
->state
!= TASK_RUNNING
))
139 walk_stackframe(task
, NULL
, save_wchan
, &pc
);
144 #ifdef CONFIG_STACKTRACE
146 static bool __save_trace(unsigned long pc
, void *arg
, bool nosched
)
148 struct stack_trace
*trace
= arg
;
150 if (unlikely(nosched
&& in_sched_functions(pc
)))
152 if (unlikely(trace
->skip
> 0)) {
157 trace
->entries
[trace
->nr_entries
++] = pc
;
158 return (trace
->nr_entries
>= trace
->max_entries
);
161 static bool save_trace(unsigned long pc
, void *arg
)
163 return __save_trace(pc
, arg
, false);
167 * Save stack-backtrace addresses into a stack_trace buffer.
169 void save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
171 walk_stackframe(tsk
, NULL
, save_trace
, trace
);
172 if (trace
->nr_entries
< trace
->max_entries
)
173 trace
->entries
[trace
->nr_entries
++] = ULONG_MAX
;
175 EXPORT_SYMBOL_GPL(save_stack_trace_tsk
);
177 void save_stack_trace(struct stack_trace
*trace
)
179 save_stack_trace_tsk(NULL
, trace
);
181 EXPORT_SYMBOL_GPL(save_stack_trace
);
183 #endif /* CONFIG_STACKTRACE */