dumpstack_32.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/hardirq.h>
  9. #include <linux/kdebug.h>
  10. #include <linux/module.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/kexec.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/bug.h>
  15. #include <linux/nmi.h>
  16. #include <asm/stacktrace.h>
  17. static void *is_irq_stack(void *p, void *irq)
  18. {
  19. if (p < irq || p >= (irq + THREAD_SIZE))
  20. return NULL;
  21. return irq + THREAD_SIZE;
  22. }
  23. static void *is_hardirq_stack(unsigned long *stack, int cpu)
  24. {
  25. void *irq = per_cpu(hardirq_stack, cpu);
  26. return is_irq_stack(stack, irq);
  27. }
  28. static void *is_softirq_stack(unsigned long *stack, int cpu)
  29. {
  30. void *irq = per_cpu(softirq_stack, cpu);
  31. return is_irq_stack(stack, irq);
  32. }
  33. void dump_trace(struct task_struct *task, struct pt_regs *regs,
  34. unsigned long *stack, unsigned long bp,
  35. const struct stacktrace_ops *ops, void *data)
  36. {
  37. const unsigned cpu = get_cpu();
  38. int graph = 0;
  39. u32 *prev_esp;
  40. if (!task)
  41. task = current;
  42. if (!stack) {
  43. unsigned long dummy;
  44. stack = &dummy;
  45. if (task != current)
  46. stack = (unsigned long *)task->thread.sp;
  47. }
  48. if (!bp)
  49. bp = stack_frame(task, regs);
  50. for (;;) {
  51. struct thread_info *context;
  52. void *end_stack;
  53. end_stack = is_hardirq_stack(stack, cpu);
  54. if (!end_stack)
  55. end_stack = is_softirq_stack(stack, cpu);
  56. context = task_thread_info(task);
  57. bp = ops->walk_stack(context, stack, bp, ops, data,
  58. end_stack, &graph);
  59. /* Stop if not on irq stack */
  60. if (!end_stack)
  61. break;
  62. /* The previous esp is saved on the bottom of the stack */
  63. prev_esp = (u32 *)(end_stack - THREAD_SIZE);
  64. stack = (unsigned long *)*prev_esp;
  65. if (!stack)
  66. break;
  67. if (ops->stack(data, "IRQ") < 0)
  68. break;
  69. touch_nmi_watchdog();
  70. }
  71. put_cpu();
  72. }
  73. EXPORT_SYMBOL(dump_trace);
  74. void
  75. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  76. unsigned long *sp, unsigned long bp, char *log_lvl)
  77. {
  78. unsigned long *stack;
  79. int i;
  80. if (sp == NULL) {
  81. if (task)
  82. sp = (unsigned long *)task->thread.sp;
  83. else
  84. sp = (unsigned long *)&sp;
  85. }
  86. stack = sp;
  87. for (i = 0; i < kstack_depth_to_print; i++) {
  88. if (kstack_end(stack))
  89. break;
  90. if ((i % STACKSLOTS_PER_LINE) == 0) {
  91. if (i != 0)
  92. pr_cont("\n");
  93. printk("%s %08lx", log_lvl, *stack++);
  94. } else
  95. pr_cont(" %08lx", *stack++);
  96. touch_nmi_watchdog();
  97. }
  98. pr_cont("\n");
  99. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  100. }
  101. void show_regs(struct pt_regs *regs)
  102. {
  103. int i;
  104. show_regs_print_info(KERN_EMERG);
  105. __show_regs(regs, !user_mode(regs));
  106. /*
  107. * When in-kernel, we also print out the stack and code at the
  108. * time of the fault..
  109. */
  110. if (!user_mode(regs)) {
  111. unsigned int code_prologue = code_bytes * 43 / 64;
  112. unsigned int code_len = code_bytes;
  113. unsigned char c;
  114. u8 *ip;
  115. pr_emerg("Stack:\n");
  116. show_stack_log_lvl(NULL, regs, &regs->sp, 0, KERN_EMERG);
  117. pr_emerg("Code:");
  118. ip = (u8 *)regs->ip - code_prologue;
  119. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  120. /* try starting at IP */
  121. ip = (u8 *)regs->ip;
  122. code_len = code_len - code_prologue + 1;
  123. }
  124. for (i = 0; i < code_len; i++, ip++) {
  125. if (ip < (u8 *)PAGE_OFFSET ||
  126. probe_kernel_address(ip, c)) {
  127. pr_cont(" Bad EIP value.");
  128. break;
  129. }
  130. if (ip == (u8 *)regs->ip)
  131. pr_cont(" <%02x>", c);
  132. else
  133. pr_cont(" %02x", c);
  134. }
  135. }
  136. pr_cont("\n");
  137. }
  138. int is_valid_bugaddr(unsigned long ip)
  139. {
  140. unsigned short ud2;
  141. if (ip < PAGE_OFFSET)
  142. return 0;
  143. if (probe_kernel_address((unsigned short *)ip, ud2))
  144. return 0;
  145. return ud2 == 0x0b0f;
  146. }