backtrace.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include <asm/stacktrace.h>
  16. #include <linux/compat.h>
  17. static void backtrace_warning_symbol(void *data, char *msg,
  18. unsigned long symbol)
  19. {
  20. /* Ignore warnings */
  21. }
  22. static void backtrace_warning(void *data, char *msg)
  23. {
  24. /* Ignore warnings */
  25. }
  26. static int backtrace_stack(void *data, char *name)
  27. {
  28. /* Yes, we want all stacks */
  29. return 0;
  30. }
  31. static void backtrace_address(void *data, unsigned long addr, int reliable)
  32. {
  33. unsigned int *depth = data;
  34. if ((*depth)--)
  35. oprofile_add_trace(addr);
  36. }
  37. static struct stacktrace_ops backtrace_ops = {
  38. .warning = backtrace_warning,
  39. .warning_symbol = backtrace_warning_symbol,
  40. .stack = backtrace_stack,
  41. .address = backtrace_address,
  42. .walk_stack = print_context_stack,
  43. };
  44. #ifdef CONFIG_COMPAT
  45. static struct stack_frame_ia32 *
  46. dump_user_backtrace_32(struct stack_frame_ia32 *head)
  47. {
  48. struct stack_frame_ia32 bufhead[2];
  49. struct stack_frame_ia32 *fp;
  50. /* Also check accessibility of one struct frame_head beyond */
  51. if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
  52. return NULL;
  53. if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
  54. return NULL;
  55. fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
  56. oprofile_add_trace(bufhead[0].return_address);
  57. /* frame pointers should strictly progress back up the stack
  58. * (towards higher addresses) */
  59. if (head >= fp)
  60. return NULL;
  61. return fp;
  62. }
  63. static inline int
  64. x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
  65. {
  66. struct stack_frame_ia32 *head;
  67. /* User process is 32-bit */
  68. if (!current || !test_thread_flag(TIF_IA32))
  69. return 0;
  70. head = (struct stack_frame_ia32 *) regs->bp;
  71. while (depth-- && head)
  72. head = dump_user_backtrace_32(head);
  73. return 1;
  74. }
  75. #else
  76. static inline int
  77. x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
  78. {
  79. return 0;
  80. }
  81. #endif /* CONFIG_COMPAT */
  82. static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
  83. {
  84. struct stack_frame bufhead[2];
  85. /* Also check accessibility of one struct stack_frame beyond */
  86. if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
  87. return NULL;
  88. if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
  89. return NULL;
  90. oprofile_add_trace(bufhead[0].return_address);
  91. /* frame pointers should strictly progress back up the stack
  92. * (towards higher addresses) */
  93. if (head >= bufhead[0].next_frame)
  94. return NULL;
  95. return bufhead[0].next_frame;
  96. }
  97. void
  98. x86_backtrace(struct pt_regs * const regs, unsigned int depth)
  99. {
  100. struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
  101. if (!user_mode_vm(regs)) {
  102. unsigned long stack = kernel_stack_pointer(regs);
  103. if (depth)
  104. dump_trace(NULL, regs, (unsigned long *)stack, 0,
  105. &backtrace_ops, &depth);
  106. return;
  107. }
  108. if (x86_backtrace_32(regs, depth))
  109. return;
  110. while (depth-- && head)
  111. head = dump_user_backtrace(head);
  112. }