1
/* libunwind - a platform-independent unwind library
2
Copyright (C) 2002-2003 Hewlett-Packard Co
3
Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5
This file is part of libunwind.
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:
15
The above copyright notice and this permission notice shall be
16
included in all copies or substantial portions of the Software.
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. */
30
unw_step (unw_cursor_t *cursor)
32
struct cursor *c = (struct cursor *) cursor;
35
Debug (1, "(cursor=%p)\n", c);
37
/* Try DWARF-based unwinding... */
38
ret = dwarf_step (&c->dwarf);
40
if (unlikely (ret == -UNW_ESTOPUNWIND))
43
if (unlikely (ret < 0))
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;
49
Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
51
if (unw_is_signal_frame (cursor))
53
/* XXX This code is Linux-specific! */
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
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;
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));
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)
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;
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
88
c->sigcontext_format = X86_SCF_LINUX_RT_SIGFRAME;
89
c->sigcontext_addr = sigcontext_ptr;
90
sigcontext_addr = sigcontext_ptr + LINUX_UC_MCONTEXT_OFF;
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);
101
ret = dwarf_get (&c->dwarf, c->dwarf.loc[EBP], &c->dwarf.cfa);
105
Debug (13, "[EBP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EBP]),
108
ebp_loc = DWARF_LOC (c->dwarf.cfa, 0);
109
eip_loc = DWARF_LOC (c->dwarf.cfa + 4, 0);
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;
120
if (!DWARF_IS_NULL_LOC (c->dwarf.loc[EBP]))
122
ret = dwarf_get (&c->dwarf, c->dwarf.loc[EIP], &c->dwarf.ip);
129
return (c->dwarf.ip == 0) ? 0 : 1;