1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
//===--------------------------- libuwind.cpp -----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
// Implements C++ ABI Exception Handling Level 1 as documented at:
// http://mentorembedded.github.io/cxx-abi/abi-eh.html
//
//===----------------------------------------------------------------------===//
#define _UNWIND_GCC_EXTENSIONS
#include <unwind.h>
#include "UnwindCursor.hpp"
using namespace _Unwind;
typedef CFI_Parser<LocalAddressSpace, NativeUnwindRegisters> MyCFIParser;
// Internal object representing the address space of this process.
static LocalAddressSpace sThisAddressSpace(MyCFIParser::findPCRange);
typedef UnwindCursor<LocalAddressSpace, NativeUnwindRegisters> ThisUnwindCursor;
static _Unwind_Reason_Code unwind_phase1(ThisUnwindCursor &cursor,
struct _Unwind_Exception *exc) {
cursor.setInfoBasedOnIPRegister();
// Walk frames looking for a place to stop.
for (;;) {
// Get next frame.
// First frame is _Unwind_RaiseException and skipped.
switch (cursor.step()) {
case UNW_STEP_END:
return _URC_END_OF_STACK;
case UNW_STEP_FAILED:
return _URC_FATAL_PHASE1_ERROR;
case UNW_STEP_SUCCESS:
break;
}
// Check if there is a personality routine for this frame.
unw_proc_info_t frameInfo;
cursor.getInfo(&frameInfo);
if (frameInfo.end_ip == 0)
return _URC_FATAL_PHASE1_ERROR;
if (frameInfo.handler == 0)
continue; // No personality routine, so try next frame.
__personality_routine p = (__personality_routine)(frameInfo.handler);
_Unwind_Reason_Code result = (*p)(1, _UA_SEARCH_PHASE, exc->exception_class,
exc, (struct _Unwind_Context *)(&cursor));
switch (result) {
case _URC_HANDLER_FOUND:
// This is either a catch clause or a local variable
// with destructor.
// Stop search and remember the frame for phase 2.
exc->private_2 = cursor.getSP();
return _URC_NO_REASON;
case _URC_CONTINUE_UNWIND:
// Continue unwinding
break;
default:
// Bad personality routine.
return _URC_FATAL_PHASE1_ERROR;
}
}
}
static _Unwind_Reason_Code unwind_phase2(ThisUnwindCursor &cursor,
struct _Unwind_Exception *exc) {
cursor.setInfoBasedOnIPRegister();
// Walk frames until the frame selected in phase 1 is reached.
for (;;) {
// Get next frame.
// First frame is _Unwind_RaiseException and skipped.
switch (cursor.step()) {
case UNW_STEP_END:
return _URC_END_OF_STACK;
case UNW_STEP_FAILED:
return _URC_FATAL_PHASE2_ERROR;
case UNW_STEP_SUCCESS:
break;
}
unw_proc_info_t frameInfo;
cursor.getInfo(&frameInfo);
if (frameInfo.end_ip == 0)
return _URC_FATAL_PHASE2_ERROR;
if (frameInfo.handler == 0)
continue; // No personality routine, continue.
uintptr_t sp = cursor.getSP();
_Unwind_Action action = _UA_CLEANUP_PHASE;
// If this frame was selected in phase 1,
// inform the personality routine.
if (sp == exc->private_2)
action = (_Unwind_Action)(action | _UA_HANDLER_FRAME);
__personality_routine p = (__personality_routine)(frameInfo.handler);
_Unwind_Reason_Code result = (*p)(1, action, exc->exception_class, exc,
(struct _Unwind_Context *)(&cursor));
switch (result) {
case _URC_CONTINUE_UNWIND:
// Continue unwinding unless the selected frame passed.
if (sp == exc->private_2)
return _URC_FATAL_PHASE2_ERROR;
break;
case _URC_INSTALL_CONTEXT:
// Transfer control to landing pad.
cursor.jumpto();
default:
// Bad personality routine.
return _URC_FATAL_PHASE2_ERROR;
}
}
}
static _Unwind_Reason_Code unwind_phase2_forced(ThisUnwindCursor &cursor,
struct _Unwind_Exception *exc,
_Unwind_Stop_Fn stop,
void *stop_arg) {
_Unwind_Action action;
cursor.setInfoBasedOnIPRegister();
// Walk frames until the frame selected in phase 1 is reached.
for (;;) {
// Get next frame.
// First frame is _Unwind_RaiseException and skipped.
switch (cursor.step()) {
case UNW_STEP_END:
case UNW_STEP_FAILED:
// End of stack or error condition.
// Call the stop function one last time.
action = (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE |
_UA_END_OF_STACK);
(*stop)(1, action, exc->exception_class, exc,
(struct _Unwind_Context *)(&cursor), stop_arg);
// Didn't stop at the expected frame, so return error.
return _URC_FATAL_PHASE2_ERROR;
case UNW_STEP_SUCCESS:
break;
}
unw_proc_info_t frameInfo;
cursor.getInfo(&frameInfo);
if (frameInfo.end_ip == 0)
return _URC_FATAL_PHASE2_ERROR;
// Call stop function for each frame
action = (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
_Unwind_Reason_Code result =
(*stop)(1, action, exc->exception_class, exc,
(struct _Unwind_Context *)(&cursor), stop_arg);
if (result != _URC_NO_REASON)
return _URC_FATAL_PHASE2_ERROR;
if (frameInfo.handler == 0)
continue; // No personality routine, continue.
__personality_routine p = (__personality_routine)(frameInfo.handler);
result = (*p)(1, action, exc->exception_class, exc,
(struct _Unwind_Context *)(&cursor));
switch (result) {
case _URC_CONTINUE_UNWIND:
// Destructors called, continue.
break;
case _URC_INSTALL_CONTEXT:
// Transfer control to landing pad.
cursor.jumpto();
default:
// Bad personality routine.
return _URC_FATAL_PHASE2_ERROR;
}
}
}
_Unwind_Reason_Code _Unwind_RaiseException(struct _Unwind_Exception *exc) {
NativeUnwindRegisters registers;
ThisUnwindCursor cursor1(registers, sThisAddressSpace);
ThisUnwindCursor cursor2(registers, sThisAddressSpace);
// Mark this as a non-forced unwind for _Unwind_Resume().
exc->private_1 = 0;
exc->private_2 = 0;
// Phase 1: searching.
_Unwind_Reason_Code phase1 = unwind_phase1(cursor1, exc);
if (phase1 != _URC_NO_REASON)
return phase1;
// Phase 2: cleaning up.
return unwind_phase2(cursor2, exc);
}
_Unwind_Reason_Code _Unwind_ForcedUnwind(struct _Unwind_Exception *exc,
_Unwind_Stop_Fn stop, void *stop_arg) {
NativeUnwindRegisters registers;
ThisUnwindCursor cursor(registers, sThisAddressSpace);
// Mark this as forced unwind for _Unwind_Resume().
exc->private_1 = (uintptr_t)stop;
exc->private_2 = (uintptr_t)stop_arg;
return unwind_phase2_forced(cursor, exc, stop, stop_arg);
}
void _Unwind_Resume(struct _Unwind_Exception *exc) {
NativeUnwindRegisters registers;
ThisUnwindCursor cursor(registers, sThisAddressSpace);
if (exc->private_1 != 0)
unwind_phase2_forced(cursor, exc, (_Unwind_Stop_Fn)exc->private_1,
(void *)exc->private_2);
else
unwind_phase2(cursor, exc);
abort();
}
_Unwind_Reason_Code _Unwind_Resume_or_Rethrow(struct _Unwind_Exception *exc) {
// This is a re-throw, if this is a non-forced unwind
// and the stopping place was found.
// In that case, call _Unwind_RaiseException() as if
// it was a new exception.
if (exc->private_1 != 0)
_Unwind_Resume(exc);
// This can return if there is no catch clause.
// In that case, __cxa_rethrow is expected to call std::terminate().
return _Unwind_RaiseException(exc);
}
void _Unwind_DeleteException(struct _Unwind_Exception *exc) {
if (exc->exception_cleanup != NULL)
(*exc->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
}
uintptr_t _Unwind_GetGR(struct _Unwind_Context *context, int index) {
ThisUnwindCursor *cursor = (ThisUnwindCursor *)context;
return cursor->getReg(index);
}
void _Unwind_SetGR(struct _Unwind_Context *context, int index,
uintptr_t new_value) {
ThisUnwindCursor *cursor = (ThisUnwindCursor *)context;
cursor->setReg(index, new_value);
}
uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
ThisUnwindCursor *cursor = (ThisUnwindCursor *)context;
return cursor->getIP();
}
uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context, int *isSignalFrame) {
ThisUnwindCursor *cursor = (ThisUnwindCursor *)context;
*isSignalFrame = cursor->isSignalFrame() ? 1 : 0;
return cursor->getIP();
}
void _Unwind_SetIP(struct _Unwind_Context *context, uintptr_t new_value) {
ThisUnwindCursor *cursor = (ThisUnwindCursor *)context;
cursor->setIP(new_value);
unw_proc_info_t info;
cursor->getInfo(&info);
cursor->setInfoBasedOnIPRegister(false);
}
uintptr_t _Unwind_GetRegionStart(struct _Unwind_Context *context) {
ThisUnwindCursor *cursor = (ThisUnwindCursor *)context;
unw_proc_info_t frameInfo;
cursor->getInfo(&frameInfo);
return frameInfo.end_ip ? frameInfo.start_ip : 0;
}
uintptr_t _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
ThisUnwindCursor *cursor = (ThisUnwindCursor *)context;
unw_proc_info_t frameInfo;
cursor->getInfo(&frameInfo);
return frameInfo.end_ip ? frameInfo.lsda : 0;
}
_Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) {
NativeUnwindRegisters registers;
ThisUnwindCursor cursor(registers, sThisAddressSpace);
cursor.setInfoBasedOnIPRegister();
// Walk each frame.
while (true) {
// Ask libuwind to get next frame (skip over first frame which is
// _Unwind_Backtrace()).
if (cursor.step() != UNW_STEP_SUCCESS)
return _URC_END_OF_STACK;
// Call trace function with this frame.
_Unwind_Reason_Code result =
(*callback)((struct _Unwind_Context *)(&cursor), ref);
if (result != _URC_NO_REASON)
return result;
}
}
uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) {
ThisUnwindCursor *cursor = (ThisUnwindCursor *)context;
return cursor->getSP();
}
void *_Unwind_FindEnclosingFunction(void *pc) {
NativeUnwindRegisters registers;
ThisUnwindCursor cursor(registers, sThisAddressSpace);
unw_proc_info_t info;
cursor.setIP((uintptr_t)pc);
cursor.setInfoBasedOnIPRegister();
cursor.getInfo(&info);
return info.end_ip ? (void *)info.start_ip : NULL;
}
void *_Unwind_Find_FDE(void *pc, struct dwarf_eh_bases *bases) {
NativeUnwindRegisters registers;
ThisUnwindCursor cursor(registers, sThisAddressSpace);
unw_proc_info_t info;
cursor.setIP((uintptr_t)pc);
cursor.setInfoBasedOnIPRegister();
cursor.getInfo(&info);
if (info.end_ip == 0)
return NULL;
bases->tbase = 0; /* Not supported */
bases->dbase = (void *)info.data_base;
bases->func = (void *)info.start_ip;
return (void *)info.unwind_info;
}
uintptr_t _Unwind_GetDataRelBase(struct _Unwind_Context *context) {
ThisUnwindCursor *cursor = (ThisUnwindCursor *)context;
unw_proc_info_t frameInfo;
cursor->getInfo(&frameInfo);
return frameInfo.data_base;
}
uintptr_t _Unwind_GetTextRelBase(struct _Unwind_Context *context) { return 0; }
void __register_frame(const void *fde) {
MyCFIParser::pint_t pcStart, pcEnd;
MyCFIParser::findPCRange(sThisAddressSpace, (uintptr_t)fde, pcStart, pcEnd);
if (pcEnd == 0)
return; // Bad FDE.
sThisAddressSpace.addFDE(pcStart, pcEnd, (uintptr_t)fde);
}
void __register_frame_info(const void *ehframe, void *storage) {
sThisAddressSpace.setLazyReload();
}
void __deregister_frame(const void *fde) {
MyCFIParser::pint_t pcStart, pcEnd;
MyCFIParser::findPCRange(sThisAddressSpace, (uintptr_t)fde, pcStart, pcEnd);
if (pcEnd == 0)
return; // Bad FDE.
sThisAddressSpace.removeFDE(pcStart, pcEnd, (uintptr_t)fde);
}
void *__deregister_frame_info(const void *ehFrameStart) {
sThisAddressSpace.removeDSO((LocalAddressSpace::pint_t)ehFrameStart);
return NULL;
}
|