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