dumpstack.c 9.0 KB

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