gdb/dwarf2-frame-tailcall.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Virtual tail call frames unwinder for GDB.

  2.    Copyright (C) 2010-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 "frame.h"
  16. #include "dwarf2-frame-tailcall.h"
  17. #include "dwarf2loc.h"
  18. #include "frame-unwind.h"
  19. #include "block.h"
  20. #include "hashtab.h"
  21. #include "gdbtypes.h"
  22. #include "regcache.h"
  23. #include "value.h"
  24. #include "dwarf2-frame.h"

  25. /* Contains struct tailcall_cache indexed by next_bottom_frame.  */
  26. static htab_t cache_htab;

  27. /* Associate structure of the unwinder to call_site_chain.  Lifetime of this
  28.    structure is maintained by REFC decremented by dealloc_cache, all of them
  29.    get deleted during reinit_frame_cache.  */
  30. struct tailcall_cache
  31. {
  32.   /* It must be the first one of this struct.  It is the furthest callee.  */
  33.   struct frame_info *next_bottom_frame;

  34.   /* Reference count.  The whole chain of virtual tail call frames shares one
  35.      tailcall_cache.  */
  36.   int refc;

  37.   /* Associated found virtual taill call frames chain, it is never NULL.  */
  38.   struct call_site_chain *chain;

  39.   /* Cached pretended_chain_levels result.  */
  40.   int chain_levels;

  41.   /* Unwound PC from the top (caller) frame, as it is not contained
  42.      in CHAIN.  */
  43.   CORE_ADDR prev_pc;

  44.   /* Compensate SP in caller frames appropriately.  prev_sp and
  45.      entry_cfa_sp_offset are valid only if PREV_SP_P.  PREV_SP is SP at the top
  46.      (caller) frame.  ENTRY_CFA_SP_OFFSET is shift of SP in tail call frames
  47.      against next_bottom_frame SP.  */
  48.   unsigned prev_sp_p : 1;
  49.   CORE_ADDR prev_sp;
  50.   LONGEST entry_cfa_sp_offset;
  51. };

  52. /* hash_f for htab_create_alloc of cache_htab.  */

  53. static hashval_t
  54. cache_hash (const void *arg)
  55. {
  56.   const struct tailcall_cache *cache = arg;

  57.   return htab_hash_pointer (cache->next_bottom_frame);
  58. }

  59. /* eq_f for htab_create_alloc of cache_htab.  */

  60. static int
  61. cache_eq (const void *arg1, const void *arg2)
  62. {
  63.   const struct tailcall_cache *cache1 = arg1;
  64.   const struct tailcall_cache *cache2 = arg2;

  65.   return cache1->next_bottom_frame == cache2->next_bottom_frame;
  66. }

  67. /* Create new tailcall_cache for NEXT_BOTTOM_FRAME, NEXT_BOTTOM_FRAME must not
  68.    yet have been indexed by cache_htab.  Caller holds one reference of the new
  69.    tailcall_cache.  */

  70. static struct tailcall_cache *
  71. cache_new_ref1 (struct frame_info *next_bottom_frame)
  72. {
  73.   struct tailcall_cache *cache;
  74.   void **slot;

  75.   cache = xzalloc (sizeof (*cache));

  76.   cache->next_bottom_frame = next_bottom_frame;
  77.   cache->refc = 1;

  78.   slot = htab_find_slot (cache_htab, cache, INSERT);
  79.   gdb_assert (*slot == NULL);
  80.   *slot = cache;

  81.   return cache;
  82. }

  83. /* Create new reference to CACHE.  */

  84. static void
  85. cache_ref (struct tailcall_cache *cache)
  86. {
  87.   gdb_assert (cache->refc > 0);

  88.   cache->refc++;
  89. }

  90. /* Drop reference to CACHE, possibly fully freeing it and unregistering it from
  91.    cache_htab.  */

  92. static void
  93. cache_unref (struct tailcall_cache *cache)
  94. {
  95.   gdb_assert (cache->refc > 0);

  96.   if (!--cache->refc)
  97.     {
  98.       gdb_assert (htab_find_slot (cache_htab, cache, NO_INSERT) != NULL);
  99.       htab_remove_elt (cache_htab, cache);

  100.       xfree (cache->chain);
  101.       xfree (cache);
  102.     }
  103. }

  104. /* Return 1 if FI is a non-bottom (not the callee) tail call frame.  Otherwise
  105.    return 0.  */

  106. static int
  107. frame_is_tailcall (struct frame_info *fi)
  108. {
  109.   return frame_unwinder_is (fi, &dwarf2_tailcall_frame_unwind);
  110. }

  111. /* Try to find tailcall_cache in cache_htab if FI is a part of its virtual tail
  112.    call chain.  Otherwise return NULL.  No new reference is created.  */

  113. static struct tailcall_cache *
  114. cache_find (struct frame_info *fi)
  115. {
  116.   struct tailcall_cache *cache;
  117.   void **slot;

  118.   while (frame_is_tailcall (fi))
  119.     {
  120.       fi = get_next_frame (fi);
  121.       gdb_assert (fi != NULL);
  122.     }

  123.   slot = htab_find_slot (cache_htab, &fi, NO_INSERT);
  124.   if (slot == NULL)
  125.     return NULL;

  126.   cache = *slot;
  127.   gdb_assert (cache != NULL);
  128.   return cache;
  129. }

  130. /* Number of virtual frames between THIS_FRAME and CACHE->NEXT_BOTTOM_FRAME.
  131.    If THIS_FRAME is CACHE-> NEXT_BOTTOM_FRAME return -1.  */

  132. static int
  133. existing_next_levels (struct frame_info *this_frame,
  134.                       struct tailcall_cache *cache)
  135. {
  136.   int retval = (frame_relative_level (this_frame)
  137.                 - frame_relative_level (cache->next_bottom_frame) - 1);

  138.   gdb_assert (retval >= -1);

  139.   return retval;
  140. }

  141. /* The number of virtual tail call frames in CHAIN.  With no virtual tail call
  142.    frames the function would return 0 (but CHAIN does not exist in such
  143.    case).  */

  144. static int
  145. pretended_chain_levels (struct call_site_chain *chain)
  146. {
  147.   int chain_levels;

  148.   gdb_assert (chain != NULL);

  149.   if (chain->callers == chain->length && chain->callees == chain->length)
  150.     return chain->length;

  151.   chain_levels = chain->callers + chain->callees;
  152.   gdb_assert (chain_levels < chain->length);

  153.   return chain_levels;
  154. }

  155. /* Implementation of frame_this_id_ftype.  THIS_CACHE must be already
  156.    initialized with tailcall_cache, THIS_FRAME must be a part of THIS_CACHE.

  157.    Specific virtual tail call frames are tracked by INLINE_DEPTH.  */

  158. static void
  159. tailcall_frame_this_id (struct frame_info *this_frame, void **this_cache,
  160.                         struct frame_id *this_id)
  161. {
  162.   struct tailcall_cache *cache = *this_cache;
  163.   struct frame_info *next_frame;

  164.   /* Tail call does not make sense for a sentinel frame.  */
  165.   next_frame = get_next_frame (this_frame);
  166.   gdb_assert (next_frame != NULL);

  167.   *this_id = get_frame_id (next_frame);
  168.   (*this_id).code_addr = get_frame_pc (this_frame);
  169.   (*this_id).code_addr_p = 1;
  170.   (*this_id).artificial_depth = (cache->chain_levels
  171.                                  - existing_next_levels (this_frame, cache));
  172.   gdb_assert ((*this_id).artificial_depth > 0);
  173. }

  174. /* Find PC to be unwound from THIS_FRAME.  THIS_FRAME must be a part of
  175.    CACHE.  */

  176. static CORE_ADDR
  177. pretend_pc (struct frame_info *this_frame, struct tailcall_cache *cache)
  178. {
  179.   int next_levels = existing_next_levels (this_frame, cache);
  180.   struct call_site_chain *chain = cache->chain;

  181.   gdb_assert (chain != NULL);

  182.   next_levels++;
  183.   gdb_assert (next_levels >= 0);

  184.   if (next_levels < chain->callees)
  185.     return chain->call_site[chain->length - next_levels - 1]->pc;
  186.   next_levels -= chain->callees;

  187.   /* Otherwise CHAIN->CALLEES are already covered by CHAIN->CALLERS.  */
  188.   if (chain->callees != chain->length)
  189.     {
  190.       if (next_levels < chain->callers)
  191.         return chain->call_site[chain->callers - next_levels - 1]->pc;
  192.       next_levels -= chain->callers;
  193.     }

  194.   gdb_assert (next_levels == 0);
  195.   return cache->prev_pc;
  196. }

  197. /* Implementation of frame_prev_register_ftype.  If no specific register
  198.    override is supplied NULL is returned (this is incompatible with
  199.    frame_prev_register_ftype semantics).  next_bottom_frame and tail call
  200.    frames unwind the NULL case differently.  */

  201. struct value *
  202. dwarf2_tailcall_prev_register_first (struct frame_info *this_frame,
  203.                                      void **tailcall_cachep, int regnum)
  204. {
  205.   struct gdbarch *this_gdbarch = get_frame_arch (this_frame);
  206.   struct tailcall_cache *cache = *tailcall_cachep;
  207.   CORE_ADDR addr;

  208.   if (regnum == gdbarch_pc_regnum (this_gdbarch))
  209.     addr = pretend_pc (this_frame, cache);
  210.   else if (cache->prev_sp_p && regnum == gdbarch_sp_regnum (this_gdbarch))
  211.     {
  212.       int next_levels = existing_next_levels (this_frame, cache);

  213.       if (next_levels == cache->chain_levels - 1)
  214.         addr = cache->prev_sp;
  215.       else
  216.         addr = dwarf2_frame_cfa (this_frame) - cache->entry_cfa_sp_offset;
  217.     }
  218.   else
  219.     return NULL;

  220.   return frame_unwind_got_address (this_frame, regnum, addr);
  221. }

  222. /* Implementation of frame_prev_register_ftype for tail call frames.  Register
  223.    set of virtual tail call frames is assumed to be the one of the top (caller)
  224.    frame - assume unchanged register value for NULL from
  225.    dwarf2_tailcall_prev_register_first.  */

  226. static struct value *
  227. tailcall_frame_prev_register (struct frame_info *this_frame,
  228.                                void **this_cache, int regnum)
  229. {
  230.   struct tailcall_cache *cache = *this_cache;
  231.   struct value *val;

  232.   gdb_assert (this_frame != cache->next_bottom_frame);

  233.   val = dwarf2_tailcall_prev_register_first (this_frame, this_cache, regnum);
  234.   if (val)
  235.     return val;

  236.   return frame_unwind_got_register (this_frame, regnum, regnum);
  237. }

  238. /* Implementation of frame_sniffer_ftype.  It will never find a new chain, use
  239.    dwarf2_tailcall_sniffer_first for the bottom (callee) frame.  It will find
  240.    all the predecessing virtual tail call frames, it will return false when
  241.    there exist no more tail call frames in this chain.  */

  242. static int
  243. tailcall_frame_sniffer (const struct frame_unwind *self,
  244.                          struct frame_info *this_frame, void **this_cache)
  245. {
  246.   struct frame_info *next_frame;
  247.   int next_levels;
  248.   struct tailcall_cache *cache;

  249.   /* Inner tail call element does not make sense for a sentinel frame.  */
  250.   next_frame = get_next_frame (this_frame);
  251.   if (next_frame == NULL)
  252.     return 0;

  253.   cache = cache_find (next_frame);
  254.   if (cache == NULL)
  255.     return 0;

  256.   cache_ref (cache);

  257.   next_levels = existing_next_levels (this_frame, cache);

  258.   /* NEXT_LEVELS is -1 only in dwarf2_tailcall_sniffer_first.  */
  259.   gdb_assert (next_levels >= 0);
  260.   gdb_assert (next_levels <= cache->chain_levels);

  261.   if (next_levels == cache->chain_levels)
  262.     {
  263.       cache_unref (cache);
  264.       return 0;
  265.     }

  266.   *this_cache = cache;
  267.   return 1;
  268. }

  269. /* The initial "sniffer" whether THIS_FRAME is a bottom (callee) frame of a new
  270.    chain to create.  Keep TAILCALL_CACHEP NULL if it did not find any chain,
  271.    initialize it otherwise.  No tail call chain is created if there are no
  272.    unambiguous virtual tail call frames to report.

  273.    ENTRY_CFA_SP_OFFSETP is NULL if no special SP handling is possible,
  274.    otherwise *ENTRY_CFA_SP_OFFSETP is the number of bytes to subtract from tail
  275.    call frames frame base to get the SP value there - to simulate return
  276.    address pushed on the stack.  */

  277. void
  278. dwarf2_tailcall_sniffer_first (struct frame_info *this_frame,
  279.                                void **tailcall_cachep,
  280.                                const LONGEST *entry_cfa_sp_offsetp)
  281. {
  282.   CORE_ADDR prev_pc = 0, prev_sp = 0;        /* GCC warning.  */
  283.   int prev_sp_p = 0;
  284.   CORE_ADDR this_pc;
  285.   struct gdbarch *prev_gdbarch;
  286.   struct call_site_chain *chain = NULL;
  287.   struct tailcall_cache *cache;
  288.   volatile struct gdb_exception except;

  289.   gdb_assert (*tailcall_cachep == NULL);

  290.   /* PC may be after the function if THIS_FRAME calls noreturn function,
  291.      get_frame_address_in_block will decrease it by 1 in such case.  */
  292.   this_pc = get_frame_address_in_block (this_frame);

  293.   /* Catch any unwinding errors.  */
  294.   TRY_CATCH (except, RETURN_MASK_ERROR)
  295.     {
  296.       int sp_regnum;

  297.       prev_gdbarch = frame_unwind_arch (this_frame);

  298.       /* Simulate frame_unwind_pc without setting this_frame->prev_pc.p.  */
  299.       prev_pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);

  300.       /* call_site_find_chain can throw an exception.  */
  301.       chain = call_site_find_chain (prev_gdbarch, prev_pc, this_pc);

  302.       if (entry_cfa_sp_offsetp == NULL)
  303.         break;
  304.       sp_regnum = gdbarch_sp_regnum (prev_gdbarch);
  305.       if (sp_regnum == -1)
  306.         break;
  307.       prev_sp = frame_unwind_register_unsigned (this_frame, sp_regnum);
  308.       prev_sp_p = 1;
  309.     }
  310.   if (except.reason < 0)
  311.     {
  312.       if (entry_values_debug)
  313.         exception_print (gdb_stdout, except);
  314.       return;
  315.     }

  316.   /* Ambiguous unwind or unambiguous unwind verified as matching.  */
  317.   if (chain == NULL || chain->length == 0)
  318.     {
  319.       xfree (chain);
  320.       return;
  321.     }

  322.   cache = cache_new_ref1 (this_frame);
  323.   *tailcall_cachep = cache;
  324.   cache->chain = chain;
  325.   cache->prev_pc = prev_pc;
  326.   cache->chain_levels = pretended_chain_levels (chain);
  327.   cache->prev_sp_p = prev_sp_p;
  328.   if (cache->prev_sp_p)
  329.     {
  330.       cache->prev_sp = prev_sp;
  331.       cache->entry_cfa_sp_offset = *entry_cfa_sp_offsetp;
  332.     }
  333.   gdb_assert (cache->chain_levels > 0);
  334. }

  335. /* Implementation of frame_dealloc_cache_ftype.  It can be called even for the
  336.    bottom chain frame from dwarf2_frame_dealloc_cache which is not a real
  337.    TAILCALL_FRAME.  */

  338. static void
  339. tailcall_frame_dealloc_cache (struct frame_info *self, void *this_cache)
  340. {
  341.   struct tailcall_cache *cache = this_cache;

  342.   cache_unref (cache);
  343. }

  344. /* Implementation of frame_prev_arch_ftype.  We assume all the virtual tail
  345.    call frames have gdbarch of the bottom (callee) frame.  */

  346. static struct gdbarch *
  347. tailcall_frame_prev_arch (struct frame_info *this_frame,
  348.                           void **this_prologue_cache)
  349. {
  350.   struct tailcall_cache *cache = *this_prologue_cache;

  351.   return get_frame_arch (cache->next_bottom_frame);
  352. }

  353. /* Virtual tail call frame unwinder if dwarf2_tailcall_sniffer_first finds
  354.    a chain to create.  */

  355. const struct frame_unwind dwarf2_tailcall_frame_unwind =
  356. {
  357.   TAILCALL_FRAME,
  358.   default_frame_unwind_stop_reason,
  359.   tailcall_frame_this_id,
  360.   tailcall_frame_prev_register,
  361.   NULL,
  362.   tailcall_frame_sniffer,
  363.   tailcall_frame_dealloc_cache,
  364.   tailcall_frame_prev_arch
  365. };

  366. /* Provide a prototype to silence -Wmissing-prototypes.  */
  367. extern initialize_file_ftype _initialize_tailcall_frame;

  368. void
  369. _initialize_tailcall_frame (void)
  370. {
  371.   cache_htab = htab_create_alloc (50, cache_hash, cache_eq, NULL, xcalloc,
  372.                                   xfree);
  373. }