2003-02-19 David Carlton <carlton@math.stanford.edu>
[external/binutils.git] / gdb / blockframe.c
1 /* Get info from stack frames; convert between frames, blocks,
2    functions and pc values.
3
4    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
5    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
6    Foundation, Inc.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24
25 #include "defs.h"
26 #include "symtab.h"
27 #include "bfd.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "frame.h"
31 #include "gdbcore.h"
32 #include "value.h"              /* for read_register */
33 #include "target.h"             /* for target_has_stack */
34 #include "inferior.h"           /* for read_pc */
35 #include "annotate.h"
36 #include "regcache.h"
37 #include "gdb_assert.h"
38 #include "dummy-frame.h"
39 #include "command.h"
40 #include "gdbcmd.h"
41 #include "block.h"
42
43 /* Prototypes for exported functions. */
44
45 void _initialize_blockframe (void);
46
47 /* Is ADDR inside the startup file?  Note that if your machine
48    has a way to detect the bottom of the stack, there is no need
49    to call this function from FRAME_CHAIN_VALID; the reason for
50    doing so is that some machines have no way of detecting bottom
51    of stack. 
52
53    A PC of zero is always considered to be the bottom of the stack. */
54
55 int
56 inside_entry_file (CORE_ADDR addr)
57 {
58   if (addr == 0)
59     return 1;
60   if (symfile_objfile == 0)
61     return 0;
62   if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT)
63     {
64       /* Do not stop backtracing if the pc is in the call dummy
65          at the entry point.  */
66       /* FIXME: Won't always work with zeros for the last two arguments */
67       if (DEPRECATED_PC_IN_CALL_DUMMY (addr, 0, 0))
68         return 0;
69     }
70   return (addr >= symfile_objfile->ei.entry_file_lowpc &&
71           addr < symfile_objfile->ei.entry_file_highpc);
72 }
73
74 /* Test a specified PC value to see if it is in the range of addresses
75    that correspond to the main() function.  See comments above for why
76    we might want to do this.
77
78    Typically called from FRAME_CHAIN_VALID.
79
80    A PC of zero is always considered to be the bottom of the stack. */
81
82 int
83 inside_main_func (CORE_ADDR pc)
84 {
85   if (pc == 0)
86     return 1;
87   if (symfile_objfile == 0)
88     return 0;
89
90   /* If the addr range is not set up at symbol reading time, set it up now.
91      This is for FRAME_CHAIN_VALID_ALTERNATE. I do this for coff, because
92      it is unable to set it up and symbol reading time. */
93
94   if (symfile_objfile->ei.main_func_lowpc == INVALID_ENTRY_LOWPC &&
95       symfile_objfile->ei.main_func_highpc == INVALID_ENTRY_HIGHPC)
96     {
97       struct symbol *mainsym;
98
99       mainsym = lookup_symbol (main_name (), NULL, VAR_NAMESPACE, NULL, NULL);
100       if (mainsym && SYMBOL_CLASS (mainsym) == LOC_BLOCK)
101         {
102           symfile_objfile->ei.main_func_lowpc =
103             BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym));
104           symfile_objfile->ei.main_func_highpc =
105             BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym));
106         }
107     }
108   return (symfile_objfile->ei.main_func_lowpc <= pc &&
109           symfile_objfile->ei.main_func_highpc > pc);
110 }
111
112 /* Test a specified PC value to see if it is in the range of addresses
113    that correspond to the process entry point function.  See comments
114    in objfiles.h for why we might want to do this.
115
116    Typically called from FRAME_CHAIN_VALID.
117
118    A PC of zero is always considered to be the bottom of the stack. */
119
120 int
121 inside_entry_func (CORE_ADDR pc)
122 {
123   if (pc == 0)
124     return 1;
125   if (symfile_objfile == 0)
126     return 0;
127   if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT)
128     {
129       /* Do not stop backtracing if the pc is in the call dummy
130          at the entry point.  */
131       /* FIXME: Won't always work with zeros for the last two arguments */
132       if (DEPRECATED_PC_IN_CALL_DUMMY (pc, 0, 0))
133         return 0;
134     }
135   return (symfile_objfile->ei.entry_func_lowpc <= pc &&
136           symfile_objfile->ei.entry_func_highpc > pc);
137 }
138
139 /* Return nonzero if the function for this frame lacks a prologue.  Many
140    machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
141    function.  */
142
143 int
144 frameless_look_for_prologue (struct frame_info *frame)
145 {
146   CORE_ADDR func_start, after_prologue;
147
148   func_start = get_pc_function_start (get_frame_pc (frame));
149   if (func_start)
150     {
151       func_start += FUNCTION_START_OFFSET;
152       /* This is faster, since only care whether there *is* a
153          prologue, not how long it is.  */
154       return PROLOGUE_FRAMELESS_P (func_start);
155     }
156   else if (get_frame_pc (frame) == 0)
157     /* A frame with a zero PC is usually created by dereferencing a
158        NULL function pointer, normally causing an immediate core dump
159        of the inferior. Mark function as frameless, as the inferior
160        has no chance of setting up a stack frame.  */
161     return 1;
162   else
163     /* If we can't find the start of the function, we don't really
164        know whether the function is frameless, but we should be able
165        to get a reasonable (i.e. best we can do under the
166        circumstances) backtrace by saying that it isn't.  */
167     return 0;
168 }
169
170 /* return the address of the PC for the given FRAME, ie the current PC value
171    if FRAME is the innermost frame, or the address adjusted to point to the
172    call instruction if not.  */
173
174 CORE_ADDR
175 frame_address_in_block (struct frame_info *frame)
176 {
177   CORE_ADDR pc = get_frame_pc (frame);
178
179   /* If we are not in the innermost frame, and we are not interrupted
180      by a signal, frame->pc points to the instruction following the
181      call. As a consequence, we need to get the address of the previous
182      instruction. Unfortunately, this is not straightforward to do, so
183      we just use the address minus one, which is a good enough
184      approximation.  */
185   /* FIXME: cagney/2002-11-10: Should this instead test for
186      NORMAL_FRAME?  A dummy frame (in fact all the abnormal frames)
187      save the PC value in the block.  */
188   if (get_next_frame (frame) != 0
189       && get_frame_type (get_next_frame (frame)) != SIGTRAMP_FRAME)
190     --pc;
191
192   return pc;
193 }
194
195 /* Return the innermost lexical block in execution
196    in a specified stack frame.  The frame address is assumed valid.
197
198    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
199    address we used to choose the block.  We use this to find a source
200    line, to decide which macro definitions are in scope.
201
202    The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
203    PC, and may not really be a valid PC at all.  For example, in the
204    caller of a function declared to never return, the code at the
205    return address will never be reached, so the call instruction may
206    be the very last instruction in the block.  So the address we use
207    to choose the block is actually one byte before the return address
208    --- hopefully pointing us at the call instruction, or its delay
209    slot instruction.  */
210
211 struct block *
212 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
213 {
214   const CORE_ADDR pc = frame_address_in_block (frame);
215
216   if (addr_in_block)
217     *addr_in_block = pc;
218
219   return block_for_pc (pc);
220 }
221
222 CORE_ADDR
223 get_pc_function_start (CORE_ADDR pc)
224 {
225   register struct block *bl;
226   register struct symbol *symbol;
227   register struct minimal_symbol *msymbol;
228   CORE_ADDR fstart;
229
230   if ((bl = block_for_pc (pc)) != NULL &&
231       (symbol = block_function (bl)) != NULL)
232     {
233       bl = SYMBOL_BLOCK_VALUE (symbol);
234       fstart = BLOCK_START (bl);
235     }
236   else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
237     {
238       fstart = SYMBOL_VALUE_ADDRESS (msymbol);
239       if (!find_pc_section (fstart))
240         return 0;
241     }
242   else
243     {
244       fstart = 0;
245     }
246   return (fstart);
247 }
248
249 /* Return the symbol for the function executing in frame FRAME.  */
250
251 struct symbol *
252 get_frame_function (struct frame_info *frame)
253 {
254   register struct block *bl = get_frame_block (frame, 0);
255   if (bl == 0)
256     return 0;
257   return block_function (bl);
258 }
259 \f
260
261 /* Return the function containing pc value PC in section SECTION.
262    Returns 0 if function is not known.  */
263
264 struct symbol *
265 find_pc_sect_function (CORE_ADDR pc, struct sec *section)
266 {
267   register struct block *b = block_for_pc_sect (pc, section);
268   if (b == 0)
269     return 0;
270   return block_function (b);
271 }
272
273 /* Return the function containing pc value PC.
274    Returns 0 if function is not known.  Backward compatibility, no section */
275
276 struct symbol *
277 find_pc_function (CORE_ADDR pc)
278 {
279   return find_pc_sect_function (pc, find_pc_mapped_section (pc));
280 }
281
282 /* These variables are used to cache the most recent result
283  * of find_pc_partial_function. */
284
285 static CORE_ADDR cache_pc_function_low = 0;
286 static CORE_ADDR cache_pc_function_high = 0;
287 static char *cache_pc_function_name = 0;
288 static struct sec *cache_pc_function_section = NULL;
289
290 /* Clear cache, e.g. when symbol table is discarded. */
291
292 void
293 clear_pc_function_cache (void)
294 {
295   cache_pc_function_low = 0;
296   cache_pc_function_high = 0;
297   cache_pc_function_name = (char *) 0;
298   cache_pc_function_section = NULL;
299 }
300
301 /* Finds the "function" (text symbol) that is smaller than PC but
302    greatest of all of the potential text symbols in SECTION.  Sets
303    *NAME and/or *ADDRESS conditionally if that pointer is non-null.
304    If ENDADDR is non-null, then set *ENDADDR to be the end of the
305    function (exclusive), but passing ENDADDR as non-null means that
306    the function might cause symbols to be read.  This function either
307    succeeds or fails (not halfway succeeds).  If it succeeds, it sets
308    *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
309    If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
310    returns 0.  */
311
312 int
313 find_pc_sect_partial_function (CORE_ADDR pc, asection *section, char **name,
314                                CORE_ADDR *address, CORE_ADDR *endaddr)
315 {
316   struct partial_symtab *pst;
317   struct symbol *f;
318   struct minimal_symbol *msymbol;
319   struct partial_symbol *psb;
320   struct obj_section *osect;
321   int i;
322   CORE_ADDR mapped_pc;
323
324   mapped_pc = overlay_mapped_address (pc, section);
325
326   if (mapped_pc >= cache_pc_function_low
327       && mapped_pc < cache_pc_function_high
328       && section == cache_pc_function_section)
329     goto return_cached_value;
330
331   /* If sigtramp is in the u area, it counts as a function (especially
332      important for step_1).  */
333   if (SIGTRAMP_START_P () && PC_IN_SIGTRAMP (mapped_pc, (char *) NULL))
334     {
335       cache_pc_function_low = SIGTRAMP_START (mapped_pc);
336       cache_pc_function_high = SIGTRAMP_END (mapped_pc);
337       cache_pc_function_name = "<sigtramp>";
338       cache_pc_function_section = section;
339       goto return_cached_value;
340     }
341
342   msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
343   pst = find_pc_sect_psymtab (mapped_pc, section);
344   if (pst)
345     {
346       /* Need to read the symbols to get a good value for the end address.  */
347       if (endaddr != NULL && !pst->readin)
348         {
349           /* Need to get the terminal in case symbol-reading produces
350              output.  */
351           target_terminal_ours_for_output ();
352           PSYMTAB_TO_SYMTAB (pst);
353         }
354
355       if (pst->readin)
356         {
357           /* Checking whether the msymbol has a larger value is for the
358              "pathological" case mentioned in print_frame_info.  */
359           f = find_pc_sect_function (mapped_pc, section);
360           if (f != NULL
361               && (msymbol == NULL
362                   || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
363                       >= SYMBOL_VALUE_ADDRESS (msymbol))))
364             {
365               cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
366               cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
367               cache_pc_function_name = SYMBOL_NAME (f);
368               cache_pc_function_section = section;
369               goto return_cached_value;
370             }
371         }
372       else
373         {
374           /* Now that static symbols go in the minimal symbol table, perhaps
375              we could just ignore the partial symbols.  But at least for now
376              we use the partial or minimal symbol, whichever is larger.  */
377           psb = find_pc_sect_psymbol (pst, mapped_pc, section);
378
379           if (psb
380               && (msymbol == NULL ||
381                   (SYMBOL_VALUE_ADDRESS (psb)
382                    >= SYMBOL_VALUE_ADDRESS (msymbol))))
383             {
384               /* This case isn't being cached currently. */
385               if (address)
386                 *address = SYMBOL_VALUE_ADDRESS (psb);
387               if (name)
388                 *name = SYMBOL_NAME (psb);
389               /* endaddr non-NULL can't happen here.  */
390               return 1;
391             }
392         }
393     }
394
395   /* Not in the normal symbol tables, see if the pc is in a known section.
396      If it's not, then give up.  This ensures that anything beyond the end
397      of the text seg doesn't appear to be part of the last function in the
398      text segment.  */
399
400   osect = find_pc_sect_section (mapped_pc, section);
401
402   if (!osect)
403     msymbol = NULL;
404
405   /* Must be in the minimal symbol table.  */
406   if (msymbol == NULL)
407     {
408       /* No available symbol.  */
409       if (name != NULL)
410         *name = 0;
411       if (address != NULL)
412         *address = 0;
413       if (endaddr != NULL)
414         *endaddr = 0;
415       return 0;
416     }
417
418   cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
419   cache_pc_function_name = SYMBOL_NAME (msymbol);
420   cache_pc_function_section = section;
421
422   /* Use the lesser of the next minimal symbol in the same section, or
423      the end of the section, as the end of the function.  */
424
425   /* Step over other symbols at this same address, and symbols in
426      other sections, to find the next symbol in this section with
427      a different address.  */
428
429   for (i = 1; SYMBOL_NAME (msymbol + i) != NULL; i++)
430     {
431       if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol)
432           && SYMBOL_BFD_SECTION (msymbol + i) == SYMBOL_BFD_SECTION (msymbol))
433         break;
434     }
435
436   if (SYMBOL_NAME (msymbol + i) != NULL
437       && SYMBOL_VALUE_ADDRESS (msymbol + i) < osect->endaddr)
438     cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
439   else
440     /* We got the start address from the last msymbol in the objfile.
441        So the end address is the end of the section.  */
442     cache_pc_function_high = osect->endaddr;
443
444  return_cached_value:
445
446   if (address)
447     {
448       if (pc_in_unmapped_range (pc, section))
449         *address = overlay_unmapped_address (cache_pc_function_low, section);
450       else
451         *address = cache_pc_function_low;
452     }
453
454   if (name)
455     *name = cache_pc_function_name;
456
457   if (endaddr)
458     {
459       if (pc_in_unmapped_range (pc, section))
460         {
461           /* Because the high address is actually beyond the end of
462              the function (and therefore possibly beyond the end of
463              the overlay), we must actually convert (high - 1) and
464              then add one to that. */
465
466           *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
467                                                    section);
468         }
469       else
470         *endaddr = cache_pc_function_high;
471     }
472
473   return 1;
474 }
475
476 /* Backward compatibility, no section argument.  */
477
478 int
479 find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
480                           CORE_ADDR *endaddr)
481 {
482   asection *section;
483
484   section = find_pc_overlay (pc);
485   return find_pc_sect_partial_function (pc, section, name, address, endaddr);
486 }
487
488 /* Return the innermost stack frame executing inside of BLOCK,
489    or NULL if there is no such frame.  If BLOCK is NULL, just return NULL.  */
490
491 struct frame_info *
492 block_innermost_frame (struct block *block)
493 {
494   struct frame_info *frame;
495   register CORE_ADDR start;
496   register CORE_ADDR end;
497   CORE_ADDR calling_pc;
498
499   if (block == NULL)
500     return NULL;
501
502   start = BLOCK_START (block);
503   end = BLOCK_END (block);
504
505   frame = NULL;
506   while (1)
507     {
508       frame = get_prev_frame (frame);
509       if (frame == NULL)
510         return NULL;
511       calling_pc = frame_address_in_block (frame);
512       if (calling_pc >= start && calling_pc < end)
513         return frame;
514     }
515 }
516
517 /* Are we in a call dummy?  The code below which allows DECR_PC_AFTER_BREAK
518    below is for infrun.c, which may give the macro a pc without that
519    subtracted out.  */
520
521 /* Is the PC in a call dummy?  SP and FRAME_ADDRESS are the bottom and
522    top of the stack frame which we are checking, where "bottom" and
523    "top" refer to some section of memory which contains the code for
524    the call dummy.  Calls to this macro assume that the contents of
525    SP_REGNUM and FP_REGNUM (or the saved values thereof), respectively,
526    are the things to pass.
527
528    This won't work on the 29k, where SP_REGNUM and FP_REGNUM don't
529    have that meaning, but the 29k doesn't use ON_STACK.  This could be
530    fixed by generalizing this scheme, perhaps by passing in a frame
531    and adding a few fields, at least on machines which need them for
532    DEPRECATED_PC_IN_CALL_DUMMY.
533
534    Something simpler, like checking for the stack segment, doesn't work,
535    since various programs (threads implementations, gcc nested function
536    stubs, etc) may either allocate stack frames in another segment, or
537    allocate other kinds of code on the stack.  */
538
539 int
540 deprecated_pc_in_call_dummy_on_stack (CORE_ADDR pc, CORE_ADDR sp,
541                                       CORE_ADDR frame_address)
542 {
543   return (INNER_THAN ((sp), (pc))
544           && (frame_address != 0)
545           && INNER_THAN ((pc), (frame_address)));
546 }
547
548 int
549 deprecated_pc_in_call_dummy_at_entry_point (CORE_ADDR pc, CORE_ADDR sp,
550                                             CORE_ADDR frame_address)
551 {
552   return ((pc) >= CALL_DUMMY_ADDRESS ()
553           && (pc) <= (CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK));
554 }
555
556 /* Function: frame_chain_valid 
557    Returns true for a user frame or a call_function_by_hand dummy frame,
558    and false for the CRT0 start-up frame.  Purpose is to terminate backtrace.  */
559
560 int
561 frame_chain_valid (CORE_ADDR fp, struct frame_info *fi)
562 {
563   /* Don't prune CALL_DUMMY frames.  */
564   if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
565       && DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), 0, 0))
566     return 1;
567
568   /* If the new frame pointer is zero, then it isn't valid.  */
569   if (fp == 0)
570     return 0;
571   
572   /* If the new frame would be inside (younger than) the previous frame,
573      then it isn't valid.  */
574   if (INNER_THAN (fp, get_frame_base (fi)))
575     return 0;
576   
577   /* If we're already inside the entry function for the main objfile, then it
578      isn't valid.  */
579   if (inside_entry_func (get_frame_pc (fi)))
580     return 0;
581
582   /* If we're inside the entry file, it isn't valid.  */
583   /* NOTE/drow 2002-12-25: should there be a way to disable this check?  It
584      assumes a single small entry file, and the way some debug readers (e.g.
585      dbxread) figure out which object is the entry file is somewhat hokey.  */
586   if (inside_entry_file (frame_pc_unwind (fi)))
587       return 0;
588
589   /* If the architecture has a custom FRAME_CHAIN_VALID, call it now.  */
590   if (FRAME_CHAIN_VALID_P ())
591     return FRAME_CHAIN_VALID (fp, fi);
592
593   return 1;
594 }