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.
5 This file is part of GDB.
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.
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.
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. */
28 #include "value.h" /* for read_register */
29 #include "target.h" /* for target_has_stack */
30 #include "inferior.h" /* for read_pc */
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
39 A PC of zero is always considered to be the bottom of the stack. */
42 inside_entry_file (addr
)
47 if (symfile_objfile
== 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))
55 return (addr
>= symfile_objfile
-> ei
.entry_file_lowpc
&&
56 addr
< symfile_objfile
-> ei
.entry_file_highpc
);
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.
63 Typically called from FRAME_CHAIN_VALID.
65 A PC of zero is always considered to be the bottom of the stack. */
73 if (symfile_objfile
== 0)
75 return (symfile_objfile
-> ei
.main_func_lowpc
<= pc
&&
76 symfile_objfile
-> ei
.main_func_highpc
> pc
);
79 /* Test a specified PC value to see if it is in the range of addresses
80 that correspond to the process entry point function. See comments
81 in objfiles.h for why we might want to do this.
83 Typically called from FRAME_CHAIN_VALID.
85 A PC of zero is always considered to be the bottom of the stack. */
88 inside_entry_func (pc
)
93 if (symfile_objfile
== 0)
95 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
96 /* Do not stop backtracing if the pc is in the call dummy
97 at the entry point. */
98 if (PC_IN_CALL_DUMMY (pc
, 0, 0))
101 return (symfile_objfile
-> ei
.entry_func_lowpc
<= pc
&&
102 symfile_objfile
-> ei
.entry_func_highpc
> pc
);
105 /* Address of innermost stack frame (contents of FP register) */
107 static FRAME current_frame
;
110 * Cache for frame addresses already read by gdb. Valid only while
111 * inferior is stopped. Control variables for the frame cache should
112 * be local to this module.
114 struct obstack frame_cache_obstack
;
116 /* Return the innermost (currently executing) stack frame. */
121 if (current_frame
== NULL
)
123 if (target_has_stack
)
124 current_frame
= create_new_frame (read_fp (), read_pc ());
128 return current_frame
;
132 set_current_frame (frame
)
135 current_frame
= frame
;
138 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
139 Always returns a non-NULL value. */
142 create_new_frame (addr
, pc
)
146 struct frame_info
*fci
; /* Same type as FRAME */
149 fci
= (struct frame_info
*)
150 obstack_alloc (&frame_cache_obstack
,
151 sizeof (struct frame_info
));
153 /* Arbitrary frame */
154 fci
->next
= (struct frame_info
*) 0;
155 fci
->prev
= (struct frame_info
*) 0;
158 find_pc_partial_function (pc
, &name
, (CORE_ADDR
*)NULL
,(CORE_ADDR
*)NULL
);
159 fci
->signal_handler_caller
= IN_SIGTRAMP (fci
->pc
, name
);
161 #ifdef INIT_EXTRA_FRAME_INFO
162 INIT_EXTRA_FRAME_INFO (0, fci
);
168 /* Return the frame that called FRAME.
169 If FRAME is the original frame (it has no caller), return 0. */
172 get_prev_frame (frame
)
175 /* We're allowed to know that FRAME and "struct frame_info *" are
177 return get_prev_frame_info (frame
);
180 /* Return the frame that FRAME calls (0 if FRAME is the innermost
184 get_next_frame (frame
)
187 /* We're allowed to know that FRAME and "struct frame_info *" are
193 * Flush the entire frame cache.
196 flush_cached_frames ()
198 /* Since we can't really be sure what the first object allocated was */
199 obstack_free (&frame_cache_obstack
, 0);
200 obstack_init (&frame_cache_obstack
);
202 current_frame
= (struct frame_info
*) 0; /* Invalidate cache */
203 select_frame ((FRAME
) 0, -1);
204 annotate_frames_invalid ();
207 /* Flush the frame cache, and start a new one if necessary. */
210 reinit_frame_cache ()
212 flush_cached_frames ();
214 /* The inferior_pid test is wrong if there is a corefile. But I don't
215 think this code is needed at all, now that get_current_frame will
216 create the frame if it is needed. */
217 if (inferior_pid
!= 0)
219 set_current_frame (create_new_frame (read_fp (), read_pc ()));
220 select_frame (get_current_frame (), 0);
224 set_current_frame (0);
225 select_frame ((FRAME
) 0, -1);
230 /* Return a structure containing various interesting information
231 about a specified stack frame. */
232 /* How do I justify including this function? Well, the FRAME
233 identifier format has gone through several changes recently, and
234 it's not completely inconceivable that it could happen again. If
235 it does, have this routine around will help */
238 get_frame_info (frame
)
244 /* If a machine allows frameless functions, it should define a macro
245 FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct
246 frame_info for the frame, and FRAMELESS should be set to nonzero
247 if it represents a frameless function invocation. */
249 /* Return nonzero if the function for this frame lacks a prologue. Many
250 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
254 frameless_look_for_prologue (frame
)
257 CORE_ADDR func_start
, after_prologue
;
258 func_start
= (get_pc_function_start (frame
->pc
) +
259 FUNCTION_START_OFFSET
);
262 after_prologue
= func_start
;
263 #ifdef SKIP_PROLOGUE_FRAMELESS_P
264 /* This is faster, since only care whether there *is* a prologue,
265 not how long it is. */
266 SKIP_PROLOGUE_FRAMELESS_P (after_prologue
);
268 SKIP_PROLOGUE (after_prologue
);
270 return after_prologue
== func_start
;
273 /* If we can't find the start of the function, we don't really
274 know whether the function is frameless, but we should be able
275 to get a reasonable (i.e. best we can do under the
276 circumstances) backtrace by saying that it isn't. */
280 /* Default a few macros that people seldom redefine. */
282 #if !defined (INIT_FRAME_PC)
283 #define INIT_FRAME_PC(fromleaf, prev) \
284 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
285 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
288 #ifndef FRAME_CHAIN_COMBINE
289 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
292 /* Return a structure containing various interesting information
293 about the frame that called NEXT_FRAME. Returns NULL
294 if there is no such frame. */
297 get_prev_frame_info (next_frame
)
300 FRAME_ADDR address
= 0;
301 struct frame_info
*prev
;
305 /* If the requested entry is in the cache, return it.
306 Otherwise, figure out what the address should be for the entry
307 we're about to add to the cache. */
312 /* This screws value_of_variable, which just wants a nice clean
313 NULL return from block_innermost_frame if there are no frames.
314 I don't think I've ever seen this message happen otherwise.
315 And returning NULL here is a perfectly legitimate thing to do. */
318 error ("You haven't set up a process's stack to examine.");
322 return current_frame
;
325 /* If we have the prev one, return it */
326 if (next_frame
->prev
)
327 return next_frame
->prev
;
329 /* On some machines it is possible to call a function without
330 setting up a stack frame for it. On these machines, we
331 define this macro to take two args; a frameinfo pointer
332 identifying a frame and a variable to set or clear if it is
333 or isn't leafless. */
334 #ifdef FRAMELESS_FUNCTION_INVOCATION
335 /* Still don't want to worry about this except on the innermost
336 frame. This macro will set FROMLEAF if NEXT_FRAME is a
337 frameless function invocation. */
338 if (!(next_frame
->next
))
340 FRAMELESS_FUNCTION_INVOCATION (next_frame
, fromleaf
);
342 address
= next_frame
->frame
;
348 /* Two macros defined in tm.h specify the machine-dependent
349 actions to be performed here.
350 First, get the frame's chain-pointer.
351 If that is zero, the frame is the outermost frame or a leaf
352 called by the outermost frame. This means that if start
353 calls main without a frame, we'll return 0 (which is fine
356 Nope; there's a problem. This also returns when the current
357 routine is a leaf of main. This is unacceptable. We move
358 this to after the ffi test; I'd rather have backtraces from
359 start go curfluy than have an abort called from main not show
361 address
= FRAME_CHAIN (next_frame
);
362 if (!FRAME_CHAIN_VALID (address
, next_frame
))
364 address
= FRAME_CHAIN_COMBINE (address
, next_frame
);
369 prev
= (struct frame_info
*)
370 obstack_alloc (&frame_cache_obstack
,
371 sizeof (struct frame_info
));
374 next_frame
->prev
= prev
;
375 prev
->next
= next_frame
;
376 prev
->prev
= (struct frame_info
*) 0;
377 prev
->frame
= address
;
378 prev
->signal_handler_caller
= 0;
380 /* This change should not be needed, FIXME! We should
381 determine whether any targets *need* INIT_FRAME_PC to happen
382 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
383 express what goes on here.
385 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
386 (where the PC is already set up) and here (where it isn't).
387 INIT_FRAME_PC is only called from here, always after
388 INIT_EXTRA_FRAME_INFO.
390 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
391 value (which hasn't been set yet). Some other machines appear to
392 require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo.
394 We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
395 an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92.
397 To answer the question, yes the sparc needs INIT_FRAME_PC after
398 INIT_EXTRA_FRAME_INFO. Suggested scheme:
400 SETUP_INNERMOST_FRAME()
401 Default version is just create_new_frame (read_fp ()),
402 read_pc ()). Machines with extra frame info would do that (or the
403 local equivalent) and then set the extra fields.
404 SETUP_ARBITRARY_FRAME(argc, argv)
405 Only change here is that create_new_frame would no longer init extra
406 frame info; SETUP_ARBITRARY_FRAME would have to do that.
407 INIT_PREV_FRAME(fromleaf, prev)
408 Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC. This should
409 also return a flag saying whether to keep the new frame, or
410 whether to discard it, because on some machines (e.g. mips) it
411 is really awkward to have FRAME_CHAIN_VALID called *before*
412 INIT_EXTRA_FRAME_INFO (there is no good way to get information
413 deduced in FRAME_CHAIN_VALID into the extra fields of the new frame).
414 std_frame_pc(fromleaf, prev)
415 This is the default setting for INIT_PREV_FRAME. It just does what
416 the default INIT_FRAME_PC does. Some machines will call it from
417 INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
418 Some machines won't use it.
419 kingdon@cygnus.com, 13Apr93, 31Jan94. */
421 #ifdef INIT_FRAME_PC_FIRST
422 INIT_FRAME_PC_FIRST (fromleaf
, prev
);
425 #ifdef INIT_EXTRA_FRAME_INFO
426 INIT_EXTRA_FRAME_INFO(fromleaf
, prev
);
429 /* This entry is in the frame queue now, which is good since
430 FRAME_SAVED_PC may use that queue to figure out its value
431 (see tm-sparc.h). We want the pc saved in the inferior frame. */
432 INIT_FRAME_PC(fromleaf
, prev
);
434 /* If ->frame and ->pc are unchanged, we are in the process of getting
435 ourselves into an infinite backtrace. Some architectures check this
436 in FRAME_CHAIN or thereabouts, but it seems like there is no reason
437 this can't be an architecture-independent check. */
438 if (next_frame
!= NULL
)
440 if (prev
->frame
== next_frame
->frame
441 && prev
->pc
== next_frame
->pc
)
443 next_frame
->prev
= NULL
;
444 obstack_free (&frame_cache_obstack
, prev
);
449 find_pc_partial_function (prev
->pc
, &name
,
450 (CORE_ADDR
*)NULL
,(CORE_ADDR
*)NULL
);
451 if (IN_SIGTRAMP (prev
->pc
, name
))
452 prev
->signal_handler_caller
= 1;
461 struct frame_info
*fi
;
462 fi
= get_frame_info (frame
);
466 #if defined (FRAME_FIND_SAVED_REGS)
467 /* Find the addresses in which registers are saved in FRAME. */
470 get_frame_saved_regs (frame_info_addr
, saved_regs_addr
)
471 struct frame_info
*frame_info_addr
;
472 struct frame_saved_regs
*saved_regs_addr
;
474 FRAME_FIND_SAVED_REGS (frame_info_addr
, *saved_regs_addr
);
478 /* Return the innermost lexical block in execution
479 in a specified stack frame. The frame address is assumed valid. */
482 get_frame_block (frame
)
485 struct frame_info
*fi
;
488 fi
= get_frame_info (frame
);
491 if (fi
->next
!= 0 && fi
->next
->signal_handler_caller
== 0)
492 /* We are not in the innermost frame and we were not interrupted
493 by a signal. We need to subtract one to get the correct block,
494 in case the call instruction was the last instruction of the block.
495 If there are any machines on which the saved pc does not point to
496 after the call insn, we probably want to make fi->pc point after
497 the call insn anyway. */
499 return block_for_pc (pc
);
505 return block_for_pc (read_pc ());
509 get_pc_function_start (pc
)
512 register struct block
*bl
;
513 register struct symbol
*symbol
;
514 register struct minimal_symbol
*msymbol
;
517 if ((bl
= block_for_pc (pc
)) != NULL
&&
518 (symbol
= block_function (bl
)) != NULL
)
520 bl
= SYMBOL_BLOCK_VALUE (symbol
);
521 fstart
= BLOCK_START (bl
);
523 else if ((msymbol
= lookup_minimal_symbol_by_pc (pc
)) != NULL
)
525 fstart
= SYMBOL_VALUE_ADDRESS (msymbol
);
534 /* Return the symbol for the function executing in frame FRAME. */
537 get_frame_function (frame
)
540 register struct block
*bl
= get_frame_block (frame
);
543 return block_function (bl
);
546 /* Return the blockvector immediately containing the innermost lexical block
547 containing the specified pc value, or 0 if there is none.
548 PINDEX is a pointer to the index value of the block. If PINDEX
549 is NULL, we don't pass this information back to the caller. */
552 blockvector_for_pc (pc
, pindex
)
553 register CORE_ADDR pc
;
556 register struct block
*b
;
557 register int bot
, top
, half
;
558 register struct symtab
*s
;
559 struct blockvector
*bl
;
561 /* First search all symtabs for one whose file contains our pc */
562 s
= find_pc_symtab (pc
);
566 bl
= BLOCKVECTOR (s
);
567 b
= BLOCKVECTOR_BLOCK (bl
, 0);
569 /* Then search that symtab for the smallest block that wins. */
570 /* Use binary search to find the last block that starts before PC. */
573 top
= BLOCKVECTOR_NBLOCKS (bl
);
575 while (top
- bot
> 1)
577 half
= (top
- bot
+ 1) >> 1;
578 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
579 if (BLOCK_START (b
) <= pc
)
585 /* Now search backward for a block that ends after PC. */
589 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
590 if (BLOCK_END (b
) > pc
)
602 /* Return the innermost lexical block containing the specified pc value,
603 or 0 if there is none. */
607 register CORE_ADDR pc
;
609 register struct blockvector
*bl
;
612 bl
= blockvector_for_pc (pc
, &index
);
614 return BLOCKVECTOR_BLOCK (bl
, index
);
618 /* Return the function containing pc value PC.
619 Returns 0 if function is not known. */
622 find_pc_function (pc
)
625 register struct block
*b
= block_for_pc (pc
);
628 return block_function (b
);
631 /* These variables are used to cache the most recent result
632 * of find_pc_partial_function. */
634 static CORE_ADDR cache_pc_function_low
= 0;
635 static CORE_ADDR cache_pc_function_high
= 0;
636 static char *cache_pc_function_name
= 0;
638 /* Clear cache, e.g. when symbol table is discarded. */
641 clear_pc_function_cache()
643 cache_pc_function_low
= 0;
644 cache_pc_function_high
= 0;
645 cache_pc_function_name
= (char *)0;
648 /* Finds the "function" (text symbol) that is smaller than PC but
649 greatest of all of the potential text symbols. Sets *NAME and/or
650 *ADDRESS conditionally if that pointer is non-null. If ENDADDR is
651 non-null, then set *ENDADDR to be the end of the function
652 (exclusive), but passing ENDADDR as non-null means that the
653 function might cause symbols to be read. This function either
654 succeeds or fails (not halfway succeeds). If it succeeds, it sets
655 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
656 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero
660 find_pc_partial_function (pc
, name
, address
, endaddr
)
666 struct partial_symtab
*pst
;
668 struct minimal_symbol
*msymbol
;
669 struct partial_symbol
*psb
;
670 struct obj_section
*sec
;
672 if (pc
>= cache_pc_function_low
&& pc
< cache_pc_function_high
)
673 goto return_cached_value
;
675 /* If sigtramp is in the u area, it counts as a function (especially
676 important for step_1). */
677 #if defined SIGTRAMP_START
678 if (IN_SIGTRAMP (pc
, (char *)NULL
))
680 cache_pc_function_low
= SIGTRAMP_START
;
681 cache_pc_function_high
= SIGTRAMP_END
;
682 cache_pc_function_name
= "<sigtramp>";
684 goto return_cached_value
;
688 msymbol
= lookup_minimal_symbol_by_pc (pc
);
689 pst
= find_pc_psymtab (pc
);
692 /* Need to read the symbols to get a good value for the end address. */
693 if (endaddr
!= NULL
&& !pst
->readin
)
695 /* Need to get the terminal in case symbol-reading produces
697 target_terminal_ours_for_output ();
698 PSYMTAB_TO_SYMTAB (pst
);
703 /* Checking whether the msymbol has a larger value is for the
704 "pathological" case mentioned in print_frame_info. */
705 f
= find_pc_function (pc
);
708 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f
))
709 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
711 cache_pc_function_low
= BLOCK_START (SYMBOL_BLOCK_VALUE (f
));
712 cache_pc_function_high
= BLOCK_END (SYMBOL_BLOCK_VALUE (f
));
713 cache_pc_function_name
= SYMBOL_NAME (f
);
714 goto return_cached_value
;
719 /* Now that static symbols go in the minimal symbol table, perhaps
720 we could just ignore the partial symbols. But at least for now
721 we use the partial or minimal symbol, whichever is larger. */
722 psb
= find_pc_psymbol (pst
, pc
);
725 && (msymbol
== NULL
||
726 (SYMBOL_VALUE_ADDRESS (psb
)
727 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
729 /* This case isn't being cached currently. */
731 *address
= SYMBOL_VALUE_ADDRESS (psb
);
733 *name
= SYMBOL_NAME (psb
);
734 /* endaddr non-NULL can't happen here. */
740 /* Not in the normal symbol tables, see if the pc is in a known section.
741 If it's not, then give up. This ensures that anything beyond the end
742 of the text seg doesn't appear to be part of the last function in the
745 sec
= find_pc_section (pc
);
750 /* Must be in the minimal symbol table. */
753 /* No available symbol. */
763 /* See if we're in a transfer table for Sun shared libs. */
765 if (msymbol
-> type
== mst_text
|| msymbol
-> type
== mst_file_text
)
766 cache_pc_function_low
= SYMBOL_VALUE_ADDRESS (msymbol
);
768 /* It is a transfer table for Sun shared libraries. */
769 cache_pc_function_low
= pc
- FUNCTION_START_OFFSET
;
771 cache_pc_function_name
= SYMBOL_NAME (msymbol
);
773 /* Use the lesser of the next minimal symbol, or the end of the section, as
774 the end of the function. */
776 if (SYMBOL_NAME (msymbol
+ 1) != NULL
777 && SYMBOL_VALUE_ADDRESS (msymbol
+ 1) < sec
->endaddr
)
778 cache_pc_function_high
= SYMBOL_VALUE_ADDRESS (msymbol
+ 1);
780 /* We got the start address from the last msymbol in the objfile.
781 So the end address is the end of the section. */
782 cache_pc_function_high
= sec
->endaddr
;
786 *address
= cache_pc_function_low
;
788 *name
= cache_pc_function_name
;
790 *endaddr
= cache_pc_function_high
;
794 /* Return the innermost stack frame executing inside of BLOCK,
795 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
798 block_innermost_frame (block
)
801 struct frame_info
*fi
;
802 register FRAME frame
;
803 register CORE_ADDR start
;
804 register CORE_ADDR end
;
809 start
= BLOCK_START (block
);
810 end
= BLOCK_END (block
);
815 frame
= get_prev_frame (frame
);
818 fi
= get_frame_info (frame
);
819 if (fi
->pc
>= start
&& fi
->pc
< end
)
824 /* Return the full FRAME which corresponds to the given FRAME_ADDR
825 or NULL if no FRAME on the chain corresponds to FRAME_ADDR. */
828 find_frame_addr_in_frame_chain (frame_addr
)
829 FRAME_ADDR frame_addr
;
833 if (frame_addr
== (CORE_ADDR
)0)
838 frame
= get_prev_frame (frame
);
842 if (FRAME_FP (frame
) == frame_addr
)
847 #ifdef SIGCONTEXT_PC_OFFSET
848 /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp. */
851 sigtramp_saved_pc (frame
)
854 CORE_ADDR sigcontext_addr
;
855 char buf
[TARGET_PTR_BIT
/ TARGET_CHAR_BIT
];
856 int ptrbytes
= TARGET_PTR_BIT
/ TARGET_CHAR_BIT
;
857 int sigcontext_offs
= (2 * TARGET_INT_BIT
) / TARGET_CHAR_BIT
;
859 /* Get sigcontext address, it is the third parameter on the stack. */
861 sigcontext_addr
= read_memory_integer (FRAME_ARGS_ADDRESS (frame
->next
)
862 + FRAME_ARGS_SKIP
+ sigcontext_offs
,
865 sigcontext_addr
= read_memory_integer (read_register (SP_REGNUM
)
869 /* Don't cause a memory_error when accessing sigcontext in case the stack
870 layout has changed or the stack is corrupt. */
871 target_read_memory (sigcontext_addr
+ SIGCONTEXT_PC_OFFSET
, buf
, ptrbytes
);
872 return extract_unsigned_integer (buf
, ptrbytes
);
874 #endif /* SIGCONTEXT_PC_OFFSET */
877 _initialize_blockframe ()
879 obstack_init (&frame_cache_obstack
);