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