dumpstack_32.c 9.7 KB

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