Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 3 | * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs |
| 4 | * Copyright (C) 2009 Matt Fleming |
Paul Mundt | 4945326 | 2012-05-24 15:03:46 +0900 | [diff] [blame] | 5 | * Copyright (C) 2002 - 2012 Paul Mundt |
| 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License. See the file "COPYING" in the main directory of this archive |
| 9 | * for more details. |
Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 10 | */ |
| 11 | #include <linux/kallsyms.h> |
| 12 | #include <linux/ftrace.h> |
| 13 | #include <linux/debug_locks.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 14 | #include <linux/sched/debug.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 15 | #include <linux/sched/task_stack.h> |
Paul Mundt | 4945326 | 2012-05-24 15:03:46 +0900 | [diff] [blame] | 16 | #include <linux/kdebug.h> |
| 17 | #include <linux/export.h> |
| 18 | #include <linux/uaccess.h> |
Matt Fleming | 0eff9f66 | 2009-08-11 22:43:20 +0100 | [diff] [blame] | 19 | #include <asm/unwinder.h> |
Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 20 | #include <asm/stacktrace.h> |
| 21 | |
Paul Mundt | 4945326 | 2012-05-24 15:03:46 +0900 | [diff] [blame] | 22 | void dump_mem(const char *str, unsigned long bottom, unsigned long top) |
| 23 | { |
| 24 | unsigned long p; |
| 25 | int i; |
| 26 | |
| 27 | printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top); |
| 28 | |
| 29 | for (p = bottom & ~31; p < top; ) { |
| 30 | printk("%04lx: ", p & 0xffff); |
| 31 | |
| 32 | for (i = 0; i < 8; i++, p += 4) { |
| 33 | unsigned int val; |
| 34 | |
| 35 | if (p < bottom || p >= top) |
| 36 | printk(" "); |
| 37 | else { |
| 38 | if (__get_user(val, (unsigned int __user *)p)) { |
| 39 | printk("\n"); |
| 40 | return; |
| 41 | } |
| 42 | printk("%08x ", val); |
| 43 | } |
| 44 | } |
| 45 | printk("\n"); |
| 46 | } |
| 47 | } |
| 48 | |
Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 49 | void printk_address(unsigned long address, int reliable) |
| 50 | { |
| 51 | printk(" [<%p>] %s%pS\n", (void *) address, |
| 52 | reliable ? "" : "? ", (void *) address); |
| 53 | } |
| 54 | |
| 55 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 56 | static void |
| 57 | print_ftrace_graph_addr(unsigned long addr, void *data, |
| 58 | const struct stacktrace_ops *ops, |
| 59 | struct thread_info *tinfo, int *graph) |
| 60 | { |
| 61 | struct task_struct *task = tinfo->task; |
| 62 | unsigned long ret_addr; |
| 63 | int index = task->curr_ret_stack; |
| 64 | |
| 65 | if (addr != (unsigned long)return_to_handler) |
| 66 | return; |
| 67 | |
| 68 | if (!task->ret_stack || index < *graph) |
| 69 | return; |
| 70 | |
| 71 | index -= *graph; |
| 72 | ret_addr = task->ret_stack[index].ret; |
| 73 | |
| 74 | ops->address(data, ret_addr, 1); |
| 75 | |
| 76 | (*graph)++; |
| 77 | } |
| 78 | #else |
| 79 | static inline void |
| 80 | print_ftrace_graph_addr(unsigned long addr, void *data, |
| 81 | const struct stacktrace_ops *ops, |
| 82 | struct thread_info *tinfo, int *graph) |
| 83 | { } |
| 84 | #endif |
| 85 | |
Matt Fleming | 0eff9f66 | 2009-08-11 22:43:20 +0100 | [diff] [blame] | 86 | void |
| 87 | stack_reader_dump(struct task_struct *task, struct pt_regs *regs, |
| 88 | unsigned long *sp, const struct stacktrace_ops *ops, |
| 89 | void *data) |
Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 90 | { |
| 91 | struct thread_info *context; |
| 92 | int graph = 0; |
| 93 | |
| 94 | context = (struct thread_info *) |
| 95 | ((unsigned long)sp & (~(THREAD_SIZE - 1))); |
| 96 | |
| 97 | while (!kstack_end(sp)) { |
| 98 | unsigned long addr = *sp++; |
| 99 | |
| 100 | if (__kernel_text_address(addr)) { |
Paul Mundt | f9967e2 | 2009-08-15 01:09:03 +0900 | [diff] [blame] | 101 | ops->address(data, addr, 1); |
Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 102 | |
| 103 | print_ftrace_graph_addr(addr, data, ops, |
| 104 | context, &graph); |
| 105 | } |
| 106 | } |
| 107 | } |
Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 108 | |
Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 109 | static int print_trace_stack(void *data, char *name) |
| 110 | { |
| 111 | printk("%s <%s> ", (char *)data, name); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Print one address/symbol entries per line. |
| 117 | */ |
| 118 | static void print_trace_address(void *data, unsigned long addr, int reliable) |
| 119 | { |
Matt Fleming | a0c3276 | 2014-04-03 14:46:20 -0700 | [diff] [blame] | 120 | printk("%s", (char *)data); |
Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 121 | printk_address(addr, reliable); |
| 122 | } |
| 123 | |
| 124 | static const struct stacktrace_ops print_trace_ops = { |
Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 125 | .stack = print_trace_stack, |
| 126 | .address = print_trace_address, |
| 127 | }; |
| 128 | |
| 129 | void show_trace(struct task_struct *tsk, unsigned long *sp, |
| 130 | struct pt_regs *regs) |
| 131 | { |
| 132 | if (regs && user_mode(regs)) |
| 133 | return; |
| 134 | |
| 135 | printk("\nCall trace:\n"); |
| 136 | |
Matt Fleming | 0eff9f66 | 2009-08-11 22:43:20 +0100 | [diff] [blame] | 137 | unwind_stack(tsk, regs, sp, &print_trace_ops, ""); |
Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 138 | |
| 139 | printk("\n"); |
| 140 | |
| 141 | if (!tsk) |
| 142 | tsk = current; |
| 143 | |
| 144 | debug_show_held_locks(tsk); |
| 145 | } |
Paul Mundt | 4945326 | 2012-05-24 15:03:46 +0900 | [diff] [blame] | 146 | |
| 147 | void show_stack(struct task_struct *tsk, unsigned long *sp) |
| 148 | { |
| 149 | unsigned long stack; |
| 150 | |
| 151 | if (!tsk) |
| 152 | tsk = current; |
| 153 | if (tsk == current) |
| 154 | sp = (unsigned long *)current_stack_pointer; |
| 155 | else |
| 156 | sp = (unsigned long *)tsk->thread.sp; |
| 157 | |
| 158 | stack = (unsigned long)sp; |
| 159 | dump_mem("Stack: ", stack, THREAD_SIZE + |
| 160 | (unsigned long)task_stack_page(tsk)); |
| 161 | show_trace(tsk, sp, NULL); |
| 162 | } |