blob: ce6eb286bab333cc9a4da6dd0151f1d9ba66f444 [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
Logan Chien06b0c7a2015-07-19 15:23:10 +000033#if !_LIBUNWIND_ARM_EHABI
Saleem Abdulrasool17552662015-04-24 19:39:17 +000034
35static _Unwind_Reason_Code
36unwind_phase1(unw_context_t *uc, _Unwind_Exception *exception_object) {
37 unw_cursor_t cursor1;
38 unw_init_local(&cursor1, uc);
39
40 // Walk each frame looking for a place to stop.
Logan Chienf93f2352015-06-25 00:05:24 +000041 bool handlerNotFound = true;
42 while (handlerNotFound) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +000043 // Ask libuwind to get next frame (skip over first which is
44 // _Unwind_RaiseException).
45 int stepResult = unw_step(&cursor1);
46 if (stepResult == 0) {
47 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_step() reached "
48 "bottom => _URC_END_OF_STACK\n",
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 => "
53 "_URC_FATAL_PHASE1_ERROR\n",
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;
61 if (unw_get_proc_info(&cursor1, &frameInfo) != UNW_ESUCCESS) {
62 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_get_proc_info "
63 "failed => _URC_FATAL_PHASE1_ERROR\n",
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;
73 if ((unw_get_proc_name(&cursor1, functionBuf, sizeof(functionBuf),
74 &offset) != UNW_ESUCCESS) ||
75 (frameInfo.start_ip + offset > frameInfo.end_ip))
76 functionName = ".anonymous.";
77 unw_word_t pc;
78 unw_get_reg(&cursor1, UNW_REG_IP, &pc);
79 _LIBUNWIND_TRACE_UNWINDING(
80 "unwind_phase1(ex_ojb=%p): pc=0x%" PRIx64 ", start_ip=0x%" PRIx64
81 ", func=%s, lsda=0x%" PRIx64 ", personality=0x%" PRIx64 "\n",
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 =
90 (__personality_routine)(long)(frameInfo.handler);
91 _LIBUNWIND_TRACE_UNWINDING(
92 "unwind_phase1(ex_ojb=%p): calling personality function %p\n",
93 (void *)exception_object, (void *)(uintptr_t)p);
94 _Unwind_Reason_Code personalityResult =
95 (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class,
96 exception_object, (struct _Unwind_Context *)(&cursor1));
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;
102 unw_get_reg(&cursor1, UNW_REG_SP, &sp);
103 exception_object->private_2 = (uintptr_t)sp;
104 _LIBUNWIND_TRACE_UNWINDING(
105 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND \n",
106 (void *)exception_object);
107 return _URC_NO_REASON;
108
109 case _URC_CONTINUE_UNWIND:
110 _LIBUNWIND_TRACE_UNWINDING(
111 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND\n",
112 (void *)exception_object);
113 // continue unwinding
114 break;
115
116 default:
117 // something went wrong
118 _LIBUNWIND_TRACE_UNWINDING(
119 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR\n",
120 (void *)exception_object);
121 return _URC_FATAL_PHASE1_ERROR;
122 }
123 }
124 }
125 return _URC_NO_REASON;
126}
127
128
129static _Unwind_Reason_Code
130unwind_phase2(unw_context_t *uc, _Unwind_Exception *exception_object) {
131 unw_cursor_t cursor2;
132 unw_init_local(&cursor2, uc);
133
134 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)\n",
135 (void *)exception_object);
136
137 // Walk each frame until we reach where search phase said to stop.
138 while (true) {
139
140 // Ask libuwind to get next frame (skip over first which is
141 // _Unwind_RaiseException).
142 int stepResult = unw_step(&cursor2);
143 if (stepResult == 0) {
144 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step() reached "
145 "bottom => _URC_END_OF_STACK\n",
146 (void *)exception_object);
147 return _URC_END_OF_STACK;
148 } else if (stepResult < 0) {
149 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step failed => "
150 "_URC_FATAL_PHASE1_ERROR\n",
151 (void *)exception_object);
152 return _URC_FATAL_PHASE2_ERROR;
153 }
154
155 // Get info about this frame.
156 unw_word_t sp;
157 unw_proc_info_t frameInfo;
158 unw_get_reg(&cursor2, UNW_REG_SP, &sp);
159 if (unw_get_proc_info(&cursor2, &frameInfo) != UNW_ESUCCESS) {
160 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_get_proc_info "
161 "failed => _URC_FATAL_PHASE1_ERROR\n",
162 (void *)exception_object);
163 return _URC_FATAL_PHASE2_ERROR;
164 }
165
166 // When tracing, print state information.
167 if (_LIBUNWIND_TRACING_UNWINDING) {
168 char functionBuf[512];
169 const char *functionName = functionBuf;
170 unw_word_t offset;
171 if ((unw_get_proc_name(&cursor2, functionBuf, sizeof(functionBuf),
172 &offset) != UNW_ESUCCESS) ||
173 (frameInfo.start_ip + offset > frameInfo.end_ip))
174 functionName = ".anonymous.";
175 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIx64
176 ", func=%s, sp=0x%" PRIx64 ", lsda=0x%" PRIx64
177 ", personality=0x%" PRIx64 "\n",
178 (void *)exception_object, frameInfo.start_ip,
179 functionName, sp, frameInfo.lsda,
180 frameInfo.handler);
181 }
182
183 // If there is a personality routine, tell it we are unwinding.
184 if (frameInfo.handler != 0) {
185 __personality_routine p =
186 (__personality_routine)(long)(frameInfo.handler);
187 _Unwind_Action action = _UA_CLEANUP_PHASE;
188 if (sp == exception_object->private_2) {
189 // Tell personality this was the frame it marked in phase 1.
190 action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME);
191 }
192 _Unwind_Reason_Code personalityResult =
193 (*p)(1, action, exception_object->exception_class, exception_object,
194 (struct _Unwind_Context *)(&cursor2));
195 switch (personalityResult) {
196 case _URC_CONTINUE_UNWIND:
197 // Continue unwinding
198 _LIBUNWIND_TRACE_UNWINDING(
199 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND\n",
200 (void *)exception_object);
201 if (sp == exception_object->private_2) {
202 // Phase 1 said we would stop at this frame, but we did not...
203 _LIBUNWIND_ABORT("during phase1 personality function said it would "
204 "stop here, but now in phase2 it did not stop here");
205 }
206 break;
207 case _URC_INSTALL_CONTEXT:
208 _LIBUNWIND_TRACE_UNWINDING(
209 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT\n",
210 (void *)exception_object);
211 // Personality routine says to transfer control to landing pad.
212 // We may get control back if landing pad calls _Unwind_Resume().
213 if (_LIBUNWIND_TRACING_UNWINDING) {
214 unw_word_t pc;
215 unw_get_reg(&cursor2, UNW_REG_IP, &pc);
216 unw_get_reg(&cursor2, UNW_REG_SP, &sp);
217 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
218 "user code with ip=0x%" PRIx64
219 ", sp=0x%" PRIx64 "\n",
220 (void *)exception_object, pc, sp);
221 }
222 unw_resume(&cursor2);
223 // unw_resume() only returns if there was an error.
224 return _URC_FATAL_PHASE2_ERROR;
225 default:
226 // Personality routine returned an unknown result code.
227 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
228 personalityResult);
229 return _URC_FATAL_PHASE2_ERROR;
230 }
231 }
232 }
233
234 // Clean up phase did not resume at the frame that the search phase
235 // said it would...
236 return _URC_FATAL_PHASE2_ERROR;
237}
238
239static _Unwind_Reason_Code
240unwind_phase2_forced(unw_context_t *uc,
241 _Unwind_Exception *exception_object,
242 _Unwind_Stop_Fn stop, void *stop_parameter) {
243 unw_cursor_t cursor2;
244 unw_init_local(&cursor2, uc);
245
246 // Walk each frame until we reach where search phase said to stop
247 while (unw_step(&cursor2) > 0) {
248
249 // Update info about this frame.
250 unw_proc_info_t frameInfo;
251 if (unw_get_proc_info(&cursor2, &frameInfo) != UNW_ESUCCESS) {
252 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): unw_step "
253 "failed => _URC_END_OF_STACK\n",
254 (void *)exception_object);
255 return _URC_FATAL_PHASE2_ERROR;
256 }
257
258 // When tracing, print state information.
259 if (_LIBUNWIND_TRACING_UNWINDING) {
260 char functionBuf[512];
261 const char *functionName = functionBuf;
262 unw_word_t offset;
263 if ((unw_get_proc_name(&cursor2, functionBuf, sizeof(functionBuf),
264 &offset) != UNW_ESUCCESS) ||
265 (frameInfo.start_ip + offset > frameInfo.end_ip))
266 functionName = ".anonymous.";
267 _LIBUNWIND_TRACE_UNWINDING(
268 "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIx64
269 ", func=%s, lsda=0x%" PRIx64 ", personality=0x%" PRIx64 "\n",
270 (void *)exception_object, frameInfo.start_ip, functionName,
271 frameInfo.lsda, frameInfo.handler);
272 }
273
274 // Call stop function at each frame.
275 _Unwind_Action action =
276 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
277 _Unwind_Reason_Code stopResult =
278 (*stop)(1, action, exception_object->exception_class, exception_object,
279 (struct _Unwind_Context *)(&cursor2), stop_parameter);
280 _LIBUNWIND_TRACE_UNWINDING(
281 "unwind_phase2_forced(ex_ojb=%p): stop function returned %d\n",
282 (void *)exception_object, stopResult);
283 if (stopResult != _URC_NO_REASON) {
284 _LIBUNWIND_TRACE_UNWINDING(
285 "unwind_phase2_forced(ex_ojb=%p): stopped by stop function\n",
286 (void *)exception_object);
287 return _URC_FATAL_PHASE2_ERROR;
288 }
289
290 // If there is a personality routine, tell it we are unwinding.
291 if (frameInfo.handler != 0) {
292 __personality_routine p =
293 (__personality_routine)(long)(frameInfo.handler);
294 _LIBUNWIND_TRACE_UNWINDING(
295 "unwind_phase2_forced(ex_ojb=%p): calling personality function %p\n",
296 (void *)exception_object, (void *)(uintptr_t)p);
297 _Unwind_Reason_Code personalityResult =
298 (*p)(1, action, exception_object->exception_class, exception_object,
299 (struct _Unwind_Context *)(&cursor2));
300 switch (personalityResult) {
301 case _URC_CONTINUE_UNWIND:
302 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
303 "personality returned "
304 "_URC_CONTINUE_UNWIND\n",
305 (void *)exception_object);
306 // Destructors called, continue unwinding
307 break;
308 case _URC_INSTALL_CONTEXT:
309 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
310 "personality returned "
311 "_URC_INSTALL_CONTEXT\n",
312 (void *)exception_object);
313 // We may get control back if landing pad calls _Unwind_Resume().
314 unw_resume(&cursor2);
315 break;
316 default:
317 // Personality routine returned an unknown result code.
318 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
319 "personality returned %d, "
320 "_URC_FATAL_PHASE2_ERROR\n",
321 (void *)exception_object, personalityResult);
322 return _URC_FATAL_PHASE2_ERROR;
323 }
324 }
325 }
326
327 // Call stop function one last time and tell it we've reached the end
328 // of the stack.
329 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
330 "function with _UA_END_OF_STACK\n",
331 (void *)exception_object);
332 _Unwind_Action lastAction =
333 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
334 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
335 (struct _Unwind_Context *)(&cursor2), stop_parameter);
336
337 // Clean up phase did not resume at the frame that the search phase said it
338 // would.
339 return _URC_FATAL_PHASE2_ERROR;
340}
341
342
343/// Called by __cxa_throw. Only returns if there is a fatal error.
344_LIBUNWIND_EXPORT _Unwind_Reason_Code
345_Unwind_RaiseException(_Unwind_Exception *exception_object) {
346 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)\n",
347 (void *)exception_object);
348 unw_context_t uc;
349 unw_getcontext(&uc);
350
351 // Mark that this is a non-forced unwind, so _Unwind_Resume()
352 // can do the right thing.
353 exception_object->private_1 = 0;
354 exception_object->private_2 = 0;
355
356 // phase 1: the search phase
357 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, exception_object);
358 if (phase1 != _URC_NO_REASON)
359 return phase1;
360
361 // phase 2: the clean up phase
362 return unwind_phase2(&uc, exception_object);
363}
364
365
366
367/// When _Unwind_RaiseException() is in phase2, it hands control
368/// to the personality function at each frame. The personality
369/// may force a jump to a landing pad in that function, the landing
370/// pad code may then call _Unwind_Resume() to continue with the
371/// unwinding. Note: the call to _Unwind_Resume() is from compiler
372/// geneated user code. All other _Unwind_* routines are called
373/// by the C++ runtime __cxa_* routines.
374///
375/// Note: re-throwing an exception (as opposed to continuing the unwind)
376/// is implemented by having the code call __cxa_rethrow() which
377/// in turn calls _Unwind_Resume_or_Rethrow().
378_LIBUNWIND_EXPORT void
379_Unwind_Resume(_Unwind_Exception *exception_object) {
380 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)\n", (void *)exception_object);
381 unw_context_t uc;
382 unw_getcontext(&uc);
383
384 if (exception_object->private_1 != 0)
385 unwind_phase2_forced(&uc, exception_object,
386 (_Unwind_Stop_Fn) exception_object->private_1,
387 (void *)exception_object->private_2);
388 else
389 unwind_phase2(&uc, exception_object);
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) {
403 _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)\n",
404 (void *)exception_object, (void *)(uintptr_t)stop);
405 unw_context_t uc;
406 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
414 return unwind_phase2_forced(&uc, exception_object, stop, stop_parameter);
415}
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(
427 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR "\n",
428 (void *)context, result);
429 if (result != 0) {
430 if (*((uint8_t *)result) != 0xFF)
431 _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF\n",
432 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;
447 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR "\n",
448 (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) {
457 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)\n",
458 (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);
470 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIx64 "\n",
471 (void *)context, index, (uint64_t)result);
472 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) {
478 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIx64
479 ")\n",
480 (void *)context, index, (uint64_t)value);
481 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);
490 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIx64 "\n",
491 (void *)context, (uint64_t)result);
492 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) {
500 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIx64 ")\n",
501 (void *)context, (uint64_t)value);
502 unw_cursor_t *cursor = (unw_cursor_t *)context;
503 unw_set_reg(cursor, UNW_REG_IP, value);
504}
505
Logan Chien06b0c7a2015-07-19 15:23:10 +0000506#endif // !_LIBUNWIND_ARM_EHABI