2003-05-03 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / dummy-frame.c
CommitLineData
9c1412c1
AC
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"
494cca16 31#include "frame-unwind.h"
9c1412c1 32
6dc42492
AC
33static void dummy_frame_this_id (struct frame_info *next_frame,
34 void **this_prologue_cache,
35 struct frame_id *this_id);
36
9c1412c1
AC
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
41struct dummy_frame
42{
43 struct dummy_frame *next;
44
f18c5a73
AC
45 /* These values belong to the caller (the previous frame, the frame
46 that this unwinds back to). */
9c1412c1
AC
47 CORE_ADDR pc;
48 CORE_ADDR fp;
49 CORE_ADDR sp;
50 CORE_ADDR top;
c689142b 51 struct frame_id id;
9c1412c1
AC
52 struct regcache *regcache;
53
54 /* Address range of the call dummy code. Look for PC in the range
55 [LO..HI) (after allowing for DECR_PC_AFTER_BREAK). */
56 CORE_ADDR call_lo;
57 CORE_ADDR call_hi;
58};
59
60static struct dummy_frame *dummy_frame_stack = NULL;
61
62/* Function: find_dummy_frame(pc, fp, sp)
63
64 Search the stack of dummy frames for one matching the given PC and
5e0f933e 65 FP/SP. Unlike pc_in_dummy_frame(), this function doesn't need to
9c1412c1
AC
66 adjust for DECR_PC_AFTER_BREAK. This is because it is only legal
67 to call this function after the PC has been adjusted. */
68
8779790c
AC
69static struct dummy_frame *
70find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
9c1412c1
AC
71{
72 struct dummy_frame *dummyframe;
73
74 for (dummyframe = dummy_frame_stack; dummyframe != NULL;
75 dummyframe = dummyframe->next)
76 {
77 /* Does the PC fall within the dummy frame's breakpoint
78 instruction. If not, discard this one. */
79 if (!(pc >= dummyframe->call_lo && pc < dummyframe->call_hi))
80 continue;
81 /* Does the FP match? */
82 if (dummyframe->top != 0)
83 {
84 /* If the target architecture explicitly saved the
85 top-of-stack before the inferior function call, assume
86 that that same architecture will always pass in an FP
87 (frame base) value that eactly matches that saved TOS.
88 Don't check the saved SP and SP as they can lead to false
89 hits. */
90 if (fp != dummyframe->top)
91 continue;
92 }
93 else
94 {
95 /* An older target that hasn't explicitly or implicitly
96 saved the dummy frame's top-of-stack. Try matching the
97 FP against the saved SP and FP. NOTE: If you're trying
98 to fix a problem with GDB not correctly finding a dummy
99 frame, check the comments that go with FRAME_ALIGN() and
100 SAVE_DUMMY_FRAME_TOS(). */
101 if (fp != dummyframe->fp && fp != dummyframe->sp)
102 continue;
103 }
104 /* The FP matches this dummy frame. */
8779790c 105 return dummyframe;
9c1412c1
AC
106 }
107
8779790c
AC
108 return NULL;
109}
110
8779790c
AC
111struct regcache *
112generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
113{
114 struct dummy_frame *dummy = find_dummy_frame (pc, fp);
115 if (dummy != NULL)
116 return dummy->regcache;
117 else
118 return NULL;
9c1412c1
AC
119}
120
121char *
122deprecated_generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
123{
124 struct regcache *regcache = generic_find_dummy_frame (pc, fp);
125 if (regcache == NULL)
126 return NULL;
127 return deprecated_grub_regcache_for_registers (regcache);
128}
129
130/* Function: pc_in_call_dummy (pc, sp, fp)
131
132 Return true if the PC falls in a dummy frame created by gdb for an
133 inferior call. The code below which allows DECR_PC_AFTER_BREAK is
134 for infrun.c, which may give the function a PC without that
135 subtracted out. */
136
137int
138generic_pc_in_call_dummy (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR fp)
5e0f933e
AC
139{
140 return pc_in_dummy_frame (pc);
141}
142
143/* Return non-zero if the PC falls in a dummy frame.
144
145 The code below which allows DECR_PC_AFTER_BREAK is for infrun.c,
146 which may give the function a PC without that subtracted out.
147
148 FIXME: cagney/2002-11-23: This is silly. Surely "infrun.c" can
149 figure out what the real PC (as in the resume address) is BEFORE
150 calling this function (Oh, and I'm not even sure that this function
151 is called with an decremented PC, the call to pc_in_call_dummy() in
b1e29e33
AC
152 that file is conditional on
153 !DEPRECATED_CALL_DUMMY_BREAKPOINT_OFFSET_P yet generic dummy
154 targets set DEPRECATED_CALL_DUMMY_BREAKPOINT_OFFSET. True?). */
5e0f933e
AC
155
156int
157pc_in_dummy_frame (CORE_ADDR pc)
9c1412c1
AC
158{
159 struct dummy_frame *dummyframe;
160 for (dummyframe = dummy_frame_stack;
161 dummyframe != NULL;
162 dummyframe = dummyframe->next)
163 {
164 if ((pc >= dummyframe->call_lo)
165 && (pc < dummyframe->call_hi + DECR_PC_AFTER_BREAK))
166 return 1;
167 }
168 return 0;
169}
170
171/* Function: read_register_dummy
172 Find a saved register from before GDB calls a function in the inferior */
173
174CORE_ADDR
175deprecated_read_register_dummy (CORE_ADDR pc, CORE_ADDR fp, int regno)
176{
177 struct regcache *dummy_regs = generic_find_dummy_frame (pc, fp);
178
179 if (dummy_regs)
180 {
181 /* NOTE: cagney/2002-08-12: Replaced a call to
182 regcache_raw_read_as_address() with a call to
183 regcache_cooked_read_unsigned(). The old, ...as_address
184 function was eventually calling extract_unsigned_integer (via
185 extract_address) to unpack the registers value. The below is
186 doing an unsigned extract so that it is functionally
187 equivalent. The read needs to be cooked as, otherwise, it
188 will never correctly return the value of a register in the
189 [NUM_REGS .. NUM_REGS+NUM_PSEUDO_REGS) range. */
190 ULONGEST val;
191 regcache_cooked_read_unsigned (dummy_regs, regno, &val);
192 return val;
193 }
194 else
195 return 0;
196}
197
198/* Save all the registers on the dummy frame stack. Most ports save the
199 registers on the target stack. This results in lots of unnecessary memory
200 references, which are slow when debugging via a serial line. Instead, we
201 save all the registers internally, and never write them to the stack. The
202 registers get restored when the called function returns to the entry point,
203 where a breakpoint is laying in wait. */
204
205void
206generic_push_dummy_frame (void)
207{
208 struct dummy_frame *dummy_frame;
8b36eed8 209 CORE_ADDR fp = get_frame_base (get_current_frame ());
9c1412c1
AC
210
211 /* check to see if there are stale dummy frames,
212 perhaps left over from when a longjump took us out of a
213 function that was called by the debugger */
214
215 dummy_frame = dummy_frame_stack;
216 while (dummy_frame)
217 if (INNER_THAN (dummy_frame->fp, fp)) /* stale -- destroy! */
218 {
219 dummy_frame_stack = dummy_frame->next;
220 regcache_xfree (dummy_frame->regcache);
221 xfree (dummy_frame);
222 dummy_frame = dummy_frame_stack;
223 }
224 else
225 dummy_frame = dummy_frame->next;
226
227 dummy_frame = xmalloc (sizeof (struct dummy_frame));
228 dummy_frame->regcache = regcache_xmalloc (current_gdbarch);
229
230 dummy_frame->pc = read_pc ();
231 dummy_frame->sp = read_sp ();
232 dummy_frame->top = 0;
233 dummy_frame->fp = fp;
c689142b 234 dummy_frame->id = get_frame_id (get_current_frame ());
9c1412c1
AC
235 regcache_cpy (dummy_frame->regcache, current_regcache);
236 dummy_frame->next = dummy_frame_stack;
237 dummy_frame_stack = dummy_frame;
238}
239
240void
241generic_save_dummy_frame_tos (CORE_ADDR sp)
242{
243 dummy_frame_stack->top = sp;
244}
245
246/* Record the upper/lower bounds on the address of the call dummy. */
247
248void
249generic_save_call_dummy_addr (CORE_ADDR lo, CORE_ADDR hi)
250{
251 dummy_frame_stack->call_lo = lo;
252 dummy_frame_stack->call_hi = hi;
253}
254
255/* Restore the machine state from either the saved dummy stack or a
256 real stack frame. */
257
258void
259generic_pop_current_frame (void (*popper) (struct frame_info * frame))
260{
261 struct frame_info *frame = get_current_frame ();
5e0f933e
AC
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. */
9c1412c1
AC
265 generic_pop_dummy_frame ();
266 else
267 (*popper) (frame);
268}
269
dbe9fe58
AC
270/* Discard the innermost dummy frame from the dummy frame stack
271 (passed in as a parameter). */
272
273static void
274discard_innermost_dummy (struct dummy_frame **stack)
275{
276 struct dummy_frame *tbd = (*stack);
277 (*stack) = (*stack)->next;
278 regcache_xfree (tbd->regcache);
279 xfree (tbd);
280}
281
9c1412c1
AC
282void
283generic_pop_dummy_frame (void)
284{
285 struct dummy_frame *dummy_frame = dummy_frame_stack;
286
287 /* FIXME: what if the first frame isn't the right one, eg..
288 because one call-by-hand function has done a longjmp into another one? */
289
290 if (!dummy_frame)
291 error ("Can't pop dummy frame!");
9c1412c1
AC
292 regcache_cpy (current_regcache, dummy_frame->regcache);
293 flush_cached_frames ();
294
dbe9fe58 295 discard_innermost_dummy (&dummy_frame_stack);
9c1412c1
AC
296}
297
9c1412c1
AC
298/* Given a call-dummy dummy-frame, return the registers. Here the
299 register value is taken from the local copy of the register buffer. */
300
494cca16 301static void
6dc42492
AC
302dummy_frame_prev_register (struct frame_info *next_frame,
303 void **this_prologue_cache,
304 int regnum, int *optimized,
305 enum lval_type *lvalp, CORE_ADDR *addrp,
306 int *realnum, void *bufferp)
9c1412c1 307{
6dc42492
AC
308 struct dummy_frame *dummy;
309 struct frame_id id;
310
311 /* Call the ID method which, if at all possible, will set the
312 prologue cache. */
313 dummy_frame_this_id (next_frame, this_prologue_cache, &id);
314 dummy = (*this_prologue_cache);
8779790c 315 gdb_assert (dummy != NULL);
9c1412c1
AC
316
317 /* Describe the register's location. Generic dummy frames always
318 have the register value in an ``expression''. */
319 *optimized = 0;
320 *lvalp = not_lval;
321 *addrp = 0;
322 *realnum = -1;
323
324 /* If needed, find and return the value of the register. */
325 if (bufferp != NULL)
326 {
9c1412c1
AC
327 /* Return the actual value. */
328 /* Use the regcache_cooked_read() method so that it, on the fly,
329 constructs either a raw or pseudo register from the raw
330 register cache. */
8779790c 331 regcache_cooked_read (dummy->regcache, regnum, bufferp);
9c1412c1
AC
332 }
333}
334
6dc42492
AC
335/* Assuming that THIS frame is a dummy (remember, the NEXT and not
336 THIS frame is passed in), return the ID of THIS frame. That ID is
337 determined by examining the NEXT frame's unwound registers using
338 the method unwind_dummy_id(). As a side effect, THIS dummy frame's
339 dummy cache is located and and saved in THIS_PROLOGUE_CACHE. */
494cca16
AC
340
341static void
6dc42492
AC
342dummy_frame_this_id (struct frame_info *next_frame,
343 void **this_prologue_cache,
344 struct frame_id *this_id)
c689142b 345{
6dc42492
AC
346 struct dummy_frame *dummy = (*this_prologue_cache);
347 if (dummy != NULL)
348 {
349 (*this_id) = dummy->id;
350 return;
351 }
352 /* When unwinding a normal frame, the stack structure is determined
353 by analyzing the frame's function's code (be it using brute force
354 prologue analysis, or the dwarf2 CFI). In the case of a dummy
355 frame, that simply isn't possible. The The PC is either the
356 program entry point, or some random address on the stack. Trying
357 to use that PC to apply standard frame ID unwind techniques is
358 just asking for trouble. */
359 if (gdbarch_unwind_dummy_id_p (current_gdbarch))
360 {
e8a8712a 361 /* Assume call_function_by_hand(), via SAVE_DUMMY_FRAME_TOS,
6dc42492
AC
362 previously saved the dummy frame's ID. Things only work if
363 the two return the same value. */
364 gdb_assert (SAVE_DUMMY_FRAME_TOS_P ());
365 /* Use an architecture specific method to extract the prev's
366 dummy ID from the next frame. Note that this method uses
367 frame_register_unwind to obtain the register values needed to
368 determine the dummy frame's ID. */
369 (*this_id) = gdbarch_unwind_dummy_id (current_gdbarch, next_frame);
370 }
371 else if (frame_relative_level (next_frame) < 0)
372 {
373 /* We're unwinding a sentinel frame, the PC of which is pointing
374 at a stack dummy. Fake up the dummy frame's ID using the
375 same sequence as is found a traditional unwinder. Once all
376 architectures supply the unwind_dummy_id method, this code
377 can go away. */
0ba6dca9 378 (*this_id) = frame_id_build (deprecated_read_fp (), read_pc ());
6dc42492
AC
379 }
380 else if (legacy_frame_p (current_gdbarch)
381 && get_prev_frame (next_frame))
382 {
383 /* Things are looking seriously grim! Assume that the legacy
384 get_prev_frame code has already created THIS frame and linked
385 it in to the frame chain (a pretty bold assumption), extract
386 the ID from THIS base / pc. */
11889732
AC
387 (*this_id) = frame_id_build (get_frame_base (get_prev_frame (next_frame)),
388 get_frame_pc (get_prev_frame (next_frame)));
6dc42492 389 }
c170fb60 390 else
6dc42492
AC
391 {
392 /* Outch! We're not trying to find the innermost frame's ID yet
393 we're trying to unwind to a dummy. The architecture must
394 provide the unwind_dummy_id() method. Abandon the unwind
395 process but only after first warning the user. */
396 internal_warning (__FILE__, __LINE__,
397 "Missing unwind_dummy_id architecture method");
398 (*this_id) = null_frame_id;
399 return;
400 }
d0a55772
AC
401 (*this_prologue_cache) = find_dummy_frame ((*this_id).code_addr,
402 (*this_id).stack_addr);
c689142b
AC
403}
404
494cca16
AC
405static struct frame_unwind dummy_frame_unwind =
406{
7df05f2b 407 DUMMY_FRAME,
6dc42492
AC
408 dummy_frame_this_id,
409 dummy_frame_prev_register
494cca16
AC
410};
411
412const struct frame_unwind *
413dummy_frame_p (CORE_ADDR pc)
414{
415 if (DEPRECATED_PC_IN_CALL_DUMMY_P ()
416 ? DEPRECATED_PC_IN_CALL_DUMMY (pc, 0, 0)
417 : pc_in_dummy_frame (pc))
418 return &dummy_frame_unwind;
419 else
420 return NULL;
421}
This page took 0.08023 seconds and 4 git commands to generate.