xref: /dflybsd-src/contrib/gdb-7/gdb/dummy-frame.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Code dealing with dummy stack frames, for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4*cf7f2e2dSJohn Marino    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009,
5*cf7f2e2dSJohn Marino    2010 Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert #include "defs.h"
245796c8dcSSimon Schubert #include "dummy-frame.h"
255796c8dcSSimon Schubert #include "regcache.h"
265796c8dcSSimon Schubert #include "frame.h"
275796c8dcSSimon Schubert #include "inferior.h"
285796c8dcSSimon Schubert #include "gdb_assert.h"
295796c8dcSSimon Schubert #include "frame-unwind.h"
305796c8dcSSimon Schubert #include "command.h"
315796c8dcSSimon Schubert #include "gdbcmd.h"
325796c8dcSSimon Schubert #include "gdb_string.h"
335796c8dcSSimon Schubert #include "observer.h"
345796c8dcSSimon Schubert 
355796c8dcSSimon Schubert /* Dummy frame.  This saves the processor state just prior to setting
365796c8dcSSimon Schubert    up the inferior function call.  Older targets save the registers
375796c8dcSSimon Schubert    on the target stack (but that really slows down function calls).  */
385796c8dcSSimon Schubert 
395796c8dcSSimon Schubert struct dummy_frame
405796c8dcSSimon Schubert {
415796c8dcSSimon Schubert   struct dummy_frame *next;
425796c8dcSSimon Schubert   /* This frame's ID.  Must match the value returned by
435796c8dcSSimon Schubert      gdbarch_dummy_id.  */
445796c8dcSSimon Schubert   struct frame_id id;
455796c8dcSSimon Schubert   /* The caller's state prior to the call.  */
465796c8dcSSimon Schubert   struct inferior_thread_state *caller_state;
475796c8dcSSimon Schubert };
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert static struct dummy_frame *dummy_frame_stack = NULL;
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert /* Function: deprecated_pc_in_call_dummy (pc)
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert    Return non-zero if the PC falls in a dummy frame created by gdb for
545796c8dcSSimon Schubert    an inferior call.  The code below which allows gdbarch_decr_pc_after_break
555796c8dcSSimon Schubert    is for infrun.c, which may give the function a PC without that
565796c8dcSSimon Schubert    subtracted out.
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert    FIXME: cagney/2002-11-23: This is silly.  Surely "infrun.c" can
595796c8dcSSimon Schubert    figure out what the real PC (as in the resume address) is BEFORE
605796c8dcSSimon Schubert    calling this function.
615796c8dcSSimon Schubert 
625796c8dcSSimon Schubert    NOTE: cagney/2004-08-02: I'm pretty sure that, with the introduction of
635796c8dcSSimon Schubert    infrun.c:adjust_pc_after_break (thanks), this function is now
645796c8dcSSimon Schubert    always called with a correctly adjusted PC!
655796c8dcSSimon Schubert 
665796c8dcSSimon Schubert    NOTE: cagney/2004-08-02: Code should not need to call this.  */
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert int
695796c8dcSSimon Schubert deprecated_pc_in_call_dummy (struct gdbarch *gdbarch, CORE_ADDR pc)
705796c8dcSSimon Schubert {
715796c8dcSSimon Schubert   struct dummy_frame *dummyframe;
72*cf7f2e2dSJohn Marino 
735796c8dcSSimon Schubert   for (dummyframe = dummy_frame_stack;
745796c8dcSSimon Schubert        dummyframe != NULL;
755796c8dcSSimon Schubert        dummyframe = dummyframe->next)
765796c8dcSSimon Schubert     {
775796c8dcSSimon Schubert       if ((pc >= dummyframe->id.code_addr)
785796c8dcSSimon Schubert 	  && (pc <= dummyframe->id.code_addr
795796c8dcSSimon Schubert 		    + gdbarch_decr_pc_after_break (gdbarch)))
805796c8dcSSimon Schubert 	return 1;
815796c8dcSSimon Schubert     }
825796c8dcSSimon Schubert   return 0;
835796c8dcSSimon Schubert }
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert /* Push the caller's state, along with the dummy frame info, onto the
865796c8dcSSimon Schubert    dummy-frame stack.  */
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert void
895796c8dcSSimon Schubert dummy_frame_push (struct inferior_thread_state *caller_state,
905796c8dcSSimon Schubert 		  const struct frame_id *dummy_id)
915796c8dcSSimon Schubert {
925796c8dcSSimon Schubert   struct dummy_frame *dummy_frame;
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert   dummy_frame = XZALLOC (struct dummy_frame);
955796c8dcSSimon Schubert   dummy_frame->caller_state = caller_state;
965796c8dcSSimon Schubert   dummy_frame->id = (*dummy_id);
975796c8dcSSimon Schubert   dummy_frame->next = dummy_frame_stack;
985796c8dcSSimon Schubert   dummy_frame_stack = dummy_frame;
995796c8dcSSimon Schubert }
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert /* Remove *DUMMY_PTR from the dummy frame stack.  */
1025796c8dcSSimon Schubert 
1035796c8dcSSimon Schubert static void
1045796c8dcSSimon Schubert remove_dummy_frame (struct dummy_frame **dummy_ptr)
1055796c8dcSSimon Schubert {
1065796c8dcSSimon Schubert   struct dummy_frame *dummy = *dummy_ptr;
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert   *dummy_ptr = dummy->next;
1095796c8dcSSimon Schubert   discard_inferior_thread_state (dummy->caller_state);
1105796c8dcSSimon Schubert   xfree (dummy);
1115796c8dcSSimon Schubert }
1125796c8dcSSimon Schubert 
1135796c8dcSSimon Schubert /* Pop *DUMMY_PTR, restoring program state to that before the
1145796c8dcSSimon Schubert    frame was created.  */
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert static void
1175796c8dcSSimon Schubert pop_dummy_frame (struct dummy_frame **dummy_ptr)
1185796c8dcSSimon Schubert {
1195796c8dcSSimon Schubert   struct dummy_frame *dummy;
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert   restore_inferior_thread_state ((*dummy_ptr)->caller_state);
1225796c8dcSSimon Schubert 
1235796c8dcSSimon Schubert   /* restore_inferior_status frees inf_state,
1245796c8dcSSimon Schubert      all that remains is to pop *dummy_ptr */
1255796c8dcSSimon Schubert   dummy = *dummy_ptr;
1265796c8dcSSimon Schubert   *dummy_ptr = dummy->next;
1275796c8dcSSimon Schubert   xfree (dummy);
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert   /* We've made right mess of GDB's local state, just discard
1305796c8dcSSimon Schubert      everything.  */
1315796c8dcSSimon Schubert   reinit_frame_cache ();
1325796c8dcSSimon Schubert }
1335796c8dcSSimon Schubert 
1345796c8dcSSimon Schubert /* Look up DUMMY_ID.
1355796c8dcSSimon Schubert    Return NULL if not found.  */
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert static struct dummy_frame **
1385796c8dcSSimon Schubert lookup_dummy_frame (struct frame_id dummy_id)
1395796c8dcSSimon Schubert {
1405796c8dcSSimon Schubert   struct dummy_frame **dp;
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert   for (dp = &dummy_frame_stack; *dp != NULL; dp = &(*dp)->next)
1435796c8dcSSimon Schubert     {
1445796c8dcSSimon Schubert       if (frame_id_eq ((*dp)->id, dummy_id))
1455796c8dcSSimon Schubert 	return dp;
1465796c8dcSSimon Schubert     }
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert   return NULL;
1495796c8dcSSimon Schubert }
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert /* Pop the dummy frame DUMMY_ID, restoring program state to that before the
1525796c8dcSSimon Schubert    frame was created.
1535796c8dcSSimon Schubert    On return reinit_frame_cache has been called.
1545796c8dcSSimon Schubert    If the frame isn't found, flag an internal error.
1555796c8dcSSimon Schubert 
1565796c8dcSSimon Schubert    NOTE: This can only pop the one frame, even if it is in the middle of the
1575796c8dcSSimon Schubert    stack, because the other frames may be for different threads, and there's
1585796c8dcSSimon Schubert    currently no way to tell which stack frame is for which thread.  */
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert void
1615796c8dcSSimon Schubert dummy_frame_pop (struct frame_id dummy_id)
1625796c8dcSSimon Schubert {
1635796c8dcSSimon Schubert   struct dummy_frame **dp;
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert   dp = lookup_dummy_frame (dummy_id);
1665796c8dcSSimon Schubert   gdb_assert (dp != NULL);
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert   pop_dummy_frame (dp);
1695796c8dcSSimon Schubert }
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert /* There may be stale dummy frames, perhaps left over from when a longjump took
1725796c8dcSSimon Schubert    us out of a function that was called by the debugger.  Clean them up at
1735796c8dcSSimon Schubert    least once whenever we start a new inferior.  */
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert static void
1765796c8dcSSimon Schubert cleanup_dummy_frames (struct target_ops *target, int from_tty)
1775796c8dcSSimon Schubert {
1785796c8dcSSimon Schubert   while (dummy_frame_stack != NULL)
1795796c8dcSSimon Schubert     remove_dummy_frame (&dummy_frame_stack);
1805796c8dcSSimon Schubert }
1815796c8dcSSimon Schubert 
1825796c8dcSSimon Schubert /* Return the dummy frame cache, it contains both the ID, and a
1835796c8dcSSimon Schubert    pointer to the regcache.  */
1845796c8dcSSimon Schubert struct dummy_frame_cache
1855796c8dcSSimon Schubert {
1865796c8dcSSimon Schubert   struct frame_id this_id;
1875796c8dcSSimon Schubert   struct regcache *prev_regcache;
1885796c8dcSSimon Schubert };
1895796c8dcSSimon Schubert 
1905796c8dcSSimon Schubert static int
1915796c8dcSSimon Schubert dummy_frame_sniffer (const struct frame_unwind *self,
1925796c8dcSSimon Schubert 		     struct frame_info *this_frame,
1935796c8dcSSimon Schubert 		     void **this_prologue_cache)
1945796c8dcSSimon Schubert {
1955796c8dcSSimon Schubert   struct dummy_frame *dummyframe;
1965796c8dcSSimon Schubert   struct frame_id this_id;
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert   /* When unwinding a normal frame, the stack structure is determined
1995796c8dcSSimon Schubert      by analyzing the frame's function's code (be it using brute force
2005796c8dcSSimon Schubert      prologue analysis, or the dwarf2 CFI).  In the case of a dummy
2015796c8dcSSimon Schubert      frame, that simply isn't possible.  The PC is either the program
2025796c8dcSSimon Schubert      entry point, or some random address on the stack.  Trying to use
2035796c8dcSSimon Schubert      that PC to apply standard frame ID unwind techniques is just
2045796c8dcSSimon Schubert      asking for trouble.  */
2055796c8dcSSimon Schubert 
2065796c8dcSSimon Schubert   /* Don't bother unless there is at least one dummy frame.  */
2075796c8dcSSimon Schubert   if (dummy_frame_stack != NULL)
2085796c8dcSSimon Schubert     {
2095796c8dcSSimon Schubert       /* Use an architecture specific method to extract this frame's
2105796c8dcSSimon Schubert 	 dummy ID, assuming it is a dummy frame.  */
2115796c8dcSSimon Schubert       this_id = gdbarch_dummy_id (get_frame_arch (this_frame), this_frame);
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert       /* Use that ID to find the corresponding cache entry.  */
2145796c8dcSSimon Schubert       for (dummyframe = dummy_frame_stack;
2155796c8dcSSimon Schubert 	   dummyframe != NULL;
2165796c8dcSSimon Schubert 	   dummyframe = dummyframe->next)
2175796c8dcSSimon Schubert 	{
2185796c8dcSSimon Schubert 	  if (frame_id_eq (dummyframe->id, this_id))
2195796c8dcSSimon Schubert 	    {
2205796c8dcSSimon Schubert 	      struct dummy_frame_cache *cache;
221*cf7f2e2dSJohn Marino 
2225796c8dcSSimon Schubert 	      cache = FRAME_OBSTACK_ZALLOC (struct dummy_frame_cache);
2235796c8dcSSimon Schubert 	      cache->prev_regcache = get_inferior_thread_state_regcache (dummyframe->caller_state);
2245796c8dcSSimon Schubert 	      cache->this_id = this_id;
2255796c8dcSSimon Schubert 	      (*this_prologue_cache) = cache;
2265796c8dcSSimon Schubert 	      return 1;
2275796c8dcSSimon Schubert 	    }
2285796c8dcSSimon Schubert 	}
2295796c8dcSSimon Schubert     }
2305796c8dcSSimon Schubert   return 0;
2315796c8dcSSimon Schubert }
2325796c8dcSSimon Schubert 
2335796c8dcSSimon Schubert /* Given a call-dummy dummy-frame, return the registers.  Here the
2345796c8dcSSimon Schubert    register value is taken from the local copy of the register buffer.  */
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert static struct value *
2375796c8dcSSimon Schubert dummy_frame_prev_register (struct frame_info *this_frame,
2385796c8dcSSimon Schubert 			   void **this_prologue_cache,
2395796c8dcSSimon Schubert 			   int regnum)
2405796c8dcSSimon Schubert {
2415796c8dcSSimon Schubert   struct dummy_frame_cache *cache = (*this_prologue_cache);
2425796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2435796c8dcSSimon Schubert   struct value *reg_val;
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert   /* The dummy-frame sniffer always fills in the cache.  */
2465796c8dcSSimon Schubert   gdb_assert (cache != NULL);
2475796c8dcSSimon Schubert 
2485796c8dcSSimon Schubert   /* Describe the register's location.  Generic dummy frames always
2495796c8dcSSimon Schubert      have the register value in an ``expression''.  */
2505796c8dcSSimon Schubert   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert   /* Use the regcache_cooked_read() method so that it, on the fly,
2535796c8dcSSimon Schubert      constructs either a raw or pseudo register from the raw
2545796c8dcSSimon Schubert      register cache.  */
2555796c8dcSSimon Schubert   regcache_cooked_read (cache->prev_regcache, regnum,
2565796c8dcSSimon Schubert 			value_contents_writeable (reg_val));
2575796c8dcSSimon Schubert   return reg_val;
2585796c8dcSSimon Schubert }
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert /* Assuming that THIS_FRAME is a dummy, return its ID.  That ID is
2615796c8dcSSimon Schubert    determined by examining the NEXT frame's unwound registers using
2625796c8dcSSimon Schubert    the method dummy_id().  As a side effect, THIS dummy frame's
2635796c8dcSSimon Schubert    dummy cache is located and and saved in THIS_PROLOGUE_CACHE.  */
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert static void
2665796c8dcSSimon Schubert dummy_frame_this_id (struct frame_info *this_frame,
2675796c8dcSSimon Schubert 		     void **this_prologue_cache,
2685796c8dcSSimon Schubert 		     struct frame_id *this_id)
2695796c8dcSSimon Schubert {
2705796c8dcSSimon Schubert   /* The dummy-frame sniffer always fills in the cache.  */
2715796c8dcSSimon Schubert   struct dummy_frame_cache *cache = (*this_prologue_cache);
272*cf7f2e2dSJohn Marino 
2735796c8dcSSimon Schubert   gdb_assert (cache != NULL);
2745796c8dcSSimon Schubert   (*this_id) = cache->this_id;
2755796c8dcSSimon Schubert }
2765796c8dcSSimon Schubert 
2775796c8dcSSimon Schubert static const struct frame_unwind dummy_frame_unwinder =
2785796c8dcSSimon Schubert {
2795796c8dcSSimon Schubert   DUMMY_FRAME,
2805796c8dcSSimon Schubert   dummy_frame_this_id,
2815796c8dcSSimon Schubert   dummy_frame_prev_register,
2825796c8dcSSimon Schubert   NULL,
2835796c8dcSSimon Schubert   dummy_frame_sniffer,
2845796c8dcSSimon Schubert };
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert const struct frame_unwind *const dummy_frame_unwind = {
2875796c8dcSSimon Schubert   &dummy_frame_unwinder
2885796c8dcSSimon Schubert };
2895796c8dcSSimon Schubert 
2905796c8dcSSimon Schubert static void
2915796c8dcSSimon Schubert fprint_dummy_frames (struct ui_file *file)
2925796c8dcSSimon Schubert {
2935796c8dcSSimon Schubert   struct dummy_frame *s;
294*cf7f2e2dSJohn Marino 
2955796c8dcSSimon Schubert   for (s = dummy_frame_stack; s != NULL; s = s->next)
2965796c8dcSSimon Schubert     {
2975796c8dcSSimon Schubert       gdb_print_host_address (s, file);
2985796c8dcSSimon Schubert       fprintf_unfiltered (file, ":");
2995796c8dcSSimon Schubert       fprintf_unfiltered (file, " id=");
3005796c8dcSSimon Schubert       fprint_frame_id (file, s->id);
3015796c8dcSSimon Schubert       fprintf_unfiltered (file, "\n");
3025796c8dcSSimon Schubert     }
3035796c8dcSSimon Schubert }
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert static void
3065796c8dcSSimon Schubert maintenance_print_dummy_frames (char *args, int from_tty)
3075796c8dcSSimon Schubert {
3085796c8dcSSimon Schubert   if (args == NULL)
3095796c8dcSSimon Schubert     fprint_dummy_frames (gdb_stdout);
3105796c8dcSSimon Schubert   else
3115796c8dcSSimon Schubert     {
3125796c8dcSSimon Schubert       struct cleanup *cleanups;
3135796c8dcSSimon Schubert       struct ui_file *file = gdb_fopen (args, "w");
314*cf7f2e2dSJohn Marino 
3155796c8dcSSimon Schubert       if (file == NULL)
3165796c8dcSSimon Schubert 	perror_with_name (_("maintenance print dummy-frames"));
3175796c8dcSSimon Schubert       cleanups = make_cleanup_ui_file_delete (file);
3185796c8dcSSimon Schubert       fprint_dummy_frames (file);
3195796c8dcSSimon Schubert       do_cleanups (cleanups);
3205796c8dcSSimon Schubert     }
3215796c8dcSSimon Schubert }
3225796c8dcSSimon Schubert 
3235796c8dcSSimon Schubert extern void _initialize_dummy_frame (void);
3245796c8dcSSimon Schubert 
3255796c8dcSSimon Schubert void
3265796c8dcSSimon Schubert _initialize_dummy_frame (void)
3275796c8dcSSimon Schubert {
3285796c8dcSSimon Schubert   add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames,
3295796c8dcSSimon Schubert 	   _("Print the contents of the internal dummy-frame stack."),
3305796c8dcSSimon Schubert 	   &maintenanceprintlist);
3315796c8dcSSimon Schubert 
3325796c8dcSSimon Schubert   observer_attach_inferior_created (cleanup_dummy_frames);
3335796c8dcSSimon Schubert }
334