2004-03-23 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
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 *next_frame,
46                    void **this_cache)
47 {
48   CORE_ADDR pc = frame_pc_unwind (next_frame);
49   struct tramp_frame_cache *tramp_cache = (*this_cache);
50   if (tramp_cache->trad_cache == NULL)
51     {
52       tramp_cache->trad_cache = trad_frame_cache_zalloc (next_frame);
53       tramp_cache->tramp_frame->init (tramp_cache->tramp_frame,
54                                       next_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 *next_frame,
63                      void **this_cache,
64                      struct frame_id *this_id)
65 {
66   struct trad_frame_cache *trad_cache
67     = tramp_frame_cache (next_frame, this_cache);
68   trad_frame_get_id (trad_cache, this_id);
69 }
70
71 static void
72 tramp_frame_prev_register (struct frame_info *next_frame,
73                            void **this_cache,
74                            int prev_regnum,
75                            int *optimizedp,
76                            enum lval_type * lvalp,
77                            CORE_ADDR *addrp,
78                            int *realnump, void *valuep)
79 {
80   struct trad_frame_cache *trad_cache
81     = tramp_frame_cache (next_frame, this_cache);
82   trad_frame_get_register (trad_cache, next_frame, prev_regnum, optimizedp,
83                            lvalp, addrp, realnump, valuep);
84 }
85
86 static CORE_ADDR
87 tramp_frame_start (CORE_ADDR pc, const struct tramp_frame *tramp)
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] != 0; ti++)
93     {
94       CORE_ADDR func = pc - tramp->insn_size * ti;
95       int i;
96       for (i = 0; 1; i++)
97         {
98           bfd_byte buf[sizeof (LONGEST)];
99           CORE_ADDR insn;
100           if (tramp->insn[i] == 0)
101             return func;
102           if (target_read_memory (func + i * tramp->insn_size, buf,
103                                   tramp->insn_size) != 0)
104             break;
105           insn = extract_unsigned_integer (buf, tramp->insn_size);
106           if (tramp->insn[i] != insn)
107             break;
108         }
109     }
110   /* Trampoline doesn't match.  */
111   return 0;
112 }
113
114 static int
115 tramp_frame_sniffer (const struct frame_unwind *self,
116                      struct frame_info *next_frame,
117                      void **this_cache)
118 {
119   const struct tramp_frame *tramp = self->unwind_data->tramp_frame;
120   CORE_ADDR pc = frame_pc_unwind (next_frame);
121   CORE_ADDR func;
122   char *name;
123   struct tramp_frame_cache *tramp_cache;
124
125   /* If the function has a valid symbol name, it isn't a
126      trampoline.  */
127   find_pc_partial_function (pc, &name, NULL, NULL);
128   if (name != NULL)
129     return 0;
130   /* If the function lives in a valid section (even without a starting
131      point) it isn't a trampoline.  */
132   if (find_pc_section (pc) != NULL)
133     return 0;
134   /* Finally, check that the trampoline matches at PC.  */
135   func = tramp_frame_start (pc, tramp);
136   if (func == 0)
137     return 0;
138   tramp_cache = FRAME_OBSTACK_ZALLOC (struct tramp_frame_cache);
139   tramp_cache->func = func;
140   tramp_cache->tramp_frame = tramp;
141   (*this_cache) = tramp_cache;
142   return 1;
143 }
144
145 void
146 tramp_frame_append (struct gdbarch *gdbarch,
147                     const struct tramp_frame *tramp_frame)
148 {
149   struct frame_data *data;
150   struct frame_unwind *unwinder;
151
152   data = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_data);
153   unwinder = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind);
154
155   data->tramp_frame = tramp_frame;
156   unwinder->type = SIGTRAMP_FRAME;
157   unwinder->unwind_data = data;
158   unwinder->sniffer = tramp_frame_sniffer;
159   unwinder->this_id = tramp_frame_this_id;
160   unwinder->prev_register = tramp_frame_prev_register;
161   frame_unwind_register_unwinder (gdbarch, unwinder);
162 }