homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
mostang.com!davidm | fa0828a | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 2 | Copyright (C) 2002-2004 Hewlett-Packard Co |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 3 | Contributed by David Mosberger-Tang <davidm@hpl.hp.com> |
| 4 | |
| 5 | This file is part of libunwind. |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining |
| 8 | a copy of this software and associated documentation files (the |
| 9 | "Software"), to deal in the Software without restriction, including |
| 10 | without limitation the rights to use, copy, modify, merge, publish, |
| 11 | distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | permit persons to whom the Software is furnished to do so, subject to |
| 13 | the following conditions: |
| 14 | |
| 15 | The above copyright notice and this permission notice shall be |
| 16 | included in all copies or substantial portions of the Software. |
| 17 | |
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 22 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 24 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 25 | |
| 26 | #include "unwind_i.h" |
| 27 | #include "offsets.h" |
| 28 | |
| 29 | PROTECTED int |
| 30 | unw_step (unw_cursor_t *cursor) |
| 31 | { |
| 32 | struct cursor *c = (struct cursor *) cursor; |
Konstantin Belousov | aeee03d | 2010-04-05 16:28:46 +0300 | [diff] [blame] | 33 | int ret, i; |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 34 | |
mostang.com!davidm | fa0828a | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 35 | Debug (1, "(cursor=%p, ip=0x%08x)\n", c, (unsigned) c->dwarf.ip); |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 36 | |
Christopher Ferris | 9dee3d2 | 2014-01-06 16:46:04 -0800 | [diff] [blame] | 37 | /* ANDROID support update. */ |
Christopher Ferris | 2a5223f | 2014-03-12 12:45:55 -0700 | [diff] [blame] | 38 | /* Save the current ip/cfa to prevent looping if the decode yields |
| 39 | the same ip/cfa as before. */ |
| 40 | unw_word_t old_ip = c->dwarf.ip; |
| 41 | unw_word_t old_cfa = c->dwarf.cfa; |
Christopher Ferris | 9dee3d2 | 2014-01-06 16:46:04 -0800 | [diff] [blame] | 42 | /* End of ANDROID update. */ |
| 43 | |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 44 | /* Try DWARF-based unwinding... */ |
| 45 | ret = dwarf_step (&c->dwarf); |
| 46 | |
Christopher Ferris | 936e80b | 2016-03-07 15:55:09 -0800 | [diff] [blame] | 47 | #if !defined(UNW_LOCAL_ONLY) |
| 48 | /* Do not use this method on a local unwind. There is a very high |
| 49 | * probability this method will try to access unmapped memory, which |
| 50 | * will crash the process. Since this almost never actually works, |
| 51 | * it should be okay to skip. |
| 52 | */ |
Christopher Ferris | 1c82a52 | 2013-10-01 12:38:42 -0700 | [diff] [blame] | 53 | if (ret < 0) |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 54 | { |
| 55 | /* DWARF failed, let's see if we can follow the frame-chain |
| 56 | or skip over the signal trampoline. */ |
| 57 | struct dwarf_loc ebp_loc, eip_loc; |
| 58 | |
Arun Sharma | ff0ae70 | 2009-03-16 11:06:26 -0700 | [diff] [blame] | 59 | /* We could get here because of missing/bad unwind information. |
| 60 | Validate all addresses before dereferencing. */ |
| 61 | c->validate = 1; |
| 62 | |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 63 | Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret); |
| 64 | |
| 65 | if (unw_is_signal_frame (cursor)) |
Arun Sharma | 8e53e62 | 2010-04-04 16:17:32 -0700 | [diff] [blame] | 66 | { |
| 67 | ret = unw_handle_signal_frame(cursor); |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 68 | if (ret < 0) |
mostang.com!davidm | fa0828a | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 69 | { |
| 70 | Debug (2, "returning 0\n"); |
| 71 | return 0; |
| 72 | } |
Arun Sharma | 8e53e62 | 2010-04-04 16:17:32 -0700 | [diff] [blame] | 73 | } |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 74 | else |
| 75 | { |
| 76 | ret = dwarf_get (&c->dwarf, c->dwarf.loc[EBP], &c->dwarf.cfa); |
| 77 | if (ret < 0) |
mostang.com!davidm | fa0828a | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 78 | { |
| 79 | Debug (2, "returning %d\n", ret); |
| 80 | return ret; |
| 81 | } |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 82 | |
homeip.net!davidm | 5742642 | 2004-08-19 12:26:11 +0000 | [diff] [blame] | 83 | Debug (13, "[EBP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EBP]), |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 84 | c->dwarf.cfa); |
| 85 | |
| 86 | ebp_loc = DWARF_LOC (c->dwarf.cfa, 0); |
| 87 | eip_loc = DWARF_LOC (c->dwarf.cfa + 4, 0); |
| 88 | c->dwarf.cfa += 8; |
mostang.com!davidm | fa0828a | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 89 | |
| 90 | /* Mark all registers unsaved, since we don't know where |
| 91 | they are saved (if at all), except for the EBP and |
| 92 | EIP. */ |
| 93 | for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i) |
| 94 | c->dwarf.loc[i] = DWARF_NULL_LOC; |
Arun Sharma | 8e53e62 | 2010-04-04 16:17:32 -0700 | [diff] [blame] | 95 | |
| 96 | c->dwarf.loc[EBP] = ebp_loc; |
| 97 | c->dwarf.loc[EIP] = eip_loc; |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 98 | } |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 99 | c->dwarf.ret_addr_column = EIP; |
| 100 | |
| 101 | if (!DWARF_IS_NULL_LOC (c->dwarf.loc[EBP])) |
| 102 | { |
| 103 | ret = dwarf_get (&c->dwarf, c->dwarf.loc[EIP], &c->dwarf.ip); |
| 104 | if (ret < 0) |
mostang.com!davidm | fa0828a | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 105 | { |
Konstantin Belousov | f10f851 | 2010-04-11 14:59:36 +0300 | [diff] [blame] | 106 | Debug (13, "dwarf_get([EIP=0x%x]) failed\n", DWARF_GET_LOC (c->dwarf.loc[EIP])); |
mostang.com!davidm | fa0828a | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 107 | Debug (2, "returning %d\n", ret); |
| 108 | return ret; |
| 109 | } |
Konstantin Belousov | f10f851 | 2010-04-11 14:59:36 +0300 | [diff] [blame] | 110 | else |
| 111 | { |
| 112 | Debug (13, "[EIP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EIP]), |
| 113 | c->dwarf.ip); |
| 114 | } |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 115 | } |
| 116 | else |
| 117 | c->dwarf.ip = 0; |
Christopher Ferris | 1b47da7 | 2017-03-23 14:53:40 -0700 | [diff] [blame^] | 118 | |
| 119 | /* Adjust the return value because the functions above return zero for success. */ |
| 120 | if (ret == 0) |
| 121 | ret = 1; |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 122 | } |
Christopher Ferris | 936e80b | 2016-03-07 15:55:09 -0800 | [diff] [blame] | 123 | #endif |
Christopher Ferris | 1c82a52 | 2013-10-01 12:38:42 -0700 | [diff] [blame] | 124 | |
| 125 | /* ANDROID support update. */ |
| 126 | if (ret >= 0) |
| 127 | { |
| 128 | if (c->dwarf.ip) |
| 129 | { |
| 130 | /* Adjust the pc to the instruction before. */ |
| 131 | c->dwarf.ip--; |
| 132 | } |
Christopher Ferris | 2a5223f | 2014-03-12 12:45:55 -0700 | [diff] [blame] | 133 | /* If the decode yields the exact same ip/cfa as before, then indicate |
Christopher Ferris | 9dee3d2 | 2014-01-06 16:46:04 -0800 | [diff] [blame] | 134 | the unwind is complete. */ |
Christopher Ferris | 2a5223f | 2014-03-12 12:45:55 -0700 | [diff] [blame] | 135 | if (old_ip == c->dwarf.ip && old_cfa == c->dwarf.cfa) |
Christopher Ferris | 2706ca2 | 2015-05-08 16:58:52 -0700 | [diff] [blame] | 136 | { |
| 137 | Dprintf ("%s: ip and cfa unchanged; stopping here (ip=0x%lx)\n", |
| 138 | __FUNCTION__, (long) c->dwarf.ip); |
| 139 | return -UNW_EBADFRAME; |
| 140 | } |
Christopher Ferris | 1c82a52 | 2013-10-01 12:38:42 -0700 | [diff] [blame] | 141 | c->dwarf.frame++; |
| 142 | } |
| 143 | /* End of ANDROID update. */ |
Christopher Ferris | c60f22c | 2016-01-11 13:35:44 -0800 | [diff] [blame] | 144 | if (unlikely (ret <= 0)) |
| 145 | return 0; |
| 146 | |
| 147 | return (c->dwarf.ip == 0) ? 0 : 1; |
homeip.net!davidm | 3dab98e | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 148 | } |