2011-07-22 22:58:34 +08:00
|
|
|
#include <linux/export.h>
|
2007-04-28 16:59:37 +08:00
|
|
|
#include <linux/sched.h>
|
2017-02-09 01:51:35 +08:00
|
|
|
#include <linux/sched/debug.h>
|
2007-04-28 16:59:37 +08:00
|
|
|
#include <linux/stacktrace.h>
|
|
|
|
|
2009-02-11 20:07:53 +08:00
|
|
|
#include <asm/stacktrace.h>
|
2014-05-03 23:17:16 +08:00
|
|
|
#include <asm/traps.h>
|
2009-02-11 20:07:53 +08:00
|
|
|
|
|
|
|
#if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
|
|
|
|
/*
|
|
|
|
* Unwind the current stack frame and store the new register values in the
|
|
|
|
* structure passed as argument. Unwinding is equivalent to a function return,
|
|
|
|
* hence the new PC value rather than LR should be used for backtrace.
|
|
|
|
*
|
|
|
|
* With framepointer enabled, a simple function prologue looks like this:
|
|
|
|
* mov ip, sp
|
|
|
|
* stmdb sp!, {fp, ip, lr, pc}
|
|
|
|
* sub fp, ip, #4
|
|
|
|
*
|
|
|
|
* A simple function epilogue looks like this:
|
|
|
|
* ldm sp, {fp, sp, pc}
|
|
|
|
*
|
|
|
|
* Note that with framepointer enabled, even the leaf functions have the same
|
|
|
|
* prologue and epilogue, therefore we can ignore the LR value in this case.
|
|
|
|
*/
|
2009-07-21 16:56:27 +08:00
|
|
|
int notrace unwind_frame(struct stackframe *frame)
|
2007-04-28 16:59:37 +08:00
|
|
|
{
|
2009-02-11 20:07:53 +08:00
|
|
|
unsigned long high, low;
|
|
|
|
unsigned long fp = frame->fp;
|
2007-04-28 16:59:37 +08:00
|
|
|
|
2009-02-11 20:07:53 +08:00
|
|
|
/* only go to a higher address on the stack */
|
|
|
|
low = frame->sp;
|
2010-11-05 01:22:51 +08:00
|
|
|
high = ALIGN(low, THREAD_SIZE);
|
2007-04-28 16:59:37 +08:00
|
|
|
|
2009-02-11 20:07:53 +08:00
|
|
|
/* check current frame pointer is within bounds */
|
2013-12-05 21:23:48 +08:00
|
|
|
if (fp < low + 12 || fp > high - 4)
|
2009-02-11 20:07:53 +08:00
|
|
|
return -EINVAL;
|
2007-04-28 16:59:37 +08:00
|
|
|
|
2009-02-11 20:07:53 +08:00
|
|
|
/* restore the registers from the stack frame */
|
|
|
|
frame->fp = *(unsigned long *)(fp - 12);
|
|
|
|
frame->sp = *(unsigned long *)(fp - 8);
|
|
|
|
frame->pc = *(unsigned long *)(fp - 4);
|
2007-04-28 16:59:37 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-11 20:07:53 +08:00
|
|
|
#endif
|
|
|
|
|
2009-07-21 16:56:27 +08:00
|
|
|
void notrace walk_stackframe(struct stackframe *frame,
|
2009-02-11 20:07:53 +08:00
|
|
|
int (*fn)(struct stackframe *, void *), void *data)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (fn(frame, data))
|
|
|
|
break;
|
|
|
|
ret = unwind_frame(frame);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-05-16 03:37:20 +08:00
|
|
|
EXPORT_SYMBOL(walk_stackframe);
|
2007-04-28 16:59:37 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_STACKTRACE
|
|
|
|
struct stack_trace_data {
|
|
|
|
struct stack_trace *trace;
|
2014-05-03 23:17:16 +08:00
|
|
|
unsigned long last_pc;
|
2008-04-24 13:31:46 +08:00
|
|
|
unsigned int no_sched_functions;
|
2007-04-28 16:59:37 +08:00
|
|
|
unsigned int skip;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int save_trace(struct stackframe *frame, void *d)
|
|
|
|
{
|
|
|
|
struct stack_trace_data *data = d;
|
|
|
|
struct stack_trace *trace = data->trace;
|
2014-05-03 23:17:16 +08:00
|
|
|
struct pt_regs *regs;
|
2009-02-11 20:07:53 +08:00
|
|
|
unsigned long addr = frame->pc;
|
2007-04-28 16:59:37 +08:00
|
|
|
|
2008-04-24 13:31:46 +08:00
|
|
|
if (data->no_sched_functions && in_sched_functions(addr))
|
|
|
|
return 0;
|
2007-04-28 16:59:37 +08:00
|
|
|
if (data->skip) {
|
|
|
|
data->skip--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-24 13:31:46 +08:00
|
|
|
trace->entries[trace->nr_entries++] = addr;
|
2007-04-28 16:59:37 +08:00
|
|
|
|
2014-05-03 23:17:16 +08:00
|
|
|
if (trace->nr_entries >= trace->max_entries)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* in_exception_text() is designed to test if the PC is one of
|
|
|
|
* the functions which has an exception stack above it, but
|
|
|
|
* unfortunately what is in frame->pc is the return LR value,
|
|
|
|
* not the saved PC value. So, we need to track the previous
|
|
|
|
* frame PC value when doing this.
|
|
|
|
*/
|
|
|
|
addr = data->last_pc;
|
|
|
|
data->last_pc = frame->pc;
|
|
|
|
if (!in_exception_text(addr))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
regs = (struct pt_regs *)frame->sp;
|
|
|
|
|
|
|
|
trace->entries[trace->nr_entries++] = regs->ARM_pc;
|
|
|
|
|
2007-04-28 16:59:37 +08:00
|
|
|
return trace->nr_entries >= trace->max_entries;
|
|
|
|
}
|
|
|
|
|
2014-05-03 18:03:28 +08:00
|
|
|
/* This must be noinline to so that our skip calculation works correctly */
|
|
|
|
static noinline void __save_stack_trace(struct task_struct *tsk,
|
|
|
|
struct stack_trace *trace, unsigned int nosched)
|
2007-04-28 16:59:37 +08:00
|
|
|
{
|
|
|
|
struct stack_trace_data data;
|
2009-02-11 20:07:53 +08:00
|
|
|
struct stackframe frame;
|
2007-04-28 16:59:37 +08:00
|
|
|
|
|
|
|
data.trace = trace;
|
2014-05-03 23:17:16 +08:00
|
|
|
data.last_pc = ULONG_MAX;
|
2007-04-28 16:59:37 +08:00
|
|
|
data.skip = trace->skip;
|
2014-05-03 18:03:28 +08:00
|
|
|
data.no_sched_functions = nosched;
|
2008-04-24 13:31:46 +08:00
|
|
|
|
|
|
|
if (tsk != current) {
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/*
|
2011-01-15 17:27:04 +08:00
|
|
|
* What guarantees do we have here that 'tsk' is not
|
|
|
|
* running on another CPU? For now, ignore it as we
|
|
|
|
* can't guarantee we won't explode.
|
2008-04-24 13:31:46 +08:00
|
|
|
*/
|
2011-01-15 17:27:04 +08:00
|
|
|
if (trace->nr_entries < trace->max_entries)
|
|
|
|
trace->entries[trace->nr_entries++] = ULONG_MAX;
|
|
|
|
return;
|
2008-04-24 13:31:46 +08:00
|
|
|
#else
|
2009-02-11 20:07:53 +08:00
|
|
|
frame.fp = thread_saved_fp(tsk);
|
|
|
|
frame.sp = thread_saved_sp(tsk);
|
|
|
|
frame.lr = 0; /* recovered from the stack */
|
|
|
|
frame.pc = thread_saved_pc(tsk);
|
2008-04-24 13:31:46 +08:00
|
|
|
#endif
|
|
|
|
} else {
|
2014-05-03 18:03:28 +08:00
|
|
|
/* We don't want this function nor the caller */
|
|
|
|
data.skip += 2;
|
2009-02-11 20:07:53 +08:00
|
|
|
frame.fp = (unsigned long)__builtin_frame_address(0);
|
2014-09-27 07:31:08 +08:00
|
|
|
frame.sp = current_stack_pointer;
|
2009-02-11 20:07:53 +08:00
|
|
|
frame.lr = (unsigned long)__builtin_return_address(0);
|
2014-05-03 18:03:28 +08:00
|
|
|
frame.pc = (unsigned long)__save_stack_trace;
|
2008-04-24 13:31:46 +08:00
|
|
|
}
|
2007-04-28 16:59:37 +08:00
|
|
|
|
2009-02-11 20:07:53 +08:00
|
|
|
walk_stackframe(&frame, save_trace, &data);
|
2008-04-24 13:31:46 +08:00
|
|
|
if (trace->nr_entries < trace->max_entries)
|
|
|
|
trace->entries[trace->nr_entries++] = ULONG_MAX;
|
|
|
|
}
|
|
|
|
|
2014-05-04 23:27:41 +08:00
|
|
|
void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
|
|
|
|
{
|
|
|
|
struct stack_trace_data data;
|
|
|
|
struct stackframe frame;
|
|
|
|
|
|
|
|
data.trace = trace;
|
|
|
|
data.skip = trace->skip;
|
|
|
|
data.no_sched_functions = 0;
|
|
|
|
|
|
|
|
frame.fp = regs->ARM_fp;
|
|
|
|
frame.sp = regs->ARM_sp;
|
|
|
|
frame.lr = regs->ARM_lr;
|
|
|
|
frame.pc = regs->ARM_pc;
|
|
|
|
|
|
|
|
walk_stackframe(&frame, save_trace, &data);
|
|
|
|
if (trace->nr_entries < trace->max_entries)
|
|
|
|
trace->entries[trace->nr_entries++] = ULONG_MAX;
|
|
|
|
}
|
|
|
|
|
2014-05-03 18:03:28 +08:00
|
|
|
void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
|
|
|
|
{
|
|
|
|
__save_stack_trace(tsk, trace, 1);
|
|
|
|
}
|
2017-08-08 02:44:01 +08:00
|
|
|
EXPORT_SYMBOL(save_stack_trace_tsk);
|
2014-05-03 18:03:28 +08:00
|
|
|
|
2008-04-24 13:31:46 +08:00
|
|
|
void save_stack_trace(struct stack_trace *trace)
|
|
|
|
{
|
2014-05-03 18:03:28 +08:00
|
|
|
__save_stack_trace(current, trace, 0);
|
2007-04-28 16:59:37 +08:00
|
|
|
}
|
2008-07-03 15:17:55 +08:00
|
|
|
EXPORT_SYMBOL_GPL(save_stack_trace);
|
2007-04-28 16:59:37 +08:00
|
|
|
#endif
|