blob: a5b64ab4cd6e3ef34be5beda88100e72091437c8 [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>
14#include <asm/ptrace.h>
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070015#include <asm/uaccess.h>
Jan Blunck574a6042007-10-19 20:35:03 +020016#include <asm/stacktrace.h>
Jiri Olsaf6dedec2010-09-29 10:46:47 -040017#include <linux/compat.h>
Jan Blunck574a6042007-10-19 20:35:03 +020018
Jan Blunck574a6042007-10-19 20:35:03 +020019static int backtrace_stack(void *data, char *name)
20{
21 /* Yes, we want all stacks */
22 return 0;
23}
24
Arjan van de Venbc850d62008-01-30 13:33:07 +010025static void backtrace_address(void *data, unsigned long addr, int reliable)
Jan Blunck574a6042007-10-19 20:35:03 +020026{
27 unsigned int *depth = data;
28
29 if ((*depth)--)
30 oprofile_add_trace(addr);
31}
32
33static struct stacktrace_ops backtrace_ops = {
Frederic Weisbecker61c19172009-12-17 05:40:33 +010034 .stack = backtrace_stack,
35 .address = backtrace_address,
36 .walk_stack = print_context_stack,
Jan Blunck574a6042007-10-19 20:35:03 +020037};
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Jiri Olsaf6dedec2010-09-29 10:46:47 -040039#ifdef CONFIG_COMPAT
40static struct stack_frame_ia32 *
41dump_user_backtrace_32(struct stack_frame_ia32 *head)
42{
43 struct stack_frame_ia32 bufhead[2];
44 struct stack_frame_ia32 *fp;
45
46 /* Also check accessibility of one struct frame_head beyond */
47 if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
48 return NULL;
49 if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
50 return NULL;
51
52 fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
53
54 oprofile_add_trace(bufhead[0].return_address);
55
56 /* frame pointers should strictly progress back up the stack
57 * (towards higher addresses) */
58 if (head >= fp)
59 return NULL;
60
61 return fp;
62}
63
64static inline int
65x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
66{
67 struct stack_frame_ia32 *head;
68
69 /* User process is 32-bit */
70 if (!current || !test_thread_flag(TIF_IA32))
71 return 0;
72
73 head = (struct stack_frame_ia32 *) regs->bp;
74 while (depth-- && head)
75 head = dump_user_backtrace_32(head);
76
77 return 1;
78}
79
80#else
81static inline int
82x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
83{
84 return 0;
85}
86#endif /* CONFIG_COMPAT */
87
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040088static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040090 struct stack_frame bufhead[2];
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070091
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040092 /* Also check accessibility of one struct stack_frame beyond */
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070093 if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
94 return NULL;
95 if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
96 return NULL;
97
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040098 oprofile_add_trace(bufhead[0].return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /* frame pointers should strictly progress back up the stack
101 * (towards higher addresses) */
Jiri Olsa40c6b3c2010-09-29 10:46:46 -0400102 if (head >= bufhead[0].next_frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return NULL;
104
Jiri Olsa40c6b3c2010-09-29 10:46:46 -0400105 return bufhead[0].next_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108void
109x86_backtrace(struct pt_regs * const regs, unsigned int depth)
110{
Jiri Olsa40c6b3c2010-09-29 10:46:46 -0400111 struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Vincent Hanquezfa1e1bd2005-06-23 00:08:44 -0700113 if (!user_mode_vm(regs)) {
Masami Hiramatsu7b6c6c72009-05-11 17:03:00 -0400114 unsigned long stack = kernel_stack_pointer(regs);
Jan Blunck574a6042007-10-19 20:35:03 +0200115 if (depth)
Namhyung Kime8e999c2011-03-18 11:40:06 +0900116 dump_trace(NULL, regs, (unsigned long *)stack, 0,
Jan Blunck574a6042007-10-19 20:35:03 +0200117 &backtrace_ops, &depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return;
119 }
120
Jiri Olsaf6dedec2010-09-29 10:46:47 -0400121 if (x86_backtrace_32(regs, depth))
122 return;
123
Hugh Dickinsc34d1b42005-10-29 18:16:32 -0700124 while (depth-- && head)
Gerald Britton30379442006-02-14 10:19:04 -0500125 head = dump_user_backtrace(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}