* blockframe.c (inside_main_func): Fix a typo in previous change.
[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, 1991, 1994 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "bfd.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "frame.h"
27 #include "gdbcore.h"
28 #include "value.h"              /* for read_register */
29 #include "target.h"             /* for target_has_stack */
30 #include "inferior.h"           /* for read_pc */
31 #include "annotate.h"
32
33 /* Is ADDR inside the startup file?  Note that if your machine
34    has a way to detect the bottom of the stack, there is no need
35    to call this function from FRAME_CHAIN_VALID; the reason for
36    doing so is that some machines have no way of detecting bottom
37    of stack. 
38
39    A PC of zero is always considered to be the bottom of the stack. */
40
41 int
42 inside_entry_file (addr)
43      CORE_ADDR addr;
44 {
45   if (addr == 0)
46     return 1;
47   if (symfile_objfile == 0)
48     return 0;
49 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
50   /* Do not stop backtracing if the pc is in the call dummy
51      at the entry point.  */
52   if (PC_IN_CALL_DUMMY (addr, 0, 0))
53     return 0;
54 #endif
55   return (addr >= symfile_objfile -> ei.entry_file_lowpc &&
56           addr <  symfile_objfile -> ei.entry_file_highpc);
57 }
58
59 /* Test a specified PC value to see if it is in the range of addresses
60    that correspond to the main() function.  See comments above for why
61    we might want to do this.
62
63    Typically called from FRAME_CHAIN_VALID.
64
65    A PC of zero is always considered to be the bottom of the stack. */
66
67 int
68 inside_main_func (pc)
69 CORE_ADDR pc;
70 {
71 struct symbol *mainsym;
72   if (pc == 0)
73     return 1;
74   if (symfile_objfile == 0)
75     return 0;
76
77   if (symfile_objfile -> ei.main_func_lowpc == 0 &&
78       symfile_objfile -> ei.main_func_highpc == 0)
79     {
80       mainsym = lookup_symbol ("main", NULL, VAR_NAMESPACE, NULL, NULL);
81       if (mainsym && SYMBOL_CLASS(mainsym) == LOC_BLOCK)
82         {
83           symfile_objfile->ei.main_func_lowpc = BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym));
84           symfile_objfile->ei.main_func_highpc = BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym));
85         }
86
87     }
88   return (symfile_objfile -> ei.main_func_lowpc  <= pc &&
89           symfile_objfile -> ei.main_func_highpc > pc);
90 }
91
92 /* Test a specified PC value to see if it is in the range of addresses
93    that correspond to the process entry point function.  See comments
94    in objfiles.h for why we might want to do this.
95
96    Typically called from FRAME_CHAIN_VALID.
97
98    A PC of zero is always considered to be the bottom of the stack. */
99
100 int
101 inside_entry_func (pc)
102 CORE_ADDR pc;
103 {
104   if (pc == 0)
105     return 1;
106   if (symfile_objfile == 0)
107     return 0;
108 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
109   /* Do not stop backtracing if the pc is in the call dummy
110      at the entry point.  */
111   if (PC_IN_CALL_DUMMY (pc, 0, 0))
112     return 0;
113 #endif
114   return (symfile_objfile -> ei.entry_func_lowpc  <= pc &&
115           symfile_objfile -> ei.entry_func_highpc > pc);
116 }
117
118 /* Info about the innermost stack frame (contents of FP register) */
119
120 static struct frame_info *current_frame;
121
122 /* Cache for frame addresses already read by gdb.  Valid only while
123    inferior is stopped.  Control variables for the frame cache should
124    be local to this module.  */
125
126 struct obstack frame_cache_obstack;
127
128 /* Return the innermost (currently executing) stack frame.  */
129
130 struct frame_info *
131 get_current_frame ()
132 {
133   if (current_frame == NULL)
134     {
135       if (target_has_stack)
136         current_frame = create_new_frame (read_fp (), read_pc ());
137       else
138         error ("No stack.");
139     }
140   return current_frame;
141 }
142
143 void
144 set_current_frame (frame)
145      struct frame_info *frame;
146 {
147   current_frame = frame;
148 }
149
150 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
151    Always returns a non-NULL value.  */
152
153 struct frame_info *
154 create_new_frame (addr, pc)
155      CORE_ADDR addr;
156      CORE_ADDR pc;
157 {
158   struct frame_info *fi;
159   char *name;
160
161   fi = (struct frame_info *)
162     obstack_alloc (&frame_cache_obstack,
163                    sizeof (struct frame_info));
164
165   /* Arbitrary frame */
166   fi->next = NULL;
167   fi->prev = NULL;
168   fi->frame = addr;
169   fi->pc = pc;
170   find_pc_partial_function (pc, &name, (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
171   fi->signal_handler_caller = IN_SIGTRAMP (fi->pc, name);
172
173 #ifdef INIT_EXTRA_FRAME_INFO
174   INIT_EXTRA_FRAME_INFO (0, fi);
175 #endif
176
177   return fi;
178 }
179
180 /* Return the frame that called FI.
181    If FI is the original frame (it has no caller), return 0.  */
182
183 struct frame_info *
184 get_prev_frame (frame)
185      struct frame_info *frame;
186 {
187   return get_prev_frame_info (frame);
188 }
189
190 /* Return the frame that FRAME calls (NULL if FRAME is the innermost
191    frame).  */
192
193 struct frame_info *
194 get_next_frame (frame)
195      struct frame_info *frame;
196 {
197   return frame->next;
198 }
199
200 /* Flush the entire frame cache.  */
201
202 void
203 flush_cached_frames ()
204 {
205   /* Since we can't really be sure what the first object allocated was */
206   obstack_free (&frame_cache_obstack, 0);
207   obstack_init (&frame_cache_obstack);
208
209   current_frame = NULL;  /* Invalidate cache */
210   select_frame (NULL, -1);
211   annotate_frames_invalid ();
212 }
213
214 /* Flush the frame cache, and start a new one if necessary.  */
215
216 void
217 reinit_frame_cache ()
218 {
219   flush_cached_frames ();
220
221   /* FIXME: The inferior_pid test is wrong if there is a corefile.  */
222   if (inferior_pid != 0)
223     {
224       select_frame (get_current_frame (), 0);
225     }
226 }
227
228 /* If a machine allows frameless functions, it should define a macro
229    FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h.  FI is the struct
230    frame_info for the frame, and FRAMELESS should be set to nonzero
231    if it represents a frameless function invocation.  */
232
233 /* Return nonzero if the function for this frame lacks a prologue.  Many
234    machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
235    function.  */
236
237 int
238 frameless_look_for_prologue (frame)
239      struct frame_info *frame;
240 {
241   CORE_ADDR func_start, after_prologue;
242   func_start = (get_pc_function_start (frame->pc) + FUNCTION_START_OFFSET);
243   if (func_start)
244     {
245       after_prologue = func_start;
246 #ifdef SKIP_PROLOGUE_FRAMELESS_P
247       /* This is faster, since only care whether there *is* a prologue,
248          not how long it is.  */
249       SKIP_PROLOGUE_FRAMELESS_P (after_prologue);
250 #else
251       SKIP_PROLOGUE (after_prologue);
252 #endif
253       return after_prologue == func_start;
254     }
255   else
256     /* If we can't find the start of the function, we don't really
257        know whether the function is frameless, but we should be able
258        to get a reasonable (i.e. best we can do under the
259        circumstances) backtrace by saying that it isn't.  */
260     return 0;
261 }
262
263 /* Default a few macros that people seldom redefine.  */
264
265 #if !defined (INIT_FRAME_PC)
266 #define INIT_FRAME_PC(fromleaf, prev) \
267   prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
268               prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
269 #endif
270
271 #ifndef FRAME_CHAIN_COMBINE
272 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
273 #endif
274
275 /* Return a structure containing various interesting information
276    about the frame that called NEXT_FRAME.  Returns NULL
277    if there is no such frame.  */
278
279 struct frame_info *
280 get_prev_frame_info (next_frame)
281      struct frame_info *next_frame;
282 {
283   CORE_ADDR address = 0;
284   struct frame_info *prev;
285   int fromleaf = 0;
286   char *name;
287
288   /* If the requested entry is in the cache, return it.
289      Otherwise, figure out what the address should be for the entry
290      we're about to add to the cache. */
291
292   if (!next_frame)
293     {
294 #if 0
295       /* This screws value_of_variable, which just wants a nice clean
296          NULL return from block_innermost_frame if there are no frames.
297          I don't think I've ever seen this message happen otherwise.
298          And returning NULL here is a perfectly legitimate thing to do.  */
299       if (!current_frame)
300         {
301           error ("You haven't set up a process's stack to examine.");
302         }
303 #endif
304
305       return current_frame;
306     }
307
308   /* If we have the prev one, return it */
309   if (next_frame->prev)
310     return next_frame->prev;
311
312   /* On some machines it is possible to call a function without
313      setting up a stack frame for it.  On these machines, we
314      define this macro to take two args; a frameinfo pointer
315      identifying a frame and a variable to set or clear if it is
316      or isn't leafless.  */
317 #ifdef FRAMELESS_FUNCTION_INVOCATION
318   /* Still don't want to worry about this except on the innermost
319      frame.  This macro will set FROMLEAF if NEXT_FRAME is a
320      frameless function invocation.  */
321   if (!(next_frame->next))
322     {
323       FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
324       if (fromleaf)
325         address = FRAME_FP (next_frame);
326     }
327 #endif
328
329   if (!fromleaf)
330     {
331       /* Two macros defined in tm.h specify the machine-dependent
332          actions to be performed here.
333          First, get the frame's chain-pointer.
334          If that is zero, the frame is the outermost frame or a leaf
335          called by the outermost frame.  This means that if start
336          calls main without a frame, we'll return 0 (which is fine
337          anyway).
338
339          Nope; there's a problem.  This also returns when the current
340          routine is a leaf of main.  This is unacceptable.  We move
341          this to after the ffi test; I'd rather have backtraces from
342          start go curfluy than have an abort called from main not show
343          main.  */
344       address = FRAME_CHAIN (next_frame);
345       if (!FRAME_CHAIN_VALID (address, next_frame))
346         return 0;
347       address = FRAME_CHAIN_COMBINE (address, next_frame);
348     }
349   if (address == 0)
350     return 0;
351
352   prev = (struct frame_info *)
353     obstack_alloc (&frame_cache_obstack,
354                    sizeof (struct frame_info));
355
356   if (next_frame)
357     next_frame->prev = prev;
358   prev->next = next_frame;
359   prev->prev = (struct frame_info *) 0;
360   prev->frame = address;
361   prev->signal_handler_caller = 0;
362
363 /* This change should not be needed, FIXME!  We should
364    determine whether any targets *need* INIT_FRAME_PC to happen
365    after INIT_EXTRA_FRAME_INFO and come up with a simple way to
366    express what goes on here.
367
368       INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
369                 (where the PC is already set up) and here (where it isn't).
370       INIT_FRAME_PC is only called from here, always after
371                 INIT_EXTRA_FRAME_INFO.
372    
373    The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
374    value (which hasn't been set yet).  Some other machines appear to
375    require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC.  Phoo.
376
377    We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
378    an already overcomplicated part of GDB.   gnu@cygnus.com, 15Sep92.
379
380    Assuming that some machines need INIT_FRAME_PC after
381    INIT_EXTRA_FRAME_INFO, one possible scheme:
382
383    SETUP_INNERMOST_FRAME()
384      Default version is just create_new_frame (read_fp ()),
385      read_pc ()).  Machines with extra frame info would do that (or the
386      local equivalent) and then set the extra fields.
387    SETUP_ARBITRARY_FRAME(argc, argv)
388      Only change here is that create_new_frame would no longer init extra
389      frame info; SETUP_ARBITRARY_FRAME would have to do that.
390    INIT_PREV_FRAME(fromleaf, prev)
391      Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC.  This should
392      also return a flag saying whether to keep the new frame, or
393      whether to discard it, because on some machines (e.g.  mips) it
394      is really awkward to have FRAME_CHAIN_VALID called *before*
395      INIT_EXTRA_FRAME_INFO (there is no good way to get information
396      deduced in FRAME_CHAIN_VALID into the extra fields of the new frame).
397    std_frame_pc(fromleaf, prev)
398      This is the default setting for INIT_PREV_FRAME.  It just does what
399      the default INIT_FRAME_PC does.  Some machines will call it from
400      INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
401      Some machines won't use it.
402    kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94.  */
403
404 #ifdef INIT_FRAME_PC_FIRST
405   INIT_FRAME_PC_FIRST (fromleaf, prev);
406 #endif
407
408 #ifdef INIT_EXTRA_FRAME_INFO
409   INIT_EXTRA_FRAME_INFO(fromleaf, prev);
410 #endif
411
412   /* This entry is in the frame queue now, which is good since
413      FRAME_SAVED_PC may use that queue to figure out its value
414      (see tm-sparc.h).  We want the pc saved in the inferior frame. */
415   INIT_FRAME_PC(fromleaf, prev);
416
417   /* If ->frame and ->pc are unchanged, we are in the process of getting
418      ourselves into an infinite backtrace.  Some architectures check this
419      in FRAME_CHAIN or thereabouts, but it seems like there is no reason
420      this can't be an architecture-independent check.  */
421   if (next_frame != NULL)
422     {
423       if (prev->frame == next_frame->frame
424           && prev->pc == next_frame->pc)
425         {
426           next_frame->prev = NULL;
427           obstack_free (&frame_cache_obstack, prev);
428           return NULL;
429         }
430     }
431
432   find_pc_partial_function (prev->pc, &name,
433                             (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
434   if (IN_SIGTRAMP (prev->pc, name))
435     prev->signal_handler_caller = 1;
436
437   return prev;
438 }
439
440 CORE_ADDR
441 get_frame_pc (frame)
442      struct frame_info *frame;
443 {
444   return frame->pc;
445 }
446
447 #if defined (FRAME_FIND_SAVED_REGS)
448 /* Find the addresses in which registers are saved in FRAME.  */
449
450 void
451 get_frame_saved_regs (frame, saved_regs_addr)
452      struct frame_info *frame;
453      struct frame_saved_regs *saved_regs_addr;
454 {
455   FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr);
456 }
457 #endif
458
459 /* Return the innermost lexical block in execution
460    in a specified stack frame.  The frame address is assumed valid.  */
461
462 struct block *
463 get_frame_block (frame)
464      struct frame_info *frame;
465 {
466   CORE_ADDR pc;
467
468   pc = frame->pc;
469   if (frame->next != 0 && frame->next->signal_handler_caller == 0)
470     /* We are not in the innermost frame and we were not interrupted
471        by a signal.  We need to subtract one to get the correct block,
472        in case the call instruction was the last instruction of the block.
473        If there are any machines on which the saved pc does not point to
474        after the call insn, we probably want to make frame->pc point after
475        the call insn anyway.  */
476     --pc;
477   return block_for_pc (pc);
478 }
479
480 struct block *
481 get_current_block ()
482 {
483   return block_for_pc (read_pc ());
484 }
485
486 CORE_ADDR
487 get_pc_function_start (pc)
488      CORE_ADDR pc;
489 {
490   register struct block *bl;
491   register struct symbol *symbol;
492   register struct minimal_symbol *msymbol;
493   CORE_ADDR fstart;
494
495   if ((bl = block_for_pc (pc)) != NULL &&
496       (symbol = block_function (bl)) != NULL)
497     {
498       bl = SYMBOL_BLOCK_VALUE (symbol);
499       fstart = BLOCK_START (bl);
500     }
501   else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
502     {
503       fstart = SYMBOL_VALUE_ADDRESS (msymbol);
504     }
505   else
506     {
507       fstart = 0;
508     }
509   return (fstart);
510 }
511
512 /* Return the symbol for the function executing in frame FRAME.  */
513
514 struct symbol *
515 get_frame_function (frame)
516      struct frame_info *frame;
517 {
518   register struct block *bl = get_frame_block (frame);
519   if (bl == 0)
520     return 0;
521   return block_function (bl);
522 }
523 \f
524 /* Return the blockvector immediately containing the innermost lexical block
525    containing the specified pc value, or 0 if there is none.
526    PINDEX is a pointer to the index value of the block.  If PINDEX
527    is NULL, we don't pass this information back to the caller.  */
528
529 struct blockvector *
530 blockvector_for_pc (pc, pindex)
531      register CORE_ADDR pc;
532      int *pindex;
533 {
534   register struct block *b;
535   register int bot, top, half;
536   register struct symtab *s;
537   struct blockvector *bl;
538
539   /* First search all symtabs for one whose file contains our pc */
540   s = find_pc_symtab (pc);
541   if (s == 0)
542     return 0;
543
544   bl = BLOCKVECTOR (s);
545   b = BLOCKVECTOR_BLOCK (bl, 0);
546
547   /* Then search that symtab for the smallest block that wins.  */
548   /* Use binary search to find the last block that starts before PC.  */
549
550   bot = 0;
551   top = BLOCKVECTOR_NBLOCKS (bl);
552
553   while (top - bot > 1)
554     {
555       half = (top - bot + 1) >> 1;
556       b = BLOCKVECTOR_BLOCK (bl, bot + half);
557       if (BLOCK_START (b) <= pc)
558         bot += half;
559       else
560         top = bot + half;
561     }
562
563   /* Now search backward for a block that ends after PC.  */
564
565   while (bot >= 0)
566     {
567       b = BLOCKVECTOR_BLOCK (bl, bot);
568       if (BLOCK_END (b) > pc)
569         {
570           if (pindex)
571             *pindex = bot;
572           return bl;
573         }
574       bot--;
575     }
576
577   return 0;
578 }
579
580 /* Return the innermost lexical block containing the specified pc value,
581    or 0 if there is none.  */
582
583 struct block *
584 block_for_pc (pc)
585      register CORE_ADDR pc;
586 {
587   register struct blockvector *bl;
588   int index;
589
590   bl = blockvector_for_pc (pc, &index);
591   if (bl)
592     return BLOCKVECTOR_BLOCK (bl, index);
593   return 0;
594 }
595
596 /* Return the function containing pc value PC.
597    Returns 0 if function is not known.  */
598
599 struct symbol *
600 find_pc_function (pc)
601      CORE_ADDR pc;
602 {
603   register struct block *b = block_for_pc (pc);
604   if (b == 0)
605     return 0;
606   return block_function (b);
607 }
608
609 /* These variables are used to cache the most recent result
610  * of find_pc_partial_function. */
611
612 static CORE_ADDR cache_pc_function_low = 0;
613 static CORE_ADDR cache_pc_function_high = 0;
614 static char *cache_pc_function_name = 0;
615
616 /* Clear cache, e.g. when symbol table is discarded. */
617
618 void
619 clear_pc_function_cache()
620 {
621   cache_pc_function_low = 0;
622   cache_pc_function_high = 0;
623   cache_pc_function_name = (char *)0;
624 }
625
626 /* Finds the "function" (text symbol) that is smaller than PC but
627    greatest of all of the potential text symbols.  Sets *NAME and/or
628    *ADDRESS conditionally if that pointer is non-null.  If ENDADDR is
629    non-null, then set *ENDADDR to be the end of the function
630    (exclusive), but passing ENDADDR as non-null means that the
631    function might cause symbols to be read.  This function either
632    succeeds or fails (not halfway succeeds).  If it succeeds, it sets
633    *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
634    If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero
635    and returns 0.  */
636
637 int
638 find_pc_partial_function (pc, name, address, endaddr)
639      CORE_ADDR pc;
640      char **name;
641      CORE_ADDR *address;
642      CORE_ADDR *endaddr;
643 {
644   struct partial_symtab *pst;
645   struct symbol *f;
646   struct minimal_symbol *msymbol;
647   struct partial_symbol *psb;
648   struct obj_section *sec;
649
650   if (pc >= cache_pc_function_low && pc < cache_pc_function_high)
651     goto return_cached_value;
652
653   /* If sigtramp is in the u area, it counts as a function (especially
654      important for step_1).  */
655 #if defined SIGTRAMP_START
656   if (IN_SIGTRAMP (pc, (char *)NULL))
657     {
658       cache_pc_function_low = SIGTRAMP_START;
659       cache_pc_function_high = SIGTRAMP_END;
660       cache_pc_function_name = "<sigtramp>";
661
662       goto return_cached_value;
663     }
664 #endif
665
666   msymbol = lookup_minimal_symbol_by_pc (pc);
667   pst = find_pc_psymtab (pc);
668   if (pst)
669     {
670       /* Need to read the symbols to get a good value for the end address.  */
671       if (endaddr != NULL && !pst->readin)
672         {
673           /* Need to get the terminal in case symbol-reading produces
674              output.  */
675           target_terminal_ours_for_output ();
676           PSYMTAB_TO_SYMTAB (pst);
677         }
678
679       if (pst->readin)
680         {
681           /* Checking whether the msymbol has a larger value is for the
682              "pathological" case mentioned in print_frame_info.  */
683           f = find_pc_function (pc);
684           if (f != NULL
685               && (msymbol == NULL
686                   || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
687                       >= SYMBOL_VALUE_ADDRESS (msymbol))))
688             {
689               cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
690               cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
691               cache_pc_function_name = SYMBOL_NAME (f);
692               goto return_cached_value;
693             }
694         }
695       else
696         {
697           /* Now that static symbols go in the minimal symbol table, perhaps
698              we could just ignore the partial symbols.  But at least for now
699              we use the partial or minimal symbol, whichever is larger.  */
700           psb = find_pc_psymbol (pst, pc);
701
702           if (psb
703               && (msymbol == NULL ||
704                   (SYMBOL_VALUE_ADDRESS (psb)
705                    >= SYMBOL_VALUE_ADDRESS (msymbol))))
706             {
707               /* This case isn't being cached currently. */
708               if (address)
709                 *address = SYMBOL_VALUE_ADDRESS (psb);
710               if (name)
711                 *name = SYMBOL_NAME (psb);
712               /* endaddr non-NULL can't happen here.  */
713               return 1;
714             }
715         }
716     }
717
718   /* Not in the normal symbol tables, see if the pc is in a known section.
719      If it's not, then give up.  This ensures that anything beyond the end
720      of the text seg doesn't appear to be part of the last function in the
721      text segment.  */
722
723   sec = find_pc_section (pc);
724
725   if (!sec)
726     msymbol = NULL;
727
728   /* Must be in the minimal symbol table.  */
729   if (msymbol == NULL)
730     {
731       /* No available symbol.  */
732       if (name != NULL)
733         *name = 0;
734       if (address != NULL)
735         *address = 0;
736       if (endaddr != NULL)
737         *endaddr = 0;
738       return 0;
739     }
740
741   /* See if we're in a transfer table for Sun shared libs.
742
743      Note the hack for Sun shared library transfer tables creates
744      problems for single stepping through the return path from a shared
745      library call if the return path includes trampoline code.
746
747      I don't really understand the reasoning behind the magic handling
748      for mst_trampoline symbols.  */
749
750 #ifdef INHIBIT_SUNSOLIB_TRANSFER_TABLE_HACK
751     cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
752 #else
753   if (msymbol -> type == mst_text || msymbol -> type == mst_file_text)
754     cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
755   else
756     /* It is a transfer table for Sun shared libraries.  */
757     cache_pc_function_low = pc - FUNCTION_START_OFFSET;
758 #endif
759
760   cache_pc_function_name = SYMBOL_NAME (msymbol);
761
762   /* Use the lesser of the next minimal symbol, or the end of the section, as
763      the end of the function.  */
764
765   if (SYMBOL_NAME (msymbol + 1) != NULL
766       && SYMBOL_VALUE_ADDRESS (msymbol + 1) < sec->endaddr)
767     cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + 1);
768   else
769     /* We got the start address from the last msymbol in the objfile.
770        So the end address is the end of the section.  */
771     cache_pc_function_high = sec->endaddr;
772
773  return_cached_value:
774   if (address)
775     *address = cache_pc_function_low;
776   if (name)
777     *name = cache_pc_function_name;
778   if (endaddr)
779     *endaddr = cache_pc_function_high;
780   return 1;
781 }
782
783 /* Return the innermost stack frame executing inside of BLOCK,
784    or NULL if there is no such frame.  If BLOCK is NULL, just return NULL.  */
785
786 struct frame_info *
787 block_innermost_frame (block)
788      struct block *block;
789 {
790   struct frame_info *frame;
791   register CORE_ADDR start;
792   register CORE_ADDR end;
793
794   if (block == NULL)
795     return NULL;
796
797   start = BLOCK_START (block);
798   end = BLOCK_END (block);
799
800   frame = NULL;
801   while (1)
802     {
803       frame = get_prev_frame (frame);
804       if (frame == NULL)
805         return NULL;
806       if (frame->pc >= start && frame->pc < end)
807         return frame;
808     }
809 }
810
811 /* Return the full FRAME which corresponds to the given CORE_ADDR
812    or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
813
814 struct frame_info *
815 find_frame_addr_in_frame_chain (frame_addr)
816      CORE_ADDR frame_addr;
817 {
818   struct frame_info *frame = NULL;
819
820   if (frame_addr == (CORE_ADDR)0)
821     return NULL;
822
823   while (1)
824     {
825       frame = get_prev_frame (frame);
826       if (frame == NULL)
827         return NULL;
828       if (FRAME_FP (frame) == frame_addr)
829         return frame;
830     }
831 }
832
833 #ifdef SIGCONTEXT_PC_OFFSET
834 /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp.  */
835
836 CORE_ADDR
837 sigtramp_saved_pc (frame)
838      struct frame_info *frame;
839 {
840   CORE_ADDR sigcontext_addr;
841   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
842   int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT;
843   int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT;
844
845   /* Get sigcontext address, it is the third parameter on the stack.  */
846   if (frame->next)
847     sigcontext_addr = read_memory_integer (FRAME_ARGS_ADDRESS (frame->next)
848                                            + FRAME_ARGS_SKIP
849                                            + sigcontext_offs,
850                                            ptrbytes);
851   else
852     sigcontext_addr = read_memory_integer (read_register (SP_REGNUM)
853                                             + sigcontext_offs,
854                                            ptrbytes);
855
856   /* Don't cause a memory_error when accessing sigcontext in case the stack
857      layout has changed or the stack is corrupt.  */
858   target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes);
859   return extract_unsigned_integer (buf, ptrbytes);
860 }
861 #endif /* SIGCONTEXT_PC_OFFSET */
862
863 void
864 _initialize_blockframe ()
865 {
866   obstack_init (&frame_cache_obstack);
867 }