blob: cb31a4440e588552f5fd046ae787c107aa09ec94 [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
Alexei Starovoitov568b3292016-02-17 19:58:57 -080026static int 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);
Alexei Starovoitov568b3292016-02-17 19:58:57 -080032 return 0;
Jan Blunck574a6042007-10-19 20:35:03 +020033}
34
35static struct stacktrace_ops backtrace_ops = {
Frederic Weisbecker61c19172009-12-17 05:40:33 +010036 .stack = backtrace_stack,
37 .address = backtrace_address,
38 .walk_stack = print_context_stack,
Jan Blunck574a6042007-10-19 20:35:03 +020039};
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jiri Olsaf6dedec2010-09-29 10:46:47 -040041#ifdef CONFIG_COMPAT
42static struct stack_frame_ia32 *
43dump_user_backtrace_32(struct stack_frame_ia32 *head)
44{
Robert Richtera0e3e702011-06-03 16:37:47 +020045 /* Also check accessibility of one struct frame_head beyond: */
Jiri Olsaf6dedec2010-09-29 10:46:47 -040046 struct stack_frame_ia32 bufhead[2];
47 struct stack_frame_ia32 *fp;
Robert Richtera0e3e702011-06-03 16:37:47 +020048 unsigned long bytes;
Jiri Olsaf6dedec2010-09-29 10:46:47 -040049
Robert Richtera0e3e702011-06-03 16:37:47 +020050 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
Peter Zijlstra0a196842013-10-30 21:16:22 +010051 if (bytes != 0)
Jiri Olsaf6dedec2010-09-29 10:46:47 -040052 return NULL;
53
54 fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
55
56 oprofile_add_trace(bufhead[0].return_address);
57
58 /* frame pointers should strictly progress back up the stack
59 * (towards higher addresses) */
60 if (head >= fp)
61 return NULL;
62
63 return fp;
64}
65
66static inline int
67x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
68{
69 struct stack_frame_ia32 *head;
70
H. Peter Anvin6bd33002012-02-06 13:03:09 -080071 /* User process is IA32 */
Jiri Olsaf6dedec2010-09-29 10:46:47 -040072 if (!current || !test_thread_flag(TIF_IA32))
73 return 0;
74
75 head = (struct stack_frame_ia32 *) regs->bp;
76 while (depth-- && head)
77 head = dump_user_backtrace_32(head);
78
79 return 1;
80}
81
82#else
83static inline int
84x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
85{
86 return 0;
87}
88#endif /* CONFIG_COMPAT */
89
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040090static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Robert Richtera0e3e702011-06-03 16:37:47 +020092 /* Also check accessibility of one struct frame_head beyond: */
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040093 struct stack_frame bufhead[2];
Robert Richtera0e3e702011-06-03 16:37:47 +020094 unsigned long bytes;
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070095
Robert Richtera0e3e702011-06-03 16:37:47 +020096 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
Peter Zijlstra0a196842013-10-30 21:16:22 +010097 if (bytes != 0)
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070098 return NULL;
99
Jiri Olsa40c6b3c2010-09-29 10:46:46 -0400100 oprofile_add_trace(bufhead[0].return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /* frame pointers should strictly progress back up the stack
103 * (towards higher addresses) */
Jiri Olsa40c6b3c2010-09-29 10:46:46 -0400104 if (head >= bufhead[0].next_frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return NULL;
106
Jiri Olsa40c6b3c2010-09-29 10:46:46 -0400107 return bufhead[0].next_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110void
111x86_backtrace(struct pt_regs * const regs, unsigned int depth)
112{
Jiri Olsa40c6b3c2010-09-29 10:46:46 -0400113 struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700115 if (!user_mode(regs)) {
Masami Hiramatsu7b6c6c72009-05-11 17:03:00 -0400116 unsigned long stack = kernel_stack_pointer(regs);
Jan Blunck574a6042007-10-19 20:35:03 +0200117 if (depth)
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900118 dump_trace(NULL, regs, (unsigned long *)stack, 0,
Jan Blunck574a6042007-10-19 20:35:03 +0200119 &backtrace_ops, &depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return;
121 }
122
Jiri Olsaf6dedec2010-09-29 10:46:47 -0400123 if (x86_backtrace_32(regs, depth))
124 return;
125
Hugh Dickinsc34d1b42005-10-29 18:16:32 -0700126 while (depth-- && head)
Gerald Britton30379442006-02-14 10:19:04 -0500127 head = dump_user_backtrace(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}