dumpstack.c 9.1 KB

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