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 GDB 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 1, or (at your option)
12 GDB 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 GDB; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #include "value.h" /* for read_register */
28 #include "target.h" /* for target_has_stack */
30 /* Required by INIT_EXTRA_FRAME_INFO on 88k. */
34 CORE_ADDR
read_pc (); /* In infcmd.c */
36 /* Start and end of object file containing the entry point.
37 STARTUP_FILE_END is the first address of the next file.
38 This file is assumed to be a startup file
39 and frames with pc's inside it
40 are treated as nonexistent.
42 Setting these variables is necessary so that backtraces do not fly off
43 the bottom of the stack. */
44 CORE_ADDR startup_file_start
;
45 CORE_ADDR startup_file_end
;
47 /* Is ADDR outside the startup file? Note that if your machine
48 has a way to detect the bottom of the stack, there is no need
49 to call this function from FRAME_CHAIN_VALID; the reason for
50 doing so is that some machines have no way of detecting bottom
53 outside_startup_file (addr
)
56 return !(addr
>= startup_file_start
&& addr
< startup_file_end
);
59 /* Address of innermost stack frame (contents of FP register) */
61 static FRAME current_frame
;
64 * Cache for frame addresses already read by gdb. Valid only while
65 * inferior is stopped. Control variables for the frame cache should
66 * be local to this module.
68 struct obstack frame_cache_obstack
;
70 /* Return the innermost (currently executing) stack frame. */
75 /* We assume its address is kept in a general register;
76 param.h says which register. */
82 set_current_frame (frame
)
85 current_frame
= frame
;
89 create_new_frame (addr
, pc
)
93 struct frame_info
*fci
; /* Same type as FRAME */
95 fci
= (struct frame_info
*)
96 obstack_alloc (&frame_cache_obstack
,
97 sizeof (struct frame_info
));
100 fci
->next
= (struct frame_info
*) 0;
101 fci
->prev
= (struct frame_info
*) 0;
103 fci
->next_frame
= 0; /* Since arbitrary */
106 #ifdef INIT_EXTRA_FRAME_INFO
107 INIT_EXTRA_FRAME_INFO (fci
);
113 /* Return the frame that called FRAME.
114 If FRAME is the original frame (it has no caller), return 0. */
117 get_prev_frame (frame
)
120 /* We're allowed to know that FRAME and "struct frame_info *" are
122 return get_prev_frame_info (frame
);
125 /* Return the frame that FRAME calls (0 if FRAME is the innermost
129 get_next_frame (frame
)
132 /* We're allowed to know that FRAME and "struct frame_info *" are
138 * Flush the entire frame cache.
141 flush_cached_frames ()
143 /* Since we can't really be sure what the first object allocated was */
144 obstack_free (&frame_cache_obstack
, 0);
145 obstack_init (&frame_cache_obstack
);
147 current_frame
= (struct frame_info
*) 0; /* Invalidate cache */
150 /* Flush the frame cache, and start a new one if necessary. */
152 reinit_frame_cache ()
154 FRAME fr
= current_frame
;
155 flush_cached_frames ();
157 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
161 /* Return a structure containing various interesting information
162 about a specified stack frame. */
163 /* How do I justify including this function? Well, the FRAME
164 identifier format has gone through several changes recently, and
165 it's not completely inconceivable that it could happen again. If
166 it does, have this routine around will help */
169 get_frame_info (frame
)
175 /* If a machine allows frameless functions, it should define a macro
176 FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct
177 frame_info for the frame, and FRAMELESS should be set to nonzero
178 if it represents a frameless function invocation. */
180 /* Return nonzero if the function for this frame has a prologue. Many
181 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
185 frameless_look_for_prologue (frame
)
188 CORE_ADDR func_start
, after_prologue
;
189 func_start
= (get_pc_function_start (frame
->pc
) +
190 FUNCTION_START_OFFSET
);
193 after_prologue
= func_start
;
194 SKIP_PROLOGUE (after_prologue
);
195 return after_prologue
== func_start
;
198 /* If we can't find the start of the function, we don't really
199 know whether the function is frameless, but we should be able
200 to get a reasonable (i.e. best we can do under the
201 circumstances) backtrace by saying that it isn't. */
205 #if !defined (INIT_FRAME_PC)
206 #define INIT_FRAME_PC(fromleaf, prev) \
207 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
208 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
211 /* Return a structure containing various interesting information
212 about the frame that called NEXT_FRAME. Returns NULL
213 if there is no such frame. */
216 get_prev_frame_info (next_frame
)
220 struct frame_info
*prev
;
223 /* If the requested entry is in the cache, return it.
224 Otherwise, figure out what the address should be for the entry
225 we're about to add to the cache. */
231 error ("You haven't set up a process's stack to examine.");
234 return current_frame
;
237 /* If we have the prev one, return it */
238 if (next_frame
->prev
)
239 return next_frame
->prev
;
241 /* On some machines it is possible to call a function without
242 setting up a stack frame for it. On these machines, we
243 define this macro to take two args; a frameinfo pointer
244 identifying a frame and a variable to set or clear if it is
245 or isn't leafless. */
246 #ifdef FRAMELESS_FUNCTION_INVOCATION
247 /* Still don't want to worry about this except on the innermost
248 frame. This macro will set FROMLEAF if NEXT_FRAME is a
249 frameless function invocation. */
250 if (!(next_frame
->next
))
252 FRAMELESS_FUNCTION_INVOCATION (next_frame
, fromleaf
);
254 address
= next_frame
->frame
;
260 /* Two macros defined in tm.h specify the machine-dependent
261 actions to be performed here.
262 First, get the frame's chain-pointer.
263 If that is zero, the frame is the outermost frame or a leaf
264 called by the outermost frame. This means that if start
265 calls main without a frame, we'll return 0 (which is fine
268 Nope; there's a problem. This also returns when the current
269 routine is a leaf of main. This is unacceptable. We move
270 this to after the ffi test; I'd rather have backtraces from
271 start go curfluy than have an abort called from main not show
273 address
= FRAME_CHAIN (next_frame
);
274 if (!FRAME_CHAIN_VALID (address
, next_frame
))
276 address
= FRAME_CHAIN_COMBINE (address
, next_frame
);
279 prev
= (struct frame_info
*)
280 obstack_alloc (&frame_cache_obstack
,
281 sizeof (struct frame_info
));
284 next_frame
->prev
= prev
;
285 prev
->next
= next_frame
;
286 prev
->prev
= (struct frame_info
*) 0;
287 prev
->frame
= address
;
288 prev
->next_frame
= prev
->next
? prev
->next
->frame
: 0;
290 #ifdef INIT_EXTRA_FRAME_INFO
291 INIT_EXTRA_FRAME_INFO(prev
);
294 /* This entry is in the frame queue now, which is good since
295 FRAME_SAVED_PC may use that queue to figure out it's value
296 (see m-sparc.h). We want the pc saved in the inferior frame. */
297 INIT_FRAME_PC(fromleaf
, prev
);
306 struct frame_info
*fi
;
307 fi
= get_frame_info (frame
);
311 #if defined (FRAME_FIND_SAVED_REGS)
312 /* Find the addresses in which registers are saved in FRAME. */
315 get_frame_saved_regs (frame_info_addr
, saved_regs_addr
)
316 struct frame_info
*frame_info_addr
;
317 struct frame_saved_regs
*saved_regs_addr
;
319 FRAME_FIND_SAVED_REGS (frame_info_addr
, *saved_regs_addr
);
323 /* Return the innermost lexical block in execution
324 in a specified stack frame. The frame address is assumed valid. */
327 get_frame_block (frame
)
330 struct frame_info
*fi
;
333 fi
= get_frame_info (frame
);
336 if (fi
->next_frame
!= 0)
337 /* We are not in the innermost frame. We need to subtract one to
338 get the correct block, in case the call instruction was the
339 last instruction of the block. If there are any machines on
340 which the saved pc does not point to after the call insn, we
341 probably want to make fi->pc point after the call insn anyway. */
343 return block_for_pc (pc
);
349 return block_for_pc (read_pc ());
353 get_pc_function_start (pc
)
356 register struct block
*bl
= block_for_pc (pc
);
357 register struct symbol
*symbol
;
358 if (bl
== 0 || (symbol
= block_function (bl
)) == 0)
360 register int misc_index
= find_pc_misc_function (pc
);
362 return misc_function_vector
[misc_index
].address
;
365 bl
= SYMBOL_BLOCK_VALUE (symbol
);
366 return BLOCK_START (bl
);
369 /* Return the symbol for the function executing in frame FRAME. */
372 get_frame_function (frame
)
375 register struct block
*bl
= get_frame_block (frame
);
378 return block_function (bl
);
381 /* Return the blockvector immediately containing the innermost lexical block
382 containing the specified pc value, or 0 if there is none.
383 PINDEX is a pointer to the index value of the block. If PINDEX
384 is NULL, we don't pass this information back to the caller. */
387 blockvector_for_pc (pc
, pindex
)
388 register CORE_ADDR pc
;
391 register struct block
*b
;
392 register int bot
, top
, half
;
393 register struct symtab
*s
;
394 struct blockvector
*bl
;
396 /* First search all symtabs for one whose file contains our pc */
397 s
= find_pc_symtab (pc
);
401 bl
= BLOCKVECTOR (s
);
402 b
= BLOCKVECTOR_BLOCK (bl
, 0);
404 /* Then search that symtab for the smallest block that wins. */
405 /* Use binary search to find the last block that starts before PC. */
408 top
= BLOCKVECTOR_NBLOCKS (bl
);
410 while (top
- bot
> 1)
412 half
= (top
- bot
+ 1) >> 1;
413 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
414 if (BLOCK_START (b
) <= pc
)
420 /* Now search backward for a block that ends after PC. */
424 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
425 if (BLOCK_END (b
) > pc
)
437 /* Return the innermost lexical block containing the specified pc value,
438 or 0 if there is none. */
442 register CORE_ADDR pc
;
444 register struct blockvector
*bl
;
447 bl
= blockvector_for_pc (pc
, &index
);
449 return BLOCKVECTOR_BLOCK (bl
, index
);
453 /* Return the function containing pc value PC.
454 Returns 0 if function is not known. */
457 find_pc_function (pc
)
460 register struct block
*b
= block_for_pc (pc
);
463 return block_function (b
);
466 /* These variables are used to cache the most recent result
467 * of find_pc_partial_function. */
469 static CORE_ADDR cache_pc_function_low
= 0;
470 static CORE_ADDR cache_pc_function_high
= 0;
471 static char *cache_pc_function_name
= 0;
473 /* Clear cache, e.g. when symbol table is discarded. */
476 clear_pc_function_cache()
478 cache_pc_function_low
= 0;
479 cache_pc_function_high
= 0;
480 cache_pc_function_name
= (char *)0;
483 /* Finds the "function" (text symbol) that is smaller than PC
484 but greatest of all of the potential text symbols. Sets
485 *NAME and/or *ADDRESS conditionally if that pointer is non-zero.
486 Returns 0 if it couldn't find anything, 1 if it did. On a zero
487 return, *NAME and *ADDRESS are always set to zero. On a 1 return,
488 *NAME and *ADDRESS contain real information. */
491 find_pc_partial_function (pc
, name
, address
)
496 struct partial_symtab
*pst
;
499 struct partial_symbol
*psb
;
501 if (pc
>= cache_pc_function_low
&& pc
< cache_pc_function_high
)
504 *address
= cache_pc_function_low
;
506 *name
= cache_pc_function_name
;
510 pst
= find_pc_psymtab (pc
);
515 /* The information we want has already been read in.
516 We can go to the already readin symbols and we'll get
517 the best possible answer. */
518 f
= find_pc_function (pc
);
522 /* No available symbol. */
530 cache_pc_function_low
= BLOCK_START (SYMBOL_BLOCK_VALUE (f
));
531 cache_pc_function_high
= BLOCK_END (SYMBOL_BLOCK_VALUE (f
));
532 cache_pc_function_name
= SYMBOL_NAME (f
);
534 *name
= cache_pc_function_name
;
536 *address
= cache_pc_function_low
;
540 /* Get the information from a combination of the pst
541 (static symbols), and the misc function vector (extern
543 miscfunc
= find_pc_misc_function (pc
);
544 psb
= find_pc_psymbol (pst
, pc
);
546 if (!psb
&& miscfunc
== -1)
552 || (SYMBOL_VALUE_ADDRESS (psb
)
553 >= misc_function_vector
[miscfunc
].address
)))
555 /* This case isn't being cached currently. */
557 *address
= SYMBOL_VALUE_ADDRESS (psb
);
559 *name
= SYMBOL_NAME (psb
);
564 /* Must be in the misc function stuff. */
566 miscfunc
= find_pc_misc_function (pc
);
572 if (misc_function_vector
[miscfunc
].type
== mf_text
)
573 cache_pc_function_low
= misc_function_vector
[miscfunc
].address
;
575 /* It is a transfer table for Sun shared libraries. */
576 cache_pc_function_low
= pc
- FUNCTION_START_OFFSET
;
578 cache_pc_function_name
= misc_function_vector
[miscfunc
].name
;
579 if (miscfunc
< misc_function_count
/* && FIXME mf_text again? */ )
580 cache_pc_function_high
= misc_function_vector
[miscfunc
+1].address
;
582 cache_pc_function_high
= cache_pc_function_low
+ 1;
584 *address
= cache_pc_function_low
;
586 *name
= cache_pc_function_name
;
590 /* Find the misc function whose address is the largest
591 while being less than PC. Return its index in misc_function_vector.
592 Returns -1 if PC is not in suitable range. */
595 find_pc_misc_function (pc
)
596 register CORE_ADDR pc
;
599 register int hi
= misc_function_count
-1;
602 /* Note that the last thing in the vector is always _etext. */
603 /* Actually, "end", now that non-functions
604 go on the misc_function_vector. */
606 /* Above statement is not *always* true - fix for case where there are */
607 /* no misc functions at all (ie no symbol table has been read). */
608 if (hi
< 0) return -1; /* no misc functions recorded */
610 /* trivial reject range test */
611 if (pc
< misc_function_vector
[0].address
||
612 pc
> misc_function_vector
[hi
].address
)
615 /* Note that the following search will not return hi if
616 pc == misc_function_vector[hi].address. If "end" points to the
617 first unused location, this is correct and the above test
618 simply needs to be changed to
619 "pc >= misc_function_vector[hi].address". */
621 new = (lo
+ hi
) >> 1;
622 if (misc_function_vector
[new].address
== pc
)
623 return new; /* an exact match */
624 else if (misc_function_vector
[new].address
> pc
)
628 } while (hi
-lo
!= 1);
630 /* if here, we had no exact match, so return the lower choice */
634 /* Return the innermost stack frame executing inside of the specified block,
635 or zero if there is no such frame. */
638 block_innermost_frame (block
)
641 struct frame_info
*fi
;
642 register FRAME frame
;
643 register CORE_ADDR start
= BLOCK_START (block
);
644 register CORE_ADDR end
= BLOCK_END (block
);
649 frame
= get_prev_frame (frame
);
652 fi
= get_frame_info (frame
);
653 if (fi
->pc
>= start
&& fi
->pc
< end
)
659 _initialize_blockframe ()
661 obstack_init (&frame_cache_obstack
);