]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dummy-frame.c
Index: ChangeLog
[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,
4ea2acf0
AC
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free
5 Software Foundation, Inc.
9c1412c1
AC
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
90ba813f
AC
35static int pc_in_dummy_frame (CORE_ADDR pc);
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 47 CORE_ADDR pc;
9c1412c1 48 CORE_ADDR top;
c689142b 49 struct frame_id id;
9c1412c1
AC
50 struct regcache *regcache;
51
52 /* Address range of the call dummy code. Look for PC in the range
53 [LO..HI) (after allowing for DECR_PC_AFTER_BREAK). */
54 CORE_ADDR call_lo;
55 CORE_ADDR call_hi;
56};
57
58static struct dummy_frame *dummy_frame_stack = NULL;
59
30a4a8e0 60/* Function: pc_in_call_dummy (pc)
9c1412c1
AC
61
62 Return true if the PC falls in a dummy frame created by gdb for an
63 inferior call. The code below which allows DECR_PC_AFTER_BREAK is
64 for infrun.c, which may give the function a PC without that
65 subtracted out. */
66
67int
30a4a8e0 68deprecated_pc_in_call_dummy (CORE_ADDR pc)
5e0f933e
AC
69{
70 return pc_in_dummy_frame (pc);
71}
72
73/* Return non-zero if the PC falls in a dummy frame.
74
75 The code below which allows DECR_PC_AFTER_BREAK is for infrun.c,
76 which may give the function a PC without that subtracted out.
77
78 FIXME: cagney/2002-11-23: This is silly. Surely "infrun.c" can
79 figure out what the real PC (as in the resume address) is BEFORE
e4a2df64 80 calling this function. */
5e0f933e 81
90ba813f 82static int
5e0f933e 83pc_in_dummy_frame (CORE_ADDR pc)
9c1412c1
AC
84{
85 struct dummy_frame *dummyframe;
86 for (dummyframe = dummy_frame_stack;
87 dummyframe != NULL;
88 dummyframe = dummyframe->next)
89 {
90 if ((pc >= dummyframe->call_lo)
91 && (pc < dummyframe->call_hi + DECR_PC_AFTER_BREAK))
92 return 1;
93 }
94 return 0;
95}
96
9c1412c1
AC
97/* Save all the registers on the dummy frame stack. Most ports save the
98 registers on the target stack. This results in lots of unnecessary memory
99 references, which are slow when debugging via a serial line. Instead, we
100 save all the registers internally, and never write them to the stack. The
101 registers get restored when the called function returns to the entry point,
102 where a breakpoint is laying in wait. */
103
104void
105generic_push_dummy_frame (void)
106{
107 struct dummy_frame *dummy_frame;
8b36eed8 108 CORE_ADDR fp = get_frame_base (get_current_frame ());
9c1412c1
AC
109
110 /* check to see if there are stale dummy frames,
111 perhaps left over from when a longjump took us out of a
112 function that was called by the debugger */
113
114 dummy_frame = dummy_frame_stack;
115 while (dummy_frame)
ff65ac78
AC
116 if (gdbarch_inner_than (current_gdbarch, dummy_frame->top, fp))
117 /* stale -- destroy! */
9c1412c1
AC
118 {
119 dummy_frame_stack = dummy_frame->next;
120 regcache_xfree (dummy_frame->regcache);
121 xfree (dummy_frame);
122 dummy_frame = dummy_frame_stack;
123 }
124 else
125 dummy_frame = dummy_frame->next;
126
127 dummy_frame = xmalloc (sizeof (struct dummy_frame));
a81dcb05 128 dummy_frame->regcache = frame_save_as_regcache (get_current_frame ());
9c1412c1
AC
129
130 dummy_frame->pc = read_pc ();
9c1412c1 131 dummy_frame->top = 0;
c689142b 132 dummy_frame->id = get_frame_id (get_current_frame ());
9c1412c1
AC
133 dummy_frame->next = dummy_frame_stack;
134 dummy_frame_stack = dummy_frame;
135}
136
137void
138generic_save_dummy_frame_tos (CORE_ADDR sp)
139{
140 dummy_frame_stack->top = sp;
141}
142
143/* Record the upper/lower bounds on the address of the call dummy. */
144
145void
146generic_save_call_dummy_addr (CORE_ADDR lo, CORE_ADDR hi)
147{
148 dummy_frame_stack->call_lo = lo;
149 dummy_frame_stack->call_hi = hi;
150}
151
d67ec5db
AC
152/* Return the dummy frame cache, it contains both the ID, and a
153 pointer to the regcache. */
154struct dummy_frame_cache
155{
156 struct frame_id this_id;
157 struct regcache *prev_regcache;
158};
159
160int
161dummy_frame_sniffer (const struct frame_unwind *self,
162 struct frame_info *next_frame,
163 void **this_prologue_cache)
164{
165 struct dummy_frame *dummyframe;
166 struct frame_id this_id;
167
168 /* When unwinding a normal frame, the stack structure is determined
169 by analyzing the frame's function's code (be it using brute force
170 prologue analysis, or the dwarf2 CFI). In the case of a dummy
171 frame, that simply isn't possible. The PC is either the program
172 entry point, or some random address on the stack. Trying to use
173 that PC to apply standard frame ID unwind techniques is just
174 asking for trouble. */
175 /* Use an architecture specific method to extract the prev's dummy
176 ID from the next frame. Note that this method uses
177 frame_register_unwind to obtain the register values needed to
178 determine the dummy frame's ID. */
179 this_id = gdbarch_unwind_dummy_id (get_frame_arch (next_frame), next_frame);
180
181 /* Use that ID to find the corresponding cache entry. */
182 for (dummyframe = dummy_frame_stack;
183 dummyframe != NULL;
184 dummyframe = dummyframe->next)
185 {
186 /* Does the PC fall within the dummy frame's breakpoint
187 instruction. If not, discard this one. */
188 if (!(this_id.code_addr >= dummyframe->call_lo
189 && this_id.code_addr < dummyframe->call_hi))
190 continue;
191 /* Does the FP match? "infcall.c" explicitly saved the
192 top-of-stack before the inferior function call, assume
193 unwind_dummy_id() returns that same stack value. */
194 if (this_id.stack_addr != dummyframe->top)
195 continue;
196 /* The FP matches this dummy frame. */
197 {
198 struct dummy_frame_cache *cache;
199 cache = FRAME_OBSTACK_ZALLOC (struct dummy_frame_cache);
200 cache->prev_regcache = dummyframe->regcache;
201 cache->this_id = this_id;
202 (*this_prologue_cache) = cache;
203 return 1;
204 }
205 }
206 return 0;
207}
208
9c1412c1
AC
209/* Given a call-dummy dummy-frame, return the registers. Here the
210 register value is taken from the local copy of the register buffer. */
211
494cca16 212static void
6dc42492
AC
213dummy_frame_prev_register (struct frame_info *next_frame,
214 void **this_prologue_cache,
215 int regnum, int *optimized,
216 enum lval_type *lvalp, CORE_ADDR *addrp,
217 int *realnum, void *bufferp)
9c1412c1 218{
d67ec5db
AC
219 /* The dummy-frame sniffer always fills in the cache. */
220 struct dummy_frame_cache *cache = (*this_prologue_cache);
221 gdb_assert (cache != NULL);
9c1412c1
AC
222
223 /* Describe the register's location. Generic dummy frames always
224 have the register value in an ``expression''. */
225 *optimized = 0;
226 *lvalp = not_lval;
227 *addrp = 0;
228 *realnum = -1;
229
230 /* If needed, find and return the value of the register. */
231 if (bufferp != NULL)
232 {
9c1412c1
AC
233 /* Return the actual value. */
234 /* Use the regcache_cooked_read() method so that it, on the fly,
235 constructs either a raw or pseudo register from the raw
236 register cache. */
d67ec5db 237 regcache_cooked_read (cache->prev_regcache, regnum, bufferp);
9c1412c1
AC
238 }
239}
240
6dc42492
AC
241/* Assuming that THIS frame is a dummy (remember, the NEXT and not
242 THIS frame is passed in), return the ID of THIS frame. That ID is
243 determined by examining the NEXT frame's unwound registers using
244 the method unwind_dummy_id(). As a side effect, THIS dummy frame's
245 dummy cache is located and and saved in THIS_PROLOGUE_CACHE. */
494cca16
AC
246
247static void
6dc42492
AC
248dummy_frame_this_id (struct frame_info *next_frame,
249 void **this_prologue_cache,
250 struct frame_id *this_id)
c689142b 251{
d67ec5db
AC
252 /* The dummy-frame sniffer always fills in the cache. */
253 struct dummy_frame_cache *cache = (*this_prologue_cache);
254 gdb_assert (cache != NULL);
255 (*this_id) = cache->this_id;
c689142b
AC
256}
257
d67ec5db 258static const struct frame_unwind dummy_frame_unwinder =
494cca16 259{
7df05f2b 260 DUMMY_FRAME,
6dc42492 261 dummy_frame_this_id,
d67ec5db
AC
262 dummy_frame_prev_register,
263 NULL,
264 dummy_frame_sniffer,
494cca16
AC
265};
266
d67ec5db
AC
267const struct frame_unwind *const dummy_frame_unwind = {
268 &dummy_frame_unwinder
269};
00905d52
AC
270
271static void
272fprint_dummy_frames (struct ui_file *file)
273{
274 struct dummy_frame *s;
275 for (s = dummy_frame_stack; s != NULL; s = s->next)
276 {
277 gdb_print_host_address (s, file);
278 fprintf_unfiltered (file, ":");
279 fprintf_unfiltered (file, " pc=0x%s", paddr (s->pc));
00905d52
AC
280 fprintf_unfiltered (file, " top=0x%s", paddr (s->top));
281 fprintf_unfiltered (file, " id=");
282 fprint_frame_id (file, s->id);
283 fprintf_unfiltered (file, " call_lo=0x%s", paddr (s->call_lo));
284 fprintf_unfiltered (file, " call_hi=0x%s", paddr (s->call_hi));
285 fprintf_unfiltered (file, "\n");
286 }
287}
288
289static void
290maintenance_print_dummy_frames (char *args, int from_tty)
291{
292 if (args == NULL)
293 fprint_dummy_frames (gdb_stdout);
294 else
295 {
296 struct ui_file *file = gdb_fopen (args, "w");
297 if (file == NULL)
298 perror_with_name ("maintenance print dummy-frames");
299 fprint_dummy_frames (file);
300 ui_file_delete (file);
301 }
302}
303
304extern void _initialize_dummy_frame (void);
305
306void
307_initialize_dummy_frame (void)
308{
309 add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames,
310 "Print the contents of the internal dummy-frame stack.",
311 &maintenanceprintlist);
312
313}