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
static struct frame_head *dump_user_backtrace(struct frame_head *head)
L
Linus Torvalds 已提交
56
{
57
	struct frame_head bufhead[2];
L
Linus Torvalds 已提交
58

59 60 61 62
	/* 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 已提交
63 64
		return NULL;

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

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

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

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

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

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