1 /* Get info from stack frames;
2 convert between frames, blocks, functions and pc values.
3 Copyright 1986, 1987, 1988, 1989, 1991 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include "value.h" /* for read_register */
29 #include "target.h" /* for target_has_stack */
30 #include "inferior.h" /* for read_pc */
32 /* Is ADDR inside the startup file? Note that if your machine
33 has a way to detect the bottom of the stack, there is no need
34 to call this function from FRAME_CHAIN_VALID; the reason for
35 doing so is that some machines have no way of detecting bottom
38 A PC of zero is always considered to be the bottom of the stack. */
41 inside_entry_file (addr)
46 if (symfile_objfile == 0)
48 return (addr >= symfile_objfile -> ei.entry_file_lowpc &&
49 addr < symfile_objfile -> ei.entry_file_highpc);
52 /* Test a specified PC value to see if it is in the range of addresses
53 that correspond to the main() function. See comments above for why
54 we might want to do this.
56 Typically called from FRAME_CHAIN_VALID.
58 A PC of zero is always considered to be the bottom of the stack. */
66 if (symfile_objfile == 0)
68 return (symfile_objfile -> ei.main_func_lowpc <= pc &&
69 symfile_objfile -> ei.main_func_highpc > pc);
72 /* Test a specified PC value to see if it is in the range of addresses
73 that correspond to the process entry point function. See comments
74 in objfiles.h for why we might want to do this.
76 Typically called from FRAME_CHAIN_VALID.
78 A PC of zero is always considered to be the bottom of the stack. */
81 inside_entry_func (pc)
86 if (symfile_objfile == 0)
88 return (symfile_objfile -> ei.entry_func_lowpc <= pc &&
89 symfile_objfile -> ei.entry_func_highpc > pc);
92 /* Address of innermost stack frame (contents of FP register) */
94 static FRAME current_frame;
97 * Cache for frame addresses already read by gdb. Valid only while
98 * inferior is stopped. Control variables for the frame cache should
99 * be local to this module.
101 struct obstack frame_cache_obstack;
103 /* Return the innermost (currently executing) stack frame. */
108 /* We assume its address is kept in a general register;
109 param.h says which register. */
111 return current_frame;
115 set_current_frame (frame)
118 current_frame = frame;
122 create_new_frame (addr, pc)
126 struct frame_info *fci; /* Same type as FRAME */
129 fci = (struct frame_info *)
130 obstack_alloc (&frame_cache_obstack,
131 sizeof (struct frame_info));
133 /* Arbitrary frame */
134 fci->next = (struct frame_info *) 0;
135 fci->prev = (struct frame_info *) 0;
138 find_pc_partial_function (pc, &name, (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
139 fci->signal_handler_caller = IN_SIGTRAMP (fci->pc, name);
141 #ifdef INIT_EXTRA_FRAME_INFO
142 INIT_EXTRA_FRAME_INFO (0, fci);
148 /* Return the frame that called FRAME.
149 If FRAME is the original frame (it has no caller), return 0. */
152 get_prev_frame (frame)
155 /* We're allowed to know that FRAME and "struct frame_info *" are
157 return get_prev_frame_info (frame);
160 /* Return the frame that FRAME calls (0 if FRAME is the innermost
164 get_next_frame (frame)
167 /* We're allowed to know that FRAME and "struct frame_info *" are
173 * Flush the entire frame cache.
176 flush_cached_frames ()
178 /* Since we can't really be sure what the first object allocated was */
179 obstack_free (&frame_cache_obstack, 0);
180 obstack_init (&frame_cache_obstack);
182 current_frame = (struct frame_info *) 0; /* Invalidate cache */
185 /* Flush the frame cache, and start a new one if necessary. */
187 reinit_frame_cache ()
189 FRAME fr = current_frame;
190 flush_cached_frames ();
192 set_current_frame ( create_new_frame (read_fp (), read_pc ()));
195 /* Return a structure containing various interesting information
196 about a specified stack frame. */
197 /* How do I justify including this function? Well, the FRAME
198 identifier format has gone through several changes recently, and
199 it's not completely inconceivable that it could happen again. If
200 it does, have this routine around will help */
203 get_frame_info (frame)
209 /* If a machine allows frameless functions, it should define a macro
210 FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct
211 frame_info for the frame, and FRAMELESS should be set to nonzero
212 if it represents a frameless function invocation. */
214 /* Return nonzero if the function for this frame lacks a prologue. Many
215 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
219 frameless_look_for_prologue (frame)
222 CORE_ADDR func_start, after_prologue;
223 func_start = (get_pc_function_start (frame->pc) +
224 FUNCTION_START_OFFSET);
227 after_prologue = func_start;
228 #ifdef SKIP_PROLOGUE_FRAMELESS_P
229 /* This is faster, since only care whether there *is* a prologue,
230 not how long it is. */
231 SKIP_PROLOGUE_FRAMELESS_P (after_prologue);
233 SKIP_PROLOGUE (after_prologue);
235 return after_prologue == func_start;
238 /* If we can't find the start of the function, we don't really
239 know whether the function is frameless, but we should be able
240 to get a reasonable (i.e. best we can do under the
241 circumstances) backtrace by saying that it isn't. */
245 /* Default a few macros that people seldom redefine. */
247 #if !defined (INIT_FRAME_PC)
248 #define INIT_FRAME_PC(fromleaf, prev) \
249 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
250 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
253 #ifndef FRAME_CHAIN_COMBINE
254 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
257 /* Return a structure containing various interesting information
258 about the frame that called NEXT_FRAME. Returns NULL
259 if there is no such frame. */
262 get_prev_frame_info (next_frame)
266 struct frame_info *prev;
270 /* If the requested entry is in the cache, return it.
271 Otherwise, figure out what the address should be for the entry
272 we're about to add to the cache. */
278 error ("You haven't set up a process's stack to examine.");
281 return current_frame;
284 /* If we have the prev one, return it */
285 if (next_frame->prev)
286 return next_frame->prev;
288 /* On some machines it is possible to call a function without
289 setting up a stack frame for it. On these machines, we
290 define this macro to take two args; a frameinfo pointer
291 identifying a frame and a variable to set or clear if it is
292 or isn't leafless. */
293 #ifdef FRAMELESS_FUNCTION_INVOCATION
294 /* Still don't want to worry about this except on the innermost
295 frame. This macro will set FROMLEAF if NEXT_FRAME is a
296 frameless function invocation. */
297 if (!(next_frame->next))
299 FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
301 address = next_frame->frame;
307 /* Two macros defined in tm.h specify the machine-dependent
308 actions to be performed here.
309 First, get the frame's chain-pointer.
310 If that is zero, the frame is the outermost frame or a leaf
311 called by the outermost frame. This means that if start
312 calls main without a frame, we'll return 0 (which is fine
315 Nope; there's a problem. This also returns when the current
316 routine is a leaf of main. This is unacceptable. We move
317 this to after the ffi test; I'd rather have backtraces from
318 start go curfluy than have an abort called from main not show
320 address = FRAME_CHAIN (next_frame);
321 if (!FRAME_CHAIN_VALID (address, next_frame))
323 address = FRAME_CHAIN_COMBINE (address, next_frame);
328 prev = (struct frame_info *)
329 obstack_alloc (&frame_cache_obstack,
330 sizeof (struct frame_info));
333 next_frame->prev = prev;
334 prev->next = next_frame;
335 prev->prev = (struct frame_info *) 0;
336 prev->frame = address;
337 prev->signal_handler_caller = 0;
339 /* This change should not be needed, FIXME! We should
340 determine whether any targets *need* INIT_FRAME_PC to happen
341 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
342 express what goes on here.
344 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
345 (where the PC is already set up) and here (where it isn't).
346 INIT_FRAME_PC is only called from here, always after
347 INIT_EXTRA_FRAME_INFO.
349 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
350 value (which hasn't been set yet). Some other machines appear to
351 require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo.
353 We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
356 To answer the question, yes the sparc needs INIT_FRAME_PC after
357 INIT_EXTRA_FRAME_INFO. Suggested scheme:
359 SETUP_INNERMOST_FRAME()
360 Default version is just create_new_frame (read_fp ()),
361 read_pc ()). Machines with extra frame info would do that (or the
362 local equivalent) and then set the extra fields.
363 SETUP_ARBITRARY_FRAME(argc, argv)
364 Only change here is that create_new_frame would no longer init extra
365 frame info; SETUP_ARBITRARY_FRAME would have to do that.
366 INIT_PREV_FRAME(fromleaf, prev)
367 Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC.
368 std_frame_pc(fromleaf, prev)
369 This is the default setting for INIT_PREV_FRAME. It just does what
370 the default INIT_FRAME_PC does. Some machines will call it from
371 INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
372 Some machines won't use it.
375 #ifdef INIT_FRAME_PC_FIRST
376 INIT_FRAME_PC_FIRST (fromleaf, prev);
379 #ifdef INIT_EXTRA_FRAME_INFO
380 INIT_EXTRA_FRAME_INFO(fromleaf, prev);
383 /* This entry is in the frame queue now, which is good since
384 FRAME_SAVED_PC may use that queue to figure out it's value
385 (see tm-sparc.h). We want the pc saved in the inferior frame. */
386 INIT_FRAME_PC(fromleaf, prev);
388 find_pc_partial_function (prev->pc, &name,
389 (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
390 if (IN_SIGTRAMP (prev->pc, name))
391 prev->signal_handler_caller = 1;
400 struct frame_info *fi;
401 fi = get_frame_info (frame);
405 #if defined (FRAME_FIND_SAVED_REGS)
406 /* Find the addresses in which registers are saved in FRAME. */
409 get_frame_saved_regs (frame_info_addr, saved_regs_addr)
410 struct frame_info *frame_info_addr;
411 struct frame_saved_regs *saved_regs_addr;
413 FRAME_FIND_SAVED_REGS (frame_info_addr, *saved_regs_addr);
417 /* Return the innermost lexical block in execution
418 in a specified stack frame. The frame address is assumed valid. */
421 get_frame_block (frame)
424 struct frame_info *fi;
427 fi = get_frame_info (frame);
431 /* We are not in the innermost frame. We need to subtract one to
432 get the correct block, in case the call instruction was the
433 last instruction of the block. If there are any machines on
434 which the saved pc does not point to after the call insn, we
435 probably want to make fi->pc point after the call insn anyway. */
437 return block_for_pc (pc);
443 return block_for_pc (read_pc ());
447 get_pc_function_start (pc)
450 register struct block *bl;
451 register struct symbol *symbol;
452 register struct minimal_symbol *msymbol;
455 if ((bl = block_for_pc (pc)) != NULL &&
456 (symbol = block_function (bl)) != NULL)
458 bl = SYMBOL_BLOCK_VALUE (symbol);
459 fstart = BLOCK_START (bl);
461 else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
463 fstart = SYMBOL_VALUE_ADDRESS (msymbol);
472 /* Return the symbol for the function executing in frame FRAME. */
475 get_frame_function (frame)
478 register struct block *bl = get_frame_block (frame);
481 return block_function (bl);
484 /* Return the blockvector immediately containing the innermost lexical block
485 containing the specified pc value, or 0 if there is none.
486 PINDEX is a pointer to the index value of the block. If PINDEX
487 is NULL, we don't pass this information back to the caller. */
490 blockvector_for_pc (pc, pindex)
491 register CORE_ADDR pc;
494 register struct block *b;
495 register int bot, top, half;
496 register struct symtab *s;
497 struct blockvector *bl;
499 /* First search all symtabs for one whose file contains our pc */
500 s = find_pc_symtab (pc);
504 bl = BLOCKVECTOR (s);
505 b = BLOCKVECTOR_BLOCK (bl, 0);
507 /* Then search that symtab for the smallest block that wins. */
508 /* Use binary search to find the last block that starts before PC. */
511 top = BLOCKVECTOR_NBLOCKS (bl);
513 while (top - bot > 1)
515 half = (top - bot + 1) >> 1;
516 b = BLOCKVECTOR_BLOCK (bl, bot + half);
517 if (BLOCK_START (b) <= pc)
523 /* Now search backward for a block that ends after PC. */
527 b = BLOCKVECTOR_BLOCK (bl, bot);
528 if (BLOCK_END (b) > pc)
540 /* Return the innermost lexical block containing the specified pc value,
541 or 0 if there is none. */
545 register CORE_ADDR pc;
547 register struct blockvector *bl;
550 bl = blockvector_for_pc (pc, &index);
552 return BLOCKVECTOR_BLOCK (bl, index);
556 /* Return the function containing pc value PC.
557 Returns 0 if function is not known. */
560 find_pc_function (pc)
563 register struct block *b = block_for_pc (pc);
566 return block_function (b);
569 /* These variables are used to cache the most recent result
570 * of find_pc_partial_function. */
572 static CORE_ADDR cache_pc_function_low = 0;
573 static CORE_ADDR cache_pc_function_high = 0;
574 static char *cache_pc_function_name = 0;
576 /* Clear cache, e.g. when symbol table is discarded. */
579 clear_pc_function_cache()
581 cache_pc_function_low = 0;
582 cache_pc_function_high = 0;
583 cache_pc_function_name = (char *)0;
586 /* Finds the "function" (text symbol) that is smaller than PC but
587 greatest of all of the potential text symbols. Sets *NAME and/or
588 *ADDRESS conditionally if that pointer is non-null. If ENDADDR is
589 non-null, then set *ENDADDR to be the end of the function
590 (exclusive), but passing ENDADDR as non-null means that the
591 function might cause symbols to be read. This function either
592 succeeds or fails (not halfway succeeds). If it succeeds, it sets
593 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
594 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero
598 find_pc_partial_function (pc, name, address, endaddr)
604 struct partial_symtab *pst;
606 struct minimal_symbol *msymbol;
607 struct partial_symbol *psb;
609 if (pc >= cache_pc_function_low && pc < cache_pc_function_high)
610 goto return_cached_value;
612 /* If sigtramp is in the u area, it counts as a function (especially
613 important for step_1). */
614 #if defined SIGTRAMP_START
615 if (IN_SIGTRAMP (pc, (char *)NULL))
617 cache_pc_function_low = SIGTRAMP_START;
618 cache_pc_function_high = SIGTRAMP_END;
619 cache_pc_function_name = "<sigtramp>";
621 goto return_cached_value;
625 msymbol = lookup_minimal_symbol_by_pc (pc);
626 pst = find_pc_psymtab (pc);
629 /* Need to read the symbols to get a good value for the end address. */
630 if (endaddr != NULL && !pst->readin)
631 PSYMTAB_TO_SYMTAB (pst);
635 /* Checking whether the msymbol has a larger value is for the
636 "pathological" case mentioned in print_frame_info. */
637 f = find_pc_function (pc);
640 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
641 >= SYMBOL_VALUE_ADDRESS (msymbol))))
643 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
644 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
645 cache_pc_function_name = SYMBOL_NAME (f);
646 goto return_cached_value;
650 /* Now that static symbols go in the minimal symbol table, perhaps
651 we could just ignore the partial symbols. But at least for now
652 we use the partial or minimal symbol, whichever is larger. */
653 psb = find_pc_psymbol (pst, pc);
656 && (msymbol == NULL ||
657 (SYMBOL_VALUE_ADDRESS (psb) >= SYMBOL_VALUE_ADDRESS (msymbol))))
659 /* This case isn't being cached currently. */
661 *address = SYMBOL_VALUE_ADDRESS (psb);
663 *name = SYMBOL_NAME (psb);
664 /* endaddr non-NULL can't happen here. */
669 /* Must be in the minimal symbol table. */
672 /* No available symbol. */
682 /* I believe the purpose of this check is to make sure that anything
683 beyond the end of the text segment does not appear as part of the
684 last function of the text segment. It assumes that there is something
685 other than a mst_text symbol after the text segment. It is broken in
686 various cases, so anything relying on this behavior (there might be
687 some places) should be using find_pc_section or some such instead. */
688 if (msymbol -> type == mst_text)
689 cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
691 /* It is a transfer table for Sun shared libraries. */
692 cache_pc_function_low = pc - FUNCTION_START_OFFSET;
693 cache_pc_function_name = SYMBOL_NAME (msymbol);
695 if (SYMBOL_NAME (msymbol + 1) != NULL)
696 /* This might be part of a different segment, which might be a bad
697 idea. Perhaps we should be using the smaller of this address or the
698 endaddr from find_pc_section. */
699 cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + 1);
702 /* We got the start address from the last msymbol in the objfile.
703 So the end address is the end of the section. */
704 struct obj_section *sec;
706 sec = find_pc_section (pc);
709 /* Don't know if this can happen but if it does, then just say
710 that the function is 1 byte long. */
711 cache_pc_function_high = cache_pc_function_low + 1;
714 cache_pc_function_high = sec->endaddr;
719 *address = cache_pc_function_low;
721 *name = cache_pc_function_name;
723 *endaddr = cache_pc_function_high;
727 /* Return the innermost stack frame executing inside of BLOCK,
728 or zero if there is no such frame. If BLOCK is NULL, just return NULL. */
731 block_innermost_frame (block)
734 struct frame_info *fi;
735 register FRAME frame;
736 register CORE_ADDR start = BLOCK_START (block);
737 register CORE_ADDR end = BLOCK_END (block);
745 frame = get_prev_frame (frame);
748 fi = get_frame_info (frame);
749 if (fi->pc >= start && fi->pc < end)
754 #ifdef SIGCONTEXT_PC_OFFSET
755 /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp. */
758 sigtramp_saved_pc (frame)
761 CORE_ADDR sigcontext_addr;
762 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
763 int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT;
764 int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT;
766 /* Get sigcontext address, it is the third parameter on the stack. */
768 sigcontext_addr = read_memory_integer (FRAME_ARGS_ADDRESS (frame->next)
769 + FRAME_ARGS_SKIP + sigcontext_offs,
772 sigcontext_addr = read_memory_integer (read_register (SP_REGNUM)
776 /* Don't cause a memory_error when accessing sigcontext in case the stack
777 layout has changed or the stack is corrupt. */
778 target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes);
779 return extract_unsigned_integer (buf, ptrbytes);
781 #endif /* SIGCONTEXT_PC_OFFSET */
784 _initialize_blockframe ()
786 obstack_init (&frame_cache_obstack);