LCOV - code coverage report
Current view: top level - libdwfl - dwfl_build_id_find_debuginfo.c (source / functions) Hit Total Coverage
Test: elfutils-0.174 Lines: 17 39 43.6 %
Date: 2018-09-14 13:30:33 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Find the debuginfo file for a module from its build ID.
       2             :    Copyright (C) 2007, 2009, 2014 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             : #include <unistd.h>
      35             : 
      36             : 
      37             : int
      38          96 : dwfl_build_id_find_debuginfo (Dwfl_Module *mod,
      39             :                               void **userdata __attribute__ ((unused)),
      40             :                               const char *modname __attribute__ ((unused)),
      41             :                               Dwarf_Addr base __attribute__ ((unused)),
      42             :                               const char *file __attribute__ ((unused)),
      43             :                               const char *debuglink __attribute__ ((unused)),
      44             :                               GElf_Word crc __attribute__ ((unused)),
      45             :                               char **debuginfo_file_name)
      46             : {
      47          96 :   int fd = -1;
      48             : 
      49             :   /* Are we looking for a separate debug file for the main file or for
      50             :      an alternate (dwz multi) debug file?  Alternatively we could check
      51             :      whether the dwbias == -1.  */
      52          96 :   if (mod->dw != NULL)
      53             :     {
      54             :       const void *build_id;
      55             :       const char *altname;
      56           5 :       ssize_t build_id_len = INTUSE(dwelf_dwarf_gnu_debugaltlink) (mod->dw,
      57             :                                                                    &altname,
      58             :                                                                    &build_id);
      59           5 :       if (build_id_len > 0)
      60           5 :         fd = __libdwfl_open_by_build_id (mod, true, debuginfo_file_name,
      61             :                                          build_id_len, build_id);
      62             : 
      63           5 :       if (fd >= 0)
      64             :         {
      65             :           /* We need to open an Elf handle on the file so we can check its
      66             :              build ID note for validation.  Backdoor the handle into the
      67             :              module data structure since we had to open it early anyway.  */
      68           0 :           Dwfl_Error error = __libdw_open_file (&fd, &mod->alt_elf,
      69             :                                                 true, false);
      70           0 :           if (error != DWFL_E_NOERROR)
      71           0 :             __libdwfl_seterrno (error);
      72             :           else
      73             :             {
      74             :               const void *alt_build_id;
      75           0 :               ssize_t alt_len = INTUSE(dwelf_elf_gnu_build_id) (mod->alt_elf,
      76             :                                                                 &alt_build_id);
      77           0 :               if (alt_len > 0 && alt_len == build_id_len
      78           0 :                   && memcmp (build_id, alt_build_id, alt_len) == 0)
      79           0 :                 return fd;
      80             :               else
      81             :                 {
      82             :                   /* A mismatch!  */
      83           0 :                   elf_end (mod->alt_elf);
      84           0 :                   mod->alt_elf = NULL;
      85           0 :                   close (fd);
      86           0 :                   fd = -1;
      87             :                 }
      88           0 :               free (*debuginfo_file_name);
      89           0 :               *debuginfo_file_name = NULL;
      90           0 :               errno = 0;
      91             :             }
      92             :         }
      93           5 :       return fd;
      94             :     }
      95             : 
      96             :   /* We don't even have the Dwarf yet and it isn't in the main file.
      97             :      Try to find separate debug file now using the module build id.  */
      98             :   const unsigned char *bits;
      99             :   GElf_Addr vaddr;
     100             : 
     101          91 :   if (INTUSE(dwfl_module_build_id) (mod, &bits, &vaddr) > 0)
     102          91 :     fd = __libdwfl_open_mod_by_build_id (mod, true, debuginfo_file_name);
     103          91 :   if (fd >= 0)
     104             :     {
     105             :       /* We need to open an Elf handle on the file so we can check its
     106             :          build ID note for validation.  Backdoor the handle into the
     107             :          module data structure since we had to open it early anyway.  */
     108           9 :       Dwfl_Error error = __libdw_open_file (&fd, &mod->debug.elf, true, false);
     109           9 :       if (error != DWFL_E_NOERROR)
     110           0 :         __libdwfl_seterrno (error);
     111           9 :       else if (likely (__libdwfl_find_build_id (mod, false,
     112             :                                                 mod->debug.elf) == 2))
     113             :         {
     114             :           /* Also backdoor the gratuitous flag.  */
     115           9 :           mod->debug.valid = true;
     116           9 :           return fd;
     117             :         }
     118             :       else
     119             :         {
     120             :           /* A mismatch!  */
     121           0 :           elf_end (mod->debug.elf);
     122           0 :           mod->debug.elf = NULL;
     123           0 :           close (fd);
     124           0 :           fd = -1;
     125             :         }
     126           0 :       free (*debuginfo_file_name);
     127           0 :       *debuginfo_file_name = NULL;
     128           0 :       errno = 0;
     129             :     }
     130          82 :   return fd;
     131             : }
     132             : INTDEF (dwfl_build_id_find_debuginfo)

Generated by: LCOV version 1.13