dumpstack_32.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/export.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. void stack_type_str(enum stack_type type, const char **begin, const char **end)
  18. {
  19. switch (type) {
  20. case STACK_TYPE_IRQ:
  21. case STACK_TYPE_SOFTIRQ:
  22. *begin = "IRQ";
  23. *end = "EOI";
  24. break;
  25. default:
  26. *begin = NULL;
  27. *end = NULL;
  28. }
  29. }
  30. static bool in_hardirq_stack(unsigned long *stack, struct stack_info *info)
  31. {
  32. unsigned long *begin = (unsigned long *)this_cpu_read(hardirq_stack);
  33. unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
  34. /*
  35. * This is a software stack, so 'end' can be a valid stack pointer.
  36. * It just means the stack is empty.
  37. */
  38. if (stack < begin || stack > end)
  39. return false;
  40. info->type = STACK_TYPE_IRQ;
  41. info->begin = begin;
  42. info->end = end;
  43. /*
  44. * See irq_32.c -- the next stack pointer is stored at the beginning of
  45. * the stack.
  46. */
  47. info->next_sp = (unsigned long *)*begin;
  48. return true;
  49. }
  50. static bool in_softirq_stack(unsigned long *stack, struct stack_info *info)
  51. {
  52. unsigned long *begin = (unsigned long *)this_cpu_read(softirq_stack);
  53. unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
  54. /*
  55. * This is a software stack, so 'end' can be a valid stack pointer.
  56. * It just means the stack is empty.
  57. */
  58. if (stack < begin || stack > end)
  59. return false;
  60. info->type = STACK_TYPE_SOFTIRQ;
  61. info->begin = begin;
  62. info->end = end;
  63. /*
  64. * The next stack pointer is stored at the beginning of the stack.
  65. * See irq_32.c.
  66. */
  67. info->next_sp = (unsigned long *)*begin;
  68. return true;
  69. }
  70. int get_stack_info(unsigned long *stack, struct task_struct *task,
  71. struct stack_info *info, unsigned long *visit_mask)
  72. {
  73. if (!stack)
  74. goto unknown;
  75. task = task ? : current;
  76. if (in_task_stack(stack, task, info))
  77. goto recursion_check;
  78. if (task != current)
  79. goto unknown;
  80. if (in_hardirq_stack(stack, info))
  81. goto recursion_check;
  82. if (in_softirq_stack(stack, info))
  83. goto recursion_check;
  84. goto unknown;
  85. recursion_check:
  86. /*
  87. * Make sure we don't iterate through any given stack more than once.
  88. * If it comes up a second time then there's something wrong going on:
  89. * just break out and report an unknown stack type.
  90. */
  91. if (visit_mask) {
  92. if (*visit_mask & (1UL << info->type))
  93. goto unknown;
  94. *visit_mask |= 1UL << info->type;
  95. }
  96. return 0;
  97. unknown:
  98. info->type = STACK_TYPE_UNKNOWN;
  99. return -EINVAL;
  100. }
  101. void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  102. unsigned long *sp, char *log_lvl)
  103. {
  104. unsigned long *stack;
  105. int i;
  106. if (!try_get_task_stack(task))
  107. return;
  108. sp = sp ? : get_stack_pointer(task, regs);
  109. stack = sp;
  110. for (i = 0; i < kstack_depth_to_print; i++) {
  111. if (kstack_end(stack))
  112. break;
  113. if ((i % STACKSLOTS_PER_LINE) == 0) {
  114. if (i != 0)
  115. pr_cont("\n");
  116. printk("%s %08lx", log_lvl, *stack++);
  117. } else
  118. pr_cont(" %08lx", *stack++);
  119. touch_nmi_watchdog();
  120. }
  121. pr_cont("\n");
  122. show_trace_log_lvl(task, regs, sp, log_lvl);
  123. put_task_stack(task);
  124. }
  125. void show_regs(struct pt_regs *regs)
  126. {
  127. int i;
  128. show_regs_print_info(KERN_EMERG);
  129. __show_regs(regs, !user_mode(regs));
  130. /*
  131. * When in-kernel, we also print out the stack and code at the
  132. * time of the fault..
  133. */
  134. if (!user_mode(regs)) {
  135. unsigned int code_prologue = code_bytes * 43 / 64;
  136. unsigned int code_len = code_bytes;
  137. unsigned char c;
  138. u8 *ip;
  139. pr_emerg("Stack:\n");
  140. show_stack_log_lvl(current, regs, NULL, KERN_EMERG);
  141. pr_emerg("Code:");
  142. ip = (u8 *)regs->ip - code_prologue;
  143. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  144. /* try starting at IP */
  145. ip = (u8 *)regs->ip;
  146. code_len = code_len - code_prologue + 1;
  147. }
  148. for (i = 0; i < code_len; i++, ip++) {
  149. if (ip < (u8 *)PAGE_OFFSET ||
  150. probe_kernel_address(ip, c)) {
  151. pr_cont(" Bad EIP value.");
  152. break;
  153. }
  154. if (ip == (u8 *)regs->ip)
  155. pr_cont(" <%02x>", c);
  156. else
  157. pr_cont(" %02x", c);
  158. }
  159. }
  160. pr_cont("\n");
  161. }
  162. int is_valid_bugaddr(unsigned long ip)
  163. {
  164. unsigned short ud2;
  165. if (ip < PAGE_OFFSET)
  166. return 0;
  167. if (probe_kernel_address((unsigned short *)ip, ud2))
  168. return 0;
  169. return ud2 == 0x0b0f;
  170. }