GDB (API)
/home/stan/gdb/src/gdb/dwarf2-frame-tailcall.c
Go to the documentation of this file.
00001 /* Virtual tail call frames unwinder for GDB.
00002 
00003    Copyright (C) 2010-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdb_assert.h"
00022 #include "frame.h"
00023 #include "dwarf2-frame-tailcall.h"
00024 #include "dwarf2loc.h"
00025 #include "frame-unwind.h"
00026 #include "block.h"
00027 #include "hashtab.h"
00028 #include "exceptions.h"
00029 #include "gdbtypes.h"
00030 #include "regcache.h"
00031 #include "value.h"
00032 #include "dwarf2-frame.h"
00033 
00034 /* Contains struct tailcall_cache indexed by next_bottom_frame.  */
00035 static htab_t cache_htab;
00036 
00037 /* Associate structure of the unwinder to call_site_chain.  Lifetime of this
00038    structure is maintained by REFC decremented by dealloc_cache, all of them
00039    get deleted during reinit_frame_cache.  */
00040 struct tailcall_cache
00041 {
00042   /* It must be the first one of this struct.  It is the furthest callee.  */
00043   struct frame_info *next_bottom_frame;
00044 
00045   /* Reference count.  The whole chain of virtual tail call frames shares one
00046      tailcall_cache.  */
00047   int refc;
00048 
00049   /* Associated found virtual taill call frames chain, it is never NULL.  */
00050   struct call_site_chain *chain;
00051 
00052   /* Cached pretended_chain_levels result.  */
00053   int chain_levels;
00054 
00055   /* Unwound PC from the top (caller) frame, as it is not contained
00056      in CHAIN.  */
00057   CORE_ADDR prev_pc;
00058 
00059   /* Compensate SP in caller frames appropriately.  prev_sp and
00060      entry_cfa_sp_offset are valid only if PREV_SP_P.  PREV_SP is SP at the top
00061      (caller) frame.  ENTRY_CFA_SP_OFFSET is shift of SP in tail call frames
00062      against next_bottom_frame SP.  */
00063   unsigned prev_sp_p : 1;
00064   CORE_ADDR prev_sp;
00065   LONGEST entry_cfa_sp_offset;
00066 };
00067 
00068 /* hash_f for htab_create_alloc of cache_htab.  */
00069 
00070 static hashval_t
00071 cache_hash (const void *arg)
00072 {
00073   const struct tailcall_cache *cache = arg;
00074 
00075   return htab_hash_pointer (cache->next_bottom_frame);
00076 }
00077 
00078 /* eq_f for htab_create_alloc of cache_htab.  */
00079 
00080 static int
00081 cache_eq (const void *arg1, const void *arg2)
00082 {
00083   const struct tailcall_cache *cache1 = arg1;
00084   const struct tailcall_cache *cache2 = arg2;
00085 
00086   return cache1->next_bottom_frame == cache2->next_bottom_frame;
00087 }
00088 
00089 /* Create new tailcall_cache for NEXT_BOTTOM_FRAME, NEXT_BOTTOM_FRAME must not
00090    yet have been indexed by cache_htab.  Caller holds one reference of the new
00091    tailcall_cache.  */
00092 
00093 static struct tailcall_cache *
00094 cache_new_ref1 (struct frame_info *next_bottom_frame)
00095 {
00096   struct tailcall_cache *cache;
00097   void **slot;
00098 
00099   cache = xzalloc (sizeof (*cache));
00100 
00101   cache->next_bottom_frame = next_bottom_frame;
00102   cache->refc = 1;
00103 
00104   slot = htab_find_slot (cache_htab, cache, INSERT);
00105   gdb_assert (*slot == NULL);
00106   *slot = cache;
00107 
00108   return cache;
00109 }
00110 
00111 /* Create new reference to CACHE.  */
00112 
00113 static void
00114 cache_ref (struct tailcall_cache *cache)
00115 {
00116   gdb_assert (cache->refc > 0);
00117 
00118   cache->refc++;
00119 }
00120 
00121 /* Drop reference to CACHE, possibly fully freeing it and unregistering it from
00122    cache_htab.  */
00123 
00124 static void
00125 cache_unref (struct tailcall_cache *cache)
00126 {
00127   gdb_assert (cache->refc > 0);
00128 
00129   if (!--cache->refc)
00130     {
00131       gdb_assert (htab_find_slot (cache_htab, cache, NO_INSERT) != NULL);
00132       htab_remove_elt (cache_htab, cache);
00133 
00134       xfree (cache->chain);
00135       xfree (cache);
00136     }
00137 }
00138 
00139 /* Return 1 if FI is a non-bottom (not the callee) tail call frame.  Otherwise
00140    return 0.  */
00141 
00142 static int
00143 frame_is_tailcall (struct frame_info *fi)
00144 {
00145   return frame_unwinder_is (fi, &dwarf2_tailcall_frame_unwind);
00146 }
00147 
00148 /* Try to find tailcall_cache in cache_htab if FI is a part of its virtual tail
00149    call chain.  Otherwise return NULL.  No new reference is created.  */
00150 
00151 static struct tailcall_cache *
00152 cache_find (struct frame_info *fi)
00153 {
00154   struct tailcall_cache *cache;
00155   void **slot;
00156 
00157   while (frame_is_tailcall (fi))
00158     {
00159       fi = get_next_frame (fi);
00160       gdb_assert (fi != NULL);
00161     }
00162 
00163   slot = htab_find_slot (cache_htab, &fi, NO_INSERT);
00164   if (slot == NULL)
00165     return NULL;
00166 
00167   cache = *slot;
00168   gdb_assert (cache != NULL);
00169   return cache;
00170 }
00171 
00172 /* Number of virtual frames between THIS_FRAME and CACHE->NEXT_BOTTOM_FRAME.
00173    If THIS_FRAME is CACHE-> NEXT_BOTTOM_FRAME return -1.  */
00174 
00175 static int
00176 existing_next_levels (struct frame_info *this_frame,
00177                       struct tailcall_cache *cache)
00178 {
00179   int retval = (frame_relative_level (this_frame)
00180                 - frame_relative_level (cache->next_bottom_frame) - 1);
00181 
00182   gdb_assert (retval >= -1);
00183 
00184   return retval;
00185 }
00186 
00187 /* The number of virtual tail call frames in CHAIN.  With no virtual tail call
00188    frames the function would return 0 (but CHAIN does not exist in such
00189    case).  */
00190 
00191 static int
00192 pretended_chain_levels (struct call_site_chain *chain)
00193 {
00194   int chain_levels;
00195 
00196   gdb_assert (chain != NULL);
00197 
00198   if (chain->callers == chain->length && chain->callees == chain->length)
00199     return chain->length;
00200 
00201   chain_levels = chain->callers + chain->callees;
00202   gdb_assert (chain_levels < chain->length);
00203 
00204   return chain_levels;
00205 }
00206 
00207 /* Implementation of frame_this_id_ftype.  THIS_CACHE must be already
00208    initialized with tailcall_cache, THIS_FRAME must be a part of THIS_CACHE.
00209 
00210    Specific virtual tail call frames are tracked by INLINE_DEPTH.  */
00211 
00212 static void
00213 tailcall_frame_this_id (struct frame_info *this_frame, void **this_cache,
00214                         struct frame_id *this_id)
00215 {
00216   struct tailcall_cache *cache = *this_cache;
00217   struct frame_info *next_frame;
00218 
00219   /* Tail call does not make sense for a sentinel frame.  */
00220   next_frame = get_next_frame (this_frame);
00221   gdb_assert (next_frame != NULL);
00222 
00223   *this_id = get_frame_id (next_frame);
00224   (*this_id).code_addr = get_frame_pc (this_frame);
00225   (*this_id).code_addr_p = 1;
00226   (*this_id).artificial_depth = (cache->chain_levels
00227                                  - existing_next_levels (this_frame, cache));
00228   gdb_assert ((*this_id).artificial_depth > 0);
00229 }
00230 
00231 /* Find PC to be unwound from THIS_FRAME.  THIS_FRAME must be a part of
00232    CACHE.  */
00233 
00234 static CORE_ADDR
00235 pretend_pc (struct frame_info *this_frame, struct tailcall_cache *cache)
00236 {
00237   int next_levels = existing_next_levels (this_frame, cache);
00238   struct call_site_chain *chain = cache->chain;
00239 
00240   gdb_assert (chain != NULL);
00241 
00242   next_levels++;
00243   gdb_assert (next_levels >= 0);
00244 
00245   if (next_levels < chain->callees)
00246     return chain->call_site[chain->length - next_levels - 1]->pc;
00247   next_levels -= chain->callees;
00248 
00249   /* Otherwise CHAIN->CALLEES are already covered by CHAIN->CALLERS.  */
00250   if (chain->callees != chain->length)
00251     {
00252       if (next_levels < chain->callers)
00253         return chain->call_site[chain->callers - next_levels - 1]->pc;
00254       next_levels -= chain->callers;
00255     }
00256 
00257   gdb_assert (next_levels == 0);
00258   return cache->prev_pc;
00259 }
00260 
00261 /* Implementation of frame_prev_register_ftype.  If no specific register
00262    override is supplied NULL is returned (this is incompatible with
00263    frame_prev_register_ftype semantics).  next_bottom_frame and tail call
00264    frames unwind the NULL case differently.  */
00265 
00266 struct value *
00267 dwarf2_tailcall_prev_register_first (struct frame_info *this_frame,
00268                                      void **tailcall_cachep, int regnum)
00269 {
00270   struct gdbarch *this_gdbarch = get_frame_arch (this_frame);
00271   struct tailcall_cache *cache = *tailcall_cachep;
00272   CORE_ADDR addr;
00273 
00274   if (regnum == gdbarch_pc_regnum (this_gdbarch))
00275     addr = pretend_pc (this_frame, cache);
00276   else if (cache->prev_sp_p && regnum == gdbarch_sp_regnum (this_gdbarch))
00277     {
00278       int next_levels = existing_next_levels (this_frame, cache);
00279 
00280       if (next_levels == cache->chain_levels - 1)
00281         addr = cache->prev_sp;
00282       else
00283         addr = dwarf2_frame_cfa (this_frame) - cache->entry_cfa_sp_offset;
00284     }
00285   else
00286     return NULL;
00287 
00288   return frame_unwind_got_address (this_frame, regnum, addr);
00289 }
00290 
00291 /* Implementation of frame_prev_register_ftype for tail call frames.  Register
00292    set of virtual tail call frames is assumed to be the one of the top (caller)
00293    frame - assume unchanged register value for NULL from
00294    dwarf2_tailcall_prev_register_first.  */
00295 
00296 static struct value *
00297 tailcall_frame_prev_register (struct frame_info *this_frame,
00298                                void **this_cache, int regnum)
00299 {
00300   struct tailcall_cache *cache = *this_cache;
00301   struct value *val;
00302 
00303   gdb_assert (this_frame != cache->next_bottom_frame);
00304 
00305   val = dwarf2_tailcall_prev_register_first (this_frame, this_cache, regnum);
00306   if (val)
00307     return val;
00308 
00309   return frame_unwind_got_register (this_frame, regnum, regnum);
00310 }
00311 
00312 /* Implementation of frame_sniffer_ftype.  It will never find a new chain, use
00313    dwarf2_tailcall_sniffer_first for the bottom (callee) frame.  It will find
00314    all the predecessing virtual tail call frames, it will return false when
00315    there exist no more tail call frames in this chain.  */
00316 
00317 static int
00318 tailcall_frame_sniffer (const struct frame_unwind *self,
00319                          struct frame_info *this_frame, void **this_cache)
00320 {
00321   struct frame_info *next_frame;
00322   int next_levels;
00323   struct tailcall_cache *cache;
00324 
00325   /* Inner tail call element does not make sense for a sentinel frame.  */
00326   next_frame = get_next_frame (this_frame);
00327   if (next_frame == NULL)
00328     return 0;
00329 
00330   cache = cache_find (next_frame);
00331   if (cache == NULL)
00332     return 0;
00333 
00334   cache_ref (cache);
00335 
00336   next_levels = existing_next_levels (this_frame, cache);
00337 
00338   /* NEXT_LEVELS is -1 only in dwarf2_tailcall_sniffer_first.  */
00339   gdb_assert (next_levels >= 0);
00340   gdb_assert (next_levels <= cache->chain_levels);
00341 
00342   if (next_levels == cache->chain_levels)
00343     {
00344       cache_unref (cache);
00345       return 0;
00346     }
00347 
00348   *this_cache = cache;
00349   return 1;
00350 }
00351 
00352 /* The initial "sniffer" whether THIS_FRAME is a bottom (callee) frame of a new
00353    chain to create.  Keep TAILCALL_CACHEP NULL if it did not find any chain,
00354    initialize it otherwise.  No tail call chain is created if there are no
00355    unambiguous virtual tail call frames to report.
00356    
00357    ENTRY_CFA_SP_OFFSETP is NULL if no special SP handling is possible,
00358    otherwise *ENTRY_CFA_SP_OFFSETP is the number of bytes to subtract from tail
00359    call frames frame base to get the SP value there - to simulate return
00360    address pushed on the stack.  */
00361 
00362 void
00363 dwarf2_tailcall_sniffer_first (struct frame_info *this_frame,
00364                                void **tailcall_cachep,
00365                                const LONGEST *entry_cfa_sp_offsetp)
00366 {
00367   CORE_ADDR prev_pc = 0, prev_sp = 0;   /* GCC warning.  */
00368   int prev_sp_p = 0;
00369   CORE_ADDR this_pc;
00370   struct gdbarch *prev_gdbarch;
00371   struct call_site_chain *chain = NULL;
00372   struct tailcall_cache *cache;
00373   volatile struct gdb_exception except;
00374 
00375   gdb_assert (*tailcall_cachep == NULL);
00376 
00377   /* PC may be after the function if THIS_FRAME calls noreturn function,
00378      get_frame_address_in_block will decrease it by 1 in such case.  */
00379   this_pc = get_frame_address_in_block (this_frame);
00380 
00381   /* Catch any unwinding errors.  */
00382   TRY_CATCH (except, RETURN_MASK_ERROR)
00383     {
00384       int sp_regnum;
00385 
00386       prev_gdbarch = frame_unwind_arch (this_frame);
00387 
00388       /* Simulate frame_unwind_pc without setting this_frame->prev_pc.p.  */
00389       prev_pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
00390 
00391       /* call_site_find_chain can throw an exception.  */
00392       chain = call_site_find_chain (prev_gdbarch, prev_pc, this_pc);
00393 
00394       if (entry_cfa_sp_offsetp == NULL)
00395         break;
00396       sp_regnum = gdbarch_sp_regnum (prev_gdbarch);
00397       if (sp_regnum == -1)
00398         break;
00399       prev_sp = frame_unwind_register_unsigned (this_frame, sp_regnum);
00400       prev_sp_p = 1;
00401     }
00402   if (except.reason < 0)
00403     {
00404       if (entry_values_debug)
00405         exception_print (gdb_stdout, except);
00406       return;
00407     }
00408 
00409   /* Ambiguous unwind or unambiguous unwind verified as matching.  */
00410   if (chain == NULL || chain->length == 0)
00411     {
00412       xfree (chain);
00413       return;
00414     }
00415 
00416   cache = cache_new_ref1 (this_frame);
00417   *tailcall_cachep = cache;
00418   cache->chain = chain;
00419   cache->prev_pc = prev_pc;
00420   cache->chain_levels = pretended_chain_levels (chain);
00421   cache->prev_sp_p = prev_sp_p;
00422   if (cache->prev_sp_p)
00423     {
00424       cache->prev_sp = prev_sp;
00425       cache->entry_cfa_sp_offset = *entry_cfa_sp_offsetp;
00426     }
00427   gdb_assert (cache->chain_levels > 0);
00428 }
00429 
00430 /* Implementation of frame_dealloc_cache_ftype.  It can be called even for the
00431    bottom chain frame from dwarf2_frame_dealloc_cache which is not a real
00432    TAILCALL_FRAME.  */
00433 
00434 static void
00435 tailcall_frame_dealloc_cache (struct frame_info *self, void *this_cache)
00436 {
00437   struct tailcall_cache *cache = this_cache;
00438 
00439   cache_unref (cache);
00440 }
00441 
00442 /* Implementation of frame_prev_arch_ftype.  We assume all the virtual tail
00443    call frames have gdbarch of the bottom (callee) frame.  */
00444 
00445 static struct gdbarch *
00446 tailcall_frame_prev_arch (struct frame_info *this_frame,
00447                           void **this_prologue_cache)
00448 {
00449   struct tailcall_cache *cache = *this_prologue_cache;
00450 
00451   return get_frame_arch (cache->next_bottom_frame);
00452 }
00453 
00454 /* Virtual tail call frame unwinder if dwarf2_tailcall_sniffer_first finds
00455    a chain to create.  */
00456 
00457 const struct frame_unwind dwarf2_tailcall_frame_unwind =
00458 {
00459   TAILCALL_FRAME,
00460   default_frame_unwind_stop_reason,
00461   tailcall_frame_this_id,
00462   tailcall_frame_prev_register,
00463   NULL,
00464   tailcall_frame_sniffer,
00465   tailcall_frame_dealloc_cache,
00466   tailcall_frame_prev_arch
00467 };
00468 
00469 /* Provide a prototype to silence -Wmissing-prototypes.  */
00470 extern initialize_file_ftype _initialize_tailcall_frame;
00471 
00472 void
00473 _initialize_tailcall_frame (void)
00474 {
00475   cache_htab = htab_create_alloc (50, cache_hash, cache_eq, NULL, xcalloc,
00476                                   xfree);
00477 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines