1 /* Get info from stack frames;
2 convert between frames, blocks, functions and pc values.
3 Copyright 1986, 1987, 1988, 1989, 1991, 1994, 1995, 1996
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #include "value.h" /* for read_register */
30 #include "target.h" /* for target_has_stack */
31 #include "inferior.h" /* for read_pc */
34 /* Is ADDR inside the startup file? Note that if your machine
35 has a way to detect the bottom of the stack, there is no need
36 to call this function from FRAME_CHAIN_VALID; the reason for
37 doing so is that some machines have no way of detecting bottom
40 A PC of zero is always considered to be the bottom of the stack. */
43 inside_entry_file (addr
)
48 if (symfile_objfile
== 0)
50 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
51 /* Do not stop backtracing if the pc is in the call dummy
52 at the entry point. */
53 /* FIXME: Won't always work with zeros for the last two arguments */
54 if (PC_IN_CALL_DUMMY (addr
, 0, 0))
57 return (addr
>= symfile_objfile
-> ei
.entry_file_lowpc
&&
58 addr
< symfile_objfile
-> ei
.entry_file_highpc
);
61 /* Test a specified PC value to see if it is in the range of addresses
62 that correspond to the main() function. See comments above for why
63 we might want to do this.
65 Typically called from FRAME_CHAIN_VALID.
67 A PC of zero is always considered to be the bottom of the stack. */
75 if (symfile_objfile
== 0)
78 /* If the addr range is not set up at symbol reading time, set it up now.
79 This is for FRAME_CHAIN_VALID_ALTERNATE. I do this for coff, because
80 it is unable to set it up and symbol reading time. */
82 if (symfile_objfile
-> ei
.main_func_lowpc
== INVALID_ENTRY_LOWPC
&&
83 symfile_objfile
-> ei
.main_func_highpc
== INVALID_ENTRY_HIGHPC
)
85 struct symbol
*mainsym
;
87 mainsym
= lookup_symbol ("main", NULL
, VAR_NAMESPACE
, NULL
, NULL
);
88 if (mainsym
&& SYMBOL_CLASS(mainsym
) == LOC_BLOCK
)
90 symfile_objfile
->ei
.main_func_lowpc
= BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym
));
91 symfile_objfile
->ei
.main_func_highpc
= BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym
));
94 return (symfile_objfile
-> ei
.main_func_lowpc
<= pc
&&
95 symfile_objfile
-> ei
.main_func_highpc
> pc
);
98 /* Test a specified PC value to see if it is in the range of addresses
99 that correspond to the process entry point function. See comments
100 in objfiles.h for why we might want to do this.
102 Typically called from FRAME_CHAIN_VALID.
104 A PC of zero is always considered to be the bottom of the stack. */
107 inside_entry_func (pc
)
112 if (symfile_objfile
== 0)
114 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
115 /* Do not stop backtracing if the pc is in the call dummy
116 at the entry point. */
117 /* FIXME: Won't always work with zeros for the last two arguments */
118 if (PC_IN_CALL_DUMMY (pc
, 0, 0))
121 return (symfile_objfile
-> ei
.entry_func_lowpc
<= pc
&&
122 symfile_objfile
-> ei
.entry_func_highpc
> pc
);
125 /* Info about the innermost stack frame (contents of FP register) */
127 static struct frame_info
*current_frame
;
129 /* Cache for frame addresses already read by gdb. Valid only while
130 inferior is stopped. Control variables for the frame cache should
131 be local to this module. */
133 struct obstack frame_cache_obstack
;
135 /* Return the innermost (currently executing) stack frame. */
140 if (current_frame
== NULL
)
142 if (target_has_stack
)
143 current_frame
= create_new_frame (read_fp (), read_pc ());
147 return current_frame
;
151 set_current_frame (frame
)
152 struct frame_info
*frame
;
154 current_frame
= frame
;
157 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
158 Always returns a non-NULL value. */
161 create_new_frame (addr
, pc
)
165 struct frame_info
*fi
;
168 fi
= (struct frame_info
*)
169 obstack_alloc (&frame_cache_obstack
,
170 sizeof (struct frame_info
));
172 /* Arbitrary frame */
177 find_pc_partial_function (pc
, &name
, (CORE_ADDR
*)NULL
,(CORE_ADDR
*)NULL
);
178 fi
->signal_handler_caller
= IN_SIGTRAMP (fi
->pc
, name
);
180 #ifdef INIT_EXTRA_FRAME_INFO
181 INIT_EXTRA_FRAME_INFO (0, fi
);
187 /* Return the frame that called FI.
188 If FI is the original frame (it has no caller), return 0. */
191 get_prev_frame (frame
)
192 struct frame_info
*frame
;
194 return get_prev_frame_info (frame
);
197 /* Return the frame that FRAME calls (NULL if FRAME is the innermost
201 get_next_frame (frame
)
202 struct frame_info
*frame
;
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
= NULL
; /* Invalidate cache */
217 select_frame (NULL
, -1);
218 annotate_frames_invalid ();
221 /* Flush the frame cache, and start a new one if necessary. */
224 reinit_frame_cache ()
226 flush_cached_frames ();
228 /* FIXME: The inferior_pid test is wrong if there is a corefile. */
229 if (inferior_pid
!= 0)
231 select_frame (get_current_frame (), 0);
235 /* If a machine allows frameless functions, it should define a macro
236 FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h. FI is the struct
237 frame_info for the frame, and FRAMELESS should be set to nonzero
238 if it represents a frameless function invocation. */
240 /* Return nonzero if the function for this frame lacks a prologue. Many
241 machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
245 frameless_look_for_prologue (frame
)
246 struct frame_info
*frame
;
248 CORE_ADDR func_start
, after_prologue
;
249 func_start
= get_pc_function_start (frame
->pc
);
252 func_start
+= FUNCTION_START_OFFSET
;
253 after_prologue
= func_start
;
254 #ifdef SKIP_PROLOGUE_FRAMELESS_P
255 /* This is faster, since only care whether there *is* a prologue,
256 not how long it is. */
257 SKIP_PROLOGUE_FRAMELESS_P (after_prologue
);
259 SKIP_PROLOGUE (after_prologue
);
261 return after_prologue
== func_start
;
264 /* If we can't find the start of the function, we don't really
265 know whether the function is frameless, but we should be able
266 to get a reasonable (i.e. best we can do under the
267 circumstances) backtrace by saying that it isn't. */
271 /* Default a few macros that people seldom redefine. */
273 #if !defined (INIT_FRAME_PC)
274 #define INIT_FRAME_PC(fromleaf, prev) \
275 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
276 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
279 #ifndef FRAME_CHAIN_COMBINE
280 #define FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
283 /* Return a structure containing various interesting information
284 about the frame that called NEXT_FRAME. Returns NULL
285 if there is no such frame. */
288 get_prev_frame_info (next_frame
)
289 struct frame_info
*next_frame
;
291 CORE_ADDR address
= 0;
292 struct frame_info
*prev
;
296 /* If the requested entry is in the cache, return it.
297 Otherwise, figure out what the address should be for the entry
298 we're about to add to the cache. */
303 /* This screws value_of_variable, which just wants a nice clean
304 NULL return from block_innermost_frame if there are no frames.
305 I don't think I've ever seen this message happen otherwise.
306 And returning NULL here is a perfectly legitimate thing to do. */
309 error ("You haven't set up a process's stack to examine.");
313 return current_frame
;
316 /* If we have the prev one, return it */
317 if (next_frame
->prev
)
318 return next_frame
->prev
;
320 /* On some machines it is possible to call a function without
321 setting up a stack frame for it. On these machines, we
322 define this macro to take two args; a frameinfo pointer
323 identifying a frame and a variable to set or clear if it is
324 or isn't leafless. */
325 #ifdef FRAMELESS_FUNCTION_INVOCATION
326 /* Still don't want to worry about this except on the innermost
327 frame. This macro will set FROMLEAF if NEXT_FRAME is a
328 frameless function invocation. */
329 if (!(next_frame
->next
))
331 FRAMELESS_FUNCTION_INVOCATION (next_frame
, fromleaf
);
333 address
= FRAME_FP (next_frame
);
339 /* Two macros defined in tm.h specify the machine-dependent
340 actions to be performed here.
341 First, get the frame's chain-pointer.
342 If that is zero, the frame is the outermost frame or a leaf
343 called by the outermost frame. This means that if start
344 calls main without a frame, we'll return 0 (which is fine
347 Nope; there's a problem. This also returns when the current
348 routine is a leaf of main. This is unacceptable. We move
349 this to after the ffi test; I'd rather have backtraces from
350 start go curfluy than have an abort called from main not show
352 address
= FRAME_CHAIN (next_frame
);
353 if (!FRAME_CHAIN_VALID (address
, next_frame
))
355 address
= FRAME_CHAIN_COMBINE (address
, next_frame
);
360 prev
= (struct frame_info
*)
361 obstack_alloc (&frame_cache_obstack
,
362 sizeof (struct frame_info
));
365 next_frame
->prev
= prev
;
366 prev
->next
= next_frame
;
367 prev
->prev
= (struct frame_info
*) 0;
368 prev
->frame
= address
;
369 prev
->signal_handler_caller
= 0;
371 /* This change should not be needed, FIXME! We should
372 determine whether any targets *need* INIT_FRAME_PC to happen
373 after INIT_EXTRA_FRAME_INFO and come up with a simple way to
374 express what goes on here.
376 INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
377 (where the PC is already set up) and here (where it isn't).
378 INIT_FRAME_PC is only called from here, always after
379 INIT_EXTRA_FRAME_INFO.
381 The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
382 value (which hasn't been set yet). Some other machines appear to
383 require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC. Phoo.
385 We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
386 an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92.
388 Assuming that some machines need INIT_FRAME_PC after
389 INIT_EXTRA_FRAME_INFO, one possible scheme:
391 SETUP_INNERMOST_FRAME()
392 Default version is just create_new_frame (read_fp ()),
393 read_pc ()). Machines with extra frame info would do that (or the
394 local equivalent) and then set the extra fields.
395 SETUP_ARBITRARY_FRAME(argc, argv)
396 Only change here is that create_new_frame would no longer init extra
397 frame info; SETUP_ARBITRARY_FRAME would have to do that.
398 INIT_PREV_FRAME(fromleaf, prev)
399 Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC. This should
400 also return a flag saying whether to keep the new frame, or
401 whether to discard it, because on some machines (e.g. mips) it
402 is really awkward to have FRAME_CHAIN_VALID called *before*
403 INIT_EXTRA_FRAME_INFO (there is no good way to get information
404 deduced in FRAME_CHAIN_VALID into the extra fields of the new frame).
405 std_frame_pc(fromleaf, prev)
406 This is the default setting for INIT_PREV_FRAME. It just does what
407 the default INIT_FRAME_PC does. Some machines will call it from
408 INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
409 Some machines won't use it.
410 kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */
412 #ifdef INIT_FRAME_PC_FIRST
413 INIT_FRAME_PC_FIRST (fromleaf
, prev
);
416 #ifdef INIT_EXTRA_FRAME_INFO
417 INIT_EXTRA_FRAME_INFO(fromleaf
, prev
);
420 /* This entry is in the frame queue now, which is good since
421 FRAME_SAVED_PC may use that queue to figure out its value
422 (see tm-sparc.h). We want the pc saved in the inferior frame. */
423 INIT_FRAME_PC(fromleaf
, prev
);
425 /* If ->frame and ->pc are unchanged, we are in the process of getting
426 ourselves into an infinite backtrace. Some architectures check this
427 in FRAME_CHAIN or thereabouts, but it seems like there is no reason
428 this can't be an architecture-independent check. */
429 if (next_frame
!= NULL
)
431 if (prev
->frame
== next_frame
->frame
432 && prev
->pc
== next_frame
->pc
)
434 next_frame
->prev
= NULL
;
435 obstack_free (&frame_cache_obstack
, prev
);
440 find_pc_partial_function (prev
->pc
, &name
,
441 (CORE_ADDR
*)NULL
,(CORE_ADDR
*)NULL
);
442 if (IN_SIGTRAMP (prev
->pc
, name
))
443 prev
->signal_handler_caller
= 1;
450 struct frame_info
*frame
;
455 #if defined (FRAME_FIND_SAVED_REGS)
456 /* Find the addresses in which registers are saved in FRAME. */
459 get_frame_saved_regs (frame
, saved_regs_addr
)
460 struct frame_info
*frame
;
461 struct frame_saved_regs
*saved_regs_addr
;
463 FRAME_FIND_SAVED_REGS (frame
, *saved_regs_addr
);
467 /* Return the innermost lexical block in execution
468 in a specified stack frame. The frame address is assumed valid. */
471 get_frame_block (frame
)
472 struct frame_info
*frame
;
477 if (frame
->next
!= 0 && frame
->next
->signal_handler_caller
== 0)
478 /* We are not in the innermost frame and we were not interrupted
479 by a signal. We need to subtract one to get the correct block,
480 in case the call instruction was the last instruction of the block.
481 If there are any machines on which the saved pc does not point to
482 after the call insn, we probably want to make frame->pc point after
483 the call insn anyway. */
485 return block_for_pc (pc
);
491 return block_for_pc (read_pc ());
495 get_pc_function_start (pc
)
498 register struct block
*bl
;
499 register struct symbol
*symbol
;
500 register struct minimal_symbol
*msymbol
;
503 if ((bl
= block_for_pc (pc
)) != NULL
&&
504 (symbol
= block_function (bl
)) != NULL
)
506 bl
= SYMBOL_BLOCK_VALUE (symbol
);
507 fstart
= BLOCK_START (bl
);
509 else if ((msymbol
= lookup_minimal_symbol_by_pc (pc
)) != NULL
)
511 fstart
= SYMBOL_VALUE_ADDRESS (msymbol
);
520 /* Return the symbol for the function executing in frame FRAME. */
523 get_frame_function (frame
)
524 struct frame_info
*frame
;
526 register struct block
*bl
= get_frame_block (frame
);
529 return block_function (bl
);
532 /* Return the blockvector immediately containing the innermost lexical block
533 containing the specified pc value, or 0 if there is none.
534 PINDEX is a pointer to the index value of the block. If PINDEX
535 is NULL, we don't pass this information back to the caller. */
538 blockvector_for_pc (pc
, pindex
)
539 register CORE_ADDR pc
;
542 register struct block
*b
;
543 register int bot
, top
, half
;
544 register struct symtab
*s
;
545 struct blockvector
*bl
;
547 /* First search all symtabs for one whose file contains our pc */
548 s
= find_pc_symtab (pc
);
552 bl
= BLOCKVECTOR (s
);
553 b
= BLOCKVECTOR_BLOCK (bl
, 0);
555 /* Then search that symtab for the smallest block that wins. */
556 /* Use binary search to find the last block that starts before PC. */
559 top
= BLOCKVECTOR_NBLOCKS (bl
);
561 while (top
- bot
> 1)
563 half
= (top
- bot
+ 1) >> 1;
564 b
= BLOCKVECTOR_BLOCK (bl
, bot
+ half
);
565 if (BLOCK_START (b
) <= pc
)
571 /* Now search backward for a block that ends after PC. */
575 b
= BLOCKVECTOR_BLOCK (bl
, bot
);
576 if (BLOCK_END (b
) > pc
)
588 /* Return the innermost lexical block containing the specified pc value,
589 or 0 if there is none. */
593 register CORE_ADDR pc
;
595 register struct blockvector
*bl
;
598 bl
= blockvector_for_pc (pc
, &index
);
600 return BLOCKVECTOR_BLOCK (bl
, index
);
604 /* Return the function containing pc value PC.
605 Returns 0 if function is not known. */
608 find_pc_function (pc
)
611 register struct block
*b
= block_for_pc (pc
);
614 return block_function (b
);
617 /* These variables are used to cache the most recent result
618 * of find_pc_partial_function. */
620 static CORE_ADDR cache_pc_function_low
= 0;
621 static CORE_ADDR cache_pc_function_high
= 0;
622 static char *cache_pc_function_name
= 0;
624 /* Clear cache, e.g. when symbol table is discarded. */
627 clear_pc_function_cache()
629 cache_pc_function_low
= 0;
630 cache_pc_function_high
= 0;
631 cache_pc_function_name
= (char *)0;
634 /* Finds the "function" (text symbol) that is smaller than PC but
635 greatest of all of the potential text symbols. Sets *NAME and/or
636 *ADDRESS conditionally if that pointer is non-null. If ENDADDR is
637 non-null, then set *ENDADDR to be the end of the function
638 (exclusive), but passing ENDADDR as non-null means that the
639 function might cause symbols to be read. This function either
640 succeeds or fails (not halfway succeeds). If it succeeds, it sets
641 *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
642 If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero
646 find_pc_partial_function (pc
, name
, address
, endaddr
)
652 struct partial_symtab
*pst
;
654 struct minimal_symbol
*msymbol
;
655 struct partial_symbol
*psb
;
656 struct obj_section
*sec
;
658 if (pc
>= cache_pc_function_low
&& pc
< cache_pc_function_high
)
659 goto return_cached_value
;
661 /* If sigtramp is in the u area, it counts as a function (especially
662 important for step_1). */
663 #if defined SIGTRAMP_START
664 if (IN_SIGTRAMP (pc
, (char *)NULL
))
666 cache_pc_function_low
= SIGTRAMP_START (pc
);
667 cache_pc_function_high
= SIGTRAMP_END (pc
);
668 cache_pc_function_name
= "<sigtramp>";
670 goto return_cached_value
;
674 msymbol
= lookup_minimal_symbol_by_pc (pc
);
675 pst
= find_pc_psymtab (pc
);
678 /* Need to read the symbols to get a good value for the end address. */
679 if (endaddr
!= NULL
&& !pst
->readin
)
681 /* Need to get the terminal in case symbol-reading produces
683 target_terminal_ours_for_output ();
684 PSYMTAB_TO_SYMTAB (pst
);
689 /* Checking whether the msymbol has a larger value is for the
690 "pathological" case mentioned in print_frame_info. */
691 f
= find_pc_function (pc
);
694 || (BLOCK_START (SYMBOL_BLOCK_VALUE (f
))
695 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
697 cache_pc_function_low
= BLOCK_START (SYMBOL_BLOCK_VALUE (f
));
698 cache_pc_function_high
= BLOCK_END (SYMBOL_BLOCK_VALUE (f
));
699 cache_pc_function_name
= SYMBOL_NAME (f
);
700 goto return_cached_value
;
705 /* Now that static symbols go in the minimal symbol table, perhaps
706 we could just ignore the partial symbols. But at least for now
707 we use the partial or minimal symbol, whichever is larger. */
708 psb
= find_pc_psymbol (pst
, pc
);
711 && (msymbol
== NULL
||
712 (SYMBOL_VALUE_ADDRESS (psb
)
713 >= SYMBOL_VALUE_ADDRESS (msymbol
))))
715 /* This case isn't being cached currently. */
717 *address
= SYMBOL_VALUE_ADDRESS (psb
);
719 *name
= SYMBOL_NAME (psb
);
720 /* endaddr non-NULL can't happen here. */
726 /* Not in the normal symbol tables, see if the pc is in a known section.
727 If it's not, then give up. This ensures that anything beyond the end
728 of the text seg doesn't appear to be part of the last function in the
731 sec
= find_pc_section (pc
);
736 /* Must be in the minimal symbol table. */
739 /* No available symbol. */
749 cache_pc_function_low
= SYMBOL_VALUE_ADDRESS (msymbol
);
750 cache_pc_function_name
= SYMBOL_NAME (msymbol
);
752 /* Use the lesser of the next minimal symbol, or the end of the section, as
753 the end of the function. */
755 if (SYMBOL_NAME (msymbol
+ 1) != NULL
756 && SYMBOL_VALUE_ADDRESS (msymbol
+ 1) < sec
->endaddr
)
757 cache_pc_function_high
= SYMBOL_VALUE_ADDRESS (msymbol
+ 1);
759 /* We got the start address from the last msymbol in the objfile.
760 So the end address is the end of the section. */
761 cache_pc_function_high
= sec
->endaddr
;
765 *address
= cache_pc_function_low
;
767 *name
= cache_pc_function_name
;
769 *endaddr
= cache_pc_function_high
;
773 /* Return the innermost stack frame executing inside of BLOCK,
774 or NULL if there is no such frame. If BLOCK is NULL, just return NULL. */
777 block_innermost_frame (block
)
780 struct frame_info
*frame
;
781 register CORE_ADDR start
;
782 register CORE_ADDR end
;
787 start
= BLOCK_START (block
);
788 end
= BLOCK_END (block
);
793 frame
= get_prev_frame (frame
);
796 if (frame
->pc
>= start
&& frame
->pc
< end
)
801 /* Return the full FRAME which corresponds to the given CORE_ADDR
802 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
805 find_frame_addr_in_frame_chain (frame_addr
)
806 CORE_ADDR frame_addr
;
808 struct frame_info
*frame
= NULL
;
810 if (frame_addr
== (CORE_ADDR
)0)
815 frame
= get_prev_frame (frame
);
818 if (FRAME_FP (frame
) == frame_addr
)
823 #ifdef SIGCONTEXT_PC_OFFSET
824 /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp. */
827 sigtramp_saved_pc (frame
)
828 struct frame_info
*frame
;
830 CORE_ADDR sigcontext_addr
;
831 char buf
[TARGET_PTR_BIT
/ TARGET_CHAR_BIT
];
832 int ptrbytes
= TARGET_PTR_BIT
/ TARGET_CHAR_BIT
;
833 int sigcontext_offs
= (2 * TARGET_INT_BIT
) / TARGET_CHAR_BIT
;
835 /* Get sigcontext address, it is the third parameter on the stack. */
837 sigcontext_addr
= read_memory_integer (FRAME_ARGS_ADDRESS (frame
->next
)
842 sigcontext_addr
= read_memory_integer (read_register (SP_REGNUM
)
846 /* Don't cause a memory_error when accessing sigcontext in case the stack
847 layout has changed or the stack is corrupt. */
848 target_read_memory (sigcontext_addr
+ SIGCONTEXT_PC_OFFSET
, buf
, ptrbytes
);
849 return extract_unsigned_integer (buf
, ptrbytes
);
851 #endif /* SIGCONTEXT_PC_OFFSET */
856 * The following code serves to maintain the dummy stack frames for
857 * inferior function calls (ie. when gdb calls into the inferior via
858 * call_function_by_hand). This code saves the machine state before
859 * the call in host memory, so it must maintain an independant stack
860 * and keep it consistant etc. I am attempting to make this code
861 * generic enough to be used by many targets.
863 * The cheapest and most generic way to do CALL_DUMMY on a new target
864 * is probably to define CALL_DUMMY to be empty, CALL_DUMMY_LENGTH to zero,
865 * and CALL_DUMMY_LOCATION to AT_ENTRY. Then you must remember to define
866 * PUSH_RETURN_ADDRESS, because there won't be a call instruction to do it.
869 static struct dummy_frame
*dummy_frame_stack
= NULL
;
871 /* Function: find_dummy_frame(pc, fp, sp)
872 Search the stack of dummy frames for one matching the given PC, FP and SP.
873 This is the work-horse for pc_in_call_dummy and read_register_dummy */
876 generic_find_dummy_frame (pc
, fp
)
880 struct dummy_frame
* dummyframe
;
881 #ifdef NEED_TEXT_START_END
882 CORE_ADDR bkpt_address
;
883 extern CORE_ADDR text_end
;
886 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
887 if (pc
!= entry_point_address ())
889 #endif /* AT_ENTRY_POINT */
891 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
892 bkpt_address
= text_end
- CALL_DUMMY_LENGTH
+ CALL_DUMMY_BREAKPOINT_OFFSET
;
893 if (pc
!= bkpt_address
)
895 #endif /* BEFORE_TEXT_END */
897 #if CALL_DUMMY_LOCATION == AFTER_TEXT_END
898 bkpt_address
= text_end
+ CALL_DUMMY_BREAKPOINT_OFFSET
;
899 if (pc
!= bkpt_address
)
901 #endif /* AFTER_TEXT_END */
903 #if CALL_DUMMY_LOCATION == ON_STACK
904 /* compute the displacement from the CALL_DUMMY breakpoint
905 to the frame pointer */
907 pc
+= CALL_DUMMY_LENGTH
- CALL_DUMMY_BREAKPOINT_OFFSET
;
909 pc
+= CALL_DUMMY_BREAKPOINT_OFFSET
;
910 #endif /* ON_STACK */
912 for (dummyframe
= dummy_frame_stack
; dummyframe
!= NULL
;
913 dummyframe
= dummyframe
->next
)
914 if (fp
== dummyframe
->fp
|| fp
== dummyframe
->sp
)
916 /* The frame in question lies between the saved fp and sp, inclusive */
917 #if CALL_DUMMY_LOCATION == ON_STACK
918 /* NOTE: a better way to do this might be simply to test whether
919 the pc lies between the saved (sp, fp) and CALL_DUMMY_LENGTH.
922 if (pc
== dummyframe
->fp
|| pc
== dummyframe
->sp
)
923 #endif /* ON_STACK */
924 return dummyframe
->regs
;
929 /* Function: pc_in_call_dummy (pc, fp)
930 Return true if this is a dummy frame created by gdb for an inferior call */
933 generic_pc_in_call_dummy (pc
, fp
)
937 /* if find_dummy_frame succeeds, then PC is in a call dummy */
938 return (generic_find_dummy_frame (pc
, fp
) != 0);
941 /* Function: read_register_dummy
942 Find a saved register from before GDB calls a function in the inferior */
945 generic_read_register_dummy (pc
, fp
, regno
)
950 char *dummy_regs
= generic_find_dummy_frame (pc
, fp
);
953 return extract_address (&dummy_regs
[REGISTER_BYTE (regno
)],
954 REGISTER_RAW_SIZE(regno
));
959 /* Save all the registers on the dummy frame stack. Most ports save the
960 registers on the target stack. This results in lots of unnecessary memory
961 references, which are slow when debugging via a serial line. Instead, we
962 save all the registers internally, and never write them to the stack. The
963 registers get restored when the called function returns to the entry point,
964 where a breakpoint is laying in wait. */
967 generic_push_dummy_frame ()
969 struct dummy_frame
*dummy_frame
;
970 CORE_ADDR fp
= (get_current_frame ())->frame
;
972 /* check to see if there are stale dummy frames,
973 perhaps left over from when a longjump took us out of a
974 function that was called by the debugger */
976 dummy_frame
= dummy_frame_stack
;
978 if (dummy_frame
->fp INNER_THAN fp
) /* stale -- destroy! */
980 dummy_frame_stack
= dummy_frame
->next
;
982 dummy_frame
= dummy_frame_stack
;
985 dummy_frame
= dummy_frame
->next
;
987 dummy_frame
= xmalloc (sizeof (struct dummy_frame
));
988 dummy_frame
->pc
= read_register (PC_REGNUM
);
989 dummy_frame
->sp
= read_register (SP_REGNUM
);
990 dummy_frame
->fp
= fp
;
991 read_register_bytes (0, dummy_frame
->regs
, REGISTER_BYTES
);
992 dummy_frame
->next
= dummy_frame_stack
;
993 dummy_frame_stack
= dummy_frame
;
996 /* Function: pop_dummy_frame
997 Restore the machine state from a saved dummy stack frame. */
1000 generic_pop_dummy_frame ()
1002 struct dummy_frame
*dummy_frame
= dummy_frame_stack
;
1004 /* FIXME: what if the first frame isn't the right one, eg..
1005 because one call-by-hand function has done a longjmp into another one? */
1008 error ("Can't pop dummy frame!");
1009 dummy_frame_stack
= dummy_frame
->next
;
1010 write_register_bytes (0, dummy_frame
->regs
, REGISTER_BYTES
);
1014 /* Function: frame_chain_valid
1015 Returns true for a user frame or a call_function_by_hand dummy frame,
1016 and false for the CRT0 start-up frame. Purpose is to terminate backtrace */
1019 generic_frame_chain_valid (fp
, fi
)
1021 struct frame_info
*fi
;
1023 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
1024 if (PC_IN_CALL_DUMMY(FRAME_SAVED_PC(fi
), fp
, fp
))
1025 return 1; /* don't prune CALL_DUMMY frames */
1026 else /* fall back to default algorithm (see frame.h) */
1028 return (fp
!= 0 && !inside_entry_file (FRAME_SAVED_PC(fi
)));
1031 /* Function: get_saved_register
1032 Find register number REGNUM relative to FRAME and put its (raw,
1033 target format) contents in *RAW_BUFFER.
1035 Set *OPTIMIZED if the variable was optimized out (and thus can't be
1036 fetched). Note that this is never set to anything other than zero
1037 in this implementation.
1039 Set *LVAL to lval_memory, lval_register, or not_lval, depending on
1040 whether the value was fetched from memory, from a register, or in a
1041 strange and non-modifiable way (e.g. a frame pointer which was
1042 calculated rather than fetched). We will use not_lval for values
1043 fetched from generic dummy frames.
1045 Set *ADDRP to the address, either in memory on as a REGISTER_BYTE
1046 offset into the registers array. If the value is stored in a dummy
1047 frame, set *ADDRP to zero.
1049 To use this implementation, define a function called
1050 "get_saved_register" in your target code, which simply passes all
1051 of its arguments to this function.
1053 The argument RAW_BUFFER must point to aligned memory. */
1056 generic_get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
1060 struct frame_info
*frame
;
1062 enum lval_type
*lval
;
1065 struct frame_saved_regs fsr
;
1067 if (!target_has_registers
)
1068 error ("No registers.");
1070 /* Normal systems don't optimize out things with register numbers. */
1071 if (optimized
!= NULL
)
1074 if (addrp
) /* default assumption: not found in memory */
1077 /* Note: since the current frame's registers could only have been
1078 saved by frames INTERIOR TO the current frame, we skip examining
1079 the current frame itself: otherwise, we would be getting the
1080 previous frame's registers which were saved by the current frame. */
1082 while ((frame
= frame
->next
) != NULL
)
1084 if (PC_IN_CALL_DUMMY (frame
->pc
, frame
->frame
, frame
->frame
))
1086 if (lval
) /* found it in a CALL_DUMMY frame */
1090 generic_find_dummy_frame (frame
->pc
, frame
->frame
) +
1091 REGISTER_BYTE (regnum
),
1092 REGISTER_RAW_SIZE (regnum
));
1096 FRAME_FIND_SAVED_REGS(frame
, fsr
);
1097 if (fsr
.regs
[regnum
] != 0)
1099 if (lval
) /* found it saved on the stack */
1100 *lval
= lval_memory
;
1101 if (regnum
== SP_REGNUM
)
1103 if (raw_buffer
) /* SP register treated specially */
1104 store_address (raw_buffer
, REGISTER_RAW_SIZE (regnum
),
1109 if (addrp
) /* any other register */
1110 *addrp
= fsr
.regs
[regnum
];
1112 read_memory (fsr
.regs
[regnum
], raw_buffer
,
1113 REGISTER_RAW_SIZE (regnum
));
1119 /* If we get thru the loop to this point, it means the register was
1120 not saved in any frame. Return the actual live-register value. */
1122 if (lval
) /* found it in a live register */
1123 *lval
= lval_register
;
1125 *addrp
= REGISTER_BYTE (regnum
);
1127 read_register_gen (regnum
, raw_buffer
);
1131 _initialize_blockframe ()
1133 obstack_init (&frame_cache_obstack
);