Linux 4.6-rc6
[linux/fpc-iii.git] / arch / x86 / oprofile / backtrace.c
blobcb31a4440e588552f5fd046ae787c107aa09ec94
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 int backtrace_address(void *data, unsigned long addr, int reliable)
28 unsigned int *depth = data;
30 if ((*depth)--)
31 oprofile_add_trace(addr);
32 return 0;
35 static struct stacktrace_ops backtrace_ops = {
36 .stack = backtrace_stack,
37 .address = backtrace_address,
38 .walk_stack = print_context_stack,
41 #ifdef CONFIG_COMPAT
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;
48 unsigned long bytes;
50 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
51 if (bytes != 0)
52 return NULL;
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) */
60 if (head >= fp)
61 return NULL;
63 return fp;
66 static inline int
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))
73 return 0;
75 head = (struct stack_frame_ia32 *) regs->bp;
76 while (depth-- && head)
77 head = dump_user_backtrace_32(head);
79 return 1;
82 #else
83 static inline int
84 x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
86 return 0;
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];
94 unsigned long bytes;
96 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
97 if (bytes != 0)
98 return NULL;
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)
105 return NULL;
107 return bufhead[0].next_frame;
110 void
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);
117 if (depth)
118 dump_trace(NULL, regs, (unsigned long *)stack, 0,
119 &backtrace_ops, &depth);
120 return;
123 if (x86_backtrace_32(regs, depth))
124 return;
126 while (depth-- && head)
127 head = dump_user_backtrace(head);