Home | History | Annotate | Line # | Download | only in gdb
inline-frame.c revision 1.1
      1  1.1  christos /* Inline frame unwinder for GDB.
      2  1.1  christos 
      3  1.1  christos    Copyright (C) 2008-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 #include "defs.h"
     21  1.1  christos #include "inline-frame.h"
     22  1.1  christos #include "addrmap.h"
     23  1.1  christos #include "block.h"
     24  1.1  christos #include "frame-unwind.h"
     25  1.1  christos #include "inferior.h"
     26  1.1  christos #include "regcache.h"
     27  1.1  christos #include "symtab.h"
     28  1.1  christos #include "vec.h"
     29  1.1  christos 
     30  1.1  christos #include "gdb_assert.h"
     31  1.1  christos 
     32  1.1  christos /* We need to save a few variables for every thread stopped at the
     33  1.1  christos    virtual call site of an inlined function.  If there was always a
     34  1.1  christos    "struct thread_info", we could hang it off that; in the mean time,
     35  1.1  christos    keep our own list.  */
     36  1.1  christos struct inline_state
     37  1.1  christos {
     38  1.1  christos   /* The thread this data relates to.  It should be a currently
     39  1.1  christos      stopped thread; we assume thread IDs never change while the
     40  1.1  christos      thread is stopped.  */
     41  1.1  christos   ptid_t ptid;
     42  1.1  christos 
     43  1.1  christos   /* The number of inlined functions we are skipping.  Each of these
     44  1.1  christos      functions can be stepped in to.  */
     45  1.1  christos   int skipped_frames;
     46  1.1  christos 
     47  1.1  christos   /* Only valid if SKIPPED_FRAMES is non-zero.  This is the PC used
     48  1.1  christos      when calculating SKIPPED_FRAMES; used to check whether we have
     49  1.1  christos      moved to a new location by user request.  If so, we invalidate
     50  1.1  christos      any skipped frames.  */
     51  1.1  christos   CORE_ADDR saved_pc;
     52  1.1  christos 
     53  1.1  christos   /* Only valid if SKIPPED_FRAMES is non-zero.  This is the symbol
     54  1.1  christos      of the outermost skipped inline function.  It's used to find the
     55  1.1  christos      call site of the current frame.  */
     56  1.1  christos   struct symbol *skipped_symbol;
     57  1.1  christos };
     58  1.1  christos 
     59  1.1  christos typedef struct inline_state inline_state_s;
     60  1.1  christos DEF_VEC_O(inline_state_s);
     61  1.1  christos 
     62  1.1  christos static VEC(inline_state_s) *inline_states;
     63  1.1  christos 
     64  1.1  christos /* Locate saved inlined frame state for PTID, if it exists
     65  1.1  christos    and is valid.  */
     66  1.1  christos 
     67  1.1  christos static struct inline_state *
     68  1.1  christos find_inline_frame_state (ptid_t ptid)
     69  1.1  christos {
     70  1.1  christos   struct inline_state *state;
     71  1.1  christos   int ix;
     72  1.1  christos 
     73  1.1  christos   for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
     74  1.1  christos     {
     75  1.1  christos       if (ptid_equal (state->ptid, ptid))
     76  1.1  christos 	{
     77  1.1  christos 	  struct regcache *regcache = get_thread_regcache (ptid);
     78  1.1  christos 	  CORE_ADDR current_pc = regcache_read_pc (regcache);
     79  1.1  christos 
     80  1.1  christos 	  if (current_pc != state->saved_pc)
     81  1.1  christos 	    {
     82  1.1  christos 	      /* PC has changed - this context is invalid.  Use the
     83  1.1  christos 		 default behavior.  */
     84  1.1  christos 	      VEC_unordered_remove (inline_state_s, inline_states, ix);
     85  1.1  christos 	      return NULL;
     86  1.1  christos 	    }
     87  1.1  christos 	  else
     88  1.1  christos 	    return state;
     89  1.1  christos 	}
     90  1.1  christos     }
     91  1.1  christos 
     92  1.1  christos   return NULL;
     93  1.1  christos }
     94  1.1  christos 
     95  1.1  christos /* Allocate saved inlined frame state for PTID.  */
     96  1.1  christos 
     97  1.1  christos static struct inline_state *
     98  1.1  christos allocate_inline_frame_state (ptid_t ptid)
     99  1.1  christos {
    100  1.1  christos   struct inline_state *state;
    101  1.1  christos 
    102  1.1  christos   state = VEC_safe_push (inline_state_s, inline_states, NULL);
    103  1.1  christos   memset (state, 0, sizeof (*state));
    104  1.1  christos   state->ptid = ptid;
    105  1.1  christos 
    106  1.1  christos   return state;
    107  1.1  christos }
    108  1.1  christos 
    109  1.1  christos /* Forget about any hidden inlined functions in PTID, which is new or
    110  1.1  christos    about to be resumed.  PTID may be minus_one_ptid (all processes)
    111  1.1  christos    or a PID (all threads in this process).  */
    112  1.1  christos 
    113  1.1  christos void
    114  1.1  christos clear_inline_frame_state (ptid_t ptid)
    115  1.1  christos {
    116  1.1  christos   struct inline_state *state;
    117  1.1  christos   int ix;
    118  1.1  christos 
    119  1.1  christos   if (ptid_equal (ptid, minus_one_ptid))
    120  1.1  christos     {
    121  1.1  christos       VEC_free (inline_state_s, inline_states);
    122  1.1  christos       return;
    123  1.1  christos     }
    124  1.1  christos 
    125  1.1  christos   if (ptid_is_pid (ptid))
    126  1.1  christos     {
    127  1.1  christos       VEC (inline_state_s) *new_states = NULL;
    128  1.1  christos       int pid = ptid_get_pid (ptid);
    129  1.1  christos 
    130  1.1  christos       for (ix = 0;
    131  1.1  christos 	   VEC_iterate (inline_state_s, inline_states, ix, state);
    132  1.1  christos 	   ix++)
    133  1.1  christos 	if (pid != ptid_get_pid (state->ptid))
    134  1.1  christos 	  VEC_safe_push (inline_state_s, new_states, state);
    135  1.1  christos       VEC_free (inline_state_s, inline_states);
    136  1.1  christos       inline_states = new_states;
    137  1.1  christos       return;
    138  1.1  christos     }
    139  1.1  christos 
    140  1.1  christos   for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
    141  1.1  christos     if (ptid_equal (state->ptid, ptid))
    142  1.1  christos       {
    143  1.1  christos 	VEC_unordered_remove (inline_state_s, inline_states, ix);
    144  1.1  christos 	return;
    145  1.1  christos       }
    146  1.1  christos }
    147  1.1  christos 
    148  1.1  christos static void
    149  1.1  christos inline_frame_this_id (struct frame_info *this_frame,
    150  1.1  christos 		      void **this_cache,
    151  1.1  christos 		      struct frame_id *this_id)
    152  1.1  christos {
    153  1.1  christos   struct symbol *func;
    154  1.1  christos 
    155  1.1  christos   /* In order to have a stable frame ID for a given inline function,
    156  1.1  christos      we must get the stack / special addresses from the underlying
    157  1.1  christos      real frame's this_id method.  So we must call get_prev_frame.
    158  1.1  christos      Because we are inlined into some function, there must be previous
    159  1.1  christos      frames, so this is safe - as long as we're careful not to
    160  1.1  christos      create any cycles.  */
    161  1.1  christos   *this_id = get_frame_id (get_prev_frame (this_frame));
    162  1.1  christos 
    163  1.1  christos   /* We need a valid frame ID, so we need to be based on a valid
    164  1.1  christos      frame.  FSF submission NOTE: this would be a good assertion to
    165  1.1  christos      apply to all frames, all the time.  That would fix the ambiguity
    166  1.1  christos      of null_frame_id (between "no/any frame" and "the outermost
    167  1.1  christos      frame").  This will take work.  */
    168  1.1  christos   gdb_assert (frame_id_p (*this_id));
    169  1.1  christos 
    170  1.1  christos   /* For now, require we don't match outer_frame_id either (see
    171  1.1  christos      comment above).  */
    172  1.1  christos   gdb_assert (!frame_id_eq (*this_id, outer_frame_id));
    173  1.1  christos 
    174  1.1  christos   /* Future work NOTE: Alexandre Oliva applied a patch to GCC 4.3
    175  1.1  christos      which generates DW_AT_entry_pc for inlined functions when
    176  1.1  christos      possible.  If this attribute is available, we should use it
    177  1.1  christos      in the frame ID (and eventually, to set breakpoints).  */
    178  1.1  christos   func = get_frame_function (this_frame);
    179  1.1  christos   gdb_assert (func != NULL);
    180  1.1  christos   (*this_id).code_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
    181  1.1  christos   (*this_id).artificial_depth++;
    182  1.1  christos }
    183  1.1  christos 
    184  1.1  christos static struct value *
    185  1.1  christos inline_frame_prev_register (struct frame_info *this_frame, void **this_cache,
    186  1.1  christos 			    int regnum)
    187  1.1  christos {
    188  1.1  christos   /* Use get_frame_register_value instead of
    189  1.1  christos      frame_unwind_got_register, to avoid requiring this frame's ID.
    190  1.1  christos      This frame's ID depends on the previous frame's ID (unusual), and
    191  1.1  christos      the previous frame's ID depends on this frame's unwound
    192  1.1  christos      registers.  If unwinding registers from this frame called
    193  1.1  christos      get_frame_id, there would be a loop.
    194  1.1  christos 
    195  1.1  christos      Do not copy this code into any other unwinder!  Inlined functions
    196  1.1  christos      are special; other unwinders must not have a dependency on the
    197  1.1  christos      previous frame's ID, and therefore can and should use
    198  1.1  christos      frame_unwind_got_register instead.  */
    199  1.1  christos   return get_frame_register_value (this_frame, regnum);
    200  1.1  christos }
    201  1.1  christos 
    202  1.1  christos /* Check whether we are at an inlining site that does not already
    203  1.1  christos    have an associated frame.  */
    204  1.1  christos 
    205  1.1  christos static int
    206  1.1  christos inline_frame_sniffer (const struct frame_unwind *self,
    207  1.1  christos 		      struct frame_info *this_frame,
    208  1.1  christos 		      void **this_cache)
    209  1.1  christos {
    210  1.1  christos   CORE_ADDR this_pc;
    211  1.1  christos   struct block *frame_block, *cur_block;
    212  1.1  christos   int depth;
    213  1.1  christos   struct frame_info *next_frame;
    214  1.1  christos   struct inline_state *state = find_inline_frame_state (inferior_ptid);
    215  1.1  christos 
    216  1.1  christos   this_pc = get_frame_address_in_block (this_frame);
    217  1.1  christos   frame_block = block_for_pc (this_pc);
    218  1.1  christos   if (frame_block == NULL)
    219  1.1  christos     return 0;
    220  1.1  christos 
    221  1.1  christos   /* Calculate DEPTH, the number of inlined functions at this
    222  1.1  christos      location.  */
    223  1.1  christos   depth = 0;
    224  1.1  christos   cur_block = frame_block;
    225  1.1  christos   while (BLOCK_SUPERBLOCK (cur_block))
    226  1.1  christos     {
    227  1.1  christos       if (block_inlined_p (cur_block))
    228  1.1  christos 	depth++;
    229  1.1  christos 
    230  1.1  christos       cur_block = BLOCK_SUPERBLOCK (cur_block);
    231  1.1  christos     }
    232  1.1  christos 
    233  1.1  christos   /* Check how many inlined functions already have frames.  */
    234  1.1  christos   for (next_frame = get_next_frame (this_frame);
    235  1.1  christos        next_frame && get_frame_type (next_frame) == INLINE_FRAME;
    236  1.1  christos        next_frame = get_next_frame (next_frame))
    237  1.1  christos     {
    238  1.1  christos       gdb_assert (depth > 0);
    239  1.1  christos       depth--;
    240  1.1  christos     }
    241  1.1  christos 
    242  1.1  christos   /* If this is the topmost frame, or all frames above us are inlined,
    243  1.1  christos      then check whether we were requested to skip some frames (so they
    244  1.1  christos      can be stepped into later).  */
    245  1.1  christos   if (state != NULL && state->skipped_frames > 0 && next_frame == NULL)
    246  1.1  christos     {
    247  1.1  christos       gdb_assert (depth >= state->skipped_frames);
    248  1.1  christos       depth -= state->skipped_frames;
    249  1.1  christos     }
    250  1.1  christos 
    251  1.1  christos   /* If all the inlined functions here already have frames, then pass
    252  1.1  christos      to the normal unwinder for this PC.  */
    253  1.1  christos   if (depth == 0)
    254  1.1  christos     return 0;
    255  1.1  christos 
    256  1.1  christos   /* If the next frame is an inlined function, but not the outermost, then
    257  1.1  christos      we are the next outer.  If it is not an inlined function, then we
    258  1.1  christos      are the innermost inlined function of a different real frame.  */
    259  1.1  christos   return 1;
    260  1.1  christos }
    261  1.1  christos 
    262  1.1  christos const struct frame_unwind inline_frame_unwind = {
    263  1.1  christos   INLINE_FRAME,
    264  1.1  christos   default_frame_unwind_stop_reason,
    265  1.1  christos   inline_frame_this_id,
    266  1.1  christos   inline_frame_prev_register,
    267  1.1  christos   NULL,
    268  1.1  christos   inline_frame_sniffer
    269  1.1  christos };
    270  1.1  christos 
    271  1.1  christos /* Return non-zero if BLOCK, an inlined function block containing PC,
    272  1.1  christos    has a group of contiguous instructions starting at PC (but not
    273  1.1  christos    before it).  */
    274  1.1  christos 
    275  1.1  christos static int
    276  1.1  christos block_starting_point_at (CORE_ADDR pc, struct block *block)
    277  1.1  christos {
    278  1.1  christos   struct blockvector *bv;
    279  1.1  christos   struct block *new_block;
    280  1.1  christos 
    281  1.1  christos   bv = blockvector_for_pc (pc, NULL);
    282  1.1  christos   if (BLOCKVECTOR_MAP (bv) == NULL)
    283  1.1  christos     return 0;
    284  1.1  christos 
    285  1.1  christos   new_block = addrmap_find (BLOCKVECTOR_MAP (bv), pc - 1);
    286  1.1  christos   if (new_block == NULL)
    287  1.1  christos     return 1;
    288  1.1  christos 
    289  1.1  christos   if (new_block == block || contained_in (new_block, block))
    290  1.1  christos     return 0;
    291  1.1  christos 
    292  1.1  christos   /* The immediately preceding address belongs to a different block,
    293  1.1  christos      which is not a child of this one.  Treat this as an entrance into
    294  1.1  christos      BLOCK.  */
    295  1.1  christos   return 1;
    296  1.1  christos }
    297  1.1  christos 
    298  1.1  christos /* Skip all inlined functions whose call sites are at the current PC.
    299  1.1  christos    Frames for the hidden functions will not appear in the backtrace until the
    300  1.1  christos    user steps into them.  */
    301  1.1  christos 
    302  1.1  christos void
    303  1.1  christos skip_inline_frames (ptid_t ptid)
    304  1.1  christos {
    305  1.1  christos   CORE_ADDR this_pc;
    306  1.1  christos   struct block *frame_block, *cur_block;
    307  1.1  christos   struct symbol *last_sym = NULL;
    308  1.1  christos   int skip_count = 0;
    309  1.1  christos   struct inline_state *state;
    310  1.1  christos 
    311  1.1  christos   /* This function is called right after reinitializing the frame
    312  1.1  christos      cache.  We try not to do more unwinding than absolutely
    313  1.1  christos      necessary, for performance.  */
    314  1.1  christos   this_pc = get_frame_pc (get_current_frame ());
    315  1.1  christos   frame_block = block_for_pc (this_pc);
    316  1.1  christos 
    317  1.1  christos   if (frame_block != NULL)
    318  1.1  christos     {
    319  1.1  christos       cur_block = frame_block;
    320  1.1  christos       while (BLOCK_SUPERBLOCK (cur_block))
    321  1.1  christos 	{
    322  1.1  christos 	  if (block_inlined_p (cur_block))
    323  1.1  christos 	    {
    324  1.1  christos 	      /* See comments in inline_frame_this_id about this use
    325  1.1  christos 		 of BLOCK_START.  */
    326  1.1  christos 	      if (BLOCK_START (cur_block) == this_pc
    327  1.1  christos 		  || block_starting_point_at (this_pc, cur_block))
    328  1.1  christos 		{
    329  1.1  christos 		  skip_count++;
    330  1.1  christos 		  last_sym = BLOCK_FUNCTION (cur_block);
    331  1.1  christos 		}
    332  1.1  christos 	      else
    333  1.1  christos 		break;
    334  1.1  christos 	    }
    335  1.1  christos 	  cur_block = BLOCK_SUPERBLOCK (cur_block);
    336  1.1  christos 	}
    337  1.1  christos     }
    338  1.1  christos 
    339  1.1  christos   gdb_assert (find_inline_frame_state (ptid) == NULL);
    340  1.1  christos   state = allocate_inline_frame_state (ptid);
    341  1.1  christos   state->skipped_frames = skip_count;
    342  1.1  christos   state->saved_pc = this_pc;
    343  1.1  christos   state->skipped_symbol = last_sym;
    344  1.1  christos 
    345  1.1  christos   if (skip_count != 0)
    346  1.1  christos     reinit_frame_cache ();
    347  1.1  christos }
    348  1.1  christos 
    349  1.1  christos /* Step into an inlined function by unhiding it.  */
    350  1.1  christos 
    351  1.1  christos void
    352  1.1  christos step_into_inline_frame (ptid_t ptid)
    353  1.1  christos {
    354  1.1  christos   struct inline_state *state = find_inline_frame_state (ptid);
    355  1.1  christos 
    356  1.1  christos   gdb_assert (state != NULL && state->skipped_frames > 0);
    357  1.1  christos   state->skipped_frames--;
    358  1.1  christos   reinit_frame_cache ();
    359  1.1  christos }
    360  1.1  christos 
    361  1.1  christos /* Return the number of hidden functions inlined into the current
    362  1.1  christos    frame.  */
    363  1.1  christos 
    364  1.1  christos int
    365  1.1  christos inline_skipped_frames (ptid_t ptid)
    366  1.1  christos {
    367  1.1  christos   struct inline_state *state = find_inline_frame_state (ptid);
    368  1.1  christos 
    369  1.1  christos   if (state == NULL)
    370  1.1  christos     return 0;
    371  1.1  christos   else
    372  1.1  christos     return state->skipped_frames;
    373  1.1  christos }
    374  1.1  christos 
    375  1.1  christos /* If one or more inlined functions are hidden, return the symbol for
    376  1.1  christos    the function inlined into the current frame.  */
    377  1.1  christos 
    378  1.1  christos struct symbol *
    379  1.1  christos inline_skipped_symbol (ptid_t ptid)
    380  1.1  christos {
    381  1.1  christos   struct inline_state *state = find_inline_frame_state (ptid);
    382  1.1  christos 
    383  1.1  christos   gdb_assert (state != NULL);
    384  1.1  christos   return state->skipped_symbol;
    385  1.1  christos }
    386  1.1  christos 
    387  1.1  christos /* Return the number of functions inlined into THIS_FRAME.  Some of
    388  1.1  christos    the callees may not have associated frames (see
    389  1.1  christos    skip_inline_frames).  */
    390  1.1  christos 
    391  1.1  christos int
    392  1.1  christos frame_inlined_callees (struct frame_info *this_frame)
    393  1.1  christos {
    394  1.1  christos   struct frame_info *next_frame;
    395  1.1  christos   int inline_count = 0;
    396  1.1  christos 
    397  1.1  christos   /* First count how many inlined functions at this PC have frames
    398  1.1  christos      above FRAME (are inlined into FRAME).  */
    399  1.1  christos   for (next_frame = get_next_frame (this_frame);
    400  1.1  christos        next_frame && get_frame_type (next_frame) == INLINE_FRAME;
    401  1.1  christos        next_frame = get_next_frame (next_frame))
    402  1.1  christos     inline_count++;
    403  1.1  christos 
    404  1.1  christos   /* Simulate some most-inner inlined frames which were suppressed, so
    405  1.1  christos      they can be stepped into later.  If we are unwinding already
    406  1.1  christos      outer frames from some non-inlined frame this does not apply.  */
    407  1.1  christos   if (next_frame == NULL)
    408  1.1  christos     inline_count += inline_skipped_frames (inferior_ptid);
    409  1.1  christos 
    410  1.1  christos   return inline_count;
    411  1.1  christos }
    412