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