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