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