LCOV - code coverage report
Current view: top level - libdwfl - debuginfod-client.c (source / functions) Coverage Total Hit
Test: elfutils-0.194 Lines: 88.9 % 45 40
Test Date: 2026-02-10 23:38:26 Functions: 100.0 % 5 5
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 58.3 % 24 14

             Branch data     Line data    Source code
       1                 :             : /* Try to get an ELF or debug file through the debuginfod.
       2                 :             :    Copyright (C) 2019 Red Hat, Inc.
       3                 :             :    Copyright (C) 2022 Mark J. Wielaard <mark@klomp.org>
       4                 :             :    This file is part of elfutils.
       5                 :             : 
       6                 :             :    This file is free software; you can redistribute it and/or modify
       7                 :             :    it under the terms of either
       8                 :             : 
       9                 :             :      * the GNU Lesser General Public License as published by the Free
      10                 :             :        Software Foundation; either version 3 of the License, or (at
      11                 :             :        your option) any later version
      12                 :             : 
      13                 :             :    or
      14                 :             : 
      15                 :             :      * the GNU General Public License as published by the Free
      16                 :             :        Software Foundation; either version 2 of the License, or (at
      17                 :             :        your option) any later version
      18                 :             : 
      19                 :             :    or both in parallel, as here.
      20                 :             : 
      21                 :             :    elfutils is distributed in the hope that it will be useful, but
      22                 :             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      23                 :             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      24                 :             :    General Public License for more details.
      25                 :             : 
      26                 :             :    You should have received copies of the GNU General Public License and
      27                 :             :    the GNU Lesser General Public License along with this program.  If
      28                 :             :    not, see <http://www.gnu.org/licenses/>.  */
      29                 :             : 
      30                 :             : #ifdef HAVE_CONFIG_H
      31                 :             : # include <config.h>
      32                 :             : #endif
      33                 :             : 
      34                 :             : #include "libdwflP.h"
      35                 :             : 
      36                 :             : #ifdef ENABLE_LIBDEBUGINFOD
      37                 :             : 
      38                 :             : #include "debuginfod.h"
      39                 :             : 
      40                 :             : #include <pthread.h>
      41                 :             : #include <dlfcn.h>
      42                 :             : 
      43                 :             : static __typeof__ (debuginfod_begin) *fp_debuginfod_begin;
      44                 :             : static __typeof__ (debuginfod_find_executable) *fp_debuginfod_find_executable;
      45                 :             : static __typeof__ (debuginfod_find_debuginfo) *fp_debuginfod_find_debuginfo;
      46                 :             : static __typeof__ (debuginfod_end) *fp_debuginfod_end;
      47                 :             : 
      48                 :             : static void __libdwfl_debuginfod_init (void);
      49                 :             : 
      50                 :             : static pthread_once_t init_control = PTHREAD_ONCE_INIT;
      51                 :             : 
      52                 :             : /* dwfl->debuginfod_lock must be held when calling this function.  */
      53                 :             : 
      54                 :             : debuginfod_client *
      55                 :         192 : dwfl_get_debuginfod_client (Dwfl *dwfl)
      56                 :             : {
      57         [ +  + ]:         192 :   if (dwfl->debuginfod != NULL)
      58                 :             :     return dwfl->debuginfod;
      59                 :             : 
      60                 :         104 :   pthread_once (&init_control, __libdwfl_debuginfod_init);
      61                 :             : 
      62         [ +  - ]:         104 :   if (fp_debuginfod_begin != NULL)
      63                 :             :     {
      64                 :         104 :       dwfl->debuginfod = (*fp_debuginfod_begin) ();
      65                 :         104 :       return dwfl->debuginfod;
      66                 :             :     }
      67                 :             : 
      68                 :             :   return NULL;
      69                 :             : }
      70                 :             : INTDEF(dwfl_get_debuginfod_client)
      71                 :             : 
      72                 :             : int
      73                 :          48 : __libdwfl_debuginfod_find_executable (Dwfl *dwfl,
      74                 :             :                                       const unsigned char *build_id_bits,
      75                 :             :                                       size_t build_id_len)
      76                 :             : {
      77                 :          48 :   int fd = -1;
      78         [ -  + ]:          48 :   if (build_id_len > 0)
      79                 :             :     {
      80                 :          48 :       mutex_lock (dwfl->debuginfod_lock);
      81                 :          48 :       debuginfod_client *c = INTUSE (dwfl_get_debuginfod_client) (dwfl);
      82         [ -  + ]:          48 :       if (c != NULL)
      83                 :          48 :         fd = (*fp_debuginfod_find_executable) (c, build_id_bits,
      84                 :             :                                                build_id_len, NULL);
      85                 :          48 :       mutex_unlock (dwfl->debuginfod_lock);
      86                 :             :     }
      87                 :             : 
      88                 :          48 :   return fd;
      89                 :             : }
      90                 :             : 
      91                 :             : int
      92                 :         144 : __libdwfl_debuginfod_find_debuginfo (Dwfl *dwfl,
      93                 :             :                                      const unsigned char *build_id_bits,
      94                 :             :                                      size_t build_id_len)
      95                 :             : {
      96                 :         144 :   int fd = -1;
      97         [ -  + ]:         144 :   if (build_id_len > 0)
      98                 :             :     {
      99                 :         144 :       mutex_lock (dwfl->debuginfod_lock);
     100                 :         144 :       debuginfod_client *c = INTUSE (dwfl_get_debuginfod_client) (dwfl);
     101         [ -  + ]:         144 :       if (c != NULL)
     102                 :         144 :         fd = (*fp_debuginfod_find_debuginfo) (c, build_id_bits,
     103                 :             :                                               build_id_len, NULL);
     104                 :         144 :       mutex_unlock (dwfl->debuginfod_lock);
     105                 :             :     }
     106                 :             : 
     107                 :         144 :   return fd;
     108                 :             : }
     109                 :             : 
     110                 :             : void
     111                 :       11552 : __libdwfl_debuginfod_end (debuginfod_client *c)
     112                 :             : {
     113         [ +  + ]:       11552 :   if (c != NULL)
     114                 :         104 :     (*fp_debuginfod_end) (c);
     115                 :       11552 : }
     116                 :             : 
     117                 :             : /* Try to get the libdebuginfod library functions.
     118                 :             :    Only needs to be called once from dwfl_get_debuginfod_client.  */
     119                 :             : static void
     120                 :         104 : __libdwfl_debuginfod_init (void)
     121                 :             : {
     122                 :         104 :   void *debuginfod_so = dlopen(DEBUGINFOD_SONAME, RTLD_LAZY);
     123                 :             : 
     124         [ +  - ]:         104 :   if (debuginfod_so != NULL)
     125                 :             :     {
     126                 :         104 :       fp_debuginfod_begin = dlsym (debuginfod_so, "debuginfod_begin");
     127                 :         104 :       fp_debuginfod_find_executable = dlsym (debuginfod_so,
     128                 :             :                                              "debuginfod_find_executable");
     129                 :         104 :       fp_debuginfod_find_debuginfo = dlsym (debuginfod_so,
     130                 :             :                                             "debuginfod_find_debuginfo");
     131                 :         104 :       fp_debuginfod_end = dlsym (debuginfod_so, "debuginfod_end");
     132                 :             : 
     133                 :             :       /* We either get them all, or we get none.  */
     134         [ +  - ]:         104 :       if (fp_debuginfod_begin == NULL
     135         [ +  - ]:         104 :           || fp_debuginfod_find_executable == NULL
     136         [ +  - ]:         104 :           || fp_debuginfod_find_debuginfo == NULL
     137         [ -  + ]:         104 :           || fp_debuginfod_end == NULL)
     138                 :             :         {
     139                 :           0 :           fp_debuginfod_begin = NULL;
     140                 :           0 :           fp_debuginfod_find_executable = NULL;
     141                 :           0 :           fp_debuginfod_find_debuginfo = NULL;
     142                 :           0 :           fp_debuginfod_end = NULL;
     143                 :           0 :           dlclose (debuginfod_so);
     144                 :             :         }
     145                 :             :     }
     146                 :         104 : }
     147                 :             : 
     148                 :             : #else // ENABLE_LIBDEBUGINFOD
     149                 :             : 
     150                 :             : debuginfod_client *
     151                 :             : dwfl_get_debuginfod_client (Dwfl *dummy __attribute__ ((unused)))
     152                 :             : {
     153                 :             :   return NULL;
     154                 :             : }
     155                 :             : 
     156                 :             : #endif // ENABLE_LIBDEBUGINFOD
        

Generated by: LCOV version 2.0-1