LCOV - code coverage report
Current view: top level - libdw - dwarf_getscopes.c (source / functions) Hit Total Coverage
Test: elfutils-0.172 Lines: 53 64 82.8 %
Date: 2018-06-11 22:52:14 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Return scope DIEs containing PC address.
       2             :    Copyright (C) 2005, 2007, 2015 Red Hat, Inc.
       3             :    This file is part of elfutils.
       4             : 
       5             :    This file is free software; you can redistribute it and/or modify
       6             :    it under the terms of either
       7             : 
       8             :      * the GNU Lesser General Public License as published by the Free
       9             :        Software Foundation; either version 3 of the License, or (at
      10             :        your option) any later version
      11             : 
      12             :    or
      13             : 
      14             :      * the GNU General Public License as published by the Free
      15             :        Software Foundation; either version 2 of the License, or (at
      16             :        your option) any later version
      17             : 
      18             :    or both in parallel, as here.
      19             : 
      20             :    elfutils is distributed in the hope that it will be useful, but
      21             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      22             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      23             :    General Public License for more details.
      24             : 
      25             :    You should have received copies of the GNU General Public License and
      26             :    the GNU Lesser General Public License along with this program.  If
      27             :    not, see <http://www.gnu.org/licenses/>.  */
      28             : 
      29             : #ifdef HAVE_CONFIG_H
      30             : # include <config.h>
      31             : #endif
      32             : 
      33             : #include <assert.h>
      34             : #include <stdlib.h>
      35             : #include "libdwP.h"
      36             : #include <dwarf.h>
      37             : 
      38             : 
      39             : struct args
      40             : {
      41             :   Dwarf_Addr pc;
      42             :   Dwarf_Die *scopes;
      43             :   unsigned int inlined, nscopes;
      44             :   Dwarf_Die inlined_origin;
      45             : };
      46             : 
      47             : /* Preorder visitor: prune the traversal if this DIE does not contain PC.  */
      48             : static int
      49        1520 : pc_match (unsigned int depth, struct Dwarf_Die_Chain *die, void *arg)
      50             : {
      51        1520 :   struct args *a = arg;
      52             : 
      53        1520 :   if (a->scopes != NULL)
      54         136 :     die->prune = true;
      55             :   else
      56             :     {
      57             :       /* dwarf_haspc returns an error if there are no appropriate attributes.
      58             :          But we use it indiscriminantly instead of presuming which tags can
      59             :          have PC attributes.  So when it fails for that reason, treat it just
      60             :          as a nonmatching return.  */
      61        1384 :       int result = INTUSE(dwarf_haspc) (&die->die, a->pc);
      62        1384 :       if (result < 0)
      63             :         {
      64           0 :           int error = INTUSE(dwarf_errno) ();
      65           0 :           if (error != DWARF_E_NOERROR
      66           0 :               && error != DWARF_E_NO_DEBUG_RANGES
      67           0 :               && error != DWARF_E_NO_DEBUG_RNGLISTS)
      68             :             {
      69           0 :               __libdw_seterrno (error);
      70           0 :               return -1;
      71             :             }
      72             :           result = 0;
      73             :         }
      74        1384 :       if (result == 0)
      75        1065 :         die->prune = true;
      76             : 
      77        1384 :       if (!die->prune
      78         319 :           && INTUSE (dwarf_tag) (&die->die) == DW_TAG_inlined_subroutine)
      79         119 :         a->inlined = depth;
      80             :     }
      81             : 
      82             :   return 0;
      83             : }
      84             : 
      85             : /* Preorder visitor for second partial traversal after finding a
      86             :    concrete inlined instance.  */
      87             : static int
      88         664 : origin_match (unsigned int depth, struct Dwarf_Die_Chain *die, void *arg)
      89             : {
      90         664 :   struct args *a = arg;
      91             : 
      92         664 :   if (die->die.addr != a->inlined_origin.addr)
      93             :     return 0;
      94             : 
      95             :   /* We have a winner!  This is the abstract definition of the inline
      96             :      function of which A->scopes[A->nscopes - 1] is a concrete instance.
      97             :   */
      98             : 
      99          79 :   unsigned int nscopes = a->nscopes + depth;
     100          79 :   Dwarf_Die *scopes = realloc (a->scopes, nscopes * sizeof scopes[0]);
     101          79 :   if (scopes == NULL)
     102             :     {
     103           0 :       free (a->scopes);
     104           0 :       __libdw_seterrno (DWARF_E_NOMEM);
     105           0 :       return -1;
     106             :     }
     107             : 
     108          79 :   a->scopes = scopes;
     109             :   do
     110             :     {
     111          79 :       die = die->parent;
     112          79 :       scopes[a->nscopes++] = die->die;
     113             :     }
     114          79 :   while (a->nscopes < nscopes);
     115          79 :   assert (die->parent == NULL);
     116          79 :   return a->nscopes;
     117             : }
     118             : 
     119             : /* Postorder visitor: first (innermost) call wins.  */
     120             : static int
     121        1520 : pc_record (unsigned int depth, struct Dwarf_Die_Chain *die, void *arg)
     122             : {
     123        1520 :   struct args *a = arg;
     124             : 
     125        1520 :   if (die->prune)
     126             :     return 0;
     127             : 
     128         319 :   if (a->scopes == NULL)
     129             :     {
     130             :       /* We have hit the innermost DIE that contains the target PC.  */
     131             : 
     132         182 :       a->nscopes = depth + 1 - a->inlined;
     133         182 :       a->scopes = malloc (a->nscopes * sizeof a->scopes[0]);
     134         182 :       if (a->scopes == NULL)
     135             :         {
     136           0 :           __libdw_seterrno (DWARF_E_NOMEM);
     137           0 :           return -1;
     138             :         }
     139             : 
     140         285 :       for (unsigned int i = 0; i < a->nscopes; ++i)
     141             :         {
     142         285 :           a->scopes[i] = die->die;
     143         285 :           die = die->parent;
     144             :         }
     145             : 
     146         182 :       if (a->inlined == 0)
     147             :         {
     148         103 :           assert (die == NULL);
     149         103 :           return a->nscopes;
     150             :         }
     151             : 
     152             :       /* This is the concrete inlined instance itself.
     153             :          Record its abstract_origin pointer.  */
     154          79 :       Dwarf_Die *const inlinedie = &a->scopes[depth - a->inlined];
     155             : 
     156          79 :       assert (INTUSE (dwarf_tag) (inlinedie) == DW_TAG_inlined_subroutine);
     157             :       Dwarf_Attribute attr_mem;
     158          79 :       Dwarf_Attribute *attr = INTUSE (dwarf_attr) (inlinedie,
     159             :                                                    DW_AT_abstract_origin,
     160             :                                                    &attr_mem);
     161          79 :       if (INTUSE (dwarf_formref_die) (attr, &a->inlined_origin) == NULL)
     162             :         return -1;
     163          79 :       return 0;
     164             :     }
     165             : 
     166             : 
     167             :   /* We've recorded the scopes back to one that is a concrete inlined
     168             :      instance.  Now return out of the traversal back to the scope
     169             :      containing that instance.  */
     170             : 
     171         137 :   assert (a->inlined);
     172         137 :   if (depth >= a->inlined)
     173             :     /* Not there yet.  */
     174             :     return 0;
     175             : 
     176             :   /* Now we are in a scope that contains the concrete inlined instance.
     177             :      Search it for the inline function's abstract definition.
     178             :      If we don't find it, return to search the containing scope.
     179             :      If we do find it, the nonzero return value will bail us out
     180             :      of the postorder traversal.  */
     181         137 :   return __libdw_visit_scopes (depth, die, NULL, &origin_match, NULL, a);
     182             : }
     183             : 
     184             : 
     185             : int
     186         188 : dwarf_getscopes (Dwarf_Die *cudie, Dwarf_Addr pc, Dwarf_Die **scopes)
     187             : {
     188         188 :   if (cudie == NULL)
     189             :     return -1;
     190             : 
     191         182 :   struct Dwarf_Die_Chain cu = { .parent = NULL, .die = *cudie };
     192         182 :   struct args a = { .pc = pc };
     193             : 
     194         182 :   int result = __libdw_visit_scopes (0, &cu, NULL, &pc_match, &pc_record, &a);
     195             : 
     196         182 :   if (result == 0 && a.scopes != NULL)
     197          79 :     result = __libdw_visit_scopes (0, &cu, NULL, &origin_match, NULL, &a);
     198             : 
     199         182 :   if (result > 0)
     200         182 :     *scopes = a.scopes;
     201             : 
     202             :   return result;
     203             : }

Generated by: LCOV version 1.13