dumpstack.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 (regs = NULL; 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>\n", 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 = *stack;
  100. unsigned long *ret_addr_p =
  101. unwind_get_return_address_ptr(&state);
  102. if (!__kernel_text_address(addr))
  103. continue;
  104. /*
  105. * Don't print regs->ip again if it was already printed
  106. * by __show_regs() below.
  107. */
  108. if (regs && stack == &regs->ip) {
  109. unwind_next_frame(&state);
  110. continue;
  111. }
  112. if (stack == ret_addr_p)
  113. reliable = 1;
  114. /*
  115. * When function graph tracing is enabled for a
  116. * function, its return address on the stack is
  117. * replaced with the address of an ftrace handler
  118. * (return_to_handler). In that case, before printing
  119. * the "real" address, we want to print the handler
  120. * address as an "unreliable" hint that function graph
  121. * tracing was involved.
  122. */
  123. real_addr = ftrace_graph_ret_addr(task, &graph_idx,
  124. addr, stack);
  125. if (real_addr != addr)
  126. printk_stack_address(addr, 0, log_lvl);
  127. printk_stack_address(real_addr, reliable, log_lvl);
  128. if (!reliable)
  129. continue;
  130. /*
  131. * Get the next frame from the unwinder. No need to
  132. * check for an error: if anything goes wrong, the rest
  133. * of the addresses will just be printed as unreliable.
  134. */
  135. unwind_next_frame(&state);
  136. /* if the frame has entry regs, print them */
  137. regs = unwind_get_entry_regs(&state);
  138. if (regs)
  139. __show_regs(regs, 0);
  140. }
  141. if (str_end)
  142. printk("%s <%s>\n", log_lvl, str_end);
  143. }
  144. }
  145. void show_stack(struct task_struct *task, unsigned long *sp)
  146. {
  147. task = task ? : current;
  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 == current)
  153. sp = get_stack_pointer(current, NULL);
  154. show_stack_log_lvl(task, NULL, sp, KERN_DEFAULT);
  155. }
  156. void show_stack_regs(struct pt_regs *regs)
  157. {
  158. show_stack_log_lvl(current, regs, NULL, KERN_DEFAULT);
  159. }
  160. static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  161. static int die_owner = -1;
  162. static unsigned int die_nest_count;
  163. unsigned long oops_begin(void)
  164. {
  165. int cpu;
  166. unsigned long flags;
  167. oops_enter();
  168. /* racy, but better than risking deadlock. */
  169. raw_local_irq_save(flags);
  170. cpu = smp_processor_id();
  171. if (!arch_spin_trylock(&die_lock)) {
  172. if (cpu == die_owner)
  173. /* nested oops. should stop eventually */;
  174. else
  175. arch_spin_lock(&die_lock);
  176. }
  177. die_nest_count++;
  178. die_owner = cpu;
  179. console_verbose();
  180. bust_spinlocks(1);
  181. return flags;
  182. }
  183. EXPORT_SYMBOL_GPL(oops_begin);
  184. NOKPROBE_SYMBOL(oops_begin);
  185. void __noreturn rewind_stack_do_exit(int signr);
  186. void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  187. {
  188. if (regs && kexec_should_crash(current))
  189. crash_kexec(regs);
  190. bust_spinlocks(0);
  191. die_owner = -1;
  192. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  193. die_nest_count--;
  194. if (!die_nest_count)
  195. /* Nest count reaches zero, release the lock. */
  196. arch_spin_unlock(&die_lock);
  197. raw_local_irq_restore(flags);
  198. oops_exit();
  199. if (!signr)
  200. return;
  201. if (in_interrupt())
  202. panic("Fatal exception in interrupt");
  203. if (panic_on_oops)
  204. panic("Fatal exception");
  205. /*
  206. * We're not going to return, but we might be on an IST stack or
  207. * have very little stack space left. Rewind the stack and kill
  208. * the task.
  209. */
  210. rewind_stack_do_exit(signr);
  211. }
  212. NOKPROBE_SYMBOL(oops_end);
  213. int __die(const char *str, struct pt_regs *regs, long err)
  214. {
  215. #ifdef CONFIG_X86_32
  216. unsigned short ss;
  217. unsigned long sp;
  218. #endif
  219. printk(KERN_DEFAULT
  220. "%s: %04lx [#%d]%s%s%s%s\n", str, err & 0xffff, ++die_counter,
  221. IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
  222. IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
  223. debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
  224. IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "");
  225. if (notify_die(DIE_OOPS, str, regs, err,
  226. current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
  227. return 1;
  228. print_modules();
  229. show_regs(regs);
  230. #ifdef CONFIG_X86_32
  231. if (user_mode(regs)) {
  232. sp = regs->sp;
  233. ss = regs->ss & 0xffff;
  234. } else {
  235. sp = kernel_stack_pointer(regs);
  236. savesegment(ss, ss);
  237. }
  238. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  239. print_symbol("%s", regs->ip);
  240. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  241. #else
  242. /* Executive summary in case the oops scrolled away */
  243. printk(KERN_ALERT "RIP ");
  244. printk_address(regs->ip);
  245. printk(" RSP <%016lx>\n", regs->sp);
  246. #endif
  247. return 0;
  248. }
  249. NOKPROBE_SYMBOL(__die);
  250. /*
  251. * This is gone through when something in the kernel has done something bad
  252. * and is about to be terminated:
  253. */
  254. void die(const char *str, struct pt_regs *regs, long err)
  255. {
  256. unsigned long flags = oops_begin();
  257. int sig = SIGSEGV;
  258. if (!user_mode(regs))
  259. report_bug(regs->ip, regs);
  260. if (__die(str, regs, err))
  261. sig = 0;
  262. oops_end(flags, regs, sig);
  263. }
  264. static int __init kstack_setup(char *s)
  265. {
  266. ssize_t ret;
  267. unsigned long val;
  268. if (!s)
  269. return -EINVAL;
  270. ret = kstrtoul(s, 0, &val);
  271. if (ret)
  272. return ret;
  273. kstack_depth_to_print = val;
  274. return 0;
  275. }
  276. early_param("kstack", kstack_setup);
  277. static int __init code_bytes_setup(char *s)
  278. {
  279. ssize_t ret;
  280. unsigned long val;
  281. if (!s)
  282. return -EINVAL;
  283. ret = kstrtoul(s, 0, &val);
  284. if (ret)
  285. return ret;
  286. code_bytes = val;
  287. if (code_bytes > 8192)
  288. code_bytes = 8192;
  289. return 1;
  290. }
  291. __setup("code_bytes=", code_bytes_setup);