dumpstack_32.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_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. show_trace_log_lvl(current, regs, NULL, KERN_EMERG);
  116. pr_emerg("Code:");
  117. ip = (u8 *)regs->ip - code_prologue;
  118. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  119. /* try starting at IP */
  120. ip = (u8 *)regs->ip;
  121. code_len = code_len - code_prologue + 1;
  122. }
  123. for (i = 0; i < code_len; i++, ip++) {
  124. if (ip < (u8 *)PAGE_OFFSET ||
  125. probe_kernel_address(ip, c)) {
  126. pr_cont(" Bad EIP value.");
  127. break;
  128. }
  129. if (ip == (u8 *)regs->ip)
  130. pr_cont(" <%02x>", c);
  131. else
  132. pr_cont(" %02x", c);
  133. }
  134. }
  135. pr_cont("\n");
  136. }
  137. int is_valid_bugaddr(unsigned long ip)
  138. {
  139. unsigned short ud2;
  140. if (ip < PAGE_OFFSET)
  141. return 0;
  142. if (probe_kernel_address((unsigned short *)ip, ud2))
  143. return 0;
  144. return ud2 == 0x0b0f;
  145. }