This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / blockframe.c
1 /* Get info from stack frames;
2    convert between frames, blocks, functions and pc values.
3    Copyright 1986, 87, 88, 89, 91, 94, 95, 96, 97, 1998
4              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, Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "bfd.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "frame.h"
28 #include "gdbcore.h"
29 #include "value.h"              /* for read_register */
30 #include "target.h"             /* for target_has_stack */
31 #include "inferior.h"           /* for read_pc */
32 #include "annotate.h"
33
34 /* Prototypes for exported functions. */
35
36 void _initialize_blockframe PARAMS ((void));
37
38 /* Is ADDR inside the startup file?  Note that if your machine
39    has a way to detect the bottom of the stack, there is no need
40    to call this function from FRAME_CHAIN_VALID; the reason for
41    doing so is that some machines have no way of detecting bottom
42    of stack. 
43
44    A PC of zero is always considered to be the bottom of the stack. */
45
46 int
47 inside_entry_file (addr)
48      CORE_ADDR addr;
49 {
50   if (addr == 0)
51     return 1;
52   if (symfile_objfile == 0)
53     return 0;
54 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
55   /* Do not stop backtracing if the pc is in the call dummy
56      at the entry point.  */
57 /* FIXME: Won't always work with zeros for the last two arguments */
58   if (PC_IN_CALL_DUMMY (addr, 0, 0))    
59     return 0;
60 #endif
61   return (addr >= symfile_objfile -> ei.entry_file_lowpc &&
62           addr <  symfile_objfile -> ei.entry_file_highpc);
63 }
64
65 /* Test a specified PC value to see if it is in the range of addresses
66    that correspond to the main() function.  See comments above for why
67    we might want to do this.
68
69    Typically called from FRAME_CHAIN_VALID.
70
71    A PC of zero is always considered to be the bottom of the stack. */
72
73 int
74 inside_main_func (pc)
75 CORE_ADDR pc;
76 {
77   if (pc == 0)
78     return 1;
79   if (symfile_objfile == 0)
80     return 0;
81
82   /* If the addr range is not set up at symbol reading time, set it up now.
83      This is for FRAME_CHAIN_VALID_ALTERNATE. I do this for coff, because
84      it is unable to set it up and symbol reading time. */
85
86   if (symfile_objfile -> ei.main_func_lowpc == INVALID_ENTRY_LOWPC &&
87       symfile_objfile -> ei.main_func_highpc == INVALID_ENTRY_HIGHPC)
88     {
89       struct symbol *mainsym;
90
91       mainsym = lookup_symbol ("main", NULL, VAR_NAMESPACE, NULL, NULL);
92       if (mainsym && SYMBOL_CLASS(mainsym) == LOC_BLOCK)
93         {
94           symfile_objfile->ei.main_func_lowpc = 
95             BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym));
96           symfile_objfile->ei.main_func_highpc = 
97             BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym));
98         }
99     }
100   return (symfile_objfile -> ei.main_func_lowpc  <= pc &&
101           symfile_objfile -> ei.main_func_highpc > pc);
102 }
103
104 /* Test a specified PC value to see if it is in the range of addresses
105    that correspond to the process entry point function.  See comments
106    in objfiles.h for why we might want to do this.
107
108    Typically called from FRAME_CHAIN_VALID.
109
110    A PC of zero is always considered to be the bottom of the stack. */
111
112 int
113 inside_entry_func (pc)
114 CORE_ADDR pc;
115 {
116   if (pc == 0)
117     return 1;
118   if (symfile_objfile == 0)
119     return 0;
120 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
121   /* Do not stop backtracing if the pc is in the call dummy
122      at the entry point.  */
123 /* FIXME: Won't always work with zeros for the last two arguments */
124   if (PC_IN_CALL_DUMMY (pc, 0, 0))
125     return 0;
126 #endif
127   return (symfile_objfile -> ei.entry_func_lowpc  <= pc &&
128           symfile_objfile -> ei.entry_func_highpc > pc);
129 }
130
131 /* Info about the innermost stack frame (contents of FP register) */
132
133 static struct frame_info *current_frame;
134
135 /* Cache for frame addresses already read by gdb.  Valid only while
136    inferior is stopped.  Control variables for the frame cache should
137    be local to this module.  */
138
139 struct obstack frame_cache_obstack;
140
141 /* Return the innermost (currently executing) stack frame.  */
142
143 struct frame_info *
144 get_current_frame ()
145 {
146   if (current_frame == NULL)
147     {
148       if (target_has_stack)
149         current_frame = create_new_frame (read_fp (), read_pc ());
150       else
151         error ("No stack.");
152     }
153   return current_frame;
154 }
155
156 void
157 set_current_frame (frame)
158      struct frame_info *frame;
159 {
160   current_frame = frame;
161 }
162
163 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
164    Always returns a non-NULL value.  */
165
166 struct frame_info *
167 create_new_frame (addr, pc)
168      CORE_ADDR addr;
169      CORE_ADDR pc;
170 {
171   struct frame_info *fi;
172   char *name;
173
174   fi = (struct frame_info *)
175     obstack_alloc (&frame_cache_obstack,
176                    sizeof (struct frame_info));
177
178   /* Arbitrary frame */
179   fi->next = NULL;
180   fi->prev = NULL;
181   fi->frame = addr;
182   fi->pc = pc;
183   find_pc_partial_function (pc, &name, (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
184   fi->signal_handler_caller = IN_SIGTRAMP (fi->pc, name);
185
186 #ifdef INIT_EXTRA_FRAME_INFO
187   INIT_EXTRA_FRAME_INFO (0, fi);
188 #endif
189
190   return fi;
191 }
192
193 /* Return the frame that called FI.
194    If FI is the original frame (it has no caller), return 0.  */
195
196 struct frame_info *
197 get_prev_frame (frame)
198      struct frame_info *frame;
199 {
200   return get_prev_frame_info (frame);
201 }
202
203 /* Return the frame that FRAME calls (NULL if FRAME is the innermost
204    frame).  */
205
206 struct frame_info *
207 get_next_frame (frame)
208      struct frame_info *frame;
209 {
210   return frame->next;
211 }
212
213 /* Flush the entire frame cache.  */
214
215 void
216 flush_cached_frames ()
217 {
218   /* Since we can't really be sure what the first object allocated was */
219   obstack_free (&frame_cache_obstack, 0);
220   obstack_init (&frame_cache_obstack);
221
222   current_frame = NULL;  /* Invalidate cache */
223   select_frame (NULL, -1);
224   annotate_frames_invalid ();
225 }
226
227 /* Flush the frame cache, and start a new one if necessary.  */
228
229 void
230 reinit_frame_cache ()
231 {
232   flush_cached_frames ();
233
234   /* FIXME: The inferior_pid test is wrong if there is a corefile.  */
235   if (inferior_pid != 0)
236     {
237       select_frame (get_current_frame (), 0);
238     }
239 }
240
241 /* If a machine allows frameless functions, it should define a macro
242    FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h.  FI is the struct
243    frame_info for the frame, and FRAMELESS should be set to nonzero
244    if it represents a frameless function invocation.  */
245
246 /* Return nonzero if the function for this frame lacks a prologue.  Many
247    machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
248    function.  */
249
250 int
251 frameless_look_for_prologue (frame)
252      struct frame_info *frame;
253 {
254   CORE_ADDR func_start, after_prologue;
255   func_start = get_pc_function_start (frame->pc);
256   if (func_start)
257     {
258       func_start += FUNCTION_START_OFFSET;
259       after_prologue = func_start;
260 #ifdef SKIP_PROLOGUE_FRAMELESS_P
261       /* This is faster, since only care whether there *is* a prologue,
262          not how long it is.  */
263       SKIP_PROLOGUE_FRAMELESS_P (after_prologue);
264 #else
265       SKIP_PROLOGUE (after_prologue);
266 #endif
267       return after_prologue == func_start;
268     }
269   else if (frame->pc == 0)
270     /* A frame with a zero PC is usually created by dereferencing a NULL
271        function pointer, normally causing an immediate core dump of the
272        inferior. Mark function as frameless, as the inferior has no chance
273        of setting up a stack frame.  */
274     return 1;
275   else
276     /* If we can't find the start of the function, we don't really
277        know whether the function is frameless, but we should be able
278        to get a reasonable (i.e. best we can do under the
279        circumstances) backtrace by saying that it isn't.  */
280     return 0;
281 }
282
283 /* Default a few macros that people seldom redefine.  */
284
285 #if !defined (INIT_FRAME_PC)
286 #define INIT_FRAME_PC(fromleaf, prev) \
287   prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
288               prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
289 #endif
290
291 #ifndef FRAME_CHAIN_COMBINE
292 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
293 #endif
294
295 /* Return a structure containing various interesting information
296    about the frame that called NEXT_FRAME.  Returns NULL
297    if there is no such frame.  */
298
299 struct frame_info *
300 get_prev_frame_info (next_frame)
301      struct frame_info *next_frame;
302 {
303   CORE_ADDR address = 0;
304   struct frame_info *prev;
305   int fromleaf = 0;
306   char *name;
307
308   /* If the requested entry is in the cache, return it.
309      Otherwise, figure out what the address should be for the entry
310      we're about to add to the cache. */
311
312   if (!next_frame)
313     {
314 #if 0
315       /* This screws value_of_variable, which just wants a nice clean
316          NULL return from block_innermost_frame if there are no frames.
317          I don't think I've ever seen this message happen otherwise.
318          And returning NULL here is a perfectly legitimate thing to do.  */
319       if (!current_frame)
320         {
321           error ("You haven't set up a process's stack to examine.");
322         }
323 #endif
324
325       return current_frame;
326     }
327
328   /* If we have the prev one, return it */
329   if (next_frame->prev)
330     return next_frame->prev;
331
332   /* On some machines it is possible to call a function without
333      setting up a stack frame for it.  On these machines, we
334      define this macro to take two args; a frameinfo pointer
335      identifying a frame and a variable to set or clear if it is
336      or isn't leafless.  */
337 #ifdef FRAMELESS_FUNCTION_INVOCATION
338   /* Still don't want to worry about this except on the innermost
339      frame.  This macro will set FROMLEAF if NEXT_FRAME is a
340      frameless function invocation.  */
341   if (!(next_frame->next))
342     {
343       FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
344       if (fromleaf)
345         address = FRAME_FP (next_frame);
346     }
347 #endif
348
349   if (!fromleaf)
350     {
351       /* Two macros defined in tm.h specify the machine-dependent
352          actions to be performed here.
353          First, get the frame's chain-pointer.
354          If that is zero, the frame is the outermost frame or a leaf
355          called by the outermost frame.  This means that if start
356          calls main without a frame, we'll return 0 (which is fine
357          anyway).
358
359          Nope; there's a problem.  This also returns when the current
360          routine is a leaf of main.  This is unacceptable.  We move
361          this to after the ffi test; I'd rather have backtraces from
362          start go curfluy than have an abort called from main not show
363          main.  */
364       address = FRAME_CHAIN (next_frame);
365       if (!FRAME_CHAIN_VALID (address, next_frame))
366         return 0;
367       address = FRAME_CHAIN_COMBINE (address, next_frame);
368     }
369   if (address == 0)
370     return 0;
371
372   prev = (struct frame_info *)
373     obstack_alloc (&frame_cache_obstack,
374                    sizeof (struct frame_info));
375
376   if (next_frame)
377     next_frame->prev = prev;
378   prev->next = next_frame;
379   prev->prev = (struct frame_info *) 0;
380   prev->frame = address;
381   prev->signal_handler_caller = 0;
382
383 /* This change should not be needed, FIXME!  We should
384    determine whether any targets *need* INIT_FRAME_PC to happen
385    after INIT_EXTRA_FRAME_INFO and come up with a simple way to
386    express what goes on here.
387
388       INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
389                 (where the PC is already set up) and here (where it isn't).
390       INIT_FRAME_PC is only called from here, always after
391                 INIT_EXTRA_FRAME_INFO.
392    
393    The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
394    value (which hasn't been set yet).  Some other machines appear to
395    require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC.  Phoo.
396
397    We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
398    an already overcomplicated part of GDB.   gnu@cygnus.com, 15Sep92.
399
400    Assuming that some machines need INIT_FRAME_PC after
401    INIT_EXTRA_FRAME_INFO, one possible scheme:
402
403    SETUP_INNERMOST_FRAME()
404      Default version is just create_new_frame (read_fp ()),
405      read_pc ()).  Machines with extra frame info would do that (or the
406      local equivalent) and then set the extra fields.
407    SETUP_ARBITRARY_FRAME(argc, argv)
408      Only change here is that create_new_frame would no longer init extra
409      frame info; SETUP_ARBITRARY_FRAME would have to do that.
410    INIT_PREV_FRAME(fromleaf, prev)
411      Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC.  This should
412      also return a flag saying whether to keep the new frame, or
413      whether to discard it, because on some machines (e.g.  mips) it
414      is really awkward to have FRAME_CHAIN_VALID called *before*
415      INIT_EXTRA_FRAME_INFO (there is no good way to get information
416      deduced in FRAME_CHAIN_VALID into the extra fields of the new frame).
417    std_frame_pc(fromleaf, prev)
418      This is the default setting for INIT_PREV_FRAME.  It just does what
419      the default INIT_FRAME_PC does.  Some machines will call it from
420      INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
421      Some machines won't use it.
422    kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94.  */
423
424 #ifdef INIT_FRAME_PC_FIRST
425   INIT_FRAME_PC_FIRST (fromleaf, prev);
426 #endif
427
428 #ifdef INIT_EXTRA_FRAME_INFO
429   INIT_EXTRA_FRAME_INFO(fromleaf, prev);
430 #endif
431
432   /* This entry is in the frame queue now, which is good since
433      FRAME_SAVED_PC may use that queue to figure out its value
434      (see tm-sparc.h).  We want the pc saved in the inferior frame. */
435   INIT_FRAME_PC(fromleaf, prev);
436
437   /* If ->frame and ->pc are unchanged, we are in the process of getting
438      ourselves into an infinite backtrace.  Some architectures check this
439      in FRAME_CHAIN or thereabouts, but it seems like there is no reason
440      this can't be an architecture-independent check.  */
441   if (next_frame != NULL)
442     {
443       if (prev->frame == next_frame->frame
444           && prev->pc == next_frame->pc)
445         {
446           next_frame->prev = NULL;
447           obstack_free (&frame_cache_obstack, prev);
448           return NULL;
449         }
450     }
451
452   find_pc_partial_function (prev->pc, &name,
453                             (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
454   if (IN_SIGTRAMP (prev->pc, name))
455     prev->signal_handler_caller = 1;
456
457   return prev;
458 }
459
460 CORE_ADDR
461 get_frame_pc (frame)
462      struct frame_info *frame;
463 {
464   return frame->pc;
465 }
466
467 #if defined (FRAME_FIND_SAVED_REGS)
468 /* Find the addresses in which registers are saved in FRAME.  */
469
470 void
471 get_frame_saved_regs (frame, saved_regs_addr)
472      struct frame_info *frame;
473      struct frame_saved_regs *saved_regs_addr;
474 {
475   FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr);
476 }
477 #endif
478
479 /* Return the innermost lexical block in execution
480    in a specified stack frame.  The frame address is assumed valid.  */
481
482 struct block *
483 get_frame_block (frame)
484      struct frame_info *frame;
485 {
486   CORE_ADDR pc;
487
488   pc = frame->pc;
489   if (frame->next != 0 && frame->next->signal_handler_caller == 0)
490     /* We are not in the innermost frame and we were not interrupted
491        by a signal.  We need to subtract one to get the correct block,
492        in case the call instruction was the last instruction of the block.
493        If there are any machines on which the saved pc does not point to
494        after the call insn, we probably want to make frame->pc point after
495        the call insn anyway.  */
496     --pc;
497   return block_for_pc (pc);
498 }
499
500 struct block *
501 get_current_block ()
502 {
503   return block_for_pc (read_pc ());
504 }
505
506 CORE_ADDR
507 get_pc_function_start (pc)
508      CORE_ADDR pc;
509 {
510   register struct block *bl;
511   register struct symbol *symbol;
512   register struct minimal_symbol *msymbol;
513   CORE_ADDR fstart;
514
515   if ((bl = block_for_pc (pc)) != NULL &&
516       (symbol = block_function (bl)) != NULL)
517     {
518       bl = SYMBOL_BLOCK_VALUE (symbol);
519       fstart = BLOCK_START (bl);
520     }
521   else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
522     {
523       fstart = SYMBOL_VALUE_ADDRESS (msymbol);
524     }
525   else
526     {
527       fstart = 0;
528     }
529   return (fstart);
530 }
531
532 /* Return the symbol for the function executing in frame FRAME.  */
533
534 struct symbol *
535 get_frame_function (frame)
536      struct frame_info *frame;
537 {
538   register struct block *bl = get_frame_block (frame);
539   if (bl == 0)
540     return 0;
541   return block_function (bl);
542 }
543 \f
544
545 /* Return the blockvector immediately containing the innermost lexical block
546    containing the specified pc value and section, or 0 if there is none.
547    PINDEX is a pointer to the index value of the block.  If PINDEX
548    is NULL, we don't pass this information back to the caller.  */
549
550 struct blockvector *
551 blockvector_for_pc_sect (pc, section, pindex, symtab)
552      register CORE_ADDR pc;
553      struct sec *section;
554      int *pindex;
555      struct symtab *symtab;
556      
557 {
558   register struct block *b;
559   register int bot, top, half;
560   struct blockvector *bl;
561
562   if (symtab == 0)      /* if no symtab specified by caller */
563     {
564       /* First search all symtabs for one whose file contains our pc */
565       if ((symtab = find_pc_sect_symtab (pc, section)) == 0)
566         return 0;
567     }
568
569   bl = BLOCKVECTOR (symtab);
570   b = BLOCKVECTOR_BLOCK (bl, 0);
571
572   /* Then search that symtab for the smallest block that wins.  */
573   /* Use binary search to find the last block that starts before PC.  */
574
575   bot = 0;
576   top = BLOCKVECTOR_NBLOCKS (bl);
577
578   while (top - bot > 1)
579     {
580       half = (top - bot + 1) >> 1;
581       b = BLOCKVECTOR_BLOCK (bl, bot + half);
582       if (BLOCK_START (b) <= pc)
583         bot += half;
584       else
585         top = bot + half;
586     }
587
588   /* Now search backward for a block that ends after PC.  */
589
590   while (bot >= 0)
591     {
592       b = BLOCKVECTOR_BLOCK (bl, bot);
593       if (BLOCK_END (b) > pc)
594         {
595           if (pindex)
596             *pindex = bot;
597           return bl;
598         }
599       bot--;
600     }
601   return 0;
602 }
603
604 /* Return the blockvector immediately containing the innermost lexical block
605    containing the specified pc value, or 0 if there is none.
606    Backward compatibility, no section.  */
607
608 struct blockvector *
609 blockvector_for_pc (pc, pindex)
610      register CORE_ADDR pc;
611      int *pindex;
612 {
613   return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
614                                   pindex, NULL);
615 }
616
617 /* Return the innermost lexical block containing the specified pc value
618    in the specified section, or 0 if there is none.  */
619
620 struct block *
621 block_for_pc_sect (pc, section)
622      register CORE_ADDR pc;
623      struct sec *section;
624 {
625   register struct blockvector *bl;
626   int index;
627
628   bl = blockvector_for_pc_sect (pc, section, &index, NULL);
629   if (bl)
630     return BLOCKVECTOR_BLOCK (bl, index);
631   return 0;
632 }
633
634 /* Return the innermost lexical block containing the specified pc value,
635    or 0 if there is none.  Backward compatibility, no section.  */
636
637 struct block *
638 block_for_pc (pc)
639      register CORE_ADDR pc;
640 {
641   return block_for_pc_sect (pc, find_pc_mapped_section (pc));
642 }
643
644 /* Return the function containing pc value PC in section SECTION.
645    Returns 0 if function is not known.  */
646
647 struct symbol *
648 find_pc_sect_function (pc, section)
649      CORE_ADDR pc;
650      struct sec *section;
651 {
652   register struct block *b = block_for_pc_sect (pc, section);
653   if (b == 0)
654     return 0;
655   return block_function (b);
656 }
657
658 /* Return the function containing pc value PC.
659    Returns 0 if function is not known.  Backward compatibility, no section */
660
661 struct symbol *
662 find_pc_function (pc)
663      CORE_ADDR pc;
664 {
665   return find_pc_sect_function (pc, find_pc_mapped_section (pc));
666 }
667
668 /* These variables are used to cache the most recent result
669  * of find_pc_partial_function. */
670
671 static CORE_ADDR   cache_pc_function_low     = 0;
672 static CORE_ADDR   cache_pc_function_high    = 0;
673 static char       *cache_pc_function_name    = 0;
674 static struct sec *cache_pc_function_section = NULL;
675
676 /* Clear cache, e.g. when symbol table is discarded. */
677
678 void
679 clear_pc_function_cache()
680 {
681   cache_pc_function_low = 0;
682   cache_pc_function_high = 0;
683   cache_pc_function_name = (char *)0;
684   cache_pc_function_section = NULL;
685 }
686
687 /* Finds the "function" (text symbol) that is smaller than PC but
688    greatest of all of the potential text symbols in SECTION.  Sets
689    *NAME and/or *ADDRESS conditionally if that pointer is non-null.
690    If ENDADDR is non-null, then set *ENDADDR to be the end of the
691    function (exclusive), but passing ENDADDR as non-null means that
692    the function might cause symbols to be read.  This function either
693    succeeds or fails (not halfway succeeds).  If it succeeds, it sets
694    *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
695    If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
696    returns 0.  */
697
698 int
699 find_pc_sect_partial_function (pc, section, name, address, endaddr)
700      CORE_ADDR  pc;
701      asection  *section;
702      char     **name;
703      CORE_ADDR *address;
704      CORE_ADDR *endaddr;
705 {
706   struct partial_symtab *pst;
707   struct symbol         *f;
708   struct minimal_symbol *msymbol;
709   struct partial_symbol *psb;
710   struct obj_section    *osect;
711   int i;
712   CORE_ADDR mapped_pc;
713
714   mapped_pc = overlay_mapped_address (pc, section);
715
716   if (mapped_pc >= cache_pc_function_low && 
717       mapped_pc < cache_pc_function_high &&
718       section == cache_pc_function_section)
719     goto return_cached_value;
720
721   /* If sigtramp is in the u area, it counts as a function (especially
722      important for step_1).  */
723 #if defined SIGTRAMP_START
724   if (IN_SIGTRAMP (mapped_pc, (char *)NULL))
725     {
726       cache_pc_function_low     = SIGTRAMP_START (mapped_pc);
727       cache_pc_function_high    = SIGTRAMP_END (mapped_pc);
728       cache_pc_function_name    = "<sigtramp>";
729       cache_pc_function_section = section;
730       goto return_cached_value;
731     }
732 #endif
733
734   msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
735   pst = find_pc_sect_psymtab (mapped_pc, section);
736   if (pst)
737     {
738       /* Need to read the symbols to get a good value for the end address.  */
739       if (endaddr != NULL && !pst->readin)
740         {
741           /* Need to get the terminal in case symbol-reading produces
742              output.  */
743           target_terminal_ours_for_output ();
744           PSYMTAB_TO_SYMTAB (pst);
745         }
746
747       if (pst->readin)
748         {
749           /* Checking whether the msymbol has a larger value is for the
750              "pathological" case mentioned in print_frame_info.  */
751           f = find_pc_sect_function (mapped_pc, section);
752           if (f != NULL
753               && (msymbol == NULL
754                   || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
755                       >= SYMBOL_VALUE_ADDRESS (msymbol))))
756             {
757               cache_pc_function_low     = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
758               cache_pc_function_high    = BLOCK_END   (SYMBOL_BLOCK_VALUE (f));
759               cache_pc_function_name    = SYMBOL_NAME (f);
760               cache_pc_function_section = section;
761               goto return_cached_value;
762             }
763         }
764       else
765         {
766           /* Now that static symbols go in the minimal symbol table, perhaps
767              we could just ignore the partial symbols.  But at least for now
768              we use the partial or minimal symbol, whichever is larger.  */
769           psb = find_pc_sect_psymbol (pst, mapped_pc, section);
770
771           if (psb
772               && (msymbol == NULL ||
773                   (SYMBOL_VALUE_ADDRESS (psb)
774                    >= SYMBOL_VALUE_ADDRESS (msymbol))))
775             {
776               /* This case isn't being cached currently. */
777               if (address)
778                 *address = SYMBOL_VALUE_ADDRESS (psb);
779               if (name)
780                 *name = SYMBOL_NAME (psb);
781               /* endaddr non-NULL can't happen here.  */
782               return 1;
783             }
784         }
785     }
786
787   /* Not in the normal symbol tables, see if the pc is in a known section.
788      If it's not, then give up.  This ensures that anything beyond the end
789      of the text seg doesn't appear to be part of the last function in the
790      text segment.  */
791
792   osect = find_pc_sect_section (mapped_pc, section);
793
794   if (!osect)
795     msymbol = NULL;
796
797   /* Must be in the minimal symbol table.  */
798   if (msymbol == NULL)
799     {
800       /* No available symbol.  */
801       if (name != NULL)
802         *name = 0;
803       if (address != NULL)
804         *address = 0;
805       if (endaddr != NULL)
806         *endaddr = 0;
807       return 0;
808     }
809
810   cache_pc_function_low     = SYMBOL_VALUE_ADDRESS (msymbol);
811   cache_pc_function_name    = SYMBOL_NAME (msymbol);
812   cache_pc_function_section = section;
813
814   /* Use the lesser of the next minimal symbol in the same section, or
815      the end of the section, as the end of the function.  */
816   
817   /* Step over other symbols at this same address, and symbols in
818      other sections, to find the next symbol in this section with
819      a different address.  */
820
821   for (i=1; SYMBOL_NAME (msymbol+i) != NULL; i++)
822     {
823       if (SYMBOL_VALUE_ADDRESS (msymbol+i) != SYMBOL_VALUE_ADDRESS (msymbol) 
824           && SYMBOL_BFD_SECTION (msymbol+i) == SYMBOL_BFD_SECTION (msymbol))
825         break;
826     }
827
828   if (SYMBOL_NAME (msymbol + i) != NULL
829       && SYMBOL_VALUE_ADDRESS (msymbol + i) < osect->endaddr)
830     cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
831   else
832     /* We got the start address from the last msymbol in the objfile.
833        So the end address is the end of the section.  */
834     cache_pc_function_high = osect->endaddr;
835
836  return_cached_value:
837
838   if (address)
839     {
840       if (pc_in_unmapped_range (pc, section))
841         *address = overlay_unmapped_address (cache_pc_function_low, section);
842       else
843         *address = cache_pc_function_low;
844     }
845     
846   if (name)
847     *name = cache_pc_function_name;
848
849   if (endaddr)
850     {
851       if (pc_in_unmapped_range (pc, section))
852         {
853           /* Because the high address is actually beyond the end of
854              the function (and therefore possibly beyond the end of
855              the overlay), we must actually convert (high - 1)
856              and then add one to that. */
857
858           *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1, 
859                                                    section);
860         }
861       else
862         *endaddr = cache_pc_function_high;
863     }
864
865   return 1;
866 }
867
868 /* Backward compatibility, no section argument */
869
870 int
871 find_pc_partial_function (pc, name, address, endaddr)
872      CORE_ADDR  pc;
873      char     **name;
874      CORE_ADDR *address;
875      CORE_ADDR *endaddr;
876 {
877   asection     *section;
878
879   section = find_pc_overlay (pc);
880   return find_pc_sect_partial_function (pc, section, name, address, endaddr);
881 }
882
883 /* Return the innermost stack frame executing inside of BLOCK,
884    or NULL if there is no such frame.  If BLOCK is NULL, just return NULL.  */
885
886 struct frame_info *
887 block_innermost_frame (block)
888      struct block *block;
889 {
890   struct frame_info *frame;
891   register CORE_ADDR start;
892   register CORE_ADDR end;
893
894   if (block == NULL)
895     return NULL;
896
897   start = BLOCK_START (block);
898   end = BLOCK_END (block);
899
900   frame = NULL;
901   while (1)
902     {
903       frame = get_prev_frame (frame);
904       if (frame == NULL)
905         return NULL;
906       if (frame->pc >= start && frame->pc < end)
907         return frame;
908     }
909 }
910
911 /* Return the full FRAME which corresponds to the given CORE_ADDR
912    or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
913
914 struct frame_info *
915 find_frame_addr_in_frame_chain (frame_addr)
916      CORE_ADDR frame_addr;
917 {
918   struct frame_info *frame = NULL;
919
920   if (frame_addr == (CORE_ADDR)0)
921     return NULL;
922
923   while (1)
924     {
925       frame = get_prev_frame (frame);
926       if (frame == NULL)
927         return NULL;
928       if (FRAME_FP (frame) == frame_addr)
929         return frame;
930     }
931 }
932
933 #ifdef SIGCONTEXT_PC_OFFSET
934 /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp.  */
935
936 CORE_ADDR
937 sigtramp_saved_pc (frame)
938      struct frame_info *frame;
939 {
940   CORE_ADDR sigcontext_addr;
941   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
942   int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT;
943   int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT;
944
945   /* Get sigcontext address, it is the third parameter on the stack.  */
946   if (frame->next)
947     sigcontext_addr = read_memory_integer (FRAME_ARGS_ADDRESS (frame->next)
948                                            + FRAME_ARGS_SKIP
949                                            + sigcontext_offs,
950                                            ptrbytes);
951   else
952     sigcontext_addr = read_memory_integer (read_register (SP_REGNUM)
953                                             + sigcontext_offs,
954                                            ptrbytes);
955
956   /* Don't cause a memory_error when accessing sigcontext in case the stack
957      layout has changed or the stack is corrupt.  */
958   target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes);
959   return extract_unsigned_integer (buf, ptrbytes);
960 }
961 #endif /* SIGCONTEXT_PC_OFFSET */
962
963 #ifdef USE_GENERIC_DUMMY_FRAMES
964
965 /*
966  * GENERIC DUMMY FRAMES
967  * 
968  * The following code serves to maintain the dummy stack frames for
969  * inferior function calls (ie. when gdb calls into the inferior via
970  * call_function_by_hand).  This code saves the machine state before 
971  * the call in host memory, so we must maintain an independant stack 
972  * and keep it consistant etc.  I am attempting to make this code 
973  * generic enough to be used by many targets.
974  *
975  * The cheapest and most generic way to do CALL_DUMMY on a new target
976  * is probably to define CALL_DUMMY to be empty, CALL_DUMMY_LENGTH to zero,
977  * and CALL_DUMMY_LOCATION to AT_ENTRY.  Then you must remember to define
978  * PUSH_RETURN_ADDRESS, because no call instruction will be being
979  * executed by the target.
980  */
981
982 static struct dummy_frame *dummy_frame_stack = NULL;
983
984 /* Function: find_dummy_frame(pc, fp, sp)
985    Search the stack of dummy frames for one matching the given PC, FP and SP.
986    This is the work-horse for pc_in_call_dummy and read_register_dummy     */
987
988 char * 
989 generic_find_dummy_frame (pc, fp)
990      CORE_ADDR pc;
991      CORE_ADDR fp;
992 {
993   struct dummy_frame * dummyframe;
994
995   if (pc != entry_point_address ())
996     return 0;
997
998   for (dummyframe = dummy_frame_stack; dummyframe != NULL;
999        dummyframe = dummyframe->next)
1000     if (fp == dummyframe->fp || fp == dummyframe->sp)
1001       /* The frame in question lies between the saved fp and sp, inclusive */
1002       return dummyframe->regs;
1003
1004   return 0;
1005 }
1006
1007 /* Function: pc_in_call_dummy (pc, fp)
1008    Return true if this is a dummy frame created by gdb for an inferior call */
1009
1010 int
1011 generic_pc_in_call_dummy (pc, fp)
1012      CORE_ADDR pc;
1013      CORE_ADDR fp;
1014 {
1015   /* if find_dummy_frame succeeds, then PC is in a call dummy */
1016   return (generic_find_dummy_frame (pc, fp) != 0);
1017 }
1018
1019 /* Function: read_register_dummy 
1020    Find a saved register from before GDB calls a function in the inferior */
1021
1022 CORE_ADDR
1023 generic_read_register_dummy (pc, fp, regno)
1024      CORE_ADDR pc;
1025      CORE_ADDR fp;
1026      int regno;
1027 {
1028   char *dummy_regs = generic_find_dummy_frame (pc, fp);
1029
1030   if (dummy_regs)
1031     return extract_address (&dummy_regs[REGISTER_BYTE (regno)],
1032                             REGISTER_RAW_SIZE(regno));
1033   else
1034     return 0;
1035 }
1036
1037 /* Save all the registers on the dummy frame stack.  Most ports save the
1038    registers on the target stack.  This results in lots of unnecessary memory
1039    references, which are slow when debugging via a serial line.  Instead, we
1040    save all the registers internally, and never write them to the stack.  The
1041    registers get restored when the called function returns to the entry point,
1042    where a breakpoint is laying in wait.  */
1043
1044 void
1045 generic_push_dummy_frame ()
1046 {
1047   struct dummy_frame *dummy_frame;
1048   CORE_ADDR fp = (get_current_frame ())->frame;
1049
1050   /* check to see if there are stale dummy frames, 
1051      perhaps left over from when a longjump took us out of a 
1052      function that was called by the debugger */
1053
1054   dummy_frame = dummy_frame_stack;
1055   while (dummy_frame)
1056     if (INNER_THAN (dummy_frame->fp, fp))       /* stale -- destroy! */
1057       {
1058         dummy_frame_stack = dummy_frame->next;
1059         free (dummy_frame);
1060         dummy_frame = dummy_frame_stack;
1061       }
1062     else
1063       dummy_frame = dummy_frame->next;
1064
1065   dummy_frame = xmalloc (sizeof (struct dummy_frame));
1066   dummy_frame->pc   = read_register (PC_REGNUM);
1067   dummy_frame->sp   = read_register (SP_REGNUM);
1068   dummy_frame->fp   = fp;
1069   read_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
1070   dummy_frame->next = dummy_frame_stack;
1071   dummy_frame_stack = dummy_frame;
1072 }
1073
1074 /* Function: pop_frame
1075    Restore the machine state from either the saved dummy stack or a
1076    real stack frame. */
1077
1078 void
1079 generic_pop_current_frame (pop)
1080   void (*pop) PARAMS ((struct frame_info *frame));
1081 {
1082   struct frame_info *frame = get_current_frame ();
1083   if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
1084     generic_pop_dummy_frame ();
1085   else
1086     pop (frame);
1087 }
1088
1089 /* Function: pop_dummy_frame
1090    Restore the machine state from a saved dummy stack frame. */
1091
1092 void
1093 generic_pop_dummy_frame ()
1094 {
1095   struct dummy_frame *dummy_frame = dummy_frame_stack;
1096
1097   /* FIXME: what if the first frame isn't the right one, eg..
1098      because one call-by-hand function has done a longjmp into another one? */
1099
1100   if (!dummy_frame)
1101     error ("Can't pop dummy frame!");
1102   dummy_frame_stack = dummy_frame->next;
1103   write_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
1104   flush_cached_frames ();
1105   free (dummy_frame);
1106 }
1107
1108 /* Function: frame_chain_valid 
1109    Returns true for a user frame or a call_function_by_hand dummy frame,
1110    and false for the CRT0 start-up frame.  Purpose is to terminate backtrace */
1111  
1112 int
1113 generic_frame_chain_valid (fp, fi)
1114      CORE_ADDR fp;
1115      struct frame_info *fi;
1116 {
1117   if (PC_IN_CALL_DUMMY(FRAME_SAVED_PC(fi), fp, fp))
1118     return 1;   /* don't prune CALL_DUMMY frames */
1119   else          /* fall back to default algorithm (see frame.h) */
1120     return (fp != 0
1121             && (INNER_THAN (fi->frame, fp) || fi->frame == fp)
1122             && !inside_entry_file (FRAME_SAVED_PC(fi)));
1123 }
1124  
1125 /* Function: get_saved_register
1126    Find register number REGNUM relative to FRAME and put its (raw,
1127    target format) contents in *RAW_BUFFER.  
1128
1129    Set *OPTIMIZED if the variable was optimized out (and thus can't be
1130    fetched).  Note that this is never set to anything other than zero
1131    in this implementation.
1132
1133    Set *LVAL to lval_memory, lval_register, or not_lval, depending on
1134    whether the value was fetched from memory, from a register, or in a
1135    strange and non-modifiable way (e.g. a frame pointer which was
1136    calculated rather than fetched).  We will use not_lval for values
1137    fetched from generic dummy frames.
1138
1139    Set *ADDRP to the address, either in memory on as a REGISTER_BYTE
1140    offset into the registers array.  If the value is stored in a dummy
1141    frame, set *ADDRP to zero.
1142
1143    To use this implementation, define a function called
1144    "get_saved_register" in your target code, which simply passes all
1145    of its arguments to this function.
1146
1147    The argument RAW_BUFFER must point to aligned memory.  */
1148
1149 void
1150 generic_get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
1151      char *raw_buffer;
1152      int *optimized;
1153      CORE_ADDR *addrp;
1154      struct frame_info *frame;
1155      int regnum;
1156      enum lval_type *lval;
1157 {
1158   struct frame_saved_regs fsr;
1159
1160   if (!target_has_registers)
1161     error ("No registers.");
1162
1163   /* Normal systems don't optimize out things with register numbers.  */
1164   if (optimized != NULL)
1165     *optimized = 0;
1166
1167   if (addrp)            /* default assumption: not found in memory */
1168     *addrp = 0;
1169
1170   /* Note: since the current frame's registers could only have been
1171      saved by frames INTERIOR TO the current frame, we skip examining
1172      the current frame itself: otherwise, we would be getting the
1173      previous frame's registers which were saved by the current frame.  */
1174
1175   while (frame && ((frame = frame->next) != NULL))
1176     {
1177       if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
1178         {
1179           if (lval)                     /* found it in a CALL_DUMMY frame */
1180             *lval = not_lval;
1181           if (raw_buffer)
1182             memcpy (raw_buffer, 
1183                     generic_find_dummy_frame (frame->pc, frame->frame) + 
1184                     REGISTER_BYTE (regnum),
1185                     REGISTER_RAW_SIZE (regnum));
1186               return;
1187         }
1188
1189       FRAME_FIND_SAVED_REGS(frame, fsr);
1190       if (fsr.regs[regnum] != 0)
1191         {
1192           if (lval)                     /* found it saved on the stack */
1193             *lval = lval_memory;
1194           if (regnum == SP_REGNUM)
1195             {
1196               if (raw_buffer)           /* SP register treated specially */
1197                 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), 
1198                                fsr.regs[regnum]);
1199             }
1200           else
1201             {
1202               if (addrp)                /* any other register */
1203                 *addrp = fsr.regs[regnum];
1204               if (raw_buffer)
1205                 read_memory (fsr.regs[regnum], raw_buffer, 
1206                              REGISTER_RAW_SIZE (regnum));
1207             }
1208           return;
1209         }
1210     }
1211
1212   /* If we get thru the loop to this point, it means the register was
1213      not saved in any frame.  Return the actual live-register value.  */
1214
1215   if (lval)                             /* found it in a live register */
1216     *lval = lval_register;
1217   if (addrp)
1218     *addrp = REGISTER_BYTE (regnum);
1219   if (raw_buffer)
1220     read_register_gen (regnum, raw_buffer);
1221 }
1222 #endif /* USE_GENERIC_DUMMY_FRAMES */
1223
1224 void
1225 _initialize_blockframe ()
1226 {
1227   obstack_init (&frame_cache_obstack);
1228 }