Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 1 | /* |
Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 2 | * Stack trace management functions |
| 3 | * |
Ingo Molnar | 8f47e16 | 2009-01-31 02:03:42 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/sched.h> |
| 7 | #include <linux/stacktrace.h> |
Andi Kleen | c0b766f | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 8 | #include <linux/module.h> |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 9 | #include <linux/uaccess.h> |
Andi Kleen | c0b766f | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 10 | #include <asm/stacktrace.h> |
Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 11 | |
Andi Kleen | c0b766f | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 12 | static int save_stack_stack(void *data, char *name) |
Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 13 | { |
Steven Rostedt | 29a6797 | 2009-05-14 23:19:09 -0400 | [diff] [blame] | 14 | return 0; |
Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 15 | } |
| 16 | |
Oleg Nesterov | 018378c | 2010-06-03 21:32:43 +0200 | [diff] [blame] | 17 | static void |
| 18 | __save_stack_address(void *data, unsigned long addr, bool reliable, bool nosched) |
Andi Kleen | c0b766f | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 19 | { |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 20 | struct stack_trace *trace = data; |
Oleg Nesterov | 147ec4d | 2010-06-03 21:32:39 +0200 | [diff] [blame] | 21 | #ifdef CONFIG_FRAME_POINTER |
Vegard Nossum | 1650743 | 2008-02-22 19:23:58 +0100 | [diff] [blame] | 22 | if (!reliable) |
| 23 | return; |
Oleg Nesterov | 147ec4d | 2010-06-03 21:32:39 +0200 | [diff] [blame] | 24 | #endif |
Oleg Nesterov | 018378c | 2010-06-03 21:32:43 +0200 | [diff] [blame] | 25 | if (nosched && in_sched_functions(addr)) |
| 26 | return; |
Andi Kleen | c0b766f | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 27 | if (trace->skip > 0) { |
| 28 | trace->skip--; |
| 29 | return; |
| 30 | } |
Catalin Marinas | 006e84ee | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 31 | if (trace->nr_entries < trace->max_entries) |
Andi Kleen | c0b766f | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 32 | trace->entries[trace->nr_entries++] = addr; |
| 33 | } |
| 34 | |
Oleg Nesterov | 018378c | 2010-06-03 21:32:43 +0200 | [diff] [blame] | 35 | static 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 Ven | 5bc27dc | 2008-01-30 13:33:07 +0100 | [diff] [blame] | 40 | static void |
| 41 | save_stack_address_nosched(void *data, unsigned long addr, int reliable) |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 42 | { |
Oleg Nesterov | 018378c | 2010-06-03 21:32:43 +0200 | [diff] [blame] | 43 | return __save_stack_address(data, addr, reliable, true); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Jan Beulich | 9689ba8 | 2007-10-17 18:04:37 +0200 | [diff] [blame] | 46 | static const struct stacktrace_ops save_stack_ops = { |
Frederic Weisbecker | 61c1917 | 2009-12-17 05:40:33 +0100 | [diff] [blame] | 47 | .stack = save_stack_stack, |
| 48 | .address = save_stack_address, |
| 49 | .walk_stack = print_context_stack, |
Andi Kleen | c0b766f | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 50 | }; |
Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 51 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 52 | static const struct stacktrace_ops save_stack_ops_nosched = { |
Frederic Weisbecker | 61c1917 | 2009-12-17 05:40:33 +0100 | [diff] [blame] | 53 | .stack = save_stack_stack, |
| 54 | .address = save_stack_address_nosched, |
| 55 | .walk_stack = print_context_stack, |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Save stack-backtrace addresses into a stack_trace buffer. |
Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 60 | */ |
Christoph Hellwig | ab1b6f0 | 2007-05-08 00:23:29 -0700 | [diff] [blame] | 61 | void save_stack_trace(struct stack_trace *trace) |
Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 62 | { |
Namhyung Kim | e8e999cf | 2011-03-18 11:40:06 +0900 | [diff] [blame] | 63 | dump_trace(current, NULL, NULL, 0, &save_stack_ops, trace); |
Catalin Marinas | 006e84ee | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 64 | if (trace->nr_entries < trace->max_entries) |
| 65 | trace->entries[trace->nr_entries++] = ULONG_MAX; |
Ingo Molnar | 21b32bb | 2006-07-03 00:24:40 -0700 | [diff] [blame] | 66 | } |
Ingo Molnar | 8594698 | 2008-06-27 21:20:17 +0200 | [diff] [blame] | 67 | EXPORT_SYMBOL_GPL(save_stack_trace); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 68 | |
Masami Hiramatsu | 3958106 | 2011-06-08 16:09:21 +0900 | [diff] [blame] | 69 | void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) |
Vegard Nossum | acc6be5 | 2008-05-20 11:15:43 +0200 | [diff] [blame] | 70 | { |
Namhyung Kim | e8e999cf | 2011-03-18 11:40:06 +0900 | [diff] [blame] | 71 | dump_trace(current, regs, NULL, 0, &save_stack_ops, trace); |
Vegard Nossum | acc6be5 | 2008-05-20 11:15:43 +0200 | [diff] [blame] | 72 | if (trace->nr_entries < trace->max_entries) |
| 73 | trace->entries[trace->nr_entries++] = ULONG_MAX; |
| 74 | } |
| 75 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 76 | void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) |
| 77 | { |
Namhyung Kim | e8e999cf | 2011-03-18 11:40:06 +0900 | [diff] [blame] | 78 | dump_trace(tsk, NULL, NULL, 0, &save_stack_ops_nosched, trace); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 79 | if (trace->nr_entries < trace->max_entries) |
| 80 | trace->entries[trace->nr_entries++] = ULONG_MAX; |
| 81 | } |
Ingo Molnar | 8594698 | 2008-06-27 21:20:17 +0200 | [diff] [blame] | 82 | EXPORT_SYMBOL_GPL(save_stack_trace_tsk); |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 83 | |
| 84 | /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */ |
| 85 | |
Frederic Weisbecker | c9cf4db | 2010-05-19 21:35:17 +0200 | [diff] [blame] | 86 | struct stack_frame_user { |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 87 | const void __user *next_fp; |
Török Edwin | 8d7c6a9 | 2008-11-23 12:39:06 +0200 | [diff] [blame] | 88 | unsigned long ret_addr; |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
Frederic Weisbecker | c9cf4db | 2010-05-19 21:35:17 +0200 | [diff] [blame] | 91 | static int |
| 92 | copy_stack_frame(const void __user *fp, struct stack_frame_user *frame) |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 93 | { |
| 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 Edwin | 8d7c6a9 | 2008-11-23 12:39:06 +0200 | [diff] [blame] | 108 | static 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 Weisbecker | c9cf4db | 2010-05-19 21:35:17 +0200 | [diff] [blame] | 117 | struct stack_frame_user frame; |
Török Edwin | 8d7c6a9 | 2008-11-23 12:39:06 +0200 | [diff] [blame] | 118 | |
| 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 Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 135 | void 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 Edwin | 8d7c6a9 | 2008-11-23 12:39:06 +0200 | [diff] [blame] | 141 | __save_stack_trace_user(trace); |
Török Edwin | 02b6751 | 2008-11-22 13:28:47 +0200 | [diff] [blame] | 142 | } |
| 143 | if (trace->nr_entries < trace->max_entries) |
| 144 | trace->entries[trace->nr_entries++] = ULONG_MAX; |
| 145 | } |
| 146 | |