blob: 266f89f4ec0e1afb19de54c05fda7d1f49ce657f [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;
33 int ret, i;
34
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
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000040 if (ret < 0 && ret != -UNW_ENOINFO)
41 {
42 Debug (2, "returning %d\n", ret);
43 return ret;
44 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000045
46 if (unlikely (ret < 0))
47 {
48 /* DWARF failed, let's see if we can follow the frame-chain
49 or skip over the signal trampoline. */
50 struct dwarf_loc ebp_loc, eip_loc;
51
Arun Sharmaff0ae702009-03-16 11:06:26 -070052 /* We could get here because of missing/bad unwind information.
53 Validate all addresses before dereferencing. */
54 c->validate = 1;
55
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000056 Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
57
58 if (unw_is_signal_frame (cursor))
59 {
60 /* XXX This code is Linux-specific! */
61
62 /* c->esp points at the arguments to the handler. Without
63 SA_SIGINFO, the arguments consist of a signal number
64 followed by a struct sigcontext. With SA_SIGINFO, the
65 arguments consist a signal number, a siginfo *, and a
66 ucontext *. */
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000067 unw_word_t sc_addr;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000068 unw_word_t siginfo_ptr_addr = c->dwarf.cfa + 4;
69 unw_word_t sigcontext_ptr_addr = c->dwarf.cfa + 8;
70 unw_word_t siginfo_ptr, sigcontext_ptr;
71 struct dwarf_loc esp_loc, siginfo_ptr_loc, sigcontext_ptr_loc;
72
73 siginfo_ptr_loc = DWARF_LOC (siginfo_ptr_addr, 0);
74 sigcontext_ptr_loc = DWARF_LOC (sigcontext_ptr_addr, 0);
75 ret = (dwarf_get (&c->dwarf, siginfo_ptr_loc, &siginfo_ptr)
76 | dwarf_get (&c->dwarf, sigcontext_ptr_loc, &sigcontext_ptr));
77 if (ret < 0)
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000078 {
79 Debug (2, "returning 0\n");
80 return 0;
81 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000082 if (siginfo_ptr < c->dwarf.cfa
83 || siginfo_ptr > c->dwarf.cfa + 256
84 || sigcontext_ptr < c->dwarf.cfa
85 || sigcontext_ptr > c->dwarf.cfa + 256)
86 {
87 /* Not plausible for SA_SIGINFO signal */
88 c->sigcontext_format = X86_SCF_LINUX_SIGFRAME;
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000089 c->sigcontext_addr = sc_addr = c->dwarf.cfa + 4;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000090 }
91 else
92 {
93 /* If SA_SIGINFO were not specified, we actually read
94 various segment pointers instead. We believe that at
95 least fs and _fsh are always zero for linux, so it is
96 not just unlikely, but impossible that we would end
97 up here. */
98 c->sigcontext_format = X86_SCF_LINUX_RT_SIGFRAME;
99 c->sigcontext_addr = sigcontext_ptr;
mostang.com!davidmfa0828a2005-05-03 09:13:17 +0000100 sc_addr = sigcontext_ptr + LINUX_UC_MCONTEXT_OFF;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000101 }
mostang.com!davidmfa0828a2005-05-03 09:13:17 +0000102 esp_loc = DWARF_LOC (sc_addr + LINUX_SC_ESP_OFF, 0);
103 ebp_loc = DWARF_LOC (sc_addr + LINUX_SC_EBP_OFF, 0);
104 eip_loc = DWARF_LOC (sc_addr + LINUX_SC_EIP_OFF, 0);
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000105 ret = dwarf_get (&c->dwarf, esp_loc, &c->dwarf.cfa);
106 if (ret < 0)
mostang.com!davidmfa0828a2005-05-03 09:13:17 +0000107 {
108 Debug (2, "returning 0\n");
109 return 0;
110 }
111
112 c->dwarf.loc[EAX] = DWARF_LOC (sc_addr + LINUX_SC_EAX_OFF, 0);
113 c->dwarf.loc[ECX] = DWARF_LOC (sc_addr + LINUX_SC_ECX_OFF, 0);
114 c->dwarf.loc[EDX] = DWARF_LOC (sc_addr + LINUX_SC_EDX_OFF, 0);
115 c->dwarf.loc[EBX] = DWARF_LOC (sc_addr + LINUX_SC_EBX_OFF, 0);
116 c->dwarf.loc[EBP] = DWARF_LOC (sc_addr + LINUX_SC_EBP_OFF, 0);
117 c->dwarf.loc[ESI] = DWARF_LOC (sc_addr + LINUX_SC_ESI_OFF, 0);
118 c->dwarf.loc[EDI] = DWARF_LOC (sc_addr + LINUX_SC_EDI_OFF, 0);
119 c->dwarf.loc[EFLAGS] = DWARF_NULL_LOC;
120 c->dwarf.loc[TRAPNO] = DWARF_NULL_LOC;
121 c->dwarf.loc[ST0] = DWARF_NULL_LOC;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000122 }
123 else
124 {
125 ret = dwarf_get (&c->dwarf, c->dwarf.loc[EBP], &c->dwarf.cfa);
126 if (ret < 0)
mostang.com!davidmfa0828a2005-05-03 09:13:17 +0000127 {
128 Debug (2, "returning %d\n", ret);
129 return ret;
130 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000131
homeip.net!davidm57426422004-08-19 12:26:11 +0000132 Debug (13, "[EBP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EBP]),
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000133 c->dwarf.cfa);
134
135 ebp_loc = DWARF_LOC (c->dwarf.cfa, 0);
136 eip_loc = DWARF_LOC (c->dwarf.cfa + 4, 0);
137 c->dwarf.cfa += 8;
mostang.com!davidmfa0828a2005-05-03 09:13:17 +0000138
139 /* Mark all registers unsaved, since we don't know where
140 they are saved (if at all), except for the EBP and
141 EIP. */
142 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
143 c->dwarf.loc[i] = DWARF_NULL_LOC;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000144 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000145 c->dwarf.loc[EBP] = ebp_loc;
146 c->dwarf.loc[EIP] = eip_loc;
147 c->dwarf.ret_addr_column = EIP;
148
149 if (!DWARF_IS_NULL_LOC (c->dwarf.loc[EBP]))
150 {
151 ret = dwarf_get (&c->dwarf, c->dwarf.loc[EIP], &c->dwarf.ip);
152 if (ret < 0)
mostang.com!davidmfa0828a2005-05-03 09:13:17 +0000153 {
154 Debug (2, "returning %d\n", ret);
155 return ret;
156 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000157 }
158 else
159 c->dwarf.ip = 0;
160 }
mostang.com!davidmfa0828a2005-05-03 09:13:17 +0000161 ret = (c->dwarf.ip == 0) ? 0 : 1;
162 Debug (2, "returning %d\n", ret);
163 return ret;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000164}