Import gdb-7.0
[dragonfly.git] / contrib / gdb-6 / gdb / tramp-frame.c
1 /* Signal trampoline unwinder, for GDB the GNU Debugger.
2
3    Copyright (C) 2004, 2007 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "tramp-frame.h"
22 #include "frame-unwind.h"
23 #include "gdbcore.h"
24 #include "symtab.h"
25 #include "objfiles.h"
26 #include "target.h"
27 #include "trad-frame.h"
28 #include "frame-base.h"
29 #include "gdb_assert.h"
30
31 struct frame_data
32 {
33   const struct tramp_frame *tramp_frame;
34 };
35
36 struct tramp_frame_cache
37 {
38   CORE_ADDR func;
39   const struct tramp_frame *tramp_frame;
40   struct trad_frame_cache *trad_cache;
41 };
42
43 static struct trad_frame_cache *
44 tramp_frame_cache (struct frame_info *next_frame,
45                    void **this_cache)
46 {
47   CORE_ADDR pc = frame_pc_unwind (next_frame);
48   struct tramp_frame_cache *tramp_cache = (*this_cache);
49   if (tramp_cache->trad_cache == NULL)
50     {
51       tramp_cache->trad_cache = trad_frame_cache_zalloc (next_frame);
52       tramp_cache->tramp_frame->init (tramp_cache->tramp_frame,
53                                       next_frame,
54                                       tramp_cache->trad_cache,
55                                       tramp_cache->func);
56     }
57   return tramp_cache->trad_cache;
58 }
59
60 static void
61 tramp_frame_this_id (struct frame_info *next_frame,
62                      void **this_cache,
63                      struct frame_id *this_id)
64 {
65   struct trad_frame_cache *trad_cache
66     = tramp_frame_cache (next_frame, this_cache);
67   trad_frame_get_id (trad_cache, this_id);
68 }
69
70 static void
71 tramp_frame_prev_register (struct frame_info *next_frame,
72                            void **this_cache,
73                            int prev_regnum,
74                            int *optimizedp,
75                            enum lval_type * lvalp,
76                            CORE_ADDR *addrp,
77                            int *realnump, gdb_byte *valuep)
78 {
79   struct trad_frame_cache *trad_cache
80     = tramp_frame_cache (next_frame, this_cache);
81   trad_frame_get_register (trad_cache, next_frame, prev_regnum, optimizedp,
82                            lvalp, addrp, realnump, valuep);
83 }
84
85 static CORE_ADDR
86 tramp_frame_start (const struct tramp_frame *tramp,
87                    struct frame_info *next_frame, CORE_ADDR pc)
88 {
89   int ti;
90   /* Search through the trampoline for one that matches the
91      instruction sequence around PC.  */
92   for (ti = 0; tramp->insn[ti].bytes != TRAMP_SENTINEL_INSN; ti++)
93     {
94       CORE_ADDR func = pc - tramp->insn_size * ti;
95       int i;
96       for (i = 0; 1; i++)
97         {
98           gdb_byte buf[sizeof (tramp->insn[0])];
99           ULONGEST insn;
100           if (tramp->insn[i].bytes == TRAMP_SENTINEL_INSN)
101             return func;
102           if (!safe_frame_unwind_memory (next_frame,
103                                          func + i * tramp->insn_size,
104                                          buf, tramp->insn_size))
105             break;
106           insn = extract_unsigned_integer (buf, tramp->insn_size);
107           if (tramp->insn[i].bytes != (insn & tramp->insn[i].mask))
108             break;
109         }
110     }
111   /* Trampoline doesn't match.  */
112   return 0;
113 }
114
115 static int
116 tramp_frame_sniffer (const struct frame_unwind *self,
117                      struct frame_info *next_frame,
118                      void **this_cache)
119 {
120   const struct tramp_frame *tramp = self->unwind_data->tramp_frame;
121   CORE_ADDR pc = frame_pc_unwind (next_frame);
122   CORE_ADDR func;
123   struct tramp_frame_cache *tramp_cache;
124
125   /* tausq/2004-12-12: We used to assume if pc has a name or is in a valid 
126      section, then this is not a trampoline.  However, this assumption is
127      false on HPUX which has a signal trampoline that has a name; it can
128      also be false when using an alternative signal stack.  */
129   func = tramp_frame_start (tramp, next_frame, pc);
130   if (func == 0)
131     return 0;
132   tramp_cache = FRAME_OBSTACK_ZALLOC (struct tramp_frame_cache);
133   tramp_cache->func = func;
134   tramp_cache->tramp_frame = tramp;
135   (*this_cache) = tramp_cache;
136   return 1;
137 }
138
139 void
140 tramp_frame_prepend_unwinder (struct gdbarch *gdbarch,
141                               const struct tramp_frame *tramp_frame)
142 {
143   struct frame_data *data;
144   struct frame_unwind *unwinder;
145   int i;
146
147   /* Check that the instruction sequence contains a sentinel.  */
148   for (i = 0; i < ARRAY_SIZE (tramp_frame->insn); i++)
149     {
150       if (tramp_frame->insn[i].bytes == TRAMP_SENTINEL_INSN)
151         break;
152     }
153   gdb_assert (i < ARRAY_SIZE (tramp_frame->insn));
154   gdb_assert (tramp_frame->insn_size <= sizeof (tramp_frame->insn[0].bytes));
155
156   data = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_data);
157   unwinder = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind);
158
159   data->tramp_frame = tramp_frame;
160   unwinder->type = tramp_frame->frame_type;
161   unwinder->unwind_data = data;
162   unwinder->sniffer = tramp_frame_sniffer;
163   unwinder->this_id = tramp_frame_this_id;
164   unwinder->prev_register = tramp_frame_prev_register;
165   frame_unwind_prepend_unwinder (gdbarch, unwinder);
166 }