unwind-internal.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2003, 2005 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #ifndef unwind_internal_h
  22. #define unwind_internal_h
  23. #define UNW_LOCAL_ONLY
  24. #include <unwind.h>
  25. #include <stdlib.h>
  26. #include <libunwind.h>
  27. #include "libunwind_i.h"
  28. /* The version of the _Unwind_*() interface implemented by this code. */
  29. #define _U_VERSION 1
  30. typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)
  31. (int, _Unwind_Action, uint64_t, struct _Unwind_Exception *,
  32. struct _Unwind_Context *);
  33. struct _Unwind_Context {
  34. unw_cursor_t cursor;
  35. int end_of_stack; /* set to 1 if the end of stack was reached */
  36. };
  37. /* This must be a macro because unw_getcontext() must be invoked from
  38. the callee, even if optimization (and hence inlining) is turned
  39. off. The macro arguments MUST NOT have any side-effects. */
  40. #define _Unwind_InitContext(context, uc) \
  41. ((context)->end_of_stack = 0, \
  42. ((unw_getcontext (uc) < 0 || unw_init_local (&(context)->cursor, uc) < 0) \
  43. ? -1 : 0))
  44. static _Unwind_Reason_Code ALWAYS_INLINE
  45. _Unwind_Phase2 (struct _Unwind_Exception *exception_object,
  46. struct _Unwind_Context *context)
  47. {
  48. _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) exception_object->private_1;
  49. uint64_t exception_class = exception_object->exception_class;
  50. void *stop_parameter = (void *) exception_object->private_2;
  51. _Unwind_Personality_Fn personality;
  52. _Unwind_Reason_Code reason;
  53. _Unwind_Action actions;
  54. unw_proc_info_t pi;
  55. unw_word_t ip;
  56. int ret;
  57. actions = _UA_CLEANUP_PHASE;
  58. if (stop)
  59. actions |= _UA_FORCE_UNWIND;
  60. while (1)
  61. {
  62. ret = unw_step (&context->cursor);
  63. if (ret <= 0)
  64. {
  65. if (ret == 0)
  66. {
  67. actions |= _UA_END_OF_STACK;
  68. context->end_of_stack = 1;
  69. }
  70. else
  71. return _URC_FATAL_PHASE2_ERROR;
  72. }
  73. if (stop)
  74. {
  75. reason = (*stop) (_U_VERSION, actions, exception_class,
  76. exception_object, context, stop_parameter);
  77. if (reason != _URC_NO_REASON)
  78. /* Stop function may return _URC_FATAL_PHASE2_ERROR if
  79. it's unable to handle end-of-stack condition or
  80. _URC_FATAL_PHASE2_ERROR if something is wrong. Not
  81. that it matters: the resulting state is indeterminate
  82. anyhow so we must return _URC_FATAL_PHASE2_ERROR... */
  83. return _URC_FATAL_PHASE2_ERROR;
  84. }
  85. if (context->end_of_stack
  86. || unw_get_proc_info (&context->cursor, &pi) < 0)
  87. return _URC_FATAL_PHASE2_ERROR;
  88. personality = (_Unwind_Personality_Fn) (uintptr_t) pi.handler;
  89. if (personality)
  90. {
  91. if (!stop)
  92. {
  93. if (unw_get_reg (&context->cursor, UNW_REG_IP, &ip) < 0)
  94. return _URC_FATAL_PHASE2_ERROR;
  95. if ((unsigned long) stop_parameter == ip)
  96. actions |= _UA_HANDLER_FRAME;
  97. }
  98. reason = (*personality) (_U_VERSION, actions, exception_class,
  99. exception_object, context);
  100. if (reason != _URC_CONTINUE_UNWIND)
  101. {
  102. if (reason == _URC_INSTALL_CONTEXT)
  103. {
  104. /* we may regain control via _Unwind_Resume() */
  105. unw_resume (&context->cursor);
  106. abort ();
  107. }
  108. else
  109. return _URC_FATAL_PHASE2_ERROR;
  110. }
  111. if (actions & _UA_HANDLER_FRAME)
  112. /* The personality routine for the handler-frame changed
  113. it's mind; that's a no-no... */
  114. abort ();
  115. }
  116. }
  117. return _URC_FATAL_PHASE2_ERROR; /* shouldn't be reached */
  118. }
  119. #endif /* unwind_internal_h */