backtrace.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <linux/compat.h>
  14. #include <linux/uaccess.h>
  15. #include <asm/ptrace.h>
  16. #include <asm/stacktrace.h>
  17. #include <asm/unwind.h>
  18. #ifdef CONFIG_COMPAT
  19. static struct stack_frame_ia32 *
  20. dump_user_backtrace_32(struct stack_frame_ia32 *head)
  21. {
  22. /* Also check accessibility of one struct frame_head beyond: */
  23. struct stack_frame_ia32 bufhead[2];
  24. struct stack_frame_ia32 *fp;
  25. unsigned long bytes;
  26. bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
  27. if (bytes != 0)
  28. return NULL;
  29. fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
  30. oprofile_add_trace(bufhead[0].return_address);
  31. /* frame pointers should strictly progress back up the stack
  32. * (towards higher addresses) */
  33. if (head >= fp)
  34. return NULL;
  35. return fp;
  36. }
  37. static inline int
  38. x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
  39. {
  40. struct stack_frame_ia32 *head;
  41. /* User process is IA32 */
  42. if (!current || !test_thread_flag(TIF_IA32))
  43. return 0;
  44. head = (struct stack_frame_ia32 *) regs->bp;
  45. while (depth-- && head)
  46. head = dump_user_backtrace_32(head);
  47. return 1;
  48. }
  49. #else
  50. static inline int
  51. x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
  52. {
  53. return 0;
  54. }
  55. #endif /* CONFIG_COMPAT */
  56. static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
  57. {
  58. /* Also check accessibility of one struct frame_head beyond: */
  59. struct stack_frame bufhead[2];
  60. unsigned long bytes;
  61. bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
  62. if (bytes != 0)
  63. return NULL;
  64. oprofile_add_trace(bufhead[0].return_address);
  65. /* frame pointers should strictly progress back up the stack
  66. * (towards higher addresses) */
  67. if (head >= bufhead[0].next_frame)
  68. return NULL;
  69. return bufhead[0].next_frame;
  70. }
  71. void
  72. x86_backtrace(struct pt_regs * const regs, unsigned int depth)
  73. {
  74. struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
  75. if (!user_mode(regs)) {
  76. struct unwind_state state;
  77. unsigned long addr;
  78. if (!depth)
  79. return;
  80. oprofile_add_trace(regs->ip);
  81. if (!--depth)
  82. return;
  83. for (unwind_start(&state, current, regs, NULL);
  84. !unwind_done(&state); unwind_next_frame(&state)) {
  85. addr = unwind_get_return_address(&state);
  86. if (!addr)
  87. break;
  88. oprofile_add_trace(addr);
  89. if (!--depth)
  90. break;
  91. }
  92. return;
  93. }
  94. if (x86_backtrace_32(regs, depth))
  95. return;
  96. while (depth-- && head)
  97. head = dump_user_backtrace(head);
  98. }