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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
|
/* debuginfod utilities for GDB.
Copyright (C) 2020-2023 Free Software Foundation, Inc.
This file is part of GDB.
This program 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.
This program 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 "defs.h"
#include "diagnostics.h"
#include <errno.h>
#include "gdbsupport/scoped_fd.h"
#include "debuginfod-support.h"
#include <optional>
#include "cli/cli-cmds.h"
#include "cli/cli-style.h"
#include "cli-out.h"
#include "target.h"
/* Set/show debuginfod commands. */
static cmd_list_element *set_debuginfod_prefix_list;
static cmd_list_element *show_debuginfod_prefix_list;
/* maint set/show debuginfod commands. */
static cmd_list_element *maint_set_debuginfod_cmdlist;
static cmd_list_element *maint_show_debuginfod_cmdlist;
static const char debuginfod_on[] = "on";
static const char debuginfod_off[] = "off";
static const char debuginfod_ask[] = "ask";
static const char *debuginfod_enabled_enum[] =
{
debuginfod_on,
debuginfod_off,
debuginfod_ask,
nullptr
};
static const char *debuginfod_enabled =
#if defined(HAVE_LIBDEBUGINFOD)
debuginfod_ask;
#else
debuginfod_off;
#endif
/* Controls whether ELF/DWARF section downloading is enabled. */
static bool debuginfod_download_sections =
#if defined(HAVE_LIBDEBUGINFOD_FIND_SECTION)
true;
#else
false;
#endif
static unsigned int debuginfod_verbose = 1;
#ifndef HAVE_LIBDEBUGINFOD
scoped_fd
debuginfod_source_query (const unsigned char *build_id,
int build_id_len,
const char *srcpath,
gdb::unique_xmalloc_ptr<char> *destname)
{
return scoped_fd (-ENOSYS);
}
scoped_fd
debuginfod_debuginfo_query (const unsigned char *build_id,
int build_id_len,
const char *filename,
gdb::unique_xmalloc_ptr<char> *destname)
{
return scoped_fd (-ENOSYS);
}
scoped_fd
debuginfod_exec_query (const unsigned char *build_id,
int build_id_len,
const char *filename,
gdb::unique_xmalloc_ptr<char> *destname)
{
return scoped_fd (-ENOSYS);
}
scoped_fd
debuginfod_section_query (const unsigned char *build_id,
int build_id_len,
const char *filename,
const char *section_name,
gdb::unique_xmalloc_ptr<char> *destname)
{
return scoped_fd (-ENOSYS);
}
#define NO_IMPL _("Support for debuginfod is not compiled into GDB.")
#else
#include <elfutils/debuginfod.h>
struct user_data
{
user_data (const char *desc, const char *fname)
: desc (desc), fname (fname)
{ }
const char * const desc;
const char * const fname;
ui_out::progress_update progress;
};
/* Convert SIZE into a unit suitable for use with progress updates.
SIZE should in given in bytes and will be converted into KB, MB, GB
or remain unchanged. UNIT will be set to "B", "KB", "MB" or "GB"
accordingly. */
static const char *
get_size_and_unit (double &size)
{
if (size < 1024)
/* If size is less than 1 KB then set unit to B. */
return "B";
size /= 1024;
if (size < 1024)
/* If size is less than 1 MB then set unit to KB. */
return "K";
size /= 1024;
if (size < 1024)
/* If size is less than 1 GB then set unit to MB. */
return "M";
size /= 1024;
return "G";
}
static int
progressfn (debuginfod_client *c, long cur, long total)
{
user_data *data = static_cast<user_data *> (debuginfod_get_user_data (c));
gdb_assert (data != nullptr);
string_file styled_fname (current_uiout->can_emit_style_escape ());
fprintf_styled (&styled_fname, file_name_style.style (), "%s",
data->fname);
if (check_quit_flag ())
{
gdb_printf ("Cancelling download of %s %s...\n",
data->desc, styled_fname.c_str ());
return 1;
}
if (debuginfod_verbose == 0)
return 0;
/* Print progress update. Include the transfer size if available. */
if (total > 0)
{
/* Transfer size is known. */
double howmuch = (double) cur / (double) total;
if (howmuch >= 0.0 && howmuch <= 1.0)
{
double d_total = (double) total;
const char *unit = get_size_and_unit (d_total);
std::string msg = string_printf ("Downloading %0.2f %s %s %s",
d_total, unit, data->desc,
styled_fname.c_str ());
data->progress.update_progress (msg, unit, howmuch, d_total);
return 0;
}
}
std::string msg = string_printf ("Downloading %s %s",
data->desc, styled_fname.c_str ());
data->progress.update_progress (msg);
return 0;
}
/* Cleanup ARG, which is a debuginfod_client pointer. */
static void
cleanup_debuginfod_client (void *arg)
{
debuginfod_client *client = static_cast<debuginfod_client *> (arg);
debuginfod_end (client);
}
/* Return a pointer to the single global debuginfod_client, initialising it
first if needed. */
static debuginfod_client *
get_debuginfod_client ()
{
static debuginfod_client *global_client = nullptr;
if (global_client == nullptr)
{
global_client = debuginfod_begin ();
if (global_client != nullptr)
{
/* It is important that we cleanup the debuginfod_client object
before calling exit. Some of the libraries used by debuginfod
make use of at_exit handlers to perform cleanup.
If we wrapped the debuginfod_client in a unique_ptr and relied
on its destructor to cleanup then this would be run as part of
the global C++ object destructors, which is after the at_exit
handlers, which is too late.
So instead, we make use of GDB's final cleanup mechanism. */
make_final_cleanup (cleanup_debuginfod_client, global_client);
debuginfod_set_progressfn (global_client, progressfn);
}
}
return global_client;
}
/* Check if debuginfod is enabled. If configured to do so, ask the user
whether to enable debuginfod. */
static bool
debuginfod_is_enabled ()
{
const char *urls = skip_spaces (getenv (DEBUGINFOD_URLS_ENV_VAR));
if (debuginfod_enabled == debuginfod_off
|| urls == nullptr
|| *urls == '\0')
return false;
if (debuginfod_enabled == debuginfod_ask)
{
gdb_printf (_("\nThis GDB supports auto-downloading debuginfo " \
"from the following URLs:\n"));
std::string_view url_view (urls);
while (true)
{
size_t off = url_view.find_first_not_of (' ');
if (off == std::string_view::npos)
break;
url_view = url_view.substr (off);
/* g++ 11.2.1 on s390x, g++ 11.3.1 on ppc64le and g++ 11 on
hppa seem convinced url_view might be of SIZE_MAX length.
And so complains because the length of an array can only
be PTRDIFF_MAX. */
DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE_STRINGOP_OVERREAD
off = url_view.find_first_of (' ');
DIAGNOSTIC_POP
gdb_printf
(_(" <%ps>\n"),
styled_string (file_name_style.style (),
std::string (url_view.substr (0, off)).c_str ()));
if (off == std::string_view::npos)
break;
url_view = url_view.substr (off);
}
int resp = nquery (_("Enable debuginfod for this session? "));
if (!resp)
{
gdb_printf (_("Debuginfod has been disabled.\nTo make this " \
"setting permanent, add \'set debuginfod " \
"enabled off\' to .gdbinit.\n"));
debuginfod_enabled = debuginfod_off;
return false;
}
gdb_printf (_("Debuginfod has been enabled.\nTo make this " \
"setting permanent, add \'set debuginfod enabled " \
"on\' to .gdbinit.\n"));
debuginfod_enabled = debuginfod_on;
}
return true;
}
/* Print the result of the most recent attempted download. */
static void
print_outcome (int fd, const char *desc, const char *fname)
{
if (fd < 0 && fd != -ENOENT)
gdb_printf (_("Download failed: %s. Continuing without %s %ps.\n"),
safe_strerror (-fd),
desc,
styled_string (file_name_style.style (), fname));
}
/* See debuginfod-support.h */
scoped_fd
debuginfod_source_query (const unsigned char *build_id,
int build_id_len,
const char *srcpath,
gdb::unique_xmalloc_ptr<char> *destname)
{
if (!debuginfod_is_enabled ())
return scoped_fd (-ENOSYS);
debuginfod_client *c = get_debuginfod_client ();
if (c == nullptr)
return scoped_fd (-ENOMEM);
char *dname = nullptr;
scoped_fd fd;
std::optional<target_terminal::scoped_restore_terminal_state> term_state;
{
user_data data ("source file", srcpath);
debuginfod_set_user_data (c, &data);
if (target_supports_terminal_ours ())
{
term_state.emplace ();
target_terminal::ours ();
}
fd = scoped_fd (debuginfod_find_source (c,
build_id,
build_id_len,
srcpath,
&dname));
debuginfod_set_user_data (c, nullptr);
}
print_outcome (fd.get (), "source file", srcpath);
if (fd.get () >= 0)
destname->reset (dname);
return fd;
}
/* See debuginfod-support.h */
scoped_fd
debuginfod_debuginfo_query (const unsigned char *build_id,
int build_id_len,
const char *filename,
gdb::unique_xmalloc_ptr<char> *destname)
{
if (!debuginfod_is_enabled ())
return scoped_fd (-ENOSYS);
debuginfod_client *c = get_debuginfod_client ();
if (c == nullptr)
return scoped_fd (-ENOMEM);
char *dname = nullptr;
scoped_fd fd;
std::optional<target_terminal::scoped_restore_terminal_state> term_state;
{
user_data data ("separate debug info for", filename);
debuginfod_set_user_data (c, &data);
if (target_supports_terminal_ours ())
{
term_state.emplace ();
target_terminal::ours ();
}
fd = scoped_fd (debuginfod_find_debuginfo (c, build_id, build_id_len,
&dname));
debuginfod_set_user_data (c, nullptr);
}
print_outcome (fd.get (), "separate debug info for", filename);
if (fd.get () >= 0)
destname->reset (dname);
return fd;
}
/* See debuginfod-support.h */
scoped_fd
debuginfod_exec_query (const unsigned char *build_id,
int build_id_len,
const char *filename,
gdb::unique_xmalloc_ptr<char> *destname)
{
if (!debuginfod_is_enabled ())
return scoped_fd (-ENOSYS);
debuginfod_client *c = get_debuginfod_client ();
if (c == nullptr)
return scoped_fd (-ENOMEM);
char *dname = nullptr;
scoped_fd fd;
std::optional<target_terminal::scoped_restore_terminal_state> term_state;
{
user_data data ("executable for", filename);
debuginfod_set_user_data (c, &data);
if (target_supports_terminal_ours ())
{
term_state.emplace ();
target_terminal::ours ();
}
fd = scoped_fd (debuginfod_find_executable (c, build_id, build_id_len,
&dname));
debuginfod_set_user_data (c, nullptr);
}
print_outcome (fd.get (), "executable for", filename);
if (fd.get () >= 0)
destname->reset (dname);
return fd;
}
/* See debuginfod-support.h */
scoped_fd
debuginfod_section_query (const unsigned char *build_id,
int build_id_len,
const char *filename,
const char *section_name,
gdb::unique_xmalloc_ptr<char> *destname)
{
#if !defined (HAVE_LIBDEBUGINFOD_FIND_SECTION)
return scoped_fd (-ENOSYS);
#else
if (!debuginfod_download_sections || !debuginfod_is_enabled ())
return scoped_fd (-ENOSYS);
debuginfod_client *c = get_debuginfod_client ();
if (c == nullptr)
return scoped_fd (-ENOMEM);
char *dname = nullptr;
std::string desc = std::string ("section ") + section_name + " for";
scoped_fd fd;
std::optional<target_terminal::scoped_restore_terminal_state> term_state;
{
user_data data (desc.c_str (), filename);
debuginfod_set_user_data (c, &data);
if (target_supports_terminal_ours ())
{
term_state.emplace ();
target_terminal::ours ();
}
fd = scoped_fd (debuginfod_find_section (c, build_id, build_id_len,
section_name, &dname));
debuginfod_set_user_data (c, nullptr);
}
print_outcome (fd.get (), desc.c_str (), filename);
gdb_assert (destname != nullptr);
if (fd.get () >= 0)
destname->reset (dname);
return fd;
#endif /* HAVE_LIBDEBUGINFOD_FIND_SECTION */
}
#endif
/* Set callback for "set debuginfod enabled". */
static void
set_debuginfod_enabled (const char *value)
{
#if defined(HAVE_LIBDEBUGINFOD)
debuginfod_enabled = value;
#else
/* Disabling debuginfod when gdb is not built with it is a no-op. */
if (value != debuginfod_off)
error (NO_IMPL);
#endif
}
/* Get callback for "set debuginfod enabled". */
static const char *
get_debuginfod_enabled ()
{
return debuginfod_enabled;
}
/* Show callback for "set debuginfod enabled". */
static void
show_debuginfod_enabled (ui_file *file, int from_tty, cmd_list_element *cmd,
const char *value)
{
gdb_printf (file,
_("Debuginfod functionality is currently set to "
"\"%s\".\n"), debuginfod_enabled);
}
/* Set callback for "set debuginfod urls". */
static void
set_debuginfod_urls (const std::string &urls)
{
#if defined(HAVE_LIBDEBUGINFOD)
if (setenv (DEBUGINFOD_URLS_ENV_VAR, urls.c_str (), 1) != 0)
warning (_("Unable to set debuginfod URLs: %s"), safe_strerror (errno));
#else
error (NO_IMPL);
#endif
}
/* Get callback for "set debuginfod urls". */
static const std::string&
get_debuginfod_urls ()
{
static std::string urls;
#if defined(HAVE_LIBDEBUGINFOD)
const char *envvar = getenv (DEBUGINFOD_URLS_ENV_VAR);
if (envvar != nullptr)
urls = envvar;
else
urls.clear ();
#endif
return urls;
}
/* Show callback for "set debuginfod urls". */
static void
show_debuginfod_urls (ui_file *file, int from_tty, cmd_list_element *cmd,
const char *value)
{
if (value[0] == '\0')
gdb_printf (file, _("Debuginfod URLs have not been set.\n"));
else
gdb_printf (file, _("Debuginfod URLs are currently set to:\n%s\n"),
value);
}
/* Show callback for "set debuginfod verbose". */
static void
show_debuginfod_verbose_command (ui_file *file, int from_tty,
cmd_list_element *cmd, const char *value)
{
gdb_printf (file, _("Debuginfod verbose output is set to %s.\n"),
value);
}
/* Set callback for "maint set debuginfod download-sections". */
static void
maint_set_debuginfod_download_sections (bool value)
{
#if !defined(HAVE_LIBDEBUGINFOD_FIND_SECTION)
if (value)
error (_("Support for section downloading is not compiled into GDB. " \
"Defaulting to \"off\"."));
#endif
debuginfod_download_sections = value;
}
/* Get callback for "maint set debuginfod download-sections". */
static bool
maint_get_debuginfod_download_sections ()
{
return debuginfod_download_sections;
}
/* Register debuginfod commands. */
void _initialize_debuginfod ();
void
_initialize_debuginfod ()
{
/* set/show debuginfod */
add_setshow_prefix_cmd ("debuginfod", class_run,
_("Set debuginfod options."),
_("Show debuginfod options."),
&set_debuginfod_prefix_list,
&show_debuginfod_prefix_list,
&setlist, &showlist);
add_setshow_enum_cmd ("enabled", class_run, debuginfod_enabled_enum,
_("Set whether to use debuginfod."),
_("Show whether to use debuginfod."),
_("\
When set to \"on\", enable the use of debuginfod to download missing\n\
debug info and source files. GDB may also download components of debug\n\
info instead of entire files. \"off\" disables the use of debuginfod.\n\
When set to \"ask\", prompt whether to enable or disable debuginfod." ),
set_debuginfod_enabled,
get_debuginfod_enabled,
show_debuginfod_enabled,
&set_debuginfod_prefix_list,
&show_debuginfod_prefix_list);
/* set/show debuginfod urls */
add_setshow_string_noescape_cmd ("urls", class_run, _("\
Set the list of debuginfod server URLs."), _("\
Show the list of debuginfod server URLs."), _("\
Manage the space-separated list of debuginfod server URLs that GDB will query \
when missing debuginfo, executables or source files.\nThe default value is \
copied from the DEBUGINFOD_URLS environment variable."),
set_debuginfod_urls,
get_debuginfod_urls,
show_debuginfod_urls,
&set_debuginfod_prefix_list,
&show_debuginfod_prefix_list);
/* set/show debuginfod verbose */
add_setshow_zuinteger_cmd ("verbose", class_support,
&debuginfod_verbose, _("\
Set verbosity of debuginfod output."), _("\
Show debuginfod debugging."), _("\
When set to a non-zero value, display verbose output for each debuginfod \
query.\nTo disable, set to zero. Verbose output is displayed by default."),
nullptr,
show_debuginfod_verbose_command,
&set_debuginfod_prefix_list,
&show_debuginfod_prefix_list);
/* maint set/show debuginfod. */
add_setshow_prefix_cmd ("debuginfod", class_maintenance,
_("Set debuginfod specific variables."),
_("Show debuginfod specific variables."),
&maint_set_debuginfod_cmdlist,
&maint_show_debuginfod_cmdlist,
&maintenance_set_cmdlist, &maintenance_show_cmdlist);
/* maint set/show debuginfod download-sections. */
add_setshow_boolean_cmd ("download-sections", class_maintenance, _("\
Set whether debuginfod may download individual ELF/DWARF sections."), _("\
Show whether debuginfod may download individual ELF/DWARF sections."), _("\
When enabled, debuginfod may attempt to download individual ELF/DWARF \
sections from debug info files.\nIf disabled, only whole debug info files \
may be downloaded."),
maint_set_debuginfod_download_sections,
maint_get_debuginfod_download_sections,
nullptr,
&maint_set_debuginfod_cmdlist,
&maint_show_debuginfod_cmdlist);
}
|