4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
11 #include <linux/oprofile.h>
12 #include <linux/sched.h>
14 #include <linux/compat.h>
15 #include <linux/uaccess.h>
17 #include <asm/ptrace.h>
18 #include <asm/stacktrace.h>
20 static int backtrace_stack(void *data
, char *name
)
22 /* Yes, we want all stacks */
26 static int backtrace_address(void *data
, unsigned long addr
, int reliable
)
28 unsigned int *depth
= data
;
31 oprofile_add_trace(addr
);
35 static struct stacktrace_ops backtrace_ops
= {
36 .stack
= backtrace_stack
,
37 .address
= backtrace_address
,
38 .walk_stack
= print_context_stack
,
42 static struct stack_frame_ia32
*
43 dump_user_backtrace_32(struct stack_frame_ia32
*head
)
45 /* Also check accessibility of one struct frame_head beyond: */
46 struct stack_frame_ia32 bufhead
[2];
47 struct stack_frame_ia32
*fp
;
50 bytes
= copy_from_user_nmi(bufhead
, head
, sizeof(bufhead
));
54 fp
= (struct stack_frame_ia32
*) compat_ptr(bufhead
[0].next_frame
);
56 oprofile_add_trace(bufhead
[0].return_address
);
58 /* frame pointers should strictly progress back up the stack
59 * (towards higher addresses) */
67 x86_backtrace_32(struct pt_regs
* const regs
, unsigned int depth
)
69 struct stack_frame_ia32
*head
;
71 /* User process is IA32 */
72 if (!current
|| !test_thread_flag(TIF_IA32
))
75 head
= (struct stack_frame_ia32
*) regs
->bp
;
76 while (depth
-- && head
)
77 head
= dump_user_backtrace_32(head
);
84 x86_backtrace_32(struct pt_regs
* const regs
, unsigned int depth
)
88 #endif /* CONFIG_COMPAT */
90 static struct stack_frame
*dump_user_backtrace(struct stack_frame
*head
)
92 /* Also check accessibility of one struct frame_head beyond: */
93 struct stack_frame bufhead
[2];
96 bytes
= copy_from_user_nmi(bufhead
, head
, sizeof(bufhead
));
100 oprofile_add_trace(bufhead
[0].return_address
);
102 /* frame pointers should strictly progress back up the stack
103 * (towards higher addresses) */
104 if (head
>= bufhead
[0].next_frame
)
107 return bufhead
[0].next_frame
;
111 x86_backtrace(struct pt_regs
* const regs
, unsigned int depth
)
113 struct stack_frame
*head
= (struct stack_frame
*)frame_pointer(regs
);
115 if (!user_mode(regs
)) {
116 unsigned long stack
= kernel_stack_pointer(regs
);
118 dump_trace(NULL
, regs
, (unsigned long *)stack
, 0,
119 &backtrace_ops
, &depth
);
123 if (x86_backtrace_32(regs
, depth
))
126 while (depth
-- && head
)
127 head
= dump_user_backtrace(head
);