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