dumpstack_32.c 3.8 KB

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