dumpstack.c 8.9 KB

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