dumpstack.c 7.9 KB

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