2002-11-19 Andrew Cagney <ac131313@redhat.com>
[external/binutils.git] / gdb / frame.h
1 /* Definitions for dealing with stack frames, for GDB, the GNU debugger.
2
3    Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996,
4    1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #if !defined (FRAME_H)
24 #define FRAME_H 1
25
26 /* The frame object.  */
27
28 struct frame_info;
29
30 /* The frame object's ID.  This provides a per-frame unique identifier
31    that can be used to relocate a `struct frame_info' after a target
32    resume or a frame cache destruct (assuming the target hasn't
33    unwound the stack past that frame - a problem handled elsewhere).  */
34
35 struct frame_id
36 {
37   /* The frame's address.  This should be constant through out the
38      lifetime of a frame.  */
39   /* NOTE: cagney/2002-11-16: The ia64 has two stacks and hence two
40      frame bases.  This will need to be expanded to accomodate that.  */
41   CORE_ADDR base;
42   /* The frame's current PC.  While the PC within the function may
43      change, the function that contains the PC does not.  Should this
44      instead be the frame's function?  */
45   CORE_ADDR pc;
46 };
47
48 /* For every stopped thread, GDB tracks two frames: current and
49    selected.  Current frame is the inner most frame of the selected
50    thread.  Selected frame is the frame currently being examined via
51    the GDB CLI (selected using `up', `down', ...).  The frames are
52    created on-demand (via get_prev_frame()) and then held in a frame
53    cache.  Provide mechanims for controlling these frames.  */
54 /* FIXME: cagney/2002-11-14: At any time, only one thread's selected
55    and current frame can be active.  Switching threads causes gdb to
56    discard all that cached frame information.  Ulgh!  Instead, current
57    and selected frame should be bound to a thread.  */
58
59 extern struct frame_info *selected_frame;
60 extern void select_frame (struct frame_info *);
61 extern void set_current_frame (struct frame_info *);
62 extern struct frame_info *get_current_frame (void);
63
64 /* Invalidates the frame cache.  */
65 extern void flush_cached_frames (void);
66
67 /* Flushes the frame cache and then selects the inner most (aka
68    current) frame - it changes selected frame.  */
69 /* FIXME: cagney/2002-11-14: Should this re-select the selected frame
70    from before the flush?  */
71 extern void reinit_frame_cache (void);
72
73 /* Given a FRAME, return the next (more inner, younger) or previous
74    (more outer, older) frame.  */
75 extern struct frame_info *get_prev_frame (struct frame_info *);
76 extern struct frame_info *get_next_frame (struct frame_info *);
77
78 /* Given a frame's ID, relocate the frame.  Returns NULL if the frame
79    is not found.  */
80 extern struct frame_info *frame_find_by_id (struct frame_id id);
81
82 /* Base attributes of a frame: */
83
84 /* The frame's `resume' address.  Where the program will resume in
85    this frame.  */
86 extern CORE_ADDR get_frame_pc (struct frame_info *);
87
88 /* Return the frame address from FI.  Except in the machine-dependent
89    *FRAME* macros, a frame address has no defined meaning other than
90    as a magic cookie which identifies a frame over calls to the
91    inferior (um, SEE NOTE BELOW).  The only known exception is
92    inferior.h (PC_IN_CALL_DUMMY) [ON_STACK]; see comments there.  You
93    cannot assume that a frame address contains enough information to
94    reconstruct the frame; if you want more than just to identify the
95    frame (e.g. be able to fetch variables relative to that frame),
96    then save the whole struct frame_info (and the next struct
97    frame_info, since the latter is used for fetching variables on some
98    machines) (um, again SEE NOTE BELOW).
99
100    NOTE: cagney/2002-11-18: Actually, the frame address isn't
101    sufficient for identifying a frame, and the counter examples are
102    wrong!
103
104    Code that needs to (re)identify a frame must use get_frame_id() and
105    frame_find_by_id() (and in the future, a frame_compare() function
106    instead of INNER_THAN()).  Two reasons: an architecture (e.g.,
107    ia64) can have more than one frame address (due to multiple stack
108    pointers) (frame ID is going to be expanded to accomodate this);
109    successive frameless function calls can only be differientated by
110    comparing both the frame's base and the frame's enclosing function
111    (frame_find_by_id() is going to be modified to perform this test). 
112
113    The generic dummy frame version of PC_IN_CALL_DUMMY() is able to
114    identify a dummy frame using only the PC value.  So the frame
115    address is not needed.  In fact, most PC_IN_CALL_DUMMY() calls now
116    pass zero as the frame/sp values as the caller knows that those
117    values won't be used.  Once all architectures are using generic
118    dummy frames, PC_IN_CALL_DUMMY() can drop the sp/frame parameters.
119    When it comes to finding a dummy frame, the next frame's frame ID
120    (with out duing an unwind) can be used (ok, could if it wasn't for
121    the need to change the way the PPC defined frame base in a strange
122    way).
123
124    Modern architectures should be using something like dwarf2's
125    location expression to describe where a variable lives.  Such
126    expressions specify their own debug info centric frame address.
127    Consequently, a generic frame address is pretty meaningless.  */
128
129 extern CORE_ADDR get_frame_base (struct frame_info *);
130
131 /* Return the per-frame unique identifer.  Can be used to relocate a
132    frame after a frame cache flush (and other similar operations).  */
133 extern void get_frame_id (struct frame_info *fi, struct frame_id *id);
134
135 /* The frame's level: 0 for innermost, 1 for its caller, ...; or -1
136    for an invalid frame).  */
137 extern int frame_relative_level (struct frame_info *fi);
138
139 /* Return the frame's type.  Some are real, some are signal
140    trampolines, and some are completly artificial (dummy).  */
141
142 enum frame_type
143 {
144   /* A true stack frame, created by the target program during normal
145      execution.  */
146   NORMAL_FRAME,
147   /* A fake frame, created by GDB when performing an inferior function
148      call.  */
149   DUMMY_FRAME,
150   /* In a signal handler, various OSs handle this in various ways.
151      The main thing is that the frame may be far from normal.  */
152   SIGTRAMP_FRAME
153 };
154 extern enum frame_type get_frame_type (struct frame_info *);
155
156 /* FIXME: cagney/2002-11-10: Some targets want to directly mark a
157    frame as being of a specific type.  This shouldn't be necessary.
158    PC_IN_SIGTRAMP() indicates a SIGTRAMP_FRAME and PC_IN_CALL_DUMMY()
159    indicates a DUMMY_FRAME.  I suspect the real problem here is that
160    get_prev_frame() only sets initialized after INIT_EXTRA_FRAME_INFO
161    as been called.  Consequently, some targets found that the frame's
162    type was wrong and tried to fix it.  The correct fix is to modify
163    get_prev_frame() so that it initializes the frame's type before
164    calling any other functions.  */
165 extern void deprecated_set_frame_type (struct frame_info *,
166                                        enum frame_type type);
167
168 /* Unwind the stack frame so that the value of REGNUM, in the previous
169    (up, older) frame is returned.  If VALUEP is NULL, don't
170    fetch/compute the value.  Instead just return the location of the
171    value.  */
172 extern void frame_register_unwind (struct frame_info *frame, int regnum,
173                                    int *optimizedp, enum lval_type *lvalp,
174                                    CORE_ADDR *addrp, int *realnump,
175                                    void *valuep);
176
177 /* More convenient interface to frame_register_unwind().  */
178 /* NOTE: cagney/2002-09-13: Return void as one day these functions may
179    be changed to return an indication that the read succeeded.  */
180
181 extern void frame_unwind_signed_register (struct frame_info *frame,
182                                           int regnum, LONGEST *val);
183
184 extern void frame_unwind_unsigned_register (struct frame_info *frame,
185                                             int regnum, ULONGEST *val);
186
187 /* Get the value of the register that belongs to this FRAME.  This
188    function is a wrapper to the call sequence ``frame_unwind_register
189    (get_next_frame (FRAME))''.  As per frame_register_unwind(), if
190    VALUEP is NULL, the registers value is not fetched/computed.  */
191
192 extern void frame_register (struct frame_info *frame, int regnum,
193                             int *optimizedp, enum lval_type *lvalp,
194                             CORE_ADDR *addrp, int *realnump,
195                             void *valuep);
196
197 /* More convenient interface to frame_register().  */
198 /* NOTE: cagney/2002-09-13: Return void as one day these functions may
199    be changed to return an indication that the read succeeded.  */
200
201 extern void frame_read_signed_register (struct frame_info *frame,
202                                         int regnum, LONGEST *val);
203
204 extern void frame_read_unsigned_register (struct frame_info *frame,
205                                           int regnum, ULONGEST *val);
206
207 /* Map between a frame register number and its name.  A frame register
208    space is a superset of the cooked register space --- it also
209    includes builtin registers.  */
210
211 extern int frame_map_name_to_regnum (const char *name, int strlen);
212 extern const char *frame_map_regnum_to_name (int regnum);
213
214 /* Unwind the PC.  Strictly speaking return the resume address of the
215    calling frame.  For GDB, `pc' is the resume address and not a
216    specific register.  */
217
218 extern CORE_ADDR frame_pc_unwind (struct frame_info *frame);
219
220 \f
221 /* Return the location (and possibly value) of REGNUM for the previous
222    (older, up) frame.  All parameters except VALUEP can be assumed to
223    be non NULL.  When VALUEP is NULL, just the location of the
224    register should be returned.
225
226    UNWIND_CACHE is provided as mechanism for implementing a per-frame
227    local cache.  It's initial value being NULL.  Memory for that cache
228    should be allocated using frame_obstack_alloc().
229
230    Register window architectures (eg SPARC) should note that REGNUM
231    identifies the register for the previous frame.  For instance, a
232    request for the value of "o1" for the previous frame would be found
233    in the register "i1" in this FRAME.  */
234
235 typedef void (frame_register_unwind_ftype) (struct frame_info *frame,
236                                             void **unwind_cache,
237                                             int regnum,
238                                             int *optimized,
239                                             enum lval_type *lvalp,
240                                             CORE_ADDR *addrp,
241                                             int *realnump,
242                                             void *valuep);
243
244 /* Same as for registers above, but return the address at which the
245    calling frame would resume.  */
246
247 typedef CORE_ADDR (frame_pc_unwind_ftype) (struct frame_info *frame,
248                                            void **unwind_cache);
249
250 /* Describe the saved registers of a frame.  */
251
252 #if defined (EXTRA_FRAME_INFO) || defined (FRAME_FIND_SAVED_REGS)
253 /* XXXX - deprecated */
254 struct frame_saved_regs
255   {
256     /* For each register R (except the SP), regs[R] is the address at
257        which it was saved on entry to the frame, or zero if it was not
258        saved on entry to this frame.  This includes special registers
259        such as pc and fp saved in special ways in the stack frame.
260
261        regs[SP_REGNUM] is different.  It holds the actual SP, not the
262        address at which it was saved.  */
263
264     CORE_ADDR regs[NUM_REGS];
265   };
266 #endif
267
268 /* We keep a cache of stack frames, each of which is a "struct
269    frame_info".  The innermost one gets allocated (in
270    wait_for_inferior) each time the inferior stops; current_frame
271    points to it.  Additional frames get allocated (in
272    get_prev_frame) as needed, and are chained through the next
273    and prev fields.  Any time that the frame cache becomes invalid
274    (most notably when we execute something, but also if we change how
275    we interpret the frames (e.g. "set heuristic-fence-post" in
276    mips-tdep.c, or anything which reads new symbols)), we should call
277    reinit_frame_cache.  */
278
279 struct frame_info
280   {
281     /* Nominal address of the frame described.  See comments at
282        get_frame_base() about what this means outside the *FRAME*
283        macros; in the *FRAME* macros, it can mean whatever makes most
284        sense for this machine.  */
285     CORE_ADDR frame;
286
287     /* Address at which execution is occurring in this frame.
288        For the innermost frame, it's the current pc.
289        For other frames, it is a pc saved in the next frame.  */
290     CORE_ADDR pc;
291
292     /* Level of this frame.  The inner-most (youngest) frame is at
293        level 0.  As you move towards the outer-most (oldest) frame,
294        the level increases.  This is a cached value.  It could just as
295        easily be computed by counting back from the selected frame to
296        the inner most frame.  */
297     /* NOTE: cagney/2002-04-05: Perhaphs a level of ``-1'' should be
298        reserved to indicate a bogus frame - one that has been created
299        just to keep GDB happy (GDB always needs a frame).  For the
300        moment leave this as speculation.  */
301     int level;
302
303     /* The frame's type.  */
304     enum frame_type type;
305
306     /* For each register, address of where it was saved on entry to
307        the frame, or zero if it was not saved on entry to this frame.
308        This includes special registers such as pc and fp saved in
309        special ways in the stack frame.  The SP_REGNUM is even more
310        special, the address here is the sp for the previous frame, not
311        the address where the sp was saved.  */
312     /* Allocated by frame_saved_regs_zalloc () which is called /
313        initialized by FRAME_INIT_SAVED_REGS(). */
314     CORE_ADDR *saved_regs;      /*NUM_REGS + NUM_PSEUDO_REGS*/
315
316 #ifdef EXTRA_FRAME_INFO
317     /* XXXX - deprecated */
318     /* Anything extra for this structure that may have been defined
319        in the machine dependent files. */
320       EXTRA_FRAME_INFO
321 #endif
322
323     /* Anything extra for this structure that may have been defined
324        in the machine dependent files. */
325     /* Allocated by frame_obstack_alloc () which is called /
326        initialized by INIT_EXTRA_FRAME_INFO */
327     struct frame_extra_info *extra_info;
328
329     /* If dwarf2 unwind frame informations is used, this structure holds all
330        related unwind data.  */
331     struct context *context;
332
333     /* Unwind cache shared between the unwind functions - they had
334        better all agree as to the contents.  */
335     void *unwind_cache;
336
337     /* See description above.  The previous frame's registers.  */
338     frame_register_unwind_ftype *register_unwind;
339
340     /* See description above.  The previous frame's resume address.
341        Save the previous PC in a local cache.  */
342     frame_pc_unwind_ftype *pc_unwind;
343     int pc_unwind_cache_p;
344     CORE_ADDR pc_unwind_cache;
345
346     /* Pointers to the next (down, inner, younger) and previous (up,
347        outer, older) frame_info's in the frame cache.  */
348     struct frame_info *next; /* down, inner, younger */
349     int prev_p;
350     struct frame_info *prev; /* up, outer, older */
351   };
352
353 /* Values for the source flag to be used in print_frame_info_base(). */
354 enum print_what
355   { 
356     /* Print only the source line, like in stepi. */
357     SRC_LINE = -1, 
358     /* Print only the location, i.e. level, address (sometimes)
359        function, args, file, line, line num. */
360     LOCATION,
361     /* Print both of the above. */
362     SRC_AND_LOC, 
363     /* Print location only, but always include the address. */
364     LOC_AND_ADDRESS 
365   };
366
367 /* Allocate additional space for appendices to a struct frame_info.
368    NOTE: Much of GDB's code works on the assumption that the allocated
369    saved_regs[] array is the size specified below.  If you try to make
370    that array smaller, GDB will happily walk off its end. */
371
372 #ifdef SIZEOF_FRAME_SAVED_REGS
373 #error "SIZEOF_FRAME_SAVED_REGS can not be re-defined"
374 #endif
375 #define SIZEOF_FRAME_SAVED_REGS \
376         (sizeof (CORE_ADDR) * (NUM_REGS+NUM_PSEUDO_REGS))
377
378 extern void *frame_obstack_alloc (unsigned long size);
379 extern void frame_saved_regs_zalloc (struct frame_info *);
380
381 /* Define a default FRAME_CHAIN_VALID, in the form that is suitable for most
382    targets.  If FRAME_CHAIN_VALID returns zero it means that the given frame
383    is the outermost one and has no caller.
384
385    XXXX - both default and alternate frame_chain_valid functions are
386    deprecated.  New code should use dummy frames and one of the
387    generic functions. */
388
389 extern int file_frame_chain_valid (CORE_ADDR, struct frame_info *);
390 extern int func_frame_chain_valid (CORE_ADDR, struct frame_info *);
391 extern int nonnull_frame_chain_valid (CORE_ADDR, struct frame_info *);
392 extern int generic_file_frame_chain_valid (CORE_ADDR, struct frame_info *);
393 extern int generic_func_frame_chain_valid (CORE_ADDR, struct frame_info *);
394 extern void generic_save_dummy_frame_tos (CORE_ADDR sp);
395
396 extern struct frame_info *create_new_frame (CORE_ADDR, CORE_ADDR);
397
398
399 #ifdef FRAME_FIND_SAVED_REGS
400 /* XXX - deprecated */
401 #define FRAME_INIT_SAVED_REGS(FI) get_frame_saved_regs (FI, NULL)
402 extern void get_frame_saved_regs (struct frame_info *,
403                                   struct frame_saved_regs *);
404 #endif
405
406 extern struct block *get_frame_block (struct frame_info *,
407                                       CORE_ADDR *addr_in_block);
408
409 extern struct block *get_current_block (CORE_ADDR *addr_in_block);
410
411 extern struct block *get_selected_block (CORE_ADDR *addr_in_block);
412
413 extern struct symbol *get_frame_function (struct frame_info *);
414
415 extern CORE_ADDR frame_address_in_block (struct frame_info *);
416
417 extern CORE_ADDR get_pc_function_start (CORE_ADDR);
418
419 extern struct block *block_for_pc (CORE_ADDR);
420
421 extern struct block *block_for_pc_sect (CORE_ADDR, asection *);
422
423 extern int frameless_look_for_prologue (struct frame_info *);
424
425 extern void print_frame_args (struct symbol *, struct frame_info *,
426                               int, struct ui_file *);
427
428 extern struct frame_info *find_relative_frame (struct frame_info *, int *);
429
430 extern void show_and_print_stack_frame (struct frame_info *fi, int level,
431                                         int source);
432
433 extern void print_stack_frame (struct frame_info *, int, int);
434
435 extern void print_only_stack_frame (struct frame_info *, int, int);
436
437 extern void show_stack_frame (struct frame_info *);
438
439 extern void print_frame_info (struct frame_info *, int, int, int);
440
441 extern void show_frame_info (struct frame_info *, int, int, int);
442
443 extern struct frame_info *block_innermost_frame (struct block *);
444
445 /* NOTE: cagney/2002-09-13: There is no need for this function.
446    Instead either of frame_unwind_signed_register() or
447    frame_unwind_unsigned_register() can be used.  */
448 extern CORE_ADDR deprecated_read_register_dummy (CORE_ADDR pc,
449                                                  CORE_ADDR fp, int);
450 extern void generic_push_dummy_frame (void);
451 extern void generic_pop_current_frame (void (*)(struct frame_info *));
452 extern void generic_pop_dummy_frame (void);
453
454 extern int generic_pc_in_call_dummy (CORE_ADDR pc,
455                                      CORE_ADDR sp, CORE_ADDR fp);
456
457 /* NOTE: cagney/2002-06-26: Targets should no longer use this
458    function.  Instead, the contents of a dummy frames registers can be
459    obtained by applying: frame_register_unwind to the dummy frame; or
460    get_saved_register to the next outer frame.  */
461
462 extern char *deprecated_generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp);
463
464 extern void generic_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun,
465                                     int nargs, struct value **args,
466                                     struct type *type, int gcc_p);
467
468 /* The function generic_get_saved_register() has been made obsolete.
469    GET_SAVED_REGISTER now defaults to the recursive equivalent -
470    generic_unwind_get_saved_register() - so there is no need to even
471    set GET_SAVED_REGISTER.  Architectures that need to override the
472    register unwind mechanism should modify frame->unwind().  */
473 extern void deprecated_generic_get_saved_register (char *, int *, CORE_ADDR *,
474                                                    struct frame_info *, int,
475                                                    enum lval_type *);
476
477 extern void generic_save_call_dummy_addr (CORE_ADDR lo, CORE_ADDR hi);
478
479 extern void get_saved_register (char *raw_buffer, int *optimized,
480                                 CORE_ADDR * addrp,
481                                 struct frame_info *frame,
482                                 int regnum, enum lval_type *lval);
483
484 extern int frame_register_read (struct frame_info *frame, int regnum,
485                                 void *buf);
486
487 /* From stack.c.  */
488 extern void args_info (char *, int);
489
490 extern void locals_info (char *, int);
491
492 extern void (*selected_frame_level_changed_hook) (int);
493
494 extern void return_command (char *, int);
495
496 #endif /* !defined (FRAME_H)  */