backtrace.c 1.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 * @file backtrace.c
 *
 * @remark Copyright 2002 OProfile authors
 * @remark Read the file COPYING
 *
 * @author John Levon
 * @author David Smith
 */

#include <linux/oprofile.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <asm/ptrace.h>
15
#include <asm/uaccess.h>
16
#include <asm/stacktrace.h>
L
Linus Torvalds 已提交
17

18 19 20 21 22
static void backtrace_warning_symbol(void *data, char *msg,
				     unsigned long symbol)
{
	/* Ignore warnings */
}
L
Linus Torvalds 已提交
23

24
static void backtrace_warning(void *data, char *msg)
25
{
26 27
	/* Ignore warnings */
}
28

29 30 31 32 33
static int backtrace_stack(void *data, char *name)
{
	/* Yes, we want all stacks */
	return 0;
}
34

35
static void backtrace_address(void *data, unsigned long addr, int reliable)
36 37 38 39 40
{
	unsigned int *depth = data;

	if ((*depth)--)
		oprofile_add_trace(addr);
41 42
}

43 44 45 46 47 48 49 50
static struct stacktrace_ops backtrace_ops = {
	.warning = backtrace_warning,
	.warning_symbol = backtrace_warning_symbol,
	.stack = backtrace_stack,
	.address = backtrace_address,
};

struct frame_head {
51
	struct frame_head *bp;
52 53 54
	unsigned long ret;
} __attribute__((packed));

55 56
static struct frame_head *
dump_user_backtrace(struct frame_head * head)
L
Linus Torvalds 已提交
57
{
58
	struct frame_head bufhead[2];
L
Linus Torvalds 已提交
59

60 61 62 63
	/* Also check accessibility of one struct frame_head beyond */
	if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
		return NULL;
	if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
L
Linus Torvalds 已提交
64 65
		return NULL;

66
	oprofile_add_trace(bufhead[0].ret);
L
Linus Torvalds 已提交
67

68 69
	/* frame pointers should strictly progress back up the stack
	 * (towards higher addresses) */
70
	if (head >= bufhead[0].bp)
71
		return NULL;
L
Linus Torvalds 已提交
72

73
	return bufhead[0].bp;
L
Linus Torvalds 已提交
74 75 76 77 78
}

void
x86_backtrace(struct pt_regs * const regs, unsigned int depth)
{
79 80
	struct frame_head *head = (struct frame_head *)frame_pointer(regs);
	unsigned long stack = stack_pointer(regs);
L
Linus Torvalds 已提交
81

82
	if (!user_mode_vm(regs)) {
83 84 85
		if (depth)
			dump_trace(NULL, regs, (unsigned long *)stack,
				   &backtrace_ops, &depth);
L
Linus Torvalds 已提交
86 87 88
		return;
	}

89
	while (depth-- && head)
90
		head = dump_user_backtrace(head);
L
Linus Torvalds 已提交
91
}