-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unwind.c
392 lines (305 loc) · 10.3 KB
/
Unwind.c
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
388
389
/* libunwind - a platform-independent unwind library
Copyright (C) 2003-2004 Hewlett-Packard Co
Contributed by David Mosberger-Tang <[email protected]>
This file is part of libunwind.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "unwind-internal.h"
PROTECTED _Unwind_Reason_Code
_Unwind_Backtrace (_Unwind_Trace_Fn trace, void *trace_parameter)
{
struct _Unwind_Context context;
unw_context_t uc;
int ret;
if (_Unwind_InitContext (&context, &uc) < 0)
return _URC_FATAL_PHASE1_ERROR;
/* Phase 1 (search phase) */
while (1)
{
if ((ret = unw_step (&context.cursor)) <= 0)
{
if (ret == 0)
return _URC_END_OF_STACK;
else
return _URC_FATAL_PHASE1_ERROR;
}
if ((*trace) (&context, trace_parameter) != _URC_NO_REASON)
return _URC_FATAL_PHASE1_ERROR;
}
}
_Unwind_Reason_Code __libunwind_Unwind_Backtrace (_Unwind_Trace_Fn, void *)
ALIAS (_Unwind_Backtrace);
PROTECTED void
_Unwind_DeleteException (struct _Unwind_Exception *exception_object)
{
_Unwind_Exception_Cleanup_Fn cleanup = exception_object->exception_cleanup;
if (cleanup)
(*cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exception_object);
}
void __libunwind_Unwind_DeleteException (struct _Unwind_Exception *)
ALIAS (_Unwind_DeleteException);
PROTECTED void *
_Unwind_FindEnclosingFunction (void *ip)
{
unw_proc_info_t pi;
if (unw_get_proc_info_by_ip (unw_local_addr_space,
(unw_word_t) (uintptr_t) ip, &pi, 0)
< 0)
return NULL;
return (void *) (uintptr_t) pi.start_ip;
}
void *__libunwind_Unwind_FindEnclosingFunction (void *)
ALIAS (_Unwind_FindEnclosingFunction);
PROTECTED _Unwind_Reason_Code
_Unwind_ForcedUnwind (struct _Unwind_Exception *exception_object,
_Unwind_Stop_Fn stop, void *stop_parameter)
{
struct _Unwind_Context context;
unw_context_t uc;
/* We check "stop" here to tell the compiler's inliner that
exception_object->private_1 isn't NULL when calling
_Unwind_Phase2(). */
if (!stop)
return _URC_FATAL_PHASE2_ERROR;
if (_Unwind_InitContext (&context, &uc) < 0)
return _URC_FATAL_PHASE2_ERROR;
exception_object->private_1 = (unsigned long) stop;
exception_object->private_2 = (unsigned long) stop_parameter;
return _Unwind_Phase2 (exception_object, &context);
}
_Unwind_Reason_Code __libunwind_Unwind_ForcedUnwind (struct _Unwind_Exception*,
_Unwind_Stop_Fn, void *)
ALIAS (_Unwind_ForcedUnwind);
PROTECTED unsigned long
_Unwind_GetBSP (struct _Unwind_Context *context)
{
#ifdef UNW_TARGET_IA64
unw_word_t val;
unw_get_reg (&context->cursor, UNW_IA64_BSP, &val);
return val;
#else
return 0;
#endif
}
unsigned long __libunwind_Unwind_GetBSP (struct _Unwind_Context *)
ALIAS (_Unwind_GetBSP);
PROTECTED unsigned long
_Unwind_GetCFA (struct _Unwind_Context *context)
{
unw_word_t val;
unw_get_reg (&context->cursor, UNW_REG_SP, &val);
return val;
}
unsigned long __libunwind_Unwind_GetCFA (struct _Unwind_Context *)
ALIAS (_Unwind_GetCFA);
PROTECTED unsigned long
_Unwind_GetDataRelBase (struct _Unwind_Context *context)
{
unw_proc_info_t pi;
pi.gp = 0;
unw_get_proc_info (&context->cursor, &pi);
return pi.gp;
}
unsigned long __libunwind_Unwind_GetDataRelBase (struct _Unwind_Context *)
ALIAS (_Unwind_GetDataRelBase);
PROTECTED unsigned long
_Unwind_GetGR (struct _Unwind_Context *context, int index)
{
unw_word_t val;
if (index == UNW_REG_SP && context->end_of_stack)
/* _Unwind_ForcedUnwind() requires us to return a NULL
stack-pointer after reaching the end of the stack. */
return 0;
unw_get_reg (&context->cursor, index, &val);
return val;
}
unsigned long __libunwind_Unwind_GetGR (struct _Unwind_Context *, int)
ALIAS (_Unwind_GetGR);
PROTECTED unsigned long
_Unwind_GetIP (struct _Unwind_Context *context)
{
unw_word_t val;
unw_get_reg (&context->cursor, UNW_REG_IP, &val);
return val;
}
unsigned long __libunwind_Unwind_GetIP (struct _Unwind_Context *)
ALIAS (_Unwind_GetIP);
PROTECTED unsigned long
_Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
{
unw_word_t val;
unw_get_reg (&context->cursor, UNW_REG_IP, &val);
*ip_before_insn = unw_is_signal_frame (&context->cursor);
return val;
}
unsigned long __libunwind_Unwind_GetIPInfo (struct _Unwind_Context *, int *)
ALIAS (_Unwind_GetIPInfo);
PROTECTED unsigned long
_Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
{
unw_proc_info_t pi;
pi.lsda = 0;
unw_get_proc_info (&context->cursor, &pi);
return pi.lsda;
}
unsigned long
__libunwind_Unwind_GetLanguageSpecificData (struct _Unwind_Context *)
ALIAS (_Unwind_GetLanguageSpecificData);
PROTECTED unsigned long
_Unwind_GetRegionStart (struct _Unwind_Context *context)
{
unw_proc_info_t pi;
pi.start_ip = 0;
unw_get_proc_info (&context->cursor, &pi);
return pi.start_ip;
}
unsigned long __libunwind_Unwind_GetRegionStart (struct _Unwind_Context *)
ALIAS (_Unwind_GetRegionStart);
PROTECTED unsigned long
_Unwind_GetTextRelBase (struct _Unwind_Context *context)
{
return 0;
}
unsigned long __libunwind_Unwind_GetTextRelBase (struct _Unwind_Context *)
ALIAS (_Unwind_GetTextRelBase);
_Unwind_Personality_Fn __libunwind_default_personality;
PROTECTED _Unwind_Reason_Code
_Unwind_RaiseException (struct _Unwind_Exception *exception_object)
{
uint64_t exception_class = exception_object->exception_class;
_Unwind_Personality_Fn personality;
struct _Unwind_Context context;
_Unwind_Reason_Code reason;
unw_proc_info_t pi;
unw_context_t uc;
unw_word_t ip;
int ret;
void *rbp = NULL;
void *rsp = NULL;
__asm__("\t movq %%rbp, %0" : "=r"(rbp));
__asm__("\t movq %%rsp, %0" : "=r"(rsp));
Debug (-1, "rsp %p rbp %p\n", rsp, rbp);
Debug (1, "(exception_object=%p)\n", exception_object);
if (_Unwind_InitContext (&context, &uc) < 0)
return _URC_FATAL_PHASE1_ERROR;
/* Phase 1 (search phase) */
while (1)
{
if ((ret = unw_step (&context.cursor)) <= 0)
{
if (ret == 0)
{
Debug (1, "no handler found\n");
return _URC_END_OF_STACK;
}
else
return _URC_FATAL_PHASE1_ERROR;
}
if (unw_get_proc_info (&context.cursor, &pi) < 0)
return _URC_FATAL_PHASE1_ERROR;
personality = (_Unwind_Personality_Fn) (uintptr_t) pi.handler;
if (personality != NULL){
reason = (*personality) (_U_VERSION, _UA_SEARCH_PHASE,
exception_class, exception_object,
&context);
if (reason != _URC_CONTINUE_UNWIND)
{
if (reason == _URC_HANDLER_FOUND)
break;
else
{
Debug (1, "personality returned %d\n", reason);
return _URC_FATAL_PHASE1_ERROR;
}
}
}else{
Debug(1, "no personality!\n");
}
}
/* Exceptions are associated with IP-ranges. If a given exception
is handled at a particular IP, it will _always_ be handled at
that IP. If this weren't true, we'd have to track the tuple
(IP,SP,BSP) to uniquely identify the stack frame that's handling
the exception. */
if (unw_get_reg (&context.cursor, UNW_REG_IP, &ip) < 0)
return _URC_FATAL_PHASE1_ERROR;
exception_object->private_1 = 0; /* clear "stop" pointer */
exception_object->private_2 = ip; /* save frame marker */
Debug (-1, "found handler for IP=%lx; entering cleanup phase\n", (long) ip);
/* Reset the cursor to the first frame: */
if (unw_init_local (&context.cursor, &uc) < 0)
return _URC_FATAL_PHASE1_ERROR;
return _Unwind_Phase2 (exception_object, &context);
}
_Unwind_Reason_Code
__libunwind_Unwind_RaiseException (struct _Unwind_Exception *)
ALIAS (_Unwind_RaiseException);
PROTECTED _Unwind_Reason_Code
_Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exception_object)
{
struct _Unwind_Context context;
unw_context_t uc;
if (exception_object->private_1)
{
if (_Unwind_InitContext (&context, &uc) < 0)
return _URC_FATAL_PHASE2_ERROR;
return _Unwind_Phase2 (exception_object, &context);
}
else
return _Unwind_RaiseException (exception_object);
}
_Unwind_Reason_Code
__libunwind_Unwind_Resume_or_Rethrow (struct _Unwind_Exception *)
ALIAS (_Unwind_Resume_or_Rethrow);
PROTECTED void
_Unwind_Resume (struct _Unwind_Exception *exception_object)
{
struct _Unwind_Context context;
unw_context_t uc;
if (_Unwind_InitContext (&context, &uc) < 0)
abort ();
_Unwind_Phase2 (exception_object, &context);
abort ();
}
void __libunwind_Unwind_Resume (struct _Unwind_Exception *)
ALIAS (_Unwind_Resume);
PROTECTED void
_Unwind_SetIP (struct _Unwind_Context *context, unsigned long new_value)
{
unw_set_reg (&context->cursor, UNW_REG_IP, new_value);
}
void __libunwind_Unwind_SetIP (struct _Unwind_Context *, unsigned long)
ALIAS (_Unwind_SetIP);
#ifdef UNW_TARGET_X86
#include "include/dwarf_i.h"
#endif
PROTECTED void
_Unwind_SetGR (struct _Unwind_Context *context, int index,
unsigned long new_value)
{
#ifdef UNW_TARGET_X86
index = dwarf_to_unw_regnum(index);
#endif
unw_set_reg (&context->cursor, index, new_value);
#ifdef UNW_TARGET_IA64
if (index >= UNW_IA64_GR && index <= UNW_IA64_GR + 127)
/* Clear the NaT bit. */
unw_set_reg (&context->cursor, UNW_IA64_NAT + (index - UNW_IA64_GR), 0);
#endif
}
void __libunwind_Unwind_SetGR (struct _Unwind_Context *, int, unsigned long)
ALIAS (_Unwind_SetGR);