dumpstack.c 6.9 KB

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