LCOV - code coverage report
Current view: top level - libdw - libdw_visit_scopes.c (source / functions) Hit Total Coverage
Test: elfutils-0.189 Lines: 49 53 92.5 %
Date: 2023-08-29 13:46:25 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 33 44 75.0 %

           Branch data     Line data    Source code
       1                 :            : /* Helper functions to descend DWARF scope trees.
       2                 :            :    Copyright (C) 2005,2006,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 "libdwP.h"
      34                 :            : #include <dwarf.h>
      35                 :            : 
      36                 :            : 
      37                 :            : static bool
      38                 :    7753359 : may_have_scopes (Dwarf_Die *die)
      39                 :            : {
      40         [ +  + ]:    7753359 :   switch (INTUSE(dwarf_tag) (die))
      41                 :            :     {
      42                 :            :       /* DIEs with addresses we can try to match.  */
      43                 :            :     case DW_TAG_compile_unit:
      44                 :            :     case DW_TAG_module:
      45                 :            :     case DW_TAG_lexical_block:
      46                 :            :     case DW_TAG_with_stmt:
      47                 :            :     case DW_TAG_catch_block:
      48                 :            :     case DW_TAG_try_block:
      49                 :            :     case DW_TAG_entry_point:
      50                 :            :     case DW_TAG_inlined_subroutine:
      51                 :            :     case DW_TAG_subprogram:
      52                 :            :       return true;
      53                 :            : 
      54                 :            :       /* DIEs without addresses that can own DIEs with addresses.  */
      55                 :            :     case DW_TAG_namespace:
      56                 :            :     case DW_TAG_class_type:
      57                 :            :     case DW_TAG_structure_type:
      58                 :            :       return true;
      59                 :            : 
      60                 :            :       /* Other DIEs we have no reason to descend.  */
      61                 :            :     default:
      62                 :    6318109 :       break;
      63                 :            :     }
      64                 :    6318109 :   return false;
      65                 :            : }
      66                 :            : 
      67                 :            : struct walk_children_state
      68                 :            : {
      69                 :            :   /* Parameters of __libdw_visit_scopes. */
      70                 :            :   unsigned int depth;
      71                 :            :   struct Dwarf_Die_Chain *imports;
      72                 :            :   int (*previsit) (unsigned int depth, struct Dwarf_Die_Chain *, void *);
      73                 :            :   int (*postvisit) (unsigned int depth, struct Dwarf_Die_Chain *, void *);
      74                 :            :   void *arg;
      75                 :            :   /* Extra local variables for the walker. */
      76                 :            :   struct Dwarf_Die_Chain child;
      77                 :            : };
      78                 :            : 
      79                 :            : static inline int
      80                 :            : walk_children (struct walk_children_state *state);
      81                 :            : 
      82                 :            : int
      83                 :            : internal_function
      84                 :    1412481 : __libdw_visit_scopes (unsigned int depth, struct Dwarf_Die_Chain *root,
      85                 :            :                       struct Dwarf_Die_Chain *imports,
      86                 :            :                       int (*previsit) (unsigned int,
      87                 :            :                                        struct Dwarf_Die_Chain *,
      88                 :            :                                        void *),
      89                 :            :                       int (*postvisit) (unsigned int,
      90                 :            :                                         struct Dwarf_Die_Chain *,
      91                 :            :                                         void *),
      92                 :            :                       void *arg)
      93                 :            : {
      94                 :    1412481 :   struct walk_children_state state =
      95                 :            :     {
      96                 :            :       .depth = depth,
      97                 :            :       .imports = imports,
      98                 :            :       .previsit = previsit,
      99                 :            :       .postvisit = postvisit,
     100                 :            :       .arg = arg
     101                 :            :     };
     102                 :            : 
     103                 :    1412481 :   state.child.parent = root;
     104                 :    1412481 :   int ret;
     105         [ +  + ]:    1412481 :   if ((ret = INTUSE(dwarf_child) (&root->die, &state.child.die)) != 0)
     106         [ +  - ]:          2 :     return ret < 0 ? -1 : 0; // Having zero children is legal.
     107                 :            : 
     108                 :    1412479 :   return walk_children (&state);
     109                 :            : }
     110                 :            : 
     111                 :            : static inline int
     112                 :    1412495 : walk_children (struct walk_children_state *state)
     113                 :            : {
     114                 :    9106879 :   int ret;
     115                 :    9106879 :   do
     116                 :            :     {
     117                 :            :       /* For an imported unit, it is logically as if the children of
     118                 :            :          that unit are siblings of the other children.  So don't do
     119                 :            :          a full recursion into the imported unit, but just walk the
     120                 :            :          children in place before moving to the next real child.  */
     121         [ +  + ]:    9106901 :       while (INTUSE(dwarf_tag) (&state->child.die) == DW_TAG_imported_unit)
     122                 :            :         {
     123                 :         26 :           Dwarf_Die orig_child_die = state->child.die;
     124                 :         26 :           Dwarf_Attribute attr_mem;
     125                 :         26 :           Dwarf_Attribute *attr = INTUSE(dwarf_attr) (&state->child.die,
     126                 :            :                                                       DW_AT_import,
     127                 :            :                                                       &attr_mem);
     128                 :            :           /* Some gcc -flto versions imported other top-level compile units,
     129                 :            :              skip those.  */
     130         [ +  - ]:         26 :           if (INTUSE(dwarf_formref_die) (attr, &state->child.die) != NULL
     131         [ +  + ]:         26 :               && INTUSE(dwarf_tag) (&state->child.die) != DW_TAG_compile_unit
     132         [ +  - ]:         16 :               && (INTUSE(dwarf_child) (&state->child.die, &state->child.die)
     133                 :            :                   == 0))
     134                 :            :             {
     135                 :            :               /* Checks the given DIE hasn't been imported yet
     136                 :            :                  to prevent cycles.  */
     137                 :         16 :               bool imported = false;
     138         [ -  + ]:         16 :               for (struct Dwarf_Die_Chain *import = state->imports; import != NULL;
     139                 :          0 :                 import = import->parent)
     140         [ #  # ]:          0 :                 if (import->die.addr == orig_child_die.addr)
     141                 :            :                   {
     142                 :            :                     imported = true;
     143                 :            :                     break;
     144                 :            :                   }
     145         [ -  + ]:         16 :               if (imported)
     146                 :            :                 {
     147                 :          0 :                   __libdw_seterrno (DWARF_E_INVALID_DWARF);
     148                 :          4 :                   return -1;
     149                 :            :                 }
     150                 :         16 :               struct Dwarf_Die_Chain *orig_imports = state->imports;
     151                 :         16 :               struct Dwarf_Die_Chain import = { .die = orig_child_die,
     152                 :            :                                                 .parent = orig_imports };
     153                 :         16 :               state->imports = &import;
     154                 :         16 :               int result = walk_children (state);
     155                 :         16 :               state->imports = orig_imports;
     156         [ +  + ]:         16 :               if (result != DWARF_CB_OK)
     157                 :          4 :                 return result;
     158                 :            :             }
     159                 :            : 
     160                 :            :           /* Any "real" children left?  */
     161         [ -  + ]:         22 :           if ((ret = INTUSE(dwarf_siblingof) (&orig_child_die,
     162                 :            :                                               &state->child.die)) != 0)
     163         [ #  # ]:          0 :             return ret < 0 ? -1 : 0;
     164                 :    9106875 :         };
     165                 :            : 
     166                 :    9106875 :         state->child.prune = false;
     167                 :            : 
     168                 :            :         /* previsit is declared NN */
     169                 :    9106875 :         int result = (*state->previsit) (state->depth + 1, &state->child, state->arg);
     170         [ +  + ]:    9106875 :         if (result != DWARF_CB_OK)
     171                 :       7667 :           return result;
     172                 :            : 
     173   [ +  +  +  + ]:    9099208 :         if (!state->child.prune && may_have_scopes (&state->child.die)
     174         [ +  + ]:    1435250 :             && INTUSE(dwarf_haschildren) (&state->child.die))
     175                 :            :           {
     176                 :    1394777 :             result = __libdw_visit_scopes (state->depth + 1, &state->child, state->imports,
     177                 :            :                                            state->previsit, state->postvisit, state->arg);
     178         [ +  + ]:    1394777 :             if (result != DWARF_CB_OK)
     179                 :      20460 :               return result;
     180                 :            :           }
     181                 :            : 
     182         [ +  + ]:    9078748 :         if (state->postvisit != NULL)
     183                 :            :           {
     184                 :       1935 :             result = (*state->postvisit) (state->depth + 1, &state->child, state->arg);
     185         [ +  + ]:       1935 :             if (result != DWARF_CB_OK)
     186                 :        252 :               return result;
     187                 :            :           }
     188                 :            :     }
     189         [ +  + ]:    9078496 :   while ((ret = INTUSE(dwarf_siblingof) (&state->child.die, &state->child.die)) == 0);
     190                 :            : 
     191         [ +  - ]:    1384112 :   return ret < 0 ? -1 : 0;
     192                 :            : }

Generated by: LCOV version 1.16