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 void backtrace_address(void *data
, unsigned long addr
, int reliable
)
28 unsigned int *depth
= data
;
31 oprofile_add_trace(addr
);
34 static struct stacktrace_ops backtrace_ops
= {
35 .stack
= backtrace_stack
,
36 .address
= backtrace_address
,
37 .walk_stack
= print_context_stack
,
41 static struct stack_frame_ia32
*
42 dump_user_backtrace_32(struct stack_frame_ia32
*head
)
44 /* Also check accessibility of one struct frame_head beyond: */
45 struct stack_frame_ia32 bufhead
[2];
46 struct stack_frame_ia32
*fp
;
49 bytes
= copy_from_user_nmi(bufhead
, head
, sizeof(bufhead
));
50 if (bytes
!= sizeof(bufhead
))
53 fp
= (struct stack_frame_ia32
*) compat_ptr(bufhead
[0].next_frame
);
55 oprofile_add_trace(bufhead
[0].return_address
);
57 /* frame pointers should strictly progress back up the stack
58 * (towards higher addresses) */
66 x86_backtrace_32(struct pt_regs
* const regs
, unsigned int depth
)
68 struct stack_frame_ia32
*head
;
70 /* User process is 32-bit */
71 if (!current
|| !test_thread_flag(TIF_IA32
))
74 head
= (struct stack_frame_ia32
*) regs
->bp
;
75 while (depth
-- && head
)
76 head
= dump_user_backtrace_32(head
);
83 x86_backtrace_32(struct pt_regs
* const regs
, unsigned int depth
)
87 #endif /* CONFIG_COMPAT */
89 static struct stack_frame
*dump_user_backtrace(struct stack_frame
*head
)
91 /* Also check accessibility of one struct frame_head beyond: */
92 struct stack_frame bufhead
[2];
95 bytes
= copy_from_user_nmi(bufhead
, head
, sizeof(bufhead
));
96 if (bytes
!= sizeof(bufhead
))
99 oprofile_add_trace(bufhead
[0].return_address
);
101 /* frame pointers should strictly progress back up the stack
102 * (towards higher addresses) */
103 if (head
>= bufhead
[0].next_frame
)
106 return bufhead
[0].next_frame
;
110 x86_backtrace(struct pt_regs
* const regs
, unsigned int depth
)
112 struct stack_frame
*head
= (struct stack_frame
*)frame_pointer(regs
);
114 if (!user_mode_vm(regs
)) {
115 unsigned long stack
= kernel_stack_pointer(regs
);
117 dump_trace(NULL
, regs
, (unsigned long *)stack
, 0,
118 &backtrace_ops
, &depth
);
122 if (x86_backtrace_32(regs
, depth
))
125 while (depth
-- && head
)
126 head
= dump_user_backtrace(head
);