backtrace.c 3.0 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>
17
#include <linux/compat.h>
L
Linus Torvalds 已提交
18

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

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

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

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

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

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

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#ifdef CONFIG_COMPAT
static struct stack_frame_ia32 *
dump_user_backtrace_32(struct stack_frame_ia32 *head)
{
	struct stack_frame_ia32 bufhead[2];
	struct stack_frame_ia32 *fp;

	/* 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)))
		return NULL;

	fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);

	oprofile_add_trace(bufhead[0].return_address);

	/* frame pointers should strictly progress back up the stack
	* (towards higher addresses) */
	if (head >= fp)
		return NULL;

	return fp;
}

static inline int
x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
{
	struct stack_frame_ia32 *head;

	/* User process is 32-bit */
	if (!current || !test_thread_flag(TIF_IA32))
		return 0;

	head = (struct stack_frame_ia32 *) regs->bp;
	while (depth-- && head)
		head = dump_user_backtrace_32(head);

	return 1;
}

#else
static inline int
x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
{
	return 0;
}
#endif /* CONFIG_COMPAT */

101
static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
L
Linus Torvalds 已提交
102
{
103
	struct stack_frame bufhead[2];
L
Linus Torvalds 已提交
104

105
	/* Also check accessibility of one struct stack_frame beyond */
106 107 108
	if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
		return NULL;
	if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
L
Linus Torvalds 已提交
109 110
		return NULL;

111
	oprofile_add_trace(bufhead[0].return_address);
L
Linus Torvalds 已提交
112

113 114
	/* frame pointers should strictly progress back up the stack
	 * (towards higher addresses) */
115
	if (head >= bufhead[0].next_frame)
116
		return NULL;
L
Linus Torvalds 已提交
117

118
	return bufhead[0].next_frame;
L
Linus Torvalds 已提交
119 120 121 122 123
}

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

126
	if (!user_mode_vm(regs)) {
127
		unsigned long stack = kernel_stack_pointer(regs);
128
		if (depth)
129
			dump_trace(NULL, regs, (unsigned long *)stack,
130
				   &backtrace_ops, &depth);
L
Linus Torvalds 已提交
131 132 133
		return;
	}

134 135 136
	if (x86_backtrace_32(regs, depth))
		return;

137
	while (depth-- && head)
138
		head = dump_user_backtrace(head);
L
Linus Torvalds 已提交
139
}