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