* externalize a function
[binutils-gdb.git] / gdb / blockframe.c
blob94ccb298d5d57039df388c9d808b720ecab14d8c
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. */
23 #include "defs.h"
24 #include "symtab.h"
25 #include "bfd.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "frame.h"
29 #include "gdbcore.h"
30 #include "value.h" /* for read_register */
31 #include "target.h" /* for target_has_stack */
32 #include "inferior.h" /* for read_pc */
33 #include "annotate.h"
34 #include "regcache.h"
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. */
44 int
45 file_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe)
47 return ((chain) != 0
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. */
55 int
56 func_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe)
58 return ((chain) != 0
59 && !inside_main_func ((thisframe)->pc)
60 && !inside_entry_func ((thisframe)->pc));
63 /* A very simple method of determining a valid frame */
65 int
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
75 of stack.
77 A PC of zero is always considered to be the bottom of the stack. */
79 int
80 inside_entry_file (CORE_ADDR addr)
82 if (addr == 0)
83 return 1;
84 if (symfile_objfile == 0)
85 return 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))
92 return 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)
109 if (pc == 0)
110 return 1;
111 if (symfile_objfile == 0)
112 return 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)
147 if (pc == 0)
148 return 1;
149 if (symfile_objfile == 0)
150 return 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))
157 return 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;
173 void *
174 frame_obstack_alloc (unsigned long size)
176 return obstack_alloc (&frame_cache_obstack, size);
179 void
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. */
190 struct frame_info *
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 ());
197 else
198 error ("No stack.");
200 return current_frame;
203 void
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. */
212 struct frame_info *
213 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
215 struct frame_info *fi;
216 char *name;
218 fi = (struct frame_info *)
219 obstack_alloc (&frame_cache_obstack,
220 sizeof (struct frame_info));
222 /* Arbitrary frame */
223 fi->saved_regs = NULL;
224 fi->next = NULL;
225 fi->prev = NULL;
226 fi->frame = addr;
227 fi->pc = pc;
228 find_pc_partial_function (pc, &name, (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
229 fi->signal_handler_caller = IN_SIGTRAMP (fi->pc, name);
231 #ifdef INIT_EXTRA_FRAME_INFO
232 INIT_EXTRA_FRAME_INFO (0, fi);
233 #endif
235 return fi;
238 /* Return the frame that FRAME calls (NULL if FRAME is the innermost
239 frame). */
241 struct frame_info *
242 get_next_frame (struct frame_info *frame)
244 return frame->next;
247 /* Flush the entire frame cache. */
249 void
250 flush_cached_frames (void)
252 /* Since we can't really be sure what the first object allocated was */
253 obstack_free (&frame_cache_obstack, 0);
254 obstack_init (&frame_cache_obstack);
256 current_frame = NULL; /* Invalidate cache */
257 select_frame (NULL, -1);
258 annotate_frames_invalid ();
261 /* Flush the frame cache, and start a new one if necessary. */
263 void
264 reinit_frame_cache (void)
266 flush_cached_frames ();
268 /* FIXME: The inferior_pid test is wrong if there is a corefile. */
269 if (inferior_pid != 0)
271 select_frame (get_current_frame (), 0);
275 /* Return nonzero if the function for this frame lacks a prologue. Many
276 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
277 function. */
280 frameless_look_for_prologue (struct frame_info *frame)
282 CORE_ADDR func_start, after_prologue;
284 func_start = get_pc_function_start (frame->pc);
285 if (func_start)
287 func_start += FUNCTION_START_OFFSET;
288 /* This is faster, since only care whether there *is* a
289 prologue, not how long it is. */
290 return PROLOGUE_FRAMELESS_P (func_start);
292 else if (frame->pc == 0)
293 /* A frame with a zero PC is usually created by dereferencing a
294 NULL function pointer, normally causing an immediate core dump
295 of the inferior. Mark function as frameless, as the inferior
296 has no chance of setting up a stack frame. */
297 return 1;
298 else
299 /* If we can't find the start of the function, we don't really
300 know whether the function is frameless, but we should be able
301 to get a reasonable (i.e. best we can do under the
302 circumstances) backtrace by saying that it isn't. */
303 return 0;
306 /* Default a few macros that people seldom redefine. */
308 #if !defined (INIT_FRAME_PC)
309 #define INIT_FRAME_PC(fromleaf, prev) \
310 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
311 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
312 #endif
314 #ifndef FRAME_CHAIN_COMBINE
315 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
316 #endif
318 /* Return a structure containing various interesting information
319 about the frame that called NEXT_FRAME. Returns NULL
320 if there is no such frame. */
322 struct frame_info *
323 get_prev_frame (struct frame_info *next_frame)
325 CORE_ADDR address = 0;
326 struct frame_info *prev;
327 int fromleaf = 0;
328 char *name;
330 /* If the requested entry is in the cache, return it.
331 Otherwise, figure out what the address should be for the entry
332 we're about to add to the cache. */
334 if (!next_frame)
336 #if 0
337 /* This screws value_of_variable, which just wants a nice clean
338 NULL return from block_innermost_frame if there are no frames.
339 I don't think I've ever seen this message happen otherwise.
340 And returning NULL here is a perfectly legitimate thing to do. */
341 if (!current_frame)
343 error ("You haven't set up a process's stack to examine.");
345 #endif
347 return current_frame;
350 /* If we have the prev one, return it */
351 if (next_frame->prev)
352 return next_frame->prev;
354 /* On some machines it is possible to call a function without
355 setting up a stack frame for it. On these machines, we
356 define this macro to take two args; a frameinfo pointer
357 identifying a frame and a variable to set or clear if it is
358 or isn't leafless. */
360 /* Still don't want to worry about this except on the innermost
361 frame. This macro will set FROMLEAF if NEXT_FRAME is a
362 frameless function invocation. */
363 if (!(next_frame->next))
365 fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame);
366 if (fromleaf)
367 address = FRAME_FP (next_frame);
370 if (!fromleaf)
372 /* Two macros defined in tm.h specify the machine-dependent
373 actions to be performed here.
374 First, get the frame's chain-pointer.
375 If that is zero, the frame is the outermost frame or a leaf
376 called by the outermost frame. This means that if start
377 calls main without a frame, we'll return 0 (which is fine
378 anyway).
380 Nope; there's a problem. This also returns when the current
381 routine is a leaf of main. This is unacceptable. We move
382 this to after the ffi test; I'd rather have backtraces from
383 start go curfluy than have an abort called from main not show
384 main. */
385 address = FRAME_CHAIN (next_frame);
386 if (!FRAME_CHAIN_VALID (address, next_frame))
387 return 0;
388 address = FRAME_CHAIN_COMBINE (address, next_frame);
390 if (address == 0)
391 return 0;
393 prev = (struct frame_info *)
394 obstack_alloc (&frame_cache_obstack,
395 sizeof (struct frame_info));
397 /* Zero all fields by default. */
398 memset (prev, 0, sizeof (struct frame_info));
400 if (next_frame)
401 next_frame->prev = prev;
402 prev->next = next_frame;
403 prev->frame = address;
405 /* This change should not be needed, FIXME! We should
406 determine whether any targets *need* INIT_FRAME_PC to happen
407 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
408 express what goes on here.
410 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
411 (where the PC is already set up) and here (where it isn't).
412 INIT_FRAME_PC is only called from here, always after
413 INIT_EXTRA_FRAME_INFO.
415 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
416 value (which hasn't been set yet). Some other machines appear to
417 require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo.
419 We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
420 an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92.
422 Assuming that some machines need INIT_FRAME_PC after
423 INIT_EXTRA_FRAME_INFO, one possible scheme:
425 SETUP_INNERMOST_FRAME()
426 Default version is just create_new_frame (read_fp ()),
427 read_pc ()). Machines with extra frame info would do that (or the
428 local equivalent) and then set the extra fields.
429 SETUP_ARBITRARY_FRAME(argc, argv)
430 Only change here is that create_new_frame would no longer init extra
431 frame info; SETUP_ARBITRARY_FRAME would have to do that.
432 INIT_PREV_FRAME(fromleaf, prev)
433 Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC. This should
434 also return a flag saying whether to keep the new frame, or
435 whether to discard it, because on some machines (e.g. mips) it
436 is really awkward to have FRAME_CHAIN_VALID called *before*
437 INIT_EXTRA_FRAME_INFO (there is no good way to get information
438 deduced in FRAME_CHAIN_VALID into the extra fields of the new frame).
439 std_frame_pc(fromleaf, prev)
440 This is the default setting for INIT_PREV_FRAME. It just does what
441 the default INIT_FRAME_PC does. Some machines will call it from
442 INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
443 Some machines won't use it.
444 kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */
446 #ifdef INIT_FRAME_PC_FIRST
447 INIT_FRAME_PC_FIRST (fromleaf, prev);
448 #endif
450 #ifdef INIT_EXTRA_FRAME_INFO
451 INIT_EXTRA_FRAME_INFO (fromleaf, prev);
452 #endif
454 /* This entry is in the frame queue now, which is good since
455 FRAME_SAVED_PC may use that queue to figure out its value
456 (see tm-sparc.h). We want the pc saved in the inferior frame. */
457 INIT_FRAME_PC (fromleaf, prev);
459 /* If ->frame and ->pc are unchanged, we are in the process of getting
460 ourselves into an infinite backtrace. Some architectures check this
461 in FRAME_CHAIN or thereabouts, but it seems like there is no reason
462 this can't be an architecture-independent check. */
463 if (next_frame != NULL)
465 if (prev->frame == next_frame->frame
466 && prev->pc == next_frame->pc)
468 next_frame->prev = NULL;
469 obstack_free (&frame_cache_obstack, prev);
470 return NULL;
474 find_pc_partial_function (prev->pc, &name,
475 (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
476 if (IN_SIGTRAMP (prev->pc, name))
477 prev->signal_handler_caller = 1;
479 return prev;
482 CORE_ADDR
483 get_frame_pc (struct frame_info *frame)
485 return frame->pc;
489 #ifdef FRAME_FIND_SAVED_REGS
490 /* XXX - deprecated. This is a compatibility function for targets
491 that do not yet implement FRAME_INIT_SAVED_REGS. */
492 /* Find the addresses in which registers are saved in FRAME. */
494 void
495 get_frame_saved_regs (struct frame_info *frame,
496 struct frame_saved_regs *saved_regs_addr)
498 if (frame->saved_regs == NULL)
500 frame->saved_regs = (CORE_ADDR *)
501 frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS);
503 if (saved_regs_addr == NULL)
505 struct frame_saved_regs saved_regs;
506 FRAME_FIND_SAVED_REGS (frame, saved_regs);
507 memcpy (frame->saved_regs, &saved_regs, SIZEOF_FRAME_SAVED_REGS);
509 else
511 FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr);
512 memcpy (frame->saved_regs, saved_regs_addr, SIZEOF_FRAME_SAVED_REGS);
515 #endif
517 /* Return the innermost lexical block in execution
518 in a specified stack frame. The frame address is assumed valid. */
520 struct block *
521 get_frame_block (struct frame_info *frame)
523 CORE_ADDR pc;
525 pc = frame->pc;
526 if (frame->next != 0 && frame->next->signal_handler_caller == 0)
527 /* We are not in the innermost frame and we were not interrupted
528 by a signal. We need to subtract one to get the correct block,
529 in case the call instruction was the last instruction of the block.
530 If there are any machines on which the saved pc does not point to
531 after the call insn, we probably want to make frame->pc point after
532 the call insn anyway. */
533 --pc;
534 return block_for_pc (pc);
537 struct block *
538 get_current_block (void)
540 return block_for_pc (read_pc ());
543 CORE_ADDR
544 get_pc_function_start (CORE_ADDR pc)
546 register struct block *bl;
547 register struct symbol *symbol;
548 register struct minimal_symbol *msymbol;
549 CORE_ADDR fstart;
551 if ((bl = block_for_pc (pc)) != NULL &&
552 (symbol = block_function (bl)) != NULL)
554 bl = SYMBOL_BLOCK_VALUE (symbol);
555 fstart = BLOCK_START (bl);
557 else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
559 fstart = SYMBOL_VALUE_ADDRESS (msymbol);
561 else
563 fstart = 0;
565 return (fstart);
568 /* Return the symbol for the function executing in frame FRAME. */
570 struct symbol *
571 get_frame_function (struct frame_info *frame)
573 register struct block *bl = get_frame_block (frame);
574 if (bl == 0)
575 return 0;
576 return block_function (bl);
580 /* Return the blockvector immediately containing the innermost lexical block
581 containing the specified pc value and section, or 0 if there is none.
582 PINDEX is a pointer to the index value of the block. If PINDEX
583 is NULL, we don't pass this information back to the caller. */
585 struct blockvector *
586 blockvector_for_pc_sect (register CORE_ADDR pc, struct sec *section,
587 int *pindex, struct symtab *symtab)
589 register struct block *b;
590 register int bot, top, half;
591 struct blockvector *bl;
593 if (symtab == 0) /* if no symtab specified by caller */
595 /* First search all symtabs for one whose file contains our pc */
596 if ((symtab = find_pc_sect_symtab (pc, section)) == 0)
597 return 0;
600 bl = BLOCKVECTOR (symtab);
601 b = BLOCKVECTOR_BLOCK (bl, 0);
603 /* Then search that symtab for the smallest block that wins. */
604 /* Use binary search to find the last block that starts before PC. */
606 bot = 0;
607 top = BLOCKVECTOR_NBLOCKS (bl);
609 while (top - bot > 1)
611 half = (top - bot + 1) >> 1;
612 b = BLOCKVECTOR_BLOCK (bl, bot + half);
613 if (BLOCK_START (b) <= pc)
614 bot += half;
615 else
616 top = bot + half;
619 /* Now search backward for a block that ends after PC. */
621 while (bot >= 0)
623 b = BLOCKVECTOR_BLOCK (bl, bot);
624 if (BLOCK_END (b) > pc)
626 if (pindex)
627 *pindex = bot;
628 return bl;
630 bot--;
632 return 0;
635 /* Return the blockvector immediately containing the innermost lexical block
636 containing the specified pc value, or 0 if there is none.
637 Backward compatibility, no section. */
639 struct blockvector *
640 blockvector_for_pc (register CORE_ADDR pc, int *pindex)
642 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
643 pindex, NULL);
646 /* Return the innermost lexical block containing the specified pc value
647 in the specified section, or 0 if there is none. */
649 struct block *
650 block_for_pc_sect (register CORE_ADDR pc, struct sec *section)
652 register struct blockvector *bl;
653 int index;
655 bl = blockvector_for_pc_sect (pc, section, &index, NULL);
656 if (bl)
657 return BLOCKVECTOR_BLOCK (bl, index);
658 return 0;
661 /* Return the innermost lexical block containing the specified pc value,
662 or 0 if there is none. Backward compatibility, no section. */
664 struct block *
665 block_for_pc (register CORE_ADDR pc)
667 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
670 /* Return the function containing pc value PC in section SECTION.
671 Returns 0 if function is not known. */
673 struct symbol *
674 find_pc_sect_function (CORE_ADDR pc, struct sec *section)
676 register struct block *b = block_for_pc_sect (pc, section);
677 if (b == 0)
678 return 0;
679 return block_function (b);
682 /* Return the function containing pc value PC.
683 Returns 0 if function is not known. Backward compatibility, no section */
685 struct symbol *
686 find_pc_function (CORE_ADDR pc)
688 return find_pc_sect_function (pc, find_pc_mapped_section (pc));
691 /* These variables are used to cache the most recent result
692 * of find_pc_partial_function. */
694 static CORE_ADDR cache_pc_function_low = 0;
695 static CORE_ADDR cache_pc_function_high = 0;
696 static char *cache_pc_function_name = 0;
697 static struct sec *cache_pc_function_section = NULL;
699 /* Clear cache, e.g. when symbol table is discarded. */
701 void
702 clear_pc_function_cache (void)
704 cache_pc_function_low = 0;
705 cache_pc_function_high = 0;
706 cache_pc_function_name = (char *) 0;
707 cache_pc_function_section = NULL;
710 /* Finds the "function" (text symbol) that is smaller than PC but
711 greatest of all of the potential text symbols in SECTION. Sets
712 *NAME and/or *ADDRESS conditionally if that pointer is non-null.
713 If ENDADDR is non-null, then set *ENDADDR to be the end of the
714 function (exclusive), but passing ENDADDR as non-null means that
715 the function might cause symbols to be read. This function either
716 succeeds or fails (not halfway succeeds). If it succeeds, it sets
717 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
718 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
719 returns 0. */
722 find_pc_sect_partial_function (CORE_ADDR pc, asection *section, char **name,
723 CORE_ADDR *address, CORE_ADDR *endaddr)
725 struct partial_symtab *pst;
726 struct symbol *f;
727 struct minimal_symbol *msymbol;
728 struct partial_symbol *psb;
729 struct obj_section *osect;
730 int i;
731 CORE_ADDR mapped_pc;
733 mapped_pc = overlay_mapped_address (pc, section);
735 if (mapped_pc >= cache_pc_function_low &&
736 mapped_pc < cache_pc_function_high &&
737 section == cache_pc_function_section)
738 goto return_cached_value;
740 /* If sigtramp is in the u area, it counts as a function (especially
741 important for step_1). */
742 #if defined SIGTRAMP_START
743 if (IN_SIGTRAMP (mapped_pc, (char *) NULL))
745 cache_pc_function_low = SIGTRAMP_START (mapped_pc);
746 cache_pc_function_high = SIGTRAMP_END (mapped_pc);
747 cache_pc_function_name = "<sigtramp>";
748 cache_pc_function_section = section;
749 goto return_cached_value;
751 #endif
753 msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
754 pst = find_pc_sect_psymtab (mapped_pc, section);
755 if (pst)
757 /* Need to read the symbols to get a good value for the end address. */
758 if (endaddr != NULL && !pst->readin)
760 /* Need to get the terminal in case symbol-reading produces
761 output. */
762 target_terminal_ours_for_output ();
763 PSYMTAB_TO_SYMTAB (pst);
766 if (pst->readin)
768 /* Checking whether the msymbol has a larger value is for the
769 "pathological" case mentioned in print_frame_info. */
770 f = find_pc_sect_function (mapped_pc, section);
771 if (f != NULL
772 && (msymbol == NULL
773 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
774 >= SYMBOL_VALUE_ADDRESS (msymbol))))
776 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
777 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
778 cache_pc_function_name = SYMBOL_NAME (f);
779 cache_pc_function_section = section;
780 goto return_cached_value;
783 else
785 /* Now that static symbols go in the minimal symbol table, perhaps
786 we could just ignore the partial symbols. But at least for now
787 we use the partial or minimal symbol, whichever is larger. */
788 psb = find_pc_sect_psymbol (pst, mapped_pc, section);
790 if (psb
791 && (msymbol == NULL ||
792 (SYMBOL_VALUE_ADDRESS (psb)
793 >= SYMBOL_VALUE_ADDRESS (msymbol))))
795 /* This case isn't being cached currently. */
796 if (address)
797 *address = SYMBOL_VALUE_ADDRESS (psb);
798 if (name)
799 *name = SYMBOL_NAME (psb);
800 /* endaddr non-NULL can't happen here. */
801 return 1;
806 /* Not in the normal symbol tables, see if the pc is in a known section.
807 If it's not, then give up. This ensures that anything beyond the end
808 of the text seg doesn't appear to be part of the last function in the
809 text segment. */
811 osect = find_pc_sect_section (mapped_pc, section);
813 if (!osect)
814 msymbol = NULL;
816 /* Must be in the minimal symbol table. */
817 if (msymbol == NULL)
819 /* No available symbol. */
820 if (name != NULL)
821 *name = 0;
822 if (address != NULL)
823 *address = 0;
824 if (endaddr != NULL)
825 *endaddr = 0;
826 return 0;
829 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
830 cache_pc_function_name = SYMBOL_NAME (msymbol);
831 cache_pc_function_section = section;
833 /* Use the lesser of the next minimal symbol in the same section, or
834 the end of the section, as the end of the function. */
836 /* Step over other symbols at this same address, and symbols in
837 other sections, to find the next symbol in this section with
838 a different address. */
840 for (i = 1; SYMBOL_NAME (msymbol + i) != NULL; i++)
842 if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol)
843 && SYMBOL_BFD_SECTION (msymbol + i) == SYMBOL_BFD_SECTION (msymbol))
844 break;
847 if (SYMBOL_NAME (msymbol + i) != NULL
848 && SYMBOL_VALUE_ADDRESS (msymbol + i) < osect->endaddr)
849 cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
850 else
851 /* We got the start address from the last msymbol in the objfile.
852 So the end address is the end of the section. */
853 cache_pc_function_high = osect->endaddr;
855 return_cached_value:
857 if (address)
859 if (pc_in_unmapped_range (pc, section))
860 *address = overlay_unmapped_address (cache_pc_function_low, section);
861 else
862 *address = cache_pc_function_low;
865 if (name)
866 *name = cache_pc_function_name;
868 if (endaddr)
870 if (pc_in_unmapped_range (pc, section))
872 /* Because the high address is actually beyond the end of
873 the function (and therefore possibly beyond the end of
874 the overlay), we must actually convert (high - 1)
875 and then add one to that. */
877 *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
878 section);
880 else
881 *endaddr = cache_pc_function_high;
884 return 1;
887 /* Backward compatibility, no section argument */
890 find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
891 CORE_ADDR *endaddr)
893 asection *section;
895 section = find_pc_overlay (pc);
896 return find_pc_sect_partial_function (pc, section, name, address, endaddr);
899 /* Return the innermost stack frame executing inside of BLOCK,
900 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
902 struct frame_info *
903 block_innermost_frame (struct block *block)
905 struct frame_info *frame;
906 register CORE_ADDR start;
907 register CORE_ADDR end;
909 if (block == NULL)
910 return NULL;
912 start = BLOCK_START (block);
913 end = BLOCK_END (block);
915 frame = NULL;
916 while (1)
918 frame = get_prev_frame (frame);
919 if (frame == NULL)
920 return NULL;
921 if (frame->pc >= start && frame->pc < end)
922 return frame;
926 /* Return the full FRAME which corresponds to the given CORE_ADDR
927 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
929 struct frame_info *
930 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
932 struct frame_info *frame = NULL;
934 if (frame_addr == (CORE_ADDR) 0)
935 return NULL;
937 while (1)
939 frame = get_prev_frame (frame);
940 if (frame == NULL)
941 return NULL;
942 if (FRAME_FP (frame) == frame_addr)
943 return frame;
947 #ifdef SIGCONTEXT_PC_OFFSET
948 /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp. */
950 CORE_ADDR
951 sigtramp_saved_pc (struct frame_info *frame)
953 CORE_ADDR sigcontext_addr;
954 char *buf;
955 int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT;
956 int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT;
958 buf = alloca (ptrbytes);
959 /* Get sigcontext address, it is the third parameter on the stack. */
960 if (frame->next)
961 sigcontext_addr = read_memory_integer (FRAME_ARGS_ADDRESS (frame->next)
962 + FRAME_ARGS_SKIP
963 + sigcontext_offs,
964 ptrbytes);
965 else
966 sigcontext_addr = read_memory_integer (read_register (SP_REGNUM)
967 + sigcontext_offs,
968 ptrbytes);
970 /* Don't cause a memory_error when accessing sigcontext in case the stack
971 layout has changed or the stack is corrupt. */
972 target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes);
973 return extract_unsigned_integer (buf, ptrbytes);
975 #endif /* SIGCONTEXT_PC_OFFSET */
978 /* Are we in a call dummy? The code below which allows DECR_PC_AFTER_BREAK
979 below is for infrun.c, which may give the macro a pc without that
980 subtracted out. */
982 extern CORE_ADDR text_end;
985 pc_in_call_dummy_before_text_end (CORE_ADDR pc, CORE_ADDR sp,
986 CORE_ADDR frame_address)
988 return ((pc) >= text_end - CALL_DUMMY_LENGTH
989 && (pc) <= text_end + DECR_PC_AFTER_BREAK);
993 pc_in_call_dummy_after_text_end (CORE_ADDR pc, CORE_ADDR sp,
994 CORE_ADDR frame_address)
996 return ((pc) >= text_end
997 && (pc) <= text_end + CALL_DUMMY_LENGTH + DECR_PC_AFTER_BREAK);
1000 /* Is the PC in a call dummy? SP and FRAME_ADDRESS are the bottom and
1001 top of the stack frame which we are checking, where "bottom" and
1002 "top" refer to some section of memory which contains the code for
1003 the call dummy. Calls to this macro assume that the contents of
1004 SP_REGNUM and FP_REGNUM (or the saved values thereof), respectively,
1005 are the things to pass.
1007 This won't work on the 29k, where SP_REGNUM and FP_REGNUM don't
1008 have that meaning, but the 29k doesn't use ON_STACK. This could be
1009 fixed by generalizing this scheme, perhaps by passing in a frame
1010 and adding a few fields, at least on machines which need them for
1011 PC_IN_CALL_DUMMY.
1013 Something simpler, like checking for the stack segment, doesn't work,
1014 since various programs (threads implementations, gcc nested function
1015 stubs, etc) may either allocate stack frames in another segment, or
1016 allocate other kinds of code on the stack. */
1019 pc_in_call_dummy_on_stack (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR frame_address)
1021 return (INNER_THAN ((sp), (pc))
1022 && (frame_address != 0)
1023 && INNER_THAN ((pc), (frame_address)));
1027 pc_in_call_dummy_at_entry_point (CORE_ADDR pc, CORE_ADDR sp,
1028 CORE_ADDR frame_address)
1030 return ((pc) >= CALL_DUMMY_ADDRESS ()
1031 && (pc) <= (CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK));
1036 * GENERIC DUMMY FRAMES
1038 * The following code serves to maintain the dummy stack frames for
1039 * inferior function calls (ie. when gdb calls into the inferior via
1040 * call_function_by_hand). This code saves the machine state before
1041 * the call in host memory, so we must maintain an independent stack
1042 * and keep it consistant etc. I am attempting to make this code
1043 * generic enough to be used by many targets.
1045 * The cheapest and most generic way to do CALL_DUMMY on a new target
1046 * is probably to define CALL_DUMMY to be empty, CALL_DUMMY_LENGTH to
1047 * zero, and CALL_DUMMY_LOCATION to AT_ENTRY. Then you must remember
1048 * to define PUSH_RETURN_ADDRESS, because no call instruction will be
1049 * being executed by the target. Also FRAME_CHAIN_VALID as
1050 * generic_{file,func}_frame_chain_valid and FIX_CALL_DUMMY as
1051 * generic_fix_call_dummy. */
1053 /* Dummy frame. This saves the processor state just prior to setting
1054 up the inferior function call. Older targets save the registers
1055 on the target stack (but that really slows down function calls). */
1057 struct dummy_frame
1059 struct dummy_frame *next;
1061 CORE_ADDR pc;
1062 CORE_ADDR fp;
1063 CORE_ADDR sp;
1064 CORE_ADDR top;
1065 char *registers;
1068 static struct dummy_frame *dummy_frame_stack = NULL;
1070 /* Function: find_dummy_frame(pc, fp, sp)
1071 Search the stack of dummy frames for one matching the given PC, FP and SP.
1072 This is the work-horse for pc_in_call_dummy and read_register_dummy */
1074 char *
1075 generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
1077 struct dummy_frame *dummyframe;
1079 if (pc != entry_point_address ())
1080 return 0;
1082 for (dummyframe = dummy_frame_stack; dummyframe != NULL;
1083 dummyframe = dummyframe->next)
1084 if (fp == dummyframe->fp
1085 || fp == dummyframe->sp
1086 || fp == dummyframe->top)
1087 /* The frame in question lies between the saved fp and sp, inclusive */
1088 return dummyframe->registers;
1090 return 0;
1093 /* Function: pc_in_call_dummy (pc, fp)
1094 Return true if this is a dummy frame created by gdb for an inferior call */
1097 generic_pc_in_call_dummy (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR fp)
1099 /* if find_dummy_frame succeeds, then PC is in a call dummy */
1100 /* Note: SP and not FP is passed on. */
1101 return (generic_find_dummy_frame (pc, sp) != 0);
1104 /* Function: read_register_dummy
1105 Find a saved register from before GDB calls a function in the inferior */
1107 CORE_ADDR
1108 generic_read_register_dummy (CORE_ADDR pc, CORE_ADDR fp, int regno)
1110 char *dummy_regs = generic_find_dummy_frame (pc, fp);
1112 if (dummy_regs)
1113 return extract_address (&dummy_regs[REGISTER_BYTE (regno)],
1114 REGISTER_RAW_SIZE (regno));
1115 else
1116 return 0;
1119 /* Save all the registers on the dummy frame stack. Most ports save the
1120 registers on the target stack. This results in lots of unnecessary memory
1121 references, which are slow when debugging via a serial line. Instead, we
1122 save all the registers internally, and never write them to the stack. The
1123 registers get restored when the called function returns to the entry point,
1124 where a breakpoint is laying in wait. */
1126 void
1127 generic_push_dummy_frame (void)
1129 struct dummy_frame *dummy_frame;
1130 CORE_ADDR fp = (get_current_frame ())->frame;
1132 /* check to see if there are stale dummy frames,
1133 perhaps left over from when a longjump took us out of a
1134 function that was called by the debugger */
1136 dummy_frame = dummy_frame_stack;
1137 while (dummy_frame)
1138 if (INNER_THAN (dummy_frame->fp, fp)) /* stale -- destroy! */
1140 dummy_frame_stack = dummy_frame->next;
1141 xfree (dummy_frame->registers);
1142 xfree (dummy_frame);
1143 dummy_frame = dummy_frame_stack;
1145 else
1146 dummy_frame = dummy_frame->next;
1148 dummy_frame = xmalloc (sizeof (struct dummy_frame));
1149 dummy_frame->registers = xmalloc (REGISTER_BYTES);
1151 dummy_frame->pc = read_pc ();
1152 dummy_frame->sp = read_sp ();
1153 dummy_frame->top = dummy_frame->sp;
1154 dummy_frame->fp = fp;
1155 read_register_bytes (0, dummy_frame->registers, REGISTER_BYTES);
1156 dummy_frame->next = dummy_frame_stack;
1157 dummy_frame_stack = dummy_frame;
1160 void
1161 generic_save_dummy_frame_tos (CORE_ADDR sp)
1163 dummy_frame_stack->top = sp;
1166 /* Restore the machine state from either the saved dummy stack or a
1167 real stack frame. */
1169 void
1170 generic_pop_current_frame (void (*popper) (struct frame_info * frame))
1172 struct frame_info *frame = get_current_frame ();
1174 if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
1175 generic_pop_dummy_frame ();
1176 else
1177 (*popper) (frame);
1180 /* Function: pop_dummy_frame
1181 Restore the machine state from a saved dummy stack frame. */
1183 void
1184 generic_pop_dummy_frame (void)
1186 struct dummy_frame *dummy_frame = dummy_frame_stack;
1188 /* FIXME: what if the first frame isn't the right one, eg..
1189 because one call-by-hand function has done a longjmp into another one? */
1191 if (!dummy_frame)
1192 error ("Can't pop dummy frame!");
1193 dummy_frame_stack = dummy_frame->next;
1194 write_register_bytes (0, dummy_frame->registers, REGISTER_BYTES);
1195 flush_cached_frames ();
1197 xfree (dummy_frame->registers);
1198 xfree (dummy_frame);
1201 /* Function: frame_chain_valid
1202 Returns true for a user frame or a call_function_by_hand dummy frame,
1203 and false for the CRT0 start-up frame. Purpose is to terminate backtrace */
1206 generic_file_frame_chain_valid (CORE_ADDR fp, struct frame_info *fi)
1208 if (PC_IN_CALL_DUMMY (FRAME_SAVED_PC (fi), fp, fp))
1209 return 1; /* don't prune CALL_DUMMY frames */
1210 else /* fall back to default algorithm (see frame.h) */
1211 return (fp != 0
1212 && (INNER_THAN (fi->frame, fp) || fi->frame == fp)
1213 && !inside_entry_file (FRAME_SAVED_PC (fi)));
1217 generic_func_frame_chain_valid (CORE_ADDR fp, struct frame_info *fi)
1219 if (PC_IN_CALL_DUMMY ((fi)->pc, fp, fp))
1220 return 1; /* don't prune CALL_DUMMY frames */
1221 else /* fall back to default algorithm (see frame.h) */
1222 return (fp != 0
1223 && (INNER_THAN (fi->frame, fp) || fi->frame == fp)
1224 && !inside_main_func ((fi)->pc)
1225 && !inside_entry_func ((fi)->pc));
1228 /* Function: fix_call_dummy
1229 Stub function. Generic dumy frames typically do not need to fix
1230 the frame being created */
1232 void
1233 generic_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
1234 struct value **args, struct type *type, int gcc_p)
1236 return;
1239 /* Function: get_saved_register
1240 Find register number REGNUM relative to FRAME and put its (raw,
1241 target format) contents in *RAW_BUFFER.
1243 Set *OPTIMIZED if the variable was optimized out (and thus can't be
1244 fetched). Note that this is never set to anything other than zero
1245 in this implementation.
1247 Set *LVAL to lval_memory, lval_register, or not_lval, depending on
1248 whether the value was fetched from memory, from a register, or in a
1249 strange and non-modifiable way (e.g. a frame pointer which was
1250 calculated rather than fetched). We will use not_lval for values
1251 fetched from generic dummy frames.
1253 Set *ADDRP to the address, either in memory or as a REGISTER_BYTE
1254 offset into the registers array. If the value is stored in a dummy
1255 frame, set *ADDRP to zero.
1257 To use this implementation, define a function called
1258 "get_saved_register" in your target code, which simply passes all
1259 of its arguments to this function.
1261 The argument RAW_BUFFER must point to aligned memory. */
1263 void
1264 generic_get_saved_register (char *raw_buffer, int *optimized, CORE_ADDR *addrp,
1265 struct frame_info *frame, int regnum,
1266 enum lval_type *lval)
1268 if (!target_has_registers)
1269 error ("No registers.");
1271 /* Normal systems don't optimize out things with register numbers. */
1272 if (optimized != NULL)
1273 *optimized = 0;
1275 if (addrp) /* default assumption: not found in memory */
1276 *addrp = 0;
1278 /* Note: since the current frame's registers could only have been
1279 saved by frames INTERIOR TO the current frame, we skip examining
1280 the current frame itself: otherwise, we would be getting the
1281 previous frame's registers which were saved by the current frame. */
1283 while (frame && ((frame = frame->next) != NULL))
1285 if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
1287 if (lval) /* found it in a CALL_DUMMY frame */
1288 *lval = not_lval;
1289 if (raw_buffer)
1290 memcpy (raw_buffer,
1291 generic_find_dummy_frame (frame->pc, frame->frame) +
1292 REGISTER_BYTE (regnum),
1293 REGISTER_RAW_SIZE (regnum));
1294 return;
1297 FRAME_INIT_SAVED_REGS (frame);
1298 if (frame->saved_regs != NULL
1299 && frame->saved_regs[regnum] != 0)
1301 if (lval) /* found it saved on the stack */
1302 *lval = lval_memory;
1303 if (regnum == SP_REGNUM)
1305 if (raw_buffer) /* SP register treated specially */
1306 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
1307 frame->saved_regs[regnum]);
1309 else
1311 if (addrp) /* any other register */
1312 *addrp = frame->saved_regs[regnum];
1313 if (raw_buffer)
1314 read_memory (frame->saved_regs[regnum], raw_buffer,
1315 REGISTER_RAW_SIZE (regnum));
1317 return;
1321 /* If we get thru the loop to this point, it means the register was
1322 not saved in any frame. Return the actual live-register value. */
1324 if (lval) /* found it in a live register */
1325 *lval = lval_register;
1326 if (addrp)
1327 *addrp = REGISTER_BYTE (regnum);
1328 if (raw_buffer)
1329 read_register_gen (regnum, raw_buffer);
1332 void
1333 _initialize_blockframe (void)
1335 obstack_init (&frame_cache_obstack);