dumpstack_32.c 9.4 KB

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