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 register unsigned long sp_in_global __asm__("sp");
19 #ifdef CONFIG_FRAME_POINTER
21 void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
22 bool (*fn)(void *, unsigned long), void *arg)
24 unsigned long fp, sp, pc;
27 fp = frame_pointer(regs);
28 sp = user_stack_pointer(regs);
29 pc = instruction_pointer(regs);
30 } else if (task == NULL || task == current) {
31 fp = (unsigned long)__builtin_frame_address(1);
32 sp = (unsigned long)__builtin_frame_address(0);
33 pc = (unsigned long)__builtin_return_address(0);
35 /* task blocked in __switch_to */
36 fp = task->thread.s[0];
42 unsigned long low, high;
43 struct stackframe *frame;
45 if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc)))
48 /* Validate frame pointer */
49 low = sp + sizeof(struct stackframe);
50 high = ALIGN(sp, THREAD_SIZE);
51 if (unlikely(fp < low || fp > high || fp & 0x7))
53 /* Unwind stack frame */
54 frame = (struct stackframe *)fp - 1;
56 if (regs && (regs->epc == pc) && (frame->fp & 0x7)) {
61 pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
62 (unsigned long *)(fp - 8));
68 #else /* !CONFIG_FRAME_POINTER */
70 void notrace walk_stackframe(struct task_struct *task,
71 struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg)
77 sp = user_stack_pointer(regs);
78 pc = instruction_pointer(regs);
79 } else if (task == NULL || task == current) {
81 pc = (unsigned long)walk_stackframe;
83 /* task blocked in __switch_to */
88 if (unlikely(sp & 0x7))
91 ksp = (unsigned long *)sp;
92 while (!kstack_end(ksp)) {
93 if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
99 #endif /* CONFIG_FRAME_POINTER */
101 static bool print_trace_address(void *arg, unsigned long pc)
103 const char *loglvl = arg;
105 print_ip_sym(loglvl, pc);
109 noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
112 walk_stackframe(task, regs, print_trace_address, (void *)loglvl);
115 void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
117 pr_cont("%sCall Trace:\n", loglvl);
118 dump_backtrace(NULL, task, loglvl);
121 static bool save_wchan(void *arg, unsigned long pc)
123 if (!in_sched_functions(pc)) {
124 unsigned long *p = arg;
131 unsigned long __get_wchan(struct task_struct *task)
133 unsigned long pc = 0;
135 if (!try_get_task_stack(task))
137 walk_stackframe(task, NULL, save_wchan, &pc);
138 put_task_stack(task);
142 noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
143 struct task_struct *task, struct pt_regs *regs)
145 walk_stackframe(task, regs, consume_entry, cookie);