dumpstack.c 7.6 KB

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