4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
11 #include <linux/oprofile.h>
12 #include <linux/sched.h>
14 #include <asm/ptrace.h>
15 #include <asm/uaccess.h>
18 struct frame_head
* ebp
;
20 } __attribute__((packed
));
22 static struct frame_head
*
23 dump_backtrace(struct frame_head
* head
)
25 struct frame_head bufhead
[2];
27 /* Also check accessibility of one struct frame_head beyond */
28 if (!access_ok(VERIFY_READ
, head
, sizeof(bufhead
)))
30 if (__copy_from_user_inatomic(bufhead
, head
, sizeof(bufhead
)))
33 oprofile_add_trace(bufhead
[0].ret
);
35 /* frame pointers should strictly progress back up the stack
36 * (towards higher addresses) */
37 if (head
>= bufhead
[0].ebp
)
40 return bufhead
[0].ebp
;
44 * | | /\ Higher addresses
46 * --------------- stack base (address of current_thread_info)
50 * --------------- saved regs->ebp value if valid (frame_head address)
52 * --------------- struct pt_regs stored on stack (struct pt_regs *)
56 * --------------- %esp
58 * | | \/ Lower addresses
60 * Thus, &pt_regs <-> stack base restricts the valid(ish) ebp values
62 #ifdef CONFIG_FRAME_POINTER
63 static int valid_kernel_stack(struct frame_head
* head
, struct pt_regs
* regs
)
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
;
69 return headaddr
> stack
&& headaddr
< stack_base
;
72 /* without fp, it's just junk */
73 static int valid_kernel_stack(struct frame_head
* head
, struct pt_regs
* regs
)
81 x86_backtrace(struct pt_regs
* const regs
, unsigned int depth
)
83 struct frame_head
*head
;
86 head
= (struct frame_head
*)regs
->rbp
;
88 head
= (struct frame_head
*)regs
->ebp
;
91 if (!user_mode_vm(regs
)) {
92 while (depth
-- && valid_kernel_stack(head
, regs
))
93 head
= dump_backtrace(head
);
97 while (depth
-- && head
)
98 head
= dump_backtrace(head
);