Gstep.c revision fa0828ac7d77fd2c4ba3c94c42d66565f379512a
1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2002-2004 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
35  Debug (1, "(cursor=%p, ip=0x%08x)\n", c, (unsigned) c->dwarf.ip);
36
37  /* Try DWARF-based unwinding... */
38  ret = dwarf_step (&c->dwarf);
39
40  if (ret < 0 && ret != -UNW_ENOINFO)
41    {
42      Debug (2, "returning %d\n", ret);
43      return ret;
44    }
45
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
52      Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
53
54      if (unw_is_signal_frame (cursor))
55	{
56	  /* XXX This code is Linux-specific! */
57
58	  /* c->esp points at the arguments to the handler.  Without
59	     SA_SIGINFO, the arguments consist of a signal number
60	     followed by a struct sigcontext.  With SA_SIGINFO, the
61	     arguments consist a signal number, a siginfo *, and a
62	     ucontext *. */
63	  unw_word_t sc_addr;
64	  unw_word_t siginfo_ptr_addr = c->dwarf.cfa + 4;
65	  unw_word_t sigcontext_ptr_addr = c->dwarf.cfa + 8;
66	  unw_word_t siginfo_ptr, sigcontext_ptr;
67	  struct dwarf_loc esp_loc, siginfo_ptr_loc, sigcontext_ptr_loc;
68
69	  siginfo_ptr_loc = DWARF_LOC (siginfo_ptr_addr, 0);
70	  sigcontext_ptr_loc = DWARF_LOC (sigcontext_ptr_addr, 0);
71	  ret = (dwarf_get (&c->dwarf, siginfo_ptr_loc, &siginfo_ptr)
72		 | dwarf_get (&c->dwarf, sigcontext_ptr_loc, &sigcontext_ptr));
73	  if (ret < 0)
74	    {
75	      Debug (2, "returning 0\n");
76	      return 0;
77	    }
78	  if (siginfo_ptr < c->dwarf.cfa
79	      || siginfo_ptr > c->dwarf.cfa + 256
80	      || sigcontext_ptr < c->dwarf.cfa
81	      || sigcontext_ptr > c->dwarf.cfa + 256)
82	    {
83	      /* Not plausible for SA_SIGINFO signal */
84	      c->sigcontext_format = X86_SCF_LINUX_SIGFRAME;
85	      c->sigcontext_addr = sc_addr = c->dwarf.cfa + 4;
86	    }
87	  else
88	    {
89	      /* If SA_SIGINFO were not specified, we actually read
90		 various segment pointers instead.  We believe that at
91		 least fs and _fsh are always zero for linux, so it is
92		 not just unlikely, but impossible that we would end
93		 up here. */
94	      c->sigcontext_format = X86_SCF_LINUX_RT_SIGFRAME;
95	      c->sigcontext_addr = sigcontext_ptr;
96	      sc_addr = sigcontext_ptr + LINUX_UC_MCONTEXT_OFF;
97	    }
98	  esp_loc = DWARF_LOC (sc_addr + LINUX_SC_ESP_OFF, 0);
99	  ebp_loc = DWARF_LOC (sc_addr + LINUX_SC_EBP_OFF, 0);
100	  eip_loc = DWARF_LOC (sc_addr + LINUX_SC_EIP_OFF, 0);
101	  ret = dwarf_get (&c->dwarf, esp_loc, &c->dwarf.cfa);
102	  if (ret < 0)
103	    {
104	      Debug (2, "returning 0\n");
105	      return 0;
106	    }
107
108	  c->dwarf.loc[EAX] = DWARF_LOC (sc_addr + LINUX_SC_EAX_OFF, 0);
109	  c->dwarf.loc[ECX] = DWARF_LOC (sc_addr + LINUX_SC_ECX_OFF, 0);
110	  c->dwarf.loc[EDX] = DWARF_LOC (sc_addr + LINUX_SC_EDX_OFF, 0);
111	  c->dwarf.loc[EBX] = DWARF_LOC (sc_addr + LINUX_SC_EBX_OFF, 0);
112	  c->dwarf.loc[EBP] = DWARF_LOC (sc_addr + LINUX_SC_EBP_OFF, 0);
113	  c->dwarf.loc[ESI] = DWARF_LOC (sc_addr + LINUX_SC_ESI_OFF, 0);
114	  c->dwarf.loc[EDI] = DWARF_LOC (sc_addr + LINUX_SC_EDI_OFF, 0);
115	  c->dwarf.loc[EFLAGS] = DWARF_NULL_LOC;
116	  c->dwarf.loc[TRAPNO] = DWARF_NULL_LOC;
117	  c->dwarf.loc[ST0] = DWARF_NULL_LOC;
118	}
119      else
120	{
121	  ret = dwarf_get (&c->dwarf, c->dwarf.loc[EBP], &c->dwarf.cfa);
122	  if (ret < 0)
123	    {
124	      Debug (2, "returning %d\n", ret);
125	      return ret;
126	    }
127
128	  Debug (13, "[EBP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EBP]),
129		 c->dwarf.cfa);
130
131	  ebp_loc = DWARF_LOC (c->dwarf.cfa, 0);
132	  eip_loc = DWARF_LOC (c->dwarf.cfa + 4, 0);
133	  c->dwarf.cfa += 8;
134
135	  /* Mark all registers unsaved, since we don't know where
136	     they are saved (if at all), except for the EBP and
137	     EIP.  */
138	  for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
139	    c->dwarf.loc[i] = DWARF_NULL_LOC;
140	}
141      c->dwarf.loc[EBP] = ebp_loc;
142      c->dwarf.loc[EIP] = eip_loc;
143      c->dwarf.ret_addr_column = EIP;
144
145      if (!DWARF_IS_NULL_LOC (c->dwarf.loc[EBP]))
146	{
147	  ret = dwarf_get (&c->dwarf, c->dwarf.loc[EIP], &c->dwarf.ip);
148	  if (ret < 0)
149	    {
150	      Debug (2, "returning %d\n", ret);
151	      return ret;
152	    }
153	}
154      else
155	c->dwarf.ip = 0;
156    }
157  ret = (c->dwarf.ip == 0) ? 0 : 1;
158  Debug (2, "returning %d\n", ret);
159  return ret;
160}
161