dumpstack.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4. */
  5. #include <linux/kallsyms.h>
  6. #include <linux/kprobes.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/utsname.h>
  9. #include <linux/hardirq.h>
  10. #include <linux/kdebug.h>
  11. #include <linux/module.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/ftrace.h>
  14. #include <linux/kexec.h>
  15. #include <linux/bug.h>
  16. #include <linux/nmi.h>
  17. #include <linux/sysfs.h>
  18. #include <asm/stacktrace.h>
  19. int panic_on_unrecovered_nmi;
  20. int panic_on_io_nmi;
  21. unsigned int code_bytes = 64;
  22. int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
  23. static int die_counter;
  24. void printk_address(unsigned long address, int reliable)
  25. {
  26. printk(" [<%p>] %s%pB\n", (void *) address,
  27. reliable ? "" : "? ", (void *) address);
  28. }
  29. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  30. static void
  31. print_ftrace_graph_addr(unsigned long addr, void *data,
  32. const struct stacktrace_ops *ops,
  33. struct thread_info *tinfo, int *graph)
  34. {
  35. struct task_struct *task = tinfo->task;
  36. unsigned long ret_addr;
  37. int index = task->curr_ret_stack;
  38. if (addr != (unsigned long)return_to_handler)
  39. return;
  40. if (!task->ret_stack || index < *graph)
  41. return;
  42. index -= *graph;
  43. ret_addr = task->ret_stack[index].ret;
  44. ops->address(data, ret_addr, 1);
  45. (*graph)++;
  46. }
  47. #else
  48. static inline void
  49. print_ftrace_graph_addr(unsigned long addr, void *data,
  50. const struct stacktrace_ops *ops,
  51. struct thread_info *tinfo, int *graph)
  52. { }
  53. #endif
  54. /*
  55. * x86-64 can have up to three kernel stacks:
  56. * process stack
  57. * interrupt stack
  58. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  59. */
  60. static inline int valid_stack_ptr(struct thread_info *tinfo,
  61. void *p, unsigned int size, void *end)
  62. {
  63. void *t = tinfo;
  64. if (end) {
  65. if (p < end && p >= (end-THREAD_SIZE))
  66. return 1;
  67. else
  68. return 0;
  69. }
  70. return p > t && p < t + THREAD_SIZE - size;
  71. }
  72. unsigned long
  73. print_context_stack(struct thread_info *tinfo,
  74. unsigned long *stack, unsigned long bp,
  75. const struct stacktrace_ops *ops, void *data,
  76. unsigned long *end, int *graph)
  77. {
  78. struct stack_frame *frame = (struct stack_frame *)bp;
  79. while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
  80. unsigned long addr;
  81. addr = *stack;
  82. if (__kernel_text_address(addr)) {
  83. if ((unsigned long) stack == bp + sizeof(long)) {
  84. ops->address(data, addr, 1);
  85. frame = frame->next_frame;
  86. bp = (unsigned long) frame;
  87. } else {
  88. ops->address(data, addr, 0);
  89. }
  90. print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
  91. }
  92. stack++;
  93. }
  94. return bp;
  95. }
  96. EXPORT_SYMBOL_GPL(print_context_stack);
  97. unsigned long
  98. print_context_stack_bp(struct thread_info *tinfo,
  99. unsigned long *stack, unsigned long bp,
  100. const struct stacktrace_ops *ops, void *data,
  101. unsigned long *end, int *graph)
  102. {
  103. struct stack_frame *frame = (struct stack_frame *)bp;
  104. unsigned long *ret_addr = &frame->return_address;
  105. while (valid_stack_ptr(tinfo, ret_addr, sizeof(*ret_addr), end)) {
  106. unsigned long addr = *ret_addr;
  107. if (!__kernel_text_address(addr))
  108. break;
  109. ops->address(data, addr, 1);
  110. frame = frame->next_frame;
  111. ret_addr = &frame->return_address;
  112. print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
  113. }
  114. return (unsigned long)frame;
  115. }
  116. EXPORT_SYMBOL_GPL(print_context_stack_bp);
  117. static int print_trace_stack(void *data, char *name)
  118. {
  119. printk("%s <%s> ", (char *)data, name);
  120. return 0;
  121. }
  122. /*
  123. * Print one address/symbol entries per line.
  124. */
  125. static void print_trace_address(void *data, unsigned long addr, int reliable)
  126. {
  127. touch_nmi_watchdog();
  128. printk(data);
  129. printk_address(addr, reliable);
  130. }
  131. static const struct stacktrace_ops print_trace_ops = {
  132. .stack = print_trace_stack,
  133. .address = print_trace_address,
  134. .walk_stack = print_context_stack,
  135. };
  136. void
  137. show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  138. unsigned long *stack, unsigned long bp, char *log_lvl)
  139. {
  140. printk("%sCall Trace:\n", log_lvl);
  141. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  142. }
  143. void show_trace(struct task_struct *task, struct pt_regs *regs,
  144. unsigned long *stack, unsigned long bp)
  145. {
  146. show_trace_log_lvl(task, regs, stack, bp, "");
  147. }
  148. void show_stack(struct task_struct *task, unsigned long *sp)
  149. {
  150. show_stack_log_lvl(task, NULL, sp, 0, "");
  151. }
  152. /*
  153. * The architecture-independent dump_stack generator
  154. */
  155. void dump_stack(void)
  156. {
  157. unsigned long bp;
  158. unsigned long stack;
  159. bp = stack_frame(current, NULL);
  160. printk("Pid: %d, comm: %.20s %s %s %.*s\n",
  161. current->pid, current->comm, print_tainted(),
  162. init_utsname()->release,
  163. (int)strcspn(init_utsname()->version, " "),
  164. init_utsname()->version);
  165. show_trace(NULL, NULL, &stack, bp);
  166. }
  167. EXPORT_SYMBOL(dump_stack);
  168. static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  169. static int die_owner = -1;
  170. static unsigned int die_nest_count;
  171. unsigned __kprobes long oops_begin(void)
  172. {
  173. int cpu;
  174. unsigned long flags;
  175. oops_enter();
  176. /* racy, but better than risking deadlock. */
  177. raw_local_irq_save(flags);
  178. cpu = smp_processor_id();
  179. if (!arch_spin_trylock(&die_lock)) {
  180. if (cpu == die_owner)
  181. /* nested oops. should stop eventually */;
  182. else
  183. arch_spin_lock(&die_lock);
  184. }
  185. die_nest_count++;
  186. die_owner = cpu;
  187. console_verbose();
  188. bust_spinlocks(1);
  189. return flags;
  190. }
  191. EXPORT_SYMBOL_GPL(oops_begin);
  192. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  193. {
  194. if (regs && kexec_should_crash(current))
  195. crash_kexec(regs);
  196. bust_spinlocks(0);
  197. die_owner = -1;
  198. add_taint(TAINT_DIE);
  199. die_nest_count--;
  200. if (!die_nest_count)
  201. /* Nest count reaches zero, release the lock. */
  202. arch_spin_unlock(&die_lock);
  203. raw_local_irq_restore(flags);
  204. oops_exit();
  205. if (!signr)
  206. return;
  207. if (in_interrupt())
  208. panic("Fatal exception in interrupt");
  209. if (panic_on_oops)
  210. panic("Fatal exception");
  211. do_exit(signr);
  212. }
  213. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  214. {
  215. #ifdef CONFIG_X86_32
  216. unsigned short ss;
  217. unsigned long sp;
  218. #endif
  219. printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
  220. #ifdef CONFIG_PREEMPT
  221. printk("PREEMPT ");
  222. #endif
  223. #ifdef CONFIG_SMP
  224. printk("SMP ");
  225. #endif
  226. #ifdef CONFIG_DEBUG_PAGEALLOC
  227. printk("DEBUG_PAGEALLOC");
  228. #endif
  229. printk("\n");
  230. if (notify_die(DIE_OOPS, str, regs, err,
  231. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  232. return 1;
  233. show_registers(regs);
  234. #ifdef CONFIG_X86_32
  235. if (user_mode_vm(regs)) {
  236. sp = regs->sp;
  237. ss = regs->ss & 0xffff;
  238. } else {
  239. sp = kernel_stack_pointer(regs);
  240. savesegment(ss, ss);
  241. }
  242. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  243. print_symbol("%s", regs->ip);
  244. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  245. #else
  246. /* Executive summary in case the oops scrolled away */
  247. printk(KERN_ALERT "RIP ");
  248. printk_address(regs->ip, 1);
  249. printk(" RSP <%016lx>\n", regs->sp);
  250. #endif
  251. return 0;
  252. }
  253. /*
  254. * This is gone through when something in the kernel has done something bad
  255. * and is about to be terminated:
  256. */
  257. void die(const char *str, struct pt_regs *regs, long err)
  258. {
  259. unsigned long flags = oops_begin();
  260. int sig = SIGSEGV;
  261. if (!user_mode_vm(regs))
  262. report_bug(regs->ip, regs);
  263. if (__die(str, regs, err))
  264. sig = 0;
  265. oops_end(flags, regs, sig);
  266. }
  267. static int __init kstack_setup(char *s)
  268. {
  269. if (!s)
  270. return -EINVAL;
  271. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  272. return 0;
  273. }
  274. early_param("kstack", kstack_setup);
  275. static int __init code_bytes_setup(char *s)
  276. {
  277. code_bytes = simple_strtoul(s, NULL, 0);
  278. if (code_bytes > 8192)
  279. code_bytes = 8192;
  280. return 1;
  281. }
  282. __setup("code_bytes=", code_bytes_setup);