LCOV - code coverage report
Current view: top level - debuginfod - debuginfod-find.c (source / functions) Hit Total Coverage
Test: elfutils-0.182 Lines: 63 74 85.1 %
Date: 2020-10-31 23:45:54 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Command-line frontend for retrieving ELF / DWARF / source files
       2             :    from the debuginfod.
       3             :    Copyright (C) 2019-2020 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 <argp.h>
      28             : #include <unistd.h>
      29             : #include <fcntl.h>
      30             : #include <gelf.h>
      31             : #include <libdwelf.h>
      32             : 
      33             : 
      34             : /* Name and version of program.  */
      35             : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
      36             : 
      37             : /* Bug report address.  */
      38             : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
      39             : 
      40             : /* Short description of program.  */
      41             : static const char doc[] = N_("Request debuginfo-related content "
      42             :                              "from debuginfods listed in $" DEBUGINFOD_URLS_ENV_VAR ".");
      43             : 
      44             : /* Strings for arguments in help texts.  */
      45             : static const char args_doc[] = N_("debuginfo BUILDID\n"
      46             :                                   "debuginfo PATH\n"
      47             :                                   "executable BUILDID\n"
      48             :                                   "executable PATH\n"
      49             :                                   "source BUILDID /FILENAME\n"
      50             :                                   "source PATH /FILENAME\n");
      51             : 
      52             : 
      53             : /* Definitions of arguments for argp functions.  */
      54             : static const struct argp_option options[] =
      55             :   {
      56             :    { "verbose", 'v', NULL, 0, "Increase verbosity.", 0 },
      57             :    { NULL, 0, NULL, 0, NULL, 0 }
      58             :   };
      59             : 
      60             : /* debuginfod connection handle.  */
      61             : static debuginfod_client *client;
      62             : static int verbose;
      63             : 
      64           3 : int progressfn(debuginfod_client *c __attribute__((__unused__)),
      65             :                long a, long b)
      66             : {
      67           3 :   fprintf (stderr, "Progress %ld / %ld\n", a, b);
      68           3 :   return 0;
      69             : }
      70             : 
      71             : 
      72         291 : static error_t parse_opt (int key, char *arg, struct argp_state *state)
      73             : {
      74         291 :   (void) arg;
      75         291 :   (void) state;
      76         291 :   switch (key)
      77             :     {
      78           1 :     case 'v': verbose++;
      79           1 :       debuginfod_set_progressfn (client, & progressfn); break;
      80             :     default: return ARGP_ERR_UNKNOWN;
      81             :     }
      82           1 :   return 0;
      83             : }
      84             : 
      85             : 
      86             : /* Data structure to communicate with argp functions.  */
      87             : static struct argp argp =
      88             :   {
      89             :    options, parse_opt, args_doc, doc, NULL, NULL, NULL
      90             :   };
      91             : 
      92             : 
      93             : 
      94             : int
      95          58 : main(int argc, char** argv)
      96             : {
      97          58 :   elf_version (EV_CURRENT);
      98             : 
      99          58 :   client = debuginfod_begin ();
     100          58 :   if (client == NULL)
     101             :     {
     102           0 :       fprintf(stderr, "Couldn't create debuginfod client context\n");
     103           0 :       return 1;
     104             :     }
     105             : 
     106             :   /* Exercise user data pointer, to support testing only. */
     107          58 :   debuginfod_set_user_data (client, (void *)"Progress");
     108             : 
     109          58 :   int remaining;
     110          58 :   (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER|ARGP_NO_ARGS, &remaining, NULL);
     111             : 
     112          58 :   if (argc < 2 || remaining+1 == argc) /* no arguments or at least two non-option words */
     113             :     {
     114           0 :       argp_help (&argp, stderr, ARGP_HELP_USAGE, argv[0]);
     115           0 :       return 1;
     116             :     }
     117             : 
     118             :   /* If we were passed an ELF file name in the BUILDID slot, look in there. */
     119          58 :   unsigned char* build_id = (unsigned char*) argv[remaining+1];
     120          58 :   int build_id_len = 0; /* assume text */
     121             : 
     122          58 :   int any_non_hex = 0;
     123          58 :   int i;
     124        2312 :   for (i = 0; build_id[i] != '\0'; i++)
     125        2254 :     if ((build_id[i] >= '0' && build_id[i] <= '9') ||
     126             :         (build_id[i] >= 'a' && build_id[i] <= 'f'))
     127             :       ;
     128             :     else
     129           6 :       any_non_hex = 1;
     130             : 
     131          58 :   int fd = -1;
     132          58 :   Elf* elf = NULL;
     133          58 :   if (any_non_hex) /* raw build-id */
     134             :     {
     135           1 :       fd = open ((char*) build_id, O_RDONLY);
     136           1 :       if (fd < 0)
     137           0 :         fprintf (stderr, "Cannot open %s: %s\n", build_id, strerror(errno));
     138             :     }
     139           1 :   if (fd >= 0)
     140             :     {
     141           1 :       elf = dwelf_elf_begin (fd);
     142           1 :       if (elf == NULL)
     143           0 :         fprintf (stderr, "Cannot open as ELF file %s: %s\n", build_id,
     144             :                  elf_errmsg (-1));
     145             :     }
     146          58 :   if (elf != NULL)
     147             :     {
     148           1 :       const void *extracted_build_id;
     149           1 :       ssize_t s = dwelf_elf_gnu_build_id(elf, &extracted_build_id);
     150           1 :       if (s > 0)
     151             :         {
     152             :           /* Success: replace the build_id pointer/len with the binary blob
     153             :              that elfutils is keeping for us.  It'll remain valid until elf_end(). */
     154           1 :           build_id = (unsigned char*) extracted_build_id;
     155           1 :           build_id_len = s;
     156             :         }
     157             :       else
     158           0 :         fprintf (stderr, "Cannot extract build-id from %s: %s\n", build_id, elf_errmsg(-1));
     159             :     }
     160             : 
     161          58 :   char *cache_name;
     162          58 :   int rc = 0;
     163             : 
     164             :   /* Check whether FILETYPE is valid and call the appropriate
     165             :      debuginfod_find_* function. If FILETYPE is "source"
     166             :      then ensure a FILENAME was also supplied as an argument.  */
     167          58 :   if (strcmp(argv[remaining], "debuginfo") == 0)
     168          21 :     rc = debuginfod_find_debuginfo(client,
     169             :                                    build_id, build_id_len,
     170             :                                    &cache_name);
     171          37 :   else if (strcmp(argv[remaining], "executable") == 0)
     172          23 :     rc = debuginfod_find_executable(client,
     173             :                                     build_id, build_id_len,
     174             :                                     &cache_name);
     175          14 :   else if (strcmp(argv[remaining], "source") == 0)
     176             :     {
     177          14 :       if (remaining+2 == argc || argv[remaining+2][0] != '/')
     178             :         {
     179           0 :           fprintf(stderr, "If FILETYPE is \"source\" then absolute /FILENAME must be given\n");
     180           0 :           return 1;
     181             :         }
     182          14 :       rc = debuginfod_find_source(client,
     183             :                                   build_id, build_id_len,
     184             :                                   argv[remaining+2], &cache_name);
     185             :     }
     186             :   else
     187             :     {
     188           0 :       argp_help (&argp, stderr, ARGP_HELP_USAGE, argv[0]);
     189           0 :       return 1;
     190             :     }
     191             : 
     192          58 :   if (verbose)
     193             :     {
     194           1 :       const char* url = debuginfod_get_url (client);
     195           1 :       if (url != NULL)
     196           1 :         fprintf(stderr, "Downloaded from %s\n", url);
     197             :     }
     198             : 
     199          58 :   debuginfod_end (client);
     200          58 :   if (elf)
     201           1 :     elf_end(elf);
     202          58 :   if (fd >= 0)
     203           1 :     close (fd);
     204             : 
     205          58 :   if (rc < 0)
     206             :     {
     207           5 :       fprintf(stderr, "Server query failed: %s\n", strerror(-rc));
     208           5 :       return 1;
     209             :     }
     210             : 
     211          53 :   printf("%s\n", cache_name);
     212          53 :   free (cache_name);
     213             : 
     214          53 :   return 0;
     215             : }

Generated by: LCOV version 1.13