dumpstack.c 9.7 KB

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