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 #include <asm/stacktrace.h>
17 #ifdef CONFIG_FRAME_POINTER
19 void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
20 bool (*fn)(void *, unsigned long), void *arg)
22 unsigned long fp, sp, pc;
26 fp = frame_pointer(regs);
27 sp = user_stack_pointer(regs);
28 pc = instruction_pointer(regs);
29 } else if (task == NULL || task == current) {
30 fp = (unsigned long)__builtin_frame_address(0);
31 sp = current_stack_pointer;
32 pc = (unsigned long)walk_stackframe;
34 /* task blocked in __switch_to */
35 fp = task->thread.s[0];
41 unsigned long low, high;
42 struct stackframe *frame;
44 if (unlikely(!__kernel_text_address(pc) || (level++ >= 1 && !fn(arg, pc))))
47 /* Validate frame pointer */
48 low = sp + sizeof(struct stackframe);
49 high = ALIGN(sp, THREAD_SIZE);
50 if (unlikely(fp < low || fp > high || fp & 0x7))
52 /* Unwind stack frame */
53 frame = (struct stackframe *)fp - 1;
55 if (regs && (regs->epc == pc) && (frame->fp & 0x7)) {
60 pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
61 (unsigned long *)(fp - 8));
67 #else /* !CONFIG_FRAME_POINTER */
69 void notrace walk_stackframe(struct task_struct *task,
70 struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg)
76 sp = user_stack_pointer(regs);
77 pc = instruction_pointer(regs);
78 } else if (task == NULL || task == current) {
79 sp = current_stack_pointer;
80 pc = (unsigned long)walk_stackframe;
82 /* task blocked in __switch_to */
87 if (unlikely(sp & 0x7))
90 ksp = (unsigned long *)sp;
91 while (!kstack_end(ksp)) {
92 if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
98 #endif /* CONFIG_FRAME_POINTER */
100 static bool print_trace_address(void *arg, unsigned long pc)
102 const char *loglvl = arg;
104 print_ip_sym(loglvl, pc);
108 noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
111 walk_stackframe(task, regs, print_trace_address, (void *)loglvl);
114 void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
116 pr_cont("%sCall Trace:\n", loglvl);
117 dump_backtrace(NULL, task, loglvl);
120 static bool save_wchan(void *arg, unsigned long pc)
122 if (!in_sched_functions(pc)) {
123 unsigned long *p = arg;
130 unsigned long __get_wchan(struct task_struct *task)
132 unsigned long pc = 0;
134 if (!try_get_task_stack(task))
136 walk_stackframe(task, NULL, save_wchan, &pc);
137 put_task_stack(task);
141 noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
142 struct task_struct *task, struct pt_regs *regs)
144 walk_stackframe(task, regs, consume_entry, cookie);