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

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

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

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

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

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

	return 0;
}
46 47
#endif

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

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

#ifdef CONFIG_STACKTRACE
struct stack_trace_data {
	struct stack_trace *trace;
66
	unsigned long last_pc;
N
Nicolas Pitre 已提交
67
	unsigned int no_sched_functions;
68 69 70 71 72 73 74
	unsigned int skip;
};

static int save_trace(struct stackframe *frame, void *d)
{
	struct stack_trace_data *data = d;
	struct stack_trace *trace = data->trace;
75
	struct pt_regs *regs;
76
	unsigned long addr = frame->pc;
77

N
Nicolas Pitre 已提交
78 79
	if (data->no_sched_functions && in_sched_functions(addr))
		return 0;
80 81 82 83 84
	if (data->skip) {
		data->skip--;
		return 0;
	}

N
Nicolas Pitre 已提交
85
	trace->entries[trace->nr_entries++] = addr;
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	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;

106 107 108
	return trace->nr_entries >= trace->max_entries;
}

109 110 111
/* 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)
112 113
{
	struct stack_trace_data data;
114
	struct stackframe frame;
115 116

	data.trace = trace;
117
	data.last_pc = ULONG_MAX;
118
	data.skip = trace->skip;
119
	data.no_sched_functions = nosched;
N
Nicolas Pitre 已提交
120 121 122 123

	if (tsk != current) {
#ifdef CONFIG_SMP
		/*
R
Russell King 已提交
124 125 126
		 * 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.
N
Nicolas Pitre 已提交
127
		 */
R
Russell King 已提交
128 129 130
		if (trace->nr_entries < trace->max_entries)
			trace->entries[trace->nr_entries++] = ULONG_MAX;
		return;
N
Nicolas Pitre 已提交
131
#else
132 133 134 135
		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);
N
Nicolas Pitre 已提交
136 137
#endif
	} else {
138 139
		/* We don't want this function nor the caller */
		data.skip += 2;
140
		frame.fp = (unsigned long)__builtin_frame_address(0);
141
		frame.sp = current_stack_pointer;
142
		frame.lr = (unsigned long)__builtin_return_address(0);
143
		frame.pc = (unsigned long)__save_stack_trace;
N
Nicolas Pitre 已提交
144
	}
145

146
	walk_stackframe(&frame, save_trace, &data);
N
Nicolas Pitre 已提交
147 148 149 150
	if (trace->nr_entries < trace->max_entries)
		trace->entries[trace->nr_entries++] = ULONG_MAX;
}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
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;
}

170 171 172 173
void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
{
	__save_stack_trace(tsk, trace, 1);
}
174
EXPORT_SYMBOL(save_stack_trace_tsk);
175

N
Nicolas Pitre 已提交
176 177
void save_stack_trace(struct stack_trace *trace)
{
178
	__save_stack_trace(current, trace, 0);
179
}
180
EXPORT_SYMBOL_GPL(save_stack_trace);
181
#endif