2003-01-19 Andrew Cagney <ac131313@redhat.com>
[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 Free Software
5    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
33 /* Dummy frame.  This saves the processor state just prior to setting
34    up the inferior function call.  Older targets save the registers
35    on the target stack (but that really slows down function calls).  */
36
37 struct dummy_frame
38 {
39   struct dummy_frame *next;
40
41   /* These values belong to the caller (the previous frame, the frame
42      that this unwinds back to).  */
43   CORE_ADDR pc;
44   CORE_ADDR fp;
45   CORE_ADDR sp;
46   CORE_ADDR top;
47   struct frame_id id;
48   struct regcache *regcache;
49
50   /* Address range of the call dummy code.  Look for PC in the range
51      [LO..HI) (after allowing for DECR_PC_AFTER_BREAK).  */
52   CORE_ADDR call_lo;
53   CORE_ADDR call_hi;
54 };
55
56 static struct dummy_frame *dummy_frame_stack = NULL;
57
58 /* Function: find_dummy_frame(pc, fp, sp)
59
60    Search the stack of dummy frames for one matching the given PC and
61    FP/SP.  Unlike pc_in_dummy_frame(), this function doesn't need to
62    adjust for DECR_PC_AFTER_BREAK.  This is because it is only legal
63    to call this function after the PC has been adjusted.  */
64
65 static struct dummy_frame *
66 find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
67 {
68   struct dummy_frame *dummyframe;
69
70   for (dummyframe = dummy_frame_stack; dummyframe != NULL;
71        dummyframe = dummyframe->next)
72     {
73       /* Does the PC fall within the dummy frame's breakpoint
74          instruction.  If not, discard this one.  */
75       if (!(pc >= dummyframe->call_lo && pc < dummyframe->call_hi))
76         continue;
77       /* Does the FP match?  */
78       if (dummyframe->top != 0)
79         {
80           /* If the target architecture explicitly saved the
81              top-of-stack before the inferior function call, assume
82              that that same architecture will always pass in an FP
83              (frame base) value that eactly matches that saved TOS.
84              Don't check the saved SP and SP as they can lead to false
85              hits.  */
86           if (fp != dummyframe->top)
87             continue;
88         }
89       else
90         {
91           /* An older target that hasn't explicitly or implicitly
92              saved the dummy frame's top-of-stack.  Try matching the
93              FP against the saved SP and FP.  NOTE: If you're trying
94              to fix a problem with GDB not correctly finding a dummy
95              frame, check the comments that go with FRAME_ALIGN() and
96              SAVE_DUMMY_FRAME_TOS().  */
97           if (fp != dummyframe->fp && fp != dummyframe->sp)
98             continue;
99         }
100       /* The FP matches this dummy frame.  */
101       return dummyframe;
102     }
103
104   return NULL;
105 }
106
107 struct dummy_frame *
108 cached_find_dummy_frame (struct frame_info *frame, void **cache)
109 {
110   if ((*cache) == NULL)
111     (*cache) = find_dummy_frame (get_frame_pc (frame), get_frame_base (frame));
112   return (*cache);
113 }
114
115 struct regcache *
116 generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
117 {
118   struct dummy_frame *dummy = find_dummy_frame (pc, fp);
119   if (dummy != NULL)
120     return dummy->regcache;
121   else
122     return NULL;
123 }
124
125 char *
126 deprecated_generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
127 {
128   struct regcache *regcache = generic_find_dummy_frame (pc, fp);
129   if (regcache == NULL)
130     return NULL;
131   return deprecated_grub_regcache_for_registers (regcache);
132 }
133
134 /* Function: pc_in_call_dummy (pc, sp, fp)
135
136    Return true if the PC falls in a dummy frame created by gdb for an
137    inferior call.  The code below which allows DECR_PC_AFTER_BREAK is
138    for infrun.c, which may give the function a PC without that
139    subtracted out.  */
140
141 int
142 generic_pc_in_call_dummy (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR fp)
143 {
144   return pc_in_dummy_frame (pc);
145 }
146
147 /* Return non-zero if the PC falls in a dummy frame.
148
149    The code below which allows DECR_PC_AFTER_BREAK is for infrun.c,
150    which may give the function a PC without that subtracted out.
151
152    FIXME: cagney/2002-11-23: This is silly.  Surely "infrun.c" can
153    figure out what the real PC (as in the resume address) is BEFORE
154    calling this function (Oh, and I'm not even sure that this function
155    is called with an decremented PC, the call to pc_in_call_dummy() in
156    that file is conditional on !CALL_DUMMY_BREAKPOINT_OFFSET_P yet
157    generic dummy targets set CALL_DUMMY_BREAKPOINT_OFFSET. True?).  */
158
159 int
160 pc_in_dummy_frame (CORE_ADDR pc)
161 {
162   struct dummy_frame *dummyframe;
163   for (dummyframe = dummy_frame_stack;
164        dummyframe != NULL;
165        dummyframe = dummyframe->next)
166     {
167       if ((pc >= dummyframe->call_lo)
168           && (pc < dummyframe->call_hi + DECR_PC_AFTER_BREAK))
169         return 1;
170     }
171   return 0;
172 }
173
174 /* Function: read_register_dummy 
175    Find a saved register from before GDB calls a function in the inferior */
176
177 CORE_ADDR
178 deprecated_read_register_dummy (CORE_ADDR pc, CORE_ADDR fp, int regno)
179 {
180   struct regcache *dummy_regs = generic_find_dummy_frame (pc, fp);
181
182   if (dummy_regs)
183     {
184       /* NOTE: cagney/2002-08-12: Replaced a call to
185          regcache_raw_read_as_address() with a call to
186          regcache_cooked_read_unsigned().  The old, ...as_address
187          function was eventually calling extract_unsigned_integer (via
188          extract_address) to unpack the registers value.  The below is
189          doing an unsigned extract so that it is functionally
190          equivalent.  The read needs to be cooked as, otherwise, it
191          will never correctly return the value of a register in the
192          [NUM_REGS .. NUM_REGS+NUM_PSEUDO_REGS) range.  */
193       ULONGEST val;
194       regcache_cooked_read_unsigned (dummy_regs, regno, &val);
195       return val;
196     }
197   else
198     return 0;
199 }
200
201 /* Save all the registers on the dummy frame stack.  Most ports save the
202    registers on the target stack.  This results in lots of unnecessary memory
203    references, which are slow when debugging via a serial line.  Instead, we
204    save all the registers internally, and never write them to the stack.  The
205    registers get restored when the called function returns to the entry point,
206    where a breakpoint is laying in wait.  */
207
208 void
209 generic_push_dummy_frame (void)
210 {
211   struct dummy_frame *dummy_frame;
212   CORE_ADDR fp = get_frame_base (get_current_frame ());
213
214   /* check to see if there are stale dummy frames, 
215      perhaps left over from when a longjump took us out of a 
216      function that was called by the debugger */
217
218   dummy_frame = dummy_frame_stack;
219   while (dummy_frame)
220     if (INNER_THAN (dummy_frame->fp, fp))       /* stale -- destroy! */
221       {
222         dummy_frame_stack = dummy_frame->next;
223         regcache_xfree (dummy_frame->regcache);
224         xfree (dummy_frame);
225         dummy_frame = dummy_frame_stack;
226       }
227     else
228       dummy_frame = dummy_frame->next;
229
230   dummy_frame = xmalloc (sizeof (struct dummy_frame));
231   dummy_frame->regcache = regcache_xmalloc (current_gdbarch);
232
233   dummy_frame->pc = read_pc ();
234   dummy_frame->sp = read_sp ();
235   dummy_frame->top = 0;
236   dummy_frame->fp = fp;
237   dummy_frame->id = get_frame_id (get_current_frame ());
238   regcache_cpy (dummy_frame->regcache, current_regcache);
239   dummy_frame->next = dummy_frame_stack;
240   dummy_frame_stack = dummy_frame;
241 }
242
243 void
244 generic_save_dummy_frame_tos (CORE_ADDR sp)
245 {
246   dummy_frame_stack->top = sp;
247 }
248
249 /* Record the upper/lower bounds on the address of the call dummy.  */
250
251 void
252 generic_save_call_dummy_addr (CORE_ADDR lo, CORE_ADDR hi)
253 {
254   dummy_frame_stack->call_lo = lo;
255   dummy_frame_stack->call_hi = hi;
256 }
257
258 /* Restore the machine state from either the saved dummy stack or a
259    real stack frame. */
260
261 void
262 generic_pop_current_frame (void (*popper) (struct frame_info * frame))
263 {
264   struct frame_info *frame = get_current_frame ();
265   if (get_frame_type (frame) == DUMMY_FRAME)
266     /* NOTE: cagney/2002-22-23: Does this ever occure?  Surely a dummy
267        frame will have already been poped by the "infrun.c" code.  */
268     generic_pop_dummy_frame ();
269   else
270     (*popper) (frame);
271 }
272
273 /* Discard the innermost dummy frame from the dummy frame stack
274    (passed in as a parameter).  */
275
276 static void
277 discard_innermost_dummy (struct dummy_frame **stack)
278 {
279   struct dummy_frame *tbd = (*stack);
280   (*stack) = (*stack)->next;
281   regcache_xfree (tbd->regcache);
282   xfree (tbd);
283 }
284
285 /* Function: dummy_frame_pop.  Restore the machine state from a saved
286    dummy stack frame. */
287
288 static void
289 dummy_frame_pop (struct frame_info *fi, void **cache,
290                  struct regcache *regcache)
291 {
292   struct dummy_frame *dummy = cached_find_dummy_frame (fi, cache);
293
294   /* If it isn't, what are we even doing here?  */
295   gdb_assert (get_frame_type (fi) == DUMMY_FRAME);
296
297   if (dummy == NULL)
298     error ("Can't pop dummy frame!");
299
300   /* Discard all dummy frames up-to but not including this one.  */
301   while (dummy_frame_stack != dummy)
302     discard_innermost_dummy (&dummy_frame_stack);
303
304   /* Restore this one.  */
305   regcache_cpy (regcache, dummy->regcache);
306   flush_cached_frames ();
307
308   /* Now discard it.  */
309   discard_innermost_dummy (&dummy_frame_stack);
310
311   /* Note: target changed would be better.  Registers, memory and
312      frame are all invalid.  */
313   flush_cached_frames ();
314 }
315
316 void
317 generic_pop_dummy_frame (void)
318 {
319   struct dummy_frame *dummy_frame = dummy_frame_stack;
320
321   /* FIXME: what if the first frame isn't the right one, eg..
322      because one call-by-hand function has done a longjmp into another one? */
323
324   if (!dummy_frame)
325     error ("Can't pop dummy frame!");
326   regcache_cpy (current_regcache, dummy_frame->regcache);
327   flush_cached_frames ();
328
329   discard_innermost_dummy (&dummy_frame_stack);
330 }
331
332 /* Function: fix_call_dummy
333    Stub function.  Generic dummy frames typically do not need to fix
334    the frame being created */
335
336 void
337 generic_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
338                         struct value **args, struct type *type, int gcc_p)
339 {
340   return;
341 }
342
343 /* Given a call-dummy dummy-frame, return the registers.  Here the
344    register value is taken from the local copy of the register buffer.  */
345
346 static void
347 dummy_frame_register_unwind (struct frame_info *frame, void **cache,
348                              int regnum, int *optimized,
349                              enum lval_type *lvalp, CORE_ADDR *addrp,
350                              int *realnum, void *bufferp)
351 {
352   struct dummy_frame *dummy = cached_find_dummy_frame (frame, cache);
353   gdb_assert (dummy != NULL);
354
355   /* Describe the register's location.  Generic dummy frames always
356      have the register value in an ``expression''.  */
357   *optimized = 0;
358   *lvalp = not_lval;
359   *addrp = 0;
360   *realnum = -1;
361
362   /* If needed, find and return the value of the register.  */
363   if (bufferp != NULL)
364     {
365       /* Return the actual value.  */
366       /* Use the regcache_cooked_read() method so that it, on the fly,
367          constructs either a raw or pseudo register from the raw
368          register cache.  */
369       regcache_cooked_read (dummy->regcache, regnum, bufferp);
370     }
371 }
372
373 /* Assuming that FRAME is a dummy, return the resume address for the
374    previous frame.  */
375
376 static CORE_ADDR
377 dummy_frame_pc_unwind (struct frame_info *frame,
378                        void **cache)
379 {
380   struct dummy_frame *dummy = cached_find_dummy_frame (frame, cache);
381   /* Oops!  In a dummy-frame but can't find the stack dummy.  Pretend
382      that the frame doesn't unwind.  Should this function instead
383      return a has-no-caller indication?  */
384   if (dummy == NULL)
385     return 0;
386   return dummy->pc;
387 }
388
389
390 /* Assuming that FRAME is a dummy, return the ID of the calling frame
391    (the frame that the dummy has the saved state of).  */
392
393 static void
394 dummy_frame_id_unwind (struct frame_info *frame,
395                        void **cache,
396                        struct frame_id *id)
397 {
398   struct dummy_frame *dummy = cached_find_dummy_frame (frame, cache);
399   /* Oops!  In a dummy-frame but can't find the stack dummy.  Pretend
400      that the frame doesn't unwind.  Should this function instead
401      return a has-no-caller indication?  */
402   if (dummy == NULL)
403     (*id) = null_frame_id;
404   else
405     (*id) = dummy->id;
406 }
407
408 static struct frame_unwind dummy_frame_unwind =
409 {
410   dummy_frame_pop,
411   dummy_frame_pc_unwind,
412   dummy_frame_id_unwind,
413   dummy_frame_register_unwind
414 };
415
416 const struct frame_unwind *
417 dummy_frame_p (CORE_ADDR pc)
418 {
419   if (DEPRECATED_PC_IN_CALL_DUMMY_P ()
420       ? DEPRECATED_PC_IN_CALL_DUMMY (pc, 0, 0)
421       : pc_in_dummy_frame (pc))
422     return &dummy_frame_unwind;
423   else
424     return NULL;
425 }