blob: fdc0a6aa6a0e0c36630ba4bedd42ad2a93d106cf [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/* ARM EABI compliant unwinding routines.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Paul Brook
4
5 This file is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
9
10 In addition to the permissions in the GNU General Public License, the
11 Free Software Foundation gives you unlimited permission to link the
12 compiled version of this file into combinations with other programs,
13 and to distribute those combinations without any restriction coming
14 from the use of this file. (The General Public License restrictions
15 do apply in other respects; for example, they cover modification of
16 the file, and distribution when not linked into a combine
17 executable.)
18
19 This file is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301, USA. */
28
29/****************************************************************************
30 * The functions here are derived from gcc/config/arm/unwind-arm.c from the
31 * 4.3.x release. The main changes here involve the use of ptrace to retrieve
32 * memory/processor states from a remote process.
33 ****************************************************************************/
34
35#include <cutils/logd.h>
36#include <sys/ptrace.h>
37#include <unwind.h>
38#include "utility.h"
39
40typedef struct _ZSt9type_info type_info; /* This names C++ type_info type */
41
42void __attribute__((weak)) __cxa_call_unexpected(_Unwind_Control_Block *ucbp);
43bool __attribute__((weak)) __cxa_begin_cleanup(_Unwind_Control_Block *ucbp);
44bool __attribute__((weak)) __cxa_type_match(_Unwind_Control_Block *ucbp,
45 const type_info *rttip,
46 bool is_reference,
47 void **matched_object);
48
49/* Misc constants. */
50#define R_IP 12
51#define R_SP 13
52#define R_LR 14
53#define R_PC 15
54
55#define EXIDX_CANTUNWIND 1
56#define uint32_highbit (((_uw) 1) << 31)
57
58#define UCB_FORCED_STOP_FN(ucbp) ((ucbp)->unwinder_cache.reserved1)
59#define UCB_PR_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved2)
60#define UCB_SAVED_CALLSITE_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved3)
61#define UCB_FORCED_STOP_ARG(ucbp) ((ucbp)->unwinder_cache.reserved4)
62
63struct core_regs
64{
65 _uw r[16];
66};
67
68/* We use normal integer types here to avoid the compiler generating
69 coprocessor instructions. */
70struct vfp_regs
71{
72 _uw64 d[16];
73 _uw pad;
74};
75
76struct vfpv3_regs
77{
78 /* Always populated via VSTM, so no need for the "pad" field from
79 vfp_regs (which is used to store the format word for FSTMX). */
80 _uw64 d[16];
81};
82
83struct fpa_reg
84{
85 _uw w[3];
86};
87
88struct fpa_regs
89{
90 struct fpa_reg f[8];
91};
92
93struct wmmxd_regs
94{
95 _uw64 wd[16];
96};
97
98struct wmmxc_regs
99{
100 _uw wc[4];
101};
102
103/* Unwind descriptors. */
104
105typedef struct
106{
107 _uw16 length;
108 _uw16 offset;
109} EHT16;
110
111typedef struct
112{
113 _uw length;
114 _uw offset;
115} EHT32;
116
117/* The ABI specifies that the unwind routines may only use core registers,
118 except when actually manipulating coprocessor state. This allows
119 us to write one implementation that works on all platforms by
120 demand-saving coprocessor registers.
121
122 During unwinding we hold the coprocessor state in the actual hardware
123 registers and allocate demand-save areas for use during phase1
124 unwinding. */
125
126typedef struct
127{
128 /* The first fields must be the same as a phase2_vrs. */
129 _uw demand_save_flags;
130 struct core_regs core;
131 _uw prev_sp; /* Only valid during forced unwinding. */
132 struct vfp_regs vfp;
133 struct vfpv3_regs vfp_regs_16_to_31;
134 struct fpa_regs fpa;
135 struct wmmxd_regs wmmxd;
136 struct wmmxc_regs wmmxc;
137} phase1_vrs;
138
139/* This must match the structure created by the assembly wrappers. */
140typedef struct
141{
142 _uw demand_save_flags;
143 struct core_regs core;
144} phase2_vrs;
145
146
147/* An exception index table entry. */
148
149typedef struct __EIT_entry
150{
151 _uw fnoffset;
152 _uw content;
153} __EIT_entry;
154
155/* Derived version to use ptrace */
156typedef _Unwind_Reason_Code (*personality_routine_with_ptrace)
157 (_Unwind_State,
158 _Unwind_Control_Block *,
159 _Unwind_Context *,
160 pid_t);
161
162/* Derived version to use ptrace */
163/* ABI defined personality routines. */
164static _Unwind_Reason_Code unwind_cpp_pr0_with_ptrace (_Unwind_State,
165 _Unwind_Control_Block *, _Unwind_Context *, pid_t);
166static _Unwind_Reason_Code unwind_cpp_pr1_with_ptrace (_Unwind_State,
167 _Unwind_Control_Block *, _Unwind_Context *, pid_t);
168static _Unwind_Reason_Code unwind_cpp_pr2_with_ptrace (_Unwind_State,
169 _Unwind_Control_Block *, _Unwind_Context *, pid_t);
170
171/* Execute the unwinding instructions described by UWS. */
172extern _Unwind_Reason_Code
173unwind_execute_with_ptrace(_Unwind_Context * context, __gnu_unwind_state * uws,
174 pid_t pid);
175
176/* Derived version to use ptrace. Only handles core registers. Disregards
177 * FP and others.
178 */
179/* ABI defined function to pop registers off the stack. */
180
181_Unwind_VRS_Result unwind_VRS_Pop_with_ptrace (_Unwind_Context *context,
182 _Unwind_VRS_RegClass regclass,
183 _uw discriminator,
184 _Unwind_VRS_DataRepresentation representation,
185 pid_t pid)
186{
187 phase1_vrs *vrs = (phase1_vrs *) context;
188
189 switch (regclass)
190 {
191 case _UVRSC_CORE:
192 {
193 _uw *ptr;
194 _uw mask;
195 int i;
196
197 if (representation != _UVRSD_UINT32)
198 return _UVRSR_FAILED;
199
200 mask = discriminator & 0xffff;
201 ptr = (_uw *) vrs->core.r[R_SP];
202 /* Pop the requested registers. */
203 for (i = 0; i < 16; i++)
204 {
205 if (mask & (1 << i)) {
206 vrs->core.r[i] = get_remote_word(pid, ptr);
207 ptr++;
208 }
209 }
210 /* Writeback the stack pointer value if it wasn't restored. */
211 if ((mask & (1 << R_SP)) == 0)
212 vrs->core.r[R_SP] = (_uw) ptr;
213 }
214 return _UVRSR_OK;
215
216 default:
217 return _UVRSR_FAILED;
218 }
219}
220
221/* Core unwinding functions. */
222
223/* Calculate the address encoded by a 31-bit self-relative offset at address
224 P. */
225static inline _uw
226selfrel_offset31 (const _uw *p, pid_t pid)
227{
228 _uw offset = get_remote_word(pid, (void*)p);
229
230 //offset = *p;
231 /* Sign extend to 32 bits. */
232 if (offset & (1 << 30))
233 offset |= 1u << 31;
234 else
235 offset &= ~(1u << 31);
236
237 return offset + (_uw) p;
238}
239
240
241/* Perform a binary search for RETURN_ADDRESS in TABLE. The table contains
242 NREC entries. */
243
244static const __EIT_entry *
245search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address,
246 pid_t pid)
247{
248 _uw next_fn;
249 _uw this_fn;
250 int n, left, right;
251
252 if (nrec == 0)
253 return (__EIT_entry *) 0;
254
255 left = 0;
256 right = nrec - 1;
257
258 while (1)
259 {
260 n = (left + right) / 2;
261 this_fn = selfrel_offset31 (&table[n].fnoffset, pid);
262 if (n != nrec - 1)
263 next_fn = selfrel_offset31 (&table[n + 1].fnoffset, pid) - 1;
264 else
265 next_fn = (_uw)0 - 1;
266
267 if (return_address < this_fn)
268 {
269 if (n == left)
270 return (__EIT_entry *) 0;
271 right = n - 1;
272 }
273 else if (return_address <= next_fn)
274 return &table[n];
275 else
276 left = n + 1;
277 }
278}
279
280/* Find the exception index table eintry for the given address. */
281static const __EIT_entry*
282get_eitp(_uw return_address, pid_t pid, mapinfo *map, mapinfo **containing_map)
283{
284 const __EIT_entry *eitp = NULL;
285 int nrec;
286 mapinfo *mi;
287
288 /* The return address is the address of the instruction following the
289 call instruction (plus one in thumb mode). If this was the last
290 instruction in the function the address will lie in the following
291 function. Subtract 2 from the address so that it points within the call
292 instruction itself. */
293 if (return_address >= 2)
294 return_address -= 2;
295
296 for (mi = map; mi != NULL; mi = mi->next) {
297 if (return_address >= mi->start && return_address <= mi->end) break;
298 }
299
300 if (mi) {
301 if (containing_map) *containing_map = mi;
302 eitp = (__EIT_entry *) mi->exidx_start;
303 nrec = (mi->exidx_end - mi->exidx_start)/sizeof(__EIT_entry);
304 eitp = search_EIT_table (eitp, nrec, return_address, pid);
305 }
306 return eitp;
307}
308
309/* Find the exception index table eintry for the given address.
310 Fill in the relevant fields of the UCB.
311 Returns _URC_FAILURE if an error occurred, _URC_OK on success. */
312
313static _Unwind_Reason_Code
314get_eit_entry (_Unwind_Control_Block *ucbp, _uw return_address, pid_t pid,
315 mapinfo *map, mapinfo **containing_map)
316{
317 const __EIT_entry *eitp;
318
319 eitp = get_eitp(return_address, pid, map, containing_map);
320
321 if (!eitp)
322 {
323 UCB_PR_ADDR (ucbp) = 0;
324 return _URC_FAILURE;
325 }
326 ucbp->pr_cache.fnstart = selfrel_offset31 (&eitp->fnoffset, pid);
327
328 _uw eitp_content = get_remote_word(pid, (void *)&eitp->content);
329
330 /* Can this frame be unwound at all? */
331 if (eitp_content == EXIDX_CANTUNWIND)
332 {
333 UCB_PR_ADDR (ucbp) = 0;
334 return _URC_END_OF_STACK;
335 }
336
337 /* Obtain the address of the "real" __EHT_Header word. */
338
339 if (eitp_content & uint32_highbit)
340 {
341 /* It is immediate data. */
342 ucbp->pr_cache.ehtp = (_Unwind_EHT_Header *)&eitp->content;
343 ucbp->pr_cache.additional = 1;
344 }
345 else
346 {
347 /* The low 31 bits of the content field are a self-relative
348 offset to an _Unwind_EHT_Entry structure. */
349 ucbp->pr_cache.ehtp =
350 (_Unwind_EHT_Header *) selfrel_offset31 (&eitp->content, pid);
351 ucbp->pr_cache.additional = 0;
352 }
353
354 /* Discover the personality routine address. */
355 if (get_remote_word(pid, ucbp->pr_cache.ehtp) & (1u << 31))
356 {
357 /* One of the predefined standard routines. */
358 _uw idx = (get_remote_word(pid, ucbp->pr_cache.ehtp) >> 24) & 0xf;
359 if (idx == 0)
360 UCB_PR_ADDR (ucbp) = (_uw) &unwind_cpp_pr0_with_ptrace;
361 else if (idx == 1)
362 UCB_PR_ADDR (ucbp) = (_uw) &unwind_cpp_pr1_with_ptrace;
363 else if (idx == 2)
364 UCB_PR_ADDR (ucbp) = (_uw) &unwind_cpp_pr2_with_ptrace;
365 else
366 { /* Failed */
367 UCB_PR_ADDR (ucbp) = 0;
368 return _URC_FAILURE;
369 }
370 }
371 else
372 {
373 /* Execute region offset to PR */
374 UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp, pid);
375 /* Since we are unwinding the stack from a different process, it is
376 * impossible to execute the personality routine in debuggerd. Punt here.
377 */
378 return _URC_FAILURE;
379 }
380 return _URC_OK;
381}
382
383/* Print out the current call level, pc, and module name in the crash log */
384static _Unwind_Reason_Code log_function(_Unwind_Context *context, int tfd,
385 int stack_level,
386 mapinfo *map,
387 unsigned int sp_list[],
388 bool at_fault)
389{
390 _uw pc;
391 phase2_vrs *vrs = (phase2_vrs*) context;
392 const mapinfo *mi;
393 bool only_in_tombstone = !at_fault;
394
395 if (stack_level < STACK_CONTENT_DEPTH) {
396 sp_list[stack_level] = vrs->core.r[R_SP];
397 }
398 pc = vrs->core.r[R_PC];
399
400 // Top level frame
401 if (stack_level == 0) {
402 pc &= ~1;
403 }
404 // For deeper framers, rollback pc by one instruction
405 else {
406 pc = vrs->core.r[R_PC];
407 // Thumb mode
408 if (pc & 1) {
409 pc = (pc & ~1) - 2;
410 }
411 else {
412 pc -= 4;
413 }
414 }
415
416 mi = pc_to_mapinfo(map, pc);
417
418 _LOG(tfd, only_in_tombstone,
419 " #%02d pc %08x %s\n", stack_level, pc,
420 mi ? mi->name : "");
421
422 return _URC_NO_REASON;
423}
424
425/* Derived from __gnu_Unwind_Backtrace to use ptrace */
426/* Perform stack backtrace through unwind data. Return the level of stack it
427 * unwinds.
428 */
429int unwind_backtrace_with_ptrace(int tfd, pid_t pid, mapinfo *map,
430 unsigned int sp_list[], int *frame0_pc_sane,
431 bool at_fault)
432{
433 phase1_vrs saved_vrs;
434 _Unwind_Reason_Code code = _URC_OK;
435 struct pt_regs r;
436 int i;
437 int stack_level = 0;
438
439 _Unwind_Control_Block ucb;
440 _Unwind_Control_Block *ucbp = &ucb;
441
442 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) return 0;
443
444 for (i = 0; i < 16; i++) {
445 saved_vrs.core.r[i] = r.uregs[i];
446 /*
447 _LOG(tfd, "r[%d] = 0x%x\n", i, saved_vrs.core.r[i]);
448 */
449 }
450
451 /* Set demand-save flags. */
452 saved_vrs.demand_save_flags = ~(_uw) 0;
453
454 /*
455 * If the app crashes because of calling the weeds, we cannot pass the PC
456 * to the usual unwinding code as the EXIDX mapping will fail.
457 * Instead, we simply print out the 0 as the top frame, and resume the
458 * unwinding process with the value stored in LR.
459 */
460 if (get_eitp(saved_vrs.core.r[R_PC], pid, map, NULL) == NULL) {
461 *frame0_pc_sane = 0;
462 log_function ((_Unwind_Context *) &saved_vrs, tfd, stack_level,
463 map, sp_list, at_fault);
464 saved_vrs.core.r[R_PC] = saved_vrs.core.r[R_LR];
465 stack_level++;
466 }
467
468 do {
469 mapinfo *this_map = NULL;
470 /* Find the entry for this routine. */
471 if (get_eit_entry(ucbp, saved_vrs.core.r[R_PC], pid, map, &this_map)
472 != _URC_OK) {
473 /* Uncomment the code below to study why the unwinder failed */
474#if 0
475 /* Shed more debugging info for stack unwinder improvement */
476 if (this_map) {
477 _LOG(tfd, 1,
478 "Relative PC=%#x from %s not contained in EXIDX\n",
479 saved_vrs.core.r[R_PC] - this_map->start, this_map->name);
480 }
481 _LOG(tfd, 1, "PC=%#x SP=%#x\n",
482 saved_vrs.core.r[R_PC], saved_vrs.core.r[R_SP]);
483#endif
484 code = _URC_FAILURE;
485 break;
486 }
487
488 /* The dwarf unwinder assumes the context structure holds things
489 like the function and LSDA pointers. The ARM implementation
490 caches these in the exception header (UCB). To avoid
491 rewriting everything we make the virtual IP register point at
492 the UCB. */
493 _Unwind_SetGR((_Unwind_Context *)&saved_vrs, 12, (_Unwind_Ptr) ucbp);
494
495 /* Call log function. */
496 if (log_function ((_Unwind_Context *) &saved_vrs, tfd, stack_level,
497 map, sp_list, at_fault) != _URC_NO_REASON) {
498 code = _URC_FAILURE;
499 break;
500 }
501 stack_level++;
502
503 /* Call the pr to decide what to do. */
504 code = ((personality_routine_with_ptrace) UCB_PR_ADDR (ucbp))(
505 _US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND, ucbp,
506 (void *) &saved_vrs, pid);
507 /*
508 * In theory the unwinding process will stop when the end of stack is
509 * reached or there is no unwinding information for the code address.
510 * To add another level of guarantee that the unwinding process
511 * will terminate we will stop it when the STACK_CONTENT_DEPTH is reached.
512 */
513 } while (code != _URC_END_OF_STACK && code != _URC_FAILURE &&
514 stack_level < STACK_CONTENT_DEPTH);
515 return stack_level;
516}
517
518
519/* Derived version to use ptrace */
520/* Common implementation for ARM ABI defined personality routines.
521 ID is the index of the personality routine, other arguments are as defined
522 by __aeabi_unwind_cpp_pr{0,1,2}. */
523
524static _Unwind_Reason_Code
525unwind_pr_common_with_ptrace (_Unwind_State state,
526 _Unwind_Control_Block *ucbp,
527 _Unwind_Context *context,
528 int id,
529 pid_t pid)
530{
531 __gnu_unwind_state uws;
532 _uw *data;
533 int phase2_call_unexpected_after_unwind = 0;
534
535 state &= _US_ACTION_MASK;
536
537 data = (_uw *) ucbp->pr_cache.ehtp;
538 uws.data = get_remote_word(pid, data);
539 data++;
540 uws.next = data;
541 if (id == 0)
542 {
543 uws.data <<= 8;
544 uws.words_left = 0;
545 uws.bytes_left = 3;
546 }
547 else
548 {
549 uws.words_left = (uws.data >> 16) & 0xff;
550 uws.data <<= 16;
551 uws.bytes_left = 2;
552 data += uws.words_left;
553 }
554
555 /* Restore the saved pointer. */
556 if (state == _US_UNWIND_FRAME_RESUME)
557 data = (_uw *) ucbp->cleanup_cache.bitpattern[0];
558
559 if ((ucbp->pr_cache.additional & 1) == 0)
560 {
561 /* Process descriptors. */
562 while (get_remote_word(pid, data)) {
563 /**********************************************************************
564 * The original code here seems to deal with exceptions that are not
565 * applicable in our toolchain, thus there is no way to test it for now.
566 * Instead of leaving it here and causing potential instability in
567 * debuggerd, we'd better punt here and leave the stack unwound.
568 * In the future when we discover cases where the stack should be unwound
569 * further but is not, we can revisit the code here.
570 **********************************************************************/
571 return _URC_FAILURE;
572 }
573 /* Finished processing this descriptor. */
574 }
575
576 if (unwind_execute_with_ptrace (context, &uws, pid) != _URC_OK)
577 return _URC_FAILURE;
578
579 if (phase2_call_unexpected_after_unwind)
580 {
581 /* Enter __cxa_unexpected as if called from the call site. */
582 _Unwind_SetGR (context, R_LR, _Unwind_GetGR (context, R_PC));
583 _Unwind_SetGR (context, R_PC, (_uw) &__cxa_call_unexpected);
584 return _URC_INSTALL_CONTEXT;
585 }
586
587 return _URC_CONTINUE_UNWIND;
588}
589
590
591/* ABI defined personality routine entry points. */
592
593static _Unwind_Reason_Code
594unwind_cpp_pr0_with_ptrace (_Unwind_State state,
595 _Unwind_Control_Block *ucbp,
596 _Unwind_Context *context,
597 pid_t pid)
598{
599 return unwind_pr_common_with_ptrace (state, ucbp, context, 0, pid);
600}
601
602static _Unwind_Reason_Code
603unwind_cpp_pr1_with_ptrace (_Unwind_State state,
604 _Unwind_Control_Block *ucbp,
605 _Unwind_Context *context,
606 pid_t pid)
607{
608 return unwind_pr_common_with_ptrace (state, ucbp, context, 1, pid);
609}
610
611static _Unwind_Reason_Code
612unwind_cpp_pr2_with_ptrace (_Unwind_State state,
613 _Unwind_Control_Block *ucbp,
614 _Unwind_Context *context,
615 pid_t pid)
616{
617 return unwind_pr_common_with_ptrace (state, ucbp, context, 2, pid);
618}