1 #include <linux/export.h>
2 #include <linux/sched.h>
3 #include <linux/sched/debug.h>
4 #include <linux/stacktrace.h>
6 #include <asm/sections.h>
7 #include <asm/stacktrace.h>
10 #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
12 * Unwind the current stack frame and store the new register values in the
13 * structure passed as argument. Unwinding is equivalent to a function return,
14 * hence the new PC value rather than LR should be used for backtrace.
16 * With framepointer enabled, a simple function prologue looks like this:
18 * stmdb sp!, {fp, ip, lr, pc}
21 * A simple function epilogue looks like this:
22 * ldm sp, {fp, sp, pc}
24 * When compiled with clang, pc and sp are not pushed. A simple function
25 * prologue looks like this when built with clang:
31 * A simple function epilogue looks like this when built with clang:
37 * Note that with framepointer enabled, even the leaf functions have the same
38 * prologue and epilogue, therefore we can ignore the LR value in this case.
40 int notrace unwind_frame(struct stackframe *frame)
42 unsigned long high, low;
43 unsigned long fp = frame->fp;
45 /* only go to a higher address on the stack */
47 high = ALIGN(low, THREAD_SIZE);
49 #ifdef CONFIG_CC_IS_CLANG
50 /* check current frame pointer is within bounds */
51 if (fp < low + 4 || fp > high - 4)
54 frame->sp = frame->fp;
55 frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
56 frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 4));
58 /* check current frame pointer is within bounds */
59 if (fp < low + 12 || fp > high - 4)
62 /* restore the registers from the stack frame */
63 frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 12));
64 frame->sp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 8));
65 frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 4));
72 void notrace walk_stackframe(struct stackframe *frame,
73 int (*fn)(struct stackframe *, void *), void *data)
80 ret = unwind_frame(frame);
85 EXPORT_SYMBOL(walk_stackframe);
87 #ifdef CONFIG_STACKTRACE
88 struct stack_trace_data {
89 struct stack_trace *trace;
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)
113 if (!in_entry_text(frame->pc))
116 regs = (struct pt_regs *)frame->sp;
117 if ((unsigned long)®s[1] > ALIGN(frame->sp, THREAD_SIZE))
120 trace->entries[trace->nr_entries++] = regs->ARM_pc;
122 return trace->nr_entries >= trace->max_entries;
125 /* This must be noinline to so that our skip calculation works correctly */
126 static noinline void __save_stack_trace(struct task_struct *tsk,
127 struct stack_trace *trace, unsigned int nosched)
129 struct stack_trace_data data;
130 struct stackframe frame;
133 data.skip = trace->skip;
134 data.no_sched_functions = nosched;
136 if (tsk != current) {
139 * What guarantees do we have here that 'tsk' is not
140 * running on another CPU? For now, ignore it as we
141 * can't guarantee we won't explode.
143 if (trace->nr_entries < trace->max_entries)
144 trace->entries[trace->nr_entries++] = ULONG_MAX;
147 frame.fp = thread_saved_fp(tsk);
148 frame.sp = thread_saved_sp(tsk);
149 frame.lr = 0; /* recovered from the stack */
150 frame.pc = thread_saved_pc(tsk);
153 /* We don't want this function nor the caller */
155 frame.fp = (unsigned long)__builtin_frame_address(0);
156 frame.sp = current_stack_pointer;
157 frame.lr = (unsigned long)__builtin_return_address(0);
158 frame.pc = (unsigned long)__save_stack_trace;
161 walk_stackframe(&frame, save_trace, &data);
162 if (trace->nr_entries < trace->max_entries)
163 trace->entries[trace->nr_entries++] = ULONG_MAX;
166 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
168 struct stack_trace_data data;
169 struct stackframe frame;
172 data.skip = trace->skip;
173 data.no_sched_functions = 0;
175 frame.fp = regs->ARM_fp;
176 frame.sp = regs->ARM_sp;
177 frame.lr = regs->ARM_lr;
178 frame.pc = regs->ARM_pc;
180 walk_stackframe(&frame, save_trace, &data);
181 if (trace->nr_entries < trace->max_entries)
182 trace->entries[trace->nr_entries++] = ULONG_MAX;
185 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
187 __save_stack_trace(tsk, trace, 1);
189 EXPORT_SYMBOL(save_stack_trace_tsk);
191 void save_stack_trace(struct stack_trace *trace)
193 __save_stack_trace(current, trace, 0);
195 EXPORT_SYMBOL_GPL(save_stack_trace);