LCOV - code coverage report
Current view: top level - libdwfl - dwfl_getmodules.c (source / functions) Hit Total Coverage
Test: elfutils-0.191 Lines: 18 25 72.0 %
Date: 2024-08-30 14:58:23 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 15 28 53.6 %

           Branch data     Line data    Source code
       1                 :            : /* Iterate through modules.
       2                 :            :    Copyright (C) 2005, 2008 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 "libdwflP.h"
      34                 :            : 
      35                 :            : ptrdiff_t
      36                 :       2142 : dwfl_getmodules (Dwfl *dwfl,
      37                 :            :                  int (*callback) (Dwfl_Module *, void **,
      38                 :            :                                   const char *, Dwarf_Addr, void *),
      39                 :            :                  void *arg,
      40                 :            :                  ptrdiff_t offset)
      41                 :            : {
      42         [ +  - ]:       2142 :   if (dwfl == NULL)
      43                 :            :     return -1;
      44                 :            : 
      45                 :            :   /* We iterate through the linked list when it's all we have.
      46                 :            :      But continuing from an offset is slow that way.  So when
      47                 :            :      DWFL->lookup_module is populated, we can instead keep our
      48                 :            :      place by jumping directly into the array.  Since the actions
      49                 :            :      of a callback could cause it to get populated, we must
      50                 :            :      choose the style of place-holder when we return an offset,
      51                 :            :      and we encode the choice in the low bits of that value.  */
      52                 :            : 
      53                 :       2142 :   Dwfl_Module *m = dwfl->modulelist;
      54                 :            : 
      55         [ +  + ]:       2142 :   if ((offset & 3) == 1)
      56                 :            :     {
      57                 :         54 :       offset >>= 2;
      58         [ +  + ]:        266 :       for (ptrdiff_t pos = 0; pos < offset; ++pos)
      59         [ +  - ]:        212 :         if (m == NULL)
      60                 :            :           return -1;
      61                 :            :         else
      62                 :        212 :           m = m->next;
      63                 :            :     }
      64   [ -  +  -  - ]:       2088 :   else if (((offset & 3) == 2) && likely (dwfl->lookup_module != NULL))
      65                 :            :     {
      66                 :          0 :       offset >>= 2;
      67                 :            : 
      68         [ #  # ]:          0 :       if ((size_t) offset - 1 == dwfl->lookup_elts)
      69                 :            :         return 0;
      70                 :            : 
      71         [ #  # ]:          0 :       if (unlikely ((size_t) offset - 1 > dwfl->lookup_elts))
      72                 :            :         return -1;
      73                 :            : 
      74                 :          0 :       m = dwfl->lookup_module[offset - 1];
      75         [ #  # ]:          0 :       if (unlikely (m == NULL))
      76                 :            :         return -1;
      77                 :            :     }
      78         [ -  + ]:       2088 :   else if (offset != 0)
      79                 :            :     {
      80                 :          0 :       __libdwfl_seterrno (DWFL_E_BADSTROFF);
      81                 :          0 :       return -1;
      82                 :            :     }
      83                 :            : 
      84         [ +  + ]:       4192 :   while (m != NULL)
      85                 :            :     {
      86                 :       2216 :       int ok = (*callback) (MODCB_ARGS (m), arg);
      87                 :       2216 :       ++offset;
      88                 :       2216 :       m = m->next;
      89         [ +  + ]:       2216 :       if (ok != DWARF_CB_OK)
      90                 :        284 :         return ((dwfl->lookup_module == NULL) ? ((offset << 2) | 1)
      91   [ +  +  +  - ]:        166 :                 : (((m == NULL ? (ptrdiff_t) dwfl->lookup_elts + 1
      92                 :         48 :                      : m->segment + 1) << 2) | 2));
      93                 :            :     }
      94                 :            :   return 0;
      95                 :            : }
      96                 :            : INTDEF (dwfl_getmodules)

Generated by: LCOV version 1.16