LCOV - code coverage report
Current view: top level - debuginfod - debuginfod-find.c (source / functions) Hit Total Coverage
Test: elfutils-0.191 Lines: 93 111 83.8 %
Date: 2024-07-24 22:43:53 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 53 68 77.9 %

           Branch data     Line data    Source code
       1                 :            : /* Command-line frontend for retrieving ELF / DWARF / source files
       2                 :            :    from the debuginfod.
       3                 :            :    Copyright (C) 2019-2023 Red Hat, Inc.
       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 the GNU General Public License as published by
       8                 :            :    the Free Software Foundation; either version 3 of the License, or
       9                 :            :    (at your option) any later version.
      10                 :            : 
      11                 :            :    elfutils is distributed in the hope that it will be useful, but
      12                 :            :    WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :            :    General Public License for more details.
      15                 :            : 
      16                 :            :    You should have received a copy of the GNU General Public License
      17                 :            :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      18                 :            : 
      19                 :            : 
      20                 :            : #include "config.h"
      21                 :            : #include "printversion.h"
      22                 :            : #include "debuginfod.h"
      23                 :            : #include <errno.h>
      24                 :            : #include <stdio.h>
      25                 :            : #include <stdlib.h>
      26                 :            : #include <string.h>
      27                 :            : #include <time.h>
      28                 :            : #include <argp.h>
      29                 :            : #include <unistd.h>
      30                 :            : #include <fcntl.h>
      31                 :            : #include <gelf.h>
      32                 :            : #include <libdwelf.h>
      33                 :            : #ifndef DUMMY_LIBDEBUGINFOD
      34                 :            : #include <json-c/json.h>
      35                 :            : #endif
      36                 :            : 
      37                 :            : /* Name and version of program.  */
      38                 :            : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
      39                 :            : 
      40                 :            : /* Bug report address.  */
      41                 :            : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
      42                 :            : 
      43                 :            : /* Short description of program.  */
      44                 :            : static const char doc[] = N_("Request debuginfo-related content "
      45                 :            :                              "from debuginfods listed in $" DEBUGINFOD_URLS_ENV_VAR ".");
      46                 :            : 
      47                 :            : /* Strings for arguments in help texts.  */
      48                 :            : static const char args_doc[] = N_("debuginfo BUILDID\n"
      49                 :            :                                   "debuginfo PATH\n"
      50                 :            :                                   "executable BUILDID\n"
      51                 :            :                                   "executable PATH\n"
      52                 :            :                                   "source BUILDID /FILENAME\n"
      53                 :            :                                   "source PATH /FILENAME\n"
      54                 :            :                                   "section BUILDID SECTION-NAME\n"
      55                 :            :                                   "section PATH SECTION-NAME\n"
      56                 :            :                                   "metadata (glob|file|KEY) (GLOB|FILENAME|VALUE)\n"
      57                 :            :                                   );
      58                 :            : 
      59                 :            : /* Definitions of arguments for argp functions.  */
      60                 :            : static const struct argp_option options[] =
      61                 :            :   {
      62                 :            :    { "verbose", 'v', NULL, 0, "Increase verbosity.", 0 },
      63                 :            :    { NULL, 0, NULL, 0, NULL, 0 }
      64                 :            :   };
      65                 :            : 
      66                 :            : /* debuginfod connection handle.  */
      67                 :            : static debuginfod_client *client;
      68                 :            : static int verbose;
      69                 :            : 
      70                 :         20 : int progressfn(debuginfod_client *c __attribute__((__unused__)),
      71                 :            :                long a, long b)
      72                 :            : {
      73                 :         20 :   static bool first = true;
      74                 :         20 :   static struct timespec last;
      75                 :         20 :   struct timespec now;
      76                 :         20 :   uint64_t delta;
      77         [ -  + ]:         20 :   if (!first)
      78                 :            :     {
      79                 :          0 :       clock_gettime (CLOCK_MONOTONIC, &now);
      80                 :          0 :       delta = ((now.tv_sec - last.tv_sec) * 1000000
      81                 :          0 :                + (now.tv_nsec - last.tv_nsec) / 1000);
      82                 :            :     }
      83                 :            :   else
      84                 :            :     {
      85                 :         20 :       first = false;
      86                 :         20 :       delta = 250000;
      87                 :            :     }
      88                 :            : 
      89                 :            :   /* Show progress the first time and then at most 5 times a second. */
      90         [ -  - ]:         20 :   if (delta > 200000)
      91                 :            :     {
      92                 :         20 :       fprintf (stderr, "Progress %ld / %ld\n", a, b);
      93                 :         20 :       clock_gettime (CLOCK_MONOTONIC, &last);
      94                 :            :     }
      95                 :         20 :   return 0;
      96                 :            : }
      97                 :            : 
      98                 :            : 
      99                 :       2482 : static error_t parse_opt (int key, char *arg, struct argp_state *state)
     100                 :            : {
     101                 :       2482 :   (void) arg;
     102                 :       2482 :   (void) state;
     103         [ +  + ]:       2482 :   switch (key)
     104                 :            :     {
     105                 :        102 :     case 'v': verbose++;
     106                 :        102 :       debuginfod_set_progressfn (client, & progressfn);
     107         [ +  + ]:        102 :       if (verbose > 1)
     108                 :         64 :         debuginfod_set_verbose_fd (client, STDERR_FILENO);
     109                 :            :       break;
     110                 :            :     default: return ARGP_ERR_UNKNOWN;
     111                 :            :     }
     112                 :            :   return 0;
     113                 :            : }
     114                 :            : 
     115                 :            : 
     116                 :            : /* Data structure to communicate with argp functions.  */
     117                 :            : static struct argp argp =
     118                 :            :   {
     119                 :            :    options, parse_opt, args_doc, doc, NULL, NULL, NULL
     120                 :            :   };
     121                 :            : 
     122                 :            : 
     123                 :            : 
     124                 :            : int
     125                 :        476 : main(int argc, char** argv)
     126                 :            : {
     127                 :        476 :   elf_version (EV_CURRENT);
     128                 :            : 
     129                 :        476 :   client = debuginfod_begin ();
     130         [ -  + ]:        476 :   if (client == NULL)
     131                 :            :     {
     132                 :          0 :       fprintf(stderr, "Couldn't create debuginfod client context\n");
     133                 :          0 :       return 1;
     134                 :            :     }
     135                 :            : 
     136                 :            :   /* Exercise user data pointer, to support testing only. */
     137                 :        476 :   debuginfod_set_user_data (client, (void *)"Progress");
     138                 :            : 
     139                 :        476 :   int remaining;
     140                 :        476 :   (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER|ARGP_NO_ARGS, &remaining, NULL);
     141                 :            : 
     142   [ +  -  +  + ]:        476 :   if (argc < 2 || remaining+1 >= argc) /* no arguments or at least two non-option words */
     143                 :            :     {
     144                 :          2 :       argp_help (&argp, stderr, ARGP_HELP_USAGE, argv[0]);
     145                 :          2 :       return 1;
     146                 :            :     }
     147                 :            : 
     148                 :            :   /* If we were passed an ELF file name in the BUILDID slot, look in there. */
     149                 :        474 :   unsigned char* build_id = (unsigned char*) argv[remaining+1];
     150                 :        474 :   int build_id_len = 0; /* assume text */
     151                 :        474 :   Elf* elf = NULL;
     152                 :            : 
     153                 :            :   /* Process optional buildid given via ELF file name, for some query types only. */
     154         [ +  + ]:        474 :   if (strcmp(argv[remaining], "debuginfo") == 0
     155         [ +  + ]:        264 :       || strcmp(argv[remaining], "executable") == 0
     156         [ +  + ]:         76 :       || strcmp(argv[remaining], "source") == 0
     157         [ +  + ]:         26 :       || strcmp(argv[remaining], "section") == 0)
     158                 :            :     {
     159                 :            :       int any_non_hex = 0;
     160                 :            :       int i;
     161         [ +  + ]:      18710 :       for (i = 0; build_id[i] != '\0'; i++)
     162         [ +  + ]:      18246 :         if ((build_id[i] >= '0' && build_id[i] <= '9') ||
     163                 :            :             (build_id[i] >= 'a' && build_id[i] <= 'f'))
     164                 :            :           ;
     165                 :            :         else
     166                 :        462 :           any_non_hex = 1;
     167                 :            :       
     168                 :        464 :       int fd = -1;
     169         [ +  + ]:        464 :       if (any_non_hex) /* raw build-id */
     170                 :            :         {
     171                 :         12 :           fd = open ((char*) build_id, O_RDONLY);
     172         [ +  - ]:         12 :           if (fd < 0)
     173                 :          0 :             fprintf (stderr, "Cannot open %s: %s\n", build_id, strerror(errno));
     174                 :            :         }
     175                 :         12 :       if (fd >= 0)
     176                 :            :         {
     177                 :         12 :           elf = dwelf_elf_begin (fd);
     178         [ +  - ]:         12 :           if (elf == NULL)
     179                 :          0 :             fprintf (stderr, "Cannot open as ELF file %s: %s\n", build_id,
     180                 :            :                      elf_errmsg (-1));
     181                 :            :         }
     182                 :          0 :       if (elf != NULL)
     183                 :            :         {
     184                 :         12 :           const void *extracted_build_id;
     185                 :         12 :           ssize_t s = dwelf_elf_gnu_build_id(elf, &extracted_build_id);
     186         [ +  - ]:         12 :           if (s > 0)
     187                 :            :             {
     188                 :            :               /* Success: replace the build_id pointer/len with the binary blob
     189                 :            :                  that elfutils is keeping for us.  It'll remain valid until elf_end(). */
     190                 :         12 :               build_id = (unsigned char*) extracted_build_id;
     191                 :         12 :               build_id_len = s;
     192                 :            :             }
     193                 :            :           else
     194                 :         12 :             fprintf (stderr, "Cannot extract build-id from %s: %s\n", build_id, elf_errmsg(-1));
     195                 :            :         }
     196                 :            :     }
     197                 :            : 
     198                 :        474 :   char *cache_name;
     199                 :        474 :   int rc = 0;
     200                 :            : 
     201                 :            :   /* By default the stdout output is the path of the cached file.
     202                 :            :      Some requests (ex. metadata query may instead choose to do a different output,
     203                 :            :      in that case a stringified json object) */
     204                 :        474 :   bool print_cached_file = true;
     205                 :            :   /* Check whether FILETYPE is valid and call the appropriate
     206                 :            :      debuginfod_find_* function. If FILETYPE is "source"
     207                 :            :      then ensure a FILENAME was also supplied as an argument.  */
     208         [ +  + ]:        474 :   if (strcmp(argv[remaining], "debuginfo") == 0)
     209                 :        210 :     rc = debuginfod_find_debuginfo(client,
     210                 :            :                                    build_id, build_id_len,
     211                 :            :                                    &cache_name);
     212         [ +  + ]:        264 :   else if (strcmp(argv[remaining], "executable") == 0)
     213                 :        188 :     rc = debuginfod_find_executable(client,
     214                 :            :                                     build_id, build_id_len,
     215                 :            :                                     &cache_name);
     216         [ +  + ]:         76 :   else if (strcmp(argv[remaining], "source") == 0)
     217                 :            :     {
     218   [ +  -  -  + ]:         50 :       if (remaining+2 == argc || argv[remaining+2][0] != '/')
     219                 :            :         {
     220                 :          0 :           fprintf(stderr, "If FILETYPE is \"source\" then absolute /FILENAME must be given\n");
     221                 :          0 :           return 1;
     222                 :            :         }
     223                 :         50 :       rc = debuginfod_find_source(client,
     224                 :            :                                   build_id, build_id_len,
     225                 :            :                                   argv[remaining+2], &cache_name);
     226                 :            :     }
     227         [ +  + ]:         26 :   else if (strcmp(argv[remaining], "section") == 0)
     228                 :            :     {
     229         [ -  + ]:         16 :       if (remaining+2 >= argc)
     230                 :            :         {
     231                 :          0 :           fprintf(stderr,
     232                 :            :                   "If FILETYPE is \"section\" then a section name must be given\n");
     233                 :          0 :           return 1;
     234                 :            :         }
     235                 :         16 :       rc = debuginfod_find_section(client, build_id, build_id_len,
     236                 :         16 :                                    argv[remaining+2], &cache_name);
     237                 :            :     }
     238         [ +  - ]:         10 :   else if (strcmp(argv[remaining], "metadata") == 0) /* no buildid! */
     239                 :            :     {
     240         [ -  + ]:         10 :       if (remaining+2 == argc)
     241                 :            :         {
     242                 :          0 :           fprintf(stderr, "Require KEY and VALUE for \"metadata\"\n");
     243                 :          0 :           return 1;
     244                 :            :         }
     245                 :            :       
     246                 :         10 :       rc = debuginfod_find_metadata (client, argv[remaining+1], argv[remaining+2],
     247                 :            :                                      &cache_name);
     248                 :            : #ifndef DUMMY_LIBDEBUGINFOD
     249         [ +  - ]:         10 :       if (rc >= 0)
     250                 :            :         {
     251                 :            :           /* We output a pprinted JSON object, not the regular debuginfod-find cached file path */
     252                 :         10 :           print_cached_file = false;
     253                 :         10 :           json_object *metadata = json_object_from_file(cache_name);
     254         [ +  - ]:         10 :           if(metadata)
     255                 :            :             {
     256                 :         10 :               printf("%s\n", json_object_to_json_string_ext(metadata,
     257                 :            :                                                             JSON_C_TO_STRING_PRETTY
     258                 :            : #ifdef JSON_C_TO_STRING_NOSLASHESCAPE /* json-c 0.15 */
     259                 :            :                                                             | JSON_C_TO_STRING_NOSLASHESCAPE
     260                 :            : #endif
     261                 :            :                                                             ));
     262                 :         10 :               json_object_put(metadata);
     263                 :            :             }
     264                 :            :           else
     265                 :            :             {
     266                 :          0 :               fprintf(stderr, "%s does not contain a valid JSON format object\n", cache_name);
     267                 :          0 :               return 1;
     268                 :            :             }
     269                 :            :         }
     270                 :            : #endif
     271                 :            :     }
     272                 :            :   else
     273                 :            :     {
     274                 :          0 :       argp_help (&argp, stderr, ARGP_HELP_USAGE, argv[0]);
     275                 :          0 :       return 1;
     276                 :            :     }
     277                 :            : 
     278         [ +  + ]:        474 :   if (verbose)
     279                 :            :     {
     280                 :         36 :       const char* headers = debuginfod_get_headers(client);
     281         [ +  + ]:         36 :       if (headers)
     282                 :         20 :         fprintf(stderr, "Headers:\n%s", headers);
     283                 :         36 :       const char* url = debuginfod_get_url (client);
     284         [ +  + ]:         36 :       if (url != NULL)
     285                 :         20 :         fprintf(stderr, "Downloaded from %s\n", url);
     286                 :            :     }
     287                 :            : 
     288                 :        474 :   debuginfod_end (client);
     289         [ +  + ]:        474 :   if (elf)
     290                 :         12 :     elf_end(elf);
     291                 :            : 
     292         [ +  + ]:        474 :   if (rc < 0)
     293                 :            :     {
     294                 :         36 :       fprintf(stderr, "Server query failed: %s\n", strerror(-rc));
     295                 :         36 :       return 1;
     296                 :            :     }
     297                 :            :   else
     298                 :        438 :     close (rc);
     299                 :            : 
     300         [ +  + ]:        438 :   if(print_cached_file) printf("%s\n", cache_name);
     301                 :        438 :   free (cache_name);
     302                 :            : 
     303                 :        438 :   return 0;
     304                 :            : }

Generated by: LCOV version 1.16