xref: /dflybsd-src/contrib/gdb-7/gdb/frame.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Cache and manage frames for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "frame.h"
225796c8dcSSimon Schubert #include "target.h"
235796c8dcSSimon Schubert #include "value.h"
245796c8dcSSimon Schubert #include "inferior.h"	/* for inferior_ptid */
255796c8dcSSimon Schubert #include "regcache.h"
265796c8dcSSimon Schubert #include "gdb_assert.h"
275796c8dcSSimon Schubert #include "gdb_string.h"
285796c8dcSSimon Schubert #include "user-regs.h"
295796c8dcSSimon Schubert #include "gdb_obstack.h"
305796c8dcSSimon Schubert #include "dummy-frame.h"
315796c8dcSSimon Schubert #include "sentinel-frame.h"
325796c8dcSSimon Schubert #include "gdbcore.h"
335796c8dcSSimon Schubert #include "annotate.h"
345796c8dcSSimon Schubert #include "language.h"
355796c8dcSSimon Schubert #include "frame-unwind.h"
365796c8dcSSimon Schubert #include "frame-base.h"
375796c8dcSSimon Schubert #include "command.h"
385796c8dcSSimon Schubert #include "gdbcmd.h"
395796c8dcSSimon Schubert #include "observer.h"
405796c8dcSSimon Schubert #include "objfiles.h"
415796c8dcSSimon Schubert #include "exceptions.h"
425796c8dcSSimon Schubert #include "gdbthread.h"
435796c8dcSSimon Schubert #include "block.h"
445796c8dcSSimon Schubert #include "inline-frame.h"
45cf7f2e2dSJohn Marino #include "tracepoint.h"
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert static struct frame_info *get_prev_frame_1 (struct frame_info *this_frame);
485796c8dcSSimon Schubert static struct frame_info *get_prev_frame_raw (struct frame_info *this_frame);
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert /* We keep a cache of stack frames, each of which is a "struct
515796c8dcSSimon Schubert    frame_info".  The innermost one gets allocated (in
525796c8dcSSimon Schubert    wait_for_inferior) each time the inferior stops; current_frame
535796c8dcSSimon Schubert    points to it.  Additional frames get allocated (in get_prev_frame)
545796c8dcSSimon Schubert    as needed, and are chained through the next and prev fields.  Any
555796c8dcSSimon Schubert    time that the frame cache becomes invalid (most notably when we
565796c8dcSSimon Schubert    execute something, but also if we change how we interpret the
575796c8dcSSimon Schubert    frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
585796c8dcSSimon Schubert    which reads new symbols)), we should call reinit_frame_cache.  */
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert struct frame_info
615796c8dcSSimon Schubert {
625796c8dcSSimon Schubert   /* Level of this frame.  The inner-most (youngest) frame is at level
635796c8dcSSimon Schubert      0.  As you move towards the outer-most (oldest) frame, the level
645796c8dcSSimon Schubert      increases.  This is a cached value.  It could just as easily be
655796c8dcSSimon Schubert      computed by counting back from the selected frame to the inner
665796c8dcSSimon Schubert      most frame.  */
675796c8dcSSimon Schubert   /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
685796c8dcSSimon Schubert      reserved to indicate a bogus frame - one that has been created
695796c8dcSSimon Schubert      just to keep GDB happy (GDB always needs a frame).  For the
705796c8dcSSimon Schubert      moment leave this as speculation.  */
715796c8dcSSimon Schubert   int level;
725796c8dcSSimon Schubert 
73cf7f2e2dSJohn Marino   /* The frame's program space.  */
74cf7f2e2dSJohn Marino   struct program_space *pspace;
75cf7f2e2dSJohn Marino 
76cf7f2e2dSJohn Marino   /* The frame's address space.  */
77cf7f2e2dSJohn Marino   struct address_space *aspace;
78cf7f2e2dSJohn Marino 
795796c8dcSSimon Schubert   /* The frame's low-level unwinder and corresponding cache.  The
805796c8dcSSimon Schubert      low-level unwinder is responsible for unwinding register values
815796c8dcSSimon Schubert      for the previous frame.  The low-level unwind methods are
825796c8dcSSimon Schubert      selected based on the presence, or otherwise, of register unwind
835796c8dcSSimon Schubert      information such as CFI.  */
845796c8dcSSimon Schubert   void *prologue_cache;
855796c8dcSSimon Schubert   const struct frame_unwind *unwind;
865796c8dcSSimon Schubert 
875796c8dcSSimon Schubert   /* Cached copy of the previous frame's architecture.  */
885796c8dcSSimon Schubert   struct
895796c8dcSSimon Schubert   {
905796c8dcSSimon Schubert     int p;
915796c8dcSSimon Schubert     struct gdbarch *arch;
925796c8dcSSimon Schubert   } prev_arch;
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert   /* Cached copy of the previous frame's resume address.  */
955796c8dcSSimon Schubert   struct {
965796c8dcSSimon Schubert     int p;
975796c8dcSSimon Schubert     CORE_ADDR value;
985796c8dcSSimon Schubert   } prev_pc;
995796c8dcSSimon Schubert 
1005796c8dcSSimon Schubert   /* Cached copy of the previous frame's function address.  */
1015796c8dcSSimon Schubert   struct
1025796c8dcSSimon Schubert   {
1035796c8dcSSimon Schubert     CORE_ADDR addr;
1045796c8dcSSimon Schubert     int p;
1055796c8dcSSimon Schubert   } prev_func;
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert   /* This frame's ID.  */
1085796c8dcSSimon Schubert   struct
1095796c8dcSSimon Schubert   {
1105796c8dcSSimon Schubert     int p;
1115796c8dcSSimon Schubert     struct frame_id value;
1125796c8dcSSimon Schubert   } this_id;
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert   /* The frame's high-level base methods, and corresponding cache.
1155796c8dcSSimon Schubert      The high level base methods are selected based on the frame's
1165796c8dcSSimon Schubert      debug info.  */
1175796c8dcSSimon Schubert   const struct frame_base *base;
1185796c8dcSSimon Schubert   void *base_cache;
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert   /* Pointers to the next (down, inner, younger) and previous (up,
1215796c8dcSSimon Schubert      outer, older) frame_info's in the frame cache.  */
1225796c8dcSSimon Schubert   struct frame_info *next; /* down, inner, younger */
1235796c8dcSSimon Schubert   int prev_p;
1245796c8dcSSimon Schubert   struct frame_info *prev; /* up, outer, older */
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert   /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
1275796c8dcSSimon Schubert      could.  Only valid when PREV_P is set.  */
1285796c8dcSSimon Schubert   enum unwind_stop_reason stop_reason;
1295796c8dcSSimon Schubert };
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert /* A frame stash used to speed up frame lookups.  */
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert /* We currently only stash one frame at a time, as this seems to be
1345796c8dcSSimon Schubert    sufficient for now.  */
1355796c8dcSSimon Schubert static struct frame_info *frame_stash = NULL;
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert /* Add the following FRAME to the frame stash.  */
1385796c8dcSSimon Schubert 
1395796c8dcSSimon Schubert static void
frame_stash_add(struct frame_info * frame)1405796c8dcSSimon Schubert frame_stash_add (struct frame_info *frame)
1415796c8dcSSimon Schubert {
1425796c8dcSSimon Schubert   frame_stash = frame;
1435796c8dcSSimon Schubert }
1445796c8dcSSimon Schubert 
1455796c8dcSSimon Schubert /* Search the frame stash for an entry with the given frame ID.
1465796c8dcSSimon Schubert    If found, return that frame.  Otherwise return NULL.  */
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert static struct frame_info *
frame_stash_find(struct frame_id id)1495796c8dcSSimon Schubert frame_stash_find (struct frame_id id)
1505796c8dcSSimon Schubert {
1515796c8dcSSimon Schubert   if (frame_stash && frame_id_eq (frame_stash->this_id.value, id))
1525796c8dcSSimon Schubert     return frame_stash;
1535796c8dcSSimon Schubert 
1545796c8dcSSimon Schubert   return NULL;
1555796c8dcSSimon Schubert }
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert /* Invalidate the frame stash by removing all entries in it.  */
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert static void
frame_stash_invalidate(void)1605796c8dcSSimon Schubert frame_stash_invalidate (void)
1615796c8dcSSimon Schubert {
1625796c8dcSSimon Schubert   frame_stash = NULL;
1635796c8dcSSimon Schubert }
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert /* Flag to control debugging.  */
1665796c8dcSSimon Schubert 
167*ef5ccd6cSJohn Marino unsigned int frame_debug;
1685796c8dcSSimon Schubert static void
show_frame_debug(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1695796c8dcSSimon Schubert show_frame_debug (struct ui_file *file, int from_tty,
1705796c8dcSSimon Schubert 		  struct cmd_list_element *c, const char *value)
1715796c8dcSSimon Schubert {
1725796c8dcSSimon Schubert   fprintf_filtered (file, _("Frame debugging is %s.\n"), value);
1735796c8dcSSimon Schubert }
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert /* Flag to indicate whether backtraces should stop at main et.al.  */
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert static int backtrace_past_main;
1785796c8dcSSimon Schubert static void
show_backtrace_past_main(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1795796c8dcSSimon Schubert show_backtrace_past_main (struct ui_file *file, int from_tty,
1805796c8dcSSimon Schubert 			  struct cmd_list_element *c, const char *value)
1815796c8dcSSimon Schubert {
182c50c785cSJohn Marino   fprintf_filtered (file,
183c50c785cSJohn Marino 		    _("Whether backtraces should "
184c50c785cSJohn Marino 		      "continue past \"main\" is %s.\n"),
1855796c8dcSSimon Schubert 		    value);
1865796c8dcSSimon Schubert }
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert static int backtrace_past_entry;
1895796c8dcSSimon Schubert static void
show_backtrace_past_entry(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1905796c8dcSSimon Schubert show_backtrace_past_entry (struct ui_file *file, int from_tty,
1915796c8dcSSimon Schubert 			   struct cmd_list_element *c, const char *value)
1925796c8dcSSimon Schubert {
193c50c785cSJohn Marino   fprintf_filtered (file, _("Whether backtraces should continue past the "
194c50c785cSJohn Marino 			    "entry point of a program is %s.\n"),
1955796c8dcSSimon Schubert 		    value);
1965796c8dcSSimon Schubert }
1975796c8dcSSimon Schubert 
198*ef5ccd6cSJohn Marino static unsigned int backtrace_limit = UINT_MAX;
1995796c8dcSSimon Schubert static void
show_backtrace_limit(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2005796c8dcSSimon Schubert show_backtrace_limit (struct ui_file *file, int from_tty,
2015796c8dcSSimon Schubert 		      struct cmd_list_element *c, const char *value)
2025796c8dcSSimon Schubert {
203c50c785cSJohn Marino   fprintf_filtered (file,
204c50c785cSJohn Marino 		    _("An upper bound on the number "
205c50c785cSJohn Marino 		      "of backtrace levels is %s.\n"),
2065796c8dcSSimon Schubert 		    value);
2075796c8dcSSimon Schubert }
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert 
2105796c8dcSSimon Schubert static void
fprint_field(struct ui_file * file,const char * name,int p,CORE_ADDR addr)2115796c8dcSSimon Schubert fprint_field (struct ui_file *file, const char *name, int p, CORE_ADDR addr)
2125796c8dcSSimon Schubert {
2135796c8dcSSimon Schubert   if (p)
2145796c8dcSSimon Schubert     fprintf_unfiltered (file, "%s=%s", name, hex_string (addr));
2155796c8dcSSimon Schubert   else
2165796c8dcSSimon Schubert     fprintf_unfiltered (file, "!%s", name);
2175796c8dcSSimon Schubert }
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert void
fprint_frame_id(struct ui_file * file,struct frame_id id)2205796c8dcSSimon Schubert fprint_frame_id (struct ui_file *file, struct frame_id id)
2215796c8dcSSimon Schubert {
2225796c8dcSSimon Schubert   fprintf_unfiltered (file, "{");
2235796c8dcSSimon Schubert   fprint_field (file, "stack", id.stack_addr_p, id.stack_addr);
2245796c8dcSSimon Schubert   fprintf_unfiltered (file, ",");
2255796c8dcSSimon Schubert   fprint_field (file, "code", id.code_addr_p, id.code_addr);
2265796c8dcSSimon Schubert   fprintf_unfiltered (file, ",");
2275796c8dcSSimon Schubert   fprint_field (file, "special", id.special_addr_p, id.special_addr);
228*ef5ccd6cSJohn Marino   if (id.artificial_depth)
229*ef5ccd6cSJohn Marino     fprintf_unfiltered (file, ",artificial=%d", id.artificial_depth);
2305796c8dcSSimon Schubert   fprintf_unfiltered (file, "}");
2315796c8dcSSimon Schubert }
2325796c8dcSSimon Schubert 
2335796c8dcSSimon Schubert static void
fprint_frame_type(struct ui_file * file,enum frame_type type)2345796c8dcSSimon Schubert fprint_frame_type (struct ui_file *file, enum frame_type type)
2355796c8dcSSimon Schubert {
2365796c8dcSSimon Schubert   switch (type)
2375796c8dcSSimon Schubert     {
2385796c8dcSSimon Schubert     case NORMAL_FRAME:
2395796c8dcSSimon Schubert       fprintf_unfiltered (file, "NORMAL_FRAME");
2405796c8dcSSimon Schubert       return;
2415796c8dcSSimon Schubert     case DUMMY_FRAME:
2425796c8dcSSimon Schubert       fprintf_unfiltered (file, "DUMMY_FRAME");
2435796c8dcSSimon Schubert       return;
2445796c8dcSSimon Schubert     case INLINE_FRAME:
2455796c8dcSSimon Schubert       fprintf_unfiltered (file, "INLINE_FRAME");
2465796c8dcSSimon Schubert       return;
2475796c8dcSSimon Schubert     case SENTINEL_FRAME:
2485796c8dcSSimon Schubert       fprintf_unfiltered (file, "SENTINEL_FRAME");
2495796c8dcSSimon Schubert       return;
2505796c8dcSSimon Schubert     case SIGTRAMP_FRAME:
2515796c8dcSSimon Schubert       fprintf_unfiltered (file, "SIGTRAMP_FRAME");
2525796c8dcSSimon Schubert       return;
2535796c8dcSSimon Schubert     case ARCH_FRAME:
2545796c8dcSSimon Schubert       fprintf_unfiltered (file, "ARCH_FRAME");
2555796c8dcSSimon Schubert       return;
2565796c8dcSSimon Schubert     default:
2575796c8dcSSimon Schubert       fprintf_unfiltered (file, "<unknown type>");
2585796c8dcSSimon Schubert       return;
2595796c8dcSSimon Schubert     };
2605796c8dcSSimon Schubert }
2615796c8dcSSimon Schubert 
2625796c8dcSSimon Schubert static void
fprint_frame(struct ui_file * file,struct frame_info * fi)2635796c8dcSSimon Schubert fprint_frame (struct ui_file *file, struct frame_info *fi)
2645796c8dcSSimon Schubert {
2655796c8dcSSimon Schubert   if (fi == NULL)
2665796c8dcSSimon Schubert     {
2675796c8dcSSimon Schubert       fprintf_unfiltered (file, "<NULL frame>");
2685796c8dcSSimon Schubert       return;
2695796c8dcSSimon Schubert     }
2705796c8dcSSimon Schubert   fprintf_unfiltered (file, "{");
2715796c8dcSSimon Schubert   fprintf_unfiltered (file, "level=%d", fi->level);
2725796c8dcSSimon Schubert   fprintf_unfiltered (file, ",");
2735796c8dcSSimon Schubert   fprintf_unfiltered (file, "type=");
2745796c8dcSSimon Schubert   if (fi->unwind != NULL)
2755796c8dcSSimon Schubert     fprint_frame_type (file, fi->unwind->type);
2765796c8dcSSimon Schubert   else
2775796c8dcSSimon Schubert     fprintf_unfiltered (file, "<unknown>");
2785796c8dcSSimon Schubert   fprintf_unfiltered (file, ",");
2795796c8dcSSimon Schubert   fprintf_unfiltered (file, "unwind=");
2805796c8dcSSimon Schubert   if (fi->unwind != NULL)
2815796c8dcSSimon Schubert     gdb_print_host_address (fi->unwind, file);
2825796c8dcSSimon Schubert   else
2835796c8dcSSimon Schubert     fprintf_unfiltered (file, "<unknown>");
2845796c8dcSSimon Schubert   fprintf_unfiltered (file, ",");
2855796c8dcSSimon Schubert   fprintf_unfiltered (file, "pc=");
2865796c8dcSSimon Schubert   if (fi->next != NULL && fi->next->prev_pc.p)
2875796c8dcSSimon Schubert     fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_pc.value));
2885796c8dcSSimon Schubert   else
2895796c8dcSSimon Schubert     fprintf_unfiltered (file, "<unknown>");
2905796c8dcSSimon Schubert   fprintf_unfiltered (file, ",");
2915796c8dcSSimon Schubert   fprintf_unfiltered (file, "id=");
2925796c8dcSSimon Schubert   if (fi->this_id.p)
2935796c8dcSSimon Schubert     fprint_frame_id (file, fi->this_id.value);
2945796c8dcSSimon Schubert   else
2955796c8dcSSimon Schubert     fprintf_unfiltered (file, "<unknown>");
2965796c8dcSSimon Schubert   fprintf_unfiltered (file, ",");
2975796c8dcSSimon Schubert   fprintf_unfiltered (file, "func=");
2985796c8dcSSimon Schubert   if (fi->next != NULL && fi->next->prev_func.p)
2995796c8dcSSimon Schubert     fprintf_unfiltered (file, "%s", hex_string (fi->next->prev_func.addr));
3005796c8dcSSimon Schubert   else
3015796c8dcSSimon Schubert     fprintf_unfiltered (file, "<unknown>");
3025796c8dcSSimon Schubert   fprintf_unfiltered (file, "}");
3035796c8dcSSimon Schubert }
3045796c8dcSSimon Schubert 
305*ef5ccd6cSJohn Marino /* Given FRAME, return the enclosing frame as found in real frames read-in from
306*ef5ccd6cSJohn Marino    inferior memory.  Skip any previous frames which were made up by GDB.
307*ef5ccd6cSJohn Marino    Return the original frame if no immediate previous frames exist.  */
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert static struct frame_info *
skip_artificial_frames(struct frame_info * frame)310*ef5ccd6cSJohn Marino skip_artificial_frames (struct frame_info *frame)
3115796c8dcSSimon Schubert {
312*ef5ccd6cSJohn Marino   while (get_frame_type (frame) == INLINE_FRAME
313*ef5ccd6cSJohn Marino 	 || get_frame_type (frame) == TAILCALL_FRAME)
3145796c8dcSSimon Schubert     frame = get_prev_frame (frame);
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert   return frame;
3175796c8dcSSimon Schubert }
3185796c8dcSSimon Schubert 
3195796c8dcSSimon Schubert /* Return a frame uniq ID that can be used to, later, re-find the
3205796c8dcSSimon Schubert    frame.  */
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert struct frame_id
get_frame_id(struct frame_info * fi)3235796c8dcSSimon Schubert get_frame_id (struct frame_info *fi)
3245796c8dcSSimon Schubert {
3255796c8dcSSimon Schubert   if (fi == NULL)
3265796c8dcSSimon Schubert     return null_frame_id;
3275796c8dcSSimon Schubert 
3285796c8dcSSimon Schubert   if (!fi->this_id.p)
3295796c8dcSSimon Schubert     {
3305796c8dcSSimon Schubert       if (frame_debug)
3315796c8dcSSimon Schubert 	fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ",
3325796c8dcSSimon Schubert 			    fi->level);
3335796c8dcSSimon Schubert       /* Find the unwinder.  */
3345796c8dcSSimon Schubert       if (fi->unwind == NULL)
335c50c785cSJohn Marino 	frame_unwind_find_by_frame (fi, &fi->prologue_cache);
3365796c8dcSSimon Schubert       /* Find THIS frame's ID.  */
3375796c8dcSSimon Schubert       /* Default to outermost if no ID is found.  */
3385796c8dcSSimon Schubert       fi->this_id.value = outer_frame_id;
3395796c8dcSSimon Schubert       fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
3405796c8dcSSimon Schubert       gdb_assert (frame_id_p (fi->this_id.value));
3415796c8dcSSimon Schubert       fi->this_id.p = 1;
3425796c8dcSSimon Schubert       if (frame_debug)
3435796c8dcSSimon Schubert 	{
3445796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "-> ");
3455796c8dcSSimon Schubert 	  fprint_frame_id (gdb_stdlog, fi->this_id.value);
3465796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, " }\n");
3475796c8dcSSimon Schubert 	}
3485796c8dcSSimon Schubert     }
3495796c8dcSSimon Schubert 
3505796c8dcSSimon Schubert   frame_stash_add (fi);
3515796c8dcSSimon Schubert 
3525796c8dcSSimon Schubert   return fi->this_id.value;
3535796c8dcSSimon Schubert }
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert struct frame_id
get_stack_frame_id(struct frame_info * next_frame)3565796c8dcSSimon Schubert get_stack_frame_id (struct frame_info *next_frame)
3575796c8dcSSimon Schubert {
358*ef5ccd6cSJohn Marino   return get_frame_id (skip_artificial_frames (next_frame));
3595796c8dcSSimon Schubert }
3605796c8dcSSimon Schubert 
3615796c8dcSSimon Schubert struct frame_id
frame_unwind_caller_id(struct frame_info * next_frame)3625796c8dcSSimon Schubert frame_unwind_caller_id (struct frame_info *next_frame)
3635796c8dcSSimon Schubert {
3645796c8dcSSimon Schubert   struct frame_info *this_frame;
3655796c8dcSSimon Schubert 
3665796c8dcSSimon Schubert   /* Use get_prev_frame_1, and not get_prev_frame.  The latter will truncate
3675796c8dcSSimon Schubert      the frame chain, leading to this function unintentionally
3685796c8dcSSimon Schubert      returning a null_frame_id (e.g., when a caller requests the frame
3695796c8dcSSimon Schubert      ID of "main()"s caller.  */
3705796c8dcSSimon Schubert 
371*ef5ccd6cSJohn Marino   next_frame = skip_artificial_frames (next_frame);
3725796c8dcSSimon Schubert   this_frame = get_prev_frame_1 (next_frame);
3735796c8dcSSimon Schubert   if (this_frame)
374*ef5ccd6cSJohn Marino     return get_frame_id (skip_artificial_frames (this_frame));
3755796c8dcSSimon Schubert   else
3765796c8dcSSimon Schubert     return null_frame_id;
3775796c8dcSSimon Schubert }
3785796c8dcSSimon Schubert 
3795796c8dcSSimon Schubert const struct frame_id null_frame_id; /* All zeros.  */
3805796c8dcSSimon Schubert const struct frame_id outer_frame_id = { 0, 0, 0, 0, 0, 1, 0 };
3815796c8dcSSimon Schubert 
3825796c8dcSSimon Schubert struct frame_id
frame_id_build_special(CORE_ADDR stack_addr,CORE_ADDR code_addr,CORE_ADDR special_addr)3835796c8dcSSimon Schubert frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
3845796c8dcSSimon Schubert                         CORE_ADDR special_addr)
3855796c8dcSSimon Schubert {
3865796c8dcSSimon Schubert   struct frame_id id = null_frame_id;
387cf7f2e2dSJohn Marino 
3885796c8dcSSimon Schubert   id.stack_addr = stack_addr;
3895796c8dcSSimon Schubert   id.stack_addr_p = 1;
3905796c8dcSSimon Schubert   id.code_addr = code_addr;
3915796c8dcSSimon Schubert   id.code_addr_p = 1;
3925796c8dcSSimon Schubert   id.special_addr = special_addr;
3935796c8dcSSimon Schubert   id.special_addr_p = 1;
3945796c8dcSSimon Schubert   return id;
3955796c8dcSSimon Schubert }
3965796c8dcSSimon Schubert 
3975796c8dcSSimon Schubert struct frame_id
frame_id_build(CORE_ADDR stack_addr,CORE_ADDR code_addr)3985796c8dcSSimon Schubert frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
3995796c8dcSSimon Schubert {
4005796c8dcSSimon Schubert   struct frame_id id = null_frame_id;
401cf7f2e2dSJohn Marino 
4025796c8dcSSimon Schubert   id.stack_addr = stack_addr;
4035796c8dcSSimon Schubert   id.stack_addr_p = 1;
4045796c8dcSSimon Schubert   id.code_addr = code_addr;
4055796c8dcSSimon Schubert   id.code_addr_p = 1;
4065796c8dcSSimon Schubert   return id;
4075796c8dcSSimon Schubert }
4085796c8dcSSimon Schubert 
4095796c8dcSSimon Schubert struct frame_id
frame_id_build_wild(CORE_ADDR stack_addr)4105796c8dcSSimon Schubert frame_id_build_wild (CORE_ADDR stack_addr)
4115796c8dcSSimon Schubert {
4125796c8dcSSimon Schubert   struct frame_id id = null_frame_id;
413cf7f2e2dSJohn Marino 
4145796c8dcSSimon Schubert   id.stack_addr = stack_addr;
4155796c8dcSSimon Schubert   id.stack_addr_p = 1;
4165796c8dcSSimon Schubert   return id;
4175796c8dcSSimon Schubert }
4185796c8dcSSimon Schubert 
4195796c8dcSSimon Schubert int
frame_id_p(struct frame_id l)4205796c8dcSSimon Schubert frame_id_p (struct frame_id l)
4215796c8dcSSimon Schubert {
4225796c8dcSSimon Schubert   int p;
423cf7f2e2dSJohn Marino 
4245796c8dcSSimon Schubert   /* The frame is valid iff it has a valid stack address.  */
4255796c8dcSSimon Schubert   p = l.stack_addr_p;
4265796c8dcSSimon Schubert   /* outer_frame_id is also valid.  */
4275796c8dcSSimon Schubert   if (!p && memcmp (&l, &outer_frame_id, sizeof (l)) == 0)
4285796c8dcSSimon Schubert     p = 1;
4295796c8dcSSimon Schubert   if (frame_debug)
4305796c8dcSSimon Schubert     {
4315796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l=");
4325796c8dcSSimon Schubert       fprint_frame_id (gdb_stdlog, l);
4335796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p);
4345796c8dcSSimon Schubert     }
4355796c8dcSSimon Schubert   return p;
4365796c8dcSSimon Schubert }
4375796c8dcSSimon Schubert 
4385796c8dcSSimon Schubert int
frame_id_artificial_p(struct frame_id l)439*ef5ccd6cSJohn Marino frame_id_artificial_p (struct frame_id l)
4405796c8dcSSimon Schubert {
4415796c8dcSSimon Schubert   if (!frame_id_p (l))
4425796c8dcSSimon Schubert     return 0;
4435796c8dcSSimon Schubert 
444*ef5ccd6cSJohn Marino   return (l.artificial_depth != 0);
4455796c8dcSSimon Schubert }
4465796c8dcSSimon Schubert 
4475796c8dcSSimon Schubert int
frame_id_eq(struct frame_id l,struct frame_id r)4485796c8dcSSimon Schubert frame_id_eq (struct frame_id l, struct frame_id r)
4495796c8dcSSimon Schubert {
4505796c8dcSSimon Schubert   int eq;
451cf7f2e2dSJohn Marino 
452c50c785cSJohn Marino   if (!l.stack_addr_p && l.special_addr_p
453c50c785cSJohn Marino       && !r.stack_addr_p && r.special_addr_p)
4545796c8dcSSimon Schubert     /* The outermost frame marker is equal to itself.  This is the
4555796c8dcSSimon Schubert        dodgy thing about outer_frame_id, since between execution steps
4565796c8dcSSimon Schubert        we might step into another function - from which we can't
4575796c8dcSSimon Schubert        unwind either.  More thought required to get rid of
4585796c8dcSSimon Schubert        outer_frame_id.  */
4595796c8dcSSimon Schubert     eq = 1;
4605796c8dcSSimon Schubert   else if (!l.stack_addr_p || !r.stack_addr_p)
4615796c8dcSSimon Schubert     /* Like a NaN, if either ID is invalid, the result is false.
4625796c8dcSSimon Schubert        Note that a frame ID is invalid iff it is the null frame ID.  */
4635796c8dcSSimon Schubert     eq = 0;
4645796c8dcSSimon Schubert   else if (l.stack_addr != r.stack_addr)
4655796c8dcSSimon Schubert     /* If .stack addresses are different, the frames are different.  */
4665796c8dcSSimon Schubert     eq = 0;
4675796c8dcSSimon Schubert   else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
4685796c8dcSSimon Schubert     /* An invalid code addr is a wild card.  If .code addresses are
4695796c8dcSSimon Schubert        different, the frames are different.  */
4705796c8dcSSimon Schubert     eq = 0;
4715796c8dcSSimon Schubert   else if (l.special_addr_p && r.special_addr_p
4725796c8dcSSimon Schubert 	   && l.special_addr != r.special_addr)
4735796c8dcSSimon Schubert     /* An invalid special addr is a wild card (or unused).  Otherwise
4745796c8dcSSimon Schubert        if special addresses are different, the frames are different.  */
4755796c8dcSSimon Schubert     eq = 0;
476*ef5ccd6cSJohn Marino   else if (l.artificial_depth != r.artificial_depth)
477*ef5ccd6cSJohn Marino     /* If artifical depths are different, the frames must be different.  */
4785796c8dcSSimon Schubert     eq = 0;
4795796c8dcSSimon Schubert   else
4805796c8dcSSimon Schubert     /* Frames are equal.  */
4815796c8dcSSimon Schubert     eq = 1;
4825796c8dcSSimon Schubert 
4835796c8dcSSimon Schubert   if (frame_debug)
4845796c8dcSSimon Schubert     {
4855796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l=");
4865796c8dcSSimon Schubert       fprint_frame_id (gdb_stdlog, l);
4875796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, ",r=");
4885796c8dcSSimon Schubert       fprint_frame_id (gdb_stdlog, r);
4895796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq);
4905796c8dcSSimon Schubert     }
4915796c8dcSSimon Schubert   return eq;
4925796c8dcSSimon Schubert }
4935796c8dcSSimon Schubert 
4945796c8dcSSimon Schubert /* Safety net to check whether frame ID L should be inner to
4955796c8dcSSimon Schubert    frame ID R, according to their stack addresses.
4965796c8dcSSimon Schubert 
4975796c8dcSSimon Schubert    This method cannot be used to compare arbitrary frames, as the
4985796c8dcSSimon Schubert    ranges of valid stack addresses may be discontiguous (e.g. due
4995796c8dcSSimon Schubert    to sigaltstack).
5005796c8dcSSimon Schubert 
5015796c8dcSSimon Schubert    However, it can be used as safety net to discover invalid frame
5025796c8dcSSimon Schubert    IDs in certain circumstances.  Assuming that NEXT is the immediate
5035796c8dcSSimon Schubert    inner frame to THIS and that NEXT and THIS are both NORMAL frames:
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert    * The stack address of NEXT must be inner-than-or-equal to the stack
5065796c8dcSSimon Schubert      address of THIS.
5075796c8dcSSimon Schubert 
5085796c8dcSSimon Schubert      Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
5095796c8dcSSimon Schubert      error has occurred.
5105796c8dcSSimon Schubert 
5115796c8dcSSimon Schubert    * If NEXT and THIS have different stack addresses, no other frame
5125796c8dcSSimon Schubert      in the frame chain may have a stack address in between.
5135796c8dcSSimon Schubert 
5145796c8dcSSimon Schubert      Therefore, if frame_id_inner (TEST, THIS) holds, but
5155796c8dcSSimon Schubert      frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
5165796c8dcSSimon Schubert      to a valid frame in the frame chain.
5175796c8dcSSimon Schubert 
5185796c8dcSSimon Schubert    The sanity checks above cannot be performed when a SIGTRAMP frame
5195796c8dcSSimon Schubert    is involved, because signal handlers might be executed on a different
5205796c8dcSSimon Schubert    stack than the stack used by the routine that caused the signal
5215796c8dcSSimon Schubert    to be raised.  This can happen for instance when a thread exceeds
5225796c8dcSSimon Schubert    its maximum stack size.  In this case, certain compilers implement
5235796c8dcSSimon Schubert    a stack overflow strategy that cause the handler to be run on a
5245796c8dcSSimon Schubert    different stack.  */
5255796c8dcSSimon Schubert 
5265796c8dcSSimon Schubert static int
frame_id_inner(struct gdbarch * gdbarch,struct frame_id l,struct frame_id r)5275796c8dcSSimon Schubert frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
5285796c8dcSSimon Schubert {
5295796c8dcSSimon Schubert   int inner;
530cf7f2e2dSJohn Marino 
5315796c8dcSSimon Schubert   if (!l.stack_addr_p || !r.stack_addr_p)
5325796c8dcSSimon Schubert     /* Like NaN, any operation involving an invalid ID always fails.  */
5335796c8dcSSimon Schubert     inner = 0;
534*ef5ccd6cSJohn Marino   else if (l.artificial_depth > r.artificial_depth
5355796c8dcSSimon Schubert 	   && l.stack_addr == r.stack_addr
5365796c8dcSSimon Schubert 	   && l.code_addr_p == r.code_addr_p
5375796c8dcSSimon Schubert 	   && l.special_addr_p == r.special_addr_p
5385796c8dcSSimon Schubert 	   && l.special_addr == r.special_addr)
5395796c8dcSSimon Schubert     {
5405796c8dcSSimon Schubert       /* Same function, different inlined functions.  */
5415796c8dcSSimon Schubert       struct block *lb, *rb;
5425796c8dcSSimon Schubert 
5435796c8dcSSimon Schubert       gdb_assert (l.code_addr_p && r.code_addr_p);
5445796c8dcSSimon Schubert 
5455796c8dcSSimon Schubert       lb = block_for_pc (l.code_addr);
5465796c8dcSSimon Schubert       rb = block_for_pc (r.code_addr);
5475796c8dcSSimon Schubert 
5485796c8dcSSimon Schubert       if (lb == NULL || rb == NULL)
5495796c8dcSSimon Schubert 	/* Something's gone wrong.  */
5505796c8dcSSimon Schubert 	inner = 0;
5515796c8dcSSimon Schubert       else
5525796c8dcSSimon Schubert 	/* This will return true if LB and RB are the same block, or
5535796c8dcSSimon Schubert 	   if the block with the smaller depth lexically encloses the
5545796c8dcSSimon Schubert 	   block with the greater depth.  */
5555796c8dcSSimon Schubert 	inner = contained_in (lb, rb);
5565796c8dcSSimon Schubert     }
5575796c8dcSSimon Schubert   else
5585796c8dcSSimon Schubert     /* Only return non-zero when strictly inner than.  Note that, per
5595796c8dcSSimon Schubert        comment in "frame.h", there is some fuzz here.  Frameless
5605796c8dcSSimon Schubert        functions are not strictly inner than (same .stack but
5615796c8dcSSimon Schubert        different .code and/or .special address).  */
5625796c8dcSSimon Schubert     inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
5635796c8dcSSimon Schubert   if (frame_debug)
5645796c8dcSSimon Schubert     {
5655796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l=");
5665796c8dcSSimon Schubert       fprint_frame_id (gdb_stdlog, l);
5675796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, ",r=");
5685796c8dcSSimon Schubert       fprint_frame_id (gdb_stdlog, r);
5695796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner);
5705796c8dcSSimon Schubert     }
5715796c8dcSSimon Schubert   return inner;
5725796c8dcSSimon Schubert }
5735796c8dcSSimon Schubert 
5745796c8dcSSimon Schubert struct frame_info *
frame_find_by_id(struct frame_id id)5755796c8dcSSimon Schubert frame_find_by_id (struct frame_id id)
5765796c8dcSSimon Schubert {
5775796c8dcSSimon Schubert   struct frame_info *frame, *prev_frame;
5785796c8dcSSimon Schubert 
5795796c8dcSSimon Schubert   /* ZERO denotes the null frame, let the caller decide what to do
5805796c8dcSSimon Schubert      about it.  Should it instead return get_current_frame()?  */
5815796c8dcSSimon Schubert   if (!frame_id_p (id))
5825796c8dcSSimon Schubert     return NULL;
5835796c8dcSSimon Schubert 
5845796c8dcSSimon Schubert   /* Try using the frame stash first.  Finding it there removes the need
5855796c8dcSSimon Schubert      to perform the search by looping over all frames, which can be very
5865796c8dcSSimon Schubert      CPU-intensive if the number of frames is very high (the loop is O(n)
5875796c8dcSSimon Schubert      and get_prev_frame performs a series of checks that are relatively
5885796c8dcSSimon Schubert      expensive).  This optimization is particularly useful when this function
5895796c8dcSSimon Schubert      is called from another function (such as value_fetch_lazy, case
5905796c8dcSSimon Schubert      VALUE_LVAL (val) == lval_register) which already loops over all frames,
5915796c8dcSSimon Schubert      making the overall behavior O(n^2).  */
5925796c8dcSSimon Schubert   frame = frame_stash_find (id);
5935796c8dcSSimon Schubert   if (frame)
5945796c8dcSSimon Schubert     return frame;
5955796c8dcSSimon Schubert 
5965796c8dcSSimon Schubert   for (frame = get_current_frame (); ; frame = prev_frame)
5975796c8dcSSimon Schubert     {
5985796c8dcSSimon Schubert       struct frame_id this = get_frame_id (frame);
599cf7f2e2dSJohn Marino 
6005796c8dcSSimon Schubert       if (frame_id_eq (id, this))
6015796c8dcSSimon Schubert 	/* An exact match.  */
6025796c8dcSSimon Schubert 	return frame;
6035796c8dcSSimon Schubert 
6045796c8dcSSimon Schubert       prev_frame = get_prev_frame (frame);
6055796c8dcSSimon Schubert       if (!prev_frame)
6065796c8dcSSimon Schubert 	return NULL;
6075796c8dcSSimon Schubert 
6085796c8dcSSimon Schubert       /* As a safety net to avoid unnecessary backtracing while trying
6095796c8dcSSimon Schubert 	 to find an invalid ID, we check for a common situation where
6105796c8dcSSimon Schubert 	 we can detect from comparing stack addresses that no other
6115796c8dcSSimon Schubert 	 frame in the current frame chain can have this ID.  See the
6125796c8dcSSimon Schubert 	 comment at frame_id_inner for details.   */
6135796c8dcSSimon Schubert       if (get_frame_type (frame) == NORMAL_FRAME
6145796c8dcSSimon Schubert 	  && !frame_id_inner (get_frame_arch (frame), id, this)
6155796c8dcSSimon Schubert 	  && frame_id_inner (get_frame_arch (prev_frame), id,
6165796c8dcSSimon Schubert 			     get_frame_id (prev_frame)))
6175796c8dcSSimon Schubert 	return NULL;
6185796c8dcSSimon Schubert     }
6195796c8dcSSimon Schubert   return NULL;
6205796c8dcSSimon Schubert }
6215796c8dcSSimon Schubert 
622c50c785cSJohn Marino static int
frame_unwind_pc_if_available(struct frame_info * this_frame,CORE_ADDR * pc)623c50c785cSJohn Marino frame_unwind_pc_if_available (struct frame_info *this_frame, CORE_ADDR *pc)
6245796c8dcSSimon Schubert {
6255796c8dcSSimon Schubert   if (!this_frame->prev_pc.p)
6265796c8dcSSimon Schubert     {
6275796c8dcSSimon Schubert       if (gdbarch_unwind_pc_p (frame_unwind_arch (this_frame)))
6285796c8dcSSimon Schubert 	{
629c50c785cSJohn Marino 	  volatile struct gdb_exception ex;
630c50c785cSJohn Marino 	  struct gdbarch *prev_gdbarch;
631c50c785cSJohn Marino 	  CORE_ADDR pc = 0;
632c50c785cSJohn Marino 
6335796c8dcSSimon Schubert 	  /* The right way.  The `pure' way.  The one true way.  This
6345796c8dcSSimon Schubert 	     method depends solely on the register-unwind code to
6355796c8dcSSimon Schubert 	     determine the value of registers in THIS frame, and hence
6365796c8dcSSimon Schubert 	     the value of this frame's PC (resume address).  A typical
6375796c8dcSSimon Schubert 	     implementation is no more than:
6385796c8dcSSimon Schubert 
6395796c8dcSSimon Schubert 	     frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
6405796c8dcSSimon Schubert 	     return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
6415796c8dcSSimon Schubert 
6425796c8dcSSimon Schubert 	     Note: this method is very heavily dependent on a correct
6435796c8dcSSimon Schubert 	     register-unwind implementation, it pays to fix that
6445796c8dcSSimon Schubert 	     method first; this method is frame type agnostic, since
6455796c8dcSSimon Schubert 	     it only deals with register values, it works with any
6465796c8dcSSimon Schubert 	     frame.  This is all in stark contrast to the old
6475796c8dcSSimon Schubert 	     FRAME_SAVED_PC which would try to directly handle all the
6485796c8dcSSimon Schubert 	     different ways that a PC could be unwound.  */
649c50c785cSJohn Marino 	  prev_gdbarch = frame_unwind_arch (this_frame);
650c50c785cSJohn Marino 
651c50c785cSJohn Marino 	  TRY_CATCH (ex, RETURN_MASK_ERROR)
652c50c785cSJohn Marino 	    {
653c50c785cSJohn Marino 	      pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
654c50c785cSJohn Marino 	    }
655c50c785cSJohn Marino 	  if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
656c50c785cSJohn Marino 	    {
657c50c785cSJohn Marino 	      this_frame->prev_pc.p = -1;
658c50c785cSJohn Marino 
659c50c785cSJohn Marino 	      if (frame_debug)
660c50c785cSJohn Marino 		fprintf_unfiltered (gdb_stdlog,
661c50c785cSJohn Marino 				    "{ frame_unwind_pc (this_frame=%d)"
662c50c785cSJohn Marino 				    " -> <unavailable> }\n",
663c50c785cSJohn Marino 				    this_frame->level);
664c50c785cSJohn Marino 	    }
665c50c785cSJohn Marino 	  else if (ex.reason < 0)
666c50c785cSJohn Marino 	    {
667c50c785cSJohn Marino 	      throw_exception (ex);
6685796c8dcSSimon Schubert 	    }
6695796c8dcSSimon Schubert 	  else
670c50c785cSJohn Marino 	    {
6715796c8dcSSimon Schubert 	      this_frame->prev_pc.value = pc;
6725796c8dcSSimon Schubert 	      this_frame->prev_pc.p = 1;
6735796c8dcSSimon Schubert 	      if (frame_debug)
6745796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog,
675c50c785cSJohn Marino 				    "{ frame_unwind_pc (this_frame=%d) "
676c50c785cSJohn Marino 				    "-> %s }\n",
6775796c8dcSSimon Schubert 				    this_frame->level,
6785796c8dcSSimon Schubert 				    hex_string (this_frame->prev_pc.value));
6795796c8dcSSimon Schubert 	    }
680c50c785cSJohn Marino 	}
681c50c785cSJohn Marino       else
682c50c785cSJohn Marino 	internal_error (__FILE__, __LINE__, _("No unwind_pc method"));
683c50c785cSJohn Marino     }
684c50c785cSJohn Marino   if (this_frame->prev_pc.p < 0)
685c50c785cSJohn Marino     {
686c50c785cSJohn Marino       *pc = -1;
687c50c785cSJohn Marino       return 0;
688c50c785cSJohn Marino     }
689c50c785cSJohn Marino   else
690c50c785cSJohn Marino     {
691c50c785cSJohn Marino       *pc = this_frame->prev_pc.value;
692c50c785cSJohn Marino       return 1;
693c50c785cSJohn Marino     }
694c50c785cSJohn Marino }
695c50c785cSJohn Marino 
696c50c785cSJohn Marino static CORE_ADDR
frame_unwind_pc(struct frame_info * this_frame)697c50c785cSJohn Marino frame_unwind_pc (struct frame_info *this_frame)
698c50c785cSJohn Marino {
699c50c785cSJohn Marino   CORE_ADDR pc;
700c50c785cSJohn Marino 
701c50c785cSJohn Marino   if (!frame_unwind_pc_if_available (this_frame, &pc))
702c50c785cSJohn Marino     throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
703c50c785cSJohn Marino   else
704c50c785cSJohn Marino     return pc;
7055796c8dcSSimon Schubert }
7065796c8dcSSimon Schubert 
7075796c8dcSSimon Schubert CORE_ADDR
frame_unwind_caller_pc(struct frame_info * this_frame)7085796c8dcSSimon Schubert frame_unwind_caller_pc (struct frame_info *this_frame)
7095796c8dcSSimon Schubert {
710*ef5ccd6cSJohn Marino   return frame_unwind_pc (skip_artificial_frames (this_frame));
7115796c8dcSSimon Schubert }
7125796c8dcSSimon Schubert 
713c50c785cSJohn Marino int
frame_unwind_caller_pc_if_available(struct frame_info * this_frame,CORE_ADDR * pc)714c50c785cSJohn Marino frame_unwind_caller_pc_if_available (struct frame_info *this_frame,
715c50c785cSJohn Marino 				     CORE_ADDR *pc)
716c50c785cSJohn Marino {
717*ef5ccd6cSJohn Marino   return frame_unwind_pc_if_available (skip_artificial_frames (this_frame), pc);
718c50c785cSJohn Marino }
719c50c785cSJohn Marino 
720c50c785cSJohn Marino int
get_frame_func_if_available(struct frame_info * this_frame,CORE_ADDR * pc)721c50c785cSJohn Marino get_frame_func_if_available (struct frame_info *this_frame, CORE_ADDR *pc)
7225796c8dcSSimon Schubert {
7235796c8dcSSimon Schubert   struct frame_info *next_frame = this_frame->next;
7245796c8dcSSimon Schubert 
7255796c8dcSSimon Schubert   if (!next_frame->prev_func.p)
7265796c8dcSSimon Schubert     {
727c50c785cSJohn Marino       CORE_ADDR addr_in_block;
728c50c785cSJohn Marino 
7295796c8dcSSimon Schubert       /* Make certain that this, and not the adjacent, function is
7305796c8dcSSimon Schubert          found.  */
731c50c785cSJohn Marino       if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
732c50c785cSJohn Marino 	{
733c50c785cSJohn Marino 	  next_frame->prev_func.p = -1;
734c50c785cSJohn Marino 	  if (frame_debug)
735c50c785cSJohn Marino 	    fprintf_unfiltered (gdb_stdlog,
736c50c785cSJohn Marino 				"{ get_frame_func (this_frame=%d)"
737c50c785cSJohn Marino 				" -> unavailable }\n",
738c50c785cSJohn Marino 				this_frame->level);
739c50c785cSJohn Marino 	}
740c50c785cSJohn Marino       else
741c50c785cSJohn Marino 	{
7425796c8dcSSimon Schubert 	  next_frame->prev_func.p = 1;
7435796c8dcSSimon Schubert 	  next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
7445796c8dcSSimon Schubert 	  if (frame_debug)
7455796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog,
7465796c8dcSSimon Schubert 				"{ get_frame_func (this_frame=%d) -> %s }\n",
7475796c8dcSSimon Schubert 				this_frame->level,
7485796c8dcSSimon Schubert 				hex_string (next_frame->prev_func.addr));
7495796c8dcSSimon Schubert 	}
7505796c8dcSSimon Schubert     }
7515796c8dcSSimon Schubert 
752c50c785cSJohn Marino   if (next_frame->prev_func.p < 0)
753c50c785cSJohn Marino     {
754c50c785cSJohn Marino       *pc = -1;
755c50c785cSJohn Marino       return 0;
756c50c785cSJohn Marino     }
757c50c785cSJohn Marino   else
758c50c785cSJohn Marino     {
759c50c785cSJohn Marino       *pc = next_frame->prev_func.addr;
760c50c785cSJohn Marino       return 1;
761c50c785cSJohn Marino     }
762c50c785cSJohn Marino }
763c50c785cSJohn Marino 
764c50c785cSJohn Marino CORE_ADDR
get_frame_func(struct frame_info * this_frame)765c50c785cSJohn Marino get_frame_func (struct frame_info *this_frame)
766c50c785cSJohn Marino {
767c50c785cSJohn Marino   CORE_ADDR pc;
768c50c785cSJohn Marino 
769c50c785cSJohn Marino   if (!get_frame_func_if_available (this_frame, &pc))
770c50c785cSJohn Marino     throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
771c50c785cSJohn Marino 
772c50c785cSJohn Marino   return pc;
773c50c785cSJohn Marino }
774c50c785cSJohn Marino 
775c50c785cSJohn Marino static enum register_status
do_frame_register_read(void * src,int regnum,gdb_byte * buf)7765796c8dcSSimon Schubert do_frame_register_read (void *src, int regnum, gdb_byte *buf)
7775796c8dcSSimon Schubert {
778*ef5ccd6cSJohn Marino   if (!deprecated_frame_register_read (src, regnum, buf))
779c50c785cSJohn Marino     return REG_UNAVAILABLE;
780c50c785cSJohn Marino   else
781c50c785cSJohn Marino     return REG_VALID;
7825796c8dcSSimon Schubert }
7835796c8dcSSimon Schubert 
7845796c8dcSSimon Schubert struct regcache *
frame_save_as_regcache(struct frame_info * this_frame)7855796c8dcSSimon Schubert frame_save_as_regcache (struct frame_info *this_frame)
7865796c8dcSSimon Schubert {
787cf7f2e2dSJohn Marino   struct address_space *aspace = get_frame_address_space (this_frame);
788cf7f2e2dSJohn Marino   struct regcache *regcache = regcache_xmalloc (get_frame_arch (this_frame),
789cf7f2e2dSJohn Marino 						aspace);
7905796c8dcSSimon Schubert   struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
791cf7f2e2dSJohn Marino 
7925796c8dcSSimon Schubert   regcache_save (regcache, do_frame_register_read, this_frame);
7935796c8dcSSimon Schubert   discard_cleanups (cleanups);
7945796c8dcSSimon Schubert   return regcache;
7955796c8dcSSimon Schubert }
7965796c8dcSSimon Schubert 
7975796c8dcSSimon Schubert void
frame_pop(struct frame_info * this_frame)7985796c8dcSSimon Schubert frame_pop (struct frame_info *this_frame)
7995796c8dcSSimon Schubert {
8005796c8dcSSimon Schubert   struct frame_info *prev_frame;
8015796c8dcSSimon Schubert   struct regcache *scratch;
8025796c8dcSSimon Schubert   struct cleanup *cleanups;
8035796c8dcSSimon Schubert 
8045796c8dcSSimon Schubert   if (get_frame_type (this_frame) == DUMMY_FRAME)
8055796c8dcSSimon Schubert     {
8065796c8dcSSimon Schubert       /* Popping a dummy frame involves restoring more than just registers.
8075796c8dcSSimon Schubert 	 dummy_frame_pop does all the work.  */
8085796c8dcSSimon Schubert       dummy_frame_pop (get_frame_id (this_frame));
8095796c8dcSSimon Schubert       return;
8105796c8dcSSimon Schubert     }
8115796c8dcSSimon Schubert 
8125796c8dcSSimon Schubert   /* Ensure that we have a frame to pop to.  */
8135796c8dcSSimon Schubert   prev_frame = get_prev_frame_1 (this_frame);
8145796c8dcSSimon Schubert 
8155796c8dcSSimon Schubert   if (!prev_frame)
8165796c8dcSSimon Schubert     error (_("Cannot pop the initial frame."));
8175796c8dcSSimon Schubert 
818*ef5ccd6cSJohn Marino   /* Ignore TAILCALL_FRAME type frames, they were executed already before
819*ef5ccd6cSJohn Marino      entering THISFRAME.  */
820*ef5ccd6cSJohn Marino   while (get_frame_type (prev_frame) == TAILCALL_FRAME)
821*ef5ccd6cSJohn Marino     prev_frame = get_prev_frame (prev_frame);
822*ef5ccd6cSJohn Marino 
8235796c8dcSSimon Schubert   /* Make a copy of all the register values unwound from this frame.
8245796c8dcSSimon Schubert      Save them in a scratch buffer so that there isn't a race between
8255796c8dcSSimon Schubert      trying to extract the old values from the current regcache while
8265796c8dcSSimon Schubert      at the same time writing new values into that same cache.  */
8275796c8dcSSimon Schubert   scratch = frame_save_as_regcache (prev_frame);
8285796c8dcSSimon Schubert   cleanups = make_cleanup_regcache_xfree (scratch);
8295796c8dcSSimon Schubert 
8305796c8dcSSimon Schubert   /* FIXME: cagney/2003-03-16: It should be possible to tell the
8315796c8dcSSimon Schubert      target's register cache that it is about to be hit with a burst
8325796c8dcSSimon Schubert      register transfer and that the sequence of register writes should
8335796c8dcSSimon Schubert      be batched.  The pair target_prepare_to_store() and
8345796c8dcSSimon Schubert      target_store_registers() kind of suggest this functionality.
8355796c8dcSSimon Schubert      Unfortunately, they don't implement it.  Their lack of a formal
8365796c8dcSSimon Schubert      definition can lead to targets writing back bogus values
8375796c8dcSSimon Schubert      (arguably a bug in the target code mind).  */
8385796c8dcSSimon Schubert   /* Now copy those saved registers into the current regcache.
8395796c8dcSSimon Schubert      Here, regcache_cpy() calls regcache_restore().  */
8405796c8dcSSimon Schubert   regcache_cpy (get_current_regcache (), scratch);
8415796c8dcSSimon Schubert   do_cleanups (cleanups);
8425796c8dcSSimon Schubert 
8435796c8dcSSimon Schubert   /* We've made right mess of GDB's local state, just discard
8445796c8dcSSimon Schubert      everything.  */
8455796c8dcSSimon Schubert   reinit_frame_cache ();
8465796c8dcSSimon Schubert }
8475796c8dcSSimon Schubert 
8485796c8dcSSimon Schubert void
frame_register_unwind(struct frame_info * frame,int regnum,int * optimizedp,int * unavailablep,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump,gdb_byte * bufferp)8495796c8dcSSimon Schubert frame_register_unwind (struct frame_info *frame, int regnum,
850c50c785cSJohn Marino 		       int *optimizedp, int *unavailablep,
851c50c785cSJohn Marino 		       enum lval_type *lvalp, CORE_ADDR *addrp,
852c50c785cSJohn Marino 		       int *realnump, gdb_byte *bufferp)
8535796c8dcSSimon Schubert {
8545796c8dcSSimon Schubert   struct value *value;
8555796c8dcSSimon Schubert 
8565796c8dcSSimon Schubert   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
8575796c8dcSSimon Schubert      that the value proper does not need to be fetched.  */
8585796c8dcSSimon Schubert   gdb_assert (optimizedp != NULL);
8595796c8dcSSimon Schubert   gdb_assert (lvalp != NULL);
8605796c8dcSSimon Schubert   gdb_assert (addrp != NULL);
8615796c8dcSSimon Schubert   gdb_assert (realnump != NULL);
8625796c8dcSSimon Schubert   /* gdb_assert (bufferp != NULL); */
8635796c8dcSSimon Schubert 
8645796c8dcSSimon Schubert   value = frame_unwind_register_value (frame, regnum);
8655796c8dcSSimon Schubert 
8665796c8dcSSimon Schubert   gdb_assert (value != NULL);
8675796c8dcSSimon Schubert 
8685796c8dcSSimon Schubert   *optimizedp = value_optimized_out (value);
869c50c785cSJohn Marino   *unavailablep = !value_entirely_available (value);
8705796c8dcSSimon Schubert   *lvalp = VALUE_LVAL (value);
8715796c8dcSSimon Schubert   *addrp = value_address (value);
8725796c8dcSSimon Schubert   *realnump = VALUE_REGNUM (value);
8735796c8dcSSimon Schubert 
874c50c785cSJohn Marino   if (bufferp)
875c50c785cSJohn Marino     {
876c50c785cSJohn Marino       if (!*optimizedp && !*unavailablep)
8775796c8dcSSimon Schubert 	memcpy (bufferp, value_contents_all (value),
8785796c8dcSSimon Schubert 		TYPE_LENGTH (value_type (value)));
879c50c785cSJohn Marino       else
880c50c785cSJohn Marino 	memset (bufferp, 0, TYPE_LENGTH (value_type (value)));
881c50c785cSJohn Marino     }
8825796c8dcSSimon Schubert 
8835796c8dcSSimon Schubert   /* Dispose of the new value.  This prevents watchpoints from
8845796c8dcSSimon Schubert      trying to watch the saved frame pointer.  */
8855796c8dcSSimon Schubert   release_value (value);
8865796c8dcSSimon Schubert   value_free (value);
8875796c8dcSSimon Schubert }
8885796c8dcSSimon Schubert 
8895796c8dcSSimon Schubert void
frame_register(struct frame_info * frame,int regnum,int * optimizedp,int * unavailablep,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump,gdb_byte * bufferp)8905796c8dcSSimon Schubert frame_register (struct frame_info *frame, int regnum,
891c50c785cSJohn Marino 		int *optimizedp, int *unavailablep, enum lval_type *lvalp,
8925796c8dcSSimon Schubert 		CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
8935796c8dcSSimon Schubert {
8945796c8dcSSimon Schubert   /* Require all but BUFFERP to be valid.  A NULL BUFFERP indicates
8955796c8dcSSimon Schubert      that the value proper does not need to be fetched.  */
8965796c8dcSSimon Schubert   gdb_assert (optimizedp != NULL);
8975796c8dcSSimon Schubert   gdb_assert (lvalp != NULL);
8985796c8dcSSimon Schubert   gdb_assert (addrp != NULL);
8995796c8dcSSimon Schubert   gdb_assert (realnump != NULL);
9005796c8dcSSimon Schubert   /* gdb_assert (bufferp != NULL); */
9015796c8dcSSimon Schubert 
9025796c8dcSSimon Schubert   /* Obtain the register value by unwinding the register from the next
9035796c8dcSSimon Schubert      (more inner frame).  */
9045796c8dcSSimon Schubert   gdb_assert (frame != NULL && frame->next != NULL);
905c50c785cSJohn Marino   frame_register_unwind (frame->next, regnum, optimizedp, unavailablep,
906c50c785cSJohn Marino 			 lvalp, addrp, realnump, bufferp);
9075796c8dcSSimon Schubert }
9085796c8dcSSimon Schubert 
9095796c8dcSSimon Schubert void
frame_unwind_register(struct frame_info * frame,int regnum,gdb_byte * buf)9105796c8dcSSimon Schubert frame_unwind_register (struct frame_info *frame, int regnum, gdb_byte *buf)
9115796c8dcSSimon Schubert {
9125796c8dcSSimon Schubert   int optimized;
913c50c785cSJohn Marino   int unavailable;
9145796c8dcSSimon Schubert   CORE_ADDR addr;
9155796c8dcSSimon Schubert   int realnum;
9165796c8dcSSimon Schubert   enum lval_type lval;
917cf7f2e2dSJohn Marino 
918c50c785cSJohn Marino   frame_register_unwind (frame, regnum, &optimized, &unavailable,
919c50c785cSJohn Marino 			 &lval, &addr, &realnum, buf);
920c50c785cSJohn Marino 
921c50c785cSJohn Marino   if (optimized)
922c50c785cSJohn Marino     error (_("Register %d was optimized out"), regnum);
923c50c785cSJohn Marino   if (unavailable)
924c50c785cSJohn Marino     throw_error (NOT_AVAILABLE_ERROR,
925c50c785cSJohn Marino 		 _("Register %d is not available"), regnum);
9265796c8dcSSimon Schubert }
9275796c8dcSSimon Schubert 
9285796c8dcSSimon Schubert void
get_frame_register(struct frame_info * frame,int regnum,gdb_byte * buf)9295796c8dcSSimon Schubert get_frame_register (struct frame_info *frame,
9305796c8dcSSimon Schubert 		    int regnum, gdb_byte *buf)
9315796c8dcSSimon Schubert {
9325796c8dcSSimon Schubert   frame_unwind_register (frame->next, regnum, buf);
9335796c8dcSSimon Schubert }
9345796c8dcSSimon Schubert 
9355796c8dcSSimon Schubert struct value *
frame_unwind_register_value(struct frame_info * frame,int regnum)9365796c8dcSSimon Schubert frame_unwind_register_value (struct frame_info *frame, int regnum)
9375796c8dcSSimon Schubert {
9385796c8dcSSimon Schubert   struct gdbarch *gdbarch;
9395796c8dcSSimon Schubert   struct value *value;
9405796c8dcSSimon Schubert 
9415796c8dcSSimon Schubert   gdb_assert (frame != NULL);
9425796c8dcSSimon Schubert   gdbarch = frame_unwind_arch (frame);
9435796c8dcSSimon Schubert 
9445796c8dcSSimon Schubert   if (frame_debug)
9455796c8dcSSimon Schubert     {
946c50c785cSJohn Marino       fprintf_unfiltered (gdb_stdlog,
947c50c785cSJohn Marino 			  "{ frame_unwind_register_value "
948c50c785cSJohn Marino 			  "(frame=%d,regnum=%d(%s),...) ",
9495796c8dcSSimon Schubert 			  frame->level, regnum,
9505796c8dcSSimon Schubert 			  user_reg_map_regnum_to_name (gdbarch, regnum));
9515796c8dcSSimon Schubert     }
9525796c8dcSSimon Schubert 
9535796c8dcSSimon Schubert   /* Find the unwinder.  */
9545796c8dcSSimon Schubert   if (frame->unwind == NULL)
955c50c785cSJohn Marino     frame_unwind_find_by_frame (frame, &frame->prologue_cache);
9565796c8dcSSimon Schubert 
9575796c8dcSSimon Schubert   /* Ask this frame to unwind its register.  */
9585796c8dcSSimon Schubert   value = frame->unwind->prev_register (frame, &frame->prologue_cache, regnum);
9595796c8dcSSimon Schubert 
9605796c8dcSSimon Schubert   if (frame_debug)
9615796c8dcSSimon Schubert     {
9625796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, "->");
9635796c8dcSSimon Schubert       if (value_optimized_out (value))
9645796c8dcSSimon Schubert 	fprintf_unfiltered (gdb_stdlog, " optimized out");
9655796c8dcSSimon Schubert       else
9665796c8dcSSimon Schubert 	{
9675796c8dcSSimon Schubert 	  if (VALUE_LVAL (value) == lval_register)
9685796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, " register=%d",
9695796c8dcSSimon Schubert 				VALUE_REGNUM (value));
9705796c8dcSSimon Schubert 	  else if (VALUE_LVAL (value) == lval_memory)
9715796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, " address=%s",
9725796c8dcSSimon Schubert 				paddress (gdbarch,
9735796c8dcSSimon Schubert 					  value_address (value)));
9745796c8dcSSimon Schubert 	  else
9755796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, " computed");
9765796c8dcSSimon Schubert 
9775796c8dcSSimon Schubert 	  if (value_lazy (value))
9785796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog, " lazy");
9795796c8dcSSimon Schubert 	  else
9805796c8dcSSimon Schubert 	    {
9815796c8dcSSimon Schubert 	      int i;
9825796c8dcSSimon Schubert 	      const gdb_byte *buf = value_contents (value);
9835796c8dcSSimon Schubert 
9845796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, " bytes=");
9855796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "[");
9865796c8dcSSimon Schubert 	      for (i = 0; i < register_size (gdbarch, regnum); i++)
9875796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
9885796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "]");
9895796c8dcSSimon Schubert 	    }
9905796c8dcSSimon Schubert 	}
9915796c8dcSSimon Schubert 
9925796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, " }\n");
9935796c8dcSSimon Schubert     }
9945796c8dcSSimon Schubert 
9955796c8dcSSimon Schubert   return value;
9965796c8dcSSimon Schubert }
9975796c8dcSSimon Schubert 
9985796c8dcSSimon Schubert struct value *
get_frame_register_value(struct frame_info * frame,int regnum)9995796c8dcSSimon Schubert get_frame_register_value (struct frame_info *frame, int regnum)
10005796c8dcSSimon Schubert {
10015796c8dcSSimon Schubert   return frame_unwind_register_value (frame->next, regnum);
10025796c8dcSSimon Schubert }
10035796c8dcSSimon Schubert 
10045796c8dcSSimon Schubert LONGEST
frame_unwind_register_signed(struct frame_info * frame,int regnum)10055796c8dcSSimon Schubert frame_unwind_register_signed (struct frame_info *frame, int regnum)
10065796c8dcSSimon Schubert {
10075796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
10085796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
10095796c8dcSSimon Schubert   int size = register_size (gdbarch, regnum);
10105796c8dcSSimon Schubert   gdb_byte buf[MAX_REGISTER_SIZE];
1011cf7f2e2dSJohn Marino 
10125796c8dcSSimon Schubert   frame_unwind_register (frame, regnum, buf);
10135796c8dcSSimon Schubert   return extract_signed_integer (buf, size, byte_order);
10145796c8dcSSimon Schubert }
10155796c8dcSSimon Schubert 
10165796c8dcSSimon Schubert LONGEST
get_frame_register_signed(struct frame_info * frame,int regnum)10175796c8dcSSimon Schubert get_frame_register_signed (struct frame_info *frame, int regnum)
10185796c8dcSSimon Schubert {
10195796c8dcSSimon Schubert   return frame_unwind_register_signed (frame->next, regnum);
10205796c8dcSSimon Schubert }
10215796c8dcSSimon Schubert 
10225796c8dcSSimon Schubert ULONGEST
frame_unwind_register_unsigned(struct frame_info * frame,int regnum)10235796c8dcSSimon Schubert frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
10245796c8dcSSimon Schubert {
10255796c8dcSSimon Schubert   struct gdbarch *gdbarch = frame_unwind_arch (frame);
10265796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
10275796c8dcSSimon Schubert   int size = register_size (gdbarch, regnum);
10285796c8dcSSimon Schubert   gdb_byte buf[MAX_REGISTER_SIZE];
1029cf7f2e2dSJohn Marino 
10305796c8dcSSimon Schubert   frame_unwind_register (frame, regnum, buf);
10315796c8dcSSimon Schubert   return extract_unsigned_integer (buf, size, byte_order);
10325796c8dcSSimon Schubert }
10335796c8dcSSimon Schubert 
10345796c8dcSSimon Schubert ULONGEST
get_frame_register_unsigned(struct frame_info * frame,int regnum)10355796c8dcSSimon Schubert get_frame_register_unsigned (struct frame_info *frame, int regnum)
10365796c8dcSSimon Schubert {
10375796c8dcSSimon Schubert   return frame_unwind_register_unsigned (frame->next, regnum);
10385796c8dcSSimon Schubert }
10395796c8dcSSimon Schubert 
1040*ef5ccd6cSJohn Marino int
read_frame_register_unsigned(struct frame_info * frame,int regnum,ULONGEST * val)1041*ef5ccd6cSJohn Marino read_frame_register_unsigned (struct frame_info *frame, int regnum,
1042*ef5ccd6cSJohn Marino 			      ULONGEST *val)
1043*ef5ccd6cSJohn Marino {
1044*ef5ccd6cSJohn Marino   struct value *regval = get_frame_register_value (frame, regnum);
1045*ef5ccd6cSJohn Marino 
1046*ef5ccd6cSJohn Marino   if (!value_optimized_out (regval)
1047*ef5ccd6cSJohn Marino       && value_entirely_available (regval))
1048*ef5ccd6cSJohn Marino     {
1049*ef5ccd6cSJohn Marino       struct gdbarch *gdbarch = get_frame_arch (frame);
1050*ef5ccd6cSJohn Marino       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1051*ef5ccd6cSJohn Marino       int size = register_size (gdbarch, VALUE_REGNUM (regval));
1052*ef5ccd6cSJohn Marino 
1053*ef5ccd6cSJohn Marino       *val = extract_unsigned_integer (value_contents (regval), size, byte_order);
1054*ef5ccd6cSJohn Marino       return 1;
1055*ef5ccd6cSJohn Marino     }
1056*ef5ccd6cSJohn Marino 
1057*ef5ccd6cSJohn Marino   return 0;
1058*ef5ccd6cSJohn Marino }
1059*ef5ccd6cSJohn Marino 
10605796c8dcSSimon Schubert void
put_frame_register(struct frame_info * frame,int regnum,const gdb_byte * buf)10615796c8dcSSimon Schubert put_frame_register (struct frame_info *frame, int regnum,
10625796c8dcSSimon Schubert 		    const gdb_byte *buf)
10635796c8dcSSimon Schubert {
10645796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
10655796c8dcSSimon Schubert   int realnum;
10665796c8dcSSimon Schubert   int optim;
1067c50c785cSJohn Marino   int unavail;
10685796c8dcSSimon Schubert   enum lval_type lval;
10695796c8dcSSimon Schubert   CORE_ADDR addr;
1070cf7f2e2dSJohn Marino 
1071c50c785cSJohn Marino   frame_register (frame, regnum, &optim, &unavail,
1072c50c785cSJohn Marino 		  &lval, &addr, &realnum, NULL);
10735796c8dcSSimon Schubert   if (optim)
10745796c8dcSSimon Schubert     error (_("Attempt to assign to a value that was optimized out."));
10755796c8dcSSimon Schubert   switch (lval)
10765796c8dcSSimon Schubert     {
10775796c8dcSSimon Schubert     case lval_memory:
10785796c8dcSSimon Schubert       {
1079*ef5ccd6cSJohn Marino 	write_memory (addr, buf, register_size (gdbarch, regnum));
10805796c8dcSSimon Schubert 	break;
10815796c8dcSSimon Schubert       }
10825796c8dcSSimon Schubert     case lval_register:
10835796c8dcSSimon Schubert       regcache_cooked_write (get_current_regcache (), realnum, buf);
10845796c8dcSSimon Schubert       break;
10855796c8dcSSimon Schubert     default:
10865796c8dcSSimon Schubert       error (_("Attempt to assign to an unmodifiable value."));
10875796c8dcSSimon Schubert     }
10885796c8dcSSimon Schubert }
10895796c8dcSSimon Schubert 
1090*ef5ccd6cSJohn Marino /* This function is deprecated.  Use get_frame_register_value instead,
1091*ef5ccd6cSJohn Marino    which provides more accurate information.
10925796c8dcSSimon Schubert 
10935796c8dcSSimon Schubert    Find and return the value of REGNUM for the specified stack frame.
10945796c8dcSSimon Schubert    The number of bytes copied is REGISTER_SIZE (REGNUM).
10955796c8dcSSimon Schubert 
10965796c8dcSSimon Schubert    Returns 0 if the register value could not be found.  */
10975796c8dcSSimon Schubert 
10985796c8dcSSimon Schubert int
deprecated_frame_register_read(struct frame_info * frame,int regnum,gdb_byte * myaddr)1099*ef5ccd6cSJohn Marino deprecated_frame_register_read (struct frame_info *frame, int regnum,
11005796c8dcSSimon Schubert 		     gdb_byte *myaddr)
11015796c8dcSSimon Schubert {
11025796c8dcSSimon Schubert   int optimized;
1103c50c785cSJohn Marino   int unavailable;
11045796c8dcSSimon Schubert   enum lval_type lval;
11055796c8dcSSimon Schubert   CORE_ADDR addr;
11065796c8dcSSimon Schubert   int realnum;
1107cf7f2e2dSJohn Marino 
1108c50c785cSJohn Marino   frame_register (frame, regnum, &optimized, &unavailable,
1109c50c785cSJohn Marino 		  &lval, &addr, &realnum, myaddr);
11105796c8dcSSimon Schubert 
1111c50c785cSJohn Marino   return !optimized && !unavailable;
11125796c8dcSSimon Schubert }
11135796c8dcSSimon Schubert 
11145796c8dcSSimon Schubert int
get_frame_register_bytes(struct frame_info * frame,int regnum,CORE_ADDR offset,int len,gdb_byte * myaddr,int * optimizedp,int * unavailablep)11155796c8dcSSimon Schubert get_frame_register_bytes (struct frame_info *frame, int regnum,
1116c50c785cSJohn Marino 			  CORE_ADDR offset, int len, gdb_byte *myaddr,
1117c50c785cSJohn Marino 			  int *optimizedp, int *unavailablep)
11185796c8dcSSimon Schubert {
11195796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
11205796c8dcSSimon Schubert   int i;
11215796c8dcSSimon Schubert   int maxsize;
11225796c8dcSSimon Schubert   int numregs;
11235796c8dcSSimon Schubert 
11245796c8dcSSimon Schubert   /* Skip registers wholly inside of OFFSET.  */
11255796c8dcSSimon Schubert   while (offset >= register_size (gdbarch, regnum))
11265796c8dcSSimon Schubert     {
11275796c8dcSSimon Schubert       offset -= register_size (gdbarch, regnum);
11285796c8dcSSimon Schubert       regnum++;
11295796c8dcSSimon Schubert     }
11305796c8dcSSimon Schubert 
11315796c8dcSSimon Schubert   /* Ensure that we will not read beyond the end of the register file.
11325796c8dcSSimon Schubert      This can only ever happen if the debug information is bad.  */
11335796c8dcSSimon Schubert   maxsize = -offset;
11345796c8dcSSimon Schubert   numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
11355796c8dcSSimon Schubert   for (i = regnum; i < numregs; i++)
11365796c8dcSSimon Schubert     {
11375796c8dcSSimon Schubert       int thissize = register_size (gdbarch, i);
1138cf7f2e2dSJohn Marino 
11395796c8dcSSimon Schubert       if (thissize == 0)
11405796c8dcSSimon Schubert 	break;	/* This register is not available on this architecture.  */
11415796c8dcSSimon Schubert       maxsize += thissize;
11425796c8dcSSimon Schubert     }
11435796c8dcSSimon Schubert   if (len > maxsize)
1144c50c785cSJohn Marino     error (_("Bad debug information detected: "
11455796c8dcSSimon Schubert 	     "Attempt to read %d bytes from registers."), len);
11465796c8dcSSimon Schubert 
11475796c8dcSSimon Schubert   /* Copy the data.  */
11485796c8dcSSimon Schubert   while (len > 0)
11495796c8dcSSimon Schubert     {
11505796c8dcSSimon Schubert       int curr_len = register_size (gdbarch, regnum) - offset;
1151cf7f2e2dSJohn Marino 
11525796c8dcSSimon Schubert       if (curr_len > len)
11535796c8dcSSimon Schubert 	curr_len = len;
11545796c8dcSSimon Schubert 
11555796c8dcSSimon Schubert       if (curr_len == register_size (gdbarch, regnum))
11565796c8dcSSimon Schubert 	{
1157c50c785cSJohn Marino 	  enum lval_type lval;
1158c50c785cSJohn Marino 	  CORE_ADDR addr;
1159c50c785cSJohn Marino 	  int realnum;
1160c50c785cSJohn Marino 
1161c50c785cSJohn Marino 	  frame_register (frame, regnum, optimizedp, unavailablep,
1162c50c785cSJohn Marino 			  &lval, &addr, &realnum, myaddr);
1163c50c785cSJohn Marino 	  if (*optimizedp || *unavailablep)
11645796c8dcSSimon Schubert 	    return 0;
11655796c8dcSSimon Schubert 	}
11665796c8dcSSimon Schubert       else
11675796c8dcSSimon Schubert 	{
11685796c8dcSSimon Schubert 	  gdb_byte buf[MAX_REGISTER_SIZE];
1169c50c785cSJohn Marino 	  enum lval_type lval;
1170c50c785cSJohn Marino 	  CORE_ADDR addr;
1171c50c785cSJohn Marino 	  int realnum;
1172cf7f2e2dSJohn Marino 
1173c50c785cSJohn Marino 	  frame_register (frame, regnum, optimizedp, unavailablep,
1174c50c785cSJohn Marino 			  &lval, &addr, &realnum, buf);
1175c50c785cSJohn Marino 	  if (*optimizedp || *unavailablep)
11765796c8dcSSimon Schubert 	    return 0;
11775796c8dcSSimon Schubert 	  memcpy (myaddr, buf + offset, curr_len);
11785796c8dcSSimon Schubert 	}
11795796c8dcSSimon Schubert 
11805796c8dcSSimon Schubert       myaddr += curr_len;
11815796c8dcSSimon Schubert       len -= curr_len;
11825796c8dcSSimon Schubert       offset = 0;
11835796c8dcSSimon Schubert       regnum++;
11845796c8dcSSimon Schubert     }
11855796c8dcSSimon Schubert 
1186c50c785cSJohn Marino   *optimizedp = 0;
1187c50c785cSJohn Marino   *unavailablep = 0;
11885796c8dcSSimon Schubert   return 1;
11895796c8dcSSimon Schubert }
11905796c8dcSSimon Schubert 
11915796c8dcSSimon Schubert void
put_frame_register_bytes(struct frame_info * frame,int regnum,CORE_ADDR offset,int len,const gdb_byte * myaddr)11925796c8dcSSimon Schubert put_frame_register_bytes (struct frame_info *frame, int regnum,
11935796c8dcSSimon Schubert 			  CORE_ADDR offset, int len, const gdb_byte *myaddr)
11945796c8dcSSimon Schubert {
11955796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
11965796c8dcSSimon Schubert 
11975796c8dcSSimon Schubert   /* Skip registers wholly inside of OFFSET.  */
11985796c8dcSSimon Schubert   while (offset >= register_size (gdbarch, regnum))
11995796c8dcSSimon Schubert     {
12005796c8dcSSimon Schubert       offset -= register_size (gdbarch, regnum);
12015796c8dcSSimon Schubert       regnum++;
12025796c8dcSSimon Schubert     }
12035796c8dcSSimon Schubert 
12045796c8dcSSimon Schubert   /* Copy the data.  */
12055796c8dcSSimon Schubert   while (len > 0)
12065796c8dcSSimon Schubert     {
12075796c8dcSSimon Schubert       int curr_len = register_size (gdbarch, regnum) - offset;
1208cf7f2e2dSJohn Marino 
12095796c8dcSSimon Schubert       if (curr_len > len)
12105796c8dcSSimon Schubert 	curr_len = len;
12115796c8dcSSimon Schubert 
12125796c8dcSSimon Schubert       if (curr_len == register_size (gdbarch, regnum))
12135796c8dcSSimon Schubert 	{
12145796c8dcSSimon Schubert 	  put_frame_register (frame, regnum, myaddr);
12155796c8dcSSimon Schubert 	}
12165796c8dcSSimon Schubert       else
12175796c8dcSSimon Schubert 	{
12185796c8dcSSimon Schubert 	  gdb_byte buf[MAX_REGISTER_SIZE];
1219cf7f2e2dSJohn Marino 
1220*ef5ccd6cSJohn Marino 	  deprecated_frame_register_read (frame, regnum, buf);
12215796c8dcSSimon Schubert 	  memcpy (buf + offset, myaddr, curr_len);
12225796c8dcSSimon Schubert 	  put_frame_register (frame, regnum, buf);
12235796c8dcSSimon Schubert 	}
12245796c8dcSSimon Schubert 
12255796c8dcSSimon Schubert       myaddr += curr_len;
12265796c8dcSSimon Schubert       len -= curr_len;
12275796c8dcSSimon Schubert       offset = 0;
12285796c8dcSSimon Schubert       regnum++;
12295796c8dcSSimon Schubert     }
12305796c8dcSSimon Schubert }
12315796c8dcSSimon Schubert 
12325796c8dcSSimon Schubert /* Create a sentinel frame.  */
12335796c8dcSSimon Schubert 
12345796c8dcSSimon Schubert static struct frame_info *
create_sentinel_frame(struct program_space * pspace,struct regcache * regcache)1235cf7f2e2dSJohn Marino create_sentinel_frame (struct program_space *pspace, struct regcache *regcache)
12365796c8dcSSimon Schubert {
12375796c8dcSSimon Schubert   struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
1238cf7f2e2dSJohn Marino 
12395796c8dcSSimon Schubert   frame->level = -1;
1240cf7f2e2dSJohn Marino   frame->pspace = pspace;
1241cf7f2e2dSJohn Marino   frame->aspace = get_regcache_aspace (regcache);
12425796c8dcSSimon Schubert   /* Explicitly initialize the sentinel frame's cache.  Provide it
12435796c8dcSSimon Schubert      with the underlying regcache.  In the future additional
12445796c8dcSSimon Schubert      information, such as the frame's thread will be added.  */
12455796c8dcSSimon Schubert   frame->prologue_cache = sentinel_frame_cache (regcache);
12465796c8dcSSimon Schubert   /* For the moment there is only one sentinel frame implementation.  */
1247c50c785cSJohn Marino   frame->unwind = &sentinel_frame_unwind;
12485796c8dcSSimon Schubert   /* Link this frame back to itself.  The frame is self referential
12495796c8dcSSimon Schubert      (the unwound PC is the same as the pc), so make it so.  */
12505796c8dcSSimon Schubert   frame->next = frame;
12515796c8dcSSimon Schubert   /* Make the sentinel frame's ID valid, but invalid.  That way all
12525796c8dcSSimon Schubert      comparisons with it should fail.  */
12535796c8dcSSimon Schubert   frame->this_id.p = 1;
12545796c8dcSSimon Schubert   frame->this_id.value = null_frame_id;
12555796c8dcSSimon Schubert   if (frame_debug)
12565796c8dcSSimon Schubert     {
12575796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> ");
12585796c8dcSSimon Schubert       fprint_frame (gdb_stdlog, frame);
12595796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, " }\n");
12605796c8dcSSimon Schubert     }
12615796c8dcSSimon Schubert   return frame;
12625796c8dcSSimon Schubert }
12635796c8dcSSimon Schubert 
1264c50c785cSJohn Marino /* Info about the innermost stack frame (contents of FP register).  */
12655796c8dcSSimon Schubert 
12665796c8dcSSimon Schubert static struct frame_info *current_frame;
12675796c8dcSSimon Schubert 
12685796c8dcSSimon Schubert /* Cache for frame addresses already read by gdb.  Valid only while
12695796c8dcSSimon Schubert    inferior is stopped.  Control variables for the frame cache should
12705796c8dcSSimon Schubert    be local to this module.  */
12715796c8dcSSimon Schubert 
12725796c8dcSSimon Schubert static struct obstack frame_cache_obstack;
12735796c8dcSSimon Schubert 
12745796c8dcSSimon Schubert void *
frame_obstack_zalloc(unsigned long size)12755796c8dcSSimon Schubert frame_obstack_zalloc (unsigned long size)
12765796c8dcSSimon Schubert {
12775796c8dcSSimon Schubert   void *data = obstack_alloc (&frame_cache_obstack, size);
1278cf7f2e2dSJohn Marino 
12795796c8dcSSimon Schubert   memset (data, 0, size);
12805796c8dcSSimon Schubert   return data;
12815796c8dcSSimon Schubert }
12825796c8dcSSimon Schubert 
12835796c8dcSSimon Schubert /* Return the innermost (currently executing) stack frame.  This is
12845796c8dcSSimon Schubert    split into two functions.  The function unwind_to_current_frame()
12855796c8dcSSimon Schubert    is wrapped in catch exceptions so that, even when the unwind of the
12865796c8dcSSimon Schubert    sentinel frame fails, the function still returns a stack frame.  */
12875796c8dcSSimon Schubert 
12885796c8dcSSimon Schubert static int
unwind_to_current_frame(struct ui_out * ui_out,void * args)12895796c8dcSSimon Schubert unwind_to_current_frame (struct ui_out *ui_out, void *args)
12905796c8dcSSimon Schubert {
12915796c8dcSSimon Schubert   struct frame_info *frame = get_prev_frame (args);
1292cf7f2e2dSJohn Marino 
12935796c8dcSSimon Schubert   /* A sentinel frame can fail to unwind, e.g., because its PC value
12945796c8dcSSimon Schubert      lands in somewhere like start.  */
12955796c8dcSSimon Schubert   if (frame == NULL)
12965796c8dcSSimon Schubert     return 1;
12975796c8dcSSimon Schubert   current_frame = frame;
12985796c8dcSSimon Schubert   return 0;
12995796c8dcSSimon Schubert }
13005796c8dcSSimon Schubert 
13015796c8dcSSimon Schubert struct frame_info *
get_current_frame(void)13025796c8dcSSimon Schubert get_current_frame (void)
13035796c8dcSSimon Schubert {
13045796c8dcSSimon Schubert   /* First check, and report, the lack of registers.  Having GDB
13055796c8dcSSimon Schubert      report "No stack!" or "No memory" when the target doesn't even
13065796c8dcSSimon Schubert      have registers is very confusing.  Besides, "printcmd.exp"
13075796c8dcSSimon Schubert      explicitly checks that ``print $pc'' with no registers prints "No
13085796c8dcSSimon Schubert      registers".  */
13095796c8dcSSimon Schubert   if (!target_has_registers)
13105796c8dcSSimon Schubert     error (_("No registers."));
13115796c8dcSSimon Schubert   if (!target_has_stack)
13125796c8dcSSimon Schubert     error (_("No stack."));
13135796c8dcSSimon Schubert   if (!target_has_memory)
13145796c8dcSSimon Schubert     error (_("No memory."));
1315cf7f2e2dSJohn Marino   /* Traceframes are effectively a substitute for the live inferior.  */
1316cf7f2e2dSJohn Marino   if (get_traceframe_number () < 0)
1317cf7f2e2dSJohn Marino     {
13185796c8dcSSimon Schubert       if (ptid_equal (inferior_ptid, null_ptid))
13195796c8dcSSimon Schubert 	error (_("No selected thread."));
13205796c8dcSSimon Schubert       if (is_exited (inferior_ptid))
13215796c8dcSSimon Schubert 	error (_("Invalid selected thread."));
13225796c8dcSSimon Schubert       if (is_executing (inferior_ptid))
13235796c8dcSSimon Schubert 	error (_("Target is executing."));
1324cf7f2e2dSJohn Marino     }
13255796c8dcSSimon Schubert 
13265796c8dcSSimon Schubert   if (current_frame == NULL)
13275796c8dcSSimon Schubert     {
13285796c8dcSSimon Schubert       struct frame_info *sentinel_frame =
1329cf7f2e2dSJohn Marino 	create_sentinel_frame (current_program_space, get_current_regcache ());
1330a45ae5f8SJohn Marino       if (catch_exceptions (current_uiout, unwind_to_current_frame,
1331a45ae5f8SJohn Marino 			    sentinel_frame, RETURN_MASK_ERROR) != 0)
13325796c8dcSSimon Schubert 	{
13335796c8dcSSimon Schubert 	  /* Oops! Fake a current frame?  Is this useful?  It has a PC
13345796c8dcSSimon Schubert              of zero, for instance.  */
13355796c8dcSSimon Schubert 	  current_frame = sentinel_frame;
13365796c8dcSSimon Schubert 	}
13375796c8dcSSimon Schubert     }
13385796c8dcSSimon Schubert   return current_frame;
13395796c8dcSSimon Schubert }
13405796c8dcSSimon Schubert 
13415796c8dcSSimon Schubert /* The "selected" stack frame is used by default for local and arg
13425796c8dcSSimon Schubert    access.  May be zero, for no selected frame.  */
13435796c8dcSSimon Schubert 
13445796c8dcSSimon Schubert static struct frame_info *selected_frame;
13455796c8dcSSimon Schubert 
13465796c8dcSSimon Schubert int
has_stack_frames(void)13475796c8dcSSimon Schubert has_stack_frames (void)
13485796c8dcSSimon Schubert {
13495796c8dcSSimon Schubert   if (!target_has_registers || !target_has_stack || !target_has_memory)
13505796c8dcSSimon Schubert     return 0;
13515796c8dcSSimon Schubert 
1352a45ae5f8SJohn Marino   /* Traceframes are effectively a substitute for the live inferior.  */
1353a45ae5f8SJohn Marino   if (get_traceframe_number () < 0)
1354a45ae5f8SJohn Marino     {
13555796c8dcSSimon Schubert       /* No current inferior, no frame.  */
13565796c8dcSSimon Schubert       if (ptid_equal (inferior_ptid, null_ptid))
13575796c8dcSSimon Schubert 	return 0;
13585796c8dcSSimon Schubert 
13595796c8dcSSimon Schubert       /* Don't try to read from a dead thread.  */
13605796c8dcSSimon Schubert       if (is_exited (inferior_ptid))
13615796c8dcSSimon Schubert 	return 0;
13625796c8dcSSimon Schubert 
13635796c8dcSSimon Schubert       /* ... or from a spinning thread.  */
13645796c8dcSSimon Schubert       if (is_executing (inferior_ptid))
13655796c8dcSSimon Schubert 	return 0;
1366a45ae5f8SJohn Marino     }
13675796c8dcSSimon Schubert 
13685796c8dcSSimon Schubert   return 1;
13695796c8dcSSimon Schubert }
13705796c8dcSSimon Schubert 
13715796c8dcSSimon Schubert /* Return the selected frame.  Always non-NULL (unless there isn't an
13725796c8dcSSimon Schubert    inferior sufficient for creating a frame) in which case an error is
13735796c8dcSSimon Schubert    thrown.  */
13745796c8dcSSimon Schubert 
13755796c8dcSSimon Schubert struct frame_info *
get_selected_frame(const char * message)13765796c8dcSSimon Schubert get_selected_frame (const char *message)
13775796c8dcSSimon Schubert {
13785796c8dcSSimon Schubert   if (selected_frame == NULL)
13795796c8dcSSimon Schubert     {
13805796c8dcSSimon Schubert       if (message != NULL && !has_stack_frames ())
13815796c8dcSSimon Schubert 	error (("%s"), message);
13825796c8dcSSimon Schubert       /* Hey!  Don't trust this.  It should really be re-finding the
13835796c8dcSSimon Schubert 	 last selected frame of the currently selected thread.  This,
13845796c8dcSSimon Schubert 	 though, is better than nothing.  */
13855796c8dcSSimon Schubert       select_frame (get_current_frame ());
13865796c8dcSSimon Schubert     }
13875796c8dcSSimon Schubert   /* There is always a frame.  */
13885796c8dcSSimon Schubert   gdb_assert (selected_frame != NULL);
13895796c8dcSSimon Schubert   return selected_frame;
13905796c8dcSSimon Schubert }
13915796c8dcSSimon Schubert 
1392c50c785cSJohn Marino /* If there is a selected frame, return it.  Otherwise, return NULL.  */
1393c50c785cSJohn Marino 
1394c50c785cSJohn Marino struct frame_info *
get_selected_frame_if_set(void)1395c50c785cSJohn Marino get_selected_frame_if_set (void)
1396c50c785cSJohn Marino {
1397c50c785cSJohn Marino   return selected_frame;
1398c50c785cSJohn Marino }
1399c50c785cSJohn Marino 
14005796c8dcSSimon Schubert /* This is a variant of get_selected_frame() which can be called when
14015796c8dcSSimon Schubert    the inferior does not have a frame; in that case it will return
14025796c8dcSSimon Schubert    NULL instead of calling error().  */
14035796c8dcSSimon Schubert 
14045796c8dcSSimon Schubert struct frame_info *
deprecated_safe_get_selected_frame(void)14055796c8dcSSimon Schubert deprecated_safe_get_selected_frame (void)
14065796c8dcSSimon Schubert {
14075796c8dcSSimon Schubert   if (!has_stack_frames ())
14085796c8dcSSimon Schubert     return NULL;
14095796c8dcSSimon Schubert   return get_selected_frame (NULL);
14105796c8dcSSimon Schubert }
14115796c8dcSSimon Schubert 
14125796c8dcSSimon Schubert /* Select frame FI (or NULL - to invalidate the current frame).  */
14135796c8dcSSimon Schubert 
14145796c8dcSSimon Schubert void
select_frame(struct frame_info * fi)14155796c8dcSSimon Schubert select_frame (struct frame_info *fi)
14165796c8dcSSimon Schubert {
14175796c8dcSSimon Schubert   selected_frame = fi;
14185796c8dcSSimon Schubert   /* NOTE: cagney/2002-05-04: FI can be NULL.  This occurs when the
14195796c8dcSSimon Schubert      frame is being invalidated.  */
14205796c8dcSSimon Schubert   if (deprecated_selected_frame_level_changed_hook)
14215796c8dcSSimon Schubert     deprecated_selected_frame_level_changed_hook (frame_relative_level (fi));
14225796c8dcSSimon Schubert 
14235796c8dcSSimon Schubert   /* FIXME: kseitz/2002-08-28: It would be nice to call
14245796c8dcSSimon Schubert      selected_frame_level_changed_event() right here, but due to limitations
14255796c8dcSSimon Schubert      in the current interfaces, we would end up flooding UIs with events
14265796c8dcSSimon Schubert      because select_frame() is used extensively internally.
14275796c8dcSSimon Schubert 
14285796c8dcSSimon Schubert      Once we have frame-parameterized frame (and frame-related) commands,
14295796c8dcSSimon Schubert      the event notification can be moved here, since this function will only
14305796c8dcSSimon Schubert      be called when the user's selected frame is being changed.  */
14315796c8dcSSimon Schubert 
14325796c8dcSSimon Schubert   /* Ensure that symbols for this frame are read in.  Also, determine the
14335796c8dcSSimon Schubert      source language of this frame, and switch to it if desired.  */
14345796c8dcSSimon Schubert   if (fi)
14355796c8dcSSimon Schubert     {
1436c50c785cSJohn Marino       CORE_ADDR pc;
1437c50c785cSJohn Marino 
1438c50c785cSJohn Marino       /* We retrieve the frame's symtab by using the frame PC.
1439c50c785cSJohn Marino 	 However we cannot use the frame PC as-is, because it usually
1440c50c785cSJohn Marino 	 points to the instruction following the "call", which is
1441c50c785cSJohn Marino 	 sometimes the first instruction of another function.  So we
1442c50c785cSJohn Marino 	 rely on get_frame_address_in_block() which provides us with a
1443c50c785cSJohn Marino 	 PC which is guaranteed to be inside the frame's code
1444c50c785cSJohn Marino 	 block.  */
1445c50c785cSJohn Marino       if (get_frame_address_in_block_if_available (fi, &pc))
1446c50c785cSJohn Marino 	{
1447c50c785cSJohn Marino 	  struct symtab *s = find_pc_symtab (pc);
1448c50c785cSJohn Marino 
14495796c8dcSSimon Schubert 	  if (s
14505796c8dcSSimon Schubert 	      && s->language != current_language->la_language
14515796c8dcSSimon Schubert 	      && s->language != language_unknown
14525796c8dcSSimon Schubert 	      && language_mode == language_mode_auto)
14535796c8dcSSimon Schubert 	    set_language (s->language);
14545796c8dcSSimon Schubert 	}
14555796c8dcSSimon Schubert     }
14565796c8dcSSimon Schubert }
14575796c8dcSSimon Schubert 
14585796c8dcSSimon Schubert /* Create an arbitrary (i.e. address specified by user) or innermost frame.
14595796c8dcSSimon Schubert    Always returns a non-NULL value.  */
14605796c8dcSSimon Schubert 
14615796c8dcSSimon Schubert struct frame_info *
create_new_frame(CORE_ADDR addr,CORE_ADDR pc)14625796c8dcSSimon Schubert create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
14635796c8dcSSimon Schubert {
14645796c8dcSSimon Schubert   struct frame_info *fi;
14655796c8dcSSimon Schubert 
14665796c8dcSSimon Schubert   if (frame_debug)
14675796c8dcSSimon Schubert     {
14685796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog,
14695796c8dcSSimon Schubert 			  "{ create_new_frame (addr=%s, pc=%s) ",
14705796c8dcSSimon Schubert 			  hex_string (addr), hex_string (pc));
14715796c8dcSSimon Schubert     }
14725796c8dcSSimon Schubert 
14735796c8dcSSimon Schubert   fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
14745796c8dcSSimon Schubert 
1475c50c785cSJohn Marino   fi->next = create_sentinel_frame (current_program_space,
1476c50c785cSJohn Marino 				    get_current_regcache ());
14775796c8dcSSimon Schubert 
14785796c8dcSSimon Schubert   /* Set/update this frame's cached PC value, found in the next frame.
14795796c8dcSSimon Schubert      Do this before looking for this frame's unwinder.  A sniffer is
14805796c8dcSSimon Schubert      very likely to read this, and the corresponding unwinder is
14815796c8dcSSimon Schubert      entitled to rely that the PC doesn't magically change.  */
14825796c8dcSSimon Schubert   fi->next->prev_pc.value = pc;
14835796c8dcSSimon Schubert   fi->next->prev_pc.p = 1;
14845796c8dcSSimon Schubert 
1485cf7f2e2dSJohn Marino   /* We currently assume that frame chain's can't cross spaces.  */
1486cf7f2e2dSJohn Marino   fi->pspace = fi->next->pspace;
1487cf7f2e2dSJohn Marino   fi->aspace = fi->next->aspace;
1488cf7f2e2dSJohn Marino 
14895796c8dcSSimon Schubert   /* Select/initialize both the unwind function and the frame's type
14905796c8dcSSimon Schubert      based on the PC.  */
1491c50c785cSJohn Marino   frame_unwind_find_by_frame (fi, &fi->prologue_cache);
14925796c8dcSSimon Schubert 
14935796c8dcSSimon Schubert   fi->this_id.p = 1;
14945796c8dcSSimon Schubert   fi->this_id.value = frame_id_build (addr, pc);
14955796c8dcSSimon Schubert 
14965796c8dcSSimon Schubert   if (frame_debug)
14975796c8dcSSimon Schubert     {
14985796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, "-> ");
14995796c8dcSSimon Schubert       fprint_frame (gdb_stdlog, fi);
15005796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, " }\n");
15015796c8dcSSimon Schubert     }
15025796c8dcSSimon Schubert 
15035796c8dcSSimon Schubert   return fi;
15045796c8dcSSimon Schubert }
15055796c8dcSSimon Schubert 
15065796c8dcSSimon Schubert /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
15075796c8dcSSimon Schubert    innermost frame).  Be careful to not fall off the bottom of the
15085796c8dcSSimon Schubert    frame chain and onto the sentinel frame.  */
15095796c8dcSSimon Schubert 
15105796c8dcSSimon Schubert struct frame_info *
get_next_frame(struct frame_info * this_frame)15115796c8dcSSimon Schubert get_next_frame (struct frame_info *this_frame)
15125796c8dcSSimon Schubert {
15135796c8dcSSimon Schubert   if (this_frame->level > 0)
15145796c8dcSSimon Schubert     return this_frame->next;
15155796c8dcSSimon Schubert   else
15165796c8dcSSimon Schubert     return NULL;
15175796c8dcSSimon Schubert }
15185796c8dcSSimon Schubert 
15195796c8dcSSimon Schubert /* Observer for the target_changed event.  */
15205796c8dcSSimon Schubert 
15215796c8dcSSimon Schubert static void
frame_observer_target_changed(struct target_ops * target)15225796c8dcSSimon Schubert frame_observer_target_changed (struct target_ops *target)
15235796c8dcSSimon Schubert {
15245796c8dcSSimon Schubert   reinit_frame_cache ();
15255796c8dcSSimon Schubert }
15265796c8dcSSimon Schubert 
15275796c8dcSSimon Schubert /* Flush the entire frame cache.  */
15285796c8dcSSimon Schubert 
15295796c8dcSSimon Schubert void
reinit_frame_cache(void)15305796c8dcSSimon Schubert reinit_frame_cache (void)
15315796c8dcSSimon Schubert {
15325796c8dcSSimon Schubert   struct frame_info *fi;
15335796c8dcSSimon Schubert 
15345796c8dcSSimon Schubert   /* Tear down all frame caches.  */
15355796c8dcSSimon Schubert   for (fi = current_frame; fi != NULL; fi = fi->prev)
15365796c8dcSSimon Schubert     {
15375796c8dcSSimon Schubert       if (fi->prologue_cache && fi->unwind->dealloc_cache)
15385796c8dcSSimon Schubert 	fi->unwind->dealloc_cache (fi, fi->prologue_cache);
15395796c8dcSSimon Schubert       if (fi->base_cache && fi->base->unwind->dealloc_cache)
15405796c8dcSSimon Schubert 	fi->base->unwind->dealloc_cache (fi, fi->base_cache);
15415796c8dcSSimon Schubert     }
15425796c8dcSSimon Schubert 
1543c50c785cSJohn Marino   /* Since we can't really be sure what the first object allocated was.  */
15445796c8dcSSimon Schubert   obstack_free (&frame_cache_obstack, 0);
15455796c8dcSSimon Schubert   obstack_init (&frame_cache_obstack);
15465796c8dcSSimon Schubert 
15475796c8dcSSimon Schubert   if (current_frame != NULL)
15485796c8dcSSimon Schubert     annotate_frames_invalid ();
15495796c8dcSSimon Schubert 
15505796c8dcSSimon Schubert   current_frame = NULL;		/* Invalidate cache */
15515796c8dcSSimon Schubert   select_frame (NULL);
15525796c8dcSSimon Schubert   frame_stash_invalidate ();
15535796c8dcSSimon Schubert   if (frame_debug)
15545796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "{ reinit_frame_cache () }\n");
15555796c8dcSSimon Schubert }
15565796c8dcSSimon Schubert 
15575796c8dcSSimon Schubert /* Find where a register is saved (in memory or another register).
15585796c8dcSSimon Schubert    The result of frame_register_unwind is just where it is saved
15595796c8dcSSimon Schubert    relative to this particular frame.  */
15605796c8dcSSimon Schubert 
15615796c8dcSSimon Schubert static void
frame_register_unwind_location(struct frame_info * this_frame,int regnum,int * optimizedp,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump)15625796c8dcSSimon Schubert frame_register_unwind_location (struct frame_info *this_frame, int regnum,
15635796c8dcSSimon Schubert 				int *optimizedp, enum lval_type *lvalp,
15645796c8dcSSimon Schubert 				CORE_ADDR *addrp, int *realnump)
15655796c8dcSSimon Schubert {
15665796c8dcSSimon Schubert   gdb_assert (this_frame == NULL || this_frame->level >= 0);
15675796c8dcSSimon Schubert 
15685796c8dcSSimon Schubert   while (this_frame != NULL)
15695796c8dcSSimon Schubert     {
1570c50c785cSJohn Marino       int unavailable;
1571c50c785cSJohn Marino 
1572c50c785cSJohn Marino       frame_register_unwind (this_frame, regnum, optimizedp, &unavailable,
1573c50c785cSJohn Marino 			     lvalp, addrp, realnump, NULL);
15745796c8dcSSimon Schubert 
15755796c8dcSSimon Schubert       if (*optimizedp)
15765796c8dcSSimon Schubert 	break;
15775796c8dcSSimon Schubert 
15785796c8dcSSimon Schubert       if (*lvalp != lval_register)
15795796c8dcSSimon Schubert 	break;
15805796c8dcSSimon Schubert 
15815796c8dcSSimon Schubert       regnum = *realnump;
15825796c8dcSSimon Schubert       this_frame = get_next_frame (this_frame);
15835796c8dcSSimon Schubert     }
15845796c8dcSSimon Schubert }
15855796c8dcSSimon Schubert 
15865796c8dcSSimon Schubert /* Return a "struct frame_info" corresponding to the frame that called
15875796c8dcSSimon Schubert    THIS_FRAME.  Returns NULL if there is no such frame.
15885796c8dcSSimon Schubert 
15895796c8dcSSimon Schubert    Unlike get_prev_frame, this function always tries to unwind the
15905796c8dcSSimon Schubert    frame.  */
15915796c8dcSSimon Schubert 
15925796c8dcSSimon Schubert static struct frame_info *
get_prev_frame_1(struct frame_info * this_frame)15935796c8dcSSimon Schubert get_prev_frame_1 (struct frame_info *this_frame)
15945796c8dcSSimon Schubert {
15955796c8dcSSimon Schubert   struct frame_id this_id;
15965796c8dcSSimon Schubert   struct gdbarch *gdbarch;
15975796c8dcSSimon Schubert 
15985796c8dcSSimon Schubert   gdb_assert (this_frame != NULL);
15995796c8dcSSimon Schubert   gdbarch = get_frame_arch (this_frame);
16005796c8dcSSimon Schubert 
16015796c8dcSSimon Schubert   if (frame_debug)
16025796c8dcSSimon Schubert     {
16035796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame_1 (this_frame=");
16045796c8dcSSimon Schubert       if (this_frame != NULL)
16055796c8dcSSimon Schubert 	fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
16065796c8dcSSimon Schubert       else
16075796c8dcSSimon Schubert 	fprintf_unfiltered (gdb_stdlog, "<NULL>");
16085796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, ") ");
16095796c8dcSSimon Schubert     }
16105796c8dcSSimon Schubert 
16115796c8dcSSimon Schubert   /* Only try to do the unwind once.  */
16125796c8dcSSimon Schubert   if (this_frame->prev_p)
16135796c8dcSSimon Schubert     {
16145796c8dcSSimon Schubert       if (frame_debug)
16155796c8dcSSimon Schubert 	{
16165796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "-> ");
16175796c8dcSSimon Schubert 	  fprint_frame (gdb_stdlog, this_frame->prev);
16185796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, " // cached \n");
16195796c8dcSSimon Schubert 	}
16205796c8dcSSimon Schubert       return this_frame->prev;
16215796c8dcSSimon Schubert     }
16225796c8dcSSimon Schubert 
16235796c8dcSSimon Schubert   /* If the frame unwinder hasn't been selected yet, we must do so
16245796c8dcSSimon Schubert      before setting prev_p; otherwise the check for misbehaved
16255796c8dcSSimon Schubert      sniffers will think that this frame's sniffer tried to unwind
16265796c8dcSSimon Schubert      further (see frame_cleanup_after_sniffer).  */
16275796c8dcSSimon Schubert   if (this_frame->unwind == NULL)
1628c50c785cSJohn Marino     frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
16295796c8dcSSimon Schubert 
16305796c8dcSSimon Schubert   this_frame->prev_p = 1;
16315796c8dcSSimon Schubert   this_frame->stop_reason = UNWIND_NO_REASON;
16325796c8dcSSimon Schubert 
16335796c8dcSSimon Schubert   /* If we are unwinding from an inline frame, all of the below tests
16345796c8dcSSimon Schubert      were already performed when we unwound from the next non-inline
16355796c8dcSSimon Schubert      frame.  We must skip them, since we can not get THIS_FRAME's ID
16365796c8dcSSimon Schubert      until we have unwound all the way down to the previous non-inline
16375796c8dcSSimon Schubert      frame.  */
16385796c8dcSSimon Schubert   if (get_frame_type (this_frame) == INLINE_FRAME)
16395796c8dcSSimon Schubert     return get_prev_frame_raw (this_frame);
16405796c8dcSSimon Schubert 
1641c50c785cSJohn Marino   /* Check that this frame is unwindable.  If it isn't, don't try to
1642c50c785cSJohn Marino      unwind to the prev frame.  */
1643c50c785cSJohn Marino   this_frame->stop_reason
1644c50c785cSJohn Marino     = this_frame->unwind->stop_reason (this_frame,
1645c50c785cSJohn Marino 				       &this_frame->prologue_cache);
1646c50c785cSJohn Marino 
1647c50c785cSJohn Marino   if (this_frame->stop_reason != UNWIND_NO_REASON)
1648c50c785cSJohn Marino     return NULL;
1649c50c785cSJohn Marino 
16505796c8dcSSimon Schubert   /* Check that this frame's ID was valid.  If it wasn't, don't try to
16515796c8dcSSimon Schubert      unwind to the prev frame.  Be careful to not apply this test to
16525796c8dcSSimon Schubert      the sentinel frame.  */
16535796c8dcSSimon Schubert   this_id = get_frame_id (this_frame);
16545796c8dcSSimon Schubert   if (this_frame->level >= 0 && frame_id_eq (this_id, outer_frame_id))
16555796c8dcSSimon Schubert     {
16565796c8dcSSimon Schubert       if (frame_debug)
16575796c8dcSSimon Schubert 	{
16585796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "-> ");
16595796c8dcSSimon Schubert 	  fprint_frame (gdb_stdlog, NULL);
16605796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n");
16615796c8dcSSimon Schubert 	}
16625796c8dcSSimon Schubert       this_frame->stop_reason = UNWIND_NULL_ID;
16635796c8dcSSimon Schubert       return NULL;
16645796c8dcSSimon Schubert     }
16655796c8dcSSimon Schubert 
16665796c8dcSSimon Schubert   /* Check that this frame's ID isn't inner to (younger, below, next)
16675796c8dcSSimon Schubert      the next frame.  This happens when a frame unwind goes backwards.
16685796c8dcSSimon Schubert      This check is valid only if this frame and the next frame are NORMAL.
16695796c8dcSSimon Schubert      See the comment at frame_id_inner for details.  */
16705796c8dcSSimon Schubert   if (get_frame_type (this_frame) == NORMAL_FRAME
16715796c8dcSSimon Schubert       && this_frame->next->unwind->type == NORMAL_FRAME
16725796c8dcSSimon Schubert       && frame_id_inner (get_frame_arch (this_frame->next), this_id,
16735796c8dcSSimon Schubert 			 get_frame_id (this_frame->next)))
16745796c8dcSSimon Schubert     {
1675c50c785cSJohn Marino       CORE_ADDR this_pc_in_block;
1676c50c785cSJohn Marino       struct minimal_symbol *morestack_msym;
1677c50c785cSJohn Marino       const char *morestack_name = NULL;
1678c50c785cSJohn Marino 
1679c50c785cSJohn Marino       /* gcc -fsplit-stack __morestack can continue the stack anywhere.  */
1680c50c785cSJohn Marino       this_pc_in_block = get_frame_address_in_block (this_frame);
1681c50c785cSJohn Marino       morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block);
1682c50c785cSJohn Marino       if (morestack_msym)
1683c50c785cSJohn Marino 	morestack_name = SYMBOL_LINKAGE_NAME (morestack_msym);
1684c50c785cSJohn Marino       if (!morestack_name || strcmp (morestack_name, "__morestack") != 0)
1685c50c785cSJohn Marino 	{
16865796c8dcSSimon Schubert 	  if (frame_debug)
16875796c8dcSSimon Schubert 	    {
16885796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "-> ");
16895796c8dcSSimon Schubert 	      fprint_frame (gdb_stdlog, NULL);
1690c50c785cSJohn Marino 	      fprintf_unfiltered (gdb_stdlog,
1691c50c785cSJohn Marino 				  " // this frame ID is inner }\n");
16925796c8dcSSimon Schubert 	    }
16935796c8dcSSimon Schubert 	  this_frame->stop_reason = UNWIND_INNER_ID;
16945796c8dcSSimon Schubert 	  return NULL;
16955796c8dcSSimon Schubert 	}
1696c50c785cSJohn Marino     }
16975796c8dcSSimon Schubert 
16985796c8dcSSimon Schubert   /* Check that this and the next frame are not identical.  If they
16995796c8dcSSimon Schubert      are, there is most likely a stack cycle.  As with the inner-than
17005796c8dcSSimon Schubert      test above, avoid comparing the inner-most and sentinel frames.  */
17015796c8dcSSimon Schubert   if (this_frame->level > 0
17025796c8dcSSimon Schubert       && frame_id_eq (this_id, get_frame_id (this_frame->next)))
17035796c8dcSSimon Schubert     {
17045796c8dcSSimon Schubert       if (frame_debug)
17055796c8dcSSimon Schubert 	{
17065796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "-> ");
17075796c8dcSSimon Schubert 	  fprint_frame (gdb_stdlog, NULL);
17085796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, " // this frame has same ID }\n");
17095796c8dcSSimon Schubert 	}
17105796c8dcSSimon Schubert       this_frame->stop_reason = UNWIND_SAME_ID;
17115796c8dcSSimon Schubert       return NULL;
17125796c8dcSSimon Schubert     }
17135796c8dcSSimon Schubert 
17145796c8dcSSimon Schubert   /* Check that this and the next frame do not unwind the PC register
17155796c8dcSSimon Schubert      to the same memory location.  If they do, then even though they
17165796c8dcSSimon Schubert      have different frame IDs, the new frame will be bogus; two
17175796c8dcSSimon Schubert      functions can't share a register save slot for the PC.  This can
17185796c8dcSSimon Schubert      happen when the prologue analyzer finds a stack adjustment, but
17195796c8dcSSimon Schubert      no PC save.
17205796c8dcSSimon Schubert 
17215796c8dcSSimon Schubert      This check does assume that the "PC register" is roughly a
17225796c8dcSSimon Schubert      traditional PC, even if the gdbarch_unwind_pc method adjusts
17235796c8dcSSimon Schubert      it (we do not rely on the value, only on the unwound PC being
17245796c8dcSSimon Schubert      dependent on this value).  A potential improvement would be
17255796c8dcSSimon Schubert      to have the frame prev_pc method and the gdbarch unwind_pc
17265796c8dcSSimon Schubert      method set the same lval and location information as
17275796c8dcSSimon Schubert      frame_register_unwind.  */
17285796c8dcSSimon Schubert   if (this_frame->level > 0
17295796c8dcSSimon Schubert       && gdbarch_pc_regnum (gdbarch) >= 0
17305796c8dcSSimon Schubert       && get_frame_type (this_frame) == NORMAL_FRAME
17315796c8dcSSimon Schubert       && (get_frame_type (this_frame->next) == NORMAL_FRAME
17325796c8dcSSimon Schubert 	  || get_frame_type (this_frame->next) == INLINE_FRAME))
17335796c8dcSSimon Schubert     {
17345796c8dcSSimon Schubert       int optimized, realnum, nrealnum;
17355796c8dcSSimon Schubert       enum lval_type lval, nlval;
17365796c8dcSSimon Schubert       CORE_ADDR addr, naddr;
17375796c8dcSSimon Schubert 
17385796c8dcSSimon Schubert       frame_register_unwind_location (this_frame,
17395796c8dcSSimon Schubert 				      gdbarch_pc_regnum (gdbarch),
17405796c8dcSSimon Schubert 				      &optimized, &lval, &addr, &realnum);
17415796c8dcSSimon Schubert       frame_register_unwind_location (get_next_frame (this_frame),
17425796c8dcSSimon Schubert 				      gdbarch_pc_regnum (gdbarch),
17435796c8dcSSimon Schubert 				      &optimized, &nlval, &naddr, &nrealnum);
17445796c8dcSSimon Schubert 
17455796c8dcSSimon Schubert       if ((lval == lval_memory && lval == nlval && addr == naddr)
17465796c8dcSSimon Schubert 	  || (lval == lval_register && lval == nlval && realnum == nrealnum))
17475796c8dcSSimon Schubert 	{
17485796c8dcSSimon Schubert 	  if (frame_debug)
17495796c8dcSSimon Schubert 	    {
17505796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, "-> ");
17515796c8dcSSimon Schubert 	      fprint_frame (gdb_stdlog, NULL);
17525796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog, " // no saved PC }\n");
17535796c8dcSSimon Schubert 	    }
17545796c8dcSSimon Schubert 
17555796c8dcSSimon Schubert 	  this_frame->stop_reason = UNWIND_NO_SAVED_PC;
17565796c8dcSSimon Schubert 	  this_frame->prev = NULL;
17575796c8dcSSimon Schubert 	  return NULL;
17585796c8dcSSimon Schubert 	}
17595796c8dcSSimon Schubert     }
17605796c8dcSSimon Schubert 
17615796c8dcSSimon Schubert   return get_prev_frame_raw (this_frame);
17625796c8dcSSimon Schubert }
17635796c8dcSSimon Schubert 
17645796c8dcSSimon Schubert /* Construct a new "struct frame_info" and link it previous to
17655796c8dcSSimon Schubert    this_frame.  */
17665796c8dcSSimon Schubert 
17675796c8dcSSimon Schubert static struct frame_info *
get_prev_frame_raw(struct frame_info * this_frame)17685796c8dcSSimon Schubert get_prev_frame_raw (struct frame_info *this_frame)
17695796c8dcSSimon Schubert {
17705796c8dcSSimon Schubert   struct frame_info *prev_frame;
17715796c8dcSSimon Schubert 
17725796c8dcSSimon Schubert   /* Allocate the new frame but do not wire it in to the frame chain.
17735796c8dcSSimon Schubert      Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
17745796c8dcSSimon Schubert      frame->next to pull some fancy tricks (of course such code is, by
17755796c8dcSSimon Schubert      definition, recursive).  Try to prevent it.
17765796c8dcSSimon Schubert 
17775796c8dcSSimon Schubert      There is no reason to worry about memory leaks, should the
17785796c8dcSSimon Schubert      remainder of the function fail.  The allocated memory will be
17795796c8dcSSimon Schubert      quickly reclaimed when the frame cache is flushed, and the `we've
17805796c8dcSSimon Schubert      been here before' check above will stop repeated memory
17815796c8dcSSimon Schubert      allocation calls.  */
17825796c8dcSSimon Schubert   prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
17835796c8dcSSimon Schubert   prev_frame->level = this_frame->level + 1;
17845796c8dcSSimon Schubert 
1785cf7f2e2dSJohn Marino   /* For now, assume we don't have frame chains crossing address
1786cf7f2e2dSJohn Marino      spaces.  */
1787cf7f2e2dSJohn Marino   prev_frame->pspace = this_frame->pspace;
1788cf7f2e2dSJohn Marino   prev_frame->aspace = this_frame->aspace;
1789cf7f2e2dSJohn Marino 
17905796c8dcSSimon Schubert   /* Don't yet compute ->unwind (and hence ->type).  It is computed
17915796c8dcSSimon Schubert      on-demand in get_frame_type, frame_register_unwind, and
17925796c8dcSSimon Schubert      get_frame_id.  */
17935796c8dcSSimon Schubert 
17945796c8dcSSimon Schubert   /* Don't yet compute the frame's ID.  It is computed on-demand by
17955796c8dcSSimon Schubert      get_frame_id().  */
17965796c8dcSSimon Schubert 
17975796c8dcSSimon Schubert   /* The unwound frame ID is validate at the start of this function,
17985796c8dcSSimon Schubert      as part of the logic to decide if that frame should be further
17995796c8dcSSimon Schubert      unwound, and not here while the prev frame is being created.
18005796c8dcSSimon Schubert      Doing this makes it possible for the user to examine a frame that
18015796c8dcSSimon Schubert      has an invalid frame ID.
18025796c8dcSSimon Schubert 
18035796c8dcSSimon Schubert      Some very old VAX code noted: [...]  For the sake of argument,
18045796c8dcSSimon Schubert      suppose that the stack is somewhat trashed (which is one reason
18055796c8dcSSimon Schubert      that "info frame" exists).  So, return 0 (indicating we don't
18065796c8dcSSimon Schubert      know the address of the arglist) if we don't know what frame this
18075796c8dcSSimon Schubert      frame calls.  */
18085796c8dcSSimon Schubert 
18095796c8dcSSimon Schubert   /* Link it in.  */
18105796c8dcSSimon Schubert   this_frame->prev = prev_frame;
18115796c8dcSSimon Schubert   prev_frame->next = this_frame;
18125796c8dcSSimon Schubert 
18135796c8dcSSimon Schubert   if (frame_debug)
18145796c8dcSSimon Schubert     {
18155796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, "-> ");
18165796c8dcSSimon Schubert       fprint_frame (gdb_stdlog, prev_frame);
18175796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, " }\n");
18185796c8dcSSimon Schubert     }
18195796c8dcSSimon Schubert 
18205796c8dcSSimon Schubert   return prev_frame;
18215796c8dcSSimon Schubert }
18225796c8dcSSimon Schubert 
18235796c8dcSSimon Schubert /* Debug routine to print a NULL frame being returned.  */
18245796c8dcSSimon Schubert 
18255796c8dcSSimon Schubert static void
frame_debug_got_null_frame(struct frame_info * this_frame,const char * reason)18265796c8dcSSimon Schubert frame_debug_got_null_frame (struct frame_info *this_frame,
18275796c8dcSSimon Schubert 			    const char *reason)
18285796c8dcSSimon Schubert {
18295796c8dcSSimon Schubert   if (frame_debug)
18305796c8dcSSimon Schubert     {
18315796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame=");
18325796c8dcSSimon Schubert       if (this_frame != NULL)
18335796c8dcSSimon Schubert 	fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level);
18345796c8dcSSimon Schubert       else
18355796c8dcSSimon Schubert 	fprintf_unfiltered (gdb_stdlog, "<NULL>");
18365796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stdlog, ") -> // %s}\n", reason);
18375796c8dcSSimon Schubert     }
18385796c8dcSSimon Schubert }
18395796c8dcSSimon Schubert 
18405796c8dcSSimon Schubert /* Is this (non-sentinel) frame in the "main"() function?  */
18415796c8dcSSimon Schubert 
18425796c8dcSSimon Schubert static int
inside_main_func(struct frame_info * this_frame)18435796c8dcSSimon Schubert inside_main_func (struct frame_info *this_frame)
18445796c8dcSSimon Schubert {
18455796c8dcSSimon Schubert   struct minimal_symbol *msymbol;
18465796c8dcSSimon Schubert   CORE_ADDR maddr;
18475796c8dcSSimon Schubert 
18485796c8dcSSimon Schubert   if (symfile_objfile == 0)
18495796c8dcSSimon Schubert     return 0;
18505796c8dcSSimon Schubert   msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
18515796c8dcSSimon Schubert   if (msymbol == NULL)
18525796c8dcSSimon Schubert     return 0;
18535796c8dcSSimon Schubert   /* Make certain that the code, and not descriptor, address is
18545796c8dcSSimon Schubert      returned.  */
18555796c8dcSSimon Schubert   maddr = gdbarch_convert_from_func_ptr_addr (get_frame_arch (this_frame),
18565796c8dcSSimon Schubert 					      SYMBOL_VALUE_ADDRESS (msymbol),
18575796c8dcSSimon Schubert 					      &current_target);
18585796c8dcSSimon Schubert   return maddr == get_frame_func (this_frame);
18595796c8dcSSimon Schubert }
18605796c8dcSSimon Schubert 
18615796c8dcSSimon Schubert /* Test whether THIS_FRAME is inside the process entry point function.  */
18625796c8dcSSimon Schubert 
18635796c8dcSSimon Schubert static int
inside_entry_func(struct frame_info * this_frame)18645796c8dcSSimon Schubert inside_entry_func (struct frame_info *this_frame)
18655796c8dcSSimon Schubert {
1866cf7f2e2dSJohn Marino   CORE_ADDR entry_point;
1867cf7f2e2dSJohn Marino 
1868cf7f2e2dSJohn Marino   if (!entry_point_address_query (&entry_point))
1869cf7f2e2dSJohn Marino     return 0;
1870cf7f2e2dSJohn Marino 
1871cf7f2e2dSJohn Marino   return get_frame_func (this_frame) == entry_point;
18725796c8dcSSimon Schubert }
18735796c8dcSSimon Schubert 
18745796c8dcSSimon Schubert /* Return a structure containing various interesting information about
18755796c8dcSSimon Schubert    the frame that called THIS_FRAME.  Returns NULL if there is entier
18765796c8dcSSimon Schubert    no such frame or the frame fails any of a set of target-independent
18775796c8dcSSimon Schubert    condition that should terminate the frame chain (e.g., as unwinding
18785796c8dcSSimon Schubert    past main()).
18795796c8dcSSimon Schubert 
18805796c8dcSSimon Schubert    This function should not contain target-dependent tests, such as
18815796c8dcSSimon Schubert    checking whether the program-counter is zero.  */
18825796c8dcSSimon Schubert 
18835796c8dcSSimon Schubert struct frame_info *
get_prev_frame(struct frame_info * this_frame)18845796c8dcSSimon Schubert get_prev_frame (struct frame_info *this_frame)
18855796c8dcSSimon Schubert {
1886c50c785cSJohn Marino   CORE_ADDR frame_pc;
1887c50c785cSJohn Marino   int frame_pc_p;
1888c50c785cSJohn Marino 
18895796c8dcSSimon Schubert   /* There is always a frame.  If this assertion fails, suspect that
18905796c8dcSSimon Schubert      something should be calling get_selected_frame() or
18915796c8dcSSimon Schubert      get_current_frame().  */
18925796c8dcSSimon Schubert   gdb_assert (this_frame != NULL);
1893c50c785cSJohn Marino   frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc);
18945796c8dcSSimon Schubert 
18955796c8dcSSimon Schubert   /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
18965796c8dcSSimon Schubert      sense to stop unwinding at a dummy frame.  One place where a dummy
18975796c8dcSSimon Schubert      frame may have an address "inside_main_func" is on HPUX.  On HPUX, the
18985796c8dcSSimon Schubert      pcsqh register (space register for the instruction at the head of the
18995796c8dcSSimon Schubert      instruction queue) cannot be written directly; the only way to set it
19005796c8dcSSimon Schubert      is to branch to code that is in the target space.  In order to implement
19015796c8dcSSimon Schubert      frame dummies on HPUX, the called function is made to jump back to where
19025796c8dcSSimon Schubert      the inferior was when the user function was called.  If gdb was inside
19035796c8dcSSimon Schubert      the main function when we created the dummy frame, the dummy frame will
19045796c8dcSSimon Schubert      point inside the main function.  */
19055796c8dcSSimon Schubert   if (this_frame->level >= 0
19065796c8dcSSimon Schubert       && get_frame_type (this_frame) == NORMAL_FRAME
19075796c8dcSSimon Schubert       && !backtrace_past_main
1908c50c785cSJohn Marino       && frame_pc_p
19095796c8dcSSimon Schubert       && inside_main_func (this_frame))
19105796c8dcSSimon Schubert     /* Don't unwind past main().  Note, this is done _before_ the
19115796c8dcSSimon Schubert        frame has been marked as previously unwound.  That way if the
19125796c8dcSSimon Schubert        user later decides to enable unwinds past main(), that will
19135796c8dcSSimon Schubert        automatically happen.  */
19145796c8dcSSimon Schubert     {
19155796c8dcSSimon Schubert       frame_debug_got_null_frame (this_frame, "inside main func");
19165796c8dcSSimon Schubert       return NULL;
19175796c8dcSSimon Schubert     }
19185796c8dcSSimon Schubert 
19195796c8dcSSimon Schubert   /* If the user's backtrace limit has been exceeded, stop.  We must
19205796c8dcSSimon Schubert      add two to the current level; one of those accounts for backtrace_limit
19215796c8dcSSimon Schubert      being 1-based and the level being 0-based, and the other accounts for
19225796c8dcSSimon Schubert      the level of the new frame instead of the level of the current
19235796c8dcSSimon Schubert      frame.  */
19245796c8dcSSimon Schubert   if (this_frame->level + 2 > backtrace_limit)
19255796c8dcSSimon Schubert     {
19265796c8dcSSimon Schubert       frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
19275796c8dcSSimon Schubert       return NULL;
19285796c8dcSSimon Schubert     }
19295796c8dcSSimon Schubert 
19305796c8dcSSimon Schubert   /* If we're already inside the entry function for the main objfile,
19315796c8dcSSimon Schubert      then it isn't valid.  Don't apply this test to a dummy frame -
19325796c8dcSSimon Schubert      dummy frame PCs typically land in the entry func.  Don't apply
19335796c8dcSSimon Schubert      this test to the sentinel frame.  Sentinel frames should always
19345796c8dcSSimon Schubert      be allowed to unwind.  */
19355796c8dcSSimon Schubert   /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
19365796c8dcSSimon Schubert      wasn't checking for "main" in the minimal symbols.  With that
19375796c8dcSSimon Schubert      fixed asm-source tests now stop in "main" instead of halting the
19385796c8dcSSimon Schubert      backtrace in weird and wonderful ways somewhere inside the entry
19395796c8dcSSimon Schubert      file.  Suspect that tests for inside the entry file/func were
19405796c8dcSSimon Schubert      added to work around that (now fixed) case.  */
19415796c8dcSSimon Schubert   /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
19425796c8dcSSimon Schubert      suggested having the inside_entry_func test use the
19435796c8dcSSimon Schubert      inside_main_func() msymbol trick (along with entry_point_address()
19445796c8dcSSimon Schubert      I guess) to determine the address range of the start function.
19455796c8dcSSimon Schubert      That should provide a far better stopper than the current
19465796c8dcSSimon Schubert      heuristics.  */
19475796c8dcSSimon Schubert   /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
19485796c8dcSSimon Schubert      applied tail-call optimizations to main so that a function called
19495796c8dcSSimon Schubert      from main returns directly to the caller of main.  Since we don't
19505796c8dcSSimon Schubert      stop at main, we should at least stop at the entry point of the
19515796c8dcSSimon Schubert      application.  */
19525796c8dcSSimon Schubert   if (this_frame->level >= 0
19535796c8dcSSimon Schubert       && get_frame_type (this_frame) == NORMAL_FRAME
19545796c8dcSSimon Schubert       && !backtrace_past_entry
1955c50c785cSJohn Marino       && frame_pc_p
19565796c8dcSSimon Schubert       && inside_entry_func (this_frame))
19575796c8dcSSimon Schubert     {
19585796c8dcSSimon Schubert       frame_debug_got_null_frame (this_frame, "inside entry func");
19595796c8dcSSimon Schubert       return NULL;
19605796c8dcSSimon Schubert     }
19615796c8dcSSimon Schubert 
19625796c8dcSSimon Schubert   /* Assume that the only way to get a zero PC is through something
19635796c8dcSSimon Schubert      like a SIGSEGV or a dummy frame, and hence that NORMAL frames
19645796c8dcSSimon Schubert      will never unwind a zero PC.  */
19655796c8dcSSimon Schubert   if (this_frame->level > 0
19665796c8dcSSimon Schubert       && (get_frame_type (this_frame) == NORMAL_FRAME
19675796c8dcSSimon Schubert 	  || get_frame_type (this_frame) == INLINE_FRAME)
19685796c8dcSSimon Schubert       && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
1969c50c785cSJohn Marino       && frame_pc_p && frame_pc == 0)
19705796c8dcSSimon Schubert     {
19715796c8dcSSimon Schubert       frame_debug_got_null_frame (this_frame, "zero PC");
19725796c8dcSSimon Schubert       return NULL;
19735796c8dcSSimon Schubert     }
19745796c8dcSSimon Schubert 
19755796c8dcSSimon Schubert   return get_prev_frame_1 (this_frame);
19765796c8dcSSimon Schubert }
19775796c8dcSSimon Schubert 
19785796c8dcSSimon Schubert CORE_ADDR
get_frame_pc(struct frame_info * frame)19795796c8dcSSimon Schubert get_frame_pc (struct frame_info *frame)
19805796c8dcSSimon Schubert {
19815796c8dcSSimon Schubert   gdb_assert (frame->next != NULL);
19825796c8dcSSimon Schubert   return frame_unwind_pc (frame->next);
19835796c8dcSSimon Schubert }
19845796c8dcSSimon Schubert 
1985c50c785cSJohn Marino int
get_frame_pc_if_available(struct frame_info * frame,CORE_ADDR * pc)1986c50c785cSJohn Marino get_frame_pc_if_available (struct frame_info *frame, CORE_ADDR *pc)
1987c50c785cSJohn Marino {
1988c50c785cSJohn Marino   volatile struct gdb_exception ex;
1989c50c785cSJohn Marino 
1990c50c785cSJohn Marino   gdb_assert (frame->next != NULL);
1991c50c785cSJohn Marino 
1992c50c785cSJohn Marino   TRY_CATCH (ex, RETURN_MASK_ERROR)
1993c50c785cSJohn Marino     {
1994c50c785cSJohn Marino       *pc = frame_unwind_pc (frame->next);
1995c50c785cSJohn Marino     }
1996c50c785cSJohn Marino   if (ex.reason < 0)
1997c50c785cSJohn Marino     {
1998c50c785cSJohn Marino       if (ex.error == NOT_AVAILABLE_ERROR)
1999c50c785cSJohn Marino 	return 0;
2000c50c785cSJohn Marino       else
2001c50c785cSJohn Marino 	throw_exception (ex);
2002c50c785cSJohn Marino     }
2003c50c785cSJohn Marino 
2004c50c785cSJohn Marino   return 1;
2005c50c785cSJohn Marino }
2006c50c785cSJohn Marino 
20075796c8dcSSimon Schubert /* Return an address that falls within THIS_FRAME's code block.  */
20085796c8dcSSimon Schubert 
20095796c8dcSSimon Schubert CORE_ADDR
get_frame_address_in_block(struct frame_info * this_frame)20105796c8dcSSimon Schubert get_frame_address_in_block (struct frame_info *this_frame)
20115796c8dcSSimon Schubert {
20125796c8dcSSimon Schubert   /* A draft address.  */
20135796c8dcSSimon Schubert   CORE_ADDR pc = get_frame_pc (this_frame);
20145796c8dcSSimon Schubert 
20155796c8dcSSimon Schubert   struct frame_info *next_frame = this_frame->next;
20165796c8dcSSimon Schubert 
20175796c8dcSSimon Schubert   /* Calling get_frame_pc returns the resume address for THIS_FRAME.
20185796c8dcSSimon Schubert      Normally the resume address is inside the body of the function
20195796c8dcSSimon Schubert      associated with THIS_FRAME, but there is a special case: when
20205796c8dcSSimon Schubert      calling a function which the compiler knows will never return
20215796c8dcSSimon Schubert      (for instance abort), the call may be the very last instruction
20225796c8dcSSimon Schubert      in the calling function.  The resume address will point after the
20235796c8dcSSimon Schubert      call and may be at the beginning of a different function
20245796c8dcSSimon Schubert      entirely.
20255796c8dcSSimon Schubert 
20265796c8dcSSimon Schubert      If THIS_FRAME is a signal frame or dummy frame, then we should
20275796c8dcSSimon Schubert      not adjust the unwound PC.  For a dummy frame, GDB pushed the
20285796c8dcSSimon Schubert      resume address manually onto the stack.  For a signal frame, the
20295796c8dcSSimon Schubert      OS may have pushed the resume address manually and invoked the
20305796c8dcSSimon Schubert      handler (e.g. GNU/Linux), or invoked the trampoline which called
20315796c8dcSSimon Schubert      the signal handler - but in either case the signal handler is
20325796c8dcSSimon Schubert      expected to return to the trampoline.  So in both of these
20335796c8dcSSimon Schubert      cases we know that the resume address is executable and
20345796c8dcSSimon Schubert      related.  So we only need to adjust the PC if THIS_FRAME
20355796c8dcSSimon Schubert      is a normal function.
20365796c8dcSSimon Schubert 
20375796c8dcSSimon Schubert      If the program has been interrupted while THIS_FRAME is current,
20385796c8dcSSimon Schubert      then clearly the resume address is inside the associated
20395796c8dcSSimon Schubert      function.  There are three kinds of interruption: debugger stop
20405796c8dcSSimon Schubert      (next frame will be SENTINEL_FRAME), operating system
20415796c8dcSSimon Schubert      signal or exception (next frame will be SIGTRAMP_FRAME),
20425796c8dcSSimon Schubert      or debugger-induced function call (next frame will be
20435796c8dcSSimon Schubert      DUMMY_FRAME).  So we only need to adjust the PC if
20445796c8dcSSimon Schubert      NEXT_FRAME is a normal function.
20455796c8dcSSimon Schubert 
20465796c8dcSSimon Schubert      We check the type of NEXT_FRAME first, since it is already
20475796c8dcSSimon Schubert      known; frame type is determined by the unwinder, and since
20485796c8dcSSimon Schubert      we have THIS_FRAME we've already selected an unwinder for
20495796c8dcSSimon Schubert      NEXT_FRAME.
20505796c8dcSSimon Schubert 
20515796c8dcSSimon Schubert      If the next frame is inlined, we need to keep going until we find
20525796c8dcSSimon Schubert      the real function - for instance, if a signal handler is invoked
20535796c8dcSSimon Schubert      while in an inlined function, then the code address of the
20545796c8dcSSimon Schubert      "calling" normal function should not be adjusted either.  */
20555796c8dcSSimon Schubert 
20565796c8dcSSimon Schubert   while (get_frame_type (next_frame) == INLINE_FRAME)
20575796c8dcSSimon Schubert     next_frame = next_frame->next;
20585796c8dcSSimon Schubert 
2059a45ae5f8SJohn Marino   if ((get_frame_type (next_frame) == NORMAL_FRAME
2060a45ae5f8SJohn Marino        || get_frame_type (next_frame) == TAILCALL_FRAME)
20615796c8dcSSimon Schubert       && (get_frame_type (this_frame) == NORMAL_FRAME
2062a45ae5f8SJohn Marino 	  || get_frame_type (this_frame) == TAILCALL_FRAME
20635796c8dcSSimon Schubert 	  || get_frame_type (this_frame) == INLINE_FRAME))
20645796c8dcSSimon Schubert     return pc - 1;
20655796c8dcSSimon Schubert 
20665796c8dcSSimon Schubert   return pc;
20675796c8dcSSimon Schubert }
20685796c8dcSSimon Schubert 
2069c50c785cSJohn Marino int
get_frame_address_in_block_if_available(struct frame_info * this_frame,CORE_ADDR * pc)2070c50c785cSJohn Marino get_frame_address_in_block_if_available (struct frame_info *this_frame,
2071c50c785cSJohn Marino 					 CORE_ADDR *pc)
2072c50c785cSJohn Marino {
2073c50c785cSJohn Marino   volatile struct gdb_exception ex;
2074c50c785cSJohn Marino 
2075c50c785cSJohn Marino   TRY_CATCH (ex, RETURN_MASK_ERROR)
2076c50c785cSJohn Marino     {
2077c50c785cSJohn Marino       *pc = get_frame_address_in_block (this_frame);
2078c50c785cSJohn Marino     }
2079c50c785cSJohn Marino   if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
2080c50c785cSJohn Marino     return 0;
2081c50c785cSJohn Marino   else if (ex.reason < 0)
2082c50c785cSJohn Marino     throw_exception (ex);
2083c50c785cSJohn Marino   else
2084c50c785cSJohn Marino     return 1;
2085c50c785cSJohn Marino }
2086c50c785cSJohn Marino 
20875796c8dcSSimon Schubert void
find_frame_sal(struct frame_info * frame,struct symtab_and_line * sal)20885796c8dcSSimon Schubert find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal)
20895796c8dcSSimon Schubert {
20905796c8dcSSimon Schubert   struct frame_info *next_frame;
20915796c8dcSSimon Schubert   int notcurrent;
2092c50c785cSJohn Marino   CORE_ADDR pc;
20935796c8dcSSimon Schubert 
20945796c8dcSSimon Schubert   /* If the next frame represents an inlined function call, this frame's
20955796c8dcSSimon Schubert      sal is the "call site" of that inlined function, which can not
20965796c8dcSSimon Schubert      be inferred from get_frame_pc.  */
20975796c8dcSSimon Schubert   next_frame = get_next_frame (frame);
20985796c8dcSSimon Schubert   if (frame_inlined_callees (frame) > 0)
20995796c8dcSSimon Schubert     {
21005796c8dcSSimon Schubert       struct symbol *sym;
21015796c8dcSSimon Schubert 
21025796c8dcSSimon Schubert       if (next_frame)
21035796c8dcSSimon Schubert 	sym = get_frame_function (next_frame);
21045796c8dcSSimon Schubert       else
21055796c8dcSSimon Schubert 	sym = inline_skipped_symbol (inferior_ptid);
21065796c8dcSSimon Schubert 
2107c50c785cSJohn Marino       /* If frame is inline, it certainly has symbols.  */
2108c50c785cSJohn Marino       gdb_assert (sym);
21095796c8dcSSimon Schubert       init_sal (sal);
21105796c8dcSSimon Schubert       if (SYMBOL_LINE (sym) != 0)
21115796c8dcSSimon Schubert 	{
21125796c8dcSSimon Schubert 	  sal->symtab = SYMBOL_SYMTAB (sym);
21135796c8dcSSimon Schubert 	  sal->line = SYMBOL_LINE (sym);
21145796c8dcSSimon Schubert 	}
21155796c8dcSSimon Schubert       else
21165796c8dcSSimon Schubert 	/* If the symbol does not have a location, we don't know where
21175796c8dcSSimon Schubert 	   the call site is.  Do not pretend to.  This is jarring, but
21185796c8dcSSimon Schubert 	   we can't do much better.  */
21195796c8dcSSimon Schubert 	sal->pc = get_frame_pc (frame);
21205796c8dcSSimon Schubert 
2121*ef5ccd6cSJohn Marino       sal->pspace = get_frame_program_space (frame);
2122*ef5ccd6cSJohn Marino 
21235796c8dcSSimon Schubert       return;
21245796c8dcSSimon Schubert     }
21255796c8dcSSimon Schubert 
21265796c8dcSSimon Schubert   /* If FRAME is not the innermost frame, that normally means that
21275796c8dcSSimon Schubert      FRAME->pc points at the return instruction (which is *after* the
21285796c8dcSSimon Schubert      call instruction), and we want to get the line containing the
21295796c8dcSSimon Schubert      call (because the call is where the user thinks the program is).
21305796c8dcSSimon Schubert      However, if the next frame is either a SIGTRAMP_FRAME or a
21315796c8dcSSimon Schubert      DUMMY_FRAME, then the next frame will contain a saved interrupt
21325796c8dcSSimon Schubert      PC and such a PC indicates the current (rather than next)
21335796c8dcSSimon Schubert      instruction/line, consequently, for such cases, want to get the
21345796c8dcSSimon Schubert      line containing fi->pc.  */
2135c50c785cSJohn Marino   if (!get_frame_pc_if_available (frame, &pc))
2136c50c785cSJohn Marino     {
2137c50c785cSJohn Marino       init_sal (sal);
2138c50c785cSJohn Marino       return;
2139c50c785cSJohn Marino     }
2140c50c785cSJohn Marino 
2141c50c785cSJohn Marino   notcurrent = (pc != get_frame_address_in_block (frame));
2142c50c785cSJohn Marino   (*sal) = find_pc_line (pc, notcurrent);
21435796c8dcSSimon Schubert }
21445796c8dcSSimon Schubert 
21455796c8dcSSimon Schubert /* Per "frame.h", return the ``address'' of the frame.  Code should
21465796c8dcSSimon Schubert    really be using get_frame_id().  */
21475796c8dcSSimon Schubert CORE_ADDR
get_frame_base(struct frame_info * fi)21485796c8dcSSimon Schubert get_frame_base (struct frame_info *fi)
21495796c8dcSSimon Schubert {
21505796c8dcSSimon Schubert   return get_frame_id (fi).stack_addr;
21515796c8dcSSimon Schubert }
21525796c8dcSSimon Schubert 
21535796c8dcSSimon Schubert /* High-level offsets into the frame.  Used by the debug info.  */
21545796c8dcSSimon Schubert 
21555796c8dcSSimon Schubert CORE_ADDR
get_frame_base_address(struct frame_info * fi)21565796c8dcSSimon Schubert get_frame_base_address (struct frame_info *fi)
21575796c8dcSSimon Schubert {
21585796c8dcSSimon Schubert   if (get_frame_type (fi) != NORMAL_FRAME)
21595796c8dcSSimon Schubert     return 0;
21605796c8dcSSimon Schubert   if (fi->base == NULL)
21615796c8dcSSimon Schubert     fi->base = frame_base_find_by_frame (fi);
21625796c8dcSSimon Schubert   /* Sneaky: If the low-level unwind and high-level base code share a
21635796c8dcSSimon Schubert      common unwinder, let them share the prologue cache.  */
21645796c8dcSSimon Schubert   if (fi->base->unwind == fi->unwind)
21655796c8dcSSimon Schubert     return fi->base->this_base (fi, &fi->prologue_cache);
21665796c8dcSSimon Schubert   return fi->base->this_base (fi, &fi->base_cache);
21675796c8dcSSimon Schubert }
21685796c8dcSSimon Schubert 
21695796c8dcSSimon Schubert CORE_ADDR
get_frame_locals_address(struct frame_info * fi)21705796c8dcSSimon Schubert get_frame_locals_address (struct frame_info *fi)
21715796c8dcSSimon Schubert {
21725796c8dcSSimon Schubert   if (get_frame_type (fi) != NORMAL_FRAME)
21735796c8dcSSimon Schubert     return 0;
21745796c8dcSSimon Schubert   /* If there isn't a frame address method, find it.  */
21755796c8dcSSimon Schubert   if (fi->base == NULL)
21765796c8dcSSimon Schubert     fi->base = frame_base_find_by_frame (fi);
21775796c8dcSSimon Schubert   /* Sneaky: If the low-level unwind and high-level base code share a
21785796c8dcSSimon Schubert      common unwinder, let them share the prologue cache.  */
21795796c8dcSSimon Schubert   if (fi->base->unwind == fi->unwind)
21805796c8dcSSimon Schubert     return fi->base->this_locals (fi, &fi->prologue_cache);
21815796c8dcSSimon Schubert   return fi->base->this_locals (fi, &fi->base_cache);
21825796c8dcSSimon Schubert }
21835796c8dcSSimon Schubert 
21845796c8dcSSimon Schubert CORE_ADDR
get_frame_args_address(struct frame_info * fi)21855796c8dcSSimon Schubert get_frame_args_address (struct frame_info *fi)
21865796c8dcSSimon Schubert {
21875796c8dcSSimon Schubert   if (get_frame_type (fi) != NORMAL_FRAME)
21885796c8dcSSimon Schubert     return 0;
21895796c8dcSSimon Schubert   /* If there isn't a frame address method, find it.  */
21905796c8dcSSimon Schubert   if (fi->base == NULL)
21915796c8dcSSimon Schubert     fi->base = frame_base_find_by_frame (fi);
21925796c8dcSSimon Schubert   /* Sneaky: If the low-level unwind and high-level base code share a
21935796c8dcSSimon Schubert      common unwinder, let them share the prologue cache.  */
21945796c8dcSSimon Schubert   if (fi->base->unwind == fi->unwind)
21955796c8dcSSimon Schubert     return fi->base->this_args (fi, &fi->prologue_cache);
21965796c8dcSSimon Schubert   return fi->base->this_args (fi, &fi->base_cache);
21975796c8dcSSimon Schubert }
21985796c8dcSSimon Schubert 
21995796c8dcSSimon Schubert /* Return true if the frame unwinder for frame FI is UNWINDER; false
22005796c8dcSSimon Schubert    otherwise.  */
22015796c8dcSSimon Schubert 
22025796c8dcSSimon Schubert int
frame_unwinder_is(struct frame_info * fi,const struct frame_unwind * unwinder)22035796c8dcSSimon Schubert frame_unwinder_is (struct frame_info *fi, const struct frame_unwind *unwinder)
22045796c8dcSSimon Schubert {
22055796c8dcSSimon Schubert   if (fi->unwind == NULL)
2206c50c785cSJohn Marino     frame_unwind_find_by_frame (fi, &fi->prologue_cache);
22075796c8dcSSimon Schubert   return fi->unwind == unwinder;
22085796c8dcSSimon Schubert }
22095796c8dcSSimon Schubert 
22105796c8dcSSimon Schubert /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
22115796c8dcSSimon Schubert    or -1 for a NULL frame.  */
22125796c8dcSSimon Schubert 
22135796c8dcSSimon Schubert int
frame_relative_level(struct frame_info * fi)22145796c8dcSSimon Schubert frame_relative_level (struct frame_info *fi)
22155796c8dcSSimon Schubert {
22165796c8dcSSimon Schubert   if (fi == NULL)
22175796c8dcSSimon Schubert     return -1;
22185796c8dcSSimon Schubert   else
22195796c8dcSSimon Schubert     return fi->level;
22205796c8dcSSimon Schubert }
22215796c8dcSSimon Schubert 
22225796c8dcSSimon Schubert enum frame_type
get_frame_type(struct frame_info * frame)22235796c8dcSSimon Schubert get_frame_type (struct frame_info *frame)
22245796c8dcSSimon Schubert {
22255796c8dcSSimon Schubert   if (frame->unwind == NULL)
22265796c8dcSSimon Schubert     /* Initialize the frame's unwinder because that's what
22275796c8dcSSimon Schubert        provides the frame's type.  */
2228c50c785cSJohn Marino     frame_unwind_find_by_frame (frame, &frame->prologue_cache);
22295796c8dcSSimon Schubert   return frame->unwind->type;
22305796c8dcSSimon Schubert }
22315796c8dcSSimon Schubert 
2232cf7f2e2dSJohn Marino struct program_space *
get_frame_program_space(struct frame_info * frame)2233cf7f2e2dSJohn Marino get_frame_program_space (struct frame_info *frame)
2234cf7f2e2dSJohn Marino {
2235cf7f2e2dSJohn Marino   return frame->pspace;
2236cf7f2e2dSJohn Marino }
2237cf7f2e2dSJohn Marino 
2238cf7f2e2dSJohn Marino struct program_space *
frame_unwind_program_space(struct frame_info * this_frame)2239cf7f2e2dSJohn Marino frame_unwind_program_space (struct frame_info *this_frame)
2240cf7f2e2dSJohn Marino {
2241cf7f2e2dSJohn Marino   gdb_assert (this_frame);
2242cf7f2e2dSJohn Marino 
2243cf7f2e2dSJohn Marino   /* This is really a placeholder to keep the API consistent --- we
2244cf7f2e2dSJohn Marino      assume for now that we don't have frame chains crossing
2245cf7f2e2dSJohn Marino      spaces.  */
2246cf7f2e2dSJohn Marino   return this_frame->pspace;
2247cf7f2e2dSJohn Marino }
2248cf7f2e2dSJohn Marino 
2249cf7f2e2dSJohn Marino struct address_space *
get_frame_address_space(struct frame_info * frame)2250cf7f2e2dSJohn Marino get_frame_address_space (struct frame_info *frame)
2251cf7f2e2dSJohn Marino {
2252cf7f2e2dSJohn Marino   return frame->aspace;
2253cf7f2e2dSJohn Marino }
2254cf7f2e2dSJohn Marino 
22555796c8dcSSimon Schubert /* Memory access methods.  */
22565796c8dcSSimon Schubert 
22575796c8dcSSimon Schubert void
get_frame_memory(struct frame_info * this_frame,CORE_ADDR addr,gdb_byte * buf,int len)22585796c8dcSSimon Schubert get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
22595796c8dcSSimon Schubert 		  gdb_byte *buf, int len)
22605796c8dcSSimon Schubert {
22615796c8dcSSimon Schubert   read_memory (addr, buf, len);
22625796c8dcSSimon Schubert }
22635796c8dcSSimon Schubert 
22645796c8dcSSimon Schubert LONGEST
get_frame_memory_signed(struct frame_info * this_frame,CORE_ADDR addr,int len)22655796c8dcSSimon Schubert get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
22665796c8dcSSimon Schubert 			 int len)
22675796c8dcSSimon Schubert {
22685796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
22695796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2270cf7f2e2dSJohn Marino 
22715796c8dcSSimon Schubert   return read_memory_integer (addr, len, byte_order);
22725796c8dcSSimon Schubert }
22735796c8dcSSimon Schubert 
22745796c8dcSSimon Schubert ULONGEST
get_frame_memory_unsigned(struct frame_info * this_frame,CORE_ADDR addr,int len)22755796c8dcSSimon Schubert get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
22765796c8dcSSimon Schubert 			   int len)
22775796c8dcSSimon Schubert {
22785796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
22795796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2280cf7f2e2dSJohn Marino 
22815796c8dcSSimon Schubert   return read_memory_unsigned_integer (addr, len, byte_order);
22825796c8dcSSimon Schubert }
22835796c8dcSSimon Schubert 
22845796c8dcSSimon Schubert int
safe_frame_unwind_memory(struct frame_info * this_frame,CORE_ADDR addr,gdb_byte * buf,int len)22855796c8dcSSimon Schubert safe_frame_unwind_memory (struct frame_info *this_frame,
22865796c8dcSSimon Schubert 			  CORE_ADDR addr, gdb_byte *buf, int len)
22875796c8dcSSimon Schubert {
22885796c8dcSSimon Schubert   /* NOTE: target_read_memory returns zero on success!  */
22895796c8dcSSimon Schubert   return !target_read_memory (addr, buf, len);
22905796c8dcSSimon Schubert }
22915796c8dcSSimon Schubert 
22925796c8dcSSimon Schubert /* Architecture methods.  */
22935796c8dcSSimon Schubert 
22945796c8dcSSimon Schubert struct gdbarch *
get_frame_arch(struct frame_info * this_frame)22955796c8dcSSimon Schubert get_frame_arch (struct frame_info *this_frame)
22965796c8dcSSimon Schubert {
22975796c8dcSSimon Schubert   return frame_unwind_arch (this_frame->next);
22985796c8dcSSimon Schubert }
22995796c8dcSSimon Schubert 
23005796c8dcSSimon Schubert struct gdbarch *
frame_unwind_arch(struct frame_info * next_frame)23015796c8dcSSimon Schubert frame_unwind_arch (struct frame_info *next_frame)
23025796c8dcSSimon Schubert {
23035796c8dcSSimon Schubert   if (!next_frame->prev_arch.p)
23045796c8dcSSimon Schubert     {
23055796c8dcSSimon Schubert       struct gdbarch *arch;
23065796c8dcSSimon Schubert 
23075796c8dcSSimon Schubert       if (next_frame->unwind == NULL)
2308c50c785cSJohn Marino 	frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
23095796c8dcSSimon Schubert 
23105796c8dcSSimon Schubert       if (next_frame->unwind->prev_arch != NULL)
23115796c8dcSSimon Schubert 	arch = next_frame->unwind->prev_arch (next_frame,
23125796c8dcSSimon Schubert 					      &next_frame->prologue_cache);
23135796c8dcSSimon Schubert       else
23145796c8dcSSimon Schubert 	arch = get_frame_arch (next_frame);
23155796c8dcSSimon Schubert 
23165796c8dcSSimon Schubert       next_frame->prev_arch.arch = arch;
23175796c8dcSSimon Schubert       next_frame->prev_arch.p = 1;
23185796c8dcSSimon Schubert       if (frame_debug)
23195796c8dcSSimon Schubert 	fprintf_unfiltered (gdb_stdlog,
23205796c8dcSSimon Schubert 			    "{ frame_unwind_arch (next_frame=%d) -> %s }\n",
23215796c8dcSSimon Schubert 			    next_frame->level,
23225796c8dcSSimon Schubert 			    gdbarch_bfd_arch_info (arch)->printable_name);
23235796c8dcSSimon Schubert     }
23245796c8dcSSimon Schubert 
23255796c8dcSSimon Schubert   return next_frame->prev_arch.arch;
23265796c8dcSSimon Schubert }
23275796c8dcSSimon Schubert 
23285796c8dcSSimon Schubert struct gdbarch *
frame_unwind_caller_arch(struct frame_info * next_frame)23295796c8dcSSimon Schubert frame_unwind_caller_arch (struct frame_info *next_frame)
23305796c8dcSSimon Schubert {
2331*ef5ccd6cSJohn Marino   return frame_unwind_arch (skip_artificial_frames (next_frame));
23325796c8dcSSimon Schubert }
23335796c8dcSSimon Schubert 
23345796c8dcSSimon Schubert /* Stack pointer methods.  */
23355796c8dcSSimon Schubert 
23365796c8dcSSimon Schubert CORE_ADDR
get_frame_sp(struct frame_info * this_frame)23375796c8dcSSimon Schubert get_frame_sp (struct frame_info *this_frame)
23385796c8dcSSimon Schubert {
23395796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2340cf7f2e2dSJohn Marino 
23415796c8dcSSimon Schubert   /* Normality - an architecture that provides a way of obtaining any
23425796c8dcSSimon Schubert      frame inner-most address.  */
23435796c8dcSSimon Schubert   if (gdbarch_unwind_sp_p (gdbarch))
23445796c8dcSSimon Schubert     /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
23455796c8dcSSimon Schubert        operate on THIS_FRAME now.  */
23465796c8dcSSimon Schubert     return gdbarch_unwind_sp (gdbarch, this_frame->next);
23475796c8dcSSimon Schubert   /* Now things are really are grim.  Hope that the value returned by
23485796c8dcSSimon Schubert      the gdbarch_sp_regnum register is meaningful.  */
23495796c8dcSSimon Schubert   if (gdbarch_sp_regnum (gdbarch) >= 0)
23505796c8dcSSimon Schubert     return get_frame_register_unsigned (this_frame,
23515796c8dcSSimon Schubert 					gdbarch_sp_regnum (gdbarch));
23525796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__, _("Missing unwind SP method"));
23535796c8dcSSimon Schubert }
23545796c8dcSSimon Schubert 
23555796c8dcSSimon Schubert /* Return the reason why we can't unwind past FRAME.  */
23565796c8dcSSimon Schubert 
23575796c8dcSSimon Schubert enum unwind_stop_reason
get_frame_unwind_stop_reason(struct frame_info * frame)23585796c8dcSSimon Schubert get_frame_unwind_stop_reason (struct frame_info *frame)
23595796c8dcSSimon Schubert {
23605796c8dcSSimon Schubert   /* If we haven't tried to unwind past this point yet, then assume
23615796c8dcSSimon Schubert      that unwinding would succeed.  */
23625796c8dcSSimon Schubert   if (frame->prev_p == 0)
23635796c8dcSSimon Schubert     return UNWIND_NO_REASON;
23645796c8dcSSimon Schubert 
23655796c8dcSSimon Schubert   /* Otherwise, we set a reason when we succeeded (or failed) to
23665796c8dcSSimon Schubert      unwind.  */
23675796c8dcSSimon Schubert   return frame->stop_reason;
23685796c8dcSSimon Schubert }
23695796c8dcSSimon Schubert 
23705796c8dcSSimon Schubert /* Return a string explaining REASON.  */
23715796c8dcSSimon Schubert 
23725796c8dcSSimon Schubert const char *
frame_stop_reason_string(enum unwind_stop_reason reason)23735796c8dcSSimon Schubert frame_stop_reason_string (enum unwind_stop_reason reason)
23745796c8dcSSimon Schubert {
23755796c8dcSSimon Schubert   switch (reason)
23765796c8dcSSimon Schubert     {
2377a45ae5f8SJohn Marino #define SET(name, description) \
2378a45ae5f8SJohn Marino     case name: return _(description);
2379a45ae5f8SJohn Marino #include "unwind_stop_reasons.def"
2380a45ae5f8SJohn Marino #undef SET
23815796c8dcSSimon Schubert 
23825796c8dcSSimon Schubert     default:
23835796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__,
23845796c8dcSSimon Schubert 		      "Invalid frame stop reason");
23855796c8dcSSimon Schubert     }
23865796c8dcSSimon Schubert }
23875796c8dcSSimon Schubert 
23885796c8dcSSimon Schubert /* Clean up after a failed (wrong unwinder) attempt to unwind past
23895796c8dcSSimon Schubert    FRAME.  */
23905796c8dcSSimon Schubert 
23915796c8dcSSimon Schubert static void
frame_cleanup_after_sniffer(void * arg)23925796c8dcSSimon Schubert frame_cleanup_after_sniffer (void *arg)
23935796c8dcSSimon Schubert {
23945796c8dcSSimon Schubert   struct frame_info *frame = arg;
23955796c8dcSSimon Schubert 
23965796c8dcSSimon Schubert   /* The sniffer should not allocate a prologue cache if it did not
23975796c8dcSSimon Schubert      match this frame.  */
23985796c8dcSSimon Schubert   gdb_assert (frame->prologue_cache == NULL);
23995796c8dcSSimon Schubert 
24005796c8dcSSimon Schubert   /* No sniffer should extend the frame chain; sniff based on what is
24015796c8dcSSimon Schubert      already certain.  */
24025796c8dcSSimon Schubert   gdb_assert (!frame->prev_p);
24035796c8dcSSimon Schubert 
24045796c8dcSSimon Schubert   /* The sniffer should not check the frame's ID; that's circular.  */
24055796c8dcSSimon Schubert   gdb_assert (!frame->this_id.p);
24065796c8dcSSimon Schubert 
24075796c8dcSSimon Schubert   /* Clear cached fields dependent on the unwinder.
24085796c8dcSSimon Schubert 
24095796c8dcSSimon Schubert      The previous PC is independent of the unwinder, but the previous
24105796c8dcSSimon Schubert      function is not (see get_frame_address_in_block).  */
24115796c8dcSSimon Schubert   frame->prev_func.p = 0;
24125796c8dcSSimon Schubert   frame->prev_func.addr = 0;
24135796c8dcSSimon Schubert 
24145796c8dcSSimon Schubert   /* Discard the unwinder last, so that we can easily find it if an assertion
24155796c8dcSSimon Schubert      in this function triggers.  */
24165796c8dcSSimon Schubert   frame->unwind = NULL;
24175796c8dcSSimon Schubert }
24185796c8dcSSimon Schubert 
24195796c8dcSSimon Schubert /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
24205796c8dcSSimon Schubert    Return a cleanup which should be called if unwinding fails, and
24215796c8dcSSimon Schubert    discarded if it succeeds.  */
24225796c8dcSSimon Schubert 
24235796c8dcSSimon Schubert struct cleanup *
frame_prepare_for_sniffer(struct frame_info * frame,const struct frame_unwind * unwind)24245796c8dcSSimon Schubert frame_prepare_for_sniffer (struct frame_info *frame,
24255796c8dcSSimon Schubert 			   const struct frame_unwind *unwind)
24265796c8dcSSimon Schubert {
24275796c8dcSSimon Schubert   gdb_assert (frame->unwind == NULL);
24285796c8dcSSimon Schubert   frame->unwind = unwind;
24295796c8dcSSimon Schubert   return make_cleanup (frame_cleanup_after_sniffer, frame);
24305796c8dcSSimon Schubert }
24315796c8dcSSimon Schubert 
24325796c8dcSSimon Schubert extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */
24335796c8dcSSimon Schubert 
24345796c8dcSSimon Schubert static struct cmd_list_element *set_backtrace_cmdlist;
24355796c8dcSSimon Schubert static struct cmd_list_element *show_backtrace_cmdlist;
24365796c8dcSSimon Schubert 
24375796c8dcSSimon Schubert static void
set_backtrace_cmd(char * args,int from_tty)24385796c8dcSSimon Schubert set_backtrace_cmd (char *args, int from_tty)
24395796c8dcSSimon Schubert {
24405796c8dcSSimon Schubert   help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout);
24415796c8dcSSimon Schubert }
24425796c8dcSSimon Schubert 
24435796c8dcSSimon Schubert static void
show_backtrace_cmd(char * args,int from_tty)24445796c8dcSSimon Schubert show_backtrace_cmd (char *args, int from_tty)
24455796c8dcSSimon Schubert {
24465796c8dcSSimon Schubert   cmd_show_list (show_backtrace_cmdlist, from_tty, "");
24475796c8dcSSimon Schubert }
24485796c8dcSSimon Schubert 
24495796c8dcSSimon Schubert void
_initialize_frame(void)24505796c8dcSSimon Schubert _initialize_frame (void)
24515796c8dcSSimon Schubert {
24525796c8dcSSimon Schubert   obstack_init (&frame_cache_obstack);
24535796c8dcSSimon Schubert 
24545796c8dcSSimon Schubert   observer_attach_target_changed (frame_observer_target_changed);
24555796c8dcSSimon Schubert 
24565796c8dcSSimon Schubert   add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, _("\
24575796c8dcSSimon Schubert Set backtrace specific variables.\n\
24585796c8dcSSimon Schubert Configure backtrace variables such as the backtrace limit"),
24595796c8dcSSimon Schubert 		  &set_backtrace_cmdlist, "set backtrace ",
24605796c8dcSSimon Schubert 		  0/*allow-unknown*/, &setlist);
24615796c8dcSSimon Schubert   add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, _("\
24625796c8dcSSimon Schubert Show backtrace specific variables\n\
24635796c8dcSSimon Schubert Show backtrace variables such as the backtrace limit"),
24645796c8dcSSimon Schubert 		  &show_backtrace_cmdlist, "show backtrace ",
24655796c8dcSSimon Schubert 		  0/*allow-unknown*/, &showlist);
24665796c8dcSSimon Schubert 
24675796c8dcSSimon Schubert   add_setshow_boolean_cmd ("past-main", class_obscure,
24685796c8dcSSimon Schubert 			   &backtrace_past_main, _("\
24695796c8dcSSimon Schubert Set whether backtraces should continue past \"main\"."), _("\
24705796c8dcSSimon Schubert Show whether backtraces should continue past \"main\"."), _("\
24715796c8dcSSimon Schubert Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
24725796c8dcSSimon Schubert the backtrace at \"main\".  Set this variable if you need to see the rest\n\
24735796c8dcSSimon Schubert of the stack trace."),
24745796c8dcSSimon Schubert 			   NULL,
24755796c8dcSSimon Schubert 			   show_backtrace_past_main,
24765796c8dcSSimon Schubert 			   &set_backtrace_cmdlist,
24775796c8dcSSimon Schubert 			   &show_backtrace_cmdlist);
24785796c8dcSSimon Schubert 
24795796c8dcSSimon Schubert   add_setshow_boolean_cmd ("past-entry", class_obscure,
24805796c8dcSSimon Schubert 			   &backtrace_past_entry, _("\
24815796c8dcSSimon Schubert Set whether backtraces should continue past the entry point of a program."),
24825796c8dcSSimon Schubert 			   _("\
24835796c8dcSSimon Schubert Show whether backtraces should continue past the entry point of a program."),
24845796c8dcSSimon Schubert 			   _("\
24855796c8dcSSimon Schubert Normally there are no callers beyond the entry point of a program, so GDB\n\
24865796c8dcSSimon Schubert will terminate the backtrace there.  Set this variable if you need to see\n\
24875796c8dcSSimon Schubert the rest of the stack trace."),
24885796c8dcSSimon Schubert 			   NULL,
24895796c8dcSSimon Schubert 			   show_backtrace_past_entry,
24905796c8dcSSimon Schubert 			   &set_backtrace_cmdlist,
24915796c8dcSSimon Schubert 			   &show_backtrace_cmdlist);
24925796c8dcSSimon Schubert 
2493*ef5ccd6cSJohn Marino   add_setshow_uinteger_cmd ("limit", class_obscure,
24945796c8dcSSimon Schubert 			    &backtrace_limit, _("\
24955796c8dcSSimon Schubert Set an upper bound on the number of backtrace levels."), _("\
24965796c8dcSSimon Schubert Show the upper bound on the number of backtrace levels."), _("\
24975796c8dcSSimon Schubert No more than the specified number of frames can be displayed or examined.\n\
24985796c8dcSSimon Schubert Zero is unlimited."),
24995796c8dcSSimon Schubert 			    NULL,
25005796c8dcSSimon Schubert 			    show_backtrace_limit,
25015796c8dcSSimon Schubert 			    &set_backtrace_cmdlist,
25025796c8dcSSimon Schubert 			    &show_backtrace_cmdlist);
25035796c8dcSSimon Schubert 
25045796c8dcSSimon Schubert   /* Debug this files internals.  */
2505*ef5ccd6cSJohn Marino   add_setshow_zuinteger_cmd ("frame", class_maintenance, &frame_debug,  _("\
25065796c8dcSSimon Schubert Set frame debugging."), _("\
25075796c8dcSSimon Schubert Show frame debugging."), _("\
25085796c8dcSSimon Schubert When non-zero, frame specific internal debugging is enabled."),
25095796c8dcSSimon Schubert 			     NULL,
25105796c8dcSSimon Schubert 			     show_frame_debug,
25115796c8dcSSimon Schubert 			     &setdebuglist, &showdebuglist);
25125796c8dcSSimon Schubert }
2513