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>
16 #include <asm/stacktrace.h>
17 #include <linux/compat.h>
19 static int backtrace_stack(void *data
, char *name
)
21 /* Yes, we want all stacks */
25 static void backtrace_address(void *data
, unsigned long addr
, int reliable
)
27 unsigned int *depth
= data
;
30 oprofile_add_trace(addr
);
33 static struct stacktrace_ops backtrace_ops
= {
34 .stack
= backtrace_stack
,
35 .address
= backtrace_address
,
36 .walk_stack
= print_context_stack
,
40 static struct stack_frame_ia32
*
41 dump_user_backtrace_32(struct stack_frame_ia32
*head
)
43 struct stack_frame_ia32 bufhead
[2];
44 struct stack_frame_ia32
*fp
;
46 /* Also check accessibility of one struct frame_head beyond */
47 if (!access_ok(VERIFY_READ
, head
, sizeof(bufhead
)))
49 if (__copy_from_user_inatomic(bufhead
, head
, sizeof(bufhead
)))
52 fp
= (struct stack_frame_ia32
*) compat_ptr(bufhead
[0].next_frame
);
54 oprofile_add_trace(bufhead
[0].return_address
);
56 /* frame pointers should strictly progress back up the stack
57 * (towards higher addresses) */
65 x86_backtrace_32(struct pt_regs
* const regs
, unsigned int depth
)
67 struct stack_frame_ia32
*head
;
69 /* User process is 32-bit */
70 if (!current
|| !test_thread_flag(TIF_IA32
))
73 head
= (struct stack_frame_ia32
*) regs
->bp
;
74 while (depth
-- && head
)
75 head
= dump_user_backtrace_32(head
);
82 x86_backtrace_32(struct pt_regs
* const regs
, unsigned int depth
)
86 #endif /* CONFIG_COMPAT */
88 static struct stack_frame
*dump_user_backtrace(struct stack_frame
*head
)
90 struct stack_frame bufhead
[2];
92 /* Also check accessibility of one struct stack_frame beyond */
93 if (!access_ok(VERIFY_READ
, head
, sizeof(bufhead
)))
95 if (__copy_from_user_inatomic(bufhead
, head
, sizeof(bufhead
)))
98 oprofile_add_trace(bufhead
[0].return_address
);
100 /* frame pointers should strictly progress back up the stack
101 * (towards higher addresses) */
102 if (head
>= bufhead
[0].next_frame
)
105 return bufhead
[0].next_frame
;
109 x86_backtrace(struct pt_regs
* const regs
, unsigned int depth
)
111 struct stack_frame
*head
= (struct stack_frame
*)frame_pointer(regs
);
113 if (!user_mode_vm(regs
)) {
114 unsigned long stack
= kernel_stack_pointer(regs
);
116 dump_trace(NULL
, regs
, (unsigned long *)stack
, 0,
117 &backtrace_ops
, &depth
);
121 if (x86_backtrace_32(regs
, depth
))
124 while (depth
-- && head
)
125 head
= dump_user_backtrace(head
);