dumpstack.c 7.7 KB

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