| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** | 
 | 2 |  * @file backtrace.c | 
 | 3 |  * | 
 | 4 |  * @remark Copyright 2002 OProfile authors | 
 | 5 |  * @remark Read the file COPYING | 
 | 6 |  * | 
 | 7 |  * @author John Levon | 
 | 8 |  * @author David Smith | 
 | 9 |  */ | 
 | 10 |  | 
 | 11 | #include <linux/oprofile.h> | 
 | 12 | #include <linux/sched.h> | 
 | 13 | #include <linux/mm.h> | 
 | 14 | #include <asm/ptrace.h> | 
| Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 15 | #include <asm/uaccess.h> | 
| Jan Blunck | 574a604 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 16 | #include <asm/stacktrace.h> | 
| Jiri Olsa | f6dedec | 2010-09-29 10:46:47 -0400 | [diff] [blame] | 17 | #include <linux/compat.h> | 
| Jan Blunck | 574a604 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 18 |  | 
 | 19 | static void backtrace_warning_symbol(void *data, char *msg, | 
 | 20 | 				     unsigned long symbol) | 
 | 21 | { | 
 | 22 | 	/* Ignore warnings */ | 
 | 23 | } | 
 | 24 |  | 
 | 25 | static void backtrace_warning(void *data, char *msg) | 
 | 26 | { | 
 | 27 | 	/* Ignore warnings */ | 
 | 28 | } | 
 | 29 |  | 
 | 30 | static int backtrace_stack(void *data, char *name) | 
 | 31 | { | 
 | 32 | 	/* Yes, we want all stacks */ | 
 | 33 | 	return 0; | 
 | 34 | } | 
 | 35 |  | 
| Arjan van de Ven | bc850d6 | 2008-01-30 13:33:07 +0100 | [diff] [blame] | 36 | static void backtrace_address(void *data, unsigned long addr, int reliable) | 
| Jan Blunck | 574a604 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 37 | { | 
 | 38 | 	unsigned int *depth = data; | 
 | 39 |  | 
 | 40 | 	if ((*depth)--) | 
 | 41 | 		oprofile_add_trace(addr); | 
 | 42 | } | 
 | 43 |  | 
 | 44 | static struct stacktrace_ops backtrace_ops = { | 
| Frederic Weisbecker | 61c1917 | 2009-12-17 05:40:33 +0100 | [diff] [blame] | 45 | 	.warning	= backtrace_warning, | 
 | 46 | 	.warning_symbol	= backtrace_warning_symbol, | 
 | 47 | 	.stack		= backtrace_stack, | 
 | 48 | 	.address	= backtrace_address, | 
 | 49 | 	.walk_stack	= print_context_stack, | 
| Jan Blunck | 574a604 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 50 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
| Jiri Olsa | f6dedec | 2010-09-29 10:46:47 -0400 | [diff] [blame] | 52 | #ifdef CONFIG_COMPAT | 
 | 53 | static struct stack_frame_ia32 * | 
 | 54 | dump_user_backtrace_32(struct stack_frame_ia32 *head) | 
 | 55 | { | 
 | 56 | 	struct stack_frame_ia32 bufhead[2]; | 
 | 57 | 	struct stack_frame_ia32 *fp; | 
 | 58 |  | 
 | 59 | 	/* Also check accessibility of one struct frame_head beyond */ | 
 | 60 | 	if (!access_ok(VERIFY_READ, head, sizeof(bufhead))) | 
 | 61 | 		return NULL; | 
 | 62 | 	if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead))) | 
 | 63 | 		return NULL; | 
 | 64 |  | 
 | 65 | 	fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame); | 
 | 66 |  | 
 | 67 | 	oprofile_add_trace(bufhead[0].return_address); | 
 | 68 |  | 
 | 69 | 	/* frame pointers should strictly progress back up the stack | 
 | 70 | 	* (towards higher addresses) */ | 
 | 71 | 	if (head >= fp) | 
 | 72 | 		return NULL; | 
 | 73 |  | 
 | 74 | 	return fp; | 
 | 75 | } | 
 | 76 |  | 
 | 77 | static inline int | 
 | 78 | x86_backtrace_32(struct pt_regs * const regs, unsigned int depth) | 
 | 79 | { | 
 | 80 | 	struct stack_frame_ia32 *head; | 
 | 81 |  | 
 | 82 | 	/* User process is 32-bit */ | 
 | 83 | 	if (!current || !test_thread_flag(TIF_IA32)) | 
 | 84 | 		return 0; | 
 | 85 |  | 
 | 86 | 	head = (struct stack_frame_ia32 *) regs->bp; | 
 | 87 | 	while (depth-- && head) | 
 | 88 | 		head = dump_user_backtrace_32(head); | 
 | 89 |  | 
 | 90 | 	return 1; | 
 | 91 | } | 
 | 92 |  | 
 | 93 | #else | 
 | 94 | static inline int | 
 | 95 | x86_backtrace_32(struct pt_regs * const regs, unsigned int depth) | 
 | 96 | { | 
 | 97 | 	return 0; | 
 | 98 | } | 
 | 99 | #endif /* CONFIG_COMPAT */ | 
 | 100 |  | 
| Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 101 | static struct stack_frame *dump_user_backtrace(struct stack_frame *head) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { | 
| Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 103 | 	struct stack_frame bufhead[2]; | 
| Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 104 |  | 
| Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 105 | 	/* Also check accessibility of one struct stack_frame beyond */ | 
| Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 106 | 	if (!access_ok(VERIFY_READ, head, sizeof(bufhead))) | 
 | 107 | 		return NULL; | 
 | 108 | 	if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead))) | 
 | 109 | 		return NULL; | 
 | 110 |  | 
| Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 111 | 	oprofile_add_trace(bufhead[0].return_address); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 |  | 
 | 113 | 	/* frame pointers should strictly progress back up the stack | 
 | 114 | 	 * (towards higher addresses) */ | 
| Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 115 | 	if (head >= bufhead[0].next_frame) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | 		return NULL; | 
 | 117 |  | 
| Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 118 | 	return bufhead[0].next_frame; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } | 
 | 120 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | void | 
 | 122 | x86_backtrace(struct pt_regs * const regs, unsigned int depth) | 
 | 123 | { | 
| Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 124 | 	struct stack_frame *head = (struct stack_frame *)frame_pointer(regs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 |  | 
| Vincent Hanquez | fa1e1bd | 2005-06-23 00:08:44 -0700 | [diff] [blame] | 126 | 	if (!user_mode_vm(regs)) { | 
| Masami Hiramatsu | 7b6c6c7 | 2009-05-11 17:03:00 -0400 | [diff] [blame] | 127 | 		unsigned long stack = kernel_stack_pointer(regs); | 
| Jan Blunck | 574a604 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 128 | 		if (depth) | 
| Soeren Sandmann Pedersen | 9c0729d | 2010-11-05 05:59:39 -0400 | [diff] [blame] | 129 | 			dump_trace(NULL, regs, (unsigned long *)stack, | 
| Jan Blunck | 574a604 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 130 | 				   &backtrace_ops, &depth); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | 		return; | 
 | 132 | 	} | 
 | 133 |  | 
| Jiri Olsa | f6dedec | 2010-09-29 10:46:47 -0400 | [diff] [blame] | 134 | 	if (x86_backtrace_32(regs, depth)) | 
 | 135 | 		return; | 
 | 136 |  | 
| Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 137 | 	while (depth-- && head) | 
| Gerald Britton | 3037944 | 2006-02-14 10:19:04 -0500 | [diff] [blame] | 138 | 		head = dump_user_backtrace(head); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |