blob: 1945c962c2cbe9dea145308ba33cbeca92e2a974 [file] [log] [blame]
homeip.net!davidm3dab98e2004-08-17 15:34:28 +00001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2002-2003 Hewlett-Packard Co
3 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
hp.com!davidmf576cce2004-08-26 10:02:46 +000035 Debug (1, "(cursor=%p)\n", c);
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000036
37 /* Try DWARF-based unwinding... */
38 ret = dwarf_step (&c->dwarf);
39
40 if (unlikely (ret == -UNW_ESTOPUNWIND))
41 return ret;
42
43 if (unlikely (ret < 0))
44 {
45 /* DWARF failed, let's see if we can follow the frame-chain
46 or skip over the signal trampoline. */
47 struct dwarf_loc ebp_loc, eip_loc;
48
49 Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
50
51 if (unw_is_signal_frame (cursor))
52 {
53 /* XXX This code is Linux-specific! */
54
55 /* c->esp points at the arguments to the handler. Without
56 SA_SIGINFO, the arguments consist of a signal number
57 followed by a struct sigcontext. With SA_SIGINFO, the
58 arguments consist a signal number, a siginfo *, and a
59 ucontext *. */
60 unw_word_t sigcontext_addr;
61 unw_word_t siginfo_ptr_addr = c->dwarf.cfa + 4;
62 unw_word_t sigcontext_ptr_addr = c->dwarf.cfa + 8;
63 unw_word_t siginfo_ptr, sigcontext_ptr;
64 struct dwarf_loc esp_loc, siginfo_ptr_loc, sigcontext_ptr_loc;
65
66 siginfo_ptr_loc = DWARF_LOC (siginfo_ptr_addr, 0);
67 sigcontext_ptr_loc = DWARF_LOC (sigcontext_ptr_addr, 0);
68 ret = (dwarf_get (&c->dwarf, siginfo_ptr_loc, &siginfo_ptr)
69 | dwarf_get (&c->dwarf, sigcontext_ptr_loc, &sigcontext_ptr));
70 if (ret < 0)
71 return 0;
72 if (siginfo_ptr < c->dwarf.cfa
73 || siginfo_ptr > c->dwarf.cfa + 256
74 || sigcontext_ptr < c->dwarf.cfa
75 || sigcontext_ptr > c->dwarf.cfa + 256)
76 {
77 /* Not plausible for SA_SIGINFO signal */
78 c->sigcontext_format = X86_SCF_LINUX_SIGFRAME;
79 c->sigcontext_addr = sigcontext_addr = c->dwarf.cfa + 4;
80 }
81 else
82 {
83 /* If SA_SIGINFO were not specified, we actually read
84 various segment pointers instead. We believe that at
85 least fs and _fsh are always zero for linux, so it is
86 not just unlikely, but impossible that we would end
87 up here. */
88 c->sigcontext_format = X86_SCF_LINUX_RT_SIGFRAME;
89 c->sigcontext_addr = sigcontext_ptr;
90 sigcontext_addr = sigcontext_ptr + LINUX_UC_MCONTEXT_OFF;
91 }
92 esp_loc = DWARF_LOC (sigcontext_addr + LINUX_SC_ESP_OFF, 0);
93 ebp_loc = DWARF_LOC (sigcontext_addr + LINUX_SC_EBP_OFF, 0);
94 eip_loc = DWARF_LOC (sigcontext_addr + LINUX_SC_EIP_OFF, 0);
95 ret = dwarf_get (&c->dwarf, esp_loc, &c->dwarf.cfa);
96 if (ret < 0)
97 return 0;
98 }
99 else
100 {
101 ret = dwarf_get (&c->dwarf, c->dwarf.loc[EBP], &c->dwarf.cfa);
102 if (ret < 0)
103 return ret;
104
homeip.net!davidm57426422004-08-19 12:26:11 +0000105 Debug (13, "[EBP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EBP]),
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000106 c->dwarf.cfa);
107
108 ebp_loc = DWARF_LOC (c->dwarf.cfa, 0);
109 eip_loc = DWARF_LOC (c->dwarf.cfa + 4, 0);
110 c->dwarf.cfa += 8;
111 }
112 /* Mark all registers unsaved, since we don't know where they
113 are saved (if at all), except for the EBP and EIP. */
114 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
115 c->dwarf.loc[i] = DWARF_NULL_LOC;
116 c->dwarf.loc[EBP] = ebp_loc;
117 c->dwarf.loc[EIP] = eip_loc;
118 c->dwarf.ret_addr_column = EIP;
119
120 if (!DWARF_IS_NULL_LOC (c->dwarf.loc[EBP]))
121 {
122 ret = dwarf_get (&c->dwarf, c->dwarf.loc[EIP], &c->dwarf.ip);
123 if (ret < 0)
124 return ret;
125 }
126 else
127 c->dwarf.ip = 0;
128 }
129 return (c->dwarf.ip == 0) ? 0 : 1;
130}