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