dumpstack.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/ftrace.h>
  14. #include <linux/kexec.h>
  15. #include <linux/bug.h>
  16. #include <linux/nmi.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/ftrace.h>
  19. #include <asm/stacktrace.h>
  20. #include "dumpstack.h"
  21. int panic_on_unrecovered_nmi;
  22. int panic_on_io_nmi;
  23. unsigned int code_bytes = 64;
  24. int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
  25. static int die_counter;
  26. void printk_address(unsigned long address, int reliable)
  27. {
  28. printk(" [<%p>] %s%pS\n", (void *) address,
  29. reliable ? "" : "? ", (void *) address);
  30. }
  31. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  32. static void
  33. print_ftrace_graph_addr(unsigned long addr, void *data,
  34. const struct stacktrace_ops *ops,
  35. struct thread_info *tinfo, int *graph)
  36. {
  37. struct task_struct *task = tinfo->task;
  38. unsigned long ret_addr;
  39. int index = task->curr_ret_stack;
  40. if (addr != (unsigned long)return_to_handler)
  41. return;
  42. if (!task->ret_stack || index < *graph)
  43. return;
  44. index -= *graph;
  45. ret_addr = task->ret_stack[index].ret;
  46. ops->address(data, ret_addr, 1);
  47. (*graph)++;
  48. }
  49. #else
  50. static inline void
  51. print_ftrace_graph_addr(unsigned long addr, void *data,
  52. const struct stacktrace_ops *ops,
  53. struct thread_info *tinfo, int *graph)
  54. { }
  55. #endif
  56. /*
  57. * x86-64 can have up to three kernel stacks:
  58. * process stack
  59. * interrupt stack
  60. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  61. */
  62. static inline int valid_stack_ptr(struct thread_info *tinfo,
  63. void *p, unsigned int size, void *end)
  64. {
  65. void *t = tinfo;
  66. if (end) {
  67. if (p < end && p >= (end-THREAD_SIZE))
  68. return 1;
  69. else
  70. return 0;
  71. }
  72. return p > t && p < t + THREAD_SIZE - size;
  73. }
  74. unsigned long
  75. print_context_stack(struct thread_info *tinfo,
  76. unsigned long *stack, unsigned long bp,
  77. const struct stacktrace_ops *ops, void *data,
  78. unsigned long *end, int *graph)
  79. {
  80. struct stack_frame *frame = (struct stack_frame *)bp;
  81. while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
  82. unsigned long addr;
  83. addr = *stack;
  84. if (__kernel_text_address(addr)) {
  85. if ((unsigned long) stack == bp + sizeof(long)) {
  86. ops->address(data, addr, 1);
  87. frame = frame->next_frame;
  88. bp = (unsigned long) frame;
  89. } else {
  90. ops->address(data, addr, 0);
  91. }
  92. print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
  93. }
  94. stack++;
  95. }
  96. return bp;
  97. }
  98. static void
  99. print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
  100. {
  101. printk(data);
  102. print_symbol(msg, symbol);
  103. printk("\n");
  104. }
  105. static void print_trace_warning(void *data, char *msg)
  106. {
  107. printk("%s%s\n", (char *)data, msg);
  108. }
  109. static int print_trace_stack(void *data, char *name)
  110. {
  111. printk("%s <%s> ", (char *)data, name);
  112. return 0;
  113. }
  114. /*
  115. * Print one address/symbol entries per line.
  116. */
  117. static void print_trace_address(void *data, unsigned long addr, int reliable)
  118. {
  119. touch_nmi_watchdog();
  120. printk(data);
  121. printk_address(addr, reliable);
  122. }
  123. static const struct stacktrace_ops print_trace_ops = {
  124. .warning = print_trace_warning,
  125. .warning_symbol = print_trace_warning_symbol,
  126. .stack = print_trace_stack,
  127. .address = print_trace_address,
  128. };
  129. void
  130. show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  131. unsigned long *stack, unsigned long bp, char *log_lvl)
  132. {
  133. printk("%sCall Trace:\n", log_lvl);
  134. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  135. }
  136. void show_trace(struct task_struct *task, struct pt_regs *regs,
  137. unsigned long *stack, unsigned long bp)
  138. {
  139. show_trace_log_lvl(task, regs, stack, bp, "");
  140. }
  141. void show_stack(struct task_struct *task, unsigned long *sp)
  142. {
  143. show_stack_log_lvl(task, NULL, sp, 0, "");
  144. }
  145. /*
  146. * The architecture-independent dump_stack generator
  147. */
  148. void dump_stack(void)
  149. {
  150. unsigned long bp = 0;
  151. unsigned long stack;
  152. #ifdef CONFIG_FRAME_POINTER
  153. if (!bp)
  154. get_bp(bp);
  155. #endif
  156. printk("Pid: %d, comm: %.20s %s %s %.*s\n",
  157. current->pid, current->comm, print_tainted(),
  158. init_utsname()->release,
  159. (int)strcspn(init_utsname()->version, " "),
  160. init_utsname()->version);
  161. show_trace(NULL, NULL, &stack, bp);
  162. }
  163. EXPORT_SYMBOL(dump_stack);
  164. static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
  165. static int die_owner = -1;
  166. static unsigned int die_nest_count;
  167. unsigned __kprobes long oops_begin(void)
  168. {
  169. int cpu;
  170. unsigned long flags;
  171. /* notify the hw-branch tracer so it may disable tracing and
  172. add the last trace to the trace buffer -
  173. the earlier this happens, the more useful the trace. */
  174. trace_hw_branch_oops();
  175. oops_enter();
  176. /* racy, but better than risking deadlock. */
  177. raw_local_irq_save(flags);
  178. cpu = smp_processor_id();
  179. if (!__raw_spin_trylock(&die_lock)) {
  180. if (cpu == die_owner)
  181. /* nested oops. should stop eventually */;
  182. else
  183. __raw_spin_lock(&die_lock);
  184. }
  185. die_nest_count++;
  186. die_owner = cpu;
  187. console_verbose();
  188. bust_spinlocks(1);
  189. return flags;
  190. }
  191. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  192. {
  193. if (regs && kexec_should_crash(current))
  194. crash_kexec(regs);
  195. bust_spinlocks(0);
  196. die_owner = -1;
  197. add_taint(TAINT_DIE);
  198. die_nest_count--;
  199. if (!die_nest_count)
  200. /* Nest count reaches zero, release the lock. */
  201. __raw_spin_unlock(&die_lock);
  202. raw_local_irq_restore(flags);
  203. oops_exit();
  204. if (!signr)
  205. return;
  206. if (in_interrupt())
  207. panic("Fatal exception in interrupt");
  208. if (panic_on_oops)
  209. panic("Fatal exception");
  210. do_exit(signr);
  211. }
  212. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  213. {
  214. #ifdef CONFIG_X86_32
  215. unsigned short ss;
  216. unsigned long sp;
  217. #endif
  218. printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
  219. #ifdef CONFIG_PREEMPT
  220. printk("PREEMPT ");
  221. #endif
  222. #ifdef CONFIG_SMP
  223. printk("SMP ");
  224. #endif
  225. #ifdef CONFIG_DEBUG_PAGEALLOC
  226. printk("DEBUG_PAGEALLOC");
  227. #endif
  228. printk("\n");
  229. sysfs_printk_last_file();
  230. if (notify_die(DIE_OOPS, str, regs, err,
  231. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  232. return 1;
  233. show_registers(regs);
  234. #ifdef CONFIG_X86_32
  235. sp = (unsigned long) (&regs->sp);
  236. savesegment(ss, ss);
  237. if (user_mode(regs)) {
  238. sp = regs->sp;
  239. ss = regs->ss & 0xffff;
  240. }
  241. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  242. print_symbol("%s", regs->ip);
  243. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  244. #else
  245. /* Executive summary in case the oops scrolled away */
  246. printk(KERN_ALERT "RIP ");
  247. printk_address(regs->ip, 1);
  248. printk(" RSP <%016lx>\n", regs->sp);
  249. #endif
  250. return 0;
  251. }
  252. /*
  253. * This is gone through when something in the kernel has done something bad
  254. * and is about to be terminated:
  255. */
  256. void die(const char *str, struct pt_regs *regs, long err)
  257. {
  258. unsigned long flags = oops_begin();
  259. int sig = SIGSEGV;
  260. if (!user_mode_vm(regs))
  261. report_bug(regs->ip, regs);
  262. if (__die(str, regs, err))
  263. sig = 0;
  264. oops_end(flags, regs, sig);
  265. }
  266. void notrace __kprobes
  267. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  268. {
  269. unsigned long flags;
  270. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  271. return;
  272. /*
  273. * We are in trouble anyway, lets at least try
  274. * to get a message out.
  275. */
  276. flags = oops_begin();
  277. printk(KERN_EMERG "%s", str);
  278. printk(" on CPU%d, ip %08lx, registers:\n",
  279. smp_processor_id(), regs->ip);
  280. show_registers(regs);
  281. oops_end(flags, regs, 0);
  282. if (do_panic || panic_on_oops)
  283. panic("Non maskable interrupt");
  284. nmi_exit();
  285. local_irq_enable();
  286. do_exit(SIGBUS);
  287. }
  288. static int __init oops_setup(char *s)
  289. {
  290. if (!s)
  291. return -EINVAL;
  292. if (!strcmp(s, "panic"))
  293. panic_on_oops = 1;
  294. return 0;
  295. }
  296. early_param("oops", oops_setup);
  297. static int __init kstack_setup(char *s)
  298. {
  299. if (!s)
  300. return -EINVAL;
  301. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  302. return 0;
  303. }
  304. early_param("kstack", kstack_setup);
  305. static int __init code_bytes_setup(char *s)
  306. {
  307. code_bytes = simple_strtoul(s, NULL, 0);
  308. if (code_bytes > 8192)
  309. code_bytes = 8192;
  310. return 1;
  311. }
  312. __setup("code_bytes=", code_bytes_setup);