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