dumpstack_32.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 <asm/stacktrace.h>
  17. int panic_on_unrecovered_nmi;
  18. int kstack_depth_to_print = 24;
  19. static unsigned int code_bytes = 64;
  20. static int die_counter;
  21. void printk_address(unsigned long address, int reliable)
  22. {
  23. printk(" [<%p>] %s%pS\n", (void *) address,
  24. reliable ? "" : "? ", (void *) address);
  25. }
  26. static inline int valid_stack_ptr(struct thread_info *tinfo,
  27. void *p, unsigned int size)
  28. {
  29. void *t = tinfo;
  30. return p > t && p <= t + THREAD_SIZE - size;
  31. }
  32. /* The form of the top of the frame on the stack */
  33. struct stack_frame {
  34. struct stack_frame *next_frame;
  35. unsigned long return_address;
  36. };
  37. static inline unsigned long
  38. print_context_stack(struct thread_info *tinfo,
  39. unsigned long *stack, unsigned long bp,
  40. const struct stacktrace_ops *ops, void *data)
  41. {
  42. struct stack_frame *frame = (struct stack_frame *)bp;
  43. while (valid_stack_ptr(tinfo, stack, sizeof(*stack))) {
  44. unsigned long addr;
  45. addr = *stack;
  46. if (__kernel_text_address(addr)) {
  47. if ((unsigned long) stack == bp + 4) {
  48. ops->address(data, addr, 1);
  49. frame = frame->next_frame;
  50. bp = (unsigned long) frame;
  51. } else {
  52. ops->address(data, addr, bp == 0);
  53. }
  54. }
  55. stack++;
  56. }
  57. return bp;
  58. }
  59. void dump_trace(struct task_struct *task, struct pt_regs *regs,
  60. unsigned long *stack, unsigned long bp,
  61. const struct stacktrace_ops *ops, void *data)
  62. {
  63. if (!task)
  64. task = current;
  65. if (!stack) {
  66. unsigned long dummy;
  67. stack = &dummy;
  68. if (task != current)
  69. stack = (unsigned long *)task->thread.sp;
  70. }
  71. #ifdef CONFIG_FRAME_POINTER
  72. if (!bp) {
  73. if (task == current) {
  74. /* Grab bp right from our regs */
  75. asm("movl %%ebp, %0" : "=r" (bp) :);
  76. } else {
  77. /* bp is the last reg pushed by switch_to */
  78. bp = *(unsigned long *) task->thread.sp;
  79. }
  80. }
  81. #endif
  82. for (;;) {
  83. struct thread_info *context;
  84. context = (struct thread_info *)
  85. ((unsigned long)stack & (~(THREAD_SIZE - 1)));
  86. bp = print_context_stack(context, stack, bp, ops, data);
  87. /*
  88. * Should be after the line below, but somewhere
  89. * in early boot context comes out corrupted and we
  90. * can't reference it:
  91. */
  92. if (ops->stack(data, "IRQ") < 0)
  93. break;
  94. stack = (unsigned long *)context->previous_esp;
  95. if (!stack)
  96. break;
  97. touch_nmi_watchdog();
  98. }
  99. }
  100. EXPORT_SYMBOL(dump_trace);
  101. static void
  102. print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
  103. {
  104. printk(data);
  105. print_symbol(msg, symbol);
  106. printk("\n");
  107. }
  108. static void print_trace_warning(void *data, char *msg)
  109. {
  110. printk("%s%s\n", (char *)data, msg);
  111. }
  112. static int print_trace_stack(void *data, char *name)
  113. {
  114. return 0;
  115. }
  116. /*
  117. * Print one address/symbol entries per line.
  118. */
  119. static void print_trace_address(void *data, unsigned long addr, int reliable)
  120. {
  121. printk("%s [<%08lx>] ", (char *)data, addr);
  122. if (!reliable)
  123. printk("? ");
  124. print_symbol("%s\n", addr);
  125. touch_nmi_watchdog();
  126. }
  127. static const struct stacktrace_ops print_trace_ops = {
  128. .warning = print_trace_warning,
  129. .warning_symbol = print_trace_warning_symbol,
  130. .stack = print_trace_stack,
  131. .address = print_trace_address,
  132. };
  133. static void
  134. show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  135. unsigned long *stack, unsigned long bp, char *log_lvl)
  136. {
  137. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  138. printk("%s =======================\n", log_lvl);
  139. }
  140. void show_trace(struct task_struct *task, struct pt_regs *regs,
  141. unsigned long *stack, unsigned long bp)
  142. {
  143. show_trace_log_lvl(task, regs, stack, bp, "");
  144. }
  145. static void
  146. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  147. unsigned long *sp, unsigned long bp, char *log_lvl)
  148. {
  149. unsigned long *stack;
  150. int i;
  151. if (sp == NULL) {
  152. if (task)
  153. sp = (unsigned long *)task->thread.sp;
  154. else
  155. sp = (unsigned long *)&sp;
  156. }
  157. stack = sp;
  158. for (i = 0; i < kstack_depth_to_print; i++) {
  159. if (kstack_end(stack))
  160. break;
  161. if (i && ((i % 8) == 0))
  162. printk("\n%s ", log_lvl);
  163. printk("%08lx ", *stack++);
  164. }
  165. printk("\n%sCall Trace:\n", log_lvl);
  166. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  167. }
  168. void show_stack(struct task_struct *task, unsigned long *sp)
  169. {
  170. printk(" ");
  171. show_stack_log_lvl(task, NULL, sp, 0, "");
  172. }
  173. /*
  174. * The architecture-independent dump_stack generator
  175. */
  176. void dump_stack(void)
  177. {
  178. unsigned long bp = 0;
  179. unsigned long stack;
  180. #ifdef CONFIG_FRAME_POINTER
  181. if (!bp)
  182. asm("movl %%ebp, %0" : "=r" (bp):);
  183. #endif
  184. printk("Pid: %d, comm: %.20s %s %s %.*s\n",
  185. current->pid, current->comm, print_tainted(),
  186. init_utsname()->release,
  187. (int)strcspn(init_utsname()->version, " "),
  188. init_utsname()->version);
  189. show_trace(current, NULL, &stack, bp);
  190. }
  191. EXPORT_SYMBOL(dump_stack);
  192. void show_registers(struct pt_regs *regs)
  193. {
  194. int i;
  195. print_modules();
  196. __show_regs(regs, 0);
  197. printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)",
  198. TASK_COMM_LEN, current->comm, task_pid_nr(current),
  199. current_thread_info(), current, task_thread_info(current));
  200. /*
  201. * When in-kernel, we also print out the stack and code at the
  202. * time of the fault..
  203. */
  204. if (!user_mode_vm(regs)) {
  205. unsigned int code_prologue = code_bytes * 43 / 64;
  206. unsigned int code_len = code_bytes;
  207. unsigned char c;
  208. u8 *ip;
  209. printk("\n" KERN_EMERG "Stack: ");
  210. show_stack_log_lvl(NULL, regs, &regs->sp, 0, KERN_EMERG);
  211. printk(KERN_EMERG "Code: ");
  212. ip = (u8 *)regs->ip - code_prologue;
  213. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  214. /* try starting at EIP */
  215. ip = (u8 *)regs->ip;
  216. code_len = code_len - code_prologue + 1;
  217. }
  218. for (i = 0; i < code_len; i++, ip++) {
  219. if (ip < (u8 *)PAGE_OFFSET ||
  220. probe_kernel_address(ip, c)) {
  221. printk(" Bad EIP value.");
  222. break;
  223. }
  224. if (ip == (u8 *)regs->ip)
  225. printk("<%02x> ", c);
  226. else
  227. printk("%02x ", c);
  228. }
  229. }
  230. printk("\n");
  231. }
  232. int is_valid_bugaddr(unsigned long ip)
  233. {
  234. unsigned short ud2;
  235. if (ip < PAGE_OFFSET)
  236. return 0;
  237. if (probe_kernel_address((unsigned short *)ip, ud2))
  238. return 0;
  239. return ud2 == 0x0b0f;
  240. }
  241. static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
  242. static int die_owner = -1;
  243. static unsigned int die_nest_count;
  244. unsigned __kprobes long oops_begin(void)
  245. {
  246. unsigned long flags;
  247. oops_enter();
  248. if (die_owner != raw_smp_processor_id()) {
  249. console_verbose();
  250. raw_local_irq_save(flags);
  251. __raw_spin_lock(&die_lock);
  252. die_owner = smp_processor_id();
  253. die_nest_count = 0;
  254. bust_spinlocks(1);
  255. } else {
  256. raw_local_irq_save(flags);
  257. }
  258. die_nest_count++;
  259. return flags;
  260. }
  261. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  262. {
  263. bust_spinlocks(0);
  264. die_owner = -1;
  265. add_taint(TAINT_DIE);
  266. __raw_spin_unlock(&die_lock);
  267. raw_local_irq_restore(flags);
  268. if (!regs)
  269. return;
  270. if (kexec_should_crash(current))
  271. crash_kexec(regs);
  272. if (in_interrupt())
  273. panic("Fatal exception in interrupt");
  274. if (panic_on_oops)
  275. panic("Fatal exception");
  276. oops_exit();
  277. do_exit(signr);
  278. }
  279. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  280. {
  281. unsigned short ss;
  282. unsigned long sp;
  283. printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
  284. #ifdef CONFIG_PREEMPT
  285. printk("PREEMPT ");
  286. #endif
  287. #ifdef CONFIG_SMP
  288. printk("SMP ");
  289. #endif
  290. #ifdef CONFIG_DEBUG_PAGEALLOC
  291. printk("DEBUG_PAGEALLOC");
  292. #endif
  293. printk("\n");
  294. if (notify_die(DIE_OOPS, str, regs, err,
  295. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  296. return 1;
  297. show_registers(regs);
  298. /* Executive summary in case the oops scrolled away */
  299. sp = (unsigned long) (&regs->sp);
  300. savesegment(ss, ss);
  301. if (user_mode(regs)) {
  302. sp = regs->sp;
  303. ss = regs->ss & 0xffff;
  304. }
  305. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  306. print_symbol("%s", regs->ip);
  307. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  308. return 0;
  309. }
  310. /*
  311. * This is gone through when something in the kernel has done something bad
  312. * and is about to be terminated:
  313. */
  314. void die(const char *str, struct pt_regs *regs, long err)
  315. {
  316. unsigned long flags = oops_begin();
  317. if (die_nest_count < 3) {
  318. report_bug(regs->ip, regs);
  319. if (__die(str, regs, err))
  320. regs = NULL;
  321. } else {
  322. printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
  323. }
  324. oops_end(flags, regs, SIGSEGV);
  325. }
  326. static DEFINE_SPINLOCK(nmi_print_lock);
  327. void notrace __kprobes
  328. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  329. {
  330. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  331. return;
  332. spin_lock(&nmi_print_lock);
  333. /*
  334. * We are in trouble anyway, lets at least try
  335. * to get a message out:
  336. */
  337. bust_spinlocks(1);
  338. printk(KERN_EMERG "%s", str);
  339. printk(" on CPU%d, ip %08lx, registers:\n",
  340. smp_processor_id(), regs->ip);
  341. show_registers(regs);
  342. if (do_panic)
  343. panic("Non maskable interrupt");
  344. console_silent();
  345. spin_unlock(&nmi_print_lock);
  346. bust_spinlocks(0);
  347. /*
  348. * If we are in kernel we are probably nested up pretty bad
  349. * and might aswell get out now while we still can:
  350. */
  351. if (!user_mode_vm(regs)) {
  352. current->thread.trap_no = 2;
  353. crash_kexec(regs);
  354. }
  355. do_exit(SIGSEGV);
  356. }
  357. static int __init kstack_setup(char *s)
  358. {
  359. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  360. return 1;
  361. }
  362. __setup("kstack=", kstack_setup);
  363. static int __init code_bytes_setup(char *s)
  364. {
  365. code_bytes = simple_strtoul(s, NULL, 0);
  366. if (code_bytes > 8192)
  367. code_bytes = 8192;
  368. return 1;
  369. }
  370. __setup("code_bytes=", code_bytes_setup);