Merge branch 'akpm'
[linux-2.6/next.git] / arch / x86 / oprofile / backtrace.c
blobbff89dfe36198225f2d7695e5d2ff01ce85e899f
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 <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 */
23 return 0;
26 static void backtrace_address(void *data, unsigned long addr, int reliable)
28 unsigned int *depth = data;
30 if ((*depth)--)
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,
40 #ifdef CONFIG_COMPAT
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;
47 unsigned long bytes;
49 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
50 if (bytes != sizeof(bufhead))
51 return NULL;
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) */
59 if (head >= fp)
60 return NULL;
62 return fp;
65 static inline int
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))
72 return 0;
74 head = (struct stack_frame_ia32 *) regs->bp;
75 while (depth-- && head)
76 head = dump_user_backtrace_32(head);
78 return 1;
81 #else
82 static inline int
83 x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
85 return 0;
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];
93 unsigned long bytes;
95 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
96 if (bytes != sizeof(bufhead))
97 return NULL;
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)
104 return NULL;
106 return bufhead[0].next_frame;
109 void
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);
116 if (depth)
117 dump_trace(NULL, regs, (unsigned long *)stack, 0,
118 &backtrace_ops, &depth);
119 return;
122 if (x86_backtrace_32(regs, depth))
123 return;
125 while (depth-- && head)
126 head = dump_user_backtrace(head);