~ubuntu-branches/ubuntu/utopic/libunwind/utopic-proposed

« back to all changes in this revision

Viewing changes to src/x86/Gstep.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthieu Delahaye
  • Date: 2005-04-22 11:12:14 UTC
  • mto: (4.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050422111214-0m74olipxly1ra8a
Tags: upstream-0.98.5
ImportĀ upstreamĀ versionĀ 0.98.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
4
 
 
5
This file is part of libunwind.
 
6
 
 
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:
 
14
 
 
15
The above copyright notice and this permission notice shall be
 
16
included in all copies or substantial portions of the Software.
 
17
 
 
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.  */
 
25
 
 
26
#include "unwind_i.h"
 
27
#include "offsets.h"
 
28
 
 
29
PROTECTED int
 
30
unw_step (unw_cursor_t *cursor)
 
31
{
 
32
  struct cursor *c = (struct cursor *) cursor;
 
33
  int ret, i;
 
34
 
 
35
  Debug (1, "(cursor=%p)\n", c);
 
36
 
 
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
 
 
105
          Debug (13, "[EBP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EBP]),
 
106
                 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
}