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;
36 fp = frame_pointer(regs);
37 sp = user_stack_pointer(regs);
38 pc = instruction_pointer(regs);
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 pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
68 (unsigned long *)(fp - 8));
72 #else /* !CONFIG_FRAME_POINTER */
74 static void notrace walk_stackframe(struct task_struct *task,
75 struct pt_regs *regs, bool (*fn)(unsigned long, void *), void *arg)
81 sp = user_stack_pointer(regs);
82 pc = instruction_pointer(regs);
83 } else if (task == NULL || task == current) {
84 const register unsigned long current_sp __asm__ ("sp");
86 pc = (unsigned long)walk_stackframe;
88 /* task blocked in __switch_to */
93 if (unlikely(sp & 0x7))
96 ksp = (unsigned long *)sp;
97 while (!kstack_end(ksp)) {
98 if (__kernel_text_address(pc) && unlikely(fn(pc, arg)))
104 #endif /* CONFIG_FRAME_POINTER */
107 static bool print_trace_address(unsigned long pc, void *arg)
113 void show_stack(struct task_struct *task, unsigned long *sp)
115 pr_cont("Call Trace:\n");
116 walk_stackframe(task, NULL, print_trace_address, NULL);
120 static bool save_wchan(unsigned long pc, void *arg)
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 (likely(task && task != current && task->state != TASK_RUNNING))
135 walk_stackframe(task, NULL, save_wchan, &pc);
140 #ifdef CONFIG_STACKTRACE
142 static bool __save_trace(unsigned long pc, void *arg, bool nosched)
144 struct stack_trace *trace = arg;
146 if (unlikely(nosched && in_sched_functions(pc)))
148 if (unlikely(trace->skip > 0)) {
153 trace->entries[trace->nr_entries++] = pc;
154 return (trace->nr_entries >= trace->max_entries);
157 static bool save_trace(unsigned long pc, void *arg)
159 return __save_trace(pc, arg, false);
163 * Save stack-backtrace addresses into a stack_trace buffer.
165 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
167 walk_stackframe(tsk, NULL, save_trace, trace);
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 */