backtrace.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @file backtrace.c
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon
  8. * @author David Smith
  9. */
  10. #include <linux/oprofile.h>
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <asm/ptrace.h>
  14. #include <asm/uaccess.h>
  15. struct frame_head {
  16. struct frame_head * ebp;
  17. unsigned long ret;
  18. } __attribute__((packed));
  19. static struct frame_head *
  20. dump_kernel_backtrace(struct frame_head * head)
  21. {
  22. oprofile_add_trace(head->ret);
  23. /* frame pointers should strictly progress back up the stack
  24. * (towards higher addresses) */
  25. if (head >= head->ebp)
  26. return NULL;
  27. return head->ebp;
  28. }
  29. static struct frame_head *
  30. dump_user_backtrace(struct frame_head * head)
  31. {
  32. struct frame_head bufhead[2];
  33. /* Also check accessibility of one struct frame_head beyond */
  34. if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
  35. return NULL;
  36. if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
  37. return NULL;
  38. oprofile_add_trace(bufhead[0].ret);
  39. /* frame pointers should strictly progress back up the stack
  40. * (towards higher addresses) */
  41. if (head >= bufhead[0].ebp)
  42. return NULL;
  43. return bufhead[0].ebp;
  44. }
  45. /*
  46. * | | /\ Higher addresses
  47. * | |
  48. * --------------- stack base (address of current_thread_info)
  49. * | thread info |
  50. * . .
  51. * | stack |
  52. * --------------- saved regs->ebp value if valid (frame_head address)
  53. * . .
  54. * --------------- saved regs->rsp value if x86_64
  55. * | |
  56. * --------------- struct pt_regs * stored on stack if 32-bit
  57. * | |
  58. * . .
  59. * | |
  60. * --------------- %esp
  61. * | |
  62. * | | \/ Lower addresses
  63. *
  64. * Thus, regs (or regs->rsp for x86_64) <-> stack base restricts the
  65. * valid(ish) ebp values. Note: (1) for x86_64, NMI and several other
  66. * exceptions use special stacks, maintained by the interrupt stack table
  67. * (IST). These stacks are set up in trap_init() in
  68. * arch/x86_64/kernel/traps.c. Thus, for x86_64, regs now does not point
  69. * to the kernel stack; instead, it points to some location on the NMI
  70. * stack. On the other hand, regs->rsp is the stack pointer saved when the
  71. * NMI occurred. (2) For 32-bit, regs->esp is not valid because the
  72. * processor does not save %esp on the kernel stack when interrupts occur
  73. * in the kernel mode.
  74. */
  75. #ifdef CONFIG_FRAME_POINTER
  76. static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs)
  77. {
  78. unsigned long headaddr = (unsigned long)head;
  79. #ifdef CONFIG_X86_64
  80. unsigned long stack = (unsigned long)regs->rsp;
  81. #else
  82. unsigned long stack = (unsigned long)regs;
  83. #endif
  84. unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE;
  85. return headaddr > stack && headaddr < stack_base;
  86. }
  87. #else
  88. /* without fp, it's just junk */
  89. static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs)
  90. {
  91. return 0;
  92. }
  93. #endif
  94. void
  95. x86_backtrace(struct pt_regs * const regs, unsigned int depth)
  96. {
  97. struct frame_head *head;
  98. #ifdef CONFIG_X86_64
  99. head = (struct frame_head *)regs->rbp;
  100. #else
  101. head = (struct frame_head *)regs->ebp;
  102. #endif
  103. if (!user_mode_vm(regs)) {
  104. while (depth-- && valid_kernel_stack(head, regs))
  105. head = dump_kernel_backtrace(head);
  106. return;
  107. }
  108. while (depth-- && head)
  109. head = dump_user_backtrace(head);
  110. }