gdb/inline-frame.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Inline frame unwinder for GDB.

  2.    Copyright (C) 2008-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "inline-frame.h"
  16. #include "addrmap.h"
  17. #include "block.h"
  18. #include "frame-unwind.h"
  19. #include "inferior.h"
  20. #include "regcache.h"
  21. #include "symtab.h"
  22. #include "vec.h"
  23. #include "frame.h"

  24. /* We need to save a few variables for every thread stopped at the
  25.    virtual call site of an inlined function.  If there was always a
  26.    "struct thread_info", we could hang it off that; in the mean time,
  27.    keep our own list.  */
  28. struct inline_state
  29. {
  30.   /* The thread this data relates to.  It should be a currently
  31.      stopped thread; we assume thread IDs never change while the
  32.      thread is stopped.  */
  33.   ptid_t ptid;

  34.   /* The number of inlined functions we are skipping.  Each of these
  35.      functions can be stepped in to.  */
  36.   int skipped_frames;

  37.   /* Only valid if SKIPPED_FRAMES is non-zero.  This is the PC used
  38.      when calculating SKIPPED_FRAMES; used to check whether we have
  39.      moved to a new location by user request.  If so, we invalidate
  40.      any skipped frames.  */
  41.   CORE_ADDR saved_pc;

  42.   /* Only valid if SKIPPED_FRAMES is non-zero.  This is the symbol
  43.      of the outermost skipped inline function.  It's used to find the
  44.      call site of the current frame.  */
  45.   struct symbol *skipped_symbol;
  46. };

  47. typedef struct inline_state inline_state_s;
  48. DEF_VEC_O(inline_state_s);

  49. static VEC(inline_state_s) *inline_states;

  50. /* Locate saved inlined frame state for PTID, if it exists
  51.    and is valid.  */

  52. static struct inline_state *
  53. find_inline_frame_state (ptid_t ptid)
  54. {
  55.   struct inline_state *state;
  56.   int ix;

  57.   for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
  58.     {
  59.       if (ptid_equal (state->ptid, ptid))
  60.         {
  61.           struct regcache *regcache = get_thread_regcache (ptid);
  62.           CORE_ADDR current_pc = regcache_read_pc (regcache);

  63.           if (current_pc != state->saved_pc)
  64.             {
  65.               /* PC has changed - this context is invalid.  Use the
  66.                  default behavior.  */
  67.               VEC_unordered_remove (inline_state_s, inline_states, ix);
  68.               return NULL;
  69.             }
  70.           else
  71.             return state;
  72.         }
  73.     }

  74.   return NULL;
  75. }

  76. /* Allocate saved inlined frame state for PTID.  */

  77. static struct inline_state *
  78. allocate_inline_frame_state (ptid_t ptid)
  79. {
  80.   struct inline_state *state;

  81.   state = VEC_safe_push (inline_state_s, inline_states, NULL);
  82.   memset (state, 0, sizeof (*state));
  83.   state->ptid = ptid;

  84.   return state;
  85. }

  86. /* Forget about any hidden inlined functions in PTID, which is new or
  87.    about to be resumed.  PTID may be minus_one_ptid (all processes)
  88.    or a PID (all threads in this process).  */

  89. void
  90. clear_inline_frame_state (ptid_t ptid)
  91. {
  92.   struct inline_state *state;
  93.   int ix;

  94.   if (ptid_equal (ptid, minus_one_ptid))
  95.     {
  96.       VEC_free (inline_state_s, inline_states);
  97.       return;
  98.     }

  99.   if (ptid_is_pid (ptid))
  100.     {
  101.       VEC (inline_state_s) *new_states = NULL;
  102.       int pid = ptid_get_pid (ptid);

  103.       for (ix = 0;
  104.            VEC_iterate (inline_state_s, inline_states, ix, state);
  105.            ix++)
  106.         if (pid != ptid_get_pid (state->ptid))
  107.           VEC_safe_push (inline_state_s, new_states, state);
  108.       VEC_free (inline_state_s, inline_states);
  109.       inline_states = new_states;
  110.       return;
  111.     }

  112.   for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++)
  113.     if (ptid_equal (state->ptid, ptid))
  114.       {
  115.         VEC_unordered_remove (inline_state_s, inline_states, ix);
  116.         return;
  117.       }
  118. }

  119. static void
  120. inline_frame_this_id (struct frame_info *this_frame,
  121.                       void **this_cache,
  122.                       struct frame_id *this_id)
  123. {
  124.   struct symbol *func;

  125.   /* In order to have a stable frame ID for a given inline function,
  126.      we must get the stack / special addresses from the underlying
  127.      real frame's this_id method.  So we must call
  128.      get_prev_frame_always.  Because we are inlined into some
  129.      function, there must be previous frames, so this is safe - as
  130.      long as we're careful not to create any cycles.  */
  131.   *this_id = get_frame_id (get_prev_frame_always (this_frame));

  132.   /* We need a valid frame ID, so we need to be based on a valid
  133.      frame.  FSF submission NOTE: this would be a good assertion to
  134.      apply to all frames, all the time.  That would fix the ambiguity
  135.      of null_frame_id (between "no/any frame" and "the outermost
  136.      frame").  This will take work.  */
  137.   gdb_assert (frame_id_p (*this_id));

  138.   /* For now, require we don't match outer_frame_id either (see
  139.      comment above).  */
  140.   gdb_assert (!frame_id_eq (*this_id, outer_frame_id));

  141.   /* Future work NOTE: Alexandre Oliva applied a patch to GCC 4.3
  142.      which generates DW_AT_entry_pc for inlined functions when
  143.      possible.  If this attribute is available, we should use it
  144.      in the frame ID (and eventually, to set breakpoints).  */
  145.   func = get_frame_function (this_frame);
  146.   gdb_assert (func != NULL);
  147.   (*this_id).code_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
  148.   (*this_id).artificial_depth++;
  149. }

  150. static struct value *
  151. inline_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  152.                             int regnum)
  153. {
  154.   /* Use get_frame_register_value instead of
  155.      frame_unwind_got_register, to avoid requiring this frame's ID.
  156.      This frame's ID depends on the previous frame's ID (unusual), and
  157.      the previous frame's ID depends on this frame's unwound
  158.      registers.  If unwinding registers from this frame called
  159.      get_frame_id, there would be a loop.

  160.      Do not copy this code into any other unwinder!  Inlined functions
  161.      are special; other unwinders must not have a dependency on the
  162.      previous frame's ID, and therefore can and should use
  163.      frame_unwind_got_register instead.  */
  164.   return get_frame_register_value (this_frame, regnum);
  165. }

  166. /* Check whether we are at an inlining site that does not already
  167.    have an associated frame.  */

  168. static int
  169. inline_frame_sniffer (const struct frame_unwind *self,
  170.                       struct frame_info *this_frame,
  171.                       void **this_cache)
  172. {
  173.   CORE_ADDR this_pc;
  174.   const struct block *frame_block, *cur_block;
  175.   int depth;
  176.   struct frame_info *next_frame;
  177.   struct inline_state *state = find_inline_frame_state (inferior_ptid);

  178.   this_pc = get_frame_address_in_block (this_frame);
  179.   frame_block = block_for_pc (this_pc);
  180.   if (frame_block == NULL)
  181.     return 0;

  182.   /* Calculate DEPTH, the number of inlined functions at this
  183.      location.  */
  184.   depth = 0;
  185.   cur_block = frame_block;
  186.   while (BLOCK_SUPERBLOCK (cur_block))
  187.     {
  188.       if (block_inlined_p (cur_block))
  189.         depth++;

  190.       cur_block = BLOCK_SUPERBLOCK (cur_block);
  191.     }

  192.   /* Check how many inlined functions already have frames.  */
  193.   for (next_frame = get_next_frame (this_frame);
  194.        next_frame && get_frame_type (next_frame) == INLINE_FRAME;
  195.        next_frame = get_next_frame (next_frame))
  196.     {
  197.       gdb_assert (depth > 0);
  198.       depth--;
  199.     }

  200.   /* If this is the topmost frame, or all frames above us are inlined,
  201.      then check whether we were requested to skip some frames (so they
  202.      can be stepped into later).  */
  203.   if (state != NULL && state->skipped_frames > 0 && next_frame == NULL)
  204.     {
  205.       gdb_assert (depth >= state->skipped_frames);
  206.       depth -= state->skipped_frames;
  207.     }

  208.   /* If all the inlined functions here already have frames, then pass
  209.      to the normal unwinder for this PC.  */
  210.   if (depth == 0)
  211.     return 0;

  212.   /* If the next frame is an inlined function, but not the outermost, then
  213.      we are the next outer.  If it is not an inlined function, then we
  214.      are the innermost inlined function of a different real frame.  */
  215.   return 1;
  216. }

  217. const struct frame_unwind inline_frame_unwind = {
  218.   INLINE_FRAME,
  219.   default_frame_unwind_stop_reason,
  220.   inline_frame_this_id,
  221.   inline_frame_prev_register,
  222.   NULL,
  223.   inline_frame_sniffer
  224. };

  225. /* Return non-zero if BLOCK, an inlined function block containing PC,
  226.    has a group of contiguous instructions starting at PC (but not
  227.    before it).  */

  228. static int
  229. block_starting_point_at (CORE_ADDR pc, const struct block *block)
  230. {
  231.   const struct blockvector *bv;
  232.   struct block *new_block;

  233.   bv = blockvector_for_pc (pc, NULL);
  234.   if (BLOCKVECTOR_MAP (bv) == NULL)
  235.     return 0;

  236.   new_block = addrmap_find (BLOCKVECTOR_MAP (bv), pc - 1);
  237.   if (new_block == NULL)
  238.     return 1;

  239.   if (new_block == block || contained_in (new_block, block))
  240.     return 0;

  241.   /* The immediately preceding address belongs to a different block,
  242.      which is not a child of this one.  Treat this as an entrance into
  243.      BLOCK.  */
  244.   return 1;
  245. }

  246. /* Skip all inlined functions whose call sites are at the current PC.
  247.    Frames for the hidden functions will not appear in the backtrace until the
  248.    user steps into them.  */

  249. void
  250. skip_inline_frames (ptid_t ptid)
  251. {
  252.   CORE_ADDR this_pc;
  253.   const struct block *frame_block, *cur_block;
  254.   struct symbol *last_sym = NULL;
  255.   int skip_count = 0;
  256.   struct inline_state *state;

  257.   /* This function is called right after reinitializing the frame
  258.      cache.  We try not to do more unwinding than absolutely
  259.      necessary, for performance.  */
  260.   this_pc = get_frame_pc (get_current_frame ());
  261.   frame_block = block_for_pc (this_pc);

  262.   if (frame_block != NULL)
  263.     {
  264.       cur_block = frame_block;
  265.       while (BLOCK_SUPERBLOCK (cur_block))
  266.         {
  267.           if (block_inlined_p (cur_block))
  268.             {
  269.               /* See comments in inline_frame_this_id about this use
  270.                  of BLOCK_START.  */
  271.               if (BLOCK_START (cur_block) == this_pc
  272.                   || block_starting_point_at (this_pc, cur_block))
  273.                 {
  274.                   skip_count++;
  275.                   last_sym = BLOCK_FUNCTION (cur_block);
  276.                 }
  277.               else
  278.                 break;
  279.             }
  280.           cur_block = BLOCK_SUPERBLOCK (cur_block);
  281.         }
  282.     }

  283.   gdb_assert (find_inline_frame_state (ptid) == NULL);
  284.   state = allocate_inline_frame_state (ptid);
  285.   state->skipped_frames = skip_count;
  286.   state->saved_pc = this_pc;
  287.   state->skipped_symbol = last_sym;

  288.   if (skip_count != 0)
  289.     reinit_frame_cache ();
  290. }

  291. /* Step into an inlined function by unhiding it.  */

  292. void
  293. step_into_inline_frame (ptid_t ptid)
  294. {
  295.   struct inline_state *state = find_inline_frame_state (ptid);

  296.   gdb_assert (state != NULL && state->skipped_frames > 0);
  297.   state->skipped_frames--;
  298.   reinit_frame_cache ();
  299. }

  300. /* Return the number of hidden functions inlined into the current
  301.    frame.  */

  302. int
  303. inline_skipped_frames (ptid_t ptid)
  304. {
  305.   struct inline_state *state = find_inline_frame_state (ptid);

  306.   if (state == NULL)
  307.     return 0;
  308.   else
  309.     return state->skipped_frames;
  310. }

  311. /* If one or more inlined functions are hidden, return the symbol for
  312.    the function inlined into the current frame.  */

  313. struct symbol *
  314. inline_skipped_symbol (ptid_t ptid)
  315. {
  316.   struct inline_state *state = find_inline_frame_state (ptid);

  317.   gdb_assert (state != NULL);
  318.   return state->skipped_symbol;
  319. }

  320. /* Return the number of functions inlined into THIS_FRAME.  Some of
  321.    the callees may not have associated frames (see
  322.    skip_inline_frames).  */

  323. int
  324. frame_inlined_callees (struct frame_info *this_frame)
  325. {
  326.   struct frame_info *next_frame;
  327.   int inline_count = 0;

  328.   /* First count how many inlined functions at this PC have frames
  329.      above FRAME (are inlined into FRAME).  */
  330.   for (next_frame = get_next_frame (this_frame);
  331.        next_frame && get_frame_type (next_frame) == INLINE_FRAME;
  332.        next_frame = get_next_frame (next_frame))
  333.     inline_count++;

  334.   /* Simulate some most-inner inlined frames which were suppressed, so
  335.      they can be stepped into later.  If we are unwinding already
  336.      outer frames from some non-inlined frame this does not apply.  */
  337.   if (next_frame == NULL)
  338.     inline_count += inline_skipped_frames (inferior_ptid);

  339.   return inline_count;
  340. }