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