stacktrace.c 2.0 KB
Newer Older
1
#include <linux/module.h>
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <linux/sched.h>
#include <linux/stacktrace.h>

#include "stacktrace.h"

int walk_stackframe(unsigned long fp, unsigned long low, unsigned long high,
		    int (*fn)(struct stackframe *, void *), void *data)
{
	struct stackframe *frame;

	do {
		/*
		 * Check current frame pointer is within bounds
		 */
16
		if (fp < (low + 12) || fp + 4 >= high)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
			break;

		frame = (struct stackframe *)(fp - 12);

		if (fn(frame, data))
			break;

		/*
		 * Update the low bound - the next frame must always
		 * be at a higher address than the current frame.
		 */
		low = fp + 4;
		fp = frame->fp;
	} while (fp);

	return 0;
}
34
EXPORT_SYMBOL(walk_stackframe);
35 36 37 38

#ifdef CONFIG_STACKTRACE
struct stack_trace_data {
	struct stack_trace *trace;
N
Nicolas Pitre 已提交
39
	unsigned int no_sched_functions;
40 41 42 43 44 45 46
	unsigned int skip;
};

static int save_trace(struct stackframe *frame, void *d)
{
	struct stack_trace_data *data = d;
	struct stack_trace *trace = data->trace;
N
Nicolas Pitre 已提交
47
	unsigned long addr = frame->lr;
48

N
Nicolas Pitre 已提交
49 50
	if (data->no_sched_functions && in_sched_functions(addr))
		return 0;
51 52 53 54 55
	if (data->skip) {
		data->skip--;
		return 0;
	}

N
Nicolas Pitre 已提交
56
	trace->entries[trace->nr_entries++] = addr;
57 58 59 60

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

N
Nicolas Pitre 已提交
61
void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
62 63 64 65 66 67
{
	struct stack_trace_data data;
	unsigned long fp, base;

	data.trace = trace;
	data.skip = trace->skip;
N
Nicolas Pitre 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	base = (unsigned long)task_stack_page(tsk);

	if (tsk != current) {
#ifdef CONFIG_SMP
		/*
		 * What guarantees do we have here that 'tsk'
		 * is not running on another CPU?
		 */
		BUG();
#else
		data.no_sched_functions = 1;
		fp = thread_saved_fp(tsk);
#endif
	} else {
		data.no_sched_functions = 0;
		asm("mov %0, fp" : "=r" (fp));
	}
85 86

	walk_stackframe(fp, base, base + THREAD_SIZE, save_trace, &data);
N
Nicolas Pitre 已提交
87 88 89 90 91 92 93
	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);
94
}
95
EXPORT_SYMBOL_GPL(save_stack_trace);
96
#endif