blob: 518577b1d50f1fb86166e6c15aeddfa1072d39d5 [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===------------------------- UnwindLevel1.c -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//
9// Implements C++ ABI Exception Handling Level 1 as documented at:
10// http://mentorembedded.github.io/cxx-abi/abi-eh.html
11// using libunwind
12//
13//===----------------------------------------------------------------------===//
14
Logan Chien5456e8d2015-07-24 00:16:48 +000015// ARM EHABI does not specify _Unwind_{Get,Set}{GR,IP}(). Thus, we are
16// defining inline functions to delegate the function calls to
17// _Unwind_VRS_{Get,Set}(). However, some applications might declare the
18// function protetype directly (instead of including <unwind.h>), thus we need
19// to export these functions from libunwind.so as well.
20#define _LIBUNWIND_UNWIND_LEVEL1_EXTERNAL_LINKAGE 1
21
Saleem Abdulrasool17552662015-04-24 19:39:17 +000022#include <inttypes.h>
23#include <stdint.h>
24#include <stdbool.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28
29#include "libunwind.h"
30#include "unwind.h"
31#include "config.h"
32
Martin Storsjo9f16c6c2017-09-26 08:07:26 +000033#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000034
35static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000036unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
37 unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000038
39 // Walk each frame looking for a place to stop.
Logan Chienf93f2352015-06-25 00:05:24 +000040 bool handlerNotFound = true;
41 while (handlerNotFound) {
Ed Maste6f723382016-08-30 13:08:21 +000042 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +000043 // _Unwind_RaiseException).
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000044 int stepResult = unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000045 if (stepResult == 0) {
46 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_step() reached "
Ed Maste41bc5a72016-08-30 15:38:10 +000047 "bottom => _URC_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000048 (void *)exception_object);
49 return _URC_END_OF_STACK;
50 } else if (stepResult < 0) {
51 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_step failed => "
Ed Maste41bc5a72016-08-30 15:38:10 +000052 "_URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000053 (void *)exception_object);
54 return _URC_FATAL_PHASE1_ERROR;
55 }
56
57 // See if frame has code to run (has personality routine).
58 unw_proc_info_t frameInfo;
59 unw_word_t sp;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000060 if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +000061 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_get_proc_info "
Ed Maste41bc5a72016-08-30 15:38:10 +000062 "failed => _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000063 (void *)exception_object);
64 return _URC_FATAL_PHASE1_ERROR;
65 }
66
67 // When tracing, print state information.
68 if (_LIBUNWIND_TRACING_UNWINDING) {
69 char functionBuf[512];
70 const char *functionName = functionBuf;
71 unw_word_t offset;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000072 if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
Saleem Abdulrasool17552662015-04-24 19:39:17 +000073 &offset) != UNW_ESUCCESS) ||
74 (frameInfo.start_ip + offset > frameInfo.end_ip))
75 functionName = ".anonymous.";
76 unw_word_t pc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000077 unw_get_reg(cursor, UNW_REG_IP, &pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000078 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +000079 "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR
80 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000081 (void *)exception_object, pc, frameInfo.start_ip, functionName,
82 frameInfo.lsda, frameInfo.handler);
83 }
84
85 // If there is a personality routine, ask it if it will want to stop at
86 // this frame.
87 if (frameInfo.handler != 0) {
88 __personality_routine p =
Martin Storsjocac41382017-10-27 08:11:36 +000089 (__personality_routine)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000090 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +000091 "unwind_phase1(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000092 (void *)exception_object, (void *)(uintptr_t)p);
93 _Unwind_Reason_Code personalityResult =
94 (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000095 exception_object, (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +000096 switch (personalityResult) {
97 case _URC_HANDLER_FOUND:
98 // found a catch clause or locals that need destructing in this frame
99 // stop search and remember stack pointer at the frame
100 handlerNotFound = false;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000101 unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000102 exception_object->private_2 = (uintptr_t)sp;
103 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000104 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000105 (void *)exception_object);
106 return _URC_NO_REASON;
107
108 case _URC_CONTINUE_UNWIND:
109 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000110 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000111 (void *)exception_object);
112 // continue unwinding
113 break;
114
115 default:
116 // something went wrong
117 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000118 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000119 (void *)exception_object);
120 return _URC_FATAL_PHASE1_ERROR;
121 }
122 }
123 }
124 return _URC_NO_REASON;
125}
126
127
128static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000129unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
130 unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000131
Ed Maste41bc5a72016-08-30 15:38:10 +0000132 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000133 (void *)exception_object);
134
135 // Walk each frame until we reach where search phase said to stop.
136 while (true) {
137
Ed Maste6f723382016-08-30 13:08:21 +0000138 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000139 // _Unwind_RaiseException).
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000140 int stepResult = unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000141 if (stepResult == 0) {
142 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step() reached "
Ed Maste41bc5a72016-08-30 15:38:10 +0000143 "bottom => _URC_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000144 (void *)exception_object);
145 return _URC_END_OF_STACK;
146 } else if (stepResult < 0) {
147 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step failed => "
Ed Maste41bc5a72016-08-30 15:38:10 +0000148 "_URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000149 (void *)exception_object);
150 return _URC_FATAL_PHASE2_ERROR;
151 }
152
153 // Get info about this frame.
154 unw_word_t sp;
155 unw_proc_info_t frameInfo;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000156 unw_get_reg(cursor, UNW_REG_SP, &sp);
157 if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000158 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_get_proc_info "
Ed Maste41bc5a72016-08-30 15:38:10 +0000159 "failed => _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000160 (void *)exception_object);
161 return _URC_FATAL_PHASE2_ERROR;
162 }
163
164 // When tracing, print state information.
165 if (_LIBUNWIND_TRACING_UNWINDING) {
166 char functionBuf[512];
167 const char *functionName = functionBuf;
168 unw_word_t offset;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000169 if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000170 &offset) != UNW_ESUCCESS) ||
171 (frameInfo.start_ip + offset > frameInfo.end_ip))
172 functionName = ".anonymous.";
Martin Storsjo97e2d982017-10-30 19:06:34 +0000173 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR
174 ", func=%s, sp=0x%" PRIxPTR ", lsda=0x%" PRIxPTR
175 ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000176 (void *)exception_object, frameInfo.start_ip,
177 functionName, sp, frameInfo.lsda,
178 frameInfo.handler);
179 }
180
181 // If there is a personality routine, tell it we are unwinding.
182 if (frameInfo.handler != 0) {
183 __personality_routine p =
Martin Storsjocac41382017-10-27 08:11:36 +0000184 (__personality_routine)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000185 _Unwind_Action action = _UA_CLEANUP_PHASE;
186 if (sp == exception_object->private_2) {
187 // Tell personality this was the frame it marked in phase 1.
188 action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME);
189 }
190 _Unwind_Reason_Code personalityResult =
191 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000192 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000193 switch (personalityResult) {
194 case _URC_CONTINUE_UNWIND:
195 // Continue unwinding
196 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000197 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000198 (void *)exception_object);
199 if (sp == exception_object->private_2) {
200 // Phase 1 said we would stop at this frame, but we did not...
201 _LIBUNWIND_ABORT("during phase1 personality function said it would "
202 "stop here, but now in phase2 it did not stop here");
203 }
204 break;
205 case _URC_INSTALL_CONTEXT:
206 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000207 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000208 (void *)exception_object);
209 // Personality routine says to transfer control to landing pad.
210 // We may get control back if landing pad calls _Unwind_Resume().
211 if (_LIBUNWIND_TRACING_UNWINDING) {
212 unw_word_t pc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000213 unw_get_reg(cursor, UNW_REG_IP, &pc);
214 unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000215 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
Martin Storsjo97e2d982017-10-30 19:06:34 +0000216 "user code with ip=0x%" PRIxPTR
217 ", sp=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000218 (void *)exception_object, pc, sp);
219 }
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000220 unw_resume(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000221 // unw_resume() only returns if there was an error.
222 return _URC_FATAL_PHASE2_ERROR;
223 default:
224 // Personality routine returned an unknown result code.
225 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
226 personalityResult);
227 return _URC_FATAL_PHASE2_ERROR;
228 }
229 }
230 }
231
232 // Clean up phase did not resume at the frame that the search phase
233 // said it would...
234 return _URC_FATAL_PHASE2_ERROR;
235}
236
237static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000238unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000239 _Unwind_Exception *exception_object,
240 _Unwind_Stop_Fn stop, void *stop_parameter) {
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000241 unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000242
243 // Walk each frame until we reach where search phase said to stop
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000244 while (unw_step(cursor) > 0) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000245
246 // Update info about this frame.
247 unw_proc_info_t frameInfo;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000248 if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000249 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): unw_step "
Ed Maste41bc5a72016-08-30 15:38:10 +0000250 "failed => _URC_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000251 (void *)exception_object);
252 return _URC_FATAL_PHASE2_ERROR;
253 }
254
255 // When tracing, print state information.
256 if (_LIBUNWIND_TRACING_UNWINDING) {
257 char functionBuf[512];
258 const char *functionName = functionBuf;
259 unw_word_t offset;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000260 if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000261 &offset) != UNW_ESUCCESS) ||
262 (frameInfo.start_ip + offset > frameInfo.end_ip))
263 functionName = ".anonymous.";
264 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +0000265 "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
266 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000267 (void *)exception_object, frameInfo.start_ip, functionName,
268 frameInfo.lsda, frameInfo.handler);
269 }
270
271 // Call stop function at each frame.
272 _Unwind_Action action =
273 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
274 _Unwind_Reason_Code stopResult =
275 (*stop)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000276 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000277 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000278 "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000279 (void *)exception_object, stopResult);
280 if (stopResult != _URC_NO_REASON) {
281 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000282 "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000283 (void *)exception_object);
284 return _URC_FATAL_PHASE2_ERROR;
285 }
286
287 // If there is a personality routine, tell it we are unwinding.
288 if (frameInfo.handler != 0) {
289 __personality_routine p =
290 (__personality_routine)(long)(frameInfo.handler);
291 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000292 "unwind_phase2_forced(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000293 (void *)exception_object, (void *)(uintptr_t)p);
294 _Unwind_Reason_Code personalityResult =
295 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000296 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000297 switch (personalityResult) {
298 case _URC_CONTINUE_UNWIND:
299 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
300 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000301 "_URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000302 (void *)exception_object);
303 // Destructors called, continue unwinding
304 break;
305 case _URC_INSTALL_CONTEXT:
306 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
307 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000308 "_URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000309 (void *)exception_object);
310 // We may get control back if landing pad calls _Unwind_Resume().
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000311 unw_resume(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000312 break;
313 default:
314 // Personality routine returned an unknown result code.
315 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
316 "personality returned %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +0000317 "_URC_FATAL_PHASE2_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000318 (void *)exception_object, personalityResult);
319 return _URC_FATAL_PHASE2_ERROR;
320 }
321 }
322 }
323
324 // Call stop function one last time and tell it we've reached the end
325 // of the stack.
326 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
Ed Maste41bc5a72016-08-30 15:38:10 +0000327 "function with _UA_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000328 (void *)exception_object);
329 _Unwind_Action lastAction =
330 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
331 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000332 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000333
334 // Clean up phase did not resume at the frame that the search phase said it
335 // would.
336 return _URC_FATAL_PHASE2_ERROR;
337}
338
339
340/// Called by __cxa_throw. Only returns if there is a fatal error.
341_LIBUNWIND_EXPORT _Unwind_Reason_Code
342_Unwind_RaiseException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000343 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000344 (void *)exception_object);
345 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000346 unw_cursor_t cursor;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000347 unw_getcontext(&uc);
348
349 // Mark that this is a non-forced unwind, so _Unwind_Resume()
350 // can do the right thing.
351 exception_object->private_1 = 0;
352 exception_object->private_2 = 0;
353
354 // phase 1: the search phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000355 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000356 if (phase1 != _URC_NO_REASON)
357 return phase1;
358
359 // phase 2: the clean up phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000360 return unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000361}
362
363
364
365/// When _Unwind_RaiseException() is in phase2, it hands control
366/// to the personality function at each frame. The personality
367/// may force a jump to a landing pad in that function, the landing
368/// pad code may then call _Unwind_Resume() to continue with the
369/// unwinding. Note: the call to _Unwind_Resume() is from compiler
370/// geneated user code. All other _Unwind_* routines are called
371/// by the C++ runtime __cxa_* routines.
372///
373/// Note: re-throwing an exception (as opposed to continuing the unwind)
374/// is implemented by having the code call __cxa_rethrow() which
375/// in turn calls _Unwind_Resume_or_Rethrow().
376_LIBUNWIND_EXPORT void
377_Unwind_Resume(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000378 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000379 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000380 unw_cursor_t cursor;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000381 unw_getcontext(&uc);
382
383 if (exception_object->private_1 != 0)
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000384 unwind_phase2_forced(&uc, &cursor, exception_object,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000385 (_Unwind_Stop_Fn) exception_object->private_1,
386 (void *)exception_object->private_2);
387 else
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000388 unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000389
390 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
391 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
392}
393
394
395
396/// Not used by C++.
397/// Unwinds stack, calling "stop" function at each frame.
398/// Could be used to implement longjmp().
399_LIBUNWIND_EXPORT _Unwind_Reason_Code
400_Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
401 _Unwind_Stop_Fn stop, void *stop_parameter) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000402 _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000403 (void *)exception_object, (void *)(uintptr_t)stop);
404 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000405 unw_cursor_t cursor;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000406 unw_getcontext(&uc);
407
408 // Mark that this is a forced unwind, so _Unwind_Resume() can do
409 // the right thing.
410 exception_object->private_1 = (uintptr_t) stop;
411 exception_object->private_2 = (uintptr_t) stop_parameter;
412
413 // do it
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000414 return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000415}
416
417
418/// Called by personality handler during phase 2 to get LSDA for current frame.
419_LIBUNWIND_EXPORT uintptr_t
420_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
421 unw_cursor_t *cursor = (unw_cursor_t *)context;
422 unw_proc_info_t frameInfo;
423 uintptr_t result = 0;
424 if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
425 result = (uintptr_t)frameInfo.lsda;
426 _LIBUNWIND_TRACE_API(
Ed Maste41bc5a72016-08-30 15:38:10 +0000427 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000428 (void *)context, result);
429 if (result != 0) {
430 if (*((uint8_t *)result) != 0xFF)
Ed Maste41bc5a72016-08-30 15:38:10 +0000431 _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000432 result);
433 }
434 return result;
435}
436
437
438/// Called by personality handler during phase 2 to find the start of the
439/// function.
440_LIBUNWIND_EXPORT uintptr_t
441_Unwind_GetRegionStart(struct _Unwind_Context *context) {
442 unw_cursor_t *cursor = (unw_cursor_t *)context;
443 unw_proc_info_t frameInfo;
444 uintptr_t result = 0;
445 if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
446 result = (uintptr_t)frameInfo.start_ip;
Ed Maste41bc5a72016-08-30 15:38:10 +0000447 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000448 (void *)context, result);
449 return result;
450}
451
452
453/// Called by personality handler during phase 2 if a foreign exception
454// is caught.
455_LIBUNWIND_EXPORT void
456_Unwind_DeleteException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000457 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000458 (void *)exception_object);
459 if (exception_object->exception_cleanup != NULL)
460 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
461 exception_object);
462}
463
464/// Called by personality handler during phase 2 to get register values.
465_LIBUNWIND_EXPORT uintptr_t
466_Unwind_GetGR(struct _Unwind_Context *context, int index) {
467 unw_cursor_t *cursor = (unw_cursor_t *)context;
468 unw_word_t result;
469 unw_get_reg(cursor, index, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000470 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR,
471 (void *)context, index, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000472 return (uintptr_t)result;
473}
474
475/// Called by personality handler during phase 2 to alter register values.
476_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
477 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000478 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR
Ed Maste41bc5a72016-08-30 15:38:10 +0000479 ")",
Martin Storsjo97e2d982017-10-30 19:06:34 +0000480 (void *)context, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000481 unw_cursor_t *cursor = (unw_cursor_t *)context;
482 unw_set_reg(cursor, index, value);
483}
484
485/// Called by personality handler during phase 2 to get instruction pointer.
486_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
487 unw_cursor_t *cursor = (unw_cursor_t *)context;
488 unw_word_t result;
489 unw_get_reg(cursor, UNW_REG_IP, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000490 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR,
491 (void *)context, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000492 return (uintptr_t)result;
493}
494
495/// Called by personality handler during phase 2 to alter instruction pointer,
496/// such as setting where the landing pad is, so _Unwind_Resume() will
497/// start executing in the landing pad.
498_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
499 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000500 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")",
501 (void *)context, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000502 unw_cursor_t *cursor = (unw_cursor_t *)context;
503 unw_set_reg(cursor, UNW_REG_IP, value);
504}
505
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000506#endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)