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. */
28 /* Start and end of object file containing the entry point.
29 STARTUP_FILE_END is the first address of the next file.
30 This file is assumed to be a startup file
31 and frames with pc's inside it
32 are treated as nonexistent.
34 Setting these variables is necessary so that backtraces do not fly off
35 the bottom of the stack. */
36 CORE_ADDR startup_file_start
;
37 CORE_ADDR startup_file_end
;
39 /* Is ADDR outside the startup file? */
41 outside_startup_file (addr
)
44 return !(addr
>= startup_file_start
&& addr
< startup_file_end
);
47 /* Address of innermost stack frame (contents of FP register) */
49 static FRAME current_frame
;
51 struct block
*block_for_pc ();
52 CORE_ADDR
get_pc_function_start ();
55 * Cache for frame addresses already read by gdb. Valid only while
56 * inferior is stopped. Control variables for the frame cache should
57 * be local to this module.
59 struct obstack frame_cache_obstack
;
61 /* Return the innermost (currently executing) stack frame. */
66 /* We assume its address is kept in a general register;
67 param.h says which register. */
73 set_current_frame (frame
)
76 current_frame
= frame
;
80 create_new_frame (addr
, pc
)
84 struct frame_info
*fci
; /* Same type as FRAME */
86 fci
= (struct frame_info
*)
87 obstack_alloc (&frame_cache_obstack
,
88 sizeof (struct frame_info
));
91 fci
->next
= (struct frame_info
*) 0;
92 fci
->prev
= (struct frame_info
*) 0;
94 fci
->next_frame
= 0; /* Since arbitrary */
97 #ifdef INIT_EXTRA_FRAME_INFO
98 INIT_EXTRA_FRAME_INFO (fci
);
104 /* Return the frame that called FRAME.
105 If FRAME is the original frame (it has no caller), return 0. */
108 get_prev_frame (frame
)
111 /* We're allowed to know that FRAME and "struct frame_info *" are
113 return get_prev_frame_info (frame
);
116 /* Return the frame that FRAME calls (0 if FRAME is the innermost
120 get_next_frame (frame
)
123 /* We're allowed to know that FRAME and "struct frame_info *" are
129 * Flush the entire frame cache.
132 flush_cached_frames ()
134 /* Since we can't really be sure what the first object allocated was */
135 obstack_free (&frame_cache_obstack
, 0);
136 obstack_init (&frame_cache_obstack
);
138 current_frame
= (struct frame_info
*) 0; /* Invalidate cache */
141 /* Return a structure containing various interesting information
142 about a specified stack frame. */
143 /* How do I justify including this function? Well, the FRAME
144 identifier format has gone through several changes recently, and
145 it's not completely inconceivable that it could happen again. If
146 it does, have this routine around will help */
149 get_frame_info (frame
)
155 /* If a machine allows frameless functions, it should define a macro
156 FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct
157 frame_info for the frame, and FRAMELESS should be set to nonzero
158 if it represents a frameless function invocation. */
160 /* Many machines which allow frameless functions can detect them using
161 this macro. Such machines should define FRAMELESS_FUNCTION_INVOCATION
162 to just call this macro. */
163 #define FRAMELESS_LOOK_FOR_PROLOGUE(FI, FRAMELESS) \
165 CORE_ADDR func_start, after_prologue; \
166 func_start = (get_pc_function_start ((FI)->pc) + \
167 FUNCTION_START_OFFSET); \
170 after_prologue = func_start; \
171 SKIP_PROLOGUE (after_prologue); \
172 (FRAMELESS) = (after_prologue == func_start); \
175 /* If we can't find the start of the function, we don't really */ \
176 /* know whether the function is frameless, but we should be */ \
177 /* able to get a reasonable (i.e. best we can do under the */ \
178 /* circumstances) backtrace by saying that it isn't. */ \
182 /* Return a structure containing various interesting information
183 about the frame that called NEXT_FRAME. Returns NULL
184 if there is no such frame. */
187 get_prev_frame_info (next_frame
)
191 struct frame_info
*prev
;
194 /* If the requested entry is in the cache, return it.
195 Otherwise, figure out what the address should be for the entry
196 we're about to add to the cache. */
202 if (!have_inferior_p () && !have_core_file_p ())
203 fatal ("get_prev_frame_info: Called before cache primed. \"Shouldn't happen.\"");
205 error ("No inferior or core file.");
208 return current_frame
;
211 /* If we have the prev one, return it */
212 if (next_frame
->prev
)
213 return next_frame
->prev
;
215 /* On some machines it is possible to call a function without
216 setting up a stack frame for it. On these machines, we
217 define this macro to take two args; a frameinfo pointer
218 identifying a frame and a variable to set or clear if it is
219 or isn't leafless. */
220 #ifdef FRAMELESS_FUNCTION_INVOCATION
221 /* Still don't want to worry about this except on the innermost
222 frame. This macro will set FROMLEAF if NEXT_FRAME is a
223 frameless function invocation. */
224 if (!(next_frame
->next
))
226 FRAMELESS_FUNCTION_INVOCATION (next_frame
, fromleaf
);
228 address
= next_frame
->frame
;
234 /* Two macros defined in param.h specify the machine-dependent
235 actions to be performed here.
236 First, get the frame's chain-pointer.
237 If that is zero, the frame is the outermost frame or a leaf
238 called by the outermost frame. This means that if start
239 calls main without a frame, we'll return 0 (which is fine
242 Nope; there's a problem. This also returns when the current
243 routine is a leaf of main. This is unacceptable. We move
244 this to after the ffi test; I'd rather have backtraces from
245 start go curfluy than have an abort called from main not show
247 address
= FRAME_CHAIN (next_frame
);
248 if (!FRAME_CHAIN_VALID (address
, next_frame
))
250 /* If this frame is a leaf, this will be superceeded by the
252 address
= FRAME_CHAIN_COMBINE (address
, next_frame
);
255 prev
= (struct frame_info
*)
256 obstack_alloc (&frame_cache_obstack
,
257 sizeof (struct frame_info
));
260 next_frame
->prev
= prev
;
261 prev
->next
= next_frame
;
262 prev
->prev
= (struct frame_info
*) 0;
263 prev
->frame
= address
;
264 prev
->next_frame
= prev
->next
? prev
->next
->frame
: 0;
266 #ifdef INIT_EXTRA_FRAME_INFO
267 INIT_EXTRA_FRAME_INFO(prev
);
270 /* This entry is in the frame queue now, which is good since
271 FRAME_SAVED_PC may use that queue to figure out it's value
272 (see m-sparc.h). We want the pc saved in the inferior frame. */
273 prev
->pc
= (fromleaf
? SAVED_PC_AFTER_CALL (next_frame
) :
274 next_frame
? FRAME_SAVED_PC (next_frame
) : read_pc ());
283 struct frame_info
*fi
;
284 fi
= get_frame_info (frame
);
288 /* Find the addresses in which registers are saved in FRAME. */
291 get_frame_saved_regs (frame_info_addr
, saved_regs_addr
)
292 struct frame_info
*frame_info_addr
;
293 struct frame_saved_regs
*saved_regs_addr
;
295 FRAME_FIND_SAVED_REGS (frame_info_addr
, *saved_regs_addr
);
298 /* Return the innermost lexical block in execution
299 in a specified stack frame. The frame address is assumed valid. */
302 get_frame_block (frame
)
305 struct frame_info
*fi
;
308 fi
= get_frame_info (frame
);
311 if (fi
->next_frame
!= 0)
312 /* We are not in the innermost frame. We need to subtract one to
313 get the correct block, in case the call instruction was the
314 last instruction of the block. If there are any machines on
315 which the saved pc does not point to after the call insn, we
316 probably want to make fi->pc point after the call insn anyway. */
318 return block_for_pc (pc
);
324 return block_for_pc (read_pc ());
328 get_pc_function_start (pc
)
331 register struct block
*bl
= block_for_pc (pc
);
332 register struct symbol
*symbol
;
333 if (bl
== 0 || (symbol
= block_function (bl
)) == 0)
335 register int misc_index
= find_pc_misc_function (pc
);
337 return misc_function_vector
[misc_index
].address
;
340 bl
= SYMBOL_BLOCK_VALUE (symbol
);
341 return BLOCK_START (bl
);
344 /* Return the symbol for the function executing in frame FRAME. */
347 get_frame_function (frame
)
350 register struct block
*bl
= get_frame_block (frame
);
353 return block_function (bl
);
356 /* Return the innermost lexical block containing the specified pc value,
357 or 0 if there is none. */
359 extern struct symtab
*psymtab_to_symtab ();
363 register CORE_ADDR pc
;
365 register struct block
*b
;
366 register int bot
, top
, half
;
367 register struct symtab
*s
;
368 register struct partial_symtab
*ps
;
369 struct blockvector
*bl
;
371 /* First search all symtabs for one whose file contains our pc */
373 for (s
= symtab_list
; s
; s
= s
->next
)
375 bl
= BLOCKVECTOR (s
);
376 b
= BLOCKVECTOR_BLOCK (bl
, 0);
377 if (BLOCK_START (b
) <= pc
378 && BLOCK_END (b
) > pc
)
383 for (ps
= partial_symtab_list
; ps
; ps
= ps
->next
)
385 if (ps
->textlow
<= pc
386 && ps
->texthigh
> pc
)
389 fatal ("Internal error: pc found in readin psymtab and not in any symtab.");
390 s
= psymtab_to_symtab (ps
);
391 bl
= BLOCKVECTOR (s
);
392 b
= BLOCKVECTOR_BLOCK (bl
, 0);
400 /* Then search that symtab for the smallest block that wins. */
401 /* Use binary search to find the last block that starts before PC. */
404 top
= BLOCKVECTOR_NBLOCKS (bl
);
406 while (top
- bot
> 1)
408 half
= (top
- bot
+ 1) >> 1;
409 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
410 if (BLOCK_START (b
) <= pc
)
416 /* Now search backward for a block that ends after PC. */
420 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
421 if (BLOCK_END (b
) > pc
)
429 /* Return the function containing pc value PC.
430 Returns 0 if function is not known. */
433 find_pc_function (pc
)
436 register struct block
*b
= block_for_pc (pc
);
439 return block_function (b
);
442 /* Finds the "function" (text symbol) that is smaller than PC
443 but greatest of all of the potential text symbols. Sets
444 *NAME and/or *ADDRESS conditionally if that pointer is non-zero.
445 Returns 0 if it couldn't find anything, 1 if it did. On a zero
446 return, *NAME and *ADDRESS are always set to zero. On a 1 return,
447 *NAME and *ADDRESS contain real information. */
450 find_pc_partial_function (pc
, name
, address
)
455 struct partial_symtab
*pst
= find_pc_psymtab (pc
);
458 struct partial_symbol
*psb
;
464 /* The information we want has already been read in.
465 We can go to the already readin symbols and we'll get
466 the best possible answer. */
467 f
= find_pc_function (pc
);
471 /* No availible symbol. */
480 *name
= SYMBOL_NAME (f
);
482 *address
= BLOCK_START (SYMBOL_BLOCK_VALUE (f
));
486 /* Get the information from a combination of the pst
487 (static symbols), and the misc function vector (extern
489 miscfunc
= find_pc_misc_function (pc
);
490 psb
= find_pc_psymbol (pst
, pc
);
492 if (!psb
&& miscfunc
== -1)
498 && (SYMBOL_VALUE(psb
)
499 < misc_function_vector
[miscfunc
].address
)))
502 *address
= misc_function_vector
[miscfunc
].address
;
504 *name
= misc_function_vector
[miscfunc
].name
;
510 *address
= SYMBOL_VALUE (psb
);
512 *name
= SYMBOL_NAME (psb
);
517 /* Must be in the misc function stuff. */
519 miscfunc
= find_pc_misc_function (pc
);
523 *address
= misc_function_vector
[miscfunc
].address
;
525 *name
= misc_function_vector
[miscfunc
].name
;
530 /* Find the misc function whose address is the largest
531 while being less than PC. Return its index in misc_function_vector.
532 Returns -1 if PC is not in suitable range. */
535 find_pc_misc_function (pc
)
536 register CORE_ADDR pc
;
539 register int hi
= misc_function_count
-1;
541 register int distance
;
543 /* Note that the last thing in the vector is always _etext. */
544 /* Actually, "end", now that non-functions
545 go on the misc_function_vector. */
547 /* Above statement is not *always* true - fix for case where there are */
548 /* no misc functions at all (ie no symbol table has been read). */
549 if (hi
< 0) return -1; /* no misc functions recorded */
551 /* trivial reject range test */
552 if (pc
< misc_function_vector
[0].address
||
553 pc
> misc_function_vector
[hi
].address
)
556 /* Note that the following search will not return hi if
557 pc == misc_function_vector[hi].address. If "end" points to the
558 first unused location, this is correct and the above test
559 simply needs to be changed to
560 "pc >= misc_function_vector[hi].address". */
562 new = (lo
+ hi
) >> 1;
563 distance
= misc_function_vector
[new].address
- pc
;
565 return new; /* an exact match */
566 else if (distance
> 0)
570 } while (hi
-lo
!= 1);
572 /* if here, we had no exact match, so return the lower choice */
576 /* Return the innermost stack frame executing inside of the specified block,
577 or zero if there is no such frame. */
580 block_innermost_frame (block
)
583 struct frame_info
*fi
;
584 register FRAME frame
;
585 register CORE_ADDR start
= BLOCK_START (block
);
586 register CORE_ADDR end
= BLOCK_END (block
);
591 frame
= get_prev_frame (frame
);
594 fi
= get_frame_info (frame
);
595 if (fi
->pc
>= start
&& fi
->pc
< end
)
601 _initialize_blockframe ()
603 obstack_init (&frame_cache_obstack
);