oprofile, x86: Adding backtrace dump for 32bit process in compat mode
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / oprofile / backtrace.c
blob2d49d4e19a3619c0be2c7d17a892b8aea582048f
1 /**
2 * @file backtrace.c
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon
8 * @author David Smith
9 */
11 #include <linux/oprofile.h>
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <asm/ptrace.h>
15 #include <asm/uaccess.h>
16 #include <asm/stacktrace.h>
17 #include <linux/compat.h>
19 static void backtrace_warning_symbol(void *data, char *msg,
20 unsigned long symbol)
22 /* Ignore warnings */
25 static void backtrace_warning(void *data, char *msg)
27 /* Ignore warnings */
30 static int backtrace_stack(void *data, char *name)
32 /* Yes, we want all stacks */
33 return 0;
36 static void backtrace_address(void *data, unsigned long addr, int reliable)
38 unsigned int *depth = data;
40 if ((*depth)--)
41 oprofile_add_trace(addr);
44 static struct stacktrace_ops backtrace_ops = {
45 .warning = backtrace_warning,
46 .warning_symbol = backtrace_warning_symbol,
47 .stack = backtrace_stack,
48 .address = backtrace_address,
49 .walk_stack = print_context_stack,
52 #ifdef CONFIG_COMPAT
53 static struct stack_frame_ia32 *
54 dump_user_backtrace_32(struct stack_frame_ia32 *head)
56 struct stack_frame_ia32 bufhead[2];
57 struct stack_frame_ia32 *fp;
59 /* Also check accessibility of one struct frame_head beyond */
60 if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
61 return NULL;
62 if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
63 return NULL;
65 fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
67 oprofile_add_trace(bufhead[0].return_address);
69 /* frame pointers should strictly progress back up the stack
70 * (towards higher addresses) */
71 if (head >= fp)
72 return NULL;
74 return fp;
77 static inline int
78 x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
80 struct stack_frame_ia32 *head;
82 /* User process is 32-bit */
83 if (!current || !test_thread_flag(TIF_IA32))
84 return 0;
86 head = (struct stack_frame_ia32 *) regs->bp;
87 while (depth-- && head)
88 head = dump_user_backtrace_32(head);
90 return 1;
93 #else
94 static inline int
95 x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
97 return 0;
99 #endif /* CONFIG_COMPAT */
101 static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
103 struct stack_frame bufhead[2];
105 /* Also check accessibility of one struct stack_frame beyond */
106 if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
107 return NULL;
108 if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
109 return NULL;
111 oprofile_add_trace(bufhead[0].return_address);
113 /* frame pointers should strictly progress back up the stack
114 * (towards higher addresses) */
115 if (head >= bufhead[0].next_frame)
116 return NULL;
118 return bufhead[0].next_frame;
121 void
122 x86_backtrace(struct pt_regs * const regs, unsigned int depth)
124 struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
126 if (!user_mode_vm(regs)) {
127 unsigned long stack = kernel_stack_pointer(regs);
128 if (depth)
129 dump_trace(NULL, regs, (unsigned long *)stack, 0,
130 &backtrace_ops, &depth);
131 return;
134 if (x86_backtrace_32(regs, depth))
135 return;
137 while (depth-- && head)
138 head = dump_user_backtrace(head);