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