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 void backtrace_warning_symbol(void *data, char *msg,
26 static void backtrace_warning(void *data, char *msg)
31 static int backtrace_stack(void *data, char *name)
33 /* Yes, we want all stacks */
37 static void backtrace_address(void *data, unsigned long addr, int reliable)
39 unsigned int *depth = data;
42 oprofile_add_trace(addr);
45 static struct stacktrace_ops backtrace_ops = {
46 .warning = backtrace_warning,
47 .warning_symbol = backtrace_warning_symbol,
48 .stack = backtrace_stack,
49 .address = backtrace_address,
50 .walk_stack = print_context_stack,
54 static struct stack_frame_ia32 *
55 dump_user_backtrace_32(struct stack_frame_ia32 *head)
57 /* Also check accessibility of one struct frame_head beyond: */
58 struct stack_frame_ia32 bufhead[2];
59 struct stack_frame_ia32 *fp;
62 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
63 if (bytes != sizeof(bufhead))
66 fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
68 oprofile_add_trace(bufhead[0].return_address);
70 /* frame pointers should strictly progress back up the stack
71 * (towards higher addresses) */
79 x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
81 struct stack_frame_ia32 *head;
83 /* User process is IA32 */
84 if (!current || !test_thread_flag(TIF_IA32))
87 head = (struct stack_frame_ia32 *) regs->bp;
88 while (depth-- && head)
89 head = dump_user_backtrace_32(head);
96 x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
100 #endif /* CONFIG_COMPAT */
102 static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
104 /* Also check accessibility of one struct frame_head beyond: */
105 struct stack_frame bufhead[2];
108 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
109 if (bytes != sizeof(bufhead))
112 oprofile_add_trace(bufhead[0].return_address);
114 /* frame pointers should strictly progress back up the stack
115 * (towards higher addresses) */
116 if (head >= bufhead[0].next_frame)
119 return bufhead[0].next_frame;
123 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
125 struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
127 if (!user_mode_vm(regs)) {
128 unsigned long stack = kernel_stack_pointer(regs);
130 dump_trace(NULL, regs, (unsigned long *)stack, 0,
131 &backtrace_ops, &depth);
135 if (x86_backtrace_32(regs, depth))
138 while (depth-- && head)
139 head = dump_user_backtrace(head);