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