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 */
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
38 A PC of zero is always considered to be the bottom of the stack. */
41 inside_entry_file (addr
)
46 if (symfile_objfile
== 0)
48 return (addr
>= symfile_objfile
-> ei
.entry_file_lowpc
&&
49 addr
< symfile_objfile
-> ei
.entry_file_highpc
);
52 /* Test a specified PC value to see if it is in the range of addresses
53 that correspond to the main() function. See comments above for why
54 we might want to do this.
56 Typically called from FRAME_CHAIN_VALID.
58 A PC of zero is always considered to be the bottom of the stack. */
66 if (symfile_objfile
== 0)
68 return (symfile_objfile
-> ei
.main_func_lowpc
<= pc
&&
69 symfile_objfile
-> ei
.main_func_highpc
> pc
);
72 /* Test a specified PC value to see if it is in the range of addresses
73 that correspond to the process entry point function. See comments
74 in objfiles.h for why we might want to do this.
76 Typically called from FRAME_CHAIN_VALID.
78 A PC of zero is always considered to be the bottom of the stack. */
81 inside_entry_func (pc
)
86 if (symfile_objfile
== 0)
88 return (symfile_objfile
-> ei
.entry_func_lowpc
<= pc
&&
89 symfile_objfile
-> ei
.entry_func_highpc
> pc
);
92 /* Address of innermost stack frame (contents of FP register) */
94 static FRAME current_frame
;
97 * Cache for frame addresses already read by gdb. Valid only while
98 * inferior is stopped. Control variables for the frame cache should
99 * be local to this module.
101 struct obstack frame_cache_obstack
;
103 /* Return the innermost (currently executing) stack frame. */
108 /* We assume its address is kept in a general register;
109 param.h says which register. */
111 return current_frame
;
115 set_current_frame (frame
)
118 current_frame
= frame
;
122 create_new_frame (addr
, pc
)
126 struct frame_info
*fci
; /* Same type as FRAME */
129 fci
= (struct frame_info
*)
130 obstack_alloc (&frame_cache_obstack
,
131 sizeof (struct frame_info
));
133 /* Arbitrary frame */
134 fci
->next
= (struct frame_info
*) 0;
135 fci
->prev
= (struct frame_info
*) 0;
138 find_pc_partial_function (pc
, &name
, (CORE_ADDR
*)NULL
,(CORE_ADDR
*)NULL
);
139 fci
->signal_handler_caller
= IN_SIGTRAMP (fci
->pc
, name
);
141 #ifdef INIT_EXTRA_FRAME_INFO
142 INIT_EXTRA_FRAME_INFO (0, fci
);
148 /* Return the frame that called FRAME.
149 If FRAME is the original frame (it has no caller), return 0. */
152 get_prev_frame (frame
)
155 /* We're allowed to know that FRAME and "struct frame_info *" are
157 return get_prev_frame_info (frame
);
160 /* Return the frame that FRAME calls (0 if FRAME is the innermost
164 get_next_frame (frame
)
167 /* We're allowed to know that FRAME and "struct frame_info *" are
173 * Flush the entire frame cache.
176 flush_cached_frames ()
178 /* Since we can't really be sure what the first object allocated was */
179 obstack_free (&frame_cache_obstack
, 0);
180 obstack_init (&frame_cache_obstack
);
182 current_frame
= (struct frame_info
*) 0; /* Invalidate cache */
185 /* Flush the frame cache, and start a new one if necessary. */
187 reinit_frame_cache ()
189 flush_cached_frames ();
190 if (target_has_stack
)
192 set_current_frame (create_new_frame (read_fp (), read_pc ()));
193 select_frame (get_current_frame (), 0);
197 set_current_frame (0);
198 select_frame ((FRAME
) 0, -1);
202 /* Return a structure containing various interesting information
203 about a specified stack frame. */
204 /* How do I justify including this function? Well, the FRAME
205 identifier format has gone through several changes recently, and
206 it's not completely inconceivable that it could happen again. If
207 it does, have this routine around will help */
210 get_frame_info (frame
)
216 /* If a machine allows frameless functions, it should define a macro
217 FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct
218 frame_info for the frame, and FRAMELESS should be set to nonzero
219 if it represents a frameless function invocation. */
221 /* Return nonzero if the function for this frame lacks a prologue. Many
222 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
226 frameless_look_for_prologue (frame
)
229 CORE_ADDR func_start
, after_prologue
;
230 func_start
= (get_pc_function_start (frame
->pc
) +
231 FUNCTION_START_OFFSET
);
234 after_prologue
= func_start
;
235 #ifdef SKIP_PROLOGUE_FRAMELESS_P
236 /* This is faster, since only care whether there *is* a prologue,
237 not how long it is. */
238 SKIP_PROLOGUE_FRAMELESS_P (after_prologue
);
240 SKIP_PROLOGUE (after_prologue
);
242 return after_prologue
== func_start
;
245 /* If we can't find the start of the function, we don't really
246 know whether the function is frameless, but we should be able
247 to get a reasonable (i.e. best we can do under the
248 circumstances) backtrace by saying that it isn't. */
252 /* Default a few macros that people seldom redefine. */
254 #if !defined (INIT_FRAME_PC)
255 #define INIT_FRAME_PC(fromleaf, prev) \
256 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
257 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
260 #ifndef FRAME_CHAIN_COMBINE
261 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
264 /* Return a structure containing various interesting information
265 about the frame that called NEXT_FRAME. Returns NULL
266 if there is no such frame. */
269 get_prev_frame_info (next_frame
)
272 FRAME_ADDR address
= 0;
273 struct frame_info
*prev
;
277 /* If the requested entry is in the cache, return it.
278 Otherwise, figure out what the address should be for the entry
279 we're about to add to the cache. */
285 error ("You haven't set up a process's stack to examine.");
288 return current_frame
;
291 /* If we have the prev one, return it */
292 if (next_frame
->prev
)
293 return next_frame
->prev
;
295 /* On some machines it is possible to call a function without
296 setting up a stack frame for it. On these machines, we
297 define this macro to take two args; a frameinfo pointer
298 identifying a frame and a variable to set or clear if it is
299 or isn't leafless. */
300 #ifdef FRAMELESS_FUNCTION_INVOCATION
301 /* Still don't want to worry about this except on the innermost
302 frame. This macro will set FROMLEAF if NEXT_FRAME is a
303 frameless function invocation. */
304 if (!(next_frame
->next
))
306 FRAMELESS_FUNCTION_INVOCATION (next_frame
, fromleaf
);
308 address
= next_frame
->frame
;
314 /* Two macros defined in tm.h specify the machine-dependent
315 actions to be performed here.
316 First, get the frame's chain-pointer.
317 If that is zero, the frame is the outermost frame or a leaf
318 called by the outermost frame. This means that if start
319 calls main without a frame, we'll return 0 (which is fine
322 Nope; there's a problem. This also returns when the current
323 routine is a leaf of main. This is unacceptable. We move
324 this to after the ffi test; I'd rather have backtraces from
325 start go curfluy than have an abort called from main not show
327 address
= FRAME_CHAIN (next_frame
);
328 if (!FRAME_CHAIN_VALID (address
, next_frame
))
330 address
= FRAME_CHAIN_COMBINE (address
, next_frame
);
335 prev
= (struct frame_info
*)
336 obstack_alloc (&frame_cache_obstack
,
337 sizeof (struct frame_info
));
340 next_frame
->prev
= prev
;
341 prev
->next
= next_frame
;
342 prev
->prev
= (struct frame_info
*) 0;
343 prev
->frame
= address
;
344 prev
->signal_handler_caller
= 0;
346 /* This change should not be needed, FIXME! We should
347 determine whether any targets *need* INIT_FRAME_PC to happen
348 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
349 express what goes on here.
351 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
352 (where the PC is already set up) and here (where it isn't).
353 INIT_FRAME_PC is only called from here, always after
354 INIT_EXTRA_FRAME_INFO.
356 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
357 value (which hasn't been set yet). Some other machines appear to
358 require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo.
360 We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
361 an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92.
363 To answer the question, yes the sparc needs INIT_FRAME_PC after
364 INIT_EXTRA_FRAME_INFO. Suggested scheme:
366 SETUP_INNERMOST_FRAME()
367 Default version is just create_new_frame (read_fp ()),
368 read_pc ()). Machines with extra frame info would do that (or the
369 local equivalent) and then set the extra fields.
370 SETUP_ARBITRARY_FRAME(argc, argv)
371 Only change here is that create_new_frame would no longer init extra
372 frame info; SETUP_ARBITRARY_FRAME would have to do that.
373 INIT_PREV_FRAME(fromleaf, prev)
374 Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC.
375 std_frame_pc(fromleaf, prev)
376 This is the default setting for INIT_PREV_FRAME. It just does what
377 the default INIT_FRAME_PC does. Some machines will call it from
378 INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
379 Some machines won't use it.
380 kingdon@cygnus.com, 13Apr93. */
382 #ifdef INIT_FRAME_PC_FIRST
383 INIT_FRAME_PC_FIRST (fromleaf
, prev
);
386 #ifdef INIT_EXTRA_FRAME_INFO
387 INIT_EXTRA_FRAME_INFO(fromleaf
, prev
);
390 /* This entry is in the frame queue now, which is good since
391 FRAME_SAVED_PC may use that queue to figure out it's value
392 (see tm-sparc.h). We want the pc saved in the inferior frame. */
393 INIT_FRAME_PC(fromleaf
, prev
);
395 find_pc_partial_function (prev
->pc
, &name
,
396 (CORE_ADDR
*)NULL
,(CORE_ADDR
*)NULL
);
397 if (IN_SIGTRAMP (prev
->pc
, name
))
398 prev
->signal_handler_caller
= 1;
407 struct frame_info
*fi
;
408 fi
= get_frame_info (frame
);
412 #if defined (FRAME_FIND_SAVED_REGS)
413 /* Find the addresses in which registers are saved in FRAME. */
416 get_frame_saved_regs (frame_info_addr
, saved_regs_addr
)
417 struct frame_info
*frame_info_addr
;
418 struct frame_saved_regs
*saved_regs_addr
;
420 FRAME_FIND_SAVED_REGS (frame_info_addr
, *saved_regs_addr
);
424 /* Return the innermost lexical block in execution
425 in a specified stack frame. The frame address is assumed valid. */
428 get_frame_block (frame
)
431 struct frame_info
*fi
;
434 fi
= get_frame_info (frame
);
438 /* We are not in the innermost frame. We need to subtract one to
439 get the correct block, in case the call instruction was the
440 last instruction of the block. If there are any machines on
441 which the saved pc does not point to after the call insn, we
442 probably want to make fi->pc point after the call insn anyway. */
444 return block_for_pc (pc
);
450 return block_for_pc (read_pc ());
454 get_pc_function_start (pc
)
457 register struct block
*bl
;
458 register struct symbol
*symbol
;
459 register struct minimal_symbol
*msymbol
;
462 if ((bl
= block_for_pc (pc
)) != NULL
&&
463 (symbol
= block_function (bl
)) != NULL
)
465 bl
= SYMBOL_BLOCK_VALUE (symbol
);
466 fstart
= BLOCK_START (bl
);
468 else if ((msymbol
= lookup_minimal_symbol_by_pc (pc
)) != NULL
)
470 fstart
= SYMBOL_VALUE_ADDRESS (msymbol
);
479 /* Return the symbol for the function executing in frame FRAME. */
482 get_frame_function (frame
)
485 register struct block
*bl
= get_frame_block (frame
);
488 return block_function (bl
);
491 /* Return the blockvector immediately containing the innermost lexical block
492 containing the specified pc value, or 0 if there is none.
493 PINDEX is a pointer to the index value of the block. If PINDEX
494 is NULL, we don't pass this information back to the caller. */
497 blockvector_for_pc (pc
, pindex
)
498 register CORE_ADDR pc
;
501 register struct block
*b
;
502 register int bot
, top
, half
;
503 register struct symtab
*s
;
504 struct blockvector
*bl
;
506 /* First search all symtabs for one whose file contains our pc */
507 s
= find_pc_symtab (pc
);
511 bl
= BLOCKVECTOR (s
);
512 b
= BLOCKVECTOR_BLOCK (bl
, 0);
514 /* Then search that symtab for the smallest block that wins. */
515 /* Use binary search to find the last block that starts before PC. */
518 top
= BLOCKVECTOR_NBLOCKS (bl
);
520 while (top
- bot
> 1)
522 half
= (top
- bot
+ 1) >> 1;
523 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
524 if (BLOCK_START (b
) <= pc
)
530 /* Now search backward for a block that ends after PC. */
534 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
535 if (BLOCK_END (b
) > pc
)
547 /* Return the innermost lexical block containing the specified pc value,
548 or 0 if there is none. */
552 register CORE_ADDR pc
;
554 register struct blockvector
*bl
;
557 bl
= blockvector_for_pc (pc
, &index
);
559 return BLOCKVECTOR_BLOCK (bl
, index
);
563 /* Return the function containing pc value PC.
564 Returns 0 if function is not known. */
567 find_pc_function (pc
)
570 register struct block
*b
= block_for_pc (pc
);
573 return block_function (b
);
576 /* These variables are used to cache the most recent result
577 * of find_pc_partial_function. */
579 static CORE_ADDR cache_pc_function_low
= 0;
580 static CORE_ADDR cache_pc_function_high
= 0;
581 static char *cache_pc_function_name
= 0;
583 /* Clear cache, e.g. when symbol table is discarded. */
586 clear_pc_function_cache()
588 cache_pc_function_low
= 0;
589 cache_pc_function_high
= 0;
590 cache_pc_function_name
= (char *)0;
593 /* Finds the "function" (text symbol) that is smaller than PC but
594 greatest of all of the potential text symbols. Sets *NAME and/or
595 *ADDRESS conditionally if that pointer is non-null. If ENDADDR is
596 non-null, then set *ENDADDR to be the end of the function
597 (exclusive), but passing ENDADDR as non-null means that the
598 function might cause symbols to be read. This function either
599 succeeds or fails (not halfway succeeds). If it succeeds, it sets
600 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
601 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero
605 find_pc_partial_function (pc
, name
, address
, endaddr
)
611 struct partial_symtab
*pst
;
613 struct minimal_symbol
*msymbol
;
614 struct partial_symbol
*psb
;
616 if (pc
>= cache_pc_function_low
&& pc
< cache_pc_function_high
)
617 goto return_cached_value
;
619 /* If sigtramp is in the u area, it counts as a function (especially
620 important for step_1). */
621 #if defined SIGTRAMP_START
622 if (IN_SIGTRAMP (pc
, (char *)NULL
))
624 cache_pc_function_low
= SIGTRAMP_START
;
625 cache_pc_function_high
= SIGTRAMP_END
;
626 cache_pc_function_name
= "<sigtramp>";
628 goto return_cached_value
;
632 msymbol
= lookup_minimal_symbol_by_pc (pc
);
633 pst
= find_pc_psymtab (pc
);
636 /* Need to read the symbols to get a good value for the end address. */
637 if (endaddr
!= NULL
&& !pst
->readin
)
638 PSYMTAB_TO_SYMTAB (pst
);
642 /* Checking whether the msymbol has a larger value is for the
643 "pathological" case mentioned in print_frame_info. */
644 f
= find_pc_function (pc
);
647 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f
))
648 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
650 cache_pc_function_low
= BLOCK_START (SYMBOL_BLOCK_VALUE (f
));
651 cache_pc_function_high
= BLOCK_END (SYMBOL_BLOCK_VALUE (f
));
652 cache_pc_function_name
= SYMBOL_NAME (f
);
653 goto return_cached_value
;
657 /* Now that static symbols go in the minimal symbol table, perhaps
658 we could just ignore the partial symbols. But at least for now
659 we use the partial or minimal symbol, whichever is larger. */
660 psb
= find_pc_psymbol (pst
, pc
);
663 && (msymbol
== NULL
||
664 (SYMBOL_VALUE_ADDRESS (psb
) >= SYMBOL_VALUE_ADDRESS (msymbol
))))
666 /* This case isn't being cached currently. */
668 *address
= SYMBOL_VALUE_ADDRESS (psb
);
670 *name
= SYMBOL_NAME (psb
);
671 /* endaddr non-NULL can't happen here. */
676 /* Must be in the minimal symbol table. */
679 /* No available symbol. */
689 /* I believe the purpose of this check is to make sure that anything
690 beyond the end of the text segment does not appear as part of the
691 last function of the text segment. It assumes that there is something
692 other than a mst_text symbol after the text segment. It is broken in
693 various cases, so anything relying on this behavior (there might be
694 some places) should be using find_pc_section or some such instead. */
695 if (msymbol
-> type
== mst_text
)
696 cache_pc_function_low
= SYMBOL_VALUE_ADDRESS (msymbol
);
698 /* It is a transfer table for Sun shared libraries. */
699 cache_pc_function_low
= pc
- FUNCTION_START_OFFSET
;
700 cache_pc_function_name
= SYMBOL_NAME (msymbol
);
702 if (SYMBOL_NAME (msymbol
+ 1) != NULL
)
703 /* This might be part of a different segment, which might be a bad
704 idea. Perhaps we should be using the smaller of this address or the
705 endaddr from find_pc_section. */
706 cache_pc_function_high
= SYMBOL_VALUE_ADDRESS (msymbol
+ 1);
709 /* We got the start address from the last msymbol in the objfile.
710 So the end address is the end of the section. */
711 struct obj_section
*sec
;
713 sec
= find_pc_section (pc
);
716 /* Don't know if this can happen but if it does, then just say
717 that the function is 1 byte long. */
718 cache_pc_function_high
= cache_pc_function_low
+ 1;
721 cache_pc_function_high
= sec
->endaddr
;
726 *address
= cache_pc_function_low
;
728 *name
= cache_pc_function_name
;
730 *endaddr
= cache_pc_function_high
;
734 /* Return the innermost stack frame executing inside of BLOCK,
735 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
738 block_innermost_frame (block
)
741 struct frame_info
*fi
;
742 register FRAME frame
;
743 register CORE_ADDR start
;
744 register CORE_ADDR end
;
749 start
= BLOCK_START (block
);
750 end
= BLOCK_END (block
);
755 frame
= get_prev_frame (frame
);
758 fi
= get_frame_info (frame
);
759 if (fi
->pc
>= start
&& fi
->pc
< end
)
764 #ifdef SIGCONTEXT_PC_OFFSET
765 /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp. */
768 sigtramp_saved_pc (frame
)
771 CORE_ADDR sigcontext_addr
;
772 char buf
[TARGET_PTR_BIT
/ TARGET_CHAR_BIT
];
773 int ptrbytes
= TARGET_PTR_BIT
/ TARGET_CHAR_BIT
;
774 int sigcontext_offs
= (2 * TARGET_INT_BIT
) / TARGET_CHAR_BIT
;
776 /* Get sigcontext address, it is the third parameter on the stack. */
778 sigcontext_addr
= read_memory_integer (FRAME_ARGS_ADDRESS (frame
->next
)
779 + FRAME_ARGS_SKIP
+ sigcontext_offs
,
782 sigcontext_addr
= read_memory_integer (read_register (SP_REGNUM
)
786 /* Don't cause a memory_error when accessing sigcontext in case the stack
787 layout has changed or the stack is corrupt. */
788 target_read_memory (sigcontext_addr
+ SIGCONTEXT_PC_OFFSET
, buf
, ptrbytes
);
789 return extract_unsigned_integer (buf
, ptrbytes
);
791 #endif /* SIGCONTEXT_PC_OFFSET */
794 _initialize_blockframe ()
796 obstack_init (&frame_cache_obstack
);