1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2003-2004 Hewlett-Packard Co
3	Contributed by David Mosberger
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
51      Debug (13, "dwarf_step() failed (ret=%d), trying fallback\n", ret);
52
53      if (unw_is_signal_frame (cursor))
54	{
55#ifdef __linux__
56	  /* Assume that the trampoline is at the beginning of the
57	     sigframe.  */
58	  unw_word_t ip, sc_addr = c->dwarf.ip + LINUX_RT_SIGFRAME_UC_OFF;
59	  dwarf_loc_t iaoq_loc = DWARF_LOC (sc_addr + LINUX_SC_IAOQ_OFF, 0);
60
61	  c->sigcontext_format = HPPA_SCF_LINUX_RT_SIGFRAME;
62	  c->sigcontext_addr = sc_addr;
63	  c->dwarf.ret_addr_column = UNW_HPPA_RP;
64
65	  if ((ret = dwarf_get (&c->dwarf, iaoq_loc, &ip)) < 0)
66	    {
67	      Debug (2, "failed to read IAOQ[1] (ret=%d)\n", ret);
68	      return ret;
69	    }
70	  c->dwarf.ip = ip & ~0x3;	/* mask out the privilege level */
71
72	  for (i = 0; i < 32; ++i)
73	    {
74	      c->dwarf.loc[UNW_HPPA_GR + i]
75		= DWARF_LOC (sc_addr + LINUX_SC_GR_OFF + 4*i, 0);
76	      c->dwarf.loc[UNW_HPPA_FR + i]
77		= DWARF_LOC (sc_addr + LINUX_SC_FR_OFF + 4*i, 0);
78	    }
79
80	  if ((ret = dwarf_get (&c->dwarf, c->dwarf.loc[UNW_HPPA_SP],
81				&c->dwarf.cfa)) < 0)
82	    {
83	      Debug (2, "failed to read SP (ret=%d)\n", ret);
84	      return ret;
85	    }
86#else
87# error Implement me!
88#endif
89	}
90      else
91	c->dwarf.ip = 0;
92    }
93  ret = (c->dwarf.ip == 0) ? 0 : 1;
94  Debug (2, "returning %d\n", ret);
95  return ret;
96}
97