dumpstack_32.c 9.6 KB

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