1 /* Code dealing with dummy stack frames, for GDB, the GNU debugger.
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software
7 This file is part of GDB.
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.
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.
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. */
26 #include "dummy-frame.h"
30 #include "gdb_assert.h"
32 /* Dummy frame. This saves the processor state just prior to setting
33 up the inferior function call. Older targets save the registers
34 on the target stack (but that really slows down function calls). */
38 struct dummy_frame *next;
40 /* These values belong to the caller (the previous frame, the frame
41 that this unwinds back to). */
46 struct regcache *regcache;
48 /* Address range of the call dummy code. Look for PC in the range
49 [LO..HI) (after allowing for DECR_PC_AFTER_BREAK). */
54 static struct dummy_frame *dummy_frame_stack = NULL;
56 /* Function: find_dummy_frame(pc, fp, sp)
58 Search the stack of dummy frames for one matching the given PC and
59 FP/SP. Unlike pc_in_dummy_frame(), this function doesn't need to
60 adjust for DECR_PC_AFTER_BREAK. This is because it is only legal
61 to call this function after the PC has been adjusted. */
63 static struct dummy_frame *
64 find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
66 struct dummy_frame *dummyframe;
68 for (dummyframe = dummy_frame_stack; dummyframe != NULL;
69 dummyframe = dummyframe->next)
71 /* Does the PC fall within the dummy frame's breakpoint
72 instruction. If not, discard this one. */
73 if (!(pc >= dummyframe->call_lo && pc < dummyframe->call_hi))
75 /* Does the FP match? */
76 if (dummyframe->top != 0)
78 /* If the target architecture explicitly saved the
79 top-of-stack before the inferior function call, assume
80 that that same architecture will always pass in an FP
81 (frame base) value that eactly matches that saved TOS.
82 Don't check the saved SP and SP as they can lead to false
84 if (fp != dummyframe->top)
89 /* An older target that hasn't explicitly or implicitly
90 saved the dummy frame's top-of-stack. Try matching the
91 FP against the saved SP and FP. NOTE: If you're trying
92 to fix a problem with GDB not correctly finding a dummy
93 frame, check the comments that go with FRAME_ALIGN() and
94 SAVE_DUMMY_FRAME_TOS(). */
95 if (fp != dummyframe->fp && fp != dummyframe->sp)
98 /* The FP matches this dummy frame. */
106 cached_find_dummy_frame (struct frame_info *frame, void **cache)
108 if ((*cache) == NULL)
109 (*cache) = find_dummy_frame (get_frame_pc (frame), get_frame_base (frame));
114 generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
116 struct dummy_frame *dummy = find_dummy_frame (pc, fp);
118 return dummy->regcache;
124 deprecated_generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
126 struct regcache *regcache = generic_find_dummy_frame (pc, fp);
127 if (regcache == NULL)
129 return deprecated_grub_regcache_for_registers (regcache);
132 /* Function: pc_in_call_dummy (pc, sp, fp)
134 Return true if the PC falls in a dummy frame created by gdb for an
135 inferior call. The code below which allows DECR_PC_AFTER_BREAK is
136 for infrun.c, which may give the function a PC without that
140 generic_pc_in_call_dummy (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR fp)
142 return pc_in_dummy_frame (pc);
145 /* Return non-zero if the PC falls in a dummy frame.
147 The code below which allows DECR_PC_AFTER_BREAK is for infrun.c,
148 which may give the function a PC without that subtracted out.
150 FIXME: cagney/2002-11-23: This is silly. Surely "infrun.c" can
151 figure out what the real PC (as in the resume address) is BEFORE
152 calling this function (Oh, and I'm not even sure that this function
153 is called with an decremented PC, the call to pc_in_call_dummy() in
154 that file is conditional on !CALL_DUMMY_BREAKPOINT_OFFSET_P yet
155 generic dummy targets set CALL_DUMMY_BREAKPOINT_OFFSET. True?). */
158 pc_in_dummy_frame (CORE_ADDR pc)
160 struct dummy_frame *dummyframe;
161 for (dummyframe = dummy_frame_stack;
163 dummyframe = dummyframe->next)
165 if ((pc >= dummyframe->call_lo)
166 && (pc < dummyframe->call_hi + DECR_PC_AFTER_BREAK))
172 /* Function: read_register_dummy
173 Find a saved register from before GDB calls a function in the inferior */
176 deprecated_read_register_dummy (CORE_ADDR pc, CORE_ADDR fp, int regno)
178 struct regcache *dummy_regs = generic_find_dummy_frame (pc, fp);
182 /* NOTE: cagney/2002-08-12: Replaced a call to
183 regcache_raw_read_as_address() with a call to
184 regcache_cooked_read_unsigned(). The old, ...as_address
185 function was eventually calling extract_unsigned_integer (via
186 extract_address) to unpack the registers value. The below is
187 doing an unsigned extract so that it is functionally
188 equivalent. The read needs to be cooked as, otherwise, it
189 will never correctly return the value of a register in the
190 [NUM_REGS .. NUM_REGS+NUM_PSEUDO_REGS) range. */
192 regcache_cooked_read_unsigned (dummy_regs, regno, &val);
199 /* Save all the registers on the dummy frame stack. Most ports save the
200 registers on the target stack. This results in lots of unnecessary memory
201 references, which are slow when debugging via a serial line. Instead, we
202 save all the registers internally, and never write them to the stack. The
203 registers get restored when the called function returns to the entry point,
204 where a breakpoint is laying in wait. */
207 generic_push_dummy_frame (void)
209 struct dummy_frame *dummy_frame;
210 CORE_ADDR fp = get_frame_base (get_current_frame ());
212 /* check to see if there are stale dummy frames,
213 perhaps left over from when a longjump took us out of a
214 function that was called by the debugger */
216 dummy_frame = dummy_frame_stack;
218 if (INNER_THAN (dummy_frame->fp, fp)) /* stale -- destroy! */
220 dummy_frame_stack = dummy_frame->next;
221 regcache_xfree (dummy_frame->regcache);
223 dummy_frame = dummy_frame_stack;
226 dummy_frame = dummy_frame->next;
228 dummy_frame = xmalloc (sizeof (struct dummy_frame));
229 dummy_frame->regcache = regcache_xmalloc (current_gdbarch);
231 dummy_frame->pc = read_pc ();
232 dummy_frame->sp = read_sp ();
233 dummy_frame->top = 0;
234 dummy_frame->fp = fp;
235 regcache_cpy (dummy_frame->regcache, current_regcache);
236 dummy_frame->next = dummy_frame_stack;
237 dummy_frame_stack = dummy_frame;
241 generic_save_dummy_frame_tos (CORE_ADDR sp)
243 dummy_frame_stack->top = sp;
246 /* Record the upper/lower bounds on the address of the call dummy. */
249 generic_save_call_dummy_addr (CORE_ADDR lo, CORE_ADDR hi)
251 dummy_frame_stack->call_lo = lo;
252 dummy_frame_stack->call_hi = hi;
255 /* Restore the machine state from either the saved dummy stack or a
259 generic_pop_current_frame (void (*popper) (struct frame_info * frame))
261 struct frame_info *frame = get_current_frame ();
262 if (get_frame_type (frame) == DUMMY_FRAME)
263 /* NOTE: cagney/2002-22-23: Does this ever occure? Surely a dummy
264 frame will have already been poped by the "infrun.c" code. */
265 generic_pop_dummy_frame ();
270 /* Function: pop_dummy_frame
271 Restore the machine state from a saved dummy stack frame. */
274 generic_pop_dummy_frame (void)
276 struct dummy_frame *dummy_frame = dummy_frame_stack;
278 /* FIXME: what if the first frame isn't the right one, eg..
279 because one call-by-hand function has done a longjmp into another one? */
282 error ("Can't pop dummy frame!");
283 dummy_frame_stack = dummy_frame->next;
284 regcache_cpy (current_regcache, dummy_frame->regcache);
285 flush_cached_frames ();
287 regcache_xfree (dummy_frame->regcache);
291 /* Function: fix_call_dummy
292 Stub function. Generic dummy frames typically do not need to fix
293 the frame being created */
296 generic_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
297 struct value **args, struct type *type, int gcc_p)
302 /* Given a call-dummy dummy-frame, return the registers. Here the
303 register value is taken from the local copy of the register buffer. */
306 dummy_frame_register_unwind (struct frame_info *frame, void **cache,
307 int regnum, int *optimized,
308 enum lval_type *lvalp, CORE_ADDR *addrp,
309 int *realnum, void *bufferp)
311 struct dummy_frame *dummy = cached_find_dummy_frame (frame, cache);
312 gdb_assert (dummy != NULL);
314 /* Describe the register's location. Generic dummy frames always
315 have the register value in an ``expression''. */
321 /* If needed, find and return the value of the register. */
324 /* Return the actual value. */
325 /* Use the regcache_cooked_read() method so that it, on the fly,
326 constructs either a raw or pseudo register from the raw
328 regcache_cooked_read (dummy->regcache, regnum, bufferp);
333 dummy_frame_pc_unwind (struct frame_info *frame,
336 struct dummy_frame *dummy = cached_find_dummy_frame (frame, cache);
337 /* Oops! In a dummy-frame but can't find the stack dummy. Pretend
338 that the frame doesn't unwind. Should this function instead
339 return a has-no-caller indication? */