dumpstack.c 7.8 KB

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