blob: bff89dfe36198225f2d7695e5d2ff01ce85e899f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file backtrace.c
3 *
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
6 *
7 * @author John Levon
8 * @author David Smith
9 */
10
11#include <linux/oprofile.h>
12#include <linux/sched.h>
13#include <linux/mm.h>
Robert Richtera0e3e702011-06-03 16:37:47 +020014#include <linux/compat.h>
Robert Richter1ac2e6c2011-06-07 11:49:55 +020015#include <linux/uaccess.h>
Robert Richtera0e3e702011-06-03 16:37:47 +020016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/ptrace.h>
Jan Blunck574a6042007-10-19 20:35:03 +020018#include <asm/stacktrace.h>
19
Jan Blunck574a6042007-10-19 20:35:03 +020020static int backtrace_stack(void *data, char *name)
21{
22 /* Yes, we want all stacks */
23 return 0;
24}
25
Arjan van de Venbc850d62008-01-30 13:33:07 +010026static void backtrace_address(void *data, unsigned long addr, int reliable)
Jan Blunck574a6042007-10-19 20:35:03 +020027{
28 unsigned int *depth = data;
29
30 if ((*depth)--)
31 oprofile_add_trace(addr);
32}
33
34static struct stacktrace_ops backtrace_ops = {
Frederic Weisbecker61c19172009-12-17 05:40:33 +010035 .stack = backtrace_stack,
36 .address = backtrace_address,
37 .walk_stack = print_context_stack,
Jan Blunck574a6042007-10-19 20:35:03 +020038};
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Jiri Olsaf6dedec2010-09-29 10:46:47 -040040#ifdef CONFIG_COMPAT
41static struct stack_frame_ia32 *
42dump_user_backtrace_32(struct stack_frame_ia32 *head)
43{
Robert Richtera0e3e702011-06-03 16:37:47 +020044 /* Also check accessibility of one struct frame_head beyond: */
Jiri Olsaf6dedec2010-09-29 10:46:47 -040045 struct stack_frame_ia32 bufhead[2];
46 struct stack_frame_ia32 *fp;
Robert Richtera0e3e702011-06-03 16:37:47 +020047 unsigned long bytes;
Jiri Olsaf6dedec2010-09-29 10:46:47 -040048
Robert Richtera0e3e702011-06-03 16:37:47 +020049 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
50 if (bytes != sizeof(bufhead))
Jiri Olsaf6dedec2010-09-29 10:46:47 -040051 return NULL;
52
53 fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
54
55 oprofile_add_trace(bufhead[0].return_address);
56
57 /* frame pointers should strictly progress back up the stack
58 * (towards higher addresses) */
59 if (head >= fp)
60 return NULL;
61
62 return fp;
63}
64
65static inline int
66x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
67{
68 struct stack_frame_ia32 *head;
69
70 /* User process is 32-bit */
71 if (!current || !test_thread_flag(TIF_IA32))
72 return 0;
73
74 head = (struct stack_frame_ia32 *) regs->bp;
75 while (depth-- && head)
76 head = dump_user_backtrace_32(head);
77
78 return 1;
79}
80
81#else
82static inline int
83x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
84{
85 return 0;
86}
87#endif /* CONFIG_COMPAT */
88
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040089static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Robert Richtera0e3e702011-06-03 16:37:47 +020091 /* Also check accessibility of one struct frame_head beyond: */
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040092 struct stack_frame bufhead[2];
Robert Richtera0e3e702011-06-03 16:37:47 +020093 unsigned long bytes;
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070094
Robert Richtera0e3e702011-06-03 16:37:47 +020095 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
96 if (bytes != sizeof(bufhead))
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070097 return NULL;
98
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040099 oprofile_add_trace(bufhead[0].return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* frame pointers should strictly progress back up the stack
102 * (towards higher addresses) */
Jiri Olsa40c6b3c2010-09-29 10:46:46 -0400103 if (head >= bufhead[0].next_frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return NULL;
105
Jiri Olsa40c6b3c2010-09-29 10:46:46 -0400106 return bufhead[0].next_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109void
110x86_backtrace(struct pt_regs * const regs, unsigned int depth)
111{
Jiri Olsa40c6b3c2010-09-29 10:46:46 -0400112 struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Vincent Hanquezfa1e1bd2005-06-23 00:08:44 -0700114 if (!user_mode_vm(regs)) {
Masami Hiramatsu7b6c6c72009-05-11 17:03:00 -0400115 unsigned long stack = kernel_stack_pointer(regs);
Jan Blunck574a6042007-10-19 20:35:03 +0200116 if (depth)
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900117 dump_trace(NULL, regs, (unsigned long *)stack, 0,
Jan Blunck574a6042007-10-19 20:35:03 +0200118 &backtrace_ops, &depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return;
120 }
121
Jiri Olsaf6dedec2010-09-29 10:46:47 -0400122 if (x86_backtrace_32(regs, depth))
123 return;
124
Hugh Dickinsc34d1b42005-10-29 18:16:32 -0700125 while (depth-- && head)
Gerald Britton30379442006-02-14 10:19:04 -0500126 head = dump_user_backtrace(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}