Home | History | Annotate | Line # | Download | only in gdb
dummy-frame.c revision 1.1
      1  1.1  christos /* Code dealing with dummy stack frames, for GDB, the GNU debugger.
      2  1.1  christos 
      3  1.1  christos    Copyright (C) 1986-2014 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos 
     21  1.1  christos #include "defs.h"
     22  1.1  christos #include "dummy-frame.h"
     23  1.1  christos #include "regcache.h"
     24  1.1  christos #include "frame.h"
     25  1.1  christos #include "inferior.h"
     26  1.1  christos #include "gdb_assert.h"
     27  1.1  christos #include "frame-unwind.h"
     28  1.1  christos #include "command.h"
     29  1.1  christos #include "gdbcmd.h"
     30  1.1  christos #include <string.h>
     31  1.1  christos #include "observer.h"
     32  1.1  christos #include "gdbthread.h"
     33  1.1  christos 
     34  1.1  christos /* Dummy frame.  This saves the processor state just prior to setting
     35  1.1  christos    up the inferior function call.  Older targets save the registers
     36  1.1  christos    on the target stack (but that really slows down function calls).  */
     37  1.1  christos 
     38  1.1  christos struct dummy_frame
     39  1.1  christos {
     40  1.1  christos   struct dummy_frame *next;
     41  1.1  christos   /* This frame's ID.  Must match the value returned by
     42  1.1  christos      gdbarch_dummy_id.  */
     43  1.1  christos   struct frame_id id;
     44  1.1  christos   /* The caller's state prior to the call.  */
     45  1.1  christos   struct infcall_suspend_state *caller_state;
     46  1.1  christos };
     47  1.1  christos 
     48  1.1  christos static struct dummy_frame *dummy_frame_stack = NULL;
     49  1.1  christos 
     50  1.1  christos /* Push the caller's state, along with the dummy frame info, onto the
     51  1.1  christos    dummy-frame stack.  */
     52  1.1  christos 
     53  1.1  christos void
     54  1.1  christos dummy_frame_push (struct infcall_suspend_state *caller_state,
     55  1.1  christos 		  const struct frame_id *dummy_id)
     56  1.1  christos {
     57  1.1  christos   struct dummy_frame *dummy_frame;
     58  1.1  christos 
     59  1.1  christos   dummy_frame = XZALLOC (struct dummy_frame);
     60  1.1  christos   dummy_frame->caller_state = caller_state;
     61  1.1  christos   dummy_frame->id = (*dummy_id);
     62  1.1  christos   dummy_frame->next = dummy_frame_stack;
     63  1.1  christos   dummy_frame_stack = dummy_frame;
     64  1.1  christos }
     65  1.1  christos 
     66  1.1  christos /* Remove *DUMMY_PTR from the dummy frame stack.  */
     67  1.1  christos 
     68  1.1  christos static void
     69  1.1  christos remove_dummy_frame (struct dummy_frame **dummy_ptr)
     70  1.1  christos {
     71  1.1  christos   struct dummy_frame *dummy = *dummy_ptr;
     72  1.1  christos 
     73  1.1  christos   *dummy_ptr = dummy->next;
     74  1.1  christos   discard_infcall_suspend_state (dummy->caller_state);
     75  1.1  christos   xfree (dummy);
     76  1.1  christos }
     77  1.1  christos 
     78  1.1  christos /* Delete any breakpoint B which is a momentary breakpoint for return from
     79  1.1  christos    inferior call matching DUMMY_VOIDP.  */
     80  1.1  christos 
     81  1.1  christos static int
     82  1.1  christos pop_dummy_frame_bpt (struct breakpoint *b, void *dummy_voidp)
     83  1.1  christos {
     84  1.1  christos   struct dummy_frame *dummy = dummy_voidp;
     85  1.1  christos 
     86  1.1  christos   if (b->thread == pid_to_thread_id (inferior_ptid)
     87  1.1  christos       && b->disposition == disp_del && frame_id_eq (b->frame_id, dummy->id))
     88  1.1  christos     {
     89  1.1  christos       while (b->related_breakpoint != b)
     90  1.1  christos 	delete_breakpoint (b->related_breakpoint);
     91  1.1  christos 
     92  1.1  christos       delete_breakpoint (b);
     93  1.1  christos 
     94  1.1  christos       /* Stop the traversal.  */
     95  1.1  christos       return 1;
     96  1.1  christos     }
     97  1.1  christos 
     98  1.1  christos   /* Continue the traversal.  */
     99  1.1  christos   return 0;
    100  1.1  christos }
    101  1.1  christos 
    102  1.1  christos /* Pop *DUMMY_PTR, restoring program state to that before the
    103  1.1  christos    frame was created.  */
    104  1.1  christos 
    105  1.1  christos static void
    106  1.1  christos pop_dummy_frame (struct dummy_frame **dummy_ptr)
    107  1.1  christos {
    108  1.1  christos   struct dummy_frame *dummy = *dummy_ptr;
    109  1.1  christos 
    110  1.1  christos   restore_infcall_suspend_state (dummy->caller_state);
    111  1.1  christos 
    112  1.1  christos   iterate_over_breakpoints (pop_dummy_frame_bpt, dummy);
    113  1.1  christos 
    114  1.1  christos   /* restore_infcall_control_state frees inf_state,
    115  1.1  christos      all that remains is to pop *dummy_ptr.  */
    116  1.1  christos   *dummy_ptr = dummy->next;
    117  1.1  christos   xfree (dummy);
    118  1.1  christos 
    119  1.1  christos   /* We've made right mess of GDB's local state, just discard
    120  1.1  christos      everything.  */
    121  1.1  christos   reinit_frame_cache ();
    122  1.1  christos }
    123  1.1  christos 
    124  1.1  christos /* Look up DUMMY_ID.
    125  1.1  christos    Return NULL if not found.  */
    126  1.1  christos 
    127  1.1  christos static struct dummy_frame **
    128  1.1  christos lookup_dummy_frame (struct frame_id dummy_id)
    129  1.1  christos {
    130  1.1  christos   struct dummy_frame **dp;
    131  1.1  christos 
    132  1.1  christos   for (dp = &dummy_frame_stack; *dp != NULL; dp = &(*dp)->next)
    133  1.1  christos     {
    134  1.1  christos       if (frame_id_eq ((*dp)->id, dummy_id))
    135  1.1  christos 	return dp;
    136  1.1  christos     }
    137  1.1  christos 
    138  1.1  christos   return NULL;
    139  1.1  christos }
    140  1.1  christos 
    141  1.1  christos /* Pop the dummy frame DUMMY_ID, restoring program state to that before the
    142  1.1  christos    frame was created.
    143  1.1  christos    On return reinit_frame_cache has been called.
    144  1.1  christos    If the frame isn't found, flag an internal error.
    145  1.1  christos 
    146  1.1  christos    NOTE: This can only pop the one frame, even if it is in the middle of the
    147  1.1  christos    stack, because the other frames may be for different threads, and there's
    148  1.1  christos    currently no way to tell which stack frame is for which thread.  */
    149  1.1  christos 
    150  1.1  christos void
    151  1.1  christos dummy_frame_pop (struct frame_id dummy_id)
    152  1.1  christos {
    153  1.1  christos   struct dummy_frame **dp;
    154  1.1  christos 
    155  1.1  christos   dp = lookup_dummy_frame (dummy_id);
    156  1.1  christos   gdb_assert (dp != NULL);
    157  1.1  christos 
    158  1.1  christos   pop_dummy_frame (dp);
    159  1.1  christos }
    160  1.1  christos 
    161  1.1  christos /* Drop dummy frame DUMMY_ID.  Do nothing if it is not found.  Do not restore
    162  1.1  christos    its state into inferior, just free its memory.  */
    163  1.1  christos 
    164  1.1  christos void
    165  1.1  christos dummy_frame_discard (struct frame_id dummy_id)
    166  1.1  christos {
    167  1.1  christos   struct dummy_frame **dp;
    168  1.1  christos 
    169  1.1  christos   dp = lookup_dummy_frame (dummy_id);
    170  1.1  christos   if (dp)
    171  1.1  christos     remove_dummy_frame (dp);
    172  1.1  christos }
    173  1.1  christos 
    174  1.1  christos /* There may be stale dummy frames, perhaps left over from when an uncaught
    175  1.1  christos    longjmp took us out of a function that was called by the debugger.  Clean
    176  1.1  christos    them up at least once whenever we start a new inferior.  */
    177  1.1  christos 
    178  1.1  christos static void
    179  1.1  christos cleanup_dummy_frames (struct target_ops *target, int from_tty)
    180  1.1  christos {
    181  1.1  christos   while (dummy_frame_stack != NULL)
    182  1.1  christos     remove_dummy_frame (&dummy_frame_stack);
    183  1.1  christos }
    184  1.1  christos 
    185  1.1  christos /* Return the dummy frame cache, it contains both the ID, and a
    186  1.1  christos    pointer to the regcache.  */
    187  1.1  christos struct dummy_frame_cache
    188  1.1  christos {
    189  1.1  christos   struct frame_id this_id;
    190  1.1  christos   struct regcache *prev_regcache;
    191  1.1  christos };
    192  1.1  christos 
    193  1.1  christos static int
    194  1.1  christos dummy_frame_sniffer (const struct frame_unwind *self,
    195  1.1  christos 		     struct frame_info *this_frame,
    196  1.1  christos 		     void **this_prologue_cache)
    197  1.1  christos {
    198  1.1  christos   struct dummy_frame *dummyframe;
    199  1.1  christos   struct frame_id this_id;
    200  1.1  christos 
    201  1.1  christos   /* When unwinding a normal frame, the stack structure is determined
    202  1.1  christos      by analyzing the frame's function's code (be it using brute force
    203  1.1  christos      prologue analysis, or the dwarf2 CFI).  In the case of a dummy
    204  1.1  christos      frame, that simply isn't possible.  The PC is either the program
    205  1.1  christos      entry point, or some random address on the stack.  Trying to use
    206  1.1  christos      that PC to apply standard frame ID unwind techniques is just
    207  1.1  christos      asking for trouble.  */
    208  1.1  christos 
    209  1.1  christos   /* Don't bother unless there is at least one dummy frame.  */
    210  1.1  christos   if (dummy_frame_stack != NULL)
    211  1.1  christos     {
    212  1.1  christos       /* Use an architecture specific method to extract this frame's
    213  1.1  christos 	 dummy ID, assuming it is a dummy frame.  */
    214  1.1  christos       this_id = gdbarch_dummy_id (get_frame_arch (this_frame), this_frame);
    215  1.1  christos 
    216  1.1  christos       /* Use that ID to find the corresponding cache entry.  */
    217  1.1  christos       for (dummyframe = dummy_frame_stack;
    218  1.1  christos 	   dummyframe != NULL;
    219  1.1  christos 	   dummyframe = dummyframe->next)
    220  1.1  christos 	{
    221  1.1  christos 	  if (frame_id_eq (dummyframe->id, this_id))
    222  1.1  christos 	    {
    223  1.1  christos 	      struct dummy_frame_cache *cache;
    224  1.1  christos 
    225  1.1  christos 	      cache = FRAME_OBSTACK_ZALLOC (struct dummy_frame_cache);
    226  1.1  christos 	      cache->prev_regcache = get_infcall_suspend_state_regcache
    227  1.1  christos 						   (dummyframe->caller_state);
    228  1.1  christos 	      cache->this_id = this_id;
    229  1.1  christos 	      (*this_prologue_cache) = cache;
    230  1.1  christos 	      return 1;
    231  1.1  christos 	    }
    232  1.1  christos 	}
    233  1.1  christos     }
    234  1.1  christos   return 0;
    235  1.1  christos }
    236  1.1  christos 
    237  1.1  christos /* Given a call-dummy dummy-frame, return the registers.  Here the
    238  1.1  christos    register value is taken from the local copy of the register buffer.  */
    239  1.1  christos 
    240  1.1  christos static struct value *
    241  1.1  christos dummy_frame_prev_register (struct frame_info *this_frame,
    242  1.1  christos 			   void **this_prologue_cache,
    243  1.1  christos 			   int regnum)
    244  1.1  christos {
    245  1.1  christos   struct dummy_frame_cache *cache = (*this_prologue_cache);
    246  1.1  christos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
    247  1.1  christos   struct value *reg_val;
    248  1.1  christos 
    249  1.1  christos   /* The dummy-frame sniffer always fills in the cache.  */
    250  1.1  christos   gdb_assert (cache != NULL);
    251  1.1  christos 
    252  1.1  christos   /* Describe the register's location.  Generic dummy frames always
    253  1.1  christos      have the register value in an ``expression''.  */
    254  1.1  christos   reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
    255  1.1  christos 
    256  1.1  christos   /* Use the regcache_cooked_read() method so that it, on the fly,
    257  1.1  christos      constructs either a raw or pseudo register from the raw
    258  1.1  christos      register cache.  */
    259  1.1  christos   regcache_cooked_read (cache->prev_regcache, regnum,
    260  1.1  christos 			value_contents_writeable (reg_val));
    261  1.1  christos   return reg_val;
    262  1.1  christos }
    263  1.1  christos 
    264  1.1  christos /* Assuming that THIS_FRAME is a dummy, return its ID.  That ID is
    265  1.1  christos    determined by examining the NEXT frame's unwound registers using
    266  1.1  christos    the method dummy_id().  As a side effect, THIS dummy frame's
    267  1.1  christos    dummy cache is located and saved in THIS_PROLOGUE_CACHE.  */
    268  1.1  christos 
    269  1.1  christos static void
    270  1.1  christos dummy_frame_this_id (struct frame_info *this_frame,
    271  1.1  christos 		     void **this_prologue_cache,
    272  1.1  christos 		     struct frame_id *this_id)
    273  1.1  christos {
    274  1.1  christos   /* The dummy-frame sniffer always fills in the cache.  */
    275  1.1  christos   struct dummy_frame_cache *cache = (*this_prologue_cache);
    276  1.1  christos 
    277  1.1  christos   gdb_assert (cache != NULL);
    278  1.1  christos   (*this_id) = cache->this_id;
    279  1.1  christos }
    280  1.1  christos 
    281  1.1  christos const struct frame_unwind dummy_frame_unwind =
    282  1.1  christos {
    283  1.1  christos   DUMMY_FRAME,
    284  1.1  christos   default_frame_unwind_stop_reason,
    285  1.1  christos   dummy_frame_this_id,
    286  1.1  christos   dummy_frame_prev_register,
    287  1.1  christos   NULL,
    288  1.1  christos   dummy_frame_sniffer,
    289  1.1  christos };
    290  1.1  christos 
    291  1.1  christos static void
    292  1.1  christos fprint_dummy_frames (struct ui_file *file)
    293  1.1  christos {
    294  1.1  christos   struct dummy_frame *s;
    295  1.1  christos 
    296  1.1  christos   for (s = dummy_frame_stack; s != NULL; s = s->next)
    297  1.1  christos     {
    298  1.1  christos       gdb_print_host_address (s, file);
    299  1.1  christos       fprintf_unfiltered (file, ":");
    300  1.1  christos       fprintf_unfiltered (file, " id=");
    301  1.1  christos       fprint_frame_id (file, s->id);
    302  1.1  christos       fprintf_unfiltered (file, "\n");
    303  1.1  christos     }
    304  1.1  christos }
    305  1.1  christos 
    306  1.1  christos static void
    307  1.1  christos maintenance_print_dummy_frames (char *args, int from_tty)
    308  1.1  christos {
    309  1.1  christos   if (args == NULL)
    310  1.1  christos     fprint_dummy_frames (gdb_stdout);
    311  1.1  christos   else
    312  1.1  christos     {
    313  1.1  christos       struct cleanup *cleanups;
    314  1.1  christos       struct ui_file *file = gdb_fopen (args, "w");
    315  1.1  christos 
    316  1.1  christos       if (file == NULL)
    317  1.1  christos 	perror_with_name (_("maintenance print dummy-frames"));
    318  1.1  christos       cleanups = make_cleanup_ui_file_delete (file);
    319  1.1  christos       fprint_dummy_frames (file);
    320  1.1  christos       do_cleanups (cleanups);
    321  1.1  christos     }
    322  1.1  christos }
    323  1.1  christos 
    324  1.1  christos extern void _initialize_dummy_frame (void);
    325  1.1  christos 
    326  1.1  christos void
    327  1.1  christos _initialize_dummy_frame (void)
    328  1.1  christos {
    329  1.1  christos   add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames,
    330  1.1  christos 	   _("Print the contents of the internal dummy-frame stack."),
    331  1.1  christos 	   &maintenanceprintlist);
    332  1.1  christos 
    333  1.1  christos   observer_attach_inferior_created (cleanup_dummy_frames);
    334  1.1  christos }
    335