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. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  139. printk("%s =======================\n", 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. }
  166. printk("\n%sCall Trace:\n", log_lvl);
  167. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  168. }
  169. void show_stack(struct task_struct *task, unsigned long *sp)
  170. {
  171. printk(" ");
  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)",
  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("\n" KERN_EMERG "Stack: ");
  211. show_stack_log_lvl(NULL, regs, &regs->sp, 0, KERN_EMERG);
  212. printk(KERN_EMERG "Code: ");
  213. ip = (u8 *)regs->ip - code_prologue;
  214. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  215. /* try starting at EIP */
  216. ip = (u8 *)regs->ip;
  217. code_len = code_len - code_prologue + 1;
  218. }
  219. for (i = 0; i < code_len; i++, ip++) {
  220. if (ip < (u8 *)PAGE_OFFSET ||
  221. probe_kernel_address(ip, c)) {
  222. printk(" Bad EIP value.");
  223. break;
  224. }
  225. if (ip == (u8 *)regs->ip)
  226. printk("<%02x> ", c);
  227. else
  228. printk("%02x ", c);
  229. }
  230. }
  231. printk("\n");
  232. }
  233. int is_valid_bugaddr(unsigned long ip)
  234. {
  235. unsigned short ud2;
  236. if (ip < PAGE_OFFSET)
  237. return 0;
  238. if (probe_kernel_address((unsigned short *)ip, ud2))
  239. return 0;
  240. return ud2 == 0x0b0f;
  241. }
  242. static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
  243. static int die_owner = -1;
  244. static unsigned int die_nest_count;
  245. unsigned __kprobes long oops_begin(void)
  246. {
  247. unsigned long flags;
  248. oops_enter();
  249. if (die_owner != raw_smp_processor_id()) {
  250. console_verbose();
  251. raw_local_irq_save(flags);
  252. __raw_spin_lock(&die_lock);
  253. die_owner = smp_processor_id();
  254. die_nest_count = 0;
  255. bust_spinlocks(1);
  256. } else {
  257. raw_local_irq_save(flags);
  258. }
  259. die_nest_count++;
  260. return flags;
  261. }
  262. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  263. {
  264. bust_spinlocks(0);
  265. die_owner = -1;
  266. add_taint(TAINT_DIE);
  267. __raw_spin_unlock(&die_lock);
  268. raw_local_irq_restore(flags);
  269. if (!regs)
  270. return;
  271. if (kexec_should_crash(current))
  272. crash_kexec(regs);
  273. if (in_interrupt())
  274. panic("Fatal exception in interrupt");
  275. if (panic_on_oops)
  276. panic("Fatal exception");
  277. oops_exit();
  278. do_exit(signr);
  279. }
  280. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  281. {
  282. unsigned short ss;
  283. unsigned long sp;
  284. printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
  285. #ifdef CONFIG_PREEMPT
  286. printk("PREEMPT ");
  287. #endif
  288. #ifdef CONFIG_SMP
  289. printk("SMP ");
  290. #endif
  291. #ifdef CONFIG_DEBUG_PAGEALLOC
  292. printk("DEBUG_PAGEALLOC");
  293. #endif
  294. printk("\n");
  295. if (notify_die(DIE_OOPS, str, regs, err,
  296. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  297. return 1;
  298. show_registers(regs);
  299. /* Executive summary in case the oops scrolled away */
  300. sp = (unsigned long) (&regs->sp);
  301. savesegment(ss, ss);
  302. if (user_mode(regs)) {
  303. sp = regs->sp;
  304. ss = regs->ss & 0xffff;
  305. }
  306. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  307. print_symbol("%s", regs->ip);
  308. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  309. return 0;
  310. }
  311. /*
  312. * This is gone through when something in the kernel has done something bad
  313. * and is about to be terminated:
  314. */
  315. void die(const char *str, struct pt_regs *regs, long err)
  316. {
  317. unsigned long flags = oops_begin();
  318. if (die_nest_count < 3) {
  319. report_bug(regs->ip, regs);
  320. if (__die(str, regs, err))
  321. regs = NULL;
  322. } else {
  323. printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
  324. }
  325. oops_end(flags, regs, SIGSEGV);
  326. }
  327. static DEFINE_SPINLOCK(nmi_print_lock);
  328. void notrace __kprobes
  329. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  330. {
  331. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  332. return;
  333. spin_lock(&nmi_print_lock);
  334. /*
  335. * We are in trouble anyway, lets at least try
  336. * to get a message out:
  337. */
  338. bust_spinlocks(1);
  339. printk(KERN_EMERG "%s", str);
  340. printk(" on CPU%d, ip %08lx, registers:\n",
  341. smp_processor_id(), regs->ip);
  342. show_registers(regs);
  343. if (do_panic)
  344. panic("Non maskable interrupt");
  345. console_silent();
  346. spin_unlock(&nmi_print_lock);
  347. bust_spinlocks(0);
  348. /*
  349. * If we are in kernel we are probably nested up pretty bad
  350. * and might aswell get out now while we still can:
  351. */
  352. if (!user_mode_vm(regs)) {
  353. current->thread.trap_no = 2;
  354. crash_kexec(regs);
  355. }
  356. do_exit(SIGSEGV);
  357. }
  358. static int __init kstack_setup(char *s)
  359. {
  360. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  361. return 1;
  362. }
  363. __setup("kstack=", kstack_setup);
  364. static int __init code_bytes_setup(char *s)
  365. {
  366. code_bytes = simple_strtoul(s, NULL, 0);
  367. if (code_bytes > 8192)
  368. code_bytes = 8192;
  369. return 1;
  370. }
  371. __setup("code_bytes=", code_bytes_setup);