dumpstack.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. static void printk_stack_address(unsigned long address, int reliable,
  25. void *data)
  26. {
  27. printk("%s [<%p>] %s%pB\n",
  28. (char *)data, (void *)address, reliable ? "" : "? ",
  29. (void *)address);
  30. }
  31. void printk_address(unsigned long address)
  32. {
  33. pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address);
  34. }
  35. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  36. static void
  37. print_ftrace_graph_addr(unsigned long addr, void *data,
  38. const struct stacktrace_ops *ops,
  39. struct thread_info *tinfo, int *graph)
  40. {
  41. struct task_struct *task;
  42. unsigned long ret_addr;
  43. int index;
  44. if (addr != (unsigned long)return_to_handler)
  45. return;
  46. task = tinfo->task;
  47. index = task->curr_ret_stack;
  48. if (!task->ret_stack || index < *graph)
  49. return;
  50. index -= *graph;
  51. ret_addr = task->ret_stack[index].ret;
  52. ops->address(data, ret_addr, 1);
  53. (*graph)++;
  54. }
  55. #else
  56. static inline 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. #endif
  62. /*
  63. * x86-64 can have up to three kernel stacks:
  64. * process stack
  65. * interrupt stack
  66. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  67. */
  68. static inline int valid_stack_ptr(struct thread_info *tinfo,
  69. void *p, unsigned int size, void *end)
  70. {
  71. void *t = tinfo;
  72. if (end) {
  73. if (p < end && p >= (end-THREAD_SIZE))
  74. return 1;
  75. else
  76. return 0;
  77. }
  78. return p > t && p < t + THREAD_SIZE - size;
  79. }
  80. unsigned long
  81. print_context_stack(struct thread_info *tinfo,
  82. unsigned long *stack, unsigned long bp,
  83. const struct stacktrace_ops *ops, void *data,
  84. unsigned long *end, int *graph)
  85. {
  86. struct stack_frame *frame = (struct stack_frame *)bp;
  87. while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
  88. unsigned long addr;
  89. addr = *stack;
  90. if (__kernel_text_address(addr)) {
  91. if ((unsigned long) stack == bp + sizeof(long)) {
  92. ops->address(data, addr, 1);
  93. frame = frame->next_frame;
  94. bp = (unsigned long) frame;
  95. } else {
  96. ops->address(data, addr, 0);
  97. }
  98. print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
  99. }
  100. stack++;
  101. }
  102. return bp;
  103. }
  104. EXPORT_SYMBOL_GPL(print_context_stack);
  105. unsigned long
  106. print_context_stack_bp(struct thread_info *tinfo,
  107. unsigned long *stack, unsigned long bp,
  108. const struct stacktrace_ops *ops, void *data,
  109. unsigned long *end, int *graph)
  110. {
  111. struct stack_frame *frame = (struct stack_frame *)bp;
  112. unsigned long *ret_addr = &frame->return_address;
  113. while (valid_stack_ptr(tinfo, ret_addr, sizeof(*ret_addr), end)) {
  114. unsigned long addr = *ret_addr;
  115. if (!__kernel_text_address(addr))
  116. break;
  117. ops->address(data, addr, 1);
  118. frame = frame->next_frame;
  119. ret_addr = &frame->return_address;
  120. print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
  121. }
  122. return (unsigned long)frame;
  123. }
  124. EXPORT_SYMBOL_GPL(print_context_stack_bp);
  125. static int print_trace_stack(void *data, char *name)
  126. {
  127. printk("%s <%s> ", (char *)data, name);
  128. return 0;
  129. }
  130. /*
  131. * Print one address/symbol entries per line.
  132. */
  133. static void print_trace_address(void *data, unsigned long addr, int reliable)
  134. {
  135. touch_nmi_watchdog();
  136. printk_stack_address(addr, reliable, data);
  137. }
  138. static const struct stacktrace_ops print_trace_ops = {
  139. .stack = print_trace_stack,
  140. .address = print_trace_address,
  141. .walk_stack = print_context_stack,
  142. };
  143. void
  144. show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  145. unsigned long *stack, unsigned long bp, char *log_lvl)
  146. {
  147. printk("%sCall Trace:\n", log_lvl);
  148. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  149. }
  150. void show_trace(struct task_struct *task, struct pt_regs *regs,
  151. unsigned long *stack, unsigned long bp)
  152. {
  153. show_trace_log_lvl(task, regs, stack, bp, "");
  154. }
  155. void show_stack(struct task_struct *task, unsigned long *sp)
  156. {
  157. unsigned long bp = 0;
  158. unsigned long stack;
  159. /*
  160. * Stack frames below this one aren't interesting. Don't show them
  161. * if we're printing for %current.
  162. */
  163. if (!sp && (!task || task == current)) {
  164. sp = &stack;
  165. bp = stack_frame(current, NULL);
  166. }
  167. show_stack_log_lvl(task, NULL, sp, bp, "");
  168. }
  169. static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  170. static int die_owner = -1;
  171. static unsigned int die_nest_count;
  172. unsigned long oops_begin(void)
  173. {
  174. int cpu;
  175. unsigned long flags;
  176. oops_enter();
  177. /* racy, but better than risking deadlock. */
  178. raw_local_irq_save(flags);
  179. cpu = smp_processor_id();
  180. if (!arch_spin_trylock(&die_lock)) {
  181. if (cpu == die_owner)
  182. /* nested oops. should stop eventually */;
  183. else
  184. arch_spin_lock(&die_lock);
  185. }
  186. die_nest_count++;
  187. die_owner = cpu;
  188. console_verbose();
  189. bust_spinlocks(1);
  190. return flags;
  191. }
  192. EXPORT_SYMBOL_GPL(oops_begin);
  193. NOKPROBE_SYMBOL(oops_begin);
  194. void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  195. {
  196. if (regs && kexec_should_crash(current))
  197. crash_kexec(regs);
  198. bust_spinlocks(0);
  199. die_owner = -1;
  200. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  201. die_nest_count--;
  202. if (!die_nest_count)
  203. /* Nest count reaches zero, release the lock. */
  204. arch_spin_unlock(&die_lock);
  205. raw_local_irq_restore(flags);
  206. oops_exit();
  207. if (!signr)
  208. return;
  209. if (in_interrupt())
  210. panic("Fatal exception in interrupt");
  211. if (panic_on_oops)
  212. panic("Fatal exception");
  213. do_exit(signr);
  214. }
  215. NOKPROBE_SYMBOL(oops_end);
  216. int __die(const char *str, struct pt_regs *regs, long err)
  217. {
  218. #ifdef CONFIG_X86_32
  219. unsigned short ss;
  220. unsigned long sp;
  221. #endif
  222. printk(KERN_DEFAULT
  223. "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
  224. #ifdef CONFIG_PREEMPT
  225. printk("PREEMPT ");
  226. #endif
  227. #ifdef CONFIG_SMP
  228. printk("SMP ");
  229. #endif
  230. #ifdef CONFIG_DEBUG_PAGEALLOC
  231. printk("DEBUG_PAGEALLOC ");
  232. #endif
  233. #ifdef CONFIG_KASAN
  234. printk("KASAN");
  235. #endif
  236. printk("\n");
  237. if (notify_die(DIE_OOPS, str, regs, err,
  238. current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
  239. return 1;
  240. print_modules();
  241. show_regs(regs);
  242. #ifdef CONFIG_X86_32
  243. if (user_mode(regs)) {
  244. sp = regs->sp;
  245. ss = regs->ss & 0xffff;
  246. } else {
  247. sp = kernel_stack_pointer(regs);
  248. savesegment(ss, ss);
  249. }
  250. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  251. print_symbol("%s", regs->ip);
  252. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  253. #else
  254. /* Executive summary in case the oops scrolled away */
  255. printk(KERN_ALERT "RIP ");
  256. printk_address(regs->ip);
  257. printk(" RSP <%016lx>\n", regs->sp);
  258. #endif
  259. return 0;
  260. }
  261. NOKPROBE_SYMBOL(__die);
  262. /*
  263. * This is gone through when something in the kernel has done something bad
  264. * and is about to be terminated:
  265. */
  266. void die(const char *str, struct pt_regs *regs, long err)
  267. {
  268. unsigned long flags = oops_begin();
  269. int sig = SIGSEGV;
  270. if (!user_mode(regs))
  271. report_bug(regs->ip, regs);
  272. if (__die(str, regs, err))
  273. sig = 0;
  274. oops_end(flags, regs, sig);
  275. }
  276. static int __init kstack_setup(char *s)
  277. {
  278. ssize_t ret;
  279. unsigned long val;
  280. if (!s)
  281. return -EINVAL;
  282. ret = kstrtoul(s, 0, &val);
  283. if (ret)
  284. return ret;
  285. kstack_depth_to_print = val;
  286. return 0;
  287. }
  288. early_param("kstack", kstack_setup);
  289. static int __init code_bytes_setup(char *s)
  290. {
  291. ssize_t ret;
  292. unsigned long val;
  293. if (!s)
  294. return -EINVAL;
  295. ret = kstrtoul(s, 0, &val);
  296. if (ret)
  297. return ret;
  298. code_bytes = val;
  299. if (code_bytes > 8192)
  300. code_bytes = 8192;
  301. return 1;
  302. }
  303. __setup("code_bytes=", code_bytes_setup);