1 /* Get info from stack frames;
2 convert between frames, blocks, functions and pc values.
3 Copyright (C) 1986, 1987, 1988, 1989 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. */
27 #include "value.h" /* for read_register */
28 #include "target.h" /* for target_has_stack */
30 CORE_ADDR read_pc (); /* In infcmd.c */
32 /* Start and end of object file containing the entry point.
33 STARTUP_FILE_END is the first address of the next file.
34 This file is assumed to be a startup file
35 and frames with pc's inside it
36 are treated as nonexistent.
38 Setting these variables is necessary so that backtraces do not fly off
39 the bottom of the stack. */
40 CORE_ADDR startup_file_start;
41 CORE_ADDR startup_file_end;
43 /* Is ADDR outside the startup file? Note that if your machine
44 has a way to detect the bottom of the stack, there is no need
45 to call this function from FRAME_CHAIN_VALID; the reason for
46 doing so is that some machines have no way of detecting bottom
49 outside_startup_file (addr)
52 return !(addr >= startup_file_start && addr < startup_file_end);
55 /* Support an alternate method to avoid running off the bottom of
56 the stack (or top, depending upon your stack orientation).
58 There are two frames that are "special", the frame for the function
59 containing the process entry point, since it has no predecessor frame,
60 and the frame for the function containing the user code entry point
61 (the main() function), since all the predecessor frames are for the
62 process startup code. Since we have no guarantee that the linked
63 in startup modules have any debugging information that gdb can use,
64 we need to avoid following frame pointers back into frames that might
65 have been built in the startup code, as we might get hopelessly
66 confused. However, we almost always have debugging information
69 These variables are used to save the range of PC values which are valid
70 within the main() function and within the function containing the process
71 entry point. If we always consider the frame for main() as the outermost
72 frame when debugging user code, and the frame for the process entry
73 point function as the outermost frame when debugging startup code, then
74 all we have to do is have FRAME_CHAIN_VALID return false whenever a
75 frame's current PC is within the range specified by these variables.
76 In essence, we set "blocks" in the frame chain beyond which we will
77 not proceed when following the frame chain.
79 A nice side effect is that we can still debug startup code without
80 running off the end of the frame chain, assuming that we have usable
81 debugging information in the startup modules, and if we choose to not
82 use the block at main, or can't find it for some reason, everything
83 still works as before. And if we have no startup code debugging
84 information but we do have usable information for main(), backtraces
85 from user code don't go wandering off into the startup code.
87 To use this method, define your FRAME_CHAIN_VALID macro like:
89 #define FRAME_CHAIN_VALID(chain, thisframe) \
91 && !(inside_main_scope ((thisframe)->pc)) \
92 && !(inside_entry_scope ((thisframe)->pc)))
94 and add initializations of the four scope controlling variables inside
95 the object file / debugging information processing modules. */
97 CORE_ADDR entry_scope_lowpc;
98 CORE_ADDR entry_scope_highpc;
99 CORE_ADDR main_scope_lowpc;
100 CORE_ADDR main_scope_highpc;
102 /* Test a specified PC value to see if it is in the range of addresses
103 that correspond to the main() function. See comments above for why
104 we might want to do this.
106 Typically called from FRAME_CHAIN_VALID. */
109 inside_main_scope (pc)
112 return (main_scope_lowpc <= pc && pc < main_scope_highpc);
115 /* Test a specified PC value to see if it is in the range of addresses
116 that correspond to the process entry point function. See comments above
117 for why we might want to do this.
119 Typically called from FRAME_CHAIN_VALID. */
122 inside_entry_scope (pc)
125 return (entry_scope_lowpc <= pc && pc < entry_scope_highpc);
128 /* Address of innermost stack frame (contents of FP register) */
130 static FRAME current_frame;
133 * Cache for frame addresses already read by gdb. Valid only while
134 * inferior is stopped. Control variables for the frame cache should
135 * be local to this module.
137 struct obstack frame_cache_obstack;
139 /* Return the innermost (currently executing) stack frame. */
144 /* We assume its address is kept in a general register;
145 param.h says which register. */
147 return current_frame;
151 set_current_frame (frame)
154 current_frame = frame;
158 create_new_frame (addr, pc)
162 struct frame_info *fci; /* Same type as FRAME */
164 fci = (struct frame_info *)
165 obstack_alloc (&frame_cache_obstack,
166 sizeof (struct frame_info));
168 /* Arbitrary frame */
169 fci->next = (struct frame_info *) 0;
170 fci->prev = (struct frame_info *) 0;
172 fci->next_frame = 0; /* Since arbitrary */
175 #ifdef INIT_EXTRA_FRAME_INFO
176 INIT_EXTRA_FRAME_INFO (0, fci);
182 /* Return the frame that called FRAME.
183 If FRAME is the original frame (it has no caller), return 0. */
186 get_prev_frame (frame)
189 /* We're allowed to know that FRAME and "struct frame_info *" are
191 return get_prev_frame_info (frame);
194 /* Return the frame that FRAME calls (0 if FRAME is the innermost
198 get_next_frame (frame)
201 /* We're allowed to know that FRAME and "struct frame_info *" are
207 * Flush the entire frame cache.
210 flush_cached_frames ()
212 /* Since we can't really be sure what the first object allocated was */
213 obstack_free (&frame_cache_obstack, 0);
214 obstack_init (&frame_cache_obstack);
216 current_frame = (struct frame_info *) 0; /* Invalidate cache */
219 /* Flush the frame cache, and start a new one if necessary. */
221 reinit_frame_cache ()
223 FRAME fr = current_frame;
224 flush_cached_frames ();
226 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
230 /* Return a structure containing various interesting information
231 about a specified stack frame. */
232 /* How do I justify including this function? Well, the FRAME
233 identifier format has gone through several changes recently, and
234 it's not completely inconceivable that it could happen again. If
235 it does, have this routine around will help */
238 get_frame_info (frame)
244 /* If a machine allows frameless functions, it should define a macro
245 FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct
246 frame_info for the frame, and FRAMELESS should be set to nonzero
247 if it represents a frameless function invocation. */
249 /* Return nonzero if the function for this frame has a prologue. Many
250 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
254 frameless_look_for_prologue (frame)
257 CORE_ADDR func_start, after_prologue;
258 func_start = (get_pc_function_start (frame->pc) +
259 FUNCTION_START_OFFSET);
262 after_prologue = func_start;
263 #ifdef SKIP_PROLOGUE_FRAMELESS_P
264 /* This is faster, since only care whether there *is* a prologue,
265 not how long it is. */
266 SKIP_PROLOGUE_FRAMELESS_P (after_prologue);
268 SKIP_PROLOGUE (after_prologue);
270 return after_prologue == func_start;
273 /* If we can't find the start of the function, we don't really
274 know whether the function is frameless, but we should be able
275 to get a reasonable (i.e. best we can do under the
276 circumstances) backtrace by saying that it isn't. */
280 /* Default a few macros that people seldom redefine. */
282 #if !defined (INIT_FRAME_PC)
283 #define INIT_FRAME_PC(fromleaf, prev) \
284 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
285 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
288 #ifndef FRAME_CHAIN_COMBINE
289 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
292 /* Return a structure containing various interesting information
293 about the frame that called NEXT_FRAME. Returns NULL
294 if there is no such frame. */
297 get_prev_frame_info (next_frame)
301 struct frame_info *prev;
304 /* If the requested entry is in the cache, return it.
305 Otherwise, figure out what the address should be for the entry
306 we're about to add to the cache. */
312 error ("You haven't set up a process's stack to examine.");
315 return current_frame;
318 /* If we have the prev one, return it */
319 if (next_frame->prev)
320 return next_frame->prev;
322 /* On some machines it is possible to call a function without
323 setting up a stack frame for it. On these machines, we
324 define this macro to take two args; a frameinfo pointer
325 identifying a frame and a variable to set or clear if it is
326 or isn't leafless. */
327 #ifdef FRAMELESS_FUNCTION_INVOCATION
328 /* Still don't want to worry about this except on the innermost
329 frame. This macro will set FROMLEAF if NEXT_FRAME is a
330 frameless function invocation. */
331 if (!(next_frame->next))
333 FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
335 address = next_frame->frame;
341 /* Two macros defined in tm.h specify the machine-dependent
342 actions to be performed here.
343 First, get the frame's chain-pointer.
344 If that is zero, the frame is the outermost frame or a leaf
345 called by the outermost frame. This means that if start
346 calls main without a frame, we'll return 0 (which is fine
349 Nope; there's a problem. This also returns when the current
350 routine is a leaf of main. This is unacceptable. We move
351 this to after the ffi test; I'd rather have backtraces from
352 start go curfluy than have an abort called from main not show
354 address = FRAME_CHAIN (next_frame);
355 if (!FRAME_CHAIN_VALID (address, next_frame))
357 address = FRAME_CHAIN_COMBINE (address, next_frame);
362 prev = (struct frame_info *)
363 obstack_alloc (&frame_cache_obstack,
364 sizeof (struct frame_info));
367 next_frame->prev = prev;
368 prev->next = next_frame;
369 prev->prev = (struct frame_info *) 0;
370 prev->frame = address;
371 prev->next_frame = prev->next ? prev->next->frame : 0;
373 #ifdef INIT_EXTRA_FRAME_INFO
374 INIT_EXTRA_FRAME_INFO(fromleaf, prev);
377 /* This entry is in the frame queue now, which is good since
378 FRAME_SAVED_PC may use that queue to figure out it's value
379 (see tm-sparc.h). We want the pc saved in the inferior frame. */
380 INIT_FRAME_PC(fromleaf, prev);
389 struct frame_info *fi;
390 fi = get_frame_info (frame);
394 #if defined (FRAME_FIND_SAVED_REGS)
395 /* Find the addresses in which registers are saved in FRAME. */
398 get_frame_saved_regs (frame_info_addr, saved_regs_addr)
399 struct frame_info *frame_info_addr;
400 struct frame_saved_regs *saved_regs_addr;
402 FRAME_FIND_SAVED_REGS (frame_info_addr, *saved_regs_addr);
406 /* Return the innermost lexical block in execution
407 in a specified stack frame. The frame address is assumed valid. */
410 get_frame_block (frame)
413 struct frame_info *fi;
416 fi = get_frame_info (frame);
419 if (fi->next_frame != 0)
420 /* We are not in the innermost frame. We need to subtract one to
421 get the correct block, in case the call instruction was the
422 last instruction of the block. If there are any machines on
423 which the saved pc does not point to after the call insn, we
424 probably want to make fi->pc point after the call insn anyway. */
426 return block_for_pc (pc);
432 return block_for_pc (read_pc ());
436 get_pc_function_start (pc)
439 register struct block *bl = block_for_pc (pc);
440 register struct symbol *symbol;
441 if (bl == 0 || (symbol = block_function (bl)) == 0)
443 register int misc_index = find_pc_misc_function (pc);
445 return misc_function_vector[misc_index].address;
448 bl = SYMBOL_BLOCK_VALUE (symbol);
449 return BLOCK_START (bl);
452 /* Return the symbol for the function executing in frame FRAME. */
455 get_frame_function (frame)
458 register struct block *bl = get_frame_block (frame);
461 return block_function (bl);
464 /* Return the blockvector immediately containing the innermost lexical block
465 containing the specified pc value, or 0 if there is none.
466 PINDEX is a pointer to the index value of the block. If PINDEX
467 is NULL, we don't pass this information back to the caller. */
470 blockvector_for_pc (pc, pindex)
471 register CORE_ADDR pc;
474 register struct block *b;
475 register int bot, top, half;
476 register struct symtab *s;
477 struct blockvector *bl;
479 /* First search all symtabs for one whose file contains our pc */
480 s = find_pc_symtab (pc);
484 bl = BLOCKVECTOR (s);
485 b = BLOCKVECTOR_BLOCK (bl, 0);
487 /* Then search that symtab for the smallest block that wins. */
488 /* Use binary search to find the last block that starts before PC. */
491 top = BLOCKVECTOR_NBLOCKS (bl);
493 while (top - bot > 1)
495 half = (top - bot + 1) >> 1;
496 b = BLOCKVECTOR_BLOCK (bl, bot + half);
497 if (BLOCK_START (b) <= pc)
503 /* Now search backward for a block that ends after PC. */
507 b = BLOCKVECTOR_BLOCK (bl, bot);
508 if (BLOCK_END (b) > pc)
520 /* Return the innermost lexical block containing the specified pc value,
521 or 0 if there is none. */
525 register CORE_ADDR pc;
527 register struct blockvector *bl;
530 bl = blockvector_for_pc (pc, &index);
532 return BLOCKVECTOR_BLOCK (bl, index);
536 /* Return the function containing pc value PC.
537 Returns 0 if function is not known. */
540 find_pc_function (pc)
543 register struct block *b = block_for_pc (pc);
546 return block_function (b);
549 /* These variables are used to cache the most recent result
550 * of find_pc_partial_function. */
552 static CORE_ADDR cache_pc_function_low = 0;
553 static CORE_ADDR cache_pc_function_high = 0;
554 static char *cache_pc_function_name = 0;
556 /* Clear cache, e.g. when symbol table is discarded. */
559 clear_pc_function_cache()
561 cache_pc_function_low = 0;
562 cache_pc_function_high = 0;
563 cache_pc_function_name = (char *)0;
566 /* Finds the "function" (text symbol) that is smaller than PC
567 but greatest of all of the potential text symbols. Sets
568 *NAME and/or *ADDRESS conditionally if that pointer is non-zero.
569 Returns 0 if it couldn't find anything, 1 if it did. On a zero
570 return, *NAME and *ADDRESS are always set to zero. On a 1 return,
571 *NAME and *ADDRESS contain real information. */
574 find_pc_partial_function (pc, name, address)
579 struct partial_symtab *pst;
582 struct partial_symbol *psb;
584 if (pc >= cache_pc_function_low && pc < cache_pc_function_high)
587 *address = cache_pc_function_low;
589 *name = cache_pc_function_name;
593 pst = find_pc_psymtab (pc);
598 /* The information we want has already been read in.
599 We can go to the already readin symbols and we'll get
600 the best possible answer. */
601 f = find_pc_function (pc);
605 /* No available symbol. */
613 cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
614 cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
615 cache_pc_function_name = SYMBOL_NAME (f);
617 *name = cache_pc_function_name;
619 *address = cache_pc_function_low;
623 /* Get the information from a combination of the pst
624 (static symbols), and the misc function vector (extern
626 miscfunc = find_pc_misc_function (pc);
627 psb = find_pc_psymbol (pst, pc);
629 if (!psb && miscfunc == -1)
635 || (SYMBOL_VALUE_ADDRESS (psb)
636 >= misc_function_vector[miscfunc].address)))
638 /* This case isn't being cached currently. */
640 *address = SYMBOL_VALUE_ADDRESS (psb);
642 *name = SYMBOL_NAME (psb);
647 /* Must be in the misc function stuff. */
649 miscfunc = find_pc_misc_function (pc);
655 if (misc_function_vector[miscfunc].type == mf_text)
656 cache_pc_function_low = misc_function_vector[miscfunc].address;
658 /* It is a transfer table for Sun shared libraries. */
659 cache_pc_function_low = pc - FUNCTION_START_OFFSET;
661 cache_pc_function_name = misc_function_vector[miscfunc].name;
662 if (miscfunc < misc_function_count /* && FIXME mf_text again? */ )
663 cache_pc_function_high = misc_function_vector[miscfunc+1].address;
665 cache_pc_function_high = cache_pc_function_low + 1;
667 *address = cache_pc_function_low;
669 *name = cache_pc_function_name;
673 /* Find the misc function whose address is the largest
674 while being less than PC. Return its index in misc_function_vector.
675 Returns -1 if PC is not in suitable range. */
678 find_pc_misc_function (pc)
679 register CORE_ADDR pc;
682 register int hi = misc_function_count-1;
685 /* Note that the last thing in the vector is always _etext. */
686 /* Actually, "end", now that non-functions
687 go on the misc_function_vector. */
689 /* Above statement is not *always* true - fix for case where there are */
690 /* no misc functions at all (ie no symbol table has been read). */
691 if (hi < 0) return -1; /* no misc functions recorded */
693 /* trivial reject range test */
694 if (pc < misc_function_vector[0].address ||
695 pc > misc_function_vector[hi].address)
698 /* Note that the following search will not return hi if
699 pc == misc_function_vector[hi].address. If "end" points to the
700 first unused location, this is correct and the above test
701 simply needs to be changed to
702 "pc >= misc_function_vector[hi].address". */
704 new = (lo + hi) >> 1;
705 if (misc_function_vector[new].address == pc)
706 return new; /* an exact match */
707 else if (misc_function_vector[new].address > pc)
711 } while (hi-lo != 1);
713 /* if here, we had no exact match, so return the lower choice */
717 /* Return the innermost stack frame executing inside of the specified block,
718 or zero if there is no such frame. */
721 block_innermost_frame (block)
724 struct frame_info *fi;
725 register FRAME frame;
726 register CORE_ADDR start = BLOCK_START (block);
727 register CORE_ADDR end = BLOCK_END (block);
732 frame = get_prev_frame (frame);
735 fi = get_frame_info (frame);
736 if (fi->pc >= start && fi->pc < end)
742 _initialize_blockframe ()
744 obstack_init (&frame_cache_obstack);