Index: ChangeLog
[platform/upstream/binutils.git] / gdb / dummy-frame.c
1 /* Code dealing with dummy stack frames, for GDB, the GNU debugger.
2
3    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free
5    Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24
25 #include "defs.h"
26 #include "dummy-frame.h"
27 #include "regcache.h"
28 #include "frame.h"
29 #include "inferior.h"
30 #include "gdb_assert.h"
31 #include "frame-unwind.h"
32 #include "command.h"
33 #include "gdbcmd.h"
34
35 static int pc_in_dummy_frame (CORE_ADDR pc);
36
37 /* Dummy frame.  This saves the processor state just prior to setting
38    up the inferior function call.  Older targets save the registers
39    on the target stack (but that really slows down function calls).  */
40
41 struct dummy_frame
42 {
43   struct dummy_frame *next;
44
45   /* These values belong to the caller (the previous frame, the frame
46      that this unwinds back to).  */
47   CORE_ADDR pc;
48   CORE_ADDR top;
49   struct frame_id id;
50   struct regcache *regcache;
51
52   /* Address range of the call dummy code.  Look for PC in the range
53      [LO..HI) (after allowing for DECR_PC_AFTER_BREAK).  */
54   CORE_ADDR call_lo;
55   CORE_ADDR call_hi;
56 };
57
58 static struct dummy_frame *dummy_frame_stack = NULL;
59
60 /* Function: pc_in_call_dummy (pc)
61
62    Return true if the PC falls in a dummy frame created by gdb for an
63    inferior call.  The code below which allows DECR_PC_AFTER_BREAK is
64    for infrun.c, which may give the function a PC without that
65    subtracted out.  */
66
67 int
68 deprecated_pc_in_call_dummy (CORE_ADDR pc)
69 {
70   return pc_in_dummy_frame (pc);
71 }
72
73 /* Return non-zero if the PC falls in a dummy frame.
74
75    The code below which allows DECR_PC_AFTER_BREAK is for infrun.c,
76    which may give the function a PC without that subtracted out.
77
78    FIXME: cagney/2002-11-23: This is silly.  Surely "infrun.c" can
79    figure out what the real PC (as in the resume address) is BEFORE
80    calling this function.  */
81
82 static int
83 pc_in_dummy_frame (CORE_ADDR pc)
84 {
85   struct dummy_frame *dummyframe;
86   for (dummyframe = dummy_frame_stack;
87        dummyframe != NULL;
88        dummyframe = dummyframe->next)
89     {
90       if ((pc >= dummyframe->call_lo)
91           && (pc < dummyframe->call_hi + DECR_PC_AFTER_BREAK))
92         return 1;
93     }
94   return 0;
95 }
96
97 /* Save all the registers on the dummy frame stack.  Most ports save the
98    registers on the target stack.  This results in lots of unnecessary memory
99    references, which are slow when debugging via a serial line.  Instead, we
100    save all the registers internally, and never write them to the stack.  The
101    registers get restored when the called function returns to the entry point,
102    where a breakpoint is laying in wait.  */
103
104 void
105 generic_push_dummy_frame (void)
106 {
107   struct dummy_frame *dummy_frame;
108   CORE_ADDR fp = get_frame_base (get_current_frame ());
109
110   /* check to see if there are stale dummy frames, 
111      perhaps left over from when a longjump took us out of a 
112      function that was called by the debugger */
113
114   dummy_frame = dummy_frame_stack;
115   while (dummy_frame)
116     if (gdbarch_inner_than (current_gdbarch, dummy_frame->top, fp))
117       /* stale -- destroy! */
118       {
119         dummy_frame_stack = dummy_frame->next;
120         regcache_xfree (dummy_frame->regcache);
121         xfree (dummy_frame);
122         dummy_frame = dummy_frame_stack;
123       }
124     else
125       dummy_frame = dummy_frame->next;
126
127   dummy_frame = xmalloc (sizeof (struct dummy_frame));
128   dummy_frame->regcache = frame_save_as_regcache (get_current_frame ());
129
130   dummy_frame->pc = read_pc ();
131   dummy_frame->top = 0;
132   dummy_frame->id = get_frame_id (get_current_frame ());
133   dummy_frame->next = dummy_frame_stack;
134   dummy_frame_stack = dummy_frame;
135 }
136
137 void
138 generic_save_dummy_frame_tos (CORE_ADDR sp)
139 {
140   dummy_frame_stack->top = sp;
141 }
142
143 /* Record the upper/lower bounds on the address of the call dummy.  */
144
145 void
146 generic_save_call_dummy_addr (CORE_ADDR lo, CORE_ADDR hi)
147 {
148   dummy_frame_stack->call_lo = lo;
149   dummy_frame_stack->call_hi = hi;
150 }
151
152 /* Return the dummy frame cache, it contains both the ID, and a
153    pointer to the regcache.  */
154 struct dummy_frame_cache
155 {
156   struct frame_id this_id;
157   struct regcache *prev_regcache;
158 };
159
160 int
161 dummy_frame_sniffer (const struct frame_unwind *self,
162                      struct frame_info *next_frame,
163                      void **this_prologue_cache)
164 {
165   struct dummy_frame *dummyframe;
166   struct frame_id this_id;
167
168   /* When unwinding a normal frame, the stack structure is determined
169      by analyzing the frame's function's code (be it using brute force
170      prologue analysis, or the dwarf2 CFI).  In the case of a dummy
171      frame, that simply isn't possible.  The PC is either the program
172      entry point, or some random address on the stack.  Trying to use
173      that PC to apply standard frame ID unwind techniques is just
174      asking for trouble.  */
175   /* Use an architecture specific method to extract the prev's dummy
176      ID from the next frame.  Note that this method uses
177      frame_register_unwind to obtain the register values needed to
178      determine the dummy frame's ID.  */
179   this_id = gdbarch_unwind_dummy_id (get_frame_arch (next_frame), next_frame);
180
181   /* Use that ID to find the corresponding cache entry.  */
182   for (dummyframe = dummy_frame_stack;
183        dummyframe != NULL;
184        dummyframe = dummyframe->next)
185     {
186       /* Does the PC fall within the dummy frame's breakpoint
187          instruction.  If not, discard this one.  */
188       if (!(this_id.code_addr >= dummyframe->call_lo
189             && this_id.code_addr < dummyframe->call_hi))
190         continue;
191       /* Does the FP match?  "infcall.c" explicitly saved the
192          top-of-stack before the inferior function call, assume
193          unwind_dummy_id() returns that same stack value.  */
194       if (this_id.stack_addr != dummyframe->top)
195         continue;
196       /* The FP matches this dummy frame.  */
197       {
198         struct dummy_frame_cache *cache;
199         cache = FRAME_OBSTACK_ZALLOC (struct dummy_frame_cache);
200         cache->prev_regcache = dummyframe->regcache;
201         cache->this_id = this_id;
202         (*this_prologue_cache) = cache;
203         return 1;
204       }
205     }
206   return 0;
207 }
208
209 /* Given a call-dummy dummy-frame, return the registers.  Here the
210    register value is taken from the local copy of the register buffer.  */
211
212 static void
213 dummy_frame_prev_register (struct frame_info *next_frame,
214                            void **this_prologue_cache,
215                            int regnum, int *optimized,
216                            enum lval_type *lvalp, CORE_ADDR *addrp,
217                            int *realnum, void *bufferp)
218 {
219   /* The dummy-frame sniffer always fills in the cache.  */
220   struct dummy_frame_cache *cache = (*this_prologue_cache);
221   gdb_assert (cache != NULL);
222
223   /* Describe the register's location.  Generic dummy frames always
224      have the register value in an ``expression''.  */
225   *optimized = 0;
226   *lvalp = not_lval;
227   *addrp = 0;
228   *realnum = -1;
229
230   /* If needed, find and return the value of the register.  */
231   if (bufferp != NULL)
232     {
233       /* Return the actual value.  */
234       /* Use the regcache_cooked_read() method so that it, on the fly,
235          constructs either a raw or pseudo register from the raw
236          register cache.  */
237       regcache_cooked_read (cache->prev_regcache, regnum, bufferp);
238     }
239 }
240
241 /* Assuming that THIS frame is a dummy (remember, the NEXT and not
242    THIS frame is passed in), return the ID of THIS frame.  That ID is
243    determined by examining the NEXT frame's unwound registers using
244    the method unwind_dummy_id().  As a side effect, THIS dummy frame's
245    dummy cache is located and and saved in THIS_PROLOGUE_CACHE.  */
246
247 static void
248 dummy_frame_this_id (struct frame_info *next_frame,
249                      void **this_prologue_cache,
250                      struct frame_id *this_id)
251 {
252   /* The dummy-frame sniffer always fills in the cache.  */
253   struct dummy_frame_cache *cache = (*this_prologue_cache);
254   gdb_assert (cache != NULL);
255   (*this_id) = cache->this_id;
256 }
257
258 static const struct frame_unwind dummy_frame_unwinder =
259 {
260   DUMMY_FRAME,
261   dummy_frame_this_id,
262   dummy_frame_prev_register,
263   NULL,
264   dummy_frame_sniffer,
265 };
266
267 const struct frame_unwind *const dummy_frame_unwind = {
268   &dummy_frame_unwinder
269 };
270
271 static void
272 fprint_dummy_frames (struct ui_file *file)
273 {
274   struct dummy_frame *s;
275   for (s = dummy_frame_stack; s != NULL; s = s->next)
276     {
277       gdb_print_host_address (s, file);
278       fprintf_unfiltered (file, ":");
279       fprintf_unfiltered (file, " pc=0x%s", paddr (s->pc));
280       fprintf_unfiltered (file, " top=0x%s", paddr (s->top));
281       fprintf_unfiltered (file, " id=");
282       fprint_frame_id (file, s->id);
283       fprintf_unfiltered (file, " call_lo=0x%s", paddr (s->call_lo));
284       fprintf_unfiltered (file, " call_hi=0x%s", paddr (s->call_hi));
285       fprintf_unfiltered (file, "\n");
286     }
287 }
288
289 static void
290 maintenance_print_dummy_frames (char *args, int from_tty)
291 {
292   if (args == NULL)
293     fprint_dummy_frames (gdb_stdout);
294   else
295     {
296       struct ui_file *file = gdb_fopen (args, "w");
297       if (file == NULL)
298         perror_with_name ("maintenance print dummy-frames");
299       fprint_dummy_frames (file);    
300       ui_file_delete (file);
301     }
302 }
303
304 extern void _initialize_dummy_frame (void);
305
306 void
307 _initialize_dummy_frame (void)
308 {
309   add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames,
310            "Print the contents of the internal dummy-frame stack.",
311            &maintenanceprintlist);
312
313 }