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> |
| 17 | |
| 18 | static void backtrace_warning_symbol(void *data, char *msg, |
| 19 | unsigned long symbol) |
| 20 | { |
| 21 | /* Ignore warnings */ |
| 22 | } |
| 23 | |
| 24 | static void backtrace_warning(void *data, char *msg) |
| 25 | { |
| 26 | /* Ignore warnings */ |
| 27 | } |
| 28 | |
| 29 | static int backtrace_stack(void *data, char *name) |
| 30 | { |
| 31 | /* Yes, we want all stacks */ |
| 32 | return 0; |
| 33 | } |
| 34 | |
Arjan van de Ven | bc850d6 | 2008-01-30 13:33:07 +0100 | [diff] [blame] | 35 | static void backtrace_address(void *data, unsigned long addr, int reliable) |
Jan Blunck | 574a604 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 36 | { |
| 37 | unsigned int *depth = data; |
| 38 | |
| 39 | if ((*depth)--) |
| 40 | oprofile_add_trace(addr); |
| 41 | } |
| 42 | |
| 43 | static struct stacktrace_ops backtrace_ops = { |
| 44 | .warning = backtrace_warning, |
| 45 | .warning_symbol = backtrace_warning_symbol, |
| 46 | .stack = backtrace_stack, |
| 47 | .address = backtrace_address, |
| 48 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | struct frame_head { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 51 | struct frame_head *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | unsigned long ret; |
| 53 | } __attribute__((packed)); |
| 54 | |
Robert Richter | 0f019cc | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 55 | static struct frame_head *dump_user_backtrace(struct frame_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 57 | struct frame_head bufhead[2]; |
| 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 | oprofile_add_trace(bufhead[0].ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | /* frame pointers should strictly progress back up the stack |
| 68 | * (towards higher addresses) */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 69 | if (head >= bufhead[0].bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | return NULL; |
| 71 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 72 | return bufhead[0].bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | void |
| 76 | x86_backtrace(struct pt_regs * const regs, unsigned int depth) |
| 77 | { |
Jan Blunck | f1df280 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 78 | struct frame_head *head = (struct frame_head *)frame_pointer(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Vincent Hanquez | fa1e1bd | 2005-06-23 00:08:44 -0700 | [diff] [blame] | 80 | if (!user_mode_vm(regs)) { |
Masami Hiramatsu | 7b6c6c7 | 2009-05-11 17:03:00 -0400 | [diff] [blame] | 81 | unsigned long stack = kernel_stack_pointer(regs); |
Jan Blunck | 574a604 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 82 | if (depth) |
Arjan van de Ven | 5bc27dc | 2008-01-30 13:33:07 +0100 | [diff] [blame] | 83 | dump_trace(NULL, regs, (unsigned long *)stack, 0, |
Jan Blunck | 574a604 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 84 | &backtrace_ops, &depth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 88 | while (depth-- && head) |
Gerald Britton | 3037944 | 2006-02-14 10:19:04 -0500 | [diff] [blame] | 89 | head = dump_user_backtrace(head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |