]> git.ipfire.org Git - thirdparty/elfutils.git/blob - debuginfod/debuginfod-find.c
debuginfod-find: Be a bit less verbose with -v
[thirdparty/elfutils.git] / debuginfod / debuginfod-find.c
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
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
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); break;
102 default: return ARGP_ERR_UNKNOWN;
103 }
104 return 0;
105 }
106
107
108 /* Data structure to communicate with argp functions. */
109 static struct argp argp =
110 {
111 options, parse_opt, args_doc, doc, NULL, NULL, NULL
112 };
113
114
115
116 int
117 main(int argc, char** argv)
118 {
119 elf_version (EV_CURRENT);
120
121 client = debuginfod_begin ();
122 if (client == NULL)
123 {
124 fprintf(stderr, "Couldn't create debuginfod client context\n");
125 return 1;
126 }
127
128 /* Exercise user data pointer, to support testing only. */
129 debuginfod_set_user_data (client, (void *)"Progress");
130
131 int remaining;
132 (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER|ARGP_NO_ARGS, &remaining, NULL);
133
134 if (argc < 2 || remaining+1 == argc) /* no arguments or at least two non-option words */
135 {
136 argp_help (&argp, stderr, ARGP_HELP_USAGE, argv[0]);
137 return 1;
138 }
139
140 /* If we were passed an ELF file name in the BUILDID slot, look in there. */
141 unsigned char* build_id = (unsigned char*) argv[remaining+1];
142 int build_id_len = 0; /* assume text */
143
144 int any_non_hex = 0;
145 int i;
146 for (i = 0; build_id[i] != '\0'; i++)
147 if ((build_id[i] >= '0' && build_id[i] <= '9') ||
148 (build_id[i] >= 'a' && build_id[i] <= 'f'))
149 ;
150 else
151 any_non_hex = 1;
152
153 int fd = -1;
154 Elf* elf = NULL;
155 if (any_non_hex) /* raw build-id */
156 {
157 fd = open ((char*) build_id, O_RDONLY);
158 if (fd < 0)
159 fprintf (stderr, "Cannot open %s: %s\n", build_id, strerror(errno));
160 }
161 if (fd >= 0)
162 {
163 elf = dwelf_elf_begin (fd);
164 if (elf == NULL)
165 fprintf (stderr, "Cannot open as ELF file %s: %s\n", build_id,
166 elf_errmsg (-1));
167 }
168 if (elf != NULL)
169 {
170 const void *extracted_build_id;
171 ssize_t s = dwelf_elf_gnu_build_id(elf, &extracted_build_id);
172 if (s > 0)
173 {
174 /* Success: replace the build_id pointer/len with the binary blob
175 that elfutils is keeping for us. It'll remain valid until elf_end(). */
176 build_id = (unsigned char*) extracted_build_id;
177 build_id_len = s;
178 }
179 else
180 fprintf (stderr, "Cannot extract build-id from %s: %s\n", build_id, elf_errmsg(-1));
181 }
182
183 char *cache_name;
184 int rc = 0;
185
186 /* Check whether FILETYPE is valid and call the appropriate
187 debuginfod_find_* function. If FILETYPE is "source"
188 then ensure a FILENAME was also supplied as an argument. */
189 if (strcmp(argv[remaining], "debuginfo") == 0)
190 rc = debuginfod_find_debuginfo(client,
191 build_id, build_id_len,
192 &cache_name);
193 else if (strcmp(argv[remaining], "executable") == 0)
194 rc = debuginfod_find_executable(client,
195 build_id, build_id_len,
196 &cache_name);
197 else if (strcmp(argv[remaining], "source") == 0)
198 {
199 if (remaining+2 == argc || argv[remaining+2][0] != '/')
200 {
201 fprintf(stderr, "If FILETYPE is \"source\" then absolute /FILENAME must be given\n");
202 return 1;
203 }
204 rc = debuginfod_find_source(client,
205 build_id, build_id_len,
206 argv[remaining+2], &cache_name);
207 }
208 else
209 {
210 argp_help (&argp, stderr, ARGP_HELP_USAGE, argv[0]);
211 return 1;
212 }
213
214 if (verbose)
215 {
216 const char* url = debuginfod_get_url (client);
217 if (url != NULL)
218 fprintf(stderr, "Downloaded from %s\n", url);
219 }
220
221 debuginfod_end (client);
222 if (elf)
223 elf_end(elf);
224 if (fd >= 0)
225 close (fd);
226
227 if (rc < 0)
228 {
229 fprintf(stderr, "Server query failed: %s\n", strerror(-rc));
230 return 1;
231 }
232
233 printf("%s\n", cache_name);
234 free (cache_name);
235
236 return 0;
237 }