2002-09-30 Andrew Cagney <ac131313@redhat.com>
[external/binutils.git] / gdb / blockframe.c
1 /* Get info from stack frames; convert between frames, blocks,
2    functions and pc values.
3
4    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
5    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software
6    Foundation, Inc.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24
25 #include "defs.h"
26 #include "symtab.h"
27 #include "bfd.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "frame.h"
31 #include "gdbcore.h"
32 #include "value.h"              /* for read_register */
33 #include "target.h"             /* for target_has_stack */
34 #include "inferior.h"           /* for read_pc */
35 #include "annotate.h"
36 #include "regcache.h"
37 #include "gdb_assert.h"
38
39 /* Prototypes for exported functions. */
40
41 static void generic_call_dummy_register_unwind (struct frame_info *frame,
42                                                 void **cache,
43                                                 int regnum,
44                                                 int *optimized,
45                                                 enum lval_type *lval,
46                                                 CORE_ADDR *addrp,
47                                                 int *realnum,
48                                                 void *raw_buffer);
49 static void frame_saved_regs_register_unwind (struct frame_info *frame,
50                                               void **cache,
51                                               int regnum,
52                                               int *optimized,
53                                               enum lval_type *lval,
54                                               CORE_ADDR *addrp,
55                                               int *realnum,
56                                               void *buffer);
57
58
59 void _initialize_blockframe (void);
60
61 /* A default FRAME_CHAIN_VALID, in the form that is suitable for most
62    targets.  If FRAME_CHAIN_VALID returns zero it means that the given
63    frame is the outermost one and has no caller. */
64
65 int
66 file_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe)
67 {
68   return ((chain) != 0
69           && !inside_entry_file (FRAME_SAVED_PC (thisframe)));
70 }
71
72 /* Use the alternate method of avoiding running up off the end of the
73    frame chain or following frames back into the startup code.  See
74    the comments in objfiles.h. */
75
76 int
77 func_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe)
78 {
79   return ((chain) != 0
80           && !inside_main_func ((thisframe)->pc)
81           && !inside_entry_func ((thisframe)->pc));
82 }
83
84 /* A very simple method of determining a valid frame */
85
86 int
87 nonnull_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe)
88 {
89   return ((chain) != 0);
90 }
91
92 /* Is ADDR inside the startup file?  Note that if your machine
93    has a way to detect the bottom of the stack, there is no need
94    to call this function from FRAME_CHAIN_VALID; the reason for
95    doing so is that some machines have no way of detecting bottom
96    of stack. 
97
98    A PC of zero is always considered to be the bottom of the stack. */
99
100 int
101 inside_entry_file (CORE_ADDR addr)
102 {
103   if (addr == 0)
104     return 1;
105   if (symfile_objfile == 0)
106     return 0;
107   if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT)
108     {
109       /* Do not stop backtracing if the pc is in the call dummy
110          at the entry point.  */
111       /* FIXME: Won't always work with zeros for the last two arguments */
112       if (PC_IN_CALL_DUMMY (addr, 0, 0))
113         return 0;
114     }
115   return (addr >= symfile_objfile->ei.entry_file_lowpc &&
116           addr < symfile_objfile->ei.entry_file_highpc);
117 }
118
119 /* Test a specified PC value to see if it is in the range of addresses
120    that correspond to the main() function.  See comments above for why
121    we might want to do this.
122
123    Typically called from FRAME_CHAIN_VALID.
124
125    A PC of zero is always considered to be the bottom of the stack. */
126
127 int
128 inside_main_func (CORE_ADDR pc)
129 {
130   if (pc == 0)
131     return 1;
132   if (symfile_objfile == 0)
133     return 0;
134
135   /* If the addr range is not set up at symbol reading time, set it up now.
136      This is for FRAME_CHAIN_VALID_ALTERNATE. I do this for coff, because
137      it is unable to set it up and symbol reading time. */
138
139   if (symfile_objfile->ei.main_func_lowpc == INVALID_ENTRY_LOWPC &&
140       symfile_objfile->ei.main_func_highpc == INVALID_ENTRY_HIGHPC)
141     {
142       struct symbol *mainsym;
143
144       mainsym = lookup_symbol (main_name (), NULL, VAR_NAMESPACE, NULL, NULL);
145       if (mainsym && SYMBOL_CLASS (mainsym) == LOC_BLOCK)
146         {
147           symfile_objfile->ei.main_func_lowpc =
148             BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym));
149           symfile_objfile->ei.main_func_highpc =
150             BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym));
151         }
152     }
153   return (symfile_objfile->ei.main_func_lowpc <= pc &&
154           symfile_objfile->ei.main_func_highpc > pc);
155 }
156
157 /* Test a specified PC value to see if it is in the range of addresses
158    that correspond to the process entry point function.  See comments
159    in objfiles.h for why we might want to do this.
160
161    Typically called from FRAME_CHAIN_VALID.
162
163    A PC of zero is always considered to be the bottom of the stack. */
164
165 int
166 inside_entry_func (CORE_ADDR pc)
167 {
168   if (pc == 0)
169     return 1;
170   if (symfile_objfile == 0)
171     return 0;
172   if (CALL_DUMMY_LOCATION == AT_ENTRY_POINT)
173     {
174       /* Do not stop backtracing if the pc is in the call dummy
175          at the entry point.  */
176       /* FIXME: Won't always work with zeros for the last two arguments */
177       if (PC_IN_CALL_DUMMY (pc, 0, 0))
178         return 0;
179     }
180   return (symfile_objfile->ei.entry_func_lowpc <= pc &&
181           symfile_objfile->ei.entry_func_highpc > pc);
182 }
183
184 /* Info about the innermost stack frame (contents of FP register) */
185
186 static struct frame_info *current_frame;
187
188 /* Cache for frame addresses already read by gdb.  Valid only while
189    inferior is stopped.  Control variables for the frame cache should
190    be local to this module.  */
191
192 static struct obstack frame_cache_obstack;
193
194 void *
195 frame_obstack_alloc (unsigned long size)
196 {
197   return obstack_alloc (&frame_cache_obstack, size);
198 }
199
200 void
201 frame_saved_regs_zalloc (struct frame_info *fi)
202 {
203   fi->saved_regs = (CORE_ADDR *)
204     frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS);
205   memset (fi->saved_regs, 0, SIZEOF_FRAME_SAVED_REGS);
206 }
207
208
209 /* Return the innermost (currently executing) stack frame.  */
210
211 struct frame_info *
212 get_current_frame (void)
213 {
214   if (current_frame == NULL)
215     {
216       if (target_has_stack)
217         current_frame = create_new_frame (read_fp (), read_pc ());
218       else
219         error ("No stack.");
220     }
221   return current_frame;
222 }
223
224 void
225 set_current_frame (struct frame_info *frame)
226 {
227   current_frame = frame;
228 }
229
230
231 /* Using the PC, select a mechanism for unwinding a frame returning
232    the previous frame.  The register unwind function should, on
233    demand, initialize the ->context object.  */
234
235 static void
236 set_unwind_by_pc (CORE_ADDR pc, CORE_ADDR fp,
237                   frame_register_unwind_ftype **unwind)
238 {
239   if (!USE_GENERIC_DUMMY_FRAMES)
240     /* Still need to set this to something.  The ``info frame'' code
241        calls this function to find out where the saved registers are.
242        Hopefully this is robust enough to stop any core dumps and
243        return vaguely correct values..  */
244     *unwind = frame_saved_regs_register_unwind;
245   else if (PC_IN_CALL_DUMMY (pc, fp, fp))
246     *unwind = generic_call_dummy_register_unwind;
247   else
248     *unwind = frame_saved_regs_register_unwind;
249 }
250
251 /* Create an arbitrary (i.e. address specified by user) or innermost frame.
252    Always returns a non-NULL value.  */
253
254 struct frame_info *
255 create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
256 {
257   struct frame_info *fi;
258   char *name;
259
260   fi = (struct frame_info *)
261     obstack_alloc (&frame_cache_obstack,
262                    sizeof (struct frame_info));
263
264   /* Zero all fields by default.  */
265   memset (fi, 0, sizeof (struct frame_info));
266
267   fi->frame = addr;
268   fi->pc = pc;
269   find_pc_partial_function (pc, &name, (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
270   fi->signal_handler_caller = PC_IN_SIGTRAMP (fi->pc, name);
271
272   if (INIT_EXTRA_FRAME_INFO_P ())
273     INIT_EXTRA_FRAME_INFO (0, fi);
274
275   /* Select/initialize an unwind function.  */
276   set_unwind_by_pc (fi->pc, fi->frame, &fi->register_unwind);
277
278   return fi;
279 }
280
281 /* Return the frame that FRAME calls (NULL if FRAME is the innermost
282    frame).  */
283
284 struct frame_info *
285 get_next_frame (struct frame_info *frame)
286 {
287   return frame->next;
288 }
289
290 /* Flush the entire frame cache.  */
291
292 void
293 flush_cached_frames (void)
294 {
295   /* Since we can't really be sure what the first object allocated was */
296   obstack_free (&frame_cache_obstack, 0);
297   obstack_init (&frame_cache_obstack);
298
299   current_frame = NULL;         /* Invalidate cache */
300   select_frame (NULL);
301   annotate_frames_invalid ();
302 }
303
304 /* Flush the frame cache, and start a new one if necessary.  */
305
306 void
307 reinit_frame_cache (void)
308 {
309   flush_cached_frames ();
310
311   /* FIXME: The inferior_ptid test is wrong if there is a corefile.  */
312   if (PIDGET (inferior_ptid) != 0)
313     {
314       select_frame (get_current_frame ());
315     }
316 }
317
318 /* Return nonzero if the function for this frame lacks a prologue.  Many
319    machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
320    function.  */
321
322 int
323 frameless_look_for_prologue (struct frame_info *frame)
324 {
325   CORE_ADDR func_start, after_prologue;
326
327   func_start = get_pc_function_start (frame->pc);
328   if (func_start)
329     {
330       func_start += FUNCTION_START_OFFSET;
331       /* This is faster, since only care whether there *is* a
332          prologue, not how long it is.  */
333       return PROLOGUE_FRAMELESS_P (func_start);
334     }
335   else if (frame->pc == 0)
336     /* A frame with a zero PC is usually created by dereferencing a
337        NULL function pointer, normally causing an immediate core dump
338        of the inferior. Mark function as frameless, as the inferior
339        has no chance of setting up a stack frame.  */
340     return 1;
341   else
342     /* If we can't find the start of the function, we don't really
343        know whether the function is frameless, but we should be able
344        to get a reasonable (i.e. best we can do under the
345        circumstances) backtrace by saying that it isn't.  */
346     return 0;
347 }
348
349 /* Return a structure containing various interesting information
350    about the frame that called NEXT_FRAME.  Returns NULL
351    if there is no such frame.  */
352
353 struct frame_info *
354 get_prev_frame (struct frame_info *next_frame)
355 {
356   CORE_ADDR address = 0;
357   struct frame_info *prev;
358   int fromleaf = 0;
359   char *name;
360
361   /* If the requested entry is in the cache, return it.
362      Otherwise, figure out what the address should be for the entry
363      we're about to add to the cache. */
364
365   if (!next_frame)
366     {
367 #if 0
368       /* This screws value_of_variable, which just wants a nice clean
369          NULL return from block_innermost_frame if there are no frames.
370          I don't think I've ever seen this message happen otherwise.
371          And returning NULL here is a perfectly legitimate thing to do.  */
372       if (!current_frame)
373         {
374           error ("You haven't set up a process's stack to examine.");
375         }
376 #endif
377
378       return current_frame;
379     }
380
381   /* If we have the prev one, return it */
382   if (next_frame->prev)
383     return next_frame->prev;
384
385   /* On some machines it is possible to call a function without
386      setting up a stack frame for it.  On these machines, we
387      define this macro to take two args; a frameinfo pointer
388      identifying a frame and a variable to set or clear if it is
389      or isn't leafless.  */
390
391   /* Still don't want to worry about this except on the innermost
392      frame.  This macro will set FROMLEAF if NEXT_FRAME is a
393      frameless function invocation.  */
394   if (!(next_frame->next))
395     {
396       fromleaf = FRAMELESS_FUNCTION_INVOCATION (next_frame);
397       if (fromleaf)
398         address = FRAME_FP (next_frame);
399     }
400
401   if (!fromleaf)
402     {
403       /* Two macros defined in tm.h specify the machine-dependent
404          actions to be performed here.
405          First, get the frame's chain-pointer.
406          If that is zero, the frame is the outermost frame or a leaf
407          called by the outermost frame.  This means that if start
408          calls main without a frame, we'll return 0 (which is fine
409          anyway).
410
411          Nope; there's a problem.  This also returns when the current
412          routine is a leaf of main.  This is unacceptable.  We move
413          this to after the ffi test; I'd rather have backtraces from
414          start go curfluy than have an abort called from main not show
415          main.  */
416       address = FRAME_CHAIN (next_frame);
417
418       /* FIXME: cagney/2002-06-08: There should be two tests here.
419          The first would check for a valid frame chain based on a user
420          selectable policy.  The default being ``stop at main'' (as
421          implemented by generic_func_frame_chain_valid()).  Other
422          policies would be available - stop at NULL, ....  The second
423          test, if provided by the target architecture, would check for
424          more exotic cases - most target architectures wouldn't bother
425          with this second case.  */
426       if (!FRAME_CHAIN_VALID (address, next_frame))
427         return 0;
428     }
429   if (address == 0)
430     return 0;
431
432   prev = (struct frame_info *)
433     obstack_alloc (&frame_cache_obstack,
434                    sizeof (struct frame_info));
435
436   /* Zero all fields by default.  */
437   memset (prev, 0, sizeof (struct frame_info));
438
439   if (next_frame)
440     next_frame->prev = prev;
441   prev->next = next_frame;
442   prev->frame = address;
443   prev->level = next_frame->level + 1;
444
445 /* This change should not be needed, FIXME!  We should
446    determine whether any targets *need* INIT_FRAME_PC to happen
447    after INIT_EXTRA_FRAME_INFO and come up with a simple way to
448    express what goes on here.
449
450    INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame
451    (where the PC is already set up) and here (where it isn't).
452    INIT_FRAME_PC is only called from here, always after
453    INIT_EXTRA_FRAME_INFO.
454
455    The catch is the MIPS, where INIT_EXTRA_FRAME_INFO requires the PC
456    value (which hasn't been set yet).  Some other machines appear to
457    require INIT_EXTRA_FRAME_INFO before they can do INIT_FRAME_PC.  Phoo.
458
459    We shouldn't need INIT_FRAME_PC_FIRST to add more complication to
460    an already overcomplicated part of GDB.   gnu@cygnus.com, 15Sep92.
461
462    Assuming that some machines need INIT_FRAME_PC after
463    INIT_EXTRA_FRAME_INFO, one possible scheme:
464
465    SETUP_INNERMOST_FRAME()
466    Default version is just create_new_frame (read_fp ()),
467    read_pc ()).  Machines with extra frame info would do that (or the
468    local equivalent) and then set the extra fields.
469    SETUP_ARBITRARY_FRAME(argc, argv)
470    Only change here is that create_new_frame would no longer init extra
471    frame info; SETUP_ARBITRARY_FRAME would have to do that.
472    INIT_PREV_FRAME(fromleaf, prev)
473    Replace INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC.  This should
474    also return a flag saying whether to keep the new frame, or
475    whether to discard it, because on some machines (e.g.  mips) it
476    is really awkward to have FRAME_CHAIN_VALID called *before*
477    INIT_EXTRA_FRAME_INFO (there is no good way to get information
478    deduced in FRAME_CHAIN_VALID into the extra fields of the new frame).
479    std_frame_pc(fromleaf, prev)
480    This is the default setting for INIT_PREV_FRAME.  It just does what
481    the default INIT_FRAME_PC does.  Some machines will call it from
482    INIT_PREV_FRAME (either at the beginning, the end, or in the middle).
483    Some machines won't use it.
484    kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94.  */
485
486   INIT_FRAME_PC_FIRST (fromleaf, prev);
487
488   if (INIT_EXTRA_FRAME_INFO_P ())
489     INIT_EXTRA_FRAME_INFO (fromleaf, prev);
490
491   /* This entry is in the frame queue now, which is good since
492      FRAME_SAVED_PC may use that queue to figure out its value
493      (see tm-sparc.h).  We want the pc saved in the inferior frame. */
494   INIT_FRAME_PC (fromleaf, prev);
495
496   /* If ->frame and ->pc are unchanged, we are in the process of getting
497      ourselves into an infinite backtrace.  Some architectures check this
498      in FRAME_CHAIN or thereabouts, but it seems like there is no reason
499      this can't be an architecture-independent check.  */
500   if (next_frame != NULL)
501     {
502       if (prev->frame == next_frame->frame
503           && prev->pc == next_frame->pc)
504         {
505           next_frame->prev = NULL;
506           obstack_free (&frame_cache_obstack, prev);
507           return NULL;
508         }
509     }
510
511   /* Initialize the code used to unwind the frame PREV based on the PC
512      (and probably other architectural information).  The PC lets you
513      check things like the debug info at that point (dwarf2cfi?) and
514      use that to decide how the frame should be unwound.  */
515   set_unwind_by_pc (prev->pc, prev->frame, &prev->register_unwind);
516
517   find_pc_partial_function (prev->pc, &name,
518                             (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
519   if (PC_IN_SIGTRAMP (prev->pc, name))
520     prev->signal_handler_caller = 1;
521
522   return prev;
523 }
524
525 CORE_ADDR
526 get_frame_pc (struct frame_info *frame)
527 {
528   return frame->pc;
529 }
530
531 /* return the address of the PC for the given FRAME, ie the current PC value
532    if FRAME is the innermost frame, or the address adjusted to point to the
533    call instruction if not.  */
534
535 CORE_ADDR
536 frame_address_in_block (struct frame_info *frame)
537 {
538   CORE_ADDR pc = frame->pc;
539
540   /* If we are not in the innermost frame, and we are not interrupted
541      by a signal, frame->pc points to the instruction following the
542      call. As a consequence, we need to get the address of the previous
543      instruction. Unfortunately, this is not straightforward to do, so
544      we just use the address minus one, which is a good enough
545      approximation.  */
546   if (frame->next != 0 && frame->next->signal_handler_caller == 0)
547     --pc;
548
549   return pc;
550 }
551
552 #ifdef FRAME_FIND_SAVED_REGS
553 /* XXX - deprecated.  This is a compatibility function for targets
554    that do not yet implement FRAME_INIT_SAVED_REGS.  */
555 /* Find the addresses in which registers are saved in FRAME.  */
556
557 void
558 get_frame_saved_regs (struct frame_info *frame,
559                       struct frame_saved_regs *saved_regs_addr)
560 {
561   if (frame->saved_regs == NULL)
562     {
563       frame->saved_regs = (CORE_ADDR *)
564         frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS);
565     }
566   if (saved_regs_addr == NULL)
567     {
568       struct frame_saved_regs saved_regs;
569       FRAME_FIND_SAVED_REGS (frame, saved_regs);
570       memcpy (frame->saved_regs, &saved_regs, SIZEOF_FRAME_SAVED_REGS);
571     }
572   else
573     {
574       FRAME_FIND_SAVED_REGS (frame, *saved_regs_addr);
575       memcpy (frame->saved_regs, saved_regs_addr, SIZEOF_FRAME_SAVED_REGS);
576     }
577 }
578 #endif
579
580 /* Return the innermost lexical block in execution
581    in a specified stack frame.  The frame address is assumed valid.
582
583    If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the exact code
584    address we used to choose the block.  We use this to find a source
585    line, to decide which macro definitions are in scope.
586
587    The value returned in *ADDR_IN_BLOCK isn't necessarily the frame's
588    PC, and may not really be a valid PC at all.  For example, in the
589    caller of a function declared to never return, the code at the
590    return address will never be reached, so the call instruction may
591    be the very last instruction in the block.  So the address we use
592    to choose the block is actually one byte before the return address
593    --- hopefully pointing us at the call instruction, or its delay
594    slot instruction.  */
595
596 struct block *
597 get_frame_block (struct frame_info *frame, CORE_ADDR *addr_in_block)
598 {
599   const CORE_ADDR pc = frame_address_in_block (frame);
600
601   if (addr_in_block)
602     *addr_in_block = pc;
603
604   return block_for_pc (pc);
605 }
606
607 struct block *
608 get_current_block (CORE_ADDR *addr_in_block)
609 {
610   CORE_ADDR pc = read_pc ();
611
612   if (addr_in_block)
613     *addr_in_block = pc;
614
615   return block_for_pc (pc);
616 }
617
618 CORE_ADDR
619 get_pc_function_start (CORE_ADDR pc)
620 {
621   register struct block *bl;
622   register struct symbol *symbol;
623   register struct minimal_symbol *msymbol;
624   CORE_ADDR fstart;
625
626   if ((bl = block_for_pc (pc)) != NULL &&
627       (symbol = block_function (bl)) != NULL)
628     {
629       bl = SYMBOL_BLOCK_VALUE (symbol);
630       fstart = BLOCK_START (bl);
631     }
632   else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
633     {
634       fstart = SYMBOL_VALUE_ADDRESS (msymbol);
635       if (!find_pc_section (fstart))
636         return 0;
637     }
638   else
639     {
640       fstart = 0;
641     }
642   return (fstart);
643 }
644
645 /* Return the symbol for the function executing in frame FRAME.  */
646
647 struct symbol *
648 get_frame_function (struct frame_info *frame)
649 {
650   register struct block *bl = get_frame_block (frame, 0);
651   if (bl == 0)
652     return 0;
653   return block_function (bl);
654 }
655 \f
656
657 /* Return the blockvector immediately containing the innermost lexical block
658    containing the specified pc value and section, or 0 if there is none.
659    PINDEX is a pointer to the index value of the block.  If PINDEX
660    is NULL, we don't pass this information back to the caller.  */
661
662 struct blockvector *
663 blockvector_for_pc_sect (register CORE_ADDR pc, struct sec *section,
664                          int *pindex, struct symtab *symtab)
665 {
666   register struct block *b;
667   register int bot, top, half;
668   struct blockvector *bl;
669
670   if (symtab == 0)              /* if no symtab specified by caller */
671     {
672       /* First search all symtabs for one whose file contains our pc */
673       if ((symtab = find_pc_sect_symtab (pc, section)) == 0)
674         return 0;
675     }
676
677   bl = BLOCKVECTOR (symtab);
678   b = BLOCKVECTOR_BLOCK (bl, 0);
679
680   /* Then search that symtab for the smallest block that wins.  */
681   /* Use binary search to find the last block that starts before PC.  */
682
683   bot = 0;
684   top = BLOCKVECTOR_NBLOCKS (bl);
685
686   while (top - bot > 1)
687     {
688       half = (top - bot + 1) >> 1;
689       b = BLOCKVECTOR_BLOCK (bl, bot + half);
690       if (BLOCK_START (b) <= pc)
691         bot += half;
692       else
693         top = bot + half;
694     }
695
696   /* Now search backward for a block that ends after PC.  */
697
698   while (bot >= 0)
699     {
700       b = BLOCKVECTOR_BLOCK (bl, bot);
701       if (BLOCK_END (b) > pc)
702         {
703           if (pindex)
704             *pindex = bot;
705           return bl;
706         }
707       bot--;
708     }
709   return 0;
710 }
711
712 /* Return the blockvector immediately containing the innermost lexical block
713    containing the specified pc value, or 0 if there is none.
714    Backward compatibility, no section.  */
715
716 struct blockvector *
717 blockvector_for_pc (register CORE_ADDR pc, int *pindex)
718 {
719   return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
720                                   pindex, NULL);
721 }
722
723 /* Return the innermost lexical block containing the specified pc value
724    in the specified section, or 0 if there is none.  */
725
726 struct block *
727 block_for_pc_sect (register CORE_ADDR pc, struct sec *section)
728 {
729   register struct blockvector *bl;
730   int index;
731
732   bl = blockvector_for_pc_sect (pc, section, &index, NULL);
733   if (bl)
734     return BLOCKVECTOR_BLOCK (bl, index);
735   return 0;
736 }
737
738 /* Return the innermost lexical block containing the specified pc value,
739    or 0 if there is none.  Backward compatibility, no section.  */
740
741 struct block *
742 block_for_pc (register CORE_ADDR pc)
743 {
744   return block_for_pc_sect (pc, find_pc_mapped_section (pc));
745 }
746
747 /* Return the function containing pc value PC in section SECTION.
748    Returns 0 if function is not known.  */
749
750 struct symbol *
751 find_pc_sect_function (CORE_ADDR pc, struct sec *section)
752 {
753   register struct block *b = block_for_pc_sect (pc, section);
754   if (b == 0)
755     return 0;
756   return block_function (b);
757 }
758
759 /* Return the function containing pc value PC.
760    Returns 0 if function is not known.  Backward compatibility, no section */
761
762 struct symbol *
763 find_pc_function (CORE_ADDR pc)
764 {
765   return find_pc_sect_function (pc, find_pc_mapped_section (pc));
766 }
767
768 /* These variables are used to cache the most recent result
769  * of find_pc_partial_function. */
770
771 static CORE_ADDR cache_pc_function_low = 0;
772 static CORE_ADDR cache_pc_function_high = 0;
773 static char *cache_pc_function_name = 0;
774 static struct sec *cache_pc_function_section = NULL;
775
776 /* Clear cache, e.g. when symbol table is discarded. */
777
778 void
779 clear_pc_function_cache (void)
780 {
781   cache_pc_function_low = 0;
782   cache_pc_function_high = 0;
783   cache_pc_function_name = (char *) 0;
784   cache_pc_function_section = NULL;
785 }
786
787 /* Finds the "function" (text symbol) that is smaller than PC but
788    greatest of all of the potential text symbols in SECTION.  Sets
789    *NAME and/or *ADDRESS conditionally if that pointer is non-null.
790    If ENDADDR is non-null, then set *ENDADDR to be the end of the
791    function (exclusive), but passing ENDADDR as non-null means that
792    the function might cause symbols to be read.  This function either
793    succeeds or fails (not halfway succeeds).  If it succeeds, it sets
794    *NAME, *ADDRESS, and *ENDADDR to real information and returns 1.
795    If it fails, it sets *NAME, *ADDRESS, and *ENDADDR to zero and
796    returns 0.  */
797
798 int
799 find_pc_sect_partial_function (CORE_ADDR pc, asection *section, char **name,
800                                CORE_ADDR *address, CORE_ADDR *endaddr)
801 {
802   struct partial_symtab *pst;
803   struct symbol *f;
804   struct minimal_symbol *msymbol;
805   struct partial_symbol *psb;
806   struct obj_section *osect;
807   int i;
808   CORE_ADDR mapped_pc;
809
810   mapped_pc = overlay_mapped_address (pc, section);
811
812   if (mapped_pc >= cache_pc_function_low
813       && mapped_pc < cache_pc_function_high
814       && section == cache_pc_function_section)
815     goto return_cached_value;
816
817   /* If sigtramp is in the u area, it counts as a function (especially
818      important for step_1).  */
819   if (SIGTRAMP_START_P () && PC_IN_SIGTRAMP (mapped_pc, (char *) NULL))
820     {
821       cache_pc_function_low = SIGTRAMP_START (mapped_pc);
822       cache_pc_function_high = SIGTRAMP_END (mapped_pc);
823       cache_pc_function_name = "<sigtramp>";
824       cache_pc_function_section = section;
825       goto return_cached_value;
826     }
827
828   msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
829   pst = find_pc_sect_psymtab (mapped_pc, section);
830   if (pst)
831     {
832       /* Need to read the symbols to get a good value for the end address.  */
833       if (endaddr != NULL && !pst->readin)
834         {
835           /* Need to get the terminal in case symbol-reading produces
836              output.  */
837           target_terminal_ours_for_output ();
838           PSYMTAB_TO_SYMTAB (pst);
839         }
840
841       if (pst->readin)
842         {
843           /* Checking whether the msymbol has a larger value is for the
844              "pathological" case mentioned in print_frame_info.  */
845           f = find_pc_sect_function (mapped_pc, section);
846           if (f != NULL
847               && (msymbol == NULL
848                   || (BLOCK_START (SYMBOL_BLOCK_VALUE (f))
849                       >= SYMBOL_VALUE_ADDRESS (msymbol))))
850             {
851               cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
852               cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
853               cache_pc_function_name = SYMBOL_NAME (f);
854               cache_pc_function_section = section;
855               goto return_cached_value;
856             }
857         }
858       else
859         {
860           /* Now that static symbols go in the minimal symbol table, perhaps
861              we could just ignore the partial symbols.  But at least for now
862              we use the partial or minimal symbol, whichever is larger.  */
863           psb = find_pc_sect_psymbol (pst, mapped_pc, section);
864
865           if (psb
866               && (msymbol == NULL ||
867                   (SYMBOL_VALUE_ADDRESS (psb)
868                    >= SYMBOL_VALUE_ADDRESS (msymbol))))
869             {
870               /* This case isn't being cached currently. */
871               if (address)
872                 *address = SYMBOL_VALUE_ADDRESS (psb);
873               if (name)
874                 *name = SYMBOL_NAME (psb);
875               /* endaddr non-NULL can't happen here.  */
876               return 1;
877             }
878         }
879     }
880
881   /* Not in the normal symbol tables, see if the pc is in a known section.
882      If it's not, then give up.  This ensures that anything beyond the end
883      of the text seg doesn't appear to be part of the last function in the
884      text segment.  */
885
886   osect = find_pc_sect_section (mapped_pc, section);
887
888   if (!osect)
889     msymbol = NULL;
890
891   /* Must be in the minimal symbol table.  */
892   if (msymbol == NULL)
893     {
894       /* No available symbol.  */
895       if (name != NULL)
896         *name = 0;
897       if (address != NULL)
898         *address = 0;
899       if (endaddr != NULL)
900         *endaddr = 0;
901       return 0;
902     }
903
904   cache_pc_function_low = SYMBOL_VALUE_ADDRESS (msymbol);
905   cache_pc_function_name = SYMBOL_NAME (msymbol);
906   cache_pc_function_section = section;
907
908   /* Use the lesser of the next minimal symbol in the same section, or
909      the end of the section, as the end of the function.  */
910
911   /* Step over other symbols at this same address, and symbols in
912      other sections, to find the next symbol in this section with
913      a different address.  */
914
915   for (i = 1; SYMBOL_NAME (msymbol + i) != NULL; i++)
916     {
917       if (SYMBOL_VALUE_ADDRESS (msymbol + i) != SYMBOL_VALUE_ADDRESS (msymbol)
918           && SYMBOL_BFD_SECTION (msymbol + i) == SYMBOL_BFD_SECTION (msymbol))
919         break;
920     }
921
922   if (SYMBOL_NAME (msymbol + i) != NULL
923       && SYMBOL_VALUE_ADDRESS (msymbol + i) < osect->endaddr)
924     cache_pc_function_high = SYMBOL_VALUE_ADDRESS (msymbol + i);
925   else
926     /* We got the start address from the last msymbol in the objfile.
927        So the end address is the end of the section.  */
928     cache_pc_function_high = osect->endaddr;
929
930  return_cached_value:
931
932   if (address)
933     {
934       if (pc_in_unmapped_range (pc, section))
935         *address = overlay_unmapped_address (cache_pc_function_low, section);
936       else
937         *address = cache_pc_function_low;
938     }
939
940   if (name)
941     *name = cache_pc_function_name;
942
943   if (endaddr)
944     {
945       if (pc_in_unmapped_range (pc, section))
946         {
947           /* Because the high address is actually beyond the end of
948              the function (and therefore possibly beyond the end of
949              the overlay), we must actually convert (high - 1) and
950              then add one to that. */
951
952           *endaddr = 1 + overlay_unmapped_address (cache_pc_function_high - 1,
953                                                    section);
954         }
955       else
956         *endaddr = cache_pc_function_high;
957     }
958
959   return 1;
960 }
961
962 /* Backward compatibility, no section argument.  */
963
964 int
965 find_pc_partial_function (CORE_ADDR pc, char **name, CORE_ADDR *address,
966                           CORE_ADDR *endaddr)
967 {
968   asection *section;
969
970   section = find_pc_overlay (pc);
971   return find_pc_sect_partial_function (pc, section, name, address, endaddr);
972 }
973
974 /* Return the innermost stack frame executing inside of BLOCK,
975    or NULL if there is no such frame.  If BLOCK is NULL, just return NULL.  */
976
977 struct frame_info *
978 block_innermost_frame (struct block *block)
979 {
980   struct frame_info *frame;
981   register CORE_ADDR start;
982   register CORE_ADDR end;
983   CORE_ADDR calling_pc;
984
985   if (block == NULL)
986     return NULL;
987
988   start = BLOCK_START (block);
989   end = BLOCK_END (block);
990
991   frame = NULL;
992   while (1)
993     {
994       frame = get_prev_frame (frame);
995       if (frame == NULL)
996         return NULL;
997       calling_pc = frame_address_in_block (frame);
998       if (calling_pc >= start && calling_pc < end)
999         return frame;
1000     }
1001 }
1002
1003 /* Return the full FRAME which corresponds to the given CORE_ADDR
1004    or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
1005
1006 struct frame_info *
1007 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
1008 {
1009   struct frame_info *frame = NULL;
1010
1011   if (frame_addr == (CORE_ADDR) 0)
1012     return NULL;
1013
1014   while (1)
1015     {
1016       frame = get_prev_frame (frame);
1017       if (frame == NULL)
1018         return NULL;
1019       if (FRAME_FP (frame) == frame_addr)
1020         return frame;
1021     }
1022 }
1023
1024 #ifdef SIGCONTEXT_PC_OFFSET
1025 /* Get saved user PC for sigtramp from sigcontext for BSD style sigtramp.  */
1026
1027 CORE_ADDR
1028 sigtramp_saved_pc (struct frame_info *frame)
1029 {
1030   CORE_ADDR sigcontext_addr;
1031   char *buf;
1032   int ptrbytes = TARGET_PTR_BIT / TARGET_CHAR_BIT;
1033   int sigcontext_offs = (2 * TARGET_INT_BIT) / TARGET_CHAR_BIT;
1034
1035   buf = alloca (ptrbytes);
1036   /* Get sigcontext address, it is the third parameter on the stack.  */
1037   if (frame->next)
1038     sigcontext_addr = read_memory_integer (FRAME_ARGS_ADDRESS (frame->next)
1039                                            + FRAME_ARGS_SKIP
1040                                            + sigcontext_offs,
1041                                            ptrbytes);
1042   else
1043     sigcontext_addr = read_memory_integer (read_register (SP_REGNUM)
1044                                            + sigcontext_offs,
1045                                            ptrbytes);
1046
1047   /* Don't cause a memory_error when accessing sigcontext in case the stack
1048      layout has changed or the stack is corrupt.  */
1049   target_read_memory (sigcontext_addr + SIGCONTEXT_PC_OFFSET, buf, ptrbytes);
1050   return extract_unsigned_integer (buf, ptrbytes);
1051 }
1052 #endif /* SIGCONTEXT_PC_OFFSET */
1053
1054
1055 /* Are we in a call dummy?  The code below which allows DECR_PC_AFTER_BREAK
1056    below is for infrun.c, which may give the macro a pc without that
1057    subtracted out.  */
1058
1059 extern CORE_ADDR text_end;
1060
1061 int
1062 pc_in_call_dummy_before_text_end (CORE_ADDR pc, CORE_ADDR sp,
1063                                   CORE_ADDR frame_address)
1064 {
1065   return ((pc) >= text_end - CALL_DUMMY_LENGTH
1066           && (pc) <= text_end + DECR_PC_AFTER_BREAK);
1067 }
1068
1069 int
1070 pc_in_call_dummy_after_text_end (CORE_ADDR pc, CORE_ADDR sp,
1071                                  CORE_ADDR frame_address)
1072 {
1073   return ((pc) >= text_end
1074           && (pc) <= text_end + CALL_DUMMY_LENGTH + DECR_PC_AFTER_BREAK);
1075 }
1076
1077 /* Is the PC in a call dummy?  SP and FRAME_ADDRESS are the bottom and
1078    top of the stack frame which we are checking, where "bottom" and
1079    "top" refer to some section of memory which contains the code for
1080    the call dummy.  Calls to this macro assume that the contents of
1081    SP_REGNUM and FP_REGNUM (or the saved values thereof), respectively,
1082    are the things to pass.
1083
1084    This won't work on the 29k, where SP_REGNUM and FP_REGNUM don't
1085    have that meaning, but the 29k doesn't use ON_STACK.  This could be
1086    fixed by generalizing this scheme, perhaps by passing in a frame
1087    and adding a few fields, at least on machines which need them for
1088    PC_IN_CALL_DUMMY.
1089
1090    Something simpler, like checking for the stack segment, doesn't work,
1091    since various programs (threads implementations, gcc nested function
1092    stubs, etc) may either allocate stack frames in another segment, or
1093    allocate other kinds of code on the stack.  */
1094
1095 int
1096 pc_in_call_dummy_on_stack (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR frame_address)
1097 {
1098   return (INNER_THAN ((sp), (pc))
1099           && (frame_address != 0)
1100           && INNER_THAN ((pc), (frame_address)));
1101 }
1102
1103 int
1104 pc_in_call_dummy_at_entry_point (CORE_ADDR pc, CORE_ADDR sp,
1105                                  CORE_ADDR frame_address)
1106 {
1107   return ((pc) >= CALL_DUMMY_ADDRESS ()
1108           && (pc) <= (CALL_DUMMY_ADDRESS () + DECR_PC_AFTER_BREAK));
1109 }
1110
1111
1112 /*
1113  * GENERIC DUMMY FRAMES
1114  * 
1115  * The following code serves to maintain the dummy stack frames for
1116  * inferior function calls (ie. when gdb calls into the inferior via
1117  * call_function_by_hand).  This code saves the machine state before 
1118  * the call in host memory, so we must maintain an independent stack 
1119  * and keep it consistant etc.  I am attempting to make this code 
1120  * generic enough to be used by many targets.
1121  *
1122  * The cheapest and most generic way to do CALL_DUMMY on a new target
1123  * is probably to define CALL_DUMMY to be empty, CALL_DUMMY_LENGTH to
1124  * zero, and CALL_DUMMY_LOCATION to AT_ENTRY.  Then you must remember
1125  * to define PUSH_RETURN_ADDRESS, because no call instruction will be
1126  * being executed by the target.  Also FRAME_CHAIN_VALID as
1127  * generic_{file,func}_frame_chain_valid and FIX_CALL_DUMMY as
1128  * generic_fix_call_dummy.  */
1129
1130 /* Dummy frame.  This saves the processor state just prior to setting
1131    up the inferior function call.  Older targets save the registers
1132    on the target stack (but that really slows down function calls).  */
1133
1134 struct dummy_frame
1135 {
1136   struct dummy_frame *next;
1137
1138   CORE_ADDR pc;
1139   CORE_ADDR fp;
1140   CORE_ADDR sp;
1141   CORE_ADDR top;
1142   struct regcache *regcache;
1143
1144   /* Address range of the call dummy code.  Look for PC in the range
1145      [LO..HI) (after allowing for DECR_PC_AFTER_BREAK).  */
1146   CORE_ADDR call_lo;
1147   CORE_ADDR call_hi;
1148 };
1149
1150 static struct dummy_frame *dummy_frame_stack = NULL;
1151
1152 /* Function: find_dummy_frame(pc, fp, sp)
1153
1154    Search the stack of dummy frames for one matching the given PC and
1155    FP/SP.  Unlike PC_IN_CALL_DUMMY, this function doesn't need to
1156    adjust for DECR_PC_AFTER_BREAK.  This is because it is only legal
1157    to call this function after the PC has been adjusted.  */
1158
1159 static struct regcache *
1160 generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
1161 {
1162   struct dummy_frame *dummyframe;
1163
1164   for (dummyframe = dummy_frame_stack; dummyframe != NULL;
1165        dummyframe = dummyframe->next)
1166     {
1167       /* Does the PC fall within the dummy frame's breakpoint
1168          instruction.  If not, discard this one.  */
1169       if (!(pc >= dummyframe->call_lo && pc < dummyframe->call_hi))
1170         continue;
1171       /* Does the FP match?  */
1172       if (dummyframe->top != 0)
1173         {
1174           /* If the target architecture explicitly saved the
1175              top-of-stack before the inferior function call, assume
1176              that that same architecture will always pass in an FP
1177              (frame base) value that eactly matches that saved TOS.
1178              Don't check the saved SP and SP as they can lead to false
1179              hits.  */
1180           if (fp != dummyframe->top)
1181             continue;
1182         }
1183       else
1184         {
1185           /* An older target that hasn't explicitly or implicitly
1186              saved the dummy frame's top-of-stack.  Try matching the
1187              FP against the saved SP and FP.  NOTE: If you're trying
1188              to fix a problem with GDB not correctly finding a dummy
1189              frame, check the comments that go with FRAME_ALIGN() and
1190              SAVE_DUMMY_FRAME_TOS().  */
1191           if (fp != dummyframe->fp && fp != dummyframe->sp)
1192             continue;
1193         }
1194       /* The FP matches this dummy frame.  */
1195       return dummyframe->regcache;
1196     }
1197
1198   return 0;
1199 }
1200
1201 char *
1202 deprecated_generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp)
1203 {
1204   struct regcache *regcache = generic_find_dummy_frame (pc, fp);
1205   if (regcache == NULL)
1206     return NULL;
1207   return deprecated_grub_regcache_for_registers (regcache);
1208 }
1209
1210 /* Function: pc_in_call_dummy (pc, sp, fp)
1211
1212    Return true if the PC falls in a dummy frame created by gdb for an
1213    inferior call.  The code below which allows DECR_PC_AFTER_BREAK is
1214    for infrun.c, which may give the function a PC without that
1215    subtracted out.  */
1216
1217 int
1218 generic_pc_in_call_dummy (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR fp)
1219 {
1220   struct dummy_frame *dummyframe;
1221   for (dummyframe = dummy_frame_stack;
1222        dummyframe != NULL;
1223        dummyframe = dummyframe->next)
1224     {
1225       if ((pc >= dummyframe->call_lo)
1226           && (pc < dummyframe->call_hi + DECR_PC_AFTER_BREAK))
1227         return 1;
1228     }
1229   return 0;
1230 }
1231
1232 /* Function: read_register_dummy 
1233    Find a saved register from before GDB calls a function in the inferior */
1234
1235 CORE_ADDR
1236 deprecated_read_register_dummy (CORE_ADDR pc, CORE_ADDR fp, int regno)
1237 {
1238   struct regcache *dummy_regs = generic_find_dummy_frame (pc, fp);
1239
1240   if (dummy_regs)
1241     {
1242       /* NOTE: cagney/2002-08-12: Replaced a call to
1243          regcache_raw_read_as_address() with a call to
1244          regcache_cooked_read_unsigned().  The old, ...as_address
1245          function was eventually calling extract_unsigned_integer (via
1246          extract_address) to unpack the registers value.  The below is
1247          doing an unsigned extract so that it is functionally
1248          equivalent.  The read needs to be cooked as, otherwise, it
1249          will never correctly return the value of a register in the
1250          [NUM_REGS .. NUM_REGS+NUM_PSEUDO_REGS) range.  */
1251       ULONGEST val;
1252       regcache_cooked_read_unsigned (dummy_regs, regno, &val);
1253       return val;
1254     }
1255   else
1256     return 0;
1257 }
1258
1259 /* Save all the registers on the dummy frame stack.  Most ports save the
1260    registers on the target stack.  This results in lots of unnecessary memory
1261    references, which are slow when debugging via a serial line.  Instead, we
1262    save all the registers internally, and never write them to the stack.  The
1263    registers get restored when the called function returns to the entry point,
1264    where a breakpoint is laying in wait.  */
1265
1266 void
1267 generic_push_dummy_frame (void)
1268 {
1269   struct dummy_frame *dummy_frame;
1270   CORE_ADDR fp = (get_current_frame ())->frame;
1271
1272   /* check to see if there are stale dummy frames, 
1273      perhaps left over from when a longjump took us out of a 
1274      function that was called by the debugger */
1275
1276   dummy_frame = dummy_frame_stack;
1277   while (dummy_frame)
1278     if (INNER_THAN (dummy_frame->fp, fp))       /* stale -- destroy! */
1279       {
1280         dummy_frame_stack = dummy_frame->next;
1281         regcache_xfree (dummy_frame->regcache);
1282         xfree (dummy_frame);
1283         dummy_frame = dummy_frame_stack;
1284       }
1285     else
1286       dummy_frame = dummy_frame->next;
1287
1288   dummy_frame = xmalloc (sizeof (struct dummy_frame));
1289   dummy_frame->regcache = regcache_xmalloc (current_gdbarch);
1290
1291   dummy_frame->pc = read_pc ();
1292   dummy_frame->sp = read_sp ();
1293   dummy_frame->top = 0;
1294   dummy_frame->fp = fp;
1295   regcache_cpy (dummy_frame->regcache, current_regcache);
1296   dummy_frame->next = dummy_frame_stack;
1297   dummy_frame_stack = dummy_frame;
1298 }
1299
1300 void
1301 generic_save_dummy_frame_tos (CORE_ADDR sp)
1302 {
1303   dummy_frame_stack->top = sp;
1304 }
1305
1306 /* Record the upper/lower bounds on the address of the call dummy.  */
1307
1308 void
1309 generic_save_call_dummy_addr (CORE_ADDR lo, CORE_ADDR hi)
1310 {
1311   dummy_frame_stack->call_lo = lo;
1312   dummy_frame_stack->call_hi = hi;
1313 }
1314
1315 /* Restore the machine state from either the saved dummy stack or a
1316    real stack frame. */
1317
1318 void
1319 generic_pop_current_frame (void (*popper) (struct frame_info * frame))
1320 {
1321   struct frame_info *frame = get_current_frame ();
1322
1323   if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
1324     generic_pop_dummy_frame ();
1325   else
1326     (*popper) (frame);
1327 }
1328
1329 /* Function: pop_dummy_frame
1330    Restore the machine state from a saved dummy stack frame. */
1331
1332 void
1333 generic_pop_dummy_frame (void)
1334 {
1335   struct dummy_frame *dummy_frame = dummy_frame_stack;
1336
1337   /* FIXME: what if the first frame isn't the right one, eg..
1338      because one call-by-hand function has done a longjmp into another one? */
1339
1340   if (!dummy_frame)
1341     error ("Can't pop dummy frame!");
1342   dummy_frame_stack = dummy_frame->next;
1343   regcache_cpy (current_regcache, dummy_frame->regcache);
1344   flush_cached_frames ();
1345
1346   regcache_xfree (dummy_frame->regcache);
1347   xfree (dummy_frame);
1348 }
1349
1350 /* Function: frame_chain_valid 
1351    Returns true for a user frame or a call_function_by_hand dummy frame,
1352    and false for the CRT0 start-up frame.  Purpose is to terminate backtrace */
1353
1354 int
1355 generic_file_frame_chain_valid (CORE_ADDR fp, struct frame_info *fi)
1356 {
1357   if (PC_IN_CALL_DUMMY (FRAME_SAVED_PC (fi), fp, fp))
1358     return 1;                   /* don't prune CALL_DUMMY frames */
1359   else                          /* fall back to default algorithm (see frame.h) */
1360     return (fp != 0
1361             && (INNER_THAN (fi->frame, fp) || fi->frame == fp)
1362             && !inside_entry_file (FRAME_SAVED_PC (fi)));
1363 }
1364
1365 int
1366 generic_func_frame_chain_valid (CORE_ADDR fp, struct frame_info *fi)
1367 {
1368   if (USE_GENERIC_DUMMY_FRAMES
1369       && PC_IN_CALL_DUMMY ((fi)->pc, 0, 0))
1370     return 1;                   /* don't prune CALL_DUMMY frames */
1371   else                          /* fall back to default algorithm (see frame.h) */
1372     return (fp != 0
1373             && (INNER_THAN (fi->frame, fp) || fi->frame == fp)
1374             && !inside_main_func ((fi)->pc)
1375             && !inside_entry_func ((fi)->pc));
1376 }
1377
1378 /* Function: fix_call_dummy
1379    Stub function.  Generic dummy frames typically do not need to fix
1380    the frame being created */
1381
1382 void
1383 generic_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
1384                         struct value **args, struct type *type, int gcc_p)
1385 {
1386   return;
1387 }
1388
1389 /* Given a call-dummy dummy-frame, return the registers.  Here the
1390    register value is taken from the local copy of the register buffer.  */
1391
1392 static void
1393 generic_call_dummy_register_unwind (struct frame_info *frame, void **cache,
1394                                     int regnum, int *optimized,
1395                                     enum lval_type *lvalp, CORE_ADDR *addrp,
1396                                     int *realnum, void *bufferp)
1397 {
1398   gdb_assert (frame != NULL);
1399   gdb_assert (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame));
1400
1401   /* Describe the register's location.  Generic dummy frames always
1402      have the register value in an ``expression''.  */
1403   *optimized = 0;
1404   *lvalp = not_lval;
1405   *addrp = 0;
1406   *realnum = -1;
1407
1408   /* If needed, find and return the value of the register.  */
1409   if (bufferp != NULL)
1410     {
1411       struct regcache *registers;
1412 #if 1
1413       /* Get the address of the register buffer that contains all the
1414          saved registers for this dummy frame.  Cache that address.  */
1415       registers = (*cache);
1416       if (registers == NULL)
1417         {
1418           registers = generic_find_dummy_frame (frame->pc, frame->frame);
1419           (*cache) = registers;
1420         }
1421 #else
1422       /* Get the address of the register buffer that contains the
1423          saved registers and then extract the value from that.  */
1424       registers = generic_find_dummy_frame (frame->pc, frame->frame);
1425 #endif
1426       gdb_assert (registers != NULL);
1427       /* Return the actual value.  */
1428       /* Use the regcache_cooked_read() method so that it, on the fly,
1429          constructs either a raw or pseudo register from the raw
1430          register cache.  */
1431       regcache_cooked_read (registers, regnum, bufferp);
1432     }
1433 }
1434
1435 /* Return the register saved in the simplistic ``saved_regs'' cache.
1436    If the value isn't here AND a value is needed, try the next inner
1437    most frame.  */
1438
1439 static void
1440 frame_saved_regs_register_unwind (struct frame_info *frame, void **cache,
1441                                   int regnum, int *optimizedp,
1442                                   enum lval_type *lvalp, CORE_ADDR *addrp,
1443                                   int *realnump, void *bufferp)
1444 {
1445   /* There is always a frame at this point.  And THIS is the frame
1446      we're interested in.  */
1447   gdb_assert (frame != NULL);
1448   /* If we're using generic dummy frames, we'd better not be in a call
1449      dummy.  (generic_call_dummy_register_unwind ought to have been called
1450      instead.)  */
1451   gdb_assert (!(USE_GENERIC_DUMMY_FRAMES
1452                 && PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)));
1453
1454   /* Load the saved_regs register cache.  */
1455   if (frame->saved_regs == NULL)
1456     FRAME_INIT_SAVED_REGS (frame);
1457
1458   if (frame->saved_regs != NULL
1459       && frame->saved_regs[regnum] != 0)
1460     {
1461       if (regnum == SP_REGNUM)
1462         {
1463           /* SP register treated specially.  */
1464           *optimizedp = 0;
1465           *lvalp = not_lval;
1466           *addrp = 0;
1467           *realnump = -1;
1468           if (bufferp != NULL)
1469             store_address (bufferp, REGISTER_RAW_SIZE (regnum),
1470                            frame->saved_regs[regnum]);
1471         }
1472       else
1473         {
1474           /* Any other register is saved in memory, fetch it but cache
1475              a local copy of its value.  */
1476           *optimizedp = 0;
1477           *lvalp = lval_memory;
1478           *addrp = frame->saved_regs[regnum];
1479           *realnump = -1;
1480           if (bufferp != NULL)
1481             {
1482 #if 1
1483               /* Save each register value, as it is read in, in a
1484                  frame based cache.  */
1485               void **regs = (*cache);
1486               if (regs == NULL)
1487                 {
1488                   int sizeof_cache = ((NUM_REGS + NUM_PSEUDO_REGS)
1489                                       * sizeof (void *));
1490                   regs = frame_obstack_alloc (sizeof_cache);
1491                   memset (regs, 0, sizeof_cache);
1492                   (*cache) = regs;
1493                 }
1494               if (regs[regnum] == NULL)
1495                 {
1496                   regs[regnum]
1497                     = frame_obstack_alloc (REGISTER_RAW_SIZE (regnum));
1498                   read_memory (frame->saved_regs[regnum], regs[regnum],
1499                                REGISTER_RAW_SIZE (regnum));
1500                 }
1501               memcpy (bufferp, regs[regnum], REGISTER_RAW_SIZE (regnum));
1502 #else
1503               /* Read the value in from memory.  */
1504               read_memory (frame->saved_regs[regnum], bufferp,
1505                            REGISTER_RAW_SIZE (regnum));
1506 #endif
1507             }
1508         }
1509       return;
1510     }
1511
1512   /* No luck, assume this and the next frame have the same register
1513      value.  If a value is needed, pass the request on down the chain;
1514      otherwise just return an indication that the value is in the same
1515      register as the next frame.  */
1516   if (bufferp == NULL)
1517     {
1518       *optimizedp = 0;
1519       *lvalp = lval_register;
1520       *addrp = 0;
1521       *realnump = regnum;
1522     }
1523   else
1524     {
1525       frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp,
1526                              realnump, bufferp);
1527     }
1528 }
1529
1530 /* Function: get_saved_register
1531    Find register number REGNUM relative to FRAME and put its (raw,
1532    target format) contents in *RAW_BUFFER.  
1533
1534    Set *OPTIMIZED if the variable was optimized out (and thus can't be
1535    fetched).  Note that this is never set to anything other than zero
1536    in this implementation.
1537
1538    Set *LVAL to lval_memory, lval_register, or not_lval, depending on
1539    whether the value was fetched from memory, from a register, or in a
1540    strange and non-modifiable way (e.g. a frame pointer which was
1541    calculated rather than fetched).  We will use not_lval for values
1542    fetched from generic dummy frames.
1543
1544    Set *ADDRP to the address, either in memory or as a REGISTER_BYTE
1545    offset into the registers array.  If the value is stored in a dummy
1546    frame, set *ADDRP to zero.
1547
1548    To use this implementation, define a function called
1549    "get_saved_register" in your target code, which simply passes all
1550    of its arguments to this function.
1551
1552    The argument RAW_BUFFER must point to aligned memory.  */
1553
1554 void
1555 generic_get_saved_register (char *raw_buffer, int *optimized, CORE_ADDR *addrp,
1556                             struct frame_info *frame, int regnum,
1557                             enum lval_type *lval)
1558 {
1559   if (!target_has_registers)
1560     error ("No registers.");
1561
1562   /* Normal systems don't optimize out things with register numbers.  */
1563   if (optimized != NULL)
1564     *optimized = 0;
1565
1566   if (addrp)                    /* default assumption: not found in memory */
1567     *addrp = 0;
1568
1569   /* Note: since the current frame's registers could only have been
1570      saved by frames INTERIOR TO the current frame, we skip examining
1571      the current frame itself: otherwise, we would be getting the
1572      previous frame's registers which were saved by the current frame.  */
1573
1574   while (frame && ((frame = frame->next) != NULL))
1575     {
1576       if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
1577         {
1578           if (lval)             /* found it in a CALL_DUMMY frame */
1579             *lval = not_lval;
1580           if (raw_buffer)
1581             /* FIXME: cagney/2002-06-26: This should be via the
1582                gdbarch_register_read() method so that it, on the fly,
1583                constructs either a raw or pseudo register from the raw
1584                register cache.  */
1585             regcache_raw_read (generic_find_dummy_frame (frame->pc,
1586                                                          frame->frame),
1587                                regnum, raw_buffer);
1588           return;
1589         }
1590
1591       FRAME_INIT_SAVED_REGS (frame);
1592       if (frame->saved_regs != NULL
1593           && frame->saved_regs[regnum] != 0)
1594         {
1595           if (lval)             /* found it saved on the stack */
1596             *lval = lval_memory;
1597           if (regnum == SP_REGNUM)
1598             {
1599               if (raw_buffer)   /* SP register treated specially */
1600                 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
1601                                frame->saved_regs[regnum]);
1602             }
1603           else
1604             {
1605               if (addrp)        /* any other register */
1606                 *addrp = frame->saved_regs[regnum];
1607               if (raw_buffer)
1608                 read_memory (frame->saved_regs[regnum], raw_buffer,
1609                              REGISTER_RAW_SIZE (regnum));
1610             }
1611           return;
1612         }
1613     }
1614
1615   /* If we get thru the loop to this point, it means the register was
1616      not saved in any frame.  Return the actual live-register value.  */
1617
1618   if (lval)                     /* found it in a live register */
1619     *lval = lval_register;
1620   if (addrp)
1621     *addrp = REGISTER_BYTE (regnum);
1622   if (raw_buffer)
1623     read_register_gen (regnum, raw_buffer);
1624 }
1625
1626 void
1627 _initialize_blockframe (void)
1628 {
1629   obstack_init (&frame_cache_obstack);
1630 }