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