stacktrace.c 3.27 KB
Newer Older
1
#include <linux/export.h>
2 3 4
#include <linux/sched.h>
#include <linux/stacktrace.h>

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <asm/stacktrace.h>

#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.
 */
24
int notrace unwind_frame(struct stackframe *frame)
25
{
26 27
	unsigned long high, low;
	unsigned long fp = frame->fp;
28

29 30
	/* only go to a higher address on the stack */
	low = frame->sp;
31
	high = ALIGN(low, THREAD_SIZE);
32

33 34 35
	/* check current frame pointer is within bounds */
	if (fp < (low + 12) || fp + 4 >= high)
		return -EINVAL;
36

37 38 39 40
	/* 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);
41 42 43

	return 0;
}
44 45
#endif

46
void notrace walk_stackframe(struct stackframe *frame,
47 48 49 50 51 52 53 54 55 56 57 58
		     int (*fn)(struct stackframe *, void *), void *data)
{
	while (1) {
		int ret;

		if (fn(frame, data))
			break;
		ret = unwind_frame(frame);
		if (ret < 0)
			break;
	}
}
59
EXPORT_SYMBOL(walk_stackframe);
60 61 62 63

#ifdef CONFIG_STACKTRACE
struct stack_trace_data {
	struct stack_trace *trace;
Nicolas Pitre's avatar
Nicolas Pitre committed
64
	unsigned int no_sched_functions;
65 66 67 68 69 70 71
	unsigned int skip;
};

static int save_trace(struct stackframe *frame, void *d)
{
	struct stack_trace_data *data = d;
	struct stack_trace *trace = data->trace;
72
	unsigned long addr = frame->pc;
73

Nicolas Pitre's avatar
Nicolas Pitre committed
74 75
	if (data->no_sched_functions && in_sched_functions(addr))
		return 0;
76 77 78 79 80
	if (data->skip) {
		data->skip--;
		return 0;
	}

Nicolas Pitre's avatar
Nicolas Pitre committed
81
	trace->entries[trace->nr_entries++] = addr;
82 83 84 85

	return trace->nr_entries >= trace->max_entries;
}

Nicolas Pitre's avatar
Nicolas Pitre committed
86
void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
87 88
{
	struct stack_trace_data data;
89
	struct stackframe frame;
90 91 92

	data.trace = trace;
	data.skip = trace->skip;
Nicolas Pitre's avatar
Nicolas Pitre committed
93 94 95 96

	if (tsk != current) {
#ifdef CONFIG_SMP
		/*
97 98 99
		 * 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.
Nicolas Pitre's avatar
Nicolas Pitre committed
100
		 */
101 102 103
		if (trace->nr_entries < trace->max_entries)
			trace->entries[trace->nr_entries++] = ULONG_MAX;
		return;
Nicolas Pitre's avatar
Nicolas Pitre committed
104 105
#else
		data.no_sched_functions = 1;
106 107 108 109
		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);
Nicolas Pitre's avatar
Nicolas Pitre committed
110 111
#endif
	} else {
112 113
		register unsigned long current_sp asm ("sp");

Nicolas Pitre's avatar
Nicolas Pitre committed
114
		data.no_sched_functions = 0;
115 116 117 118
		frame.fp = (unsigned long)__builtin_frame_address(0);
		frame.sp = current_sp;
		frame.lr = (unsigned long)__builtin_return_address(0);
		frame.pc = (unsigned long)save_stack_trace_tsk;
Nicolas Pitre's avatar
Nicolas Pitre committed
119
	}
120

121
	walk_stackframe(&frame, save_trace, &data);
Nicolas Pitre's avatar
Nicolas Pitre committed
122 123 124 125 126 127 128
	if (trace->nr_entries < trace->max_entries)
		trace->entries[trace->nr_entries++] = ULONG_MAX;
}

void save_stack_trace(struct stack_trace *trace)
{
	save_stack_trace_tsk(current, trace);
129
}
130
EXPORT_SYMBOL_GPL(save_stack_trace);
131
#endif