blob: 7df64bf3dc3e3081c2af6e640e052355ef78bcee [file] [log] [blame]
homeip.net!davidm3dab98e2004-08-17 15:34:28 +00001/* libunwind - a platform-independent unwind library
mostang.com!davidmfa0828a2005-05-03 09:13:17 +00002 Copyright (C) 2002-2004 Hewlett-Packard Co
homeip.net!davidm3dab98e2004-08-17 15:34:28 +00003 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26#include "unwind_i.h"
27#include "offsets.h"
28
29PROTECTED int
30unw_step (unw_cursor_t *cursor)
31{
32 struct cursor *c = (struct cursor *) cursor;
Konstantin Belousovaeee03d2010-04-05 16:28:46 +030033 int ret, i;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000034
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000035 Debug (1, "(cursor=%p, ip=0x%08x)\n", c, (unsigned) c->dwarf.ip);
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000036
37 /* Try DWARF-based unwinding... */
38 ret = dwarf_step (&c->dwarf);
39
Christopher Ferris1c82a522013-10-01 12:38:42 -070040 if (ret < 0)
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000041 {
42 /* DWARF failed, let's see if we can follow the frame-chain
43 or skip over the signal trampoline. */
44 struct dwarf_loc ebp_loc, eip_loc;
45
Arun Sharmaff0ae702009-03-16 11:06:26 -070046 /* We could get here because of missing/bad unwind information.
47 Validate all addresses before dereferencing. */
48 c->validate = 1;
49
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000050 Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
51
52 if (unw_is_signal_frame (cursor))
Arun Sharma8e53e622010-04-04 16:17:32 -070053 {
54 ret = unw_handle_signal_frame(cursor);
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000055 if (ret < 0)
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000056 {
57 Debug (2, "returning 0\n");
58 return 0;
59 }
Arun Sharma8e53e622010-04-04 16:17:32 -070060 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000061 else
62 {
63 ret = dwarf_get (&c->dwarf, c->dwarf.loc[EBP], &c->dwarf.cfa);
64 if (ret < 0)
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000065 {
66 Debug (2, "returning %d\n", ret);
67 return ret;
68 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000069
homeip.net!davidm57426422004-08-19 12:26:11 +000070 Debug (13, "[EBP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EBP]),
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000071 c->dwarf.cfa);
72
73 ebp_loc = DWARF_LOC (c->dwarf.cfa, 0);
74 eip_loc = DWARF_LOC (c->dwarf.cfa + 4, 0);
75 c->dwarf.cfa += 8;
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000076
77 /* Mark all registers unsaved, since we don't know where
78 they are saved (if at all), except for the EBP and
79 EIP. */
80 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
81 c->dwarf.loc[i] = DWARF_NULL_LOC;
Arun Sharma8e53e622010-04-04 16:17:32 -070082
83 c->dwarf.loc[EBP] = ebp_loc;
84 c->dwarf.loc[EIP] = eip_loc;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000085 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000086 c->dwarf.ret_addr_column = EIP;
87
88 if (!DWARF_IS_NULL_LOC (c->dwarf.loc[EBP]))
89 {
90 ret = dwarf_get (&c->dwarf, c->dwarf.loc[EIP], &c->dwarf.ip);
91 if (ret < 0)
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000092 {
Konstantin Belousovf10f8512010-04-11 14:59:36 +030093 Debug (13, "dwarf_get([EIP=0x%x]) failed\n", DWARF_GET_LOC (c->dwarf.loc[EIP]));
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000094 Debug (2, "returning %d\n", ret);
95 return ret;
96 }
Konstantin Belousovf10f8512010-04-11 14:59:36 +030097 else
98 {
99 Debug (13, "[EIP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EIP]),
100 c->dwarf.ip);
101 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000102 }
103 else
104 c->dwarf.ip = 0;
105 }
Christopher Ferris1c82a522013-10-01 12:38:42 -0700106
107 /* ANDROID support update. */
108 if (ret >= 0)
109 {
110 if (c->dwarf.ip)
111 {
112 /* Adjust the pc to the instruction before. */
113 c->dwarf.ip--;
114 }
115 c->dwarf.frame++;
116 }
117 /* End of ANDROID update. */
mostang.com!davidmfa0828a2005-05-03 09:13:17 +0000118 ret = (c->dwarf.ip == 0) ? 0 : 1;
119 Debug (2, "returning %d\n", ret);
120 return ret;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000121}