dumpstack.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include <asm/unwind.h>
  20. int panic_on_unrecovered_nmi;
  21. int panic_on_io_nmi;
  22. unsigned int code_bytes = 64;
  23. int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
  24. static int die_counter;
  25. bool in_task_stack(unsigned long *stack, struct task_struct *task,
  26. struct stack_info *info)
  27. {
  28. unsigned long *begin = task_stack_page(task);
  29. unsigned long *end = task_stack_page(task) + THREAD_SIZE;
  30. if (stack < begin || stack >= end)
  31. return false;
  32. info->type = STACK_TYPE_TASK;
  33. info->begin = begin;
  34. info->end = end;
  35. info->next_sp = NULL;
  36. return true;
  37. }
  38. static void printk_stack_address(unsigned long address, int reliable,
  39. char *log_lvl)
  40. {
  41. touch_nmi_watchdog();
  42. printk("%s [<%p>] %s%pB\n",
  43. log_lvl, (void *)address, reliable ? "" : "? ",
  44. (void *)address);
  45. }
  46. void printk_address(unsigned long address)
  47. {
  48. pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address);
  49. }
  50. void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  51. unsigned long *stack, char *log_lvl)
  52. {
  53. struct unwind_state state;
  54. struct stack_info stack_info = {0};
  55. unsigned long visit_mask = 0;
  56. int graph_idx = 0;
  57. printk("%sCall Trace:\n", log_lvl);
  58. unwind_start(&state, task, regs, stack);
  59. /*
  60. * Iterate through the stacks, starting with the current stack pointer.
  61. * Each stack has a pointer to the next one.
  62. *
  63. * x86-64 can have several stacks:
  64. * - task stack
  65. * - interrupt stack
  66. * - HW exception stacks (double fault, nmi, debug, mce)
  67. *
  68. * x86-32 can have up to three stacks:
  69. * - task stack
  70. * - softirq stack
  71. * - hardirq stack
  72. */
  73. for (; stack; stack = stack_info.next_sp) {
  74. const char *str_begin, *str_end;
  75. /*
  76. * If we overflowed the task stack into a guard page, jump back
  77. * to the bottom of the usable stack.
  78. */
  79. if (task_stack_page(task) - (void *)stack < PAGE_SIZE)
  80. stack = task_stack_page(task);
  81. if (get_stack_info(stack, task, &stack_info, &visit_mask))
  82. break;
  83. stack_type_str(stack_info.type, &str_begin, &str_end);
  84. if (str_begin)
  85. printk("%s <%s> ", log_lvl, str_begin);
  86. /*
  87. * Scan the stack, printing any text addresses we find. At the
  88. * same time, follow proper stack frames with the unwinder.
  89. *
  90. * Addresses found during the scan which are not reported by
  91. * the unwinder are considered to be additional clues which are
  92. * sometimes useful for debugging and are prefixed with '?'.
  93. * This also serves as a failsafe option in case the unwinder
  94. * goes off in the weeds.
  95. */
  96. for (; stack < stack_info.end; stack++) {
  97. unsigned long real_addr;
  98. int reliable = 0;
  99. unsigned long addr = READ_ONCE_NOCHECK(*stack);
  100. unsigned long *ret_addr_p =
  101. unwind_get_return_address_ptr(&state);
  102. if (!__kernel_text_address(addr))
  103. continue;
  104. if (stack == ret_addr_p)
  105. reliable = 1;
  106. /*
  107. * When function graph tracing is enabled for a
  108. * function, its return address on the stack is
  109. * replaced with the address of an ftrace handler
  110. * (return_to_handler). In that case, before printing
  111. * the "real" address, we want to print the handler
  112. * address as an "unreliable" hint that function graph
  113. * tracing was involved.
  114. */
  115. real_addr = ftrace_graph_ret_addr(task, &graph_idx,
  116. addr, stack);
  117. if (real_addr != addr)
  118. printk_stack_address(addr, 0, log_lvl);
  119. printk_stack_address(real_addr, reliable, log_lvl);
  120. if (!reliable)
  121. continue;
  122. /*
  123. * Get the next frame from the unwinder. No need to
  124. * check for an error: if anything goes wrong, the rest
  125. * of the addresses will just be printed as unreliable.
  126. */
  127. unwind_next_frame(&state);
  128. }
  129. if (str_end)
  130. printk("%s <%s> ", log_lvl, str_end);
  131. }
  132. }
  133. void show_stack(struct task_struct *task, unsigned long *sp)
  134. {
  135. task = task ? : current;
  136. /*
  137. * Stack frames below this one aren't interesting. Don't show them
  138. * if we're printing for %current.
  139. */
  140. if (!sp && task == current)
  141. sp = get_stack_pointer(current, NULL);
  142. show_stack_log_lvl(task, NULL, sp, "");
  143. }
  144. void show_stack_regs(struct pt_regs *regs)
  145. {
  146. show_stack_log_lvl(current, regs, NULL, "");
  147. }
  148. static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  149. static int die_owner = -1;
  150. static unsigned int die_nest_count;
  151. unsigned long oops_begin(void)
  152. {
  153. int cpu;
  154. unsigned long flags;
  155. oops_enter();
  156. /* racy, but better than risking deadlock. */
  157. raw_local_irq_save(flags);
  158. cpu = smp_processor_id();
  159. if (!arch_spin_trylock(&die_lock)) {
  160. if (cpu == die_owner)
  161. /* nested oops. should stop eventually */;
  162. else
  163. arch_spin_lock(&die_lock);
  164. }
  165. die_nest_count++;
  166. die_owner = cpu;
  167. console_verbose();
  168. bust_spinlocks(1);
  169. return flags;
  170. }
  171. EXPORT_SYMBOL_GPL(oops_begin);
  172. NOKPROBE_SYMBOL(oops_begin);
  173. void __noreturn rewind_stack_do_exit(int signr);
  174. void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  175. {
  176. if (regs && kexec_should_crash(current))
  177. crash_kexec(regs);
  178. bust_spinlocks(0);
  179. die_owner = -1;
  180. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  181. die_nest_count--;
  182. if (!die_nest_count)
  183. /* Nest count reaches zero, release the lock. */
  184. arch_spin_unlock(&die_lock);
  185. raw_local_irq_restore(flags);
  186. oops_exit();
  187. if (!signr)
  188. return;
  189. if (in_interrupt())
  190. panic("Fatal exception in interrupt");
  191. if (panic_on_oops)
  192. panic("Fatal exception");
  193. /*
  194. * We're not going to return, but we might be on an IST stack or
  195. * have very little stack space left. Rewind the stack and kill
  196. * the task.
  197. */
  198. rewind_stack_do_exit(signr);
  199. }
  200. NOKPROBE_SYMBOL(oops_end);
  201. int __die(const char *str, struct pt_regs *regs, long err)
  202. {
  203. #ifdef CONFIG_X86_32
  204. unsigned short ss;
  205. unsigned long sp;
  206. #endif
  207. printk(KERN_DEFAULT
  208. "%s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff, ++die_counter,
  209. IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
  210. IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
  211. debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
  212. IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "");
  213. if (notify_die(DIE_OOPS, str, regs, err,
  214. current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
  215. return 1;
  216. print_modules();
  217. show_regs(regs);
  218. #ifdef CONFIG_X86_32
  219. if (user_mode(regs)) {
  220. sp = regs->sp;
  221. ss = regs->ss & 0xffff;
  222. } else {
  223. sp = kernel_stack_pointer(regs);
  224. savesegment(ss, ss);
  225. }
  226. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  227. print_symbol("%s", regs->ip);
  228. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  229. #else
  230. /* Executive summary in case the oops scrolled away */
  231. printk(KERN_ALERT "RIP ");
  232. printk_address(regs->ip);
  233. printk(" RSP <%016lx>\n", regs->sp);
  234. #endif
  235. return 0;
  236. }
  237. NOKPROBE_SYMBOL(__die);
  238. /*
  239. * This is gone through when something in the kernel has done something bad
  240. * and is about to be terminated:
  241. */
  242. void die(const char *str, struct pt_regs *regs, long err)
  243. {
  244. unsigned long flags = oops_begin();
  245. int sig = SIGSEGV;
  246. if (!user_mode(regs))
  247. report_bug(regs->ip, regs);
  248. if (__die(str, regs, err))
  249. sig = 0;
  250. oops_end(flags, regs, sig);
  251. }
  252. static int __init kstack_setup(char *s)
  253. {
  254. ssize_t ret;
  255. unsigned long val;
  256. if (!s)
  257. return -EINVAL;
  258. ret = kstrtoul(s, 0, &val);
  259. if (ret)
  260. return ret;
  261. kstack_depth_to_print = val;
  262. return 0;
  263. }
  264. early_param("kstack", kstack_setup);
  265. static int __init code_bytes_setup(char *s)
  266. {
  267. ssize_t ret;
  268. unsigned long val;
  269. if (!s)
  270. return -EINVAL;
  271. ret = kstrtoul(s, 0, &val);
  272. if (ret)
  273. return ret;
  274. code_bytes = val;
  275. if (code_bytes > 8192)
  276. code_bytes = 8192;
  277. return 1;
  278. }
  279. __setup("code_bytes=", code_bytes_setup);