dumpstack_32.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. int cpu;
  252. unsigned long flags;
  253. oops_enter();
  254. /* racy, but better than risking deadlock. */
  255. raw_local_irq_save(flags);
  256. cpu = smp_processor_id();
  257. if (!__raw_spin_trylock(&die_lock)) {
  258. if (cpu == die_owner)
  259. /* nested oops. should stop eventually */;
  260. else
  261. __raw_spin_lock(&die_lock);
  262. }
  263. die_nest_count++;
  264. die_owner = cpu;
  265. console_verbose();
  266. bust_spinlocks(1);
  267. return flags;
  268. }
  269. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  270. {
  271. if (regs && kexec_should_crash(current))
  272. crash_kexec(regs);
  273. bust_spinlocks(0);
  274. die_owner = -1;
  275. add_taint(TAINT_DIE);
  276. die_nest_count--;
  277. if (!die_nest_count)
  278. /* Nest count reaches zero, release the lock. */
  279. __raw_spin_unlock(&die_lock);
  280. raw_local_irq_restore(flags);
  281. oops_exit();
  282. if (!signr)
  283. return;
  284. if (in_interrupt())
  285. panic("Fatal exception in interrupt");
  286. if (panic_on_oops)
  287. panic("Fatal exception");
  288. do_exit(signr);
  289. }
  290. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  291. {
  292. unsigned short ss;
  293. unsigned long sp;
  294. printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
  295. #ifdef CONFIG_PREEMPT
  296. printk("PREEMPT ");
  297. #endif
  298. #ifdef CONFIG_SMP
  299. printk("SMP ");
  300. #endif
  301. #ifdef CONFIG_DEBUG_PAGEALLOC
  302. printk("DEBUG_PAGEALLOC");
  303. #endif
  304. printk("\n");
  305. sysfs_printk_last_file();
  306. if (notify_die(DIE_OOPS, str, regs, err,
  307. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  308. return 1;
  309. show_registers(regs);
  310. /* Executive summary in case the oops scrolled away */
  311. sp = (unsigned long) (&regs->sp);
  312. savesegment(ss, ss);
  313. if (user_mode(regs)) {
  314. sp = regs->sp;
  315. ss = regs->ss & 0xffff;
  316. }
  317. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  318. print_symbol("%s", regs->ip);
  319. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  320. return 0;
  321. }
  322. /*
  323. * This is gone through when something in the kernel has done something bad
  324. * and is about to be terminated:
  325. */
  326. void die(const char *str, struct pt_regs *regs, long err)
  327. {
  328. unsigned long flags = oops_begin();
  329. int sig = SIGSEGV;
  330. if (!user_mode_vm(regs))
  331. report_bug(regs->ip, regs);
  332. if (__die(str, regs, err))
  333. sig = 0;
  334. oops_end(flags, regs, sig);
  335. }
  336. void notrace __kprobes
  337. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  338. {
  339. unsigned long flags;
  340. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  341. return;
  342. /*
  343. * We are in trouble anyway, lets at least try
  344. * to get a message out.
  345. */
  346. flags = oops_begin();
  347. printk(KERN_EMERG "%s", str);
  348. printk(" on CPU%d, ip %08lx, registers:\n",
  349. smp_processor_id(), regs->ip);
  350. show_registers(regs);
  351. oops_end(flags, regs, 0);
  352. if (do_panic || panic_on_oops)
  353. panic("Non maskable interrupt");
  354. nmi_exit();
  355. local_irq_enable();
  356. do_exit(SIGBUS);
  357. }
  358. static int __init oops_setup(char *s)
  359. {
  360. if (!s)
  361. return -EINVAL;
  362. if (!strcmp(s, "panic"))
  363. panic_on_oops = 1;
  364. return 0;
  365. }
  366. early_param("oops", oops_setup);
  367. static int __init kstack_setup(char *s)
  368. {
  369. if (!s)
  370. return -EINVAL;
  371. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  372. return 0;
  373. }
  374. early_param("kstack", kstack_setup);
  375. static int __init code_bytes_setup(char *s)
  376. {
  377. code_bytes = simple_strtoul(s, NULL, 0);
  378. if (code_bytes > 8192)
  379. code_bytes = 8192;
  380. return 1;
  381. }
  382. __setup("code_bytes=", code_bytes_setup);