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 = *(unsigned long *)(fp + 4);
56 /* check current frame pointer is within bounds */
57 if (fp < low + 12 || fp > high - 4)
60 /* restore the registers from the stack frame */
61 frame->fp = *(unsigned long *)(fp - 12);
62 frame->sp = *(unsigned long *)(fp - 8);
63 frame->pc = *(unsigned long *)(fp - 4);
70 void notrace walk_stackframe(struct stackframe *frame,
71 int (*fn)(struct stackframe *, void *), void *data)
78 ret = unwind_frame(frame);
83 EXPORT_SYMBOL(walk_stackframe);
85 #ifdef CONFIG_STACKTRACE
86 struct stack_trace_data {
87 struct stack_trace *trace;
88 unsigned long last_pc;
89 unsigned int no_sched_functions;
93 static int save_trace(struct stackframe *frame, void *d)
95 struct stack_trace_data *data = d;
96 struct stack_trace *trace = data->trace;
98 unsigned long addr = frame->pc;
100 if (data->no_sched_functions && in_sched_functions(addr))
107 trace->entries[trace->nr_entries++] = addr;
109 if (trace->nr_entries >= trace->max_entries)
113 * in_exception_text() is designed to test if the PC is one of
114 * the functions which has an exception stack above it, but
115 * unfortunately what is in frame->pc is the return LR value,
116 * not the saved PC value. So, we need to track the previous
117 * frame PC value when doing this.
119 addr = data->last_pc;
120 data->last_pc = frame->pc;
121 if (!in_exception_text(addr))
124 regs = (struct pt_regs *)frame->sp;
126 trace->entries[trace->nr_entries++] = regs->ARM_pc;
128 return trace->nr_entries >= trace->max_entries;
131 /* This must be noinline to so that our skip calculation works correctly */
132 static noinline void __save_stack_trace(struct task_struct *tsk,
133 struct stack_trace *trace, unsigned int nosched)
135 struct stack_trace_data data;
136 struct stackframe frame;
139 data.last_pc = ULONG_MAX;
140 data.skip = trace->skip;
141 data.no_sched_functions = nosched;
143 if (tsk != current) {
146 * What guarantees do we have here that 'tsk' is not
147 * running on another CPU? For now, ignore it as we
148 * can't guarantee we won't explode.
150 if (trace->nr_entries < trace->max_entries)
151 trace->entries[trace->nr_entries++] = ULONG_MAX;
154 frame.fp = thread_saved_fp(tsk);
155 frame.sp = thread_saved_sp(tsk);
156 frame.lr = 0; /* recovered from the stack */
157 frame.pc = thread_saved_pc(tsk);
160 /* We don't want this function nor the caller */
162 frame.fp = (unsigned long)__builtin_frame_address(0);
163 frame.sp = current_stack_pointer;
164 frame.lr = (unsigned long)__builtin_return_address(0);
165 frame.pc = (unsigned long)__save_stack_trace;
168 walk_stackframe(&frame, save_trace, &data);
169 if (trace->nr_entries < trace->max_entries)
170 trace->entries[trace->nr_entries++] = ULONG_MAX;
173 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
175 struct stack_trace_data data;
176 struct stackframe frame;
179 data.skip = trace->skip;
180 data.no_sched_functions = 0;
182 frame.fp = regs->ARM_fp;
183 frame.sp = regs->ARM_sp;
184 frame.lr = regs->ARM_lr;
185 frame.pc = regs->ARM_pc;
187 walk_stackframe(&frame, save_trace, &data);
188 if (trace->nr_entries < trace->max_entries)
189 trace->entries[trace->nr_entries++] = ULONG_MAX;
192 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
194 __save_stack_trace(tsk, trace, 1);
197 void save_stack_trace(struct stack_trace *trace)
199 __save_stack_trace(current, trace, 0);
201 EXPORT_SYMBOL_GPL(save_stack_trace);