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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | struct frame_head { |
| 18 | struct frame_head * ebp; |
| 19 | unsigned long ret; |
| 20 | } __attribute__((packed)); |
| 21 | |
| 22 | static struct frame_head * |
| 23 | dump_backtrace(struct frame_head * head) |
| 24 | { |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 25 | struct frame_head bufhead[2]; |
| 26 | |
| 27 | /* Also check accessibility of one struct frame_head beyond */ |
| 28 | if (!access_ok(VERIFY_READ, head, sizeof(bufhead))) |
| 29 | return NULL; |
| 30 | if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead))) |
| 31 | return NULL; |
| 32 | |
| 33 | oprofile_add_trace(bufhead[0].ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* frame pointers should strictly progress back up the stack |
| 36 | * (towards higher addresses) */ |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 37 | if (head >= bufhead[0].ebp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | return NULL; |
| 39 | |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 40 | return bufhead[0].ebp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* |
| 44 | * | | /\ Higher addresses |
| 45 | * | | |
| 46 | * --------------- stack base (address of current_thread_info) |
| 47 | * | thread info | |
| 48 | * . . |
| 49 | * | stack | |
| 50 | * --------------- saved regs->ebp value if valid (frame_head address) |
| 51 | * . . |
| 52 | * --------------- struct pt_regs stored on stack (struct pt_regs *) |
| 53 | * | | |
| 54 | * . . |
| 55 | * | | |
| 56 | * --------------- %esp |
| 57 | * | | |
| 58 | * | | \/ Lower addresses |
| 59 | * |
| 60 | * Thus, &pt_regs <-> stack base restricts the valid(ish) ebp values |
| 61 | */ |
| 62 | #ifdef CONFIG_FRAME_POINTER |
| 63 | static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs) |
| 64 | { |
| 65 | unsigned long headaddr = (unsigned long)head; |
| 66 | unsigned long stack = (unsigned long)regs; |
| 67 | unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE; |
| 68 | |
| 69 | return headaddr > stack && headaddr < stack_base; |
| 70 | } |
| 71 | #else |
| 72 | /* without fp, it's just junk */ |
| 73 | static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs) |
| 74 | { |
| 75 | return 0; |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | |
| 80 | void |
| 81 | x86_backtrace(struct pt_regs * const regs, unsigned int depth) |
| 82 | { |
| 83 | struct frame_head *head; |
| 84 | |
| 85 | #ifdef CONFIG_X86_64 |
| 86 | head = (struct frame_head *)regs->rbp; |
| 87 | #else |
| 88 | head = (struct frame_head *)regs->ebp; |
| 89 | #endif |
| 90 | |
Vincent Hanquez | fa1e1bd | 2005-06-23 00:08:44 -0700 | [diff] [blame] | 91 | if (!user_mode_vm(regs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | while (depth-- && valid_kernel_stack(head, regs)) |
| 93 | head = dump_backtrace(head); |
| 94 | return; |
| 95 | } |
| 96 | |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 97 | while (depth-- && head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | head = dump_backtrace(head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |