dumpstack_32.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 dump_trace(struct task_struct *task, struct pt_regs *regs,
  102. unsigned long *stack, unsigned long bp,
  103. const struct stacktrace_ops *ops, void *data)
  104. {
  105. unsigned long visit_mask = 0;
  106. int graph = 0;
  107. task = task ? : current;
  108. stack = stack ? : get_stack_pointer(task, regs);
  109. bp = bp ? : (unsigned long)get_frame_pointer(task, regs);
  110. for (;;) {
  111. const char *begin_str, *end_str;
  112. struct stack_info info;
  113. if (get_stack_info(stack, task, &info, &visit_mask))
  114. break;
  115. stack_type_str(info.type, &begin_str, &end_str);
  116. if (begin_str && ops->stack(data, begin_str) < 0)
  117. break;
  118. bp = ops->walk_stack(task, stack, bp, ops, data, &info, &graph);
  119. if (end_str && ops->stack(data, end_str) < 0)
  120. break;
  121. stack = info.next_sp;
  122. touch_nmi_watchdog();
  123. }
  124. }
  125. EXPORT_SYMBOL(dump_trace);
  126. void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  127. unsigned long *sp, char *log_lvl)
  128. {
  129. unsigned long *stack;
  130. int i;
  131. if (!try_get_task_stack(task))
  132. return;
  133. sp = sp ? : get_stack_pointer(task, regs);
  134. stack = sp;
  135. for (i = 0; i < kstack_depth_to_print; i++) {
  136. if (kstack_end(stack))
  137. break;
  138. if ((i % STACKSLOTS_PER_LINE) == 0) {
  139. if (i != 0)
  140. pr_cont("\n");
  141. printk("%s %08lx", log_lvl, *stack++);
  142. } else
  143. pr_cont(" %08lx", *stack++);
  144. touch_nmi_watchdog();
  145. }
  146. pr_cont("\n");
  147. show_trace_log_lvl(task, regs, sp, log_lvl);
  148. put_task_stack(task);
  149. }
  150. void show_regs(struct pt_regs *regs)
  151. {
  152. int i;
  153. show_regs_print_info(KERN_EMERG);
  154. __show_regs(regs, !user_mode(regs));
  155. /*
  156. * When in-kernel, we also print out the stack and code at the
  157. * time of the fault..
  158. */
  159. if (!user_mode(regs)) {
  160. unsigned int code_prologue = code_bytes * 43 / 64;
  161. unsigned int code_len = code_bytes;
  162. unsigned char c;
  163. u8 *ip;
  164. pr_emerg("Stack:\n");
  165. show_stack_log_lvl(current, regs, NULL, KERN_EMERG);
  166. pr_emerg("Code:");
  167. ip = (u8 *)regs->ip - code_prologue;
  168. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  169. /* try starting at IP */
  170. ip = (u8 *)regs->ip;
  171. code_len = code_len - code_prologue + 1;
  172. }
  173. for (i = 0; i < code_len; i++, ip++) {
  174. if (ip < (u8 *)PAGE_OFFSET ||
  175. probe_kernel_address(ip, c)) {
  176. pr_cont(" Bad EIP value.");
  177. break;
  178. }
  179. if (ip == (u8 *)regs->ip)
  180. pr_cont(" <%02x>", c);
  181. else
  182. pr_cont(" %02x", c);
  183. }
  184. }
  185. pr_cont("\n");
  186. }
  187. int is_valid_bugaddr(unsigned long ip)
  188. {
  189. unsigned short ud2;
  190. if (ip < PAGE_OFFSET)
  191. return 0;
  192. if (probe_kernel_address((unsigned short *)ip, ud2))
  193. return 0;
  194. return ud2 == 0x0b0f;
  195. }