Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 1 | //===------------------------- UnwindLevel1.c -----------------------------===// |
| 2 | // |
Chandler Carruth | 61860a5 | 2019-01-19 10:56:40 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 6 | // |
| 7 | // |
| 8 | // Implements C++ ABI Exception Handling Level 1 as documented at: |
| 9 | // http://mentorembedded.github.io/cxx-abi/abi-eh.html |
| 10 | // using libunwind |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Logan Chien | 5456e8d | 2015-07-24 00:16:48 +0000 | [diff] [blame] | 14 | // ARM EHABI does not specify _Unwind_{Get,Set}{GR,IP}(). Thus, we are |
| 15 | // defining inline functions to delegate the function calls to |
| 16 | // _Unwind_VRS_{Get,Set}(). However, some applications might declare the |
| 17 | // function protetype directly (instead of including <unwind.h>), thus we need |
| 18 | // to export these functions from libunwind.so as well. |
| 19 | #define _LIBUNWIND_UNWIND_LEVEL1_EXTERNAL_LINKAGE 1 |
| 20 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 21 | #include <inttypes.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | #include "libunwind.h" |
| 29 | #include "unwind.h" |
| 30 | #include "config.h" |
| 31 | |
Martin Storsjo | 9f16c6c | 2017-09-26 08:07:26 +0000 | [diff] [blame] | 32 | #if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__) |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 33 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 34 | #ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND |
| 35 | |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 36 | static _Unwind_Reason_Code |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 37 | unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) { |
| 38 | unw_init_local(cursor, uc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 39 | |
| 40 | // Walk each frame looking for a place to stop. |
Logan Chien | f93f235 | 2015-06-25 00:05:24 +0000 | [diff] [blame] | 41 | bool handlerNotFound = true; |
| 42 | while (handlerNotFound) { |
Ed Maste | 6f72338 | 2016-08-30 13:08:21 +0000 | [diff] [blame] | 43 | // Ask libunwind to get next frame (skip over first which is |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 44 | // _Unwind_RaiseException). |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 45 | int stepResult = unw_step(cursor); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 46 | if (stepResult == 0) { |
| 47 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_step() reached " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 48 | "bottom => _URC_END_OF_STACK", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 49 | (void *)exception_object); |
| 50 | return _URC_END_OF_STACK; |
| 51 | } else if (stepResult < 0) { |
| 52 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_step failed => " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 53 | "_URC_FATAL_PHASE1_ERROR", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 54 | (void *)exception_object); |
| 55 | return _URC_FATAL_PHASE1_ERROR; |
| 56 | } |
| 57 | |
| 58 | // See if frame has code to run (has personality routine). |
| 59 | unw_proc_info_t frameInfo; |
| 60 | unw_word_t sp; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 61 | if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 62 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_get_proc_info " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 63 | "failed => _URC_FATAL_PHASE1_ERROR", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 64 | (void *)exception_object); |
| 65 | return _URC_FATAL_PHASE1_ERROR; |
| 66 | } |
| 67 | |
| 68 | // When tracing, print state information. |
| 69 | if (_LIBUNWIND_TRACING_UNWINDING) { |
| 70 | char functionBuf[512]; |
| 71 | const char *functionName = functionBuf; |
| 72 | unw_word_t offset; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 73 | if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 74 | &offset) != UNW_ESUCCESS) || |
| 75 | (frameInfo.start_ip + offset > frameInfo.end_ip)) |
| 76 | functionName = ".anonymous."; |
| 77 | unw_word_t pc; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 78 | unw_get_reg(cursor, UNW_REG_IP, &pc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 79 | _LIBUNWIND_TRACE_UNWINDING( |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 80 | "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR |
| 81 | ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 82 | (void *)exception_object, pc, frameInfo.start_ip, functionName, |
| 83 | frameInfo.lsda, frameInfo.handler); |
| 84 | } |
| 85 | |
| 86 | // If there is a personality routine, ask it if it will want to stop at |
| 87 | // this frame. |
| 88 | if (frameInfo.handler != 0) { |
| 89 | __personality_routine p = |
Martin Storsjo | cac4138 | 2017-10-27 08:11:36 +0000 | [diff] [blame] | 90 | (__personality_routine)(uintptr_t)(frameInfo.handler); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 91 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 92 | "unwind_phase1(ex_ojb=%p): calling personality function %p", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 93 | (void *)exception_object, (void *)(uintptr_t)p); |
| 94 | _Unwind_Reason_Code personalityResult = |
| 95 | (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class, |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 96 | exception_object, (struct _Unwind_Context *)(cursor)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 97 | switch (personalityResult) { |
| 98 | case _URC_HANDLER_FOUND: |
| 99 | // found a catch clause or locals that need destructing in this frame |
| 100 | // stop search and remember stack pointer at the frame |
| 101 | handlerNotFound = false; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 102 | unw_get_reg(cursor, UNW_REG_SP, &sp); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 103 | exception_object->private_2 = (uintptr_t)sp; |
| 104 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 105 | "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 106 | (void *)exception_object); |
| 107 | return _URC_NO_REASON; |
| 108 | |
| 109 | case _URC_CONTINUE_UNWIND: |
| 110 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 111 | "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 112 | (void *)exception_object); |
| 113 | // continue unwinding |
| 114 | break; |
| 115 | |
| 116 | default: |
| 117 | // something went wrong |
| 118 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 119 | "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 120 | (void *)exception_object); |
| 121 | return _URC_FATAL_PHASE1_ERROR; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | return _URC_NO_REASON; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | static _Unwind_Reason_Code |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 130 | unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) { |
| 131 | unw_init_local(cursor, uc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 132 | |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 133 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 134 | (void *)exception_object); |
| 135 | |
| 136 | // Walk each frame until we reach where search phase said to stop. |
| 137 | while (true) { |
| 138 | |
Ed Maste | 6f72338 | 2016-08-30 13:08:21 +0000 | [diff] [blame] | 139 | // Ask libunwind to get next frame (skip over first which is |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 140 | // _Unwind_RaiseException). |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 141 | int stepResult = unw_step(cursor); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 142 | if (stepResult == 0) { |
| 143 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step() reached " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 144 | "bottom => _URC_END_OF_STACK", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 145 | (void *)exception_object); |
| 146 | return _URC_END_OF_STACK; |
| 147 | } else if (stepResult < 0) { |
| 148 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step failed => " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 149 | "_URC_FATAL_PHASE1_ERROR", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 150 | (void *)exception_object); |
| 151 | return _URC_FATAL_PHASE2_ERROR; |
| 152 | } |
| 153 | |
| 154 | // Get info about this frame. |
| 155 | unw_word_t sp; |
| 156 | unw_proc_info_t frameInfo; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 157 | unw_get_reg(cursor, UNW_REG_SP, &sp); |
| 158 | if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 159 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_get_proc_info " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 160 | "failed => _URC_FATAL_PHASE1_ERROR", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 161 | (void *)exception_object); |
| 162 | return _URC_FATAL_PHASE2_ERROR; |
| 163 | } |
| 164 | |
| 165 | // When tracing, print state information. |
| 166 | if (_LIBUNWIND_TRACING_UNWINDING) { |
| 167 | char functionBuf[512]; |
| 168 | const char *functionName = functionBuf; |
| 169 | unw_word_t offset; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 170 | if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 171 | &offset) != UNW_ESUCCESS) || |
| 172 | (frameInfo.start_ip + offset > frameInfo.end_ip)) |
| 173 | functionName = ".anonymous."; |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 174 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR |
| 175 | ", func=%s, sp=0x%" PRIxPTR ", lsda=0x%" PRIxPTR |
| 176 | ", personality=0x%" PRIxPTR, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 177 | (void *)exception_object, frameInfo.start_ip, |
| 178 | functionName, sp, frameInfo.lsda, |
| 179 | frameInfo.handler); |
| 180 | } |
| 181 | |
| 182 | // If there is a personality routine, tell it we are unwinding. |
| 183 | if (frameInfo.handler != 0) { |
| 184 | __personality_routine p = |
Martin Storsjo | cac4138 | 2017-10-27 08:11:36 +0000 | [diff] [blame] | 185 | (__personality_routine)(uintptr_t)(frameInfo.handler); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 186 | _Unwind_Action action = _UA_CLEANUP_PHASE; |
| 187 | if (sp == exception_object->private_2) { |
| 188 | // Tell personality this was the frame it marked in phase 1. |
| 189 | action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME); |
| 190 | } |
| 191 | _Unwind_Reason_Code personalityResult = |
| 192 | (*p)(1, action, exception_object->exception_class, exception_object, |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 193 | (struct _Unwind_Context *)(cursor)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 194 | switch (personalityResult) { |
| 195 | case _URC_CONTINUE_UNWIND: |
| 196 | // Continue unwinding |
| 197 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 198 | "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 199 | (void *)exception_object); |
| 200 | if (sp == exception_object->private_2) { |
| 201 | // Phase 1 said we would stop at this frame, but we did not... |
| 202 | _LIBUNWIND_ABORT("during phase1 personality function said it would " |
| 203 | "stop here, but now in phase2 it did not stop here"); |
| 204 | } |
| 205 | break; |
| 206 | case _URC_INSTALL_CONTEXT: |
| 207 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 208 | "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 209 | (void *)exception_object); |
| 210 | // Personality routine says to transfer control to landing pad. |
| 211 | // We may get control back if landing pad calls _Unwind_Resume(). |
| 212 | if (_LIBUNWIND_TRACING_UNWINDING) { |
| 213 | unw_word_t pc; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 214 | unw_get_reg(cursor, UNW_REG_IP, &pc); |
| 215 | unw_get_reg(cursor, UNW_REG_SP, &sp); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 216 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering " |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 217 | "user code with ip=0x%" PRIxPTR |
| 218 | ", sp=0x%" PRIxPTR, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 219 | (void *)exception_object, pc, sp); |
| 220 | } |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 221 | unw_resume(cursor); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 222 | // unw_resume() only returns if there was an error. |
| 223 | return _URC_FATAL_PHASE2_ERROR; |
| 224 | default: |
| 225 | // Personality routine returned an unknown result code. |
| 226 | _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d", |
| 227 | personalityResult); |
| 228 | return _URC_FATAL_PHASE2_ERROR; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Clean up phase did not resume at the frame that the search phase |
| 234 | // said it would... |
| 235 | return _URC_FATAL_PHASE2_ERROR; |
| 236 | } |
| 237 | |
| 238 | static _Unwind_Reason_Code |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 239 | unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 240 | _Unwind_Exception *exception_object, |
| 241 | _Unwind_Stop_Fn stop, void *stop_parameter) { |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 242 | unw_init_local(cursor, uc); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 243 | |
| 244 | // Walk each frame until we reach where search phase said to stop |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 245 | while (unw_step(cursor) > 0) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 246 | |
| 247 | // Update info about this frame. |
| 248 | unw_proc_info_t frameInfo; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 249 | if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 250 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): unw_step " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 251 | "failed => _URC_END_OF_STACK", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 252 | (void *)exception_object); |
| 253 | return _URC_FATAL_PHASE2_ERROR; |
| 254 | } |
| 255 | |
| 256 | // When tracing, print state information. |
| 257 | if (_LIBUNWIND_TRACING_UNWINDING) { |
| 258 | char functionBuf[512]; |
| 259 | const char *functionName = functionBuf; |
| 260 | unw_word_t offset; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 261 | if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 262 | &offset) != UNW_ESUCCESS) || |
| 263 | (frameInfo.start_ip + offset > frameInfo.end_ip)) |
| 264 | functionName = ".anonymous."; |
| 265 | _LIBUNWIND_TRACE_UNWINDING( |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 266 | "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR |
| 267 | ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 268 | (void *)exception_object, frameInfo.start_ip, functionName, |
| 269 | frameInfo.lsda, frameInfo.handler); |
| 270 | } |
| 271 | |
| 272 | // Call stop function at each frame. |
| 273 | _Unwind_Action action = |
| 274 | (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE); |
| 275 | _Unwind_Reason_Code stopResult = |
| 276 | (*stop)(1, action, exception_object->exception_class, exception_object, |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 277 | (struct _Unwind_Context *)(cursor), stop_parameter); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 278 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 279 | "unwind_phase2_forced(ex_ojb=%p): stop function returned %d", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 280 | (void *)exception_object, stopResult); |
| 281 | if (stopResult != _URC_NO_REASON) { |
| 282 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 283 | "unwind_phase2_forced(ex_ojb=%p): stopped by stop function", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 284 | (void *)exception_object); |
| 285 | return _URC_FATAL_PHASE2_ERROR; |
| 286 | } |
| 287 | |
| 288 | // If there is a personality routine, tell it we are unwinding. |
| 289 | if (frameInfo.handler != 0) { |
| 290 | __personality_routine p = |
Charles Davis | 5aede5d | 2018-08-08 04:21:24 +0000 | [diff] [blame] | 291 | (__personality_routine)(intptr_t)(frameInfo.handler); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 292 | _LIBUNWIND_TRACE_UNWINDING( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 293 | "unwind_phase2_forced(ex_ojb=%p): calling personality function %p", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 294 | (void *)exception_object, (void *)(uintptr_t)p); |
| 295 | _Unwind_Reason_Code personalityResult = |
| 296 | (*p)(1, action, exception_object->exception_class, exception_object, |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 297 | (struct _Unwind_Context *)(cursor)); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 298 | switch (personalityResult) { |
| 299 | case _URC_CONTINUE_UNWIND: |
| 300 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
| 301 | "personality returned " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 302 | "_URC_CONTINUE_UNWIND", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 303 | (void *)exception_object); |
| 304 | // Destructors called, continue unwinding |
| 305 | break; |
| 306 | case _URC_INSTALL_CONTEXT: |
| 307 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
| 308 | "personality returned " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 309 | "_URC_INSTALL_CONTEXT", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 310 | (void *)exception_object); |
| 311 | // We may get control back if landing pad calls _Unwind_Resume(). |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 312 | unw_resume(cursor); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 313 | break; |
| 314 | default: |
| 315 | // Personality routine returned an unknown result code. |
| 316 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " |
| 317 | "personality returned %d, " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 318 | "_URC_FATAL_PHASE2_ERROR", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 319 | (void *)exception_object, personalityResult); |
| 320 | return _URC_FATAL_PHASE2_ERROR; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // Call stop function one last time and tell it we've reached the end |
| 326 | // of the stack. |
| 327 | _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop " |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 328 | "function with _UA_END_OF_STACK", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 329 | (void *)exception_object); |
| 330 | _Unwind_Action lastAction = |
| 331 | (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK); |
| 332 | (*stop)(1, lastAction, exception_object->exception_class, exception_object, |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 333 | (struct _Unwind_Context *)(cursor), stop_parameter); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 334 | |
| 335 | // Clean up phase did not resume at the frame that the search phase said it |
| 336 | // would. |
| 337 | return _URC_FATAL_PHASE2_ERROR; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | /// Called by __cxa_throw. Only returns if there is a fatal error. |
| 342 | _LIBUNWIND_EXPORT _Unwind_Reason_Code |
| 343 | _Unwind_RaiseException(_Unwind_Exception *exception_object) { |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 344 | _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 345 | (void *)exception_object); |
| 346 | unw_context_t uc; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 347 | unw_cursor_t cursor; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 348 | unw_getcontext(&uc); |
| 349 | |
| 350 | // Mark that this is a non-forced unwind, so _Unwind_Resume() |
| 351 | // can do the right thing. |
| 352 | exception_object->private_1 = 0; |
| 353 | exception_object->private_2 = 0; |
| 354 | |
| 355 | // phase 1: the search phase |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 356 | _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 357 | if (phase1 != _URC_NO_REASON) |
| 358 | return phase1; |
| 359 | |
| 360 | // phase 2: the clean up phase |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 361 | return unwind_phase2(&uc, &cursor, exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
| 365 | |
| 366 | /// When _Unwind_RaiseException() is in phase2, it hands control |
| 367 | /// to the personality function at each frame. The personality |
| 368 | /// may force a jump to a landing pad in that function, the landing |
| 369 | /// pad code may then call _Unwind_Resume() to continue with the |
| 370 | /// unwinding. Note: the call to _Unwind_Resume() is from compiler |
| 371 | /// geneated user code. All other _Unwind_* routines are called |
| 372 | /// by the C++ runtime __cxa_* routines. |
| 373 | /// |
| 374 | /// Note: re-throwing an exception (as opposed to continuing the unwind) |
| 375 | /// is implemented by having the code call __cxa_rethrow() which |
| 376 | /// in turn calls _Unwind_Resume_or_Rethrow(). |
| 377 | _LIBUNWIND_EXPORT void |
| 378 | _Unwind_Resume(_Unwind_Exception *exception_object) { |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 379 | _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 380 | unw_context_t uc; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 381 | unw_cursor_t cursor; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 382 | unw_getcontext(&uc); |
| 383 | |
| 384 | if (exception_object->private_1 != 0) |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 385 | unwind_phase2_forced(&uc, &cursor, exception_object, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 386 | (_Unwind_Stop_Fn) exception_object->private_1, |
| 387 | (void *)exception_object->private_2); |
| 388 | else |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 389 | unwind_phase2(&uc, &cursor, exception_object); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 390 | |
| 391 | // Clients assume _Unwind_Resume() does not return, so all we can do is abort. |
| 392 | _LIBUNWIND_ABORT("_Unwind_Resume() can't return"); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | |
| 397 | /// Not used by C++. |
| 398 | /// Unwinds stack, calling "stop" function at each frame. |
| 399 | /// Could be used to implement longjmp(). |
| 400 | _LIBUNWIND_EXPORT _Unwind_Reason_Code |
| 401 | _Unwind_ForcedUnwind(_Unwind_Exception *exception_object, |
| 402 | _Unwind_Stop_Fn stop, void *stop_parameter) { |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 403 | _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 404 | (void *)exception_object, (void *)(uintptr_t)stop); |
| 405 | unw_context_t uc; |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 406 | unw_cursor_t cursor; |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 407 | unw_getcontext(&uc); |
| 408 | |
| 409 | // Mark that this is a forced unwind, so _Unwind_Resume() can do |
| 410 | // the right thing. |
| 411 | exception_object->private_1 = (uintptr_t) stop; |
| 412 | exception_object->private_2 = (uintptr_t) stop_parameter; |
| 413 | |
| 414 | // do it |
Asiri Rathnayake | 2ae6e62 | 2016-06-14 15:51:01 +0000 | [diff] [blame] | 415 | return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | |
| 419 | /// Called by personality handler during phase 2 to get LSDA for current frame. |
| 420 | _LIBUNWIND_EXPORT uintptr_t |
| 421 | _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) { |
| 422 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 423 | unw_proc_info_t frameInfo; |
| 424 | uintptr_t result = 0; |
| 425 | if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS) |
| 426 | result = (uintptr_t)frameInfo.lsda; |
| 427 | _LIBUNWIND_TRACE_API( |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 428 | "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 429 | (void *)context, result); |
| 430 | if (result != 0) { |
| 431 | if (*((uint8_t *)result) != 0xFF) |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 432 | _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 433 | result); |
| 434 | } |
| 435 | return result; |
| 436 | } |
| 437 | |
| 438 | |
| 439 | /// Called by personality handler during phase 2 to find the start of the |
| 440 | /// function. |
| 441 | _LIBUNWIND_EXPORT uintptr_t |
| 442 | _Unwind_GetRegionStart(struct _Unwind_Context *context) { |
| 443 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 444 | unw_proc_info_t frameInfo; |
| 445 | uintptr_t result = 0; |
| 446 | if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS) |
| 447 | result = (uintptr_t)frameInfo.start_ip; |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 448 | _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR, |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 449 | (void *)context, result); |
| 450 | return result; |
| 451 | } |
| 452 | |
Charles Davis | fa2e620 | 2018-08-30 21:29:00 +0000 | [diff] [blame] | 453 | #endif // !_LIBUNWIND_SUPPORT_SEH_UNWIND |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 454 | |
| 455 | /// Called by personality handler during phase 2 if a foreign exception |
| 456 | // is caught. |
| 457 | _LIBUNWIND_EXPORT void |
| 458 | _Unwind_DeleteException(_Unwind_Exception *exception_object) { |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 459 | _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)", |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 460 | (void *)exception_object); |
| 461 | if (exception_object->exception_cleanup != NULL) |
| 462 | (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, |
| 463 | exception_object); |
| 464 | } |
| 465 | |
| 466 | /// Called by personality handler during phase 2 to get register values. |
| 467 | _LIBUNWIND_EXPORT uintptr_t |
| 468 | _Unwind_GetGR(struct _Unwind_Context *context, int index) { |
| 469 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 470 | unw_word_t result; |
| 471 | unw_get_reg(cursor, index, &result); |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 472 | _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR, |
| 473 | (void *)context, index, result); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 474 | return (uintptr_t)result; |
| 475 | } |
| 476 | |
| 477 | /// Called by personality handler during phase 2 to alter register values. |
| 478 | _LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index, |
| 479 | uintptr_t value) { |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 480 | _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR |
Ed Maste | 41bc5a7 | 2016-08-30 15:38:10 +0000 | [diff] [blame] | 481 | ")", |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 482 | (void *)context, index, value); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 483 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 484 | unw_set_reg(cursor, index, value); |
| 485 | } |
| 486 | |
| 487 | /// Called by personality handler during phase 2 to get instruction pointer. |
| 488 | _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) { |
| 489 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 490 | unw_word_t result; |
| 491 | unw_get_reg(cursor, UNW_REG_IP, &result); |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 492 | _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR, |
| 493 | (void *)context, result); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 494 | return (uintptr_t)result; |
| 495 | } |
| 496 | |
| 497 | /// Called by personality handler during phase 2 to alter instruction pointer, |
| 498 | /// such as setting where the landing pad is, so _Unwind_Resume() will |
| 499 | /// start executing in the landing pad. |
| 500 | _LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context, |
| 501 | uintptr_t value) { |
Martin Storsjo | 97e2d98 | 2017-10-30 19:06:34 +0000 | [diff] [blame] | 502 | _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")", |
| 503 | (void *)context, value); |
Saleem Abdulrasool | 1755266 | 2015-04-24 19:39:17 +0000 | [diff] [blame] | 504 | unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 505 | unw_set_reg(cursor, UNW_REG_IP, value); |
| 506 | } |
| 507 | |
Martin Storsjo | 9f16c6c | 2017-09-26 08:07:26 +0000 | [diff] [blame] | 508 | #endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__) |