RaiseException.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2003-2004 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. #include "unwind-internal.h"
  22. PROTECTED _Unwind_Reason_Code
  23. _Unwind_RaiseException (struct _Unwind_Exception *exception_object)
  24. {
  25. uint64_t exception_class = exception_object->exception_class;
  26. _Unwind_Personality_Fn personality;
  27. struct _Unwind_Context context;
  28. _Unwind_Reason_Code reason;
  29. unw_proc_info_t pi;
  30. unw_context_t uc;
  31. unw_word_t ip;
  32. int ret;
  33. Debug (1, "(exception_object=%p)\n", exception_object);
  34. if (_Unwind_InitContext (&context, &uc) < 0)
  35. return _URC_FATAL_PHASE1_ERROR;
  36. /* Phase 1 (search phase) */
  37. while (1)
  38. {
  39. if ((ret = unw_step (&context.cursor)) <= 0)
  40. {
  41. if (ret == 0)
  42. {
  43. Debug (1, "no handler found\n");
  44. return _URC_END_OF_STACK;
  45. }
  46. else
  47. return _URC_FATAL_PHASE1_ERROR;
  48. }
  49. if (unw_get_proc_info (&context.cursor, &pi) < 0)
  50. return _URC_FATAL_PHASE1_ERROR;
  51. personality = (_Unwind_Personality_Fn) (uintptr_t) pi.handler;
  52. if (personality)
  53. {
  54. reason = (*personality) (_U_VERSION, _UA_SEARCH_PHASE,
  55. exception_class, exception_object,
  56. &context);
  57. if (reason != _URC_CONTINUE_UNWIND)
  58. {
  59. if (reason == _URC_HANDLER_FOUND)
  60. break;
  61. else
  62. {
  63. Debug (1, "personality returned %d\n", reason);
  64. return _URC_FATAL_PHASE1_ERROR;
  65. }
  66. }
  67. }
  68. }
  69. /* Exceptions are associated with IP-ranges. If a given exception
  70. is handled at a particular IP, it will _always_ be handled at
  71. that IP. If this weren't true, we'd have to track the tuple
  72. (IP,SP,BSP) to uniquely identify the stack frame that's handling
  73. the exception. */
  74. if (unw_get_reg (&context.cursor, UNW_REG_IP, &ip) < 0)
  75. return _URC_FATAL_PHASE1_ERROR;
  76. exception_object->private_1 = 0; /* clear "stop" pointer */
  77. exception_object->private_2 = ip; /* save frame marker */
  78. Debug (1, "found handler for IP=%lx; entering cleanup phase\n", (long) ip);
  79. /* Reset the cursor to the first frame: */
  80. if (unw_init_local (&context.cursor, &uc) < 0)
  81. return _URC_FATAL_PHASE1_ERROR;
  82. return _Unwind_Phase2 (exception_object, &context);
  83. }
  84. _Unwind_Reason_Code
  85. __libunwind_Unwind_RaiseException (struct _Unwind_Exception *)
  86. ALIAS (_Unwind_RaiseException);