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