1 /* Get info from stack frames;
2 convert between frames, blocks, functions and pc values.
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
30 #include "value.h" /* for read_register */
31 #include "target.h" /* for target_has_stack */
32 #include "inferior.h" /* for read_pc */
36 /* Prototypes for exported functions. */
38 void _initialize_blockframe (void);
40 /* A default FRAME_CHAIN_VALID, in the form that is suitable for most
41 targets. If FRAME_CHAIN_VALID returns zero it means that the given
42 frame is the outermost one and has no caller. */
45 file_frame_chain_valid (CORE_ADDR chain
, struct frame_info
*thisframe
)
48 && !inside_entry_file (FRAME_SAVED_PC (thisframe
)));
51 /* Use the alternate method of avoiding running up off the end of the
52 frame chain or following frames back into the startup code. See
53 the comments in objfiles.h. */
56 func_frame_chain_valid (CORE_ADDR chain
, struct frame_info
*thisframe
)
59 && !inside_main_func ((thisframe
)->pc
)
60 && !inside_entry_func ((thisframe
)->pc
));
63 /* A very simple method of determining a valid frame */
66 nonnull_frame_chain_valid (CORE_ADDR chain
, struct frame_info
*thisframe
)
68 return ((chain
) != 0);
71 /* Is ADDR inside the startup file? Note that if your machine
72 has a way to detect the bottom of the stack, there is no need
73 to call this function from FRAME_CHAIN_VALID; the reason for
74 doing so is that some machines have no way of detecting bottom
77 A PC of zero is always considered to be the bottom of the stack. */
80 inside_entry_file (CORE_ADDR addr
)
84 if (symfile_objfile
== 0)
86 if (CALL_DUMMY_LOCATION
== AT_ENTRY_POINT
)
88 /* Do not stop backtracing if the pc is in the call dummy
89 at the entry point. */
90 /* FIXME: Won't always work with zeros for the last two arguments */
91 if (PC_IN_CALL_DUMMY (addr
, 0, 0))
94 return (addr
>= symfile_objfile
->ei
.entry_file_lowpc
&&
95 addr
< symfile_objfile
->ei
.entry_file_highpc
);
98 /* Test a specified PC value to see if it is in the range of addresses
99 that correspond to the main() function. See comments above for why
100 we might want to do this.
102 Typically called from FRAME_CHAIN_VALID.
104 A PC of zero is always considered to be the bottom of the stack. */
107 inside_main_func (CORE_ADDR pc
)
111 if (symfile_objfile
== 0)
114 /* If the addr range is not set up at symbol reading time, set it up now.
115 This is for FRAME_CHAIN_VALID_ALTERNATE. I do this for coff, because
116 it is unable to set it up and symbol reading time. */
118 if (symfile_objfile
->ei
.main_func_lowpc
== INVALID_ENTRY_LOWPC
&&
119 symfile_objfile
->ei
.main_func_highpc
== INVALID_ENTRY_HIGHPC
)
121 struct symbol
*mainsym
;
123 mainsym
= lookup_symbol ("main", NULL
, VAR_NAMESPACE
, NULL
, NULL
);
124 if (mainsym
&& SYMBOL_CLASS (mainsym
) == LOC_BLOCK
)
126 symfile_objfile
->ei
.main_func_lowpc
=
127 BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym
));
128 symfile_objfile
->ei
.main_func_highpc
=
129 BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym
));
132 return (symfile_objfile
->ei
.main_func_lowpc
<= pc
&&
133 symfile_objfile
->ei
.main_func_highpc
> pc
);
136 /* Test a specified PC value to see if it is in the range of addresses
137 that correspond to the process entry point function. See comments
138 in objfiles.h for why we might want to do this.
140 Typically called from FRAME_CHAIN_VALID.
142 A PC of zero is always considered to be the bottom of the stack. */
145 inside_entry_func (CORE_ADDR pc
)
149 if (symfile_objfile
== 0)
151 if (CALL_DUMMY_LOCATION
== AT_ENTRY_POINT
)
153 /* Do not stop backtracing if the pc is in the call dummy
154 at the entry point. */
155 /* FIXME: Won't always work with zeros for the last two arguments */
156 if (PC_IN_CALL_DUMMY (pc
, 0, 0))
159 return (symfile_objfile
->ei
.entry_func_lowpc
<= pc
&&
160 symfile_objfile
->ei
.entry_func_highpc
> pc
);
163 /* Info about the innermost stack frame (contents of FP register) */
165 static struct frame_info
*current_frame
;
167 /* Cache for frame addresses already read by gdb. Valid only while
168 inferior is stopped. Control variables for the frame cache should
169 be local to this module. */
171 static struct obstack frame_cache_obstack
;
174 frame_obstack_alloc (unsigned long size
)
176 return obstack_alloc (&frame_cache_obstack
, size
);
180 frame_saved_regs_zalloc (struct frame_info
*fi
)
182 fi
->saved_regs
= (CORE_ADDR
*)
183 frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS
);
184 memset (fi
->saved_regs
, 0, SIZEOF_FRAME_SAVED_REGS
);
188 /* Return the innermost (currently executing) stack frame. */
191 get_current_frame (void)
193 if (current_frame
== NULL
)
195 if (target_has_stack
)
196 current_frame
= create_new_frame (read_fp (), read_pc ());
200 return current_frame
;
204 set_current_frame (struct frame_info
*frame
)
206 current_frame
= frame
;
209 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
210 Always returns a non-NULL value. */
213 create_new_frame (CORE_ADDR addr
, CORE_ADDR pc
)
215 struct frame_info
*fi
;
218 fi
= (struct frame_info
*)
219 obstack_alloc (&frame_cache_obstack
,
220 sizeof (struct frame_info
));
222 /* Zero all fields by default. */
223 memset (fi
, 0, sizeof (struct frame_info
));
227 find_pc_partial_function (pc
, &name
, (CORE_ADDR
*) NULL
, (CORE_ADDR
*) NULL
);
228 fi
->signal_handler_caller
= IN_SIGTRAMP (fi
->pc
, name
);
230 #ifdef INIT_EXTRA_FRAME_INFO
231 INIT_EXTRA_FRAME_INFO (0, fi
);
237 /* Return the frame that FRAME calls (NULL if FRAME is the innermost
241 get_next_frame (struct frame_info
*frame
)
246 /* Flush the entire frame cache. */
249 flush_cached_frames (void)
251 /* Since we can't really be sure what the first object allocated was */
252 obstack_free (&frame_cache_obstack
, 0);
253 obstack_init (&frame_cache_obstack
);
255 current_frame
= NULL
; /* Invalidate cache */
256 select_frame (NULL
, -1);
257 annotate_frames_invalid ();
260 /* Flush the frame cache, and start a new one if necessary. */
263 reinit_frame_cache (void)
265 flush_cached_frames ();
267 /* FIXME: The inferior_ptid test is wrong if there is a corefile. */
268 if (PIDGET (inferior_ptid
) != 0)
270 select_frame (get_current_frame (), 0);
274 /* Return nonzero if the function for this frame lacks a prologue. Many
275 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
279 frameless_look_for_prologue (struct frame_info
*frame
)
281 CORE_ADDR func_start
, after_prologue
;
283 func_start
= get_pc_function_start (frame
->pc
);
286 func_start
+= FUNCTION_START_OFFSET
;
287 /* This is faster, since only care whether there *is* a
288 prologue, not how long it is. */
289 return PROLOGUE_FRAMELESS_P (func_start
);
291 else if (frame
->pc
== 0)
292 /* A frame with a zero PC is usually created by dereferencing a
293 NULL function pointer, normally causing an immediate core dump
294 of the inferior. Mark function as frameless, as the inferior
295 has no chance of setting up a stack frame. */
298 /* If we can't find the start of the function, we don't really
299 know whether the function is frameless, but we should be able
300 to get a reasonable (i.e. best we can do under the
301 circumstances) backtrace by saying that it isn't. */
305 /* Default a few macros that people seldom redefine. */
307 #if !defined (INIT_FRAME_PC)
308 #define INIT_FRAME_PC(fromleaf, prev) \
309 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
310 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
313 #ifndef FRAME_CHAIN_COMBINE
314 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
317 /* Return a structure containing various interesting information
318 about the frame that called NEXT_FRAME. Returns NULL
319 if there is no such frame. */
322 get_prev_frame (struct frame_info
*next_frame
)
324 CORE_ADDR address
= 0;
325 struct frame_info
*prev
;
329 /* If the requested entry is in the cache, return it.
330 Otherwise, figure out what the address should be for the entry
331 we're about to add to the cache. */
336 /* This screws value_of_variable, which just wants a nice clean
337 NULL return from block_innermost_frame if there are no frames.
338 I don't think I've ever seen this message happen otherwise.
339 And returning NULL here is a perfectly legitimate thing to do. */
342 error ("You haven't set up a process's stack to examine.");
346 return current_frame
;
349 /* If we have the prev one, return it */
350 if (next_frame
->prev
)
351 return next_frame
->prev
;
353 /* On some machines it is possible to call a function without
354 setting up a stack frame for it. On these machines, we
355 define this macro to take two args; a frameinfo pointer
356 identifying a frame and a variable to set or clear if it is
357 or isn't leafless. */
359 /* Still don't want to worry about this except on the innermost
360 frame. This macro will set FROMLEAF if NEXT_FRAME is a
361 frameless function invocation. */
362 if (!(next_frame
->next
))
364 fromleaf
= FRAMELESS_FUNCTION_INVOCATION (next_frame
);
366 address
= FRAME_FP (next_frame
);
371 /* Two macros defined in tm.h specify the machine-dependent
372 actions to be performed here.
373 First, get the frame's chain-pointer.
374 If that is zero, the frame is the outermost frame or a leaf
375 called by the outermost frame. This means that if start
376 calls main without a frame, we'll return 0 (which is fine
379 Nope; there's a problem. This also returns when the current
380 routine is a leaf of main. This is unacceptable. We move
381 this to after the ffi test; I'd rather have backtraces from
382 start go curfluy than have an abort called from main not show
384 address
= FRAME_CHAIN (next_frame
);
385 if (!FRAME_CHAIN_VALID (address
, next_frame
))
387 address
= FRAME_CHAIN_COMBINE (address
, next_frame
);
392 prev
= (struct frame_info
*)
393 obstack_alloc (&frame_cache_obstack
,
394 sizeof (struct frame_info
));
396 /* Zero all fields by default. */
397 memset (prev
, 0, sizeof (struct frame_info
));
400 next_frame
->prev
= prev
;
401 prev
->next
= next_frame
;
402 prev
->frame
= address
;
404 /* This change should not be needed, FIXME! We should
405 determine whether any targets *need* INIT_FRAME_PC to happen
406 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
407 express what goes on here.
409 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
410 (where the PC is already set up) and here (where it isn't).
411 INIT_FRAME_PC is only called from here, always after
412 INIT_EXTRA_FRAME_INFO.
414 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
415 value (which hasn't been set yet). Some other machines appear to
416 require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo.
418 We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
419 an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92.
421 Assuming that some machines need INIT_FRAME_PC after
422 INIT_EXTRA_FRAME_INFO, one possible scheme:
424 SETUP_INNERMOST_FRAME()
425 Default version is just create_new_frame (read_fp ()),
426 read_pc ()). Machines with extra frame info would do that (or the
427 local equivalent) and then set the extra fields.
428 SETUP_ARBITRARY_FRAME(argc, argv)
429 Only change here is that create_new_frame would no longer init extra
430 frame info; SETUP_ARBITRARY_FRAME would have to do that.
431 INIT_PREV_FRAME(fromleaf, prev)
432 Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC. This should
433 also return a flag saying whether to keep the new frame, or
434 whether to discard it, because on some machines (e.g. mips) it
435 is really awkward to have FRAME_CHAIN_VALID called *before*
436 INIT_EXTRA_FRAME_INFO (there is no good way to get information
437 deduced in FRAME_CHAIN_VALID into the extra fields of the new frame).
438 std_frame_pc(fromleaf, prev)
439 This is the default setting for INIT_PREV_FRAME. It just does what
440 the default INIT_FRAME_PC does. Some machines will call it from
441 INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
442 Some machines won't use it.
443 kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */
445 INIT_FRAME_PC_FIRST (fromleaf
, prev
);
447 #ifdef INIT_EXTRA_FRAME_INFO
448 INIT_EXTRA_FRAME_INFO (fromleaf
, prev
);
451 /* This entry is in the frame queue now, which is good since
452 FRAME_SAVED_PC may use that queue to figure out its value
453 (see tm-sparc.h). We want the pc saved in the inferior frame. */
454 INIT_FRAME_PC (fromleaf
, prev
);
456 /* If ->frame and ->pc are unchanged, we are in the process of getting
457 ourselves into an infinite backtrace. Some architectures check this
458 in FRAME_CHAIN or thereabouts, but it seems like there is no reason
459 this can't be an architecture-independent check. */
460 if (next_frame
!= NULL
)
462 if (prev
->frame
== next_frame
->frame
463 && prev
->pc
== next_frame
->pc
)
465 next_frame
->prev
= NULL
;
466 obstack_free (&frame_cache_obstack
, prev
);
471 find_pc_partial_function (prev
->pc
, &name
,
472 (CORE_ADDR
*) NULL
, (CORE_ADDR
*) NULL
);
473 if (IN_SIGTRAMP (prev
->pc
, name
))
474 prev
->signal_handler_caller
= 1;
480 get_frame_pc (struct frame_info
*frame
)
486 #ifdef FRAME_FIND_SAVED_REGS
487 /* XXX - deprecated. This is a compatibility function for targets
488 that do not yet implement FRAME_INIT_SAVED_REGS. */
489 /* Find the addresses in which registers are saved in FRAME. */
492 get_frame_saved_regs (struct frame_info
*frame
,
493 struct frame_saved_regs
*saved_regs_addr
)
495 if (frame
->saved_regs
== NULL
)
497 frame
->saved_regs
= (CORE_ADDR
*)
498 frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS
);
500 if (saved_regs_addr
== NULL
)
502 struct frame_saved_regs saved_regs
;
503 FRAME_FIND_SAVED_REGS (frame
, saved_regs
);
504 memcpy (frame
->saved_regs
, &saved_regs
, SIZEOF_FRAME_SAVED_REGS
);
508 FRAME_FIND_SAVED_REGS (frame
, *saved_regs_addr
);
509 memcpy (frame
->saved_regs
, saved_regs_addr
, SIZEOF_FRAME_SAVED_REGS
);
514 /* Return the innermost lexical block in execution
515 in a specified stack frame. The frame address is assumed valid. */
518 get_frame_block (struct frame_info
*frame
)
523 if (frame
->next
!= 0 && frame
->next
->signal_handler_caller
== 0)
524 /* We are not in the innermost frame and we were not interrupted
525 by a signal. We need to subtract one to get the correct block,
526 in case the call instruction was the last instruction of the block.
527 If there are any machines on which the saved pc does not point to
528 after the call insn, we probably want to make frame->pc point after
529 the call insn anyway. */
531 return block_for_pc (pc
);
535 get_current_block (void)
537 return block_for_pc (read_pc ());
541 get_pc_function_start (CORE_ADDR pc
)
543 register struct block
*bl
;
544 register struct symbol
*symbol
;
545 register struct minimal_symbol
*msymbol
;
548 if ((bl
= block_for_pc (pc
)) != NULL
&&
549 (symbol
= block_function (bl
)) != NULL
)
551 bl
= SYMBOL_BLOCK_VALUE (symbol
);
552 fstart
= BLOCK_START (bl
);
554 else if ((msymbol
= lookup_minimal_symbol_by_pc (pc
)) != NULL
)
556 fstart
= SYMBOL_VALUE_ADDRESS (msymbol
);
565 /* Return the symbol for the function executing in frame FRAME. */
568 get_frame_function (struct frame_info
*frame
)
570 register struct block
*bl
= get_frame_block (frame
);
573 return block_function (bl
);
577 /* Return the blockvector immediately containing the innermost lexical block
578 containing the specified pc value and section, or 0 if there is none.
579 PINDEX is a pointer to the index value of the block. If PINDEX
580 is NULL, we don't pass this information back to the caller. */
583 blockvector_for_pc_sect (register CORE_ADDR pc
, struct sec
*section
,
584 int *pindex
, struct symtab
*symtab
)
586 register struct block
*b
;
587 register int bot
, top
, half
;
588 struct blockvector
*bl
;
590 if (symtab
== 0) /* if no symtab specified by caller */
592 /* First search all symtabs for one whose file contains our pc */
593 if ((symtab
= find_pc_sect_symtab (pc
, section
)) == 0)
597 bl
= BLOCKVECTOR (symtab
);
598 b
= BLOCKVECTOR_BLOCK (bl
, 0);
600 /* Then search that symtab for the smallest block that wins. */
601 /* Use binary search to find the last block that starts before PC. */
604 top
= BLOCKVECTOR_NBLOCKS (bl
);
606 while (top
- bot
> 1)
608 half
= (top
- bot
+ 1) >> 1;
609 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
610 if (BLOCK_START (b
) <= pc
)
616 /* Now search backward for a block that ends after PC. */
620 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
621 if (BLOCK_END (b
) > pc
)
632 /* Return the blockvector immediately containing the innermost lexical block
633 containing the specified pc value, or 0 if there is none.
634 Backward compatibility, no section. */
637 blockvector_for_pc (register CORE_ADDR pc
, int *pindex
)
639 return blockvector_for_pc_sect (pc
, find_pc_mapped_section (pc
),
643 /* Return the innermost lexical block containing the specified pc value
644 in the specified section, or 0 if there is none. */
647 block_for_pc_sect (register CORE_ADDR pc
, struct sec
*section
)
649 register struct blockvector
*bl
;
652 bl
= blockvector_for_pc_sect (pc
, section
, &index
, NULL
);
654 return BLOCKVECTOR_BLOCK (bl
, index
);
658 /* Return the innermost lexical block containing the specified pc value,
659 or 0 if there is none. Backward compatibility, no section. */
662 block_for_pc (register CORE_ADDR pc
)
664 return block_for_pc_sect (pc
, find_pc_mapped_section (pc
));
667 /* Return the function containing pc value PC in section SECTION.
668 Returns 0 if function is not known. */
671 find_pc_sect_function (CORE_ADDR pc
, struct sec
*section
)
673 register struct block
*b
= block_for_pc_sect (pc
, section
);
676 return block_function (b
);
679 /* Return the function containing pc value PC.
680 Returns 0 if function is not known. Backward compatibility, no section */
683 find_pc_function (CORE_ADDR pc
)
685 return find_pc_sect_function (pc
, find_pc_mapped_section (pc
));
688 /* These variables are used to cache the most recent result
689 * of find_pc_partial_function. */
691 static CORE_ADDR cache_pc_function_low
= 0;
692 static CORE_ADDR cache_pc_function_high
= 0;
693 static char *cache_pc_function_name
= 0;
694 static struct sec
*cache_pc_function_section
= NULL
;
696 /* Clear cache, e.g. when symbol table is discarded. */
699 clear_pc_function_cache (void)
701 cache_pc_function_low
= 0;
702 cache_pc_function_high
= 0;
703 cache_pc_function_name
= (char *) 0;
704 cache_pc_function_section
= NULL
;
707 /* Finds the "function" (text symbol) that is smaller than PC but
708 greatest of all of the potential text symbols in SECTION. Sets
709 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
710 If ENDADDR is non-null, then set *ENDADDR to be the end of the
711 function (exclusive), but passing ENDADDR as non-null means that
712 the function might cause symbols to be read. This function either
713 succeeds or fails (not halfway succeeds). If it succeeds, it sets
714 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
715 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
719 find_pc_sect_partial_function (CORE_ADDR pc
, asection
*section
, char **name
,
720 CORE_ADDR
*address
, CORE_ADDR
*endaddr
)
722 struct partial_symtab
*pst
;
724 struct minimal_symbol
*msymbol
;
725 struct partial_symbol
*psb
;
726 struct obj_section
*osect
;
730 mapped_pc
= overlay_mapped_address (pc
, section
);
732 if (mapped_pc
>= cache_pc_function_low
&&
733 mapped_pc
< cache_pc_function_high
&&
734 section
== cache_pc_function_section
)
735 goto return_cached_value
;
737 /* If sigtramp is in the u area, it counts as a function (especially
738 important for step_1). */
739 #if defined SIGTRAMP_START
740 if (IN_SIGTRAMP (mapped_pc
, (char *) NULL
))
742 cache_pc_function_low
= SIGTRAMP_START (mapped_pc
);
743 cache_pc_function_high
= SIGTRAMP_END (mapped_pc
);
744 cache_pc_function_name
= "<sigtramp>";
745 cache_pc_function_section
= section
;
746 goto return_cached_value
;
750 msymbol
= lookup_minimal_symbol_by_pc_section (mapped_pc
, section
);
751 pst
= find_pc_sect_psymtab (mapped_pc
, section
);
754 /* Need to read the symbols to get a good value for the end address. */
755 if (endaddr
!= NULL
&& !pst
->readin
)
757 /* Need to get the terminal in case symbol-reading produces
759 target_terminal_ours_for_output ();
760 PSYMTAB_TO_SYMTAB (pst
);
765 /* Checking whether the msymbol has a larger value is for the
766 "pathological" case mentioned in print_frame_info. */
767 f
= find_pc_sect_function (mapped_pc
, section
);
770 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f
))
771 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
773 cache_pc_function_low
= BLOCK_START (SYMBOL_BLOCK_VALUE (f
));
774 cache_pc_function_high
= BLOCK_END (SYMBOL_BLOCK_VALUE (f
));
775 cache_pc_function_name
= SYMBOL_NAME (f
);
776 cache_pc_function_section
= section
;
777 goto return_cached_value
;
782 /* Now that static symbols go in the minimal symbol table, perhaps
783 we could just ignore the partial symbols. But at least for now
784 we use the partial or minimal symbol, whichever is larger. */
785 psb
= find_pc_sect_psymbol (pst
, mapped_pc
, section
);
788 && (msymbol
== NULL
||
789 (SYMBOL_VALUE_ADDRESS (psb
)
790 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
792 /* This case isn't being cached currently. */
794 *address
= SYMBOL_VALUE_ADDRESS (psb
);
796 *name
= SYMBOL_NAME (psb
);
797 /* endaddr non-NULL can't happen here. */
803 /* Not in the normal symbol tables, see if the pc is in a known section.
804 If it's not, then give up. This ensures that anything beyond the end
805 of the text seg doesn't appear to be part of the last function in the
808 osect
= find_pc_sect_section (mapped_pc
, section
);
813 /* Must be in the minimal symbol table. */
816 /* No available symbol. */
826 cache_pc_function_low
= SYMBOL_VALUE_ADDRESS (msymbol
);
827 cache_pc_function_name
= SYMBOL_NAME (msymbol
);
828 cache_pc_function_section
= section
;
830 /* Use the lesser of the next minimal symbol in the same section, or
831 the end of the section, as the end of the function. */
833 /* Step over other symbols at this same address, and symbols in
834 other sections, to find the next symbol in this section with
835 a different address. */
837 for (i
= 1; SYMBOL_NAME (msymbol
+ i
) != NULL
; i
++)
839 if (SYMBOL_VALUE_ADDRESS (msymbol
+ i
) != SYMBOL_VALUE_ADDRESS (msymbol
)
840 && SYMBOL_BFD_SECTION (msymbol
+ i
) == SYMBOL_BFD_SECTION (msymbol
))
844 if (SYMBOL_NAME (msymbol
+ i
) != NULL
845 && SYMBOL_VALUE_ADDRESS (msymbol
+ i
) < osect
->endaddr
)
846 cache_pc_function_high
= SYMBOL_VALUE_ADDRESS (msymbol
+ i
);
848 /* We got the start address from the last msymbol in the objfile.
849 So the end address is the end of the section. */
850 cache_pc_function_high
= osect
->endaddr
;
856 if (pc_in_unmapped_range (pc
, section
))
857 *address
= overlay_unmapped_address (cache_pc_function_low
, section
);
859 *address
= cache_pc_function_low
;
863 *name
= cache_pc_function_name
;
867 if (pc_in_unmapped_range (pc
, section
))
869 /* Because the high address is actually beyond the end of
870 the function (and therefore possibly beyond the end of
871 the overlay), we must actually convert (high - 1)
872 and then add one to that. */
874 *endaddr
= 1 + overlay_unmapped_address (cache_pc_function_high
- 1,
878 *endaddr
= cache_pc_function_high
;
884 /* Backward compatibility, no section argument */
887 find_pc_partial_function (CORE_ADDR pc
, char **name
, CORE_ADDR
*address
,
892 section
= find_pc_overlay (pc
);
893 return find_pc_sect_partial_function (pc
, section
, name
, address
, endaddr
);
896 /* Return the innermost stack frame executing inside of BLOCK,
897 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
900 block_innermost_frame (struct block
*block
)
902 struct frame_info
*frame
;
903 register CORE_ADDR start
;
904 register CORE_ADDR end
;
909 start
= BLOCK_START (block
);
910 end
= BLOCK_END (block
);
915 frame
= get_prev_frame (frame
);
918 if (frame
->pc
>= start
&& frame
->pc
< end
)
923 /* Return the full FRAME which corresponds to the given CORE_ADDR
924 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
927 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
929 struct frame_info
*frame
= NULL
;
931 if (frame_addr
== (CORE_ADDR
) 0)
936 frame
= get_prev_frame (frame
);
939 if (FRAME_FP (frame
) == frame_addr
)
944 #ifdef SIGCONTEXT_PC_OFFSET
945 /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp. */
948 sigtramp_saved_pc (struct frame_info
*frame
)
950 CORE_ADDR sigcontext_addr
;
952 int ptrbytes
= TARGET_PTR_BIT
/ TARGET_CHAR_BIT
;
953 int sigcontext_offs
= (2 * TARGET_INT_BIT
) / TARGET_CHAR_BIT
;
955 buf
= alloca (ptrbytes
);
956 /* Get sigcontext address, it is the third parameter on the stack. */
958 sigcontext_addr
= read_memory_integer (FRAME_ARGS_ADDRESS (frame
->next
)
963 sigcontext_addr
= read_memory_integer (read_register (SP_REGNUM
)
967 /* Don't cause a memory_error when accessing sigcontext in case the stack
968 layout has changed or the stack is corrupt. */
969 target_read_memory (sigcontext_addr
+ SIGCONTEXT_PC_OFFSET
, buf
, ptrbytes
);
970 return extract_unsigned_integer (buf
, ptrbytes
);
972 #endif /* SIGCONTEXT_PC_OFFSET */
975 /* Are we in a call dummy? The code below which allows DECR_PC_AFTER_BREAK
976 below is for infrun.c, which may give the macro a pc without that
979 extern CORE_ADDR text_end
;
982 pc_in_call_dummy_before_text_end (CORE_ADDR pc
, CORE_ADDR sp
,
983 CORE_ADDR frame_address
)
985 return ((pc
) >= text_end
- CALL_DUMMY_LENGTH
986 && (pc
) <= text_end
+ DECR_PC_AFTER_BREAK
);
990 pc_in_call_dummy_after_text_end (CORE_ADDR pc
, CORE_ADDR sp
,
991 CORE_ADDR frame_address
)
993 return ((pc
) >= text_end
994 && (pc
) <= text_end
+ CALL_DUMMY_LENGTH
+ DECR_PC_AFTER_BREAK
);
997 /* Is the PC in a call dummy? SP and FRAME_ADDRESS are the bottom and
998 top of the stack frame which we are checking, where "bottom" and
999 "top" refer to some section of memory which contains the code for
1000 the call dummy. Calls to this macro assume that the contents of
1001 SP_REGNUM and FP_REGNUM (or the saved values thereof), respectively,
1002 are the things to pass.
1004 This won't work on the 29k, where SP_REGNUM and FP_REGNUM don't
1005 have that meaning, but the 29k doesn't use ON_STACK. This could be
1006 fixed by generalizing this scheme, perhaps by passing in a frame
1007 and adding a few fields, at least on machines which need them for
1010 Something simpler, like checking for the stack segment, doesn't work,
1011 since various programs (threads implementations, gcc nested function
1012 stubs, etc) may either allocate stack frames in another segment, or
1013 allocate other kinds of code on the stack. */
1016 pc_in_call_dummy_on_stack (CORE_ADDR pc
, CORE_ADDR sp
, CORE_ADDR frame_address
)
1018 return (INNER_THAN ((sp
), (pc
))
1019 && (frame_address
!= 0)
1020 && INNER_THAN ((pc
), (frame_address
)));
1024 pc_in_call_dummy_at_entry_point (CORE_ADDR pc
, CORE_ADDR sp
,
1025 CORE_ADDR frame_address
)
1027 return ((pc
) >= CALL_DUMMY_ADDRESS ()
1028 && (pc
) <= (CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK
));
1033 * GENERIC DUMMY FRAMES
1035 * The following code serves to maintain the dummy stack frames for
1036 * inferior function calls (ie. when gdb calls into the inferior via
1037 * call_function_by_hand). This code saves the machine state before
1038 * the call in host memory, so we must maintain an independent stack
1039 * and keep it consistant etc. I am attempting to make this code
1040 * generic enough to be used by many targets.
1042 * The cheapest and most generic way to do CALL_DUMMY on a new target
1043 * is probably to define CALL_DUMMY to be empty, CALL_DUMMY_LENGTH to
1044 * zero, and CALL_DUMMY_LOCATION to AT_ENTRY. Then you must remember
1045 * to define PUSH_RETURN_ADDRESS, because no call instruction will be
1046 * being executed by the target. Also FRAME_CHAIN_VALID as
1047 * generic_{file,func}_frame_chain_valid and FIX_CALL_DUMMY as
1048 * generic_fix_call_dummy. */
1050 /* Dummy frame. This saves the processor state just prior to setting
1051 up the inferior function call. Older targets save the registers
1052 on the target stack (but that really slows down function calls). */
1056 struct dummy_frame
*next
;
1065 static struct dummy_frame
*dummy_frame_stack
= NULL
;
1067 /* Function: find_dummy_frame(pc, fp, sp)
1068 Search the stack of dummy frames for one matching the given PC, FP and SP.
1069 This is the work-horse for pc_in_call_dummy and read_register_dummy */
1072 generic_find_dummy_frame (CORE_ADDR pc
, CORE_ADDR fp
)
1074 struct dummy_frame
*dummyframe
;
1076 if (pc
!= entry_point_address ())
1079 for (dummyframe
= dummy_frame_stack
; dummyframe
!= NULL
;
1080 dummyframe
= dummyframe
->next
)
1081 if (fp
== dummyframe
->fp
1082 || fp
== dummyframe
->sp
1083 || fp
== dummyframe
->top
)
1084 /* The frame in question lies between the saved fp and sp, inclusive */
1085 return dummyframe
->registers
;
1090 /* Function: pc_in_call_dummy (pc, fp)
1091 Return true if this is a dummy frame created by gdb for an inferior call */
1094 generic_pc_in_call_dummy (CORE_ADDR pc
, CORE_ADDR sp
, CORE_ADDR fp
)
1096 /* if find_dummy_frame succeeds, then PC is in a call dummy */
1097 /* Note: SP and not FP is passed on. */
1098 return (generic_find_dummy_frame (pc
, sp
) != 0);
1101 /* Function: read_register_dummy
1102 Find a saved register from before GDB calls a function in the inferior */
1105 generic_read_register_dummy (CORE_ADDR pc
, CORE_ADDR fp
, int regno
)
1107 char *dummy_regs
= generic_find_dummy_frame (pc
, fp
);
1110 return extract_address (&dummy_regs
[REGISTER_BYTE (regno
)],
1111 REGISTER_RAW_SIZE (regno
));
1116 /* Save all the registers on the dummy frame stack. Most ports save the
1117 registers on the target stack. This results in lots of unnecessary memory
1118 references, which are slow when debugging via a serial line. Instead, we
1119 save all the registers internally, and never write them to the stack. The
1120 registers get restored when the called function returns to the entry point,
1121 where a breakpoint is laying in wait. */
1124 generic_push_dummy_frame (void)
1126 struct dummy_frame
*dummy_frame
;
1127 CORE_ADDR fp
= (get_current_frame ())->frame
;
1129 /* check to see if there are stale dummy frames,
1130 perhaps left over from when a longjump took us out of a
1131 function that was called by the debugger */
1133 dummy_frame
= dummy_frame_stack
;
1135 if (INNER_THAN (dummy_frame
->fp
, fp
)) /* stale -- destroy! */
1137 dummy_frame_stack
= dummy_frame
->next
;
1138 xfree (dummy_frame
->registers
);
1139 xfree (dummy_frame
);
1140 dummy_frame
= dummy_frame_stack
;
1143 dummy_frame
= dummy_frame
->next
;
1145 dummy_frame
= xmalloc (sizeof (struct dummy_frame
));
1146 dummy_frame
->registers
= xmalloc (REGISTER_BYTES
);
1148 dummy_frame
->pc
= read_pc ();
1149 dummy_frame
->sp
= read_sp ();
1150 dummy_frame
->top
= dummy_frame
->sp
;
1151 dummy_frame
->fp
= fp
;
1152 read_register_bytes (0, dummy_frame
->registers
, REGISTER_BYTES
);
1153 dummy_frame
->next
= dummy_frame_stack
;
1154 dummy_frame_stack
= dummy_frame
;
1158 generic_save_dummy_frame_tos (CORE_ADDR sp
)
1160 dummy_frame_stack
->top
= sp
;
1163 /* Restore the machine state from either the saved dummy stack or a
1164 real stack frame. */
1167 generic_pop_current_frame (void (*popper
) (struct frame_info
* frame
))
1169 struct frame_info
*frame
= get_current_frame ();
1171 if (PC_IN_CALL_DUMMY (frame
->pc
, frame
->frame
, frame
->frame
))
1172 generic_pop_dummy_frame ();
1177 /* Function: pop_dummy_frame
1178 Restore the machine state from a saved dummy stack frame. */
1181 generic_pop_dummy_frame (void)
1183 struct dummy_frame
*dummy_frame
= dummy_frame_stack
;
1185 /* FIXME: what if the first frame isn't the right one, eg..
1186 because one call-by-hand function has done a longjmp into another one? */
1189 error ("Can't pop dummy frame!");
1190 dummy_frame_stack
= dummy_frame
->next
;
1191 write_register_bytes (0, dummy_frame
->registers
, REGISTER_BYTES
);
1192 flush_cached_frames ();
1194 xfree (dummy_frame
->registers
);
1195 xfree (dummy_frame
);
1198 /* Function: frame_chain_valid
1199 Returns true for a user frame or a call_function_by_hand dummy frame,
1200 and false for the CRT0 start-up frame. Purpose is to terminate backtrace */
1203 generic_file_frame_chain_valid (CORE_ADDR fp
, struct frame_info
*fi
)
1205 if (PC_IN_CALL_DUMMY (FRAME_SAVED_PC (fi
), fp
, fp
))
1206 return 1; /* don't prune CALL_DUMMY frames */
1207 else /* fall back to default algorithm (see frame.h) */
1209 && (INNER_THAN (fi
->frame
, fp
) || fi
->frame
== fp
)
1210 && !inside_entry_file (FRAME_SAVED_PC (fi
)));
1214 generic_func_frame_chain_valid (CORE_ADDR fp
, struct frame_info
*fi
)
1216 if (PC_IN_CALL_DUMMY ((fi
)->pc
, fp
, fp
))
1217 return 1; /* don't prune CALL_DUMMY frames */
1218 else /* fall back to default algorithm (see frame.h) */
1220 && (INNER_THAN (fi
->frame
, fp
) || fi
->frame
== fp
)
1221 && !inside_main_func ((fi
)->pc
)
1222 && !inside_entry_func ((fi
)->pc
));
1225 /* Function: fix_call_dummy
1226 Stub function. Generic dumy frames typically do not need to fix
1227 the frame being created */
1230 generic_fix_call_dummy (char *dummy
, CORE_ADDR pc
, CORE_ADDR fun
, int nargs
,
1231 struct value
**args
, struct type
*type
, int gcc_p
)
1236 /* Function: get_saved_register
1237 Find register number REGNUM relative to FRAME and put its (raw,
1238 target format) contents in *RAW_BUFFER.
1240 Set *OPTIMIZED if the variable was optimized out (and thus can't be
1241 fetched). Note that this is never set to anything other than zero
1242 in this implementation.
1244 Set *LVAL to lval_memory, lval_register, or not_lval, depending on
1245 whether the value was fetched from memory, from a register, or in a
1246 strange and non-modifiable way (e.g. a frame pointer which was
1247 calculated rather than fetched). We will use not_lval for values
1248 fetched from generic dummy frames.
1250 Set *ADDRP to the address, either in memory or as a REGISTER_BYTE
1251 offset into the registers array. If the value is stored in a dummy
1252 frame, set *ADDRP to zero.
1254 To use this implementation, define a function called
1255 "get_saved_register" in your target code, which simply passes all
1256 of its arguments to this function.
1258 The argument RAW_BUFFER must point to aligned memory. */
1261 generic_get_saved_register (char *raw_buffer
, int *optimized
, CORE_ADDR
*addrp
,
1262 struct frame_info
*frame
, int regnum
,
1263 enum lval_type
*lval
)
1265 if (!target_has_registers
)
1266 error ("No registers.");
1268 /* Normal systems don't optimize out things with register numbers. */
1269 if (optimized
!= NULL
)
1272 if (addrp
) /* default assumption: not found in memory */
1275 /* Note: since the current frame's registers could only have been
1276 saved by frames INTERIOR TO the current frame, we skip examining
1277 the current frame itself: otherwise, we would be getting the
1278 previous frame's registers which were saved by the current frame. */
1280 while (frame
&& ((frame
= frame
->next
) != NULL
))
1282 if (PC_IN_CALL_DUMMY (frame
->pc
, frame
->frame
, frame
->frame
))
1284 if (lval
) /* found it in a CALL_DUMMY frame */
1288 generic_find_dummy_frame (frame
->pc
, frame
->frame
) +
1289 REGISTER_BYTE (regnum
),
1290 REGISTER_RAW_SIZE (regnum
));
1294 FRAME_INIT_SAVED_REGS (frame
);
1295 if (frame
->saved_regs
!= NULL
1296 && frame
->saved_regs
[regnum
] != 0)
1298 if (lval
) /* found it saved on the stack */
1299 *lval
= lval_memory
;
1300 if (regnum
== SP_REGNUM
)
1302 if (raw_buffer
) /* SP register treated specially */
1303 store_address (raw_buffer
, REGISTER_RAW_SIZE (regnum
),
1304 frame
->saved_regs
[regnum
]);
1308 if (addrp
) /* any other register */
1309 *addrp
= frame
->saved_regs
[regnum
];
1311 read_memory (frame
->saved_regs
[regnum
], raw_buffer
,
1312 REGISTER_RAW_SIZE (regnum
));
1318 /* If we get thru the loop to this point, it means the register was
1319 not saved in any frame. Return the actual live-register value. */
1321 if (lval
) /* found it in a live register */
1322 *lval
= lval_register
;
1324 *addrp
= REGISTER_BYTE (regnum
);
1326 read_register_gen (regnum
, raw_buffer
);
1330 _initialize_blockframe (void)
1332 obstack_init (&frame_cache_obstack
);