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