1 #include <linux/export.h>
2 #include <linux/sched.h>
3 #include <linux/stacktrace.h>
5 #include <asm/stacktrace.h>
8 #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
10 * Unwind the current stack frame and store the new register values in the
11 * structure passed as argument. Unwinding is equivalent to a function return,
12 * hence the new PC value rather than LR should be used for backtrace.
14 * With framepointer enabled, a simple function prologue looks like this:
16 * stmdb sp!, {fp, ip, lr, pc}
19 * A simple function epilogue looks like this:
20 * ldm sp, {fp, sp, pc}
22 * When compiled with clang, pc and sp are not pushed. A simple function
23 * prologue looks like this when built with clang:
29 * A simple function epilogue looks like this when built with clang:
35 * Note that with framepointer enabled, even the leaf functions have the same
36 * prologue and epilogue, therefore we can ignore the LR value in this case.
38 int notrace unwind_frame(struct stackframe *frame)
40 unsigned long high, low;
41 unsigned long fp = frame->fp;
43 /* only go to a higher address on the stack */
45 high = ALIGN(low, THREAD_SIZE);
47 #ifdef CONFIG_CC_IS_CLANG
48 /* check current frame pointer is within bounds */
49 if (fp < low + 4 || fp > high - 4)
52 frame->sp = frame->fp;
53 frame->fp = *(unsigned long *)(fp);
54 frame->pc = frame->lr;
55 frame->lr = *(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);
198 void save_stack_trace(struct stack_trace *trace)
200 __save_stack_trace(current, trace, 0);
202 EXPORT_SYMBOL_GPL(save_stack_trace);