backtrace.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // backtrace.h - Fallback backtrace implementation. i386 implementation.
  2. /* Copyright (C) 2005, 2006 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. #ifndef __SYSDEP_BACKTRACE_H__
  8. #define __SYSDEP_BACKTRACE_H__
  9. #include <java-stack.h>
  10. #ifdef __CYGWIN__
  11. /* To allow this to link as a DLL. */
  12. #define MAIN_FUNC dll_crt0__FP11per_process
  13. extern "C" int MAIN_FUNC () __declspec(dllimport);
  14. #elif defined (_WIN32)
  15. #define MAIN_FUNC DllMain
  16. extern "C" int __stdcall MAIN_FUNC (void *, unsigned long, void *);
  17. #else /* !__CYGWIN__ && !_WIN32 */
  18. #define MAIN_FUNC main
  19. extern int MAIN_FUNC (int, char **);
  20. #endif /* ?__CYGWIN__ */
  21. /* The context used to keep track of our position while unwinding through
  22. the call stack. */
  23. struct _Unwind_Context
  24. {
  25. /* The starting address of the method. */
  26. _Jv_uintptr_t meth_addr;
  27. /* The return address in the method. */
  28. _Jv_uintptr_t ret_addr;
  29. };
  30. #ifdef SJLJ_EXCEPTIONS
  31. #undef _Unwind_GetIPInfo
  32. #define _Unwind_GetIPInfo(ctx,ip_before_insn) \
  33. (*(ip_before_insn) = 1, (ctx)->ret_addr)
  34. #undef _Unwind_GetRegionStart
  35. #define _Unwind_GetRegionStart(ctx) \
  36. ((ctx)->meth_addr)
  37. #undef _Unwind_Backtrace
  38. #define _Unwind_Backtrace(trace_fn,state_ptr) \
  39. (fallback_backtrace (trace_fn, state_ptr))
  40. #endif /* SJLJ_EXCEPTIONS */
  41. /* Unwind through the call stack calling TRACE_FN with STATE for each stack
  42. frame. Returns the reason why the unwinding was stopped. */
  43. _Unwind_Reason_Code
  44. fallback_backtrace (_Unwind_Trace_Fn trace_fn, _Jv_UnwindState *state)
  45. {
  46. register _Jv_uintptr_t *_ebp __asm__ ("ebp");
  47. register _Jv_uintptr_t _esp __asm__ ("esp");
  48. _Jv_uintptr_t rfp;
  49. _Unwind_Context ctx;
  50. for (rfp = *_ebp; rfp; rfp = *(_Jv_uintptr_t *)rfp)
  51. {
  52. /* Sanity checks to eliminate dubious-looking frame pointer chains.
  53. The frame pointer should be a 32-bit word-aligned stack address.
  54. Since the stack grows downwards on x86, the frame pointer must have
  55. a value greater than the current value of the stack pointer, it
  56. should not be below the supposed next frame pointer and it should
  57. not be too far off from the supposed next frame pointer. */
  58. int diff = *(_Jv_uintptr_t *)rfp - rfp;
  59. if ((rfp & 0x00000003) != 0 || rfp < _esp
  60. || diff > 4 * 1024 || diff < 0)
  61. break;
  62. /* Get the return address in the calling function. This is stored on
  63. the stack just before the value of the old frame pointer. */
  64. ctx.ret_addr = *(_Jv_uintptr_t *)(rfp + sizeof (_Jv_uintptr_t));
  65. /* Try to locate a "pushl %ebp; movl %esp, %ebp" function prologue
  66. by scanning backwards at even addresses below the return address.
  67. This instruction sequence is encoded either as 0x55 0x89 0xE5 or as
  68. 0x55 0x8B 0xEC. We give up if we do not find this sequence even
  69. after scanning 1024K of memory.
  70. FIXME: This is not robust and will probably give us false positives,
  71. but this is about the best we can do if we do not have DWARF-2 unwind
  72. information based exception handling. */
  73. ctx.meth_addr = (_Jv_uintptr_t)NULL;
  74. _Jv_uintptr_t scan_addr = (ctx.ret_addr & 0xFFFFFFFE) - 2;
  75. _Jv_uintptr_t limit_addr
  76. = (scan_addr > 1024 * 1024) ? (scan_addr - 1024 * 1024) : 2;
  77. for ( ; scan_addr >= limit_addr; scan_addr -= 2)
  78. {
  79. unsigned char *scan_bytes = (unsigned char *)scan_addr;
  80. if (scan_bytes[0] == 0x55
  81. && ((scan_bytes[1] == 0x89 && scan_bytes[2] == 0xE5)
  82. || (scan_bytes[1] == 0x8B && scan_bytes[2] == 0xEC)))
  83. {
  84. ctx.meth_addr = scan_addr;
  85. break;
  86. }
  87. }
  88. /* Now call the unwinder callback function. */
  89. if (trace_fn != NULL)
  90. (*trace_fn) (&ctx, state);
  91. /* No need to unwind beyond _Jv_RunMain(), _Jv_ThreadStart or
  92. main(). */
  93. void *jv_runmain
  94. = (void *)(void (*)(JvVMInitArgs *, jclass, const char *, int,
  95. const char **, bool))_Jv_RunMain;
  96. if (ctx.meth_addr == (_Jv_uintptr_t)jv_runmain
  97. || ctx.meth_addr == (_Jv_uintptr_t)_Jv_ThreadStart
  98. || (ctx.meth_addr - (_Jv_uintptr_t)MAIN_FUNC) < 16)
  99. break;
  100. }
  101. return _URC_NO_REASON;
  102. }
  103. #endif