OSDN Git Service

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