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