2004-03-24 Andrew Cagney <cagney@redhat.com>
[external/binutils.git] / gdb / tramp-frame.c
1 /* Signal trampoline unwinder, for GDB the GNU Debugger.
2
3    Copyright 2004 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 2 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "tramp-frame.h"
24 #include "frame-unwind.h"
25 #include "gdbcore.h"
26 #include "symtab.h"
27 #include "objfiles.h"
28 #include "target.h"
29 #include "trad-frame.h"
30 #include "frame-base.h"
31 #include "gdb_assert.h"
32
33 struct frame_data
34 {
35   const struct tramp_frame *tramp_frame;
36 };
37
38 struct tramp_frame_cache
39 {
40   CORE_ADDR func;
41   const struct tramp_frame *tramp_frame;
42   struct trad_frame_cache *trad_cache;
43 };
44
45 static struct trad_frame_cache *
46 tramp_frame_cache (struct frame_info *next_frame,
47                    void **this_cache)
48 {
49   CORE_ADDR pc = frame_pc_unwind (next_frame);
50   struct tramp_frame_cache *tramp_cache = (*this_cache);
51   if (tramp_cache->trad_cache == NULL)
52     {
53       tramp_cache->trad_cache = trad_frame_cache_zalloc (next_frame);
54       tramp_cache->tramp_frame->init (tramp_cache->tramp_frame,
55                                       next_frame,
56                                       tramp_cache->trad_cache,
57                                       tramp_cache->func);
58     }
59   return tramp_cache->trad_cache;
60 }
61
62 static void
63 tramp_frame_this_id (struct frame_info *next_frame,
64                      void **this_cache,
65                      struct frame_id *this_id)
66 {
67   struct trad_frame_cache *trad_cache
68     = tramp_frame_cache (next_frame, this_cache);
69   trad_frame_get_id (trad_cache, this_id);
70 }
71
72 static void
73 tramp_frame_prev_register (struct frame_info *next_frame,
74                            void **this_cache,
75                            int prev_regnum,
76                            int *optimizedp,
77                            enum lval_type * lvalp,
78                            CORE_ADDR *addrp,
79                            int *realnump, void *valuep)
80 {
81   struct trad_frame_cache *trad_cache
82     = tramp_frame_cache (next_frame, this_cache);
83   trad_frame_get_register (trad_cache, next_frame, prev_regnum, optimizedp,
84                            lvalp, addrp, realnump, valuep);
85 }
86
87 static CORE_ADDR
88 tramp_frame_start (CORE_ADDR pc, const struct tramp_frame *tramp)
89 {
90   int ti;
91   /* Search through the trampoline for one that matches the
92      instruction sequence around PC.  */
93   for (ti = 0; tramp->insn[ti] != TRAMP_SENTINEL_INSN; ti++)
94     {
95       CORE_ADDR func = pc - tramp->insn_size * ti;
96       int i;
97       for (i = 0; 1; i++)
98         {
99           bfd_byte buf[sizeof (tramp->insn[0])];
100           ULONGEST insn;
101           if (tramp->insn[i] == TRAMP_SENTINEL_INSN)
102             return func;
103           if (target_read_memory (func + i * tramp->insn_size, buf,
104                                   tramp->insn_size) != 0)
105             break;
106           insn = extract_unsigned_integer (buf, tramp->insn_size);
107           if (tramp->insn[i] != insn)
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   char *name;
124   struct tramp_frame_cache *tramp_cache;
125
126   /* If the function has a valid symbol name, it isn't a
127      trampoline.  */
128   find_pc_partial_function (pc, &name, NULL, NULL);
129   if (name != NULL)
130     return 0;
131   /* If the function lives in a valid section (even without a starting
132      point) it isn't a trampoline.  */
133   if (find_pc_section (pc) != NULL)
134     return 0;
135   /* Finally, check that the trampoline matches at PC.  */
136   func = tramp_frame_start (pc, tramp);
137   if (func == 0)
138     return 0;
139   tramp_cache = FRAME_OBSTACK_ZALLOC (struct tramp_frame_cache);
140   tramp_cache->func = func;
141   tramp_cache->tramp_frame = tramp;
142   (*this_cache) = tramp_cache;
143   return 1;
144 }
145
146 void
147 tramp_frame_append (struct gdbarch *gdbarch,
148                     const struct tramp_frame *tramp_frame)
149 {
150   struct frame_data *data;
151   struct frame_unwind *unwinder;
152   int i;
153
154   /* Check that the instruction sequence contains a sentinel.  */
155   for (i = 0; i < ARRAY_SIZE (tramp_frame->insn); i++)
156     {
157       if (tramp_frame->insn[i] == TRAMP_SENTINEL_INSN)
158         break;
159     }
160   gdb_assert (i < ARRAY_SIZE (tramp_frame->insn));
161   gdb_assert (tramp_frame->insn_size <= sizeof (tramp_frame->insn[0]));
162
163   data = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_data);
164   unwinder = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind);
165
166   data->tramp_frame = tramp_frame;
167   unwinder->type = SIGTRAMP_FRAME;
168   unwinder->unwind_data = data;
169   unwinder->sniffer = tramp_frame_sniffer;
170   unwinder->this_id = tramp_frame_this_id;
171   unwinder->prev_register = tramp_frame_prev_register;
172   frame_unwind_register_unwinder (gdbarch, unwinder);
173 }