dumpstack.c 7.9 KB

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