dumpstack.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <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. oops_enter();
  170. /* racy, but better than risking deadlock. */
  171. raw_local_irq_save(flags);
  172. cpu = smp_processor_id();
  173. if (!__raw_spin_trylock(&die_lock)) {
  174. if (cpu == die_owner)
  175. /* nested oops. should stop eventually */;
  176. else
  177. __raw_spin_lock(&die_lock);
  178. }
  179. die_nest_count++;
  180. die_owner = cpu;
  181. console_verbose();
  182. bust_spinlocks(1);
  183. return flags;
  184. }
  185. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  186. {
  187. if (regs && kexec_should_crash(current))
  188. crash_kexec(regs);
  189. bust_spinlocks(0);
  190. die_owner = -1;
  191. add_taint(TAINT_DIE);
  192. die_nest_count--;
  193. if (!die_nest_count)
  194. /* Nest count reaches zero, release the lock. */
  195. __raw_spin_unlock(&die_lock);
  196. raw_local_irq_restore(flags);
  197. oops_exit();
  198. if (!signr)
  199. return;
  200. if (in_interrupt())
  201. panic("Fatal exception in interrupt");
  202. if (panic_on_oops)
  203. panic("Fatal exception");
  204. do_exit(signr);
  205. }
  206. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  207. {
  208. #ifdef CONFIG_X86_32
  209. unsigned short ss;
  210. unsigned long sp;
  211. #endif
  212. printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
  213. #ifdef CONFIG_PREEMPT
  214. printk("PREEMPT ");
  215. #endif
  216. #ifdef CONFIG_SMP
  217. printk("SMP ");
  218. #endif
  219. #ifdef CONFIG_DEBUG_PAGEALLOC
  220. printk("DEBUG_PAGEALLOC");
  221. #endif
  222. printk("\n");
  223. sysfs_printk_last_file();
  224. if (notify_die(DIE_OOPS, str, regs, err,
  225. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  226. return 1;
  227. show_registers(regs);
  228. #ifdef CONFIG_X86_32
  229. sp = (unsigned long) (&regs->sp);
  230. savesegment(ss, ss);
  231. if (user_mode(regs)) {
  232. sp = regs->sp;
  233. ss = regs->ss & 0xffff;
  234. }
  235. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  236. print_symbol("%s", regs->ip);
  237. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  238. #else
  239. /* Executive summary in case the oops scrolled away */
  240. printk(KERN_ALERT "RIP ");
  241. printk_address(regs->ip, 1);
  242. printk(" RSP <%016lx>\n", regs->sp);
  243. #endif
  244. return 0;
  245. }
  246. /*
  247. * This is gone through when something in the kernel has done something bad
  248. * and is about to be terminated:
  249. */
  250. void die(const char *str, struct pt_regs *regs, long err)
  251. {
  252. unsigned long flags = oops_begin();
  253. int sig = SIGSEGV;
  254. if (!user_mode_vm(regs))
  255. report_bug(regs->ip, regs);
  256. if (__die(str, regs, err))
  257. sig = 0;
  258. oops_end(flags, regs, sig);
  259. }
  260. void notrace __kprobes
  261. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  262. {
  263. unsigned long flags;
  264. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  265. return;
  266. /*
  267. * We are in trouble anyway, lets at least try
  268. * to get a message out.
  269. */
  270. flags = oops_begin();
  271. printk(KERN_EMERG "%s", str);
  272. printk(" on CPU%d, ip %08lx, registers:\n",
  273. smp_processor_id(), regs->ip);
  274. show_registers(regs);
  275. oops_end(flags, regs, 0);
  276. if (do_panic || panic_on_oops)
  277. panic("Non maskable interrupt");
  278. nmi_exit();
  279. local_irq_enable();
  280. do_exit(SIGBUS);
  281. }
  282. static int __init oops_setup(char *s)
  283. {
  284. if (!s)
  285. return -EINVAL;
  286. if (!strcmp(s, "panic"))
  287. panic_on_oops = 1;
  288. return 0;
  289. }
  290. early_param("oops", oops_setup);
  291. static int __init kstack_setup(char *s)
  292. {
  293. if (!s)
  294. return -EINVAL;
  295. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  296. return 0;
  297. }
  298. early_param("kstack", kstack_setup);
  299. static int __init code_bytes_setup(char *s)
  300. {
  301. code_bytes = simple_strtoul(s, NULL, 0);
  302. if (code_bytes > 8192)
  303. code_bytes = 8192;
  304. return 1;
  305. }
  306. __setup("code_bytes=", code_bytes_setup);