xref: /dragonfly/contrib/gdb-7/gdb/dummy-frame.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Code dealing with dummy stack 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 
215796c8dcSSimon Schubert #include "defs.h"
225796c8dcSSimon Schubert #include "dummy-frame.h"
235796c8dcSSimon Schubert #include "regcache.h"
245796c8dcSSimon Schubert #include "frame.h"
255796c8dcSSimon Schubert #include "inferior.h"
265796c8dcSSimon Schubert #include "gdb_assert.h"
275796c8dcSSimon Schubert #include "frame-unwind.h"
285796c8dcSSimon Schubert #include "command.h"
295796c8dcSSimon Schubert #include "gdbcmd.h"
305796c8dcSSimon Schubert #include "gdb_string.h"
315796c8dcSSimon Schubert #include "observer.h"
32*ef5ccd6cSJohn Marino #include "gdbthread.h"
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert /* Dummy frame.  This saves the processor state just prior to setting
355796c8dcSSimon Schubert    up the inferior function call.  Older targets save the registers
365796c8dcSSimon Schubert    on the target stack (but that really slows down function calls).  */
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert struct dummy_frame
395796c8dcSSimon Schubert {
405796c8dcSSimon Schubert   struct dummy_frame *next;
415796c8dcSSimon Schubert   /* This frame's ID.  Must match the value returned by
425796c8dcSSimon Schubert      gdbarch_dummy_id.  */
435796c8dcSSimon Schubert   struct frame_id id;
445796c8dcSSimon Schubert   /* The caller's state prior to the call.  */
45c50c785cSJohn Marino   struct infcall_suspend_state *caller_state;
465796c8dcSSimon Schubert };
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert static struct dummy_frame *dummy_frame_stack = NULL;
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert /* Push the caller's state, along with the dummy frame info, onto the
515796c8dcSSimon Schubert    dummy-frame stack.  */
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert void
dummy_frame_push(struct infcall_suspend_state * caller_state,const struct frame_id * dummy_id)54c50c785cSJohn Marino dummy_frame_push (struct infcall_suspend_state *caller_state,
555796c8dcSSimon Schubert 		  const struct frame_id *dummy_id)
565796c8dcSSimon Schubert {
575796c8dcSSimon Schubert   struct dummy_frame *dummy_frame;
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert   dummy_frame = XZALLOC (struct dummy_frame);
605796c8dcSSimon Schubert   dummy_frame->caller_state = caller_state;
615796c8dcSSimon Schubert   dummy_frame->id = (*dummy_id);
625796c8dcSSimon Schubert   dummy_frame->next = dummy_frame_stack;
635796c8dcSSimon Schubert   dummy_frame_stack = dummy_frame;
645796c8dcSSimon Schubert }
655796c8dcSSimon Schubert 
665796c8dcSSimon Schubert /* Remove *DUMMY_PTR from the dummy frame stack.  */
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert static void
remove_dummy_frame(struct dummy_frame ** dummy_ptr)695796c8dcSSimon Schubert remove_dummy_frame (struct dummy_frame **dummy_ptr)
705796c8dcSSimon Schubert {
715796c8dcSSimon Schubert   struct dummy_frame *dummy = *dummy_ptr;
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert   *dummy_ptr = dummy->next;
74c50c785cSJohn Marino   discard_infcall_suspend_state (dummy->caller_state);
755796c8dcSSimon Schubert   xfree (dummy);
765796c8dcSSimon Schubert }
775796c8dcSSimon Schubert 
78*ef5ccd6cSJohn Marino /* Delete any breakpoint B which is a momentary breakpoint for return from
79*ef5ccd6cSJohn Marino    inferior call matching DUMMY_VOIDP.  */
80*ef5ccd6cSJohn Marino 
81*ef5ccd6cSJohn Marino static int
pop_dummy_frame_bpt(struct breakpoint * b,void * dummy_voidp)82*ef5ccd6cSJohn Marino pop_dummy_frame_bpt (struct breakpoint *b, void *dummy_voidp)
83*ef5ccd6cSJohn Marino {
84*ef5ccd6cSJohn Marino   struct dummy_frame *dummy = dummy_voidp;
85*ef5ccd6cSJohn Marino 
86*ef5ccd6cSJohn Marino   if (b->thread == pid_to_thread_id (inferior_ptid)
87*ef5ccd6cSJohn Marino       && b->disposition == disp_del && frame_id_eq (b->frame_id, dummy->id))
88*ef5ccd6cSJohn Marino     {
89*ef5ccd6cSJohn Marino       while (b->related_breakpoint != b)
90*ef5ccd6cSJohn Marino 	delete_breakpoint (b->related_breakpoint);
91*ef5ccd6cSJohn Marino 
92*ef5ccd6cSJohn Marino       delete_breakpoint (b);
93*ef5ccd6cSJohn Marino 
94*ef5ccd6cSJohn Marino       /* Stop the traversal.  */
95*ef5ccd6cSJohn Marino       return 1;
96*ef5ccd6cSJohn Marino     }
97*ef5ccd6cSJohn Marino 
98*ef5ccd6cSJohn Marino   /* Continue the traversal.  */
99*ef5ccd6cSJohn Marino   return 0;
100*ef5ccd6cSJohn Marino }
101*ef5ccd6cSJohn Marino 
1025796c8dcSSimon Schubert /* Pop *DUMMY_PTR, restoring program state to that before the
1035796c8dcSSimon Schubert    frame was created.  */
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert static void
pop_dummy_frame(struct dummy_frame ** dummy_ptr)1065796c8dcSSimon Schubert pop_dummy_frame (struct dummy_frame **dummy_ptr)
1075796c8dcSSimon Schubert {
108*ef5ccd6cSJohn Marino   struct dummy_frame *dummy = *dummy_ptr;
1095796c8dcSSimon Schubert 
110*ef5ccd6cSJohn Marino   restore_infcall_suspend_state (dummy->caller_state);
111*ef5ccd6cSJohn Marino 
112*ef5ccd6cSJohn Marino   iterate_over_breakpoints (pop_dummy_frame_bpt, dummy);
1135796c8dcSSimon Schubert 
114c50c785cSJohn Marino   /* restore_infcall_control_state frees inf_state,
115c50c785cSJohn Marino      all that remains is to pop *dummy_ptr.  */
1165796c8dcSSimon Schubert   *dummy_ptr = dummy->next;
1175796c8dcSSimon Schubert   xfree (dummy);
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert   /* We've made right mess of GDB's local state, just discard
1205796c8dcSSimon Schubert      everything.  */
1215796c8dcSSimon Schubert   reinit_frame_cache ();
1225796c8dcSSimon Schubert }
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert /* Look up DUMMY_ID.
1255796c8dcSSimon Schubert    Return NULL if not found.  */
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert static struct dummy_frame **
lookup_dummy_frame(struct frame_id dummy_id)1285796c8dcSSimon Schubert lookup_dummy_frame (struct frame_id dummy_id)
1295796c8dcSSimon Schubert {
1305796c8dcSSimon Schubert   struct dummy_frame **dp;
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert   for (dp = &dummy_frame_stack; *dp != NULL; dp = &(*dp)->next)
1335796c8dcSSimon Schubert     {
1345796c8dcSSimon Schubert       if (frame_id_eq ((*dp)->id, dummy_id))
1355796c8dcSSimon Schubert 	return dp;
1365796c8dcSSimon Schubert     }
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert   return NULL;
1395796c8dcSSimon Schubert }
1405796c8dcSSimon Schubert 
1415796c8dcSSimon Schubert /* Pop the dummy frame DUMMY_ID, restoring program state to that before the
1425796c8dcSSimon Schubert    frame was created.
1435796c8dcSSimon Schubert    On return reinit_frame_cache has been called.
1445796c8dcSSimon Schubert    If the frame isn't found, flag an internal error.
1455796c8dcSSimon Schubert 
1465796c8dcSSimon Schubert    NOTE: This can only pop the one frame, even if it is in the middle of the
1475796c8dcSSimon Schubert    stack, because the other frames may be for different threads, and there's
1485796c8dcSSimon Schubert    currently no way to tell which stack frame is for which thread.  */
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert void
dummy_frame_pop(struct frame_id dummy_id)1515796c8dcSSimon Schubert dummy_frame_pop (struct frame_id dummy_id)
1525796c8dcSSimon Schubert {
1535796c8dcSSimon Schubert   struct dummy_frame **dp;
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert   dp = lookup_dummy_frame (dummy_id);
1565796c8dcSSimon Schubert   gdb_assert (dp != NULL);
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert   pop_dummy_frame (dp);
1595796c8dcSSimon Schubert }
1605796c8dcSSimon Schubert 
161*ef5ccd6cSJohn Marino /* Drop dummy frame DUMMY_ID.  Do nothing if it is not found.  Do not restore
162*ef5ccd6cSJohn Marino    its state into inferior, just free its memory.  */
163*ef5ccd6cSJohn Marino 
164*ef5ccd6cSJohn Marino void
dummy_frame_discard(struct frame_id dummy_id)165*ef5ccd6cSJohn Marino dummy_frame_discard (struct frame_id dummy_id)
166*ef5ccd6cSJohn Marino {
167*ef5ccd6cSJohn Marino   struct dummy_frame **dp;
168*ef5ccd6cSJohn Marino 
169*ef5ccd6cSJohn Marino   dp = lookup_dummy_frame (dummy_id);
170*ef5ccd6cSJohn Marino   if (dp)
171*ef5ccd6cSJohn Marino     remove_dummy_frame (dp);
172*ef5ccd6cSJohn Marino }
173*ef5ccd6cSJohn Marino 
174*ef5ccd6cSJohn Marino /* There may be stale dummy frames, perhaps left over from when an uncaught
175*ef5ccd6cSJohn Marino    longjmp took us out of a function that was called by the debugger.  Clean
176*ef5ccd6cSJohn Marino    them up at least once whenever we start a new inferior.  */
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert static void
cleanup_dummy_frames(struct target_ops * target,int from_tty)1795796c8dcSSimon Schubert cleanup_dummy_frames (struct target_ops *target, int from_tty)
1805796c8dcSSimon Schubert {
1815796c8dcSSimon Schubert   while (dummy_frame_stack != NULL)
1825796c8dcSSimon Schubert     remove_dummy_frame (&dummy_frame_stack);
1835796c8dcSSimon Schubert }
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert /* Return the dummy frame cache, it contains both the ID, and a
1865796c8dcSSimon Schubert    pointer to the regcache.  */
1875796c8dcSSimon Schubert struct dummy_frame_cache
1885796c8dcSSimon Schubert {
1895796c8dcSSimon Schubert   struct frame_id this_id;
1905796c8dcSSimon Schubert   struct regcache *prev_regcache;
1915796c8dcSSimon Schubert };
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert static int
dummy_frame_sniffer(const struct frame_unwind * self,struct frame_info * this_frame,void ** this_prologue_cache)1945796c8dcSSimon Schubert dummy_frame_sniffer (const struct frame_unwind *self,
1955796c8dcSSimon Schubert 		     struct frame_info *this_frame,
1965796c8dcSSimon Schubert 		     void **this_prologue_cache)
1975796c8dcSSimon Schubert {
1985796c8dcSSimon Schubert   struct dummy_frame *dummyframe;
1995796c8dcSSimon Schubert   struct frame_id this_id;
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert   /* When unwinding a normal frame, the stack structure is determined
2025796c8dcSSimon Schubert      by analyzing the frame's function's code (be it using brute force
2035796c8dcSSimon Schubert      prologue analysis, or the dwarf2 CFI).  In the case of a dummy
2045796c8dcSSimon Schubert      frame, that simply isn't possible.  The PC is either the program
2055796c8dcSSimon Schubert      entry point, or some random address on the stack.  Trying to use
2065796c8dcSSimon Schubert      that PC to apply standard frame ID unwind techniques is just
2075796c8dcSSimon Schubert      asking for trouble.  */
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert   /* Don't bother unless there is at least one dummy frame.  */
2105796c8dcSSimon Schubert   if (dummy_frame_stack != NULL)
2115796c8dcSSimon Schubert     {
2125796c8dcSSimon Schubert       /* Use an architecture specific method to extract this frame's
2135796c8dcSSimon Schubert 	 dummy ID, assuming it is a dummy frame.  */
2145796c8dcSSimon Schubert       this_id = gdbarch_dummy_id (get_frame_arch (this_frame), this_frame);
2155796c8dcSSimon Schubert 
2165796c8dcSSimon Schubert       /* Use that ID to find the corresponding cache entry.  */
2175796c8dcSSimon Schubert       for (dummyframe = dummy_frame_stack;
2185796c8dcSSimon Schubert 	   dummyframe != NULL;
2195796c8dcSSimon Schubert 	   dummyframe = dummyframe->next)
2205796c8dcSSimon Schubert 	{
2215796c8dcSSimon Schubert 	  if (frame_id_eq (dummyframe->id, this_id))
2225796c8dcSSimon Schubert 	    {
2235796c8dcSSimon Schubert 	      struct dummy_frame_cache *cache;
224cf7f2e2dSJohn Marino 
2255796c8dcSSimon Schubert 	      cache = FRAME_OBSTACK_ZALLOC (struct dummy_frame_cache);
226c50c785cSJohn Marino 	      cache->prev_regcache = get_infcall_suspend_state_regcache
227c50c785cSJohn Marino 						   (dummyframe->caller_state);
2285796c8dcSSimon Schubert 	      cache->this_id = this_id;
2295796c8dcSSimon Schubert 	      (*this_prologue_cache) = cache;
2305796c8dcSSimon Schubert 	      return 1;
2315796c8dcSSimon Schubert 	    }
2325796c8dcSSimon Schubert 	}
2335796c8dcSSimon Schubert     }
2345796c8dcSSimon Schubert   return 0;
2355796c8dcSSimon Schubert }
2365796c8dcSSimon Schubert 
2375796c8dcSSimon Schubert /* Given a call-dummy dummy-frame, return the registers.  Here the
2385796c8dcSSimon Schubert    register value is taken from the local copy of the register buffer.  */
2395796c8dcSSimon Schubert 
2405796c8dcSSimon Schubert static struct value *
dummy_frame_prev_register(struct frame_info * this_frame,void ** this_prologue_cache,int regnum)2415796c8dcSSimon Schubert dummy_frame_prev_register (struct frame_info *this_frame,
2425796c8dcSSimon Schubert 			   void **this_prologue_cache,
2435796c8dcSSimon Schubert 			   int regnum)
2445796c8dcSSimon Schubert {
2455796c8dcSSimon Schubert   struct dummy_frame_cache *cache = (*this_prologue_cache);
2465796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2475796c8dcSSimon Schubert   struct value *reg_val;
2485796c8dcSSimon Schubert 
2495796c8dcSSimon Schubert   /* The dummy-frame sniffer always fills in the cache.  */
2505796c8dcSSimon Schubert   gdb_assert (cache != NULL);
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert   /* Describe the register's location.  Generic dummy frames always
2535796c8dcSSimon Schubert      have the register value in an ``expression''.  */
2545796c8dcSSimon Schubert   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert   /* Use the regcache_cooked_read() method so that it, on the fly,
2575796c8dcSSimon Schubert      constructs either a raw or pseudo register from the raw
2585796c8dcSSimon Schubert      register cache.  */
2595796c8dcSSimon Schubert   regcache_cooked_read (cache->prev_regcache, regnum,
2605796c8dcSSimon Schubert 			value_contents_writeable (reg_val));
2615796c8dcSSimon Schubert   return reg_val;
2625796c8dcSSimon Schubert }
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert /* Assuming that THIS_FRAME is a dummy, return its ID.  That ID is
2655796c8dcSSimon Schubert    determined by examining the NEXT frame's unwound registers using
2665796c8dcSSimon Schubert    the method dummy_id().  As a side effect, THIS dummy frame's
267a45ae5f8SJohn Marino    dummy cache is located and saved in THIS_PROLOGUE_CACHE.  */
2685796c8dcSSimon Schubert 
2695796c8dcSSimon Schubert static void
dummy_frame_this_id(struct frame_info * this_frame,void ** this_prologue_cache,struct frame_id * this_id)2705796c8dcSSimon Schubert dummy_frame_this_id (struct frame_info *this_frame,
2715796c8dcSSimon Schubert 		     void **this_prologue_cache,
2725796c8dcSSimon Schubert 		     struct frame_id *this_id)
2735796c8dcSSimon Schubert {
2745796c8dcSSimon Schubert   /* The dummy-frame sniffer always fills in the cache.  */
2755796c8dcSSimon Schubert   struct dummy_frame_cache *cache = (*this_prologue_cache);
276cf7f2e2dSJohn Marino 
2775796c8dcSSimon Schubert   gdb_assert (cache != NULL);
2785796c8dcSSimon Schubert   (*this_id) = cache->this_id;
2795796c8dcSSimon Schubert }
2805796c8dcSSimon Schubert 
281c50c785cSJohn Marino const struct frame_unwind dummy_frame_unwind =
2825796c8dcSSimon Schubert {
2835796c8dcSSimon Schubert   DUMMY_FRAME,
284c50c785cSJohn Marino   default_frame_unwind_stop_reason,
2855796c8dcSSimon Schubert   dummy_frame_this_id,
2865796c8dcSSimon Schubert   dummy_frame_prev_register,
2875796c8dcSSimon Schubert   NULL,
2885796c8dcSSimon Schubert   dummy_frame_sniffer,
2895796c8dcSSimon Schubert };
2905796c8dcSSimon Schubert 
2915796c8dcSSimon Schubert static void
fprint_dummy_frames(struct ui_file * file)2925796c8dcSSimon Schubert fprint_dummy_frames (struct ui_file *file)
2935796c8dcSSimon Schubert {
2945796c8dcSSimon Schubert   struct dummy_frame *s;
295cf7f2e2dSJohn Marino 
2965796c8dcSSimon Schubert   for (s = dummy_frame_stack; s != NULL; s = s->next)
2975796c8dcSSimon Schubert     {
2985796c8dcSSimon Schubert       gdb_print_host_address (s, file);
2995796c8dcSSimon Schubert       fprintf_unfiltered (file, ":");
3005796c8dcSSimon Schubert       fprintf_unfiltered (file, " id=");
3015796c8dcSSimon Schubert       fprint_frame_id (file, s->id);
3025796c8dcSSimon Schubert       fprintf_unfiltered (file, "\n");
3035796c8dcSSimon Schubert     }
3045796c8dcSSimon Schubert }
3055796c8dcSSimon Schubert 
3065796c8dcSSimon Schubert static void
maintenance_print_dummy_frames(char * args,int from_tty)3075796c8dcSSimon Schubert maintenance_print_dummy_frames (char *args, int from_tty)
3085796c8dcSSimon Schubert {
3095796c8dcSSimon Schubert   if (args == NULL)
3105796c8dcSSimon Schubert     fprint_dummy_frames (gdb_stdout);
3115796c8dcSSimon Schubert   else
3125796c8dcSSimon Schubert     {
3135796c8dcSSimon Schubert       struct cleanup *cleanups;
3145796c8dcSSimon Schubert       struct ui_file *file = gdb_fopen (args, "w");
315cf7f2e2dSJohn Marino 
3165796c8dcSSimon Schubert       if (file == NULL)
3175796c8dcSSimon Schubert 	perror_with_name (_("maintenance print dummy-frames"));
3185796c8dcSSimon Schubert       cleanups = make_cleanup_ui_file_delete (file);
3195796c8dcSSimon Schubert       fprint_dummy_frames (file);
3205796c8dcSSimon Schubert       do_cleanups (cleanups);
3215796c8dcSSimon Schubert     }
3225796c8dcSSimon Schubert }
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert extern void _initialize_dummy_frame (void);
3255796c8dcSSimon Schubert 
3265796c8dcSSimon Schubert void
_initialize_dummy_frame(void)3275796c8dcSSimon Schubert _initialize_dummy_frame (void)
3285796c8dcSSimon Schubert {
3295796c8dcSSimon Schubert   add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames,
3305796c8dcSSimon Schubert 	   _("Print the contents of the internal dummy-frame stack."),
3315796c8dcSSimon Schubert 	   &maintenanceprintlist);
3325796c8dcSSimon Schubert 
3335796c8dcSSimon Schubert   observer_attach_inferior_created (cleanup_dummy_frames);
3345796c8dcSSimon Schubert }
335