backtrace.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_backtrace(struct frame_head * head)
  21. {
  22. struct frame_head bufhead[2];
  23. /* Also check accessibility of one struct frame_head beyond */
  24. if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
  25. return NULL;
  26. if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
  27. return NULL;
  28. oprofile_add_trace(bufhead[0].ret);
  29. /* frame pointers should strictly progress back up the stack
  30. * (towards higher addresses) */
  31. if (head >= bufhead[0].ebp)
  32. return NULL;
  33. return bufhead[0].ebp;
  34. }
  35. /*
  36. * | | /\ Higher addresses
  37. * | |
  38. * --------------- stack base (address of current_thread_info)
  39. * | thread info |
  40. * . .
  41. * | stack |
  42. * --------------- saved regs->ebp value if valid (frame_head address)
  43. * . .
  44. * --------------- saved regs->rsp value if x86_64
  45. * | |
  46. * --------------- struct pt_regs * stored on stack if 32-bit
  47. * | |
  48. * . .
  49. * | |
  50. * --------------- %esp
  51. * | |
  52. * | | \/ Lower addresses
  53. *
  54. * Thus, regs (or regs->rsp for x86_64) <-> stack base restricts the
  55. * valid(ish) ebp values. Note: (1) for x86_64, NMI and several other
  56. * exceptions use special stacks, maintained by the interrupt stack table
  57. * (IST). These stacks are set up in trap_init() in
  58. * arch/x86_64/kernel/traps.c. Thus, for x86_64, regs now does not point
  59. * to the kernel stack; instead, it points to some location on the NMI
  60. * stack. On the other hand, regs->rsp is the stack pointer saved when the
  61. * NMI occurred. (2) For 32-bit, regs->esp is not valid because the
  62. * processor does not save %esp on the kernel stack when interrupts occur
  63. * in the kernel mode.
  64. */
  65. #ifdef CONFIG_FRAME_POINTER
  66. static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs)
  67. {
  68. unsigned long headaddr = (unsigned long)head;
  69. #ifdef CONFIG_X86_64
  70. unsigned long stack = (unsigned long)regs->rsp;
  71. #else
  72. unsigned long stack = (unsigned long)regs;
  73. #endif
  74. unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE;
  75. return headaddr > stack && headaddr < stack_base;
  76. }
  77. #else
  78. /* without fp, it's just junk */
  79. static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs)
  80. {
  81. return 0;
  82. }
  83. #endif
  84. void
  85. x86_backtrace(struct pt_regs * const regs, unsigned int depth)
  86. {
  87. struct frame_head *head;
  88. #ifdef CONFIG_X86_64
  89. head = (struct frame_head *)regs->rbp;
  90. #else
  91. head = (struct frame_head *)regs->ebp;
  92. #endif
  93. if (!user_mode_vm(regs)) {
  94. while (depth-- && valid_kernel_stack(head, regs))
  95. head = dump_backtrace(head);
  96. return;
  97. }
  98. while (depth-- && head)
  99. head = dump_backtrace(head);
  100. }