blob: fdd0c6430e5abc97c878d192439447298b2fd06c [file] [log] [blame]
Ingo Molnar21b32bb2006-07-03 00:24:40 -07001/*
Ingo Molnar21b32bb2006-07-03 00:24:40 -07002 * Stack trace management functions
3 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01004 * Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
Ingo Molnar21b32bb2006-07-03 00:24:40 -07005 */
6#include <linux/sched.h>
7#include <linux/stacktrace.h>
Andi Kleenc0b766f2006-09-26 10:52:34 +02008#include <linux/module.h>
Török Edwin02b67512008-11-22 13:28:47 +02009#include <linux/uaccess.h>
Andi Kleenc0b766f2006-09-26 10:52:34 +020010#include <asm/stacktrace.h>
Ingo Molnar21b32bb2006-07-03 00:24:40 -070011
Andi Kleenc0b766f2006-09-26 10:52:34 +020012static int save_stack_stack(void *data, char *name)
Ingo Molnar21b32bb2006-07-03 00:24:40 -070013{
Steven Rostedt29a67972009-05-14 23:19:09 -040014 return 0;
Ingo Molnar21b32bb2006-07-03 00:24:40 -070015}
16
Oleg Nesterov018378c2010-06-03 21:32:43 +020017static void
18__save_stack_address(void *data, unsigned long addr, bool reliable, bool nosched)
Andi Kleenc0b766f2006-09-26 10:52:34 +020019{
Jan Engelhardtade1af72008-01-30 13:33:23 +010020 struct stack_trace *trace = data;
Oleg Nesterov147ec4d2010-06-03 21:32:39 +020021#ifdef CONFIG_FRAME_POINTER
Vegard Nossum16507432008-02-22 19:23:58 +010022 if (!reliable)
23 return;
Oleg Nesterov147ec4d2010-06-03 21:32:39 +020024#endif
Oleg Nesterov018378c2010-06-03 21:32:43 +020025 if (nosched && in_sched_functions(addr))
26 return;
Andi Kleenc0b766f2006-09-26 10:52:34 +020027 if (trace->skip > 0) {
28 trace->skip--;
29 return;
30 }
Catalin Marinas006e84ee2007-02-13 13:26:21 +010031 if (trace->nr_entries < trace->max_entries)
Andi Kleenc0b766f2006-09-26 10:52:34 +020032 trace->entries[trace->nr_entries++] = addr;
33}
34
Oleg Nesterov018378c2010-06-03 21:32:43 +020035static void save_stack_address(void *data, unsigned long addr, int reliable)
36{
37 return __save_stack_address(data, addr, reliable, false);
38}
39
Arjan van de Ven5bc27dc2008-01-30 13:33:07 +010040static void
41save_stack_address_nosched(void *data, unsigned long addr, int reliable)
Arjan van de Ven97455122008-01-25 21:08:34 +010042{
Oleg Nesterov018378c2010-06-03 21:32:43 +020043 return __save_stack_address(data, addr, reliable, true);
Arjan van de Ven97455122008-01-25 21:08:34 +010044}
45
Jan Beulich9689ba82007-10-17 18:04:37 +020046static const struct stacktrace_ops save_stack_ops = {
Frederic Weisbecker61c19172009-12-17 05:40:33 +010047 .stack = save_stack_stack,
48 .address = save_stack_address,
49 .walk_stack = print_context_stack,
Andi Kleenc0b766f2006-09-26 10:52:34 +020050};
Ingo Molnar21b32bb2006-07-03 00:24:40 -070051
Arjan van de Ven97455122008-01-25 21:08:34 +010052static const struct stacktrace_ops save_stack_ops_nosched = {
Frederic Weisbecker61c19172009-12-17 05:40:33 +010053 .stack = save_stack_stack,
54 .address = save_stack_address_nosched,
55 .walk_stack = print_context_stack,
Arjan van de Ven97455122008-01-25 21:08:34 +010056};
57
Ingo Molnar21b32bb2006-07-03 00:24:40 -070058/*
59 * Save stack-backtrace addresses into a stack_trace buffer.
Ingo Molnar21b32bb2006-07-03 00:24:40 -070060 */
Christoph Hellwigab1b6f02007-05-08 00:23:29 -070061void save_stack_trace(struct stack_trace *trace)
Ingo Molnar21b32bb2006-07-03 00:24:40 -070062{
Namhyung Kime8e999cf2011-03-18 11:40:06 +090063 dump_trace(current, NULL, NULL, 0, &save_stack_ops, trace);
Catalin Marinas006e84ee2007-02-13 13:26:21 +010064 if (trace->nr_entries < trace->max_entries)
65 trace->entries[trace->nr_entries++] = ULONG_MAX;
Ingo Molnar21b32bb2006-07-03 00:24:40 -070066}
Ingo Molnar85946982008-06-27 21:20:17 +020067EXPORT_SYMBOL_GPL(save_stack_trace);
Arjan van de Ven97455122008-01-25 21:08:34 +010068
Masami Hiramatsu39581062011-06-08 16:09:21 +090069void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
Vegard Nossumacc6be52008-05-20 11:15:43 +020070{
Namhyung Kime8e999cf2011-03-18 11:40:06 +090071 dump_trace(current, regs, NULL, 0, &save_stack_ops, trace);
Vegard Nossumacc6be52008-05-20 11:15:43 +020072 if (trace->nr_entries < trace->max_entries)
73 trace->entries[trace->nr_entries++] = ULONG_MAX;
74}
75
Arjan van de Ven97455122008-01-25 21:08:34 +010076void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
77{
Namhyung Kime8e999cf2011-03-18 11:40:06 +090078 dump_trace(tsk, NULL, NULL, 0, &save_stack_ops_nosched, trace);
Arjan van de Ven97455122008-01-25 21:08:34 +010079 if (trace->nr_entries < trace->max_entries)
80 trace->entries[trace->nr_entries++] = ULONG_MAX;
81}
Ingo Molnar85946982008-06-27 21:20:17 +020082EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
Török Edwin02b67512008-11-22 13:28:47 +020083
84/* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
85
Frederic Weisbeckerc9cf4db2010-05-19 21:35:17 +020086struct stack_frame_user {
Török Edwin02b67512008-11-22 13:28:47 +020087 const void __user *next_fp;
Török Edwin8d7c6a92008-11-23 12:39:06 +020088 unsigned long ret_addr;
Török Edwin02b67512008-11-22 13:28:47 +020089};
90
Frederic Weisbeckerc9cf4db2010-05-19 21:35:17 +020091static int
92copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
Török Edwin02b67512008-11-22 13:28:47 +020093{
94 int ret;
95
96 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
97 return 0;
98
99 ret = 1;
100 pagefault_disable();
101 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
102 ret = 0;
103 pagefault_enable();
104
105 return ret;
106}
107
Török Edwin8d7c6a92008-11-23 12:39:06 +0200108static inline void __save_stack_trace_user(struct stack_trace *trace)
109{
110 const struct pt_regs *regs = task_pt_regs(current);
111 const void __user *fp = (const void __user *)regs->bp;
112
113 if (trace->nr_entries < trace->max_entries)
114 trace->entries[trace->nr_entries++] = regs->ip;
115
116 while (trace->nr_entries < trace->max_entries) {
Frederic Weisbeckerc9cf4db2010-05-19 21:35:17 +0200117 struct stack_frame_user frame;
Török Edwin8d7c6a92008-11-23 12:39:06 +0200118
119 frame.next_fp = NULL;
120 frame.ret_addr = 0;
121 if (!copy_stack_frame(fp, &frame))
122 break;
123 if ((unsigned long)fp < regs->sp)
124 break;
125 if (frame.ret_addr) {
126 trace->entries[trace->nr_entries++] =
127 frame.ret_addr;
128 }
129 if (fp == frame.next_fp)
130 break;
131 fp = frame.next_fp;
132 }
133}
134
Török Edwin02b67512008-11-22 13:28:47 +0200135void save_stack_trace_user(struct stack_trace *trace)
136{
137 /*
138 * Trace user stack if we are not a kernel thread
139 */
140 if (current->mm) {
Török Edwin8d7c6a92008-11-23 12:39:06 +0200141 __save_stack_trace_user(trace);
Török Edwin02b67512008-11-22 13:28:47 +0200142 }
143 if (trace->nr_entries < trace->max_entries)
144 trace->entries[trace->nr_entries++] = ULONG_MAX;
145}
146