dumpstack_32.c 9.1 KB

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