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> |
Robert Richter | a0e3e70 | 2011-06-03 16:37:47 +0200 | [diff] [blame] | 14 | #include <linux/compat.h> |
Robert Richter | 1ac2e6c | 2011-06-07 11:49:55 +0200 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
Robert Richter | a0e3e70 | 2011-06-03 16:37:47 +0200 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/ptrace.h> |
Jan Blunck | 574a604 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 18 | #include <asm/stacktrace.h> |
Josh Poimboeuf | ec2ad9c | 2016-09-16 14:18:15 -0500 | [diff] [blame^] | 19 | #include <asm/unwind.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Jiri Olsa | f6dedec | 2010-09-29 10:46:47 -0400 | [diff] [blame] | 21 | #ifdef CONFIG_COMPAT |
| 22 | static struct stack_frame_ia32 * |
| 23 | dump_user_backtrace_32(struct stack_frame_ia32 *head) |
| 24 | { |
Robert Richter | a0e3e70 | 2011-06-03 16:37:47 +0200 | [diff] [blame] | 25 | /* Also check accessibility of one struct frame_head beyond: */ |
Jiri Olsa | f6dedec | 2010-09-29 10:46:47 -0400 | [diff] [blame] | 26 | struct stack_frame_ia32 bufhead[2]; |
| 27 | struct stack_frame_ia32 *fp; |
Robert Richter | a0e3e70 | 2011-06-03 16:37:47 +0200 | [diff] [blame] | 28 | unsigned long bytes; |
Jiri Olsa | f6dedec | 2010-09-29 10:46:47 -0400 | [diff] [blame] | 29 | |
Robert Richter | a0e3e70 | 2011-06-03 16:37:47 +0200 | [diff] [blame] | 30 | bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead)); |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 31 | if (bytes != 0) |
Jiri Olsa | f6dedec | 2010-09-29 10:46:47 -0400 | [diff] [blame] | 32 | return NULL; |
| 33 | |
| 34 | fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame); |
| 35 | |
| 36 | oprofile_add_trace(bufhead[0].return_address); |
| 37 | |
| 38 | /* frame pointers should strictly progress back up the stack |
| 39 | * (towards higher addresses) */ |
| 40 | if (head >= fp) |
| 41 | return NULL; |
| 42 | |
| 43 | return fp; |
| 44 | } |
| 45 | |
| 46 | static inline int |
| 47 | x86_backtrace_32(struct pt_regs * const regs, unsigned int depth) |
| 48 | { |
| 49 | struct stack_frame_ia32 *head; |
| 50 | |
H. Peter Anvin | 6bd3300 | 2012-02-06 13:03:09 -0800 | [diff] [blame] | 51 | /* User process is IA32 */ |
Jiri Olsa | f6dedec | 2010-09-29 10:46:47 -0400 | [diff] [blame] | 52 | if (!current || !test_thread_flag(TIF_IA32)) |
| 53 | return 0; |
| 54 | |
| 55 | head = (struct stack_frame_ia32 *) regs->bp; |
| 56 | while (depth-- && head) |
| 57 | head = dump_user_backtrace_32(head); |
| 58 | |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | #else |
| 63 | static inline int |
| 64 | x86_backtrace_32(struct pt_regs * const regs, unsigned int depth) |
| 65 | { |
| 66 | return 0; |
| 67 | } |
| 68 | #endif /* CONFIG_COMPAT */ |
| 69 | |
Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 70 | static struct stack_frame *dump_user_backtrace(struct stack_frame *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Robert Richter | a0e3e70 | 2011-06-03 16:37:47 +0200 | [diff] [blame] | 72 | /* Also check accessibility of one struct frame_head beyond: */ |
Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 73 | struct stack_frame bufhead[2]; |
Robert Richter | a0e3e70 | 2011-06-03 16:37:47 +0200 | [diff] [blame] | 74 | unsigned long bytes; |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 75 | |
Robert Richter | a0e3e70 | 2011-06-03 16:37:47 +0200 | [diff] [blame] | 76 | bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead)); |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 77 | if (bytes != 0) |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 78 | return NULL; |
| 79 | |
Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 80 | oprofile_add_trace(bufhead[0].return_address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | /* frame pointers should strictly progress back up the stack |
| 83 | * (towards higher addresses) */ |
Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 84 | if (head >= bufhead[0].next_frame) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return NULL; |
| 86 | |
Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 87 | return bufhead[0].next_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | void |
| 91 | x86_backtrace(struct pt_regs * const regs, unsigned int depth) |
| 92 | { |
Jiri Olsa | 40c6b3c | 2010-09-29 10:46:46 -0400 | [diff] [blame] | 93 | struct stack_frame *head = (struct stack_frame *)frame_pointer(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Andy Lutomirski | f39b6f0 | 2015-03-18 18:33:33 -0700 | [diff] [blame] | 95 | if (!user_mode(regs)) { |
Josh Poimboeuf | ec2ad9c | 2016-09-16 14:18:15 -0500 | [diff] [blame^] | 96 | struct unwind_state state; |
| 97 | unsigned long addr; |
| 98 | |
Josh Poimboeuf | 3e344a0 | 2016-08-24 11:50:15 -0500 | [diff] [blame] | 99 | if (!depth) |
| 100 | return; |
| 101 | |
| 102 | oprofile_add_trace(regs->ip); |
Josh Poimboeuf | ec2ad9c | 2016-09-16 14:18:15 -0500 | [diff] [blame^] | 103 | |
Josh Poimboeuf | 3e344a0 | 2016-08-24 11:50:15 -0500 | [diff] [blame] | 104 | if (!--depth) |
| 105 | return; |
| 106 | |
Josh Poimboeuf | ec2ad9c | 2016-09-16 14:18:15 -0500 | [diff] [blame^] | 107 | for (unwind_start(&state, current, regs, NULL); |
| 108 | !unwind_done(&state); unwind_next_frame(&state)) { |
| 109 | addr = unwind_get_return_address(&state); |
| 110 | if (!addr) |
| 111 | break; |
| 112 | |
| 113 | oprofile_add_trace(addr); |
| 114 | |
| 115 | if (!--depth) |
| 116 | break; |
| 117 | } |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | |
Jiri Olsa | f6dedec | 2010-09-29 10:46:47 -0400 | [diff] [blame] | 122 | if (x86_backtrace_32(regs, depth)) |
| 123 | return; |
| 124 | |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 125 | while (depth-- && head) |
Gerald Britton | 3037944 | 2006-02-14 10:19:04 -0500 | [diff] [blame] | 126 | head = dump_user_backtrace(head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |