1 /* Get info from stack frames; convert between frames, blocks,
2 functions and pc values.
4 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
5 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
32 #include "value.h" /* for read_register */
33 #include "target.h" /* for target_has_stack */
34 #include "inferior.h" /* for read_pc */
37 #include "gdb_assert.h"
38 #include "dummy-frame.h"
42 /* Flag to indicate whether backtraces should stop at main. */
44 static int backtrace_below_main
;
46 /* Prototypes for exported functions. */
48 void _initialize_blockframe (void);
50 /* Is ADDR inside the startup file? Note that if your machine
51 has a way to detect the bottom of the stack, there is no need
52 to call this function from FRAME_CHAIN_VALID; the reason for
53 doing so is that some machines have no way of detecting bottom
56 A PC of zero is always considered to be the bottom of the stack. */
59 inside_entry_file (CORE_ADDR addr
)
63 if (symfile_objfile
== 0)
65 if (CALL_DUMMY_LOCATION
== AT_ENTRY_POINT
)
67 /* Do not stop backtracing if the pc is in the call dummy
68 at the entry point. */
69 /* FIXME: Won't always work with zeros for the last two arguments */
70 if (DEPRECATED_PC_IN_CALL_DUMMY (addr
, 0, 0))
73 return (addr
>= symfile_objfile
->ei
.entry_file_lowpc
&&
74 addr
< symfile_objfile
->ei
.entry_file_highpc
);
77 /* Test a specified PC value to see if it is in the range of addresses
78 that correspond to the main() function. See comments above for why
79 we might want to do this.
81 Typically called from FRAME_CHAIN_VALID.
83 A PC of zero is always considered to be the bottom of the stack. */
86 inside_main_func (CORE_ADDR pc
)
90 if (symfile_objfile
== 0)
93 /* If the addr range is not set up at symbol reading time, set it up now.
94 This is for FRAME_CHAIN_VALID_ALTERNATE. I do this for coff, because
95 it is unable to set it up and symbol reading time. */
97 if (symfile_objfile
->ei
.main_func_lowpc
== INVALID_ENTRY_LOWPC
&&
98 symfile_objfile
->ei
.main_func_highpc
== INVALID_ENTRY_HIGHPC
)
100 struct symbol
*mainsym
;
102 mainsym
= lookup_symbol (main_name (), NULL
, VAR_NAMESPACE
, NULL
, NULL
);
103 if (mainsym
&& SYMBOL_CLASS (mainsym
) == LOC_BLOCK
)
105 symfile_objfile
->ei
.main_func_lowpc
=
106 BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym
));
107 symfile_objfile
->ei
.main_func_highpc
=
108 BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym
));
111 return (symfile_objfile
->ei
.main_func_lowpc
<= pc
&&
112 symfile_objfile
->ei
.main_func_highpc
> pc
);
115 /* Test a specified PC value to see if it is in the range of addresses
116 that correspond to the process entry point function. See comments
117 in objfiles.h for why we might want to do this.
119 Typically called from FRAME_CHAIN_VALID.
121 A PC of zero is always considered to be the bottom of the stack. */
124 inside_entry_func (CORE_ADDR pc
)
128 if (symfile_objfile
== 0)
130 if (CALL_DUMMY_LOCATION
== AT_ENTRY_POINT
)
132 /* Do not stop backtracing if the pc is in the call dummy
133 at the entry point. */
134 /* FIXME: Won't always work with zeros for the last two arguments */
135 if (DEPRECATED_PC_IN_CALL_DUMMY (pc
, 0, 0))
138 return (symfile_objfile
->ei
.entry_func_lowpc
<= pc
&&
139 symfile_objfile
->ei
.entry_func_highpc
> pc
);
142 /* Return nonzero if the function for this frame lacks a prologue. Many
143 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
147 frameless_look_for_prologue (struct frame_info
*frame
)
149 CORE_ADDR func_start
, after_prologue
;
151 func_start
= get_pc_function_start (get_frame_pc (frame
));
154 func_start
+= FUNCTION_START_OFFSET
;
155 /* This is faster, since only care whether there *is* a
156 prologue, not how long it is. */
157 return PROLOGUE_FRAMELESS_P (func_start
);
159 else if (get_frame_pc (frame
) == 0)
160 /* A frame with a zero PC is usually created by dereferencing a
161 NULL function pointer, normally causing an immediate core dump
162 of the inferior. Mark function as frameless, as the inferior
163 has no chance of setting up a stack frame. */
166 /* If we can't find the start of the function, we don't really
167 know whether the function is frameless, but we should be able
168 to get a reasonable (i.e. best we can do under the
169 circumstances) backtrace by saying that it isn't. */
173 /* return the address of the PC for the given FRAME, ie the current PC value
174 if FRAME is the innermost frame, or the address adjusted to point to the
175 call instruction if not. */
178 frame_address_in_block (struct frame_info
*frame
)
180 CORE_ADDR pc
= get_frame_pc (frame
);
182 /* If we are not in the innermost frame, and we are not interrupted
183 by a signal, frame->pc points to the instruction following the
184 call. As a consequence, we need to get the address of the previous
185 instruction. Unfortunately, this is not straightforward to do, so
186 we just use the address minus one, which is a good enough
188 /* FIXME: cagney/2002-11-10: Should this instead test for
189 NORMAL_FRAME? A dummy frame (in fact all the abnormal frames)
190 save the PC value in the block. */
191 if (get_next_frame (frame
) != 0
192 && get_frame_type (get_next_frame (frame
)) != SIGTRAMP_FRAME
)
198 /* Return the innermost lexical block in execution
199 in a specified stack frame. The frame address is assumed valid.
201 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
202 address we used to choose the block. We use this to find a source
203 line, to decide which macro definitions are in scope.
205 The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
206 PC, and may not really be a valid PC at all. For example, in the
207 caller of a function declared to never return, the code at the
208 return address will never be reached, so the call instruction may
209 be the very last instruction in the block. So the address we use
210 to choose the block is actually one byte before the return address
211 --- hopefully pointing us at the call instruction, or its delay
215 get_frame_block (struct frame_info
*frame
, CORE_ADDR
*addr_in_block
)
217 const CORE_ADDR pc
= frame_address_in_block (frame
);
222 return block_for_pc (pc
);
226 get_pc_function_start (CORE_ADDR pc
)
228 register struct block
*bl
;
229 register struct symbol
*symbol
;
230 register struct minimal_symbol
*msymbol
;
233 if ((bl
= block_for_pc (pc
)) != NULL
&&
234 (symbol
= block_function (bl
)) != NULL
)
236 bl
= SYMBOL_BLOCK_VALUE (symbol
);
237 fstart
= BLOCK_START (bl
);
239 else if ((msymbol
= lookup_minimal_symbol_by_pc (pc
)) != NULL
)
241 fstart
= SYMBOL_VALUE_ADDRESS (msymbol
);
242 if (!find_pc_section (fstart
))
252 /* Return the symbol for the function executing in frame FRAME. */
255 get_frame_function (struct frame_info
*frame
)
257 register struct block
*bl
= get_frame_block (frame
, 0);
260 return block_function (bl
);
264 /* Return the blockvector immediately containing the innermost lexical block
265 containing the specified pc value and section, or 0 if there is none.
266 PINDEX is a pointer to the index value of the block. If PINDEX
267 is NULL, we don't pass this information back to the caller. */
270 blockvector_for_pc_sect (register CORE_ADDR pc
, struct sec
*section
,
271 int *pindex
, struct symtab
*symtab
)
273 register struct block
*b
;
274 register int bot
, top
, half
;
275 struct blockvector
*bl
;
277 if (symtab
== 0) /* if no symtab specified by caller */
279 /* First search all symtabs for one whose file contains our pc */
280 if ((symtab
= find_pc_sect_symtab (pc
, section
)) == 0)
284 bl
= BLOCKVECTOR (symtab
);
285 b
= BLOCKVECTOR_BLOCK (bl
, 0);
287 /* Then search that symtab for the smallest block that wins. */
288 /* Use binary search to find the last block that starts before PC. */
291 top
= BLOCKVECTOR_NBLOCKS (bl
);
293 while (top
- bot
> 1)
295 half
= (top
- bot
+ 1) >> 1;
296 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
297 if (BLOCK_START (b
) <= pc
)
303 /* Now search backward for a block that ends after PC. */
307 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
308 if (BLOCK_END (b
) > pc
)
319 /* Return the blockvector immediately containing the innermost lexical block
320 containing the specified pc value, or 0 if there is none.
321 Backward compatibility, no section. */
324 blockvector_for_pc (register CORE_ADDR pc
, int *pindex
)
326 return blockvector_for_pc_sect (pc
, find_pc_mapped_section (pc
),
330 /* Return the innermost lexical block containing the specified pc value
331 in the specified section, or 0 if there is none. */
334 block_for_pc_sect (register CORE_ADDR pc
, struct sec
*section
)
336 register struct blockvector
*bl
;
339 bl
= blockvector_for_pc_sect (pc
, section
, &index
, NULL
);
341 return BLOCKVECTOR_BLOCK (bl
, index
);
345 /* Return the innermost lexical block containing the specified pc value,
346 or 0 if there is none. Backward compatibility, no section. */
349 block_for_pc (register CORE_ADDR pc
)
351 return block_for_pc_sect (pc
, find_pc_mapped_section (pc
));
354 /* Return the function containing pc value PC in section SECTION.
355 Returns 0 if function is not known. */
358 find_pc_sect_function (CORE_ADDR pc
, struct sec
*section
)
360 register struct block
*b
= block_for_pc_sect (pc
, section
);
363 return block_function (b
);
366 /* Return the function containing pc value PC.
367 Returns 0 if function is not known. Backward compatibility, no section */
370 find_pc_function (CORE_ADDR pc
)
372 return find_pc_sect_function (pc
, find_pc_mapped_section (pc
));
375 /* These variables are used to cache the most recent result
376 * of find_pc_partial_function. */
378 static CORE_ADDR cache_pc_function_low
= 0;
379 static CORE_ADDR cache_pc_function_high
= 0;
380 static char *cache_pc_function_name
= 0;
381 static struct sec
*cache_pc_function_section
= NULL
;
383 /* Clear cache, e.g. when symbol table is discarded. */
386 clear_pc_function_cache (void)
388 cache_pc_function_low
= 0;
389 cache_pc_function_high
= 0;
390 cache_pc_function_name
= (char *) 0;
391 cache_pc_function_section
= NULL
;
394 /* Finds the "function" (text symbol) that is smaller than PC but
395 greatest of all of the potential text symbols in SECTION. Sets
396 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
397 If ENDADDR is non-null, then set *ENDADDR to be the end of the
398 function (exclusive), but passing ENDADDR as non-null means that
399 the function might cause symbols to be read. This function either
400 succeeds or fails (not halfway succeeds). If it succeeds, it sets
401 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
402 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
406 find_pc_sect_partial_function (CORE_ADDR pc
, asection
*section
, char **name
,
407 CORE_ADDR
*address
, CORE_ADDR
*endaddr
)
409 struct partial_symtab
*pst
;
411 struct minimal_symbol
*msymbol
;
412 struct partial_symbol
*psb
;
413 struct obj_section
*osect
;
417 mapped_pc
= overlay_mapped_address (pc
, section
);
419 if (mapped_pc
>= cache_pc_function_low
420 && mapped_pc
< cache_pc_function_high
421 && section
== cache_pc_function_section
)
422 goto return_cached_value
;
424 /* If sigtramp is in the u area, it counts as a function (especially
425 important for step_1). */
426 if (SIGTRAMP_START_P () && PC_IN_SIGTRAMP (mapped_pc
, (char *) NULL
))
428 cache_pc_function_low
= SIGTRAMP_START (mapped_pc
);
429 cache_pc_function_high
= SIGTRAMP_END (mapped_pc
);
430 cache_pc_function_name
= "<sigtramp>";
431 cache_pc_function_section
= section
;
432 goto return_cached_value
;
435 msymbol
= lookup_minimal_symbol_by_pc_section (mapped_pc
, section
);
436 pst
= find_pc_sect_psymtab (mapped_pc
, section
);
439 /* Need to read the symbols to get a good value for the end address. */
440 if (endaddr
!= NULL
&& !pst
->readin
)
442 /* Need to get the terminal in case symbol-reading produces
444 target_terminal_ours_for_output ();
445 PSYMTAB_TO_SYMTAB (pst
);
450 /* Checking whether the msymbol has a larger value is for the
451 "pathological" case mentioned in print_frame_info. */
452 f
= find_pc_sect_function (mapped_pc
, section
);
455 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f
))
456 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
458 cache_pc_function_low
= BLOCK_START (SYMBOL_BLOCK_VALUE (f
));
459 cache_pc_function_high
= BLOCK_END (SYMBOL_BLOCK_VALUE (f
));
460 cache_pc_function_name
= SYMBOL_NAME (f
);
461 cache_pc_function_section
= section
;
462 goto return_cached_value
;
467 /* Now that static symbols go in the minimal symbol table, perhaps
468 we could just ignore the partial symbols. But at least for now
469 we use the partial or minimal symbol, whichever is larger. */
470 psb
= find_pc_sect_psymbol (pst
, mapped_pc
, section
);
473 && (msymbol
== NULL
||
474 (SYMBOL_VALUE_ADDRESS (psb
)
475 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
477 /* This case isn't being cached currently. */
479 *address
= SYMBOL_VALUE_ADDRESS (psb
);
481 *name
= SYMBOL_NAME (psb
);
482 /* endaddr non-NULL can't happen here. */
488 /* Not in the normal symbol tables, see if the pc is in a known section.
489 If it's not, then give up. This ensures that anything beyond the end
490 of the text seg doesn't appear to be part of the last function in the
493 osect
= find_pc_sect_section (mapped_pc
, section
);
498 /* Must be in the minimal symbol table. */
501 /* No available symbol. */
511 cache_pc_function_low
= SYMBOL_VALUE_ADDRESS (msymbol
);
512 cache_pc_function_name
= SYMBOL_NAME (msymbol
);
513 cache_pc_function_section
= section
;
515 /* Use the lesser of the next minimal symbol in the same section, or
516 the end of the section, as the end of the function. */
518 /* Step over other symbols at this same address, and symbols in
519 other sections, to find the next symbol in this section with
520 a different address. */
522 for (i
= 1; SYMBOL_NAME (msymbol
+ i
) != NULL
; i
++)
524 if (SYMBOL_VALUE_ADDRESS (msymbol
+ i
) != SYMBOL_VALUE_ADDRESS (msymbol
)
525 && SYMBOL_BFD_SECTION (msymbol
+ i
) == SYMBOL_BFD_SECTION (msymbol
))
529 if (SYMBOL_NAME (msymbol
+ i
) != NULL
530 && SYMBOL_VALUE_ADDRESS (msymbol
+ i
) < osect
->endaddr
)
531 cache_pc_function_high
= SYMBOL_VALUE_ADDRESS (msymbol
+ i
);
533 /* We got the start address from the last msymbol in the objfile.
534 So the end address is the end of the section. */
535 cache_pc_function_high
= osect
->endaddr
;
541 if (pc_in_unmapped_range (pc
, section
))
542 *address
= overlay_unmapped_address (cache_pc_function_low
, section
);
544 *address
= cache_pc_function_low
;
548 *name
= cache_pc_function_name
;
552 if (pc_in_unmapped_range (pc
, section
))
554 /* Because the high address is actually beyond the end of
555 the function (and therefore possibly beyond the end of
556 the overlay), we must actually convert (high - 1) and
557 then add one to that. */
559 *endaddr
= 1 + overlay_unmapped_address (cache_pc_function_high
- 1,
563 *endaddr
= cache_pc_function_high
;
569 /* Backward compatibility, no section argument. */
572 find_pc_partial_function (CORE_ADDR pc
, char **name
, CORE_ADDR
*address
,
577 section
= find_pc_overlay (pc
);
578 return find_pc_sect_partial_function (pc
, section
, name
, address
, endaddr
);
581 /* Return the innermost stack frame executing inside of BLOCK,
582 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
585 block_innermost_frame (struct block
*block
)
587 struct frame_info
*frame
;
588 register CORE_ADDR start
;
589 register CORE_ADDR end
;
590 CORE_ADDR calling_pc
;
595 start
= BLOCK_START (block
);
596 end
= BLOCK_END (block
);
601 frame
= get_prev_frame (frame
);
604 calling_pc
= frame_address_in_block (frame
);
605 if (calling_pc
>= start
&& calling_pc
< end
)
610 /* Are we in a call dummy? The code below which allows DECR_PC_AFTER_BREAK
611 below is for infrun.c, which may give the macro a pc without that
614 extern CORE_ADDR text_end
;
617 deprecated_pc_in_call_dummy_before_text_end (CORE_ADDR pc
, CORE_ADDR sp
,
618 CORE_ADDR frame_address
)
620 return ((pc
) >= text_end
- CALL_DUMMY_LENGTH
621 && (pc
) <= text_end
+ DECR_PC_AFTER_BREAK
);
625 deprecated_pc_in_call_dummy_after_text_end (CORE_ADDR pc
, CORE_ADDR sp
,
626 CORE_ADDR frame_address
)
628 return ((pc
) >= text_end
629 && (pc
) <= text_end
+ CALL_DUMMY_LENGTH
+ DECR_PC_AFTER_BREAK
);
632 /* Is the PC in a call dummy? SP and FRAME_ADDRESS are the bottom and
633 top of the stack frame which we are checking, where "bottom" and
634 "top" refer to some section of memory which contains the code for
635 the call dummy. Calls to this macro assume that the contents of
636 SP_REGNUM and FP_REGNUM (or the saved values thereof), respectively,
637 are the things to pass.
639 This won't work on the 29k, where SP_REGNUM and FP_REGNUM don't
640 have that meaning, but the 29k doesn't use ON_STACK. This could be
641 fixed by generalizing this scheme, perhaps by passing in a frame
642 and adding a few fields, at least on machines which need them for
643 DEPRECATED_PC_IN_CALL_DUMMY.
645 Something simpler, like checking for the stack segment, doesn't work,
646 since various programs (threads implementations, gcc nested function
647 stubs, etc) may either allocate stack frames in another segment, or
648 allocate other kinds of code on the stack. */
651 deprecated_pc_in_call_dummy_on_stack (CORE_ADDR pc
, CORE_ADDR sp
,
652 CORE_ADDR frame_address
)
654 return (INNER_THAN ((sp
), (pc
))
655 && (frame_address
!= 0)
656 && INNER_THAN ((pc
), (frame_address
)));
660 deprecated_pc_in_call_dummy_at_entry_point (CORE_ADDR pc
, CORE_ADDR sp
,
661 CORE_ADDR frame_address
)
663 return ((pc
) >= CALL_DUMMY_ADDRESS ()
664 && (pc
) <= (CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK
));
667 /* Function: frame_chain_valid
668 Returns true for a user frame or a call_function_by_hand dummy frame,
669 and false for the CRT0 start-up frame. Purpose is to terminate backtrace. */
672 frame_chain_valid (CORE_ADDR fp
, struct frame_info
*fi
)
674 /* Don't prune CALL_DUMMY frames. */
675 if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES
676 && DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi
), 0, 0))
679 /* If the new frame pointer is zero, then it isn't valid. */
683 /* If the new frame would be inside (younger than) the previous frame,
684 then it isn't valid. */
685 if (INNER_THAN (fp
, get_frame_base (fi
)))
688 /* If we're already inside the entry function for the main objfile, then it
690 if (inside_entry_func (get_frame_pc (fi
)))
693 /* If we're inside the entry file, it isn't valid. */
694 /* NOTE/drow 2002-12-25: should there be a way to disable this check? It
695 assumes a single small entry file, and the way some debug readers (e.g.
696 dbxread) figure out which object is the entry file is somewhat hokey. */
697 if (inside_entry_file (frame_pc_unwind (fi
)))
700 /* If we want backtraces to stop at main, and we're inside main, then it
702 if (!backtrace_below_main
&& inside_main_func (get_frame_pc (fi
)))
705 /* If the architecture has a custom FRAME_CHAIN_VALID, call it now. */
706 if (FRAME_CHAIN_VALID_P ())
707 return FRAME_CHAIN_VALID (fp
, fi
);
713 do_flush_frames_sfunc (char *args
, int from_tty
, struct cmd_list_element
*c
)
716 struct frame_info
*cur_frame
;
718 if (! target_has_stack
)
721 saved_level
= frame_relative_level (get_selected_frame ());
723 flush_cached_frames ();
725 cur_frame
= find_relative_frame (get_current_frame (), &saved_level
);
726 select_frame (cur_frame
);
728 /* If we were below main and backtrace-below-main was turned off,
729 SAVED_LEVEL will be non-zero. CUR_FRAME will point to main.
730 Accept this but print the new frame. */
731 if (saved_level
!= 0)
732 print_stack_frame (get_selected_frame (), -1, 0);
736 _initialize_blockframe (void)
738 add_setshow_boolean_cmd ("backtrace-below-main", class_obscure
,
739 &backtrace_below_main
,
740 "Set whether backtraces should continue past \"main\".\n"
741 "Normally the caller of \"main\" is not of interest, so GDB will terminate\n"
742 "the backtrace at \"main\". Set this variable if you need to see the rest\n"
743 "of the stack trace.",
744 "Show whether backtraces should continue past \"main\".\n"
745 "Normally the caller of \"main\" is not of interest, so GDB will terminate\n"
746 "the backtrace at \"main\". Set this variable if you need to see the rest\n"
747 "of the stack trace.",
748 do_flush_frames_sfunc
, NULL
, &setlist
, &showlist
);