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 */
128 fci
= (struct frame_info
*)
129 obstack_alloc (&frame_cache_obstack
,
130 sizeof (struct frame_info
));
132 /* Arbitrary frame */
133 fci
->next
= (struct frame_info
*) 0;
134 fci
->prev
= (struct frame_info
*) 0;
137 fci
->signal_handler_caller
= IN_SIGTRAMP (fci
->pc
, (char *)NULL
);
139 #ifdef INIT_EXTRA_FRAME_INFO
140 INIT_EXTRA_FRAME_INFO (0, fci
);
146 /* Return the frame that called FRAME.
147 If FRAME is the original frame (it has no caller), return 0. */
150 get_prev_frame (frame
)
153 /* We're allowed to know that FRAME and "struct frame_info *" are
155 return get_prev_frame_info (frame
);
158 /* Return the frame that FRAME calls (0 if FRAME is the innermost
162 get_next_frame (frame
)
165 /* We're allowed to know that FRAME and "struct frame_info *" are
171 * Flush the entire frame cache.
174 flush_cached_frames ()
176 /* Since we can't really be sure what the first object allocated was */
177 obstack_free (&frame_cache_obstack
, 0);
178 obstack_init (&frame_cache_obstack
);
180 current_frame
= (struct frame_info
*) 0; /* Invalidate cache */
183 /* Flush the frame cache, and start a new one if necessary. */
185 reinit_frame_cache ()
187 FRAME fr
= current_frame
;
188 flush_cached_frames ();
190 set_current_frame ( create_new_frame (read_fp (), read_pc ()));
193 /* Return a structure containing various interesting information
194 about a specified stack frame. */
195 /* How do I justify including this function? Well, the FRAME
196 identifier format has gone through several changes recently, and
197 it's not completely inconceivable that it could happen again. If
198 it does, have this routine around will help */
201 get_frame_info (frame
)
207 /* If a machine allows frameless functions, it should define a macro
208 FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct
209 frame_info for the frame, and FRAMELESS should be set to nonzero
210 if it represents a frameless function invocation. */
212 /* Return nonzero if the function for this frame lacks a prologue. Many
213 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
217 frameless_look_for_prologue (frame
)
220 CORE_ADDR func_start
, after_prologue
;
221 func_start
= (get_pc_function_start (frame
->pc
) +
222 FUNCTION_START_OFFSET
);
225 after_prologue
= func_start
;
226 #ifdef SKIP_PROLOGUE_FRAMELESS_P
227 /* This is faster, since only care whether there *is* a prologue,
228 not how long it is. */
229 SKIP_PROLOGUE_FRAMELESS_P (after_prologue
);
231 SKIP_PROLOGUE (after_prologue
);
233 return after_prologue
== func_start
;
236 /* If we can't find the start of the function, we don't really
237 know whether the function is frameless, but we should be able
238 to get a reasonable (i.e. best we can do under the
239 circumstances) backtrace by saying that it isn't. */
243 /* Default a few macros that people seldom redefine. */
245 #if !defined (INIT_FRAME_PC)
246 #define INIT_FRAME_PC(fromleaf, prev) \
247 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
248 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
251 #ifndef FRAME_CHAIN_COMBINE
252 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
255 /* Return a structure containing various interesting information
256 about the frame that called NEXT_FRAME. Returns NULL
257 if there is no such frame. */
260 get_prev_frame_info (next_frame
)
264 struct frame_info
*prev
;
267 /* If the requested entry is in the cache, return it.
268 Otherwise, figure out what the address should be for the entry
269 we're about to add to the cache. */
275 error ("You haven't set up a process's stack to examine.");
278 return current_frame
;
281 /* If we have the prev one, return it */
282 if (next_frame
->prev
)
283 return next_frame
->prev
;
285 /* On some machines it is possible to call a function without
286 setting up a stack frame for it. On these machines, we
287 define this macro to take two args; a frameinfo pointer
288 identifying a frame and a variable to set or clear if it is
289 or isn't leafless. */
290 #ifdef FRAMELESS_FUNCTION_INVOCATION
291 /* Still don't want to worry about this except on the innermost
292 frame. This macro will set FROMLEAF if NEXT_FRAME is a
293 frameless function invocation. */
294 if (!(next_frame
->next
))
296 FRAMELESS_FUNCTION_INVOCATION (next_frame
, fromleaf
);
298 address
= next_frame
->frame
;
304 /* Two macros defined in tm.h specify the machine-dependent
305 actions to be performed here.
306 First, get the frame's chain-pointer.
307 If that is zero, the frame is the outermost frame or a leaf
308 called by the outermost frame. This means that if start
309 calls main without a frame, we'll return 0 (which is fine
312 Nope; there's a problem. This also returns when the current
313 routine is a leaf of main. This is unacceptable. We move
314 this to after the ffi test; I'd rather have backtraces from
315 start go curfluy than have an abort called from main not show
317 address
= FRAME_CHAIN (next_frame
);
318 if (!FRAME_CHAIN_VALID (address
, next_frame
))
320 address
= FRAME_CHAIN_COMBINE (address
, next_frame
);
325 prev
= (struct frame_info
*)
326 obstack_alloc (&frame_cache_obstack
,
327 sizeof (struct frame_info
));
330 next_frame
->prev
= prev
;
331 prev
->next
= next_frame
;
332 prev
->prev
= (struct frame_info
*) 0;
333 prev
->frame
= address
;
334 prev
->signal_handler_caller
= 0;
336 /* This change should not be needed, FIXME! We should
337 determine whether any targets *need* INIT_FRAME_PC to happen
338 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
339 express what goes on here.
341 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
342 (where the PC is already set up) and here (where it isn't).
343 INIT_FRAME_PC is only called from here, always after
344 INIT_EXTRA_FRAME_INFO.
346 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
347 value (which hasn't been set yet). Some other machines appear to
348 require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo.
350 We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
351 an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92.
353 To answer the question, yes the sparc needs INIT_FRAME_PC after
354 INIT_EXTRA_FRAME_INFO. Suggested scheme:
356 SETUP_INNERMOST_FRAME()
357 Default version is just create_new_frame (read_fp ()),
358 read_pc ()). Machines with extra frame info would do that (or the
359 local equivalent) and then set the extra fields.
360 SETUP_ARBITRARY_FRAME(argc, argv)
361 Only change here is that create_new_frame would no longer init extra
362 frame info; SETUP_ARBITRARY_FRAME would have to do that.
363 INIT_PREV_FRAME(fromleaf, prev)
364 Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC.
365 std_frame_pc(fromleaf, prev)
366 This is the default setting for INIT_PREV_FRAME. It just does what
367 the default INIT_FRAME_PC does. Some machines will call it from
368 INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
369 Some machines won't use it.
370 kingdon@cygnus.com, 13Apr93. */
372 #ifdef INIT_FRAME_PC_FIRST
373 INIT_FRAME_PC_FIRST (fromleaf
, prev
);
376 #ifdef INIT_EXTRA_FRAME_INFO
377 INIT_EXTRA_FRAME_INFO(fromleaf
, prev
);
380 /* This entry is in the frame queue now, which is good since
381 FRAME_SAVED_PC may use that queue to figure out it's value
382 (see tm-sparc.h). We want the pc saved in the inferior frame. */
383 INIT_FRAME_PC(fromleaf
, prev
);
385 if (IN_SIGTRAMP (prev
->pc
, (char *)NULL
))
386 prev
->signal_handler_caller
= 1;
395 struct frame_info
*fi
;
396 fi
= get_frame_info (frame
);
400 #if defined (FRAME_FIND_SAVED_REGS)
401 /* Find the addresses in which registers are saved in FRAME. */
404 get_frame_saved_regs (frame_info_addr
, saved_regs_addr
)
405 struct frame_info
*frame_info_addr
;
406 struct frame_saved_regs
*saved_regs_addr
;
408 FRAME_FIND_SAVED_REGS (frame_info_addr
, *saved_regs_addr
);
412 /* Return the innermost lexical block in execution
413 in a specified stack frame. The frame address is assumed valid. */
416 get_frame_block (frame
)
419 struct frame_info
*fi
;
422 fi
= get_frame_info (frame
);
426 /* We are not in the innermost frame. We need to subtract one to
427 get the correct block, in case the call instruction was the
428 last instruction of the block. If there are any machines on
429 which the saved pc does not point to after the call insn, we
430 probably want to make fi->pc point after the call insn anyway. */
432 return block_for_pc (pc
);
438 return block_for_pc (read_pc ());
442 get_pc_function_start (pc
)
445 register struct block
*bl
;
446 register struct symbol
*symbol
;
447 register struct minimal_symbol
*msymbol
;
450 if ((bl
= block_for_pc (pc
)) != NULL
&&
451 (symbol
= block_function (bl
)) != NULL
)
453 bl
= SYMBOL_BLOCK_VALUE (symbol
);
454 fstart
= BLOCK_START (bl
);
456 else if ((msymbol
= lookup_minimal_symbol_by_pc (pc
)) != NULL
)
458 fstart
= SYMBOL_VALUE_ADDRESS (msymbol
);
467 /* Return the symbol for the function executing in frame FRAME. */
470 get_frame_function (frame
)
473 register struct block
*bl
= get_frame_block (frame
);
476 return block_function (bl
);
479 /* Return the blockvector immediately containing the innermost lexical block
480 containing the specified pc value, or 0 if there is none.
481 PINDEX is a pointer to the index value of the block. If PINDEX
482 is NULL, we don't pass this information back to the caller. */
485 blockvector_for_pc (pc
, pindex
)
486 register CORE_ADDR pc
;
489 register struct block
*b
;
490 register int bot
, top
, half
;
491 register struct symtab
*s
;
492 struct blockvector
*bl
;
494 /* First search all symtabs for one whose file contains our pc */
495 s
= find_pc_symtab (pc
);
499 bl
= BLOCKVECTOR (s
);
500 b
= BLOCKVECTOR_BLOCK (bl
, 0);
502 /* Then search that symtab for the smallest block that wins. */
503 /* Use binary search to find the last block that starts before PC. */
506 top
= BLOCKVECTOR_NBLOCKS (bl
);
508 while (top
- bot
> 1)
510 half
= (top
- bot
+ 1) >> 1;
511 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
512 if (BLOCK_START (b
) <= pc
)
518 /* Now search backward for a block that ends after PC. */
522 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
523 if (BLOCK_END (b
) > pc
)
535 /* Return the innermost lexical block containing the specified pc value,
536 or 0 if there is none. */
540 register CORE_ADDR pc
;
542 register struct blockvector
*bl
;
545 bl
= blockvector_for_pc (pc
, &index
);
547 return BLOCKVECTOR_BLOCK (bl
, index
);
551 /* Return the function containing pc value PC.
552 Returns 0 if function is not known. */
555 find_pc_function (pc
)
558 register struct block
*b
= block_for_pc (pc
);
561 return block_function (b
);
564 /* These variables are used to cache the most recent result
565 * of find_pc_partial_function. */
567 static CORE_ADDR cache_pc_function_low
= 0;
568 static CORE_ADDR cache_pc_function_high
= 0;
569 static char *cache_pc_function_name
= 0;
571 /* Clear cache, e.g. when symbol table is discarded. */
574 clear_pc_function_cache()
576 cache_pc_function_low
= 0;
577 cache_pc_function_high
= 0;
578 cache_pc_function_name
= (char *)0;
581 /* Finds the "function" (text symbol) that is smaller than PC
582 but greatest of all of the potential text symbols. Sets
583 *NAME and/or *ADDRESS conditionally if that pointer is non-zero.
584 Returns 0 if it couldn't find anything, 1 if it did. On a zero
585 return, *NAME and *ADDRESS are always set to zero. On a 1 return,
586 *NAME and *ADDRESS contain real information. */
589 find_pc_partial_function (pc
, name
, address
)
594 struct partial_symtab
*pst
;
596 struct minimal_symbol
*msymbol
;
597 struct partial_symbol
*psb
;
599 if (pc
>= cache_pc_function_low
&& pc
< cache_pc_function_high
)
602 *address
= cache_pc_function_low
;
604 *name
= cache_pc_function_name
;
608 pst
= find_pc_psymtab (pc
);
613 /* The information we want has already been read in.
614 We can go to the already readin symbols and we'll get
615 the best possible answer. */
616 f
= find_pc_function (pc
);
620 /* No available symbol. */
628 cache_pc_function_low
= BLOCK_START (SYMBOL_BLOCK_VALUE (f
));
629 cache_pc_function_high
= BLOCK_END (SYMBOL_BLOCK_VALUE (f
));
630 cache_pc_function_name
= SYMBOL_NAME (f
);
632 *name
= cache_pc_function_name
;
634 *address
= cache_pc_function_low
;
638 /* Get the information from a combination of the pst
639 (static symbols), and the minimal symbol table (extern
641 msymbol
= lookup_minimal_symbol_by_pc (pc
);
642 psb
= find_pc_psymbol (pst
, pc
);
644 if (!psb
&& (msymbol
== NULL
))
649 && (msymbol
== NULL
||
650 (SYMBOL_VALUE_ADDRESS (psb
) >= SYMBOL_VALUE_ADDRESS (msymbol
))))
652 /* This case isn't being cached currently. */
654 *address
= SYMBOL_VALUE_ADDRESS (psb
);
656 *name
= SYMBOL_NAME (psb
);
661 /* Must be in the minimal symbol table. */
663 msymbol
= lookup_minimal_symbol_by_pc (pc
);
669 if (msymbol
-> type
== mst_text
)
670 cache_pc_function_low
= SYMBOL_VALUE_ADDRESS (msymbol
);
672 /* It is a transfer table for Sun shared libraries. */
673 cache_pc_function_low
= pc
- FUNCTION_START_OFFSET
;
675 cache_pc_function_name
= SYMBOL_NAME (msymbol
);
676 /* FIXME: Deal with bumping into end of minimal symbols for a given
677 objfile, and what about testing for mst_text again? */
678 if (SYMBOL_NAME (msymbol
+ 1) != NULL
)
679 cache_pc_function_high
= SYMBOL_VALUE_ADDRESS (msymbol
+ 1);
681 cache_pc_function_high
= cache_pc_function_low
+ 1;
683 *address
= cache_pc_function_low
;
685 *name
= cache_pc_function_name
;
689 /* Return the innermost stack frame executing inside of the specified block,
690 or zero if there is no such frame. */
692 #if 0 /* Currently unused */
695 block_innermost_frame (block
)
698 struct frame_info
*fi
;
699 register FRAME frame
;
700 register CORE_ADDR start
= BLOCK_START (block
);
701 register CORE_ADDR end
= BLOCK_END (block
);
706 frame
= get_prev_frame (frame
);
709 fi
= get_frame_info (frame
);
710 if (fi
->pc
>= start
&& fi
->pc
< end
)
718 _initialize_blockframe ()
720 obstack_init (&frame_cache_obstack
);