1 #include <linux/export.h>
2 #include <linux/sched.h>
3 #include <linux/sched/debug.h>
4 #include <linux/stacktrace.h>
6 #include <asm/stacktrace.h>
9 #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
11 * Unwind the current stack frame and store the new register values in the
12 * structure passed as argument. Unwinding is equivalent to a function return,
13 * hence the new PC value rather than LR should be used for backtrace.
15 * With framepointer enabled, a simple function prologue looks like this:
17 * stmdb sp!, {fp, ip, lr, pc}
20 * A simple function epilogue looks like this:
21 * ldm sp, {fp, sp, pc}
23 * When compiled with clang, pc and sp are not pushed. A simple function
24 * prologue looks like this when built with clang:
30 * A simple function epilogue looks like this when built with clang:
36 * Note that with framepointer enabled, even the leaf functions have the same
37 * prologue and epilogue, therefore we can ignore the LR value in this case.
39 int notrace unwind_frame(struct stackframe *frame)
41 unsigned long high, low;
42 unsigned long fp = frame->fp;
44 /* only go to a higher address on the stack */
46 high = ALIGN(low, THREAD_SIZE);
48 #ifdef CONFIG_CC_IS_CLANG
49 /* check current frame pointer is within bounds */
50 if (fp < low + 4 || fp > high - 4)
53 frame->sp = frame->fp;
54 frame->fp = *(unsigned long *)(fp);
55 frame->pc = *(unsigned long *)(fp + 4);
57 /* check current frame pointer is within bounds */
58 if (fp < low + 12 || fp > high - 4)
61 /* restore the registers from the stack frame */
62 frame->fp = *(unsigned long *)(fp - 12);
63 frame->sp = *(unsigned long *)(fp - 8);
64 frame->pc = *(unsigned long *)(fp - 4);
71 void notrace walk_stackframe(struct stackframe *frame,
72 int (*fn)(struct stackframe *, void *), void *data)
79 ret = unwind_frame(frame);
84 EXPORT_SYMBOL(walk_stackframe);
86 #ifdef CONFIG_STACKTRACE
87 struct stack_trace_data {
88 struct stack_trace *trace;
89 unsigned long last_pc;
90 unsigned int no_sched_functions;
94 static int save_trace(struct stackframe *frame, void *d)
96 struct stack_trace_data *data = d;
97 struct stack_trace *trace = data->trace;
99 unsigned long addr = frame->pc;
101 if (data->no_sched_functions && in_sched_functions(addr))
108 trace->entries[trace->nr_entries++] = addr;
110 if (trace->nr_entries >= trace->max_entries)
114 * in_exception_text() is designed to test if the PC is one of
115 * the functions which has an exception stack above it, but
116 * unfortunately what is in frame->pc is the return LR value,
117 * not the saved PC value. So, we need to track the previous
118 * frame PC value when doing this.
120 addr = data->last_pc;
121 data->last_pc = frame->pc;
122 if (!in_exception_text(addr))
125 regs = (struct pt_regs *)frame->sp;
127 trace->entries[trace->nr_entries++] = regs->ARM_pc;
129 return trace->nr_entries >= trace->max_entries;
132 /* This must be noinline to so that our skip calculation works correctly */
133 static noinline void __save_stack_trace(struct task_struct *tsk,
134 struct stack_trace *trace, unsigned int nosched)
136 struct stack_trace_data data;
137 struct stackframe frame;
140 data.last_pc = ULONG_MAX;
141 data.skip = trace->skip;
142 data.no_sched_functions = nosched;
144 if (tsk != current) {
147 * What guarantees do we have here that 'tsk' is not
148 * running on another CPU? For now, ignore it as we
149 * can't guarantee we won't explode.
151 if (trace->nr_entries < trace->max_entries)
152 trace->entries[trace->nr_entries++] = ULONG_MAX;
155 frame.fp = thread_saved_fp(tsk);
156 frame.sp = thread_saved_sp(tsk);
157 frame.lr = 0; /* recovered from the stack */
158 frame.pc = thread_saved_pc(tsk);
161 /* We don't want this function nor the caller */
163 frame.fp = (unsigned long)__builtin_frame_address(0);
164 frame.sp = current_stack_pointer;
165 frame.lr = (unsigned long)__builtin_return_address(0);
166 frame.pc = (unsigned long)__save_stack_trace;
169 walk_stackframe(&frame, save_trace, &data);
170 if (trace->nr_entries < trace->max_entries)
171 trace->entries[trace->nr_entries++] = ULONG_MAX;
174 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
176 struct stack_trace_data data;
177 struct stackframe frame;
180 data.skip = trace->skip;
181 data.no_sched_functions = 0;
183 frame.fp = regs->ARM_fp;
184 frame.sp = regs->ARM_sp;
185 frame.lr = regs->ARM_lr;
186 frame.pc = regs->ARM_pc;
188 walk_stackframe(&frame, save_trace, &data);
189 if (trace->nr_entries < trace->max_entries)
190 trace->entries[trace->nr_entries++] = ULONG_MAX;
193 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
195 __save_stack_trace(tsk, trace, 1);
197 EXPORT_SYMBOL(save_stack_trace_tsk);
199 void save_stack_trace(struct stack_trace *trace)
201 __save_stack_trace(current, trace, 0);
203 EXPORT_SYMBOL_GPL(save_stack_trace);