]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/maint.c
libsframe: fix error code in sframe_decode
[thirdparty/binutils-gdb.git] / gdb / maint.c
CommitLineData
c906108c 1/* Support for GDB maintenance commands.
c6f0559b 2
d01e8234 3 Copyright (C) 1992-2025 Free Software Foundation, Inc.
c6f0559b 4
c906108c
SS
5 Written by Fred Fish at Cygnus Support.
6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22
e17c207e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
aa17805f 25#include <cmath>
c906108c
SS
26#include <signal.h>
27#include "command.h"
c906108c 28#include "symtab.h"
bd712aed 29#include "block.h"
c906108c 30#include "gdbtypes.h"
ef0f16cc 31#include "expression.h"
c906108c
SS
32#include "language.h"
33#include "symfile.h"
34#include "objfiles.h"
35#include "value.h"
bd712aed 36#include "top.h"
bd712aed 37#include "maint.h"
268a13a5 38#include "gdbsupport/selftest.h"
328d42d8 39#include "inferior.h"
0981fe10 40#include "gdbsupport/thread-pool.h"
a584cfc9 41#include "event-top.h"
e5e619ac 42#include "cp-support.h"
c906108c 43
18a642a1 44#include "cli/cli-decode.h"
529480d0 45#include "cli/cli-utils.h"
bd712aed 46#include "cli/cli-setshow.h"
9e69a2e1 47#include "cli/cli-style.h"
fdbc9870 48#include "cli/cli-cmds.h"
18a642a1 49
58971144 50static void maintenance_do_deprecate (const char *, int);
1c689132 51
c906108c 52#ifndef _WIN32
c906108c 53static void
58971144 54maintenance_dump_me (const char *args, int from_tty)
c906108c 55{
9e2f0ad4 56 if (query (_("Should GDB dump core? ")))
c906108c 57 {
7be570e7
JM
58#ifdef __DJGPP__
59 /* SIGQUIT by default is ignored, so use SIGABRT instead. */
60 signal (SIGABRT, SIG_DFL);
61 kill (getpid (), SIGABRT);
62#else
c906108c
SS
63 signal (SIGQUIT, SIG_DFL);
64 kill (getpid (), SIGQUIT);
7be570e7 65#endif
c906108c
SS
66 }
67}
68#endif
69
7be570e7
JM
70/* Stimulate the internal error mechanism that GDB uses when an
71 internal problem is detected. Allows testing of the mechanism.
72 Also useful when the user wants to drop a core file but not exit
025bb325 73 GDB. */
7be570e7
JM
74
75static void
5fed81ff 76maintenance_internal_error (const char *args, int from_tty)
7be570e7 77{
f34652de 78 internal_error ("%s", (args == NULL ? "" : args));
dec43320
AC
79}
80
81/* Stimulate the internal error mechanism that GDB uses when an
82 internal problem is detected. Allows testing of the mechanism.
83 Also useful when the user wants to drop a core file but not exit
025bb325 84 GDB. */
dec43320
AC
85
86static void
5fed81ff 87maintenance_internal_warning (const char *args, int from_tty)
dec43320 88{
f34652de 89 internal_warning ("%s", (args == NULL ? "" : args));
7be570e7
JM
90}
91
57fcfb1b
GB
92/* Stimulate the internal error mechanism that GDB uses when an
93 demangler problem is detected. Allows testing of the mechanism. */
94
95static void
5fed81ff 96maintenance_demangler_warning (const char *args, int from_tty)
57fcfb1b
GB
97{
98 demangler_warning (__FILE__, __LINE__, "%s", (args == NULL ? "" : args));
99}
100
439250fb
DE
101/* Old command to demangle a string. The command has been moved to "demangle".
102 It is kept for now because otherwise "mt demangle" gets interpreted as
103 "mt demangler-warning" which artificially creates an internal gdb error. */
c906108c
SS
104
105static void
58971144 106maintenance_demangle (const char *args, int from_tty)
c906108c 107{
9e69a2e1
TT
108 gdb_printf (_("This command has been moved to \"%ps\".\n"),
109 styled_string (command_style.style (), "demangle"));
c906108c
SS
110}
111
e5e619ac
TT
112/* Print the canonical form of a name. */
113
114static void
115maintenance_canonicalize (const char *args, int from_tty)
116{
117 gdb::unique_xmalloc_ptr<char> canon = cp_canonicalize_string (args);
118 if (canon == nullptr)
119 gdb_printf ("No change.\n");
120 else
121 gdb_printf ("canonical = %s\n", canon.get ());
122}
123
c906108c 124static void
58971144 125maintenance_time_display (const char *args, int from_tty)
c906108c 126{
c906108c 127 if (args == NULL || *args == '\0')
9e69a2e1
TT
128 gdb_printf (_("\"%ps\" takes a numeric argument.\n"),
129 styled_string (command_style.style (), "maintenance time"));
c906108c 130 else
bd712aed 131 set_per_command_time (strtol (args, NULL, 10));
c906108c
SS
132}
133
134static void
5fed81ff 135maintenance_space_display (const char *args, int from_tty)
c906108c 136{
c906108c 137 if (args == NULL || *args == '\0')
9e69a2e1
TT
138 gdb_printf ("\"%ps\" takes a numeric argument.\n",
139 styled_string (command_style.style (), "maintenance space"));
c906108c 140 else
bd712aed 141 set_per_command_space (strtol (args, NULL, 10));
c906108c
SS
142}
143
a532ca62
MS
144/* Mini tokenizing lexer for 'maint info sections' command. */
145
bf3386f0 146static bool
473e38f3 147match_substring (const char *string, const char *substr)
a532ca62 148{
bf3386f0 149 int substr_len = strlen (substr);
473e38f3 150 const char *tok;
a532ca62
MS
151
152 while ((tok = strstr (string, substr)) != NULL)
153 {
025bb325 154 /* Got a partial match. Is it a whole word? */
b01d807c
MS
155 if (tok == string
156 || tok[-1] == ' '
157 || tok[-1] == '\t')
a532ca62 158 {
025bb325 159 /* Token is delimited at the front... */
b01d807c
MS
160 if (tok[substr_len] == ' '
161 || tok[substr_len] == '\t'
162 || tok[substr_len] == '\0')
a532ca62
MS
163 {
164 /* Token is delimited at the rear. Got a whole-word match. */
bf3386f0 165 return true;
a532ca62
MS
166 }
167 }
168 /* Token didn't match as a whole word. Advance and try again. */
169 string = tok + 1;
170 }
bf3386f0 171 return false;
a532ca62
MS
172}
173
bf3386f0
AB
174/* Structure holding information about a single bfd section flag. This is
175 used by the "maintenance info sections" command to print the sections,
176 and for filtering which sections are printed. */
177
178struct single_bfd_flag_info
179{
180 /* The name of the section. This is what is printed for the flag, and
181 what the user enter in order to filter by flag. */
182 const char *name;
183
184 /* The bfd defined SEC_* flagword value for this flag. */
185 flagword value;
186};
187
188/* Vector of all the known bfd flags. */
189
190static const single_bfd_flag_info bfd_flag_info[] =
191 {
192 { "ALLOC", SEC_ALLOC },
193 { "LOAD", SEC_LOAD },
194 { "RELOC", SEC_RELOC },
195 { "READONLY", SEC_READONLY },
196 { "CODE", SEC_CODE },
197 { "DATA", SEC_DATA },
198 { "ROM", SEC_ROM },
199 { "CONSTRUCTOR", SEC_CONSTRUCTOR },
200 { "HAS_CONTENTS", SEC_HAS_CONTENTS },
201 { "NEVER_LOAD", SEC_NEVER_LOAD },
202 { "COFF_SHARED_LIBRARY", SEC_COFF_SHARED_LIBRARY },
203 { "IS_COMMON", SEC_IS_COMMON }
204 };
205
206/* For each flag in the global BFD_FLAG_INFO list, if FLAGS has a flag's
207 flagword value set, and STRING contains the flag's name then return
208 true, otherwise return false. STRING is never nullptr. */
209
210static bool
fc4baa5e 211match_bfd_flags (const char *string, flagword flags)
c906108c 212{
bf3386f0
AB
213 gdb_assert (string != nullptr);
214
215 for (const auto &f : bfd_flag_info)
216 {
217 if (flags & f.value
218 && match_substring (string, f.name))
219 return true;
220 }
221
222 return false;
43155bc1 223}
c906108c 224
bf3386f0
AB
225/* Print the names of all flags set in FLAGS. The names are taken from the
226 BFD_FLAG_INFO global. */
227
43155bc1
MS
228static void
229print_bfd_flags (flagword flags)
230{
bf3386f0
AB
231 for (const auto &f : bfd_flag_info)
232 {
233 if (flags & f.value)
6cb06a8c 234 gdb_printf (" %s", f.name);
bf3386f0 235 }
43155bc1
MS
236}
237
238static void
bf3386f0
AB
239maint_print_section_info (const char *name, flagword flags,
240 CORE_ADDR addr, CORE_ADDR endaddr,
5af949e3 241 unsigned long filepos, int addr_size)
43155bc1 242{
6cb06a8c
TT
243 gdb_printf (" %s", hex_string_custom (addr, addr_size));
244 gdb_printf ("->%s", hex_string_custom (endaddr, addr_size));
245 gdb_printf (" at %s",
246 hex_string_custom ((unsigned long) filepos, 8));
247 gdb_printf (": %s", name);
e3d3bfda 248 print_bfd_flags (flags);
6cb06a8c 249 gdb_printf ("\n");
e3d3bfda 250}
c906108c 251
c5065df0
SM
252/* Return the number of digits required to display COUNT in decimal.
253
254 Used when pretty printing index numbers to ensure all of the indexes line
255 up.*/
256
257static int
258index_digits (int count)
aa17805f 259{
c5065df0
SM
260 return ((int) log10 ((float) count)) + 1;
261}
aa17805f
AB
262
263/* Helper function to pretty-print the section index of ASECT from ABFD.
264 The INDEX_DIGITS is the number of digits in the largest index that will
265 be printed, and is used to pretty-print the resulting string. */
266
267static void
268print_section_index (bfd *abfd,
269 asection *asect,
270 int index_digits)
271{
272 std::string result
273 = string_printf (" [%d] ", gdb_bfd_section_index (abfd, asect));
274 /* The '+ 4' for the leading and trailing characters. */
6cb06a8c 275 gdb_printf ("%-*s", (index_digits + 4), result.c_str ());
aa17805f
AB
276}
277
b886559f
SM
278/* Print information about ASECT from ABFD. The section will be printed using
279 the VMA's from the bfd, which will not be the relocated addresses for bfds
280 that should be relocated. The information must be printed with the same
c5065df0
SM
281 layout as PRINT_OBJFILE_SECTION_INFO below.
282
283 ARG is the argument string passed by the user to the top level maintenance
284 info sections command. Used for filtering which sections are printed. */
aa17805f 285
e3d3bfda 286static void
c5065df0
SM
287print_bfd_section_info (bfd *abfd, asection *asect, const char *arg,
288 int index_digits)
e3d3bfda 289{
fd361982
AM
290 flagword flags = bfd_section_flags (asect);
291 const char *name = bfd_section_name (asect);
e3d3bfda 292
fc4baa5e
TT
293 if (arg == NULL || *arg == '\0'
294 || match_substring (arg, name)
295 || match_bfd_flags (arg, flags))
e3d3bfda 296 {
5af949e3
UW
297 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
298 int addr_size = gdbarch_addr_bit (gdbarch) / 8;
e3d3bfda
MS
299 CORE_ADDR addr, endaddr;
300
fd361982
AM
301 addr = bfd_section_vma (asect);
302 endaddr = addr + bfd_section_size (asect);
c5065df0 303 print_section_index (abfd, asect, index_digits);
5af949e3
UW
304 maint_print_section_info (name, flags, addr, endaddr,
305 asect->filepos, addr_size);
e3d3bfda
MS
306 }
307}
308
aa17805f
AB
309/* Print information about ASECT which is GDB's wrapper around a section
310 from ABFD. The information must be printed with the same layout as
311 PRINT_BFD_SECTION_INFO above. PRINT_DATA holds information used to
c5065df0
SM
312 filter which sections are printed, and for formatting the output.
313
314 ARG is the argument string passed by the user to the top level maintenance
315 info sections command. Used for filtering which sections are printed. */
aa17805f 316
e3d3bfda 317static void
c5065df0
SM
318print_objfile_section_info (bfd *abfd, struct obj_section *asect,
319 const char *arg, int index_digits)
e3d3bfda 320{
fd361982
AM
321 flagword flags = bfd_section_flags (asect->the_bfd_section);
322 const char *name = bfd_section_name (asect->the_bfd_section);
43155bc1 323
c5065df0
SM
324 if (arg == NULL || *arg == '\0'
325 || match_substring (arg, name)
326 || match_bfd_flags (arg, flags))
43155bc1 327 {
5af949e3
UW
328 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
329 int addr_size = gdbarch_addr_bit (gdbarch) / 8;
b8d56208 330
c5065df0 331 print_section_index (abfd, asect->the_bfd_section, index_digits);
f1f6aadf 332 maint_print_section_info (name, flags,
0c1bcd23 333 asect->addr (), asect->endaddr (),
5af949e3
UW
334 asect->the_bfd_section->filepos,
335 addr_size);
43155bc1 336 }
c906108c
SS
337}
338
aa17805f
AB
339/* Find an obj_section, GDB's wrapper around a bfd section for ASECTION
340 from ABFD. It might be that no such wrapper exists (for example debug
341 sections don't have such wrappers) in which case nullptr is returned. */
342
545e49f5 343obj_section *
aa17805f
AB
344maint_obj_section_from_bfd_section (bfd *abfd,
345 asection *asection,
346 objfile *ofile)
347{
9ed8433a 348 if (ofile->sections_start == nullptr)
aa17805f
AB
349 return nullptr;
350
351 obj_section *osect
9ed8433a 352 = &ofile->sections_start[gdb_bfd_section_index (abfd, asection)];
aa17805f
AB
353
354 if (osect >= ofile->sections_end)
355 return nullptr;
356
357 return osect;
358}
359
4790db14
AB
360/* Print information about all sections from ABFD, which is the bfd
361 corresponding to OBJFILE. It is fine for OBJFILE to be nullptr, but
362 ABFD must never be nullptr. If OBJFILE is provided then the sections of
363 ABFD will (potentially) be displayed relocated (i.e. the object file was
364 loaded with add-symbol-file and custom offsets were provided).
c5065df0 365
4790db14
AB
366 HEADER is a string that describes this file, e.g. 'Exec file: ', or
367 'Core file: '.
368
369 ARG is a string used for filtering which sections are printed, this can
370 be nullptr for no filtering. See the top level 'maint info sections'
371 for a fuller description of the possible filtering strings. */
aa17805f
AB
372
373static void
4790db14
AB
374maint_print_all_sections (const char *header, bfd *abfd, objfile *objfile,
375 const char *arg)
aa17805f 376{
0426ad51 377 gdb_puts (header);
1285ce86 378 gdb_stdout->wrap_here (8);
6cb06a8c 379 gdb_printf ("`%s', ", bfd_get_filename (abfd));
1285ce86 380 gdb_stdout->wrap_here (8);
6cb06a8c 381 gdb_printf (_("file type %s.\n"), bfd_get_target (abfd));
aa17805f 382
4790db14
AB
383 int section_count = gdb_bfd_count_sections (abfd);
384 int digits = index_digits (section_count);
385
386 for (asection *sect : gdb_bfd_sections (abfd))
387 {
a584cfc9
TT
388 QUIT;
389
4790db14
AB
390 obj_section *osect = nullptr;
391
392 if (objfile != nullptr)
393 {
9ed8433a 394 gdb_assert (objfile->sections_start != nullptr);
4790db14
AB
395 osect
396 = maint_obj_section_from_bfd_section (abfd, sect, objfile);
397 if (osect->the_bfd_section == nullptr)
398 osect = nullptr;
399 }
400
401 if (osect == nullptr)
402 print_bfd_section_info (abfd, sect, arg, digits);
403 else
404 print_objfile_section_info (abfd, osect, arg, digits);
405 }
aa17805f
AB
406}
407
bf3386f0
AB
408/* The options for the "maintenance info sections" command. */
409
410struct maint_info_sections_opts
411{
412 /* For "-all-objects". */
413 bool all_objects = false;
414};
415
416static const gdb::option::option_def maint_info_sections_option_defs[] = {
417
418 gdb::option::flag_option_def<maint_info_sections_opts> {
419 "all-objects",
420 [] (maint_info_sections_opts *opts) { return &opts->all_objects; },
421 N_("Display information from all loaded object files."),
422 },
423};
424
425/* Create an option_def_group for the "maintenance info sections" options,
426 with CC_OPTS as context. */
427
428static inline gdb::option::option_def_group
429make_maint_info_sections_options_def_group (maint_info_sections_opts *cc_opts)
430{
431 return {{maint_info_sections_option_defs}, cc_opts};
432}
433
434/* Completion for the "maintenance info sections" command. */
435
436static void
437maint_info_sections_completer (struct cmd_list_element *cmd,
438 completion_tracker &tracker,
439 const char *text, const char * /* word */)
440{
441 /* Complete command options. */
442 const auto group = make_maint_info_sections_options_def_group (nullptr);
443 if (gdb::option::complete_options
444 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
445 return;
446 const char *word = advance_to_expression_complete_word_point (tracker, text);
447
448 /* Offer completion for section flags, but not section names. This is
449 only a maintenance command after all, no point going over the top. */
450 std::vector<const char *> flags;
451 for (const auto &f : bfd_flag_info)
452 flags.push_back (f.name);
453 flags.push_back (nullptr);
454 complete_on_enum (tracker, flags.data (), text, word);
455}
456
aa17805f
AB
457/* Implement the "maintenance info sections" command. */
458
c906108c 459static void
58971144 460maintenance_info_sections (const char *arg, int from_tty)
c906108c 461{
bf3386f0
AB
462 /* Check if the "-all-objects" flag was passed. */
463 maint_info_sections_opts opts;
464 const auto group = make_maint_info_sections_options_def_group (&opts);
465 gdb::option::process_options
466 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
c906108c 467
4790db14 468 for (objfile *ofile : current_program_space->objfiles ())
c906108c 469 {
4790db14 470 if (ofile->obfd == current_program_space->exec_bfd ())
98badbfd
TT
471 maint_print_all_sections (_("Exec file: "), ofile->obfd.get (),
472 ofile, arg);
bf3386f0 473 else if (opts.all_objects)
98badbfd
TT
474 maint_print_all_sections (_("Object file: "), ofile->obfd.get (),
475 ofile, arg);
c906108c 476 }
4790db14 477
6fdf95ae
SM
478 if (current_program_space->core_bfd () != nullptr)
479 maint_print_all_sections (_("Core file: "),
480 current_program_space->core_bfd (), nullptr, arg);
c906108c
SS
481}
482
02a79309
AB
483/* Implement the "maintenance info target-sections" command. */
484
485static void
486maintenance_info_target_sections (const char *arg, int from_tty)
487{
488 bfd *abfd = nullptr;
489 int digits = 0;
25b5a04e 490 const std::vector<target_section> *table
328d42d8 491 = target_get_section_table (current_inferior ()->top_target ());
02a79309
AB
492 if (table == nullptr)
493 return;
494
495 for (const target_section &sec : *table)
496 {
497 if (abfd == nullptr || sec.the_bfd_section->owner != abfd)
498 {
499 abfd = sec.the_bfd_section->owner;
500 digits = std::max (index_digits (gdb_bfd_count_sections (abfd)),
501 digits);
502 }
503 }
504
505 struct gdbarch *gdbarch = nullptr;
506 int addr_size = 0;
507 abfd = nullptr;
508 for (const target_section &sec : *table)
509 {
510 if (sec.the_bfd_section->owner != abfd)
511 {
512 abfd = sec.the_bfd_section->owner;
513 gdbarch = gdbarch_from_bfd (abfd);
514 addr_size = gdbarch_addr_bit (gdbarch) / 8;
515
6cb06a8c
TT
516 gdb_printf (_("From '%s', file type %s:\n"),
517 bfd_get_filename (abfd), bfd_get_target (abfd));
02a79309
AB
518 }
519 print_bfd_section_info (abfd,
520 sec.the_bfd_section,
521 nullptr,
522 digits);
523 /* The magic '8 + digits' here ensures that the 'Start' is aligned
524 with the output of print_bfd_section_info. */
6cb06a8c
TT
525 gdb_printf ("%*sStart: %s, End: %s, Owner token: %p\n",
526 (8 + digits), "",
527 hex_string_custom (sec.addr, addr_size),
528 hex_string_custom (sec.endaddr, addr_size),
0e17d3fc 529 sec.owner.v ());
02a79309
AB
530 }
531}
532
025cfdb2 533static void
58971144 534maintenance_print_statistics (const char *args, int from_tty)
c906108c
SS
535{
536 print_objfile_statistics ();
c906108c
SS
537}
538
b9362cc7 539static void
5fed81ff 540maintenance_print_architecture (const char *args, int from_tty)
4b9b3959 541{
e17c207e
UW
542 struct gdbarch *gdbarch = get_current_arch ();
543
4b9b3959 544 if (args == NULL)
e17c207e 545 gdbarch_dump (gdbarch, gdb_stdout);
4b9b3959
AC
546 else
547 {
d7e74731 548 stdio_file file;
b8d56208 549
d7e74731 550 if (!file.open (args, "w"))
e2e0b3e5 551 perror_with_name (_("maintenance print architecture"));
d7e74731 552 gdbarch_dump (gdbarch, &file);
4b9b3959
AC
553 }
554}
555
c906108c
SS
556/* The "maintenance translate-address" command converts a section and address
557 to a symbol. This can be called in two ways:
c5aa993b 558 maintenance translate-address <secname> <addr>
025bb325 559 or maintenance translate-address <addr>. */
c906108c
SS
560
561static void
5fed81ff 562maintenance_translate_address (const char *arg, int from_tty)
c906108c
SS
563{
564 CORE_ADDR address;
714835d5 565 struct obj_section *sect;
5fed81ff 566 const char *p;
c906108c
SS
567
568 if (arg == NULL || *arg == 0)
8a3fe4f8 569 error (_("requires argument (address or section + address)"));
c906108c
SS
570
571 sect = NULL;
572 p = arg;
573
574 if (!isdigit (*p))
025bb325
MS
575 { /* See if we have a valid section name. */
576 while (*p && !isspace (*p)) /* Find end of section name. */
c906108c 577 p++;
025bb325 578 if (*p == '\000') /* End of command? */
65e65158 579 error (_("Need to specify section name and address"));
5fed81ff
TT
580
581 int arg_len = p - arg;
582 p = skip_spaces (p + 1);
c906108c 583
2030c079 584 for (objfile *objfile : current_program_space->objfiles ())
5250cbc8 585 for (obj_section *iter : objfile->sections ())
3b9d3ac2 586 {
5250cbc8 587 if (strncmp (iter->the_bfd_section->name, arg, arg_len) == 0)
3b9d3ac2
TT
588 goto found;
589 }
c906108c 590
3b9d3ac2
TT
591 error (_("Unknown section %s."), arg);
592 found: ;
c906108c
SS
593 }
594
595 address = parse_and_eval_address (p);
596
03b40f6f 597 bound_minimal_symbol sym;
c906108c
SS
598 if (sect)
599 sym = lookup_minimal_symbol_by_pc_section (address, sect);
600 else
601 sym = lookup_minimal_symbol_by_pc (address);
602
7cbd4a93 603 if (sym.minsym)
c14c28ba 604 {
c9d95fa3 605 const char *symbol_name = sym.minsym->print_name ();
3e43a32a 606 const char *symbol_offset
4aeddc50 607 = pulongest (address - sym.value_address ());
c14c28ba 608
1db66e34 609 sect = sym.obj_section ();
c14c28ba
PP
610 if (sect != NULL)
611 {
612 const char *section_name;
613 const char *obj_name;
614
615 gdb_assert (sect->the_bfd_section && sect->the_bfd_section->name);
616 section_name = sect->the_bfd_section->name;
617
4262abfb
JK
618 gdb_assert (sect->objfile && objfile_name (sect->objfile));
619 obj_name = objfile_name (sect->objfile);
c14c28ba 620
deeafabb 621 if (current_program_space->multi_objfile_p ())
6cb06a8c
TT
622 gdb_printf (_("%s + %s in section %s of %s\n"),
623 symbol_name, symbol_offset,
624 section_name, obj_name);
c14c28ba 625 else
6cb06a8c
TT
626 gdb_printf (_("%s + %s in section %s\n"),
627 symbol_name, symbol_offset, section_name);
c14c28ba
PP
628 }
629 else
6cb06a8c 630 gdb_printf (_("%s + %s\n"), symbol_name, symbol_offset);
c14c28ba 631 }
c906108c 632 else if (sect)
6cb06a8c
TT
633 gdb_printf (_("no symbol at %s:%s\n"),
634 sect->the_bfd_section->name, hex_string (address));
c906108c 635 else
6cb06a8c 636 gdb_printf (_("no symbol at %s\n"), hex_string (address));
c906108c
SS
637
638 return;
639}
640
56382845 641
c114dcd5 642/* When a command is deprecated the user will be warned the first time
33f91161 643 the command is used. If possible, a replacement will be
025bb325 644 offered. */
56382845
FN
645
646static void
58971144 647maintenance_deprecate (const char *args, int from_tty)
56382845
FN
648{
649 if (args == NULL || *args == '\0')
650 {
9e69a2e1 651 gdb_printf (_("\"%ps\" takes an argument,\n\
cce7e648 652the command you want to deprecate, and optionally the replacement command\n\
9e69a2e1
TT
653enclosed in quotes.\n"),
654 styled_string (command_style.style (),
655 "maintenance deprecate"));
56382845 656 }
33f91161 657
56382845 658 maintenance_do_deprecate (args, 1);
56382845
FN
659}
660
661
662static void
58971144 663maintenance_undeprecate (const char *args, int from_tty)
56382845
FN
664{
665 if (args == NULL || *args == '\0')
666 {
9e69a2e1
TT
667 gdb_printf (_("\"%ps\" takes an argument, \n\
668the command you want to undeprecate.\n"),
669 styled_string (command_style.style (),
670 "maintenance undeprecate"));
56382845 671 }
33f91161 672
56382845 673 maintenance_do_deprecate (args, 0);
56382845
FN
674}
675
025bb325 676/* You really shouldn't be using this. It is just for the testsuite.
33f91161
AC
677 Rather, you should use deprecate_cmd() when the command is created
678 in _initialize_blah().
679
680 This function deprecates a command and optionally assigns it a
681 replacement. */
682
8399535b 683static void
58971144 684maintenance_do_deprecate (const char *text, int deprecate)
33f91161 685{
33f91161
AC
686 struct cmd_list_element *alias = NULL;
687 struct cmd_list_element *prefix_cmd = NULL;
688 struct cmd_list_element *cmd = NULL;
689
58971144
TT
690 const char *start_ptr = NULL;
691 const char *end_ptr = NULL;
56382845 692 int len;
33f91161
AC
693 char *replacement = NULL;
694
1c689132
DB
695 if (text == NULL)
696 return;
56382845 697
33f91161
AC
698 if (!lookup_cmd_composition (text, &alias, &prefix_cmd, &cmd))
699 {
6cb06a8c 700 gdb_printf (_("Can't find command '%s' to deprecate.\n"), text);
33f91161
AC
701 return;
702 }
56382845 703
56382845
FN
704 if (deprecate)
705 {
025bb325 706 /* Look for a replacement command. */
80ce1ecb
AC
707 start_ptr = strchr (text, '\"');
708 if (start_ptr != NULL)
33f91161
AC
709 {
710 start_ptr++;
80ce1ecb
AC
711 end_ptr = strrchr (start_ptr, '\"');
712 if (end_ptr != NULL)
33f91161
AC
713 {
714 len = end_ptr - start_ptr;
58971144 715 replacement = savestring (start_ptr, len);
33f91161
AC
716 }
717 }
56382845 718 }
33f91161 719
56382845
FN
720 if (!start_ptr || !end_ptr)
721 replacement = NULL;
33f91161
AC
722
723
56382845 724 /* If they used an alias, we only want to deprecate the alias.
33f91161 725
56382845
FN
726 Note the MALLOCED_REPLACEMENT test. If the command's replacement
727 string was allocated at compile time we don't want to free the
025bb325 728 memory. */
56382845
FN
729 if (alias)
730 {
1f2bdf09 731 if (alias->malloced_replacement)
429e55ea 732 xfree ((char *) alias->replacement);
33f91161 733
56382845 734 if (deprecate)
1f2bdf09
TT
735 {
736 alias->deprecated_warn_user = 1;
737 alias->cmd_deprecated = 1;
738 }
56382845 739 else
1f2bdf09
TT
740 {
741 alias->deprecated_warn_user = 0;
742 alias->cmd_deprecated = 0;
743 }
33f91161 744 alias->replacement = replacement;
1f2bdf09 745 alias->malloced_replacement = 1;
56382845
FN
746 return;
747 }
748 else if (cmd)
749 {
1f2bdf09 750 if (cmd->malloced_replacement)
429e55ea 751 xfree ((char *) cmd->replacement);
56382845
FN
752
753 if (deprecate)
1f2bdf09
TT
754 {
755 cmd->deprecated_warn_user = 1;
756 cmd->cmd_deprecated = 1;
757 }
56382845 758 else
1f2bdf09
TT
759 {
760 cmd->deprecated_warn_user = 0;
761 cmd->cmd_deprecated = 0;
762 }
33f91161 763 cmd->replacement = replacement;
1f2bdf09 764 cmd->malloced_replacement = 1;
56382845
FN
765 return;
766 }
240f9570 767 xfree (replacement);
56382845
FN
768}
769
4f337972
AC
770/* Maintenance set/show framework. */
771
ae038cb0
DJ
772struct cmd_list_element *maintenance_set_cmdlist;
773struct cmd_list_element *maintenance_show_cmdlist;
4f337972 774
fdbc9870
PA
775/* "maintenance with" command. */
776
777static void
778maintenance_with_cmd (const char *args, int from_tty)
779{
780 with_command_1 ("maintenance set ", maintenance_set_cmdlist, args, from_tty);
781}
782
783/* "maintenance with" command completer. */
784
785static void
786maintenance_with_cmd_completer (struct cmd_list_element *ignore,
787 completion_tracker &tracker,
788 const char *text, const char * /*word*/)
789{
790 with_command_completer_1 ("maintenance set ", tracker, text);
791}
792
4f337972
AC
793/* Profiling support. */
794
491144b5 795static bool maintenance_profile_p;
920d2a44
AC
796static void
797show_maintenance_profile_p (struct ui_file *file, int from_tty,
798 struct cmd_list_element *c, const char *value)
799{
6cb06a8c 800 gdb_printf (file, _("Internal profiling is %s.\n"), value);
920d2a44 801}
d9feb4e7 802
b0b1c2c0
MK
803#ifdef HAVE__ETEXT
804extern char _etext;
805#define TEXTEND &_etext
01fe12f6 806#elif defined (HAVE_ETEXT)
b0b1c2c0
MK
807extern char etext;
808#define TEXTEND &etext
809#endif
810
01fe12f6
JB
811#if defined (HAVE_MONSTARTUP) && defined (HAVE__MCLEANUP) && defined (TEXTEND)
812
d28f9cdf
DJ
813static int profiling_state;
814
cf0d07fd 815extern "C" void _mcleanup (void);
56000a98 816
d28f9cdf
DJ
817static void
818mcleanup_wrapper (void)
819{
d28f9cdf
DJ
820 if (profiling_state)
821 _mcleanup ();
822}
4f337972 823
cf0d07fd 824extern "C" void monstartup (unsigned long, unsigned long);
527907e0 825extern int main (int, char **);
56000a98 826
4f337972 827static void
eb4c3f4a 828maintenance_set_profile_cmd (const char *args, int from_tty,
3e43a32a 829 struct cmd_list_element *c)
4f337972 830{
d28f9cdf
DJ
831 if (maintenance_profile_p == profiling_state)
832 return;
833
834 profiling_state = maintenance_profile_p;
835
836 if (maintenance_profile_p)
837 {
838 static int profiling_initialized;
839
d28f9cdf
DJ
840 if (!profiling_initialized)
841 {
842 atexit (mcleanup_wrapper);
843 profiling_initialized = 1;
844 }
845
846 /* "main" is now always the first function in the text segment, so use
847 its address for monstartup. */
b0b1c2c0 848 monstartup ((unsigned long) &main, (unsigned long) TEXTEND);
d28f9cdf
DJ
849 }
850 else
851 {
852 extern void _mcleanup (void);
b8d56208 853
d28f9cdf
DJ
854 _mcleanup ();
855 }
4f337972 856}
d9feb4e7
DJ
857#else
858static void
eb4c3f4a 859maintenance_set_profile_cmd (const char *args, int from_tty,
3e43a32a 860 struct cmd_list_element *c)
d9feb4e7 861{
8a3fe4f8 862 error (_("Profiling support is not available on this system."));
d9feb4e7
DJ
863}
864#endif
22138db6 865
f1d293cc 866static int n_worker_threads = -1;
22138db6 867
33ae4543
RB
868/* See maint.h. */
869
870void
22138db6
TT
871update_thread_pool_size ()
872{
873#if CXX_STD_THREAD
874 int n_threads = n_worker_threads;
875
876 if (n_threads < 0)
33ae4543
RB
877 {
878 const int hardware_threads = std::thread::hardware_concurrency ();
5a22e042
AB
879 /* Testing in PR gdb/29959 indicates that parallel efficiency drops
880 between n_threads=5 to 8. Therefore, use no more than 8 threads
881 to avoid an excessive number of threads in the pool on many-core
882 systems. */
883 const int max_thread_count = 8;
884 n_threads = std::min (hardware_threads, max_thread_count);
33ae4543 885 }
22138db6
TT
886
887 gdb::thread_pool::g_thread_pool->set_thread_count (n_threads);
888#endif
889}
890
891static void
892maintenance_set_worker_threads (const char *args, int from_tty,
893 struct cmd_list_element *c)
894{
895 update_thread_pool_size ();
896}
897
ee8b8845
TT
898static void
899maintenance_show_worker_threads (struct ui_file *file, int from_tty,
900 struct cmd_list_element *c,
901 const char *value)
902{
42f46152 903#if CXX_STD_THREAD
ee8b8845 904 if (n_worker_threads == -1)
42f46152 905 {
6cb06a8c 906 gdb_printf (file, _("The number of worker threads GDB "
33ae4543 907 "can use is the default (currently %zu).\n"),
6cb06a8c 908 gdb::thread_pool::g_thread_pool->thread_count ());
42f46152
TV
909 return;
910 }
911#endif
912
913 int report_threads = 0;
914#if CXX_STD_THREAD
915 report_threads = n_worker_threads;
916#endif
6cb06a8c
TT
917 gdb_printf (file, _("The number of worker threads GDB "
918 "can use is %d.\n"),
919 report_threads);
ee8b8845
TT
920}
921
bd712aed 922\f
c6115b5e 923/* See maint.h. */
56382845 924
c6115b5e 925bool per_command_time;
bd712aed 926
491144b5 927/* If true, display space usage both at startup and for each command. */
bd712aed 928
491144b5 929static bool per_command_space;
bd712aed 930
491144b5 931/* If true, display basic symtab stats for each command. */
bd712aed 932
491144b5 933static bool per_command_symtab;
bd712aed
DE
934
935/* mt per-command commands. */
936
937static struct cmd_list_element *per_command_setlist;
938static struct cmd_list_element *per_command_showlist;
939
bd712aed
DE
940/* Set whether to display time statistics to NEW_VALUE
941 (non-zero means true). */
942
943void
944set_per_command_time (int new_value)
945{
946 per_command_time = new_value;
947}
948
949/* Set whether to display space statistics to NEW_VALUE
950 (non-zero means true). */
951
952void
953set_per_command_space (int new_value)
954{
955 per_command_space = new_value;
956}
957
958/* Count the number of symtabs and blocks. */
959
960static void
43f3e411 961count_symtabs_and_blocks (int *nr_symtabs_ptr, int *nr_compunit_symtabs_ptr,
bd712aed
DE
962 int *nr_blocks_ptr)
963{
bd712aed 964 int nr_symtabs = 0;
43f3e411 965 int nr_compunit_symtabs = 0;
bd712aed
DE
966 int nr_blocks = 0;
967
b8b8facf
DE
968 /* When collecting statistics during startup, this is called before
969 pretty much anything in gdb has been initialized, and thus
970 current_program_space may be NULL. */
971 if (current_program_space != NULL)
bd712aed 972 {
2030c079 973 for (objfile *o : current_program_space->objfiles ())
bd712aed 974 {
b669c953 975 for (compunit_symtab *cu : o->compunits ())
d8aeb77f
TT
976 {
977 ++nr_compunit_symtabs;
63d609de 978 nr_blocks += cu->blockvector ()->num_blocks ();
102cc235
SM
979 nr_symtabs += std::distance (cu->filetabs ().begin (),
980 cu->filetabs ().end ());
d8aeb77f 981 }
bd712aed
DE
982 }
983 }
984
985 *nr_symtabs_ptr = nr_symtabs;
43f3e411 986 *nr_compunit_symtabs_ptr = nr_compunit_symtabs;
bd712aed
DE
987 *nr_blocks_ptr = nr_blocks;
988}
989
1e3b796d
TT
990/* As indicated by display_time and display_space, report GDB's
991 elapsed time and space usage from the base time and space recorded
992 in this object. */
bd712aed 993
1e3b796d 994scoped_command_stats::~scoped_command_stats ()
bd712aed 995{
1e3b796d
TT
996 /* Early exit if we're not reporting any stats. It can be expensive to
997 compute the pre-command values so don't collect them at all if we're
998 not reporting stats. Alas this doesn't work in the startup case because
999 we don't know yet whether we will be reporting the stats. For the
1000 startup case collect the data anyway (it should be cheap at this point),
1001 and leave it to the reporter to decide whether to print them. */
1002 if (m_msg_type
1003 && !per_command_time
1004 && !per_command_space
1005 && !per_command_symtab)
1006 return;
bd712aed 1007
1e3b796d 1008 if (m_time_enabled && per_command_time)
bd712aed 1009 {
3847a7bf
TT
1010 print_time (_("command finished"));
1011
dcb07cfa 1012 using namespace std::chrono;
bd712aed 1013
dcb07cfa
PA
1014 run_time_clock::duration cmd_time
1015 = run_time_clock::now () - m_start_cpu_time;
bd712aed 1016
dcb07cfa
PA
1017 steady_clock::duration wall_time
1018 = steady_clock::now () - m_start_wall_time;
bd712aed 1019 /* Subtract time spend in prompt_for_continue from walltime. */
dcb07cfa 1020 wall_time -= get_prompt_for_continue_wait_time ();
bd712aed 1021
6cb06a8c
TT
1022 gdb_printf (gdb_stdlog,
1023 !m_msg_type
1024 ? _("Startup time: %.6f (cpu), %.6f (wall)\n")
1025 : _("Command execution time: %.6f (cpu), %.6f (wall)\n"),
1026 duration<double> (cmd_time).count (),
1027 duration<double> (wall_time).count ());
bd712aed
DE
1028 }
1029
1e3b796d 1030 if (m_space_enabled && per_command_space)
bd712aed 1031 {
6242c6a6 1032#ifdef HAVE_USEFUL_SBRK
bd712aed
DE
1033 char *lim = (char *) sbrk (0);
1034
1035 long space_now = lim - lim_at_start;
1e3b796d 1036 long space_diff = space_now - m_start_space;
bd712aed 1037
6cb06a8c
TT
1038 gdb_printf (gdb_stdlog,
1039 !m_msg_type
1040 ? _("Space used: %ld (%s%ld during startup)\n")
1041 : _("Space used: %ld (%s%ld for this command)\n"),
1042 space_now,
1043 (space_diff >= 0 ? "+" : ""),
1044 space_diff);
bd712aed
DE
1045#endif
1046 }
1047
1e3b796d 1048 if (m_symtab_enabled && per_command_symtab)
bd712aed 1049 {
43f3e411 1050 int nr_symtabs, nr_compunit_symtabs, nr_blocks;
bd712aed 1051
43f3e411 1052 count_symtabs_and_blocks (&nr_symtabs, &nr_compunit_symtabs, &nr_blocks);
6cb06a8c
TT
1053 gdb_printf (gdb_stdlog,
1054 _("#symtabs: %d (+%d),"
1055 " #compunits: %d (+%d),"
1056 " #blocks: %d (+%d)\n"),
1057 nr_symtabs,
1058 nr_symtabs - m_start_nr_symtabs,
1059 nr_compunit_symtabs,
1060 (nr_compunit_symtabs
1061 - m_start_nr_compunit_symtabs),
1062 nr_blocks,
1063 nr_blocks - m_start_nr_blocks);
bd712aed
DE
1064 }
1065}
1066
1e3b796d
TT
1067scoped_command_stats::scoped_command_stats (bool msg_type)
1068: m_msg_type (msg_type)
bd712aed 1069{
1e3b796d 1070 if (!m_msg_type || per_command_space)
bd712aed 1071 {
6242c6a6 1072#ifdef HAVE_USEFUL_SBRK
bd712aed 1073 char *lim = (char *) sbrk (0);
1e3b796d 1074 m_start_space = lim - lim_at_start;
59d49a8d 1075 m_space_enabled = true;
bd712aed
DE
1076#endif
1077 }
44d83468 1078 else
59d49a8d 1079 m_space_enabled = false;
bd712aed 1080
b8b8facf 1081 if (msg_type == 0 || per_command_time)
bd712aed 1082 {
dcb07cfa
PA
1083 using namespace std::chrono;
1084
1085 m_start_cpu_time = run_time_clock::now ();
1086 m_start_wall_time = steady_clock::now ();
59d49a8d 1087 m_time_enabled = true;
3847a7bf
TT
1088
1089 if (per_command_time)
1090 print_time (_("command started"));
bd712aed 1091 }
44d83468 1092 else
59d49a8d 1093 m_time_enabled = false;
bd712aed 1094
b8b8facf 1095 if (msg_type == 0 || per_command_symtab)
bd712aed 1096 {
43f3e411 1097 int nr_symtabs, nr_compunit_symtabs, nr_blocks;
bd712aed 1098
43f3e411 1099 count_symtabs_and_blocks (&nr_symtabs, &nr_compunit_symtabs, &nr_blocks);
1e3b796d
TT
1100 m_start_nr_symtabs = nr_symtabs;
1101 m_start_nr_compunit_symtabs = nr_compunit_symtabs;
1102 m_start_nr_blocks = nr_blocks;
59d49a8d 1103 m_symtab_enabled = true;
bd712aed 1104 }
44d83468 1105 else
59d49a8d 1106 m_symtab_enabled = false;
bd712aed 1107
26c4b26f 1108 /* Initialize timer to keep track of how long we waited for the user. */
bd712aed 1109 reset_prompt_for_continue_wait_time ();
bd712aed
DE
1110}
1111
3847a7bf
TT
1112/* See maint.h. */
1113
1114void
1115scoped_command_stats::print_time (const char *msg)
1116{
1117 using namespace std::chrono;
1118
1119 auto now = system_clock::now ();
1120 auto ticks = now.time_since_epoch ().count () / (1000 * 1000);
1121 auto millis = ticks % 1000;
1122
1123 std::time_t as_time = system_clock::to_time_t (now);
53fea9c7
CB
1124 struct tm tm;
1125 localtime_r (&as_time, &tm);
3847a7bf
TT
1126
1127 char out[100];
53fea9c7 1128 strftime (out, sizeof (out), "%F %H:%M:%S", &tm);
3847a7bf 1129
6cb06a8c 1130 gdb_printf (gdb_stdlog, "%s.%03d - %s\n", out, (int) millis, msg);
3847a7bf
TT
1131}
1132
bd712aed
DE
1133/* Handle unknown "mt set per-command" arguments.
1134 In this case have "mt set per-command on|off" affect every setting. */
1135
1136static void
981a3fb3 1137set_per_command_cmd (const char *args, int from_tty)
bd712aed
DE
1138{
1139 struct cmd_list_element *list;
bd712aed
DE
1140 int val;
1141
1142 val = parse_cli_boolean_value (args);
1143 if (val < 0)
1144 error (_("Bad value for 'mt set per-command no'."));
1145
1146 for (list = per_command_setlist; list != NULL; list = list->next)
1d7fe7f0 1147 if (list->var->type () == var_boolean)
bd712aed
DE
1148 {
1149 gdb_assert (list->type == set_cmd);
1150 do_set_command (args, from_tty, list);
1151 }
1152}
1153
42339bc4
PA
1154/* Handle "mt set per-command time". Warn if per-thread run time
1155 information is not possible. */
1156
1157static void
1158maintenance_set_command_time_cmd (const char *args, int from_tty,
1159 cmd_list_element *c)
1160{
1161 /* No point warning if this platform can't use multiple threads at
1162 all. */
1163#if CXX_STD_THREAD
1164 static bool already_warned = false;
1165 if (per_command_time
1166 && !get_run_time_thread_scope_available ()
1167 && !already_warned)
1168 {
1169 warning (_("\
1170per-thread run time information not available on this platform"));
bc5237a2 1171 already_warned = true;
42339bc4
PA
1172 }
1173#endif
1174}
1175
0b79576c
SM
1176/* See maint.h. */
1177
c6115b5e
TV
1178scoped_time_it::scoped_time_it (const char *what, bool enabled)
1179 : m_enabled (enabled),
0b79576c
SM
1180 m_what (what),
1181 m_start_wall (m_enabled
1182 ? std::chrono::steady_clock::now ()
1183 : std::chrono::steady_clock::time_point ())
1184{
1185 if (m_enabled)
1186 get_run_time (m_start_user, m_start_sys, run_time_scope::thread);
1187}
1188
1189/* See maint.h. */
1190
1191scoped_time_it::~scoped_time_it ()
1192{
1193 if (!m_enabled)
1194 return;
1195
1196 namespace chr = std::chrono;
1197 auto end_wall = chr::steady_clock::now ();
1198
1199 user_cpu_time_clock::time_point end_user;
1200 system_cpu_time_clock::time_point end_sys;
1201 get_run_time (end_user, end_sys, run_time_scope::thread);
1202
1203 auto user = end_user - m_start_user;
1204 auto sys = end_sys - m_start_sys;
1205 auto wall = end_wall - m_start_wall;
1206 auto user_ms = chr::duration_cast<chr::milliseconds> (user).count ();
1207 auto sys_ms = chr::duration_cast<chr::milliseconds> (sys).count ();
1208 auto wall_ms = chr::duration_cast<chr::milliseconds> (wall).count ();
1209 auto user_plus_sys_ms = user_ms + sys_ms;
1210
1211 auto str
1212 = string_printf ("Time for \"%s\": wall %.03f, user %.03f, sys %.03f, "
1213 "user+sys %.03f, %.01f %% CPU\n",
1214 m_what, wall_ms / 1000.0, user_ms / 1000.0,
1215 sys_ms / 1000.0, user_plus_sys_ms / 1000.0,
1216 user_plus_sys_ms * 100.0 / wall_ms);
1217 gdb_stdlog->write_async_safe (str.data (), str.size ());
1218}
1219
a1ff87d7
SM
1220/* Options affecting the "maintenance selftest" command. */
1221
1222struct maintenance_selftest_options
1223{
1224 bool verbose = false;
1225} user_maintenance_selftest_options;
1226
1227static const gdb::option::option_def maintenance_selftest_option_defs[] = {
1228 gdb::option::boolean_option_def<maintenance_selftest_options> {
1229 "verbose",
1230 [] (maintenance_selftest_options *opt) { return &opt->verbose; },
1231 nullptr,
1232 N_("Set whether selftests run in verbose mode."),
1233 N_("Show whether selftests run in verbose mode."),
1234 N_("\
1235When on, selftests may print verbose information."),
1236 },
1237};
1238
1239/* Make option groups for the "maintenance selftest" command. */
1240
1241static std::array<gdb::option::option_def_group, 1>
1242make_maintenance_selftest_option_group (maintenance_selftest_options *opts)
1243{
1244 return {{
1245 {{maintenance_selftest_option_defs}, opts},
1246 }};
1247}
dcd1f979
TT
1248
1249/* The "maintenance selftest" command. */
1250
1251static void
58971144 1252maintenance_selftest (const char *args, int from_tty)
dcd1f979 1253{
1e5ded6c 1254#if GDB_SELF_TEST
a1ff87d7
SM
1255 maintenance_selftest_options opts = user_maintenance_selftest_options;
1256 auto grp = make_maintenance_selftest_option_group (&opts);
1257 gdb::option::process_options
1258 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
e92f2b5e 1259 const gdb_argv argv (args);
a1ff87d7 1260 selftests::run_tests (argv.as_array_view (), opts.verbose);
1e5ded6c 1261#else
6cb06a8c 1262 gdb_printf (_("\
8ecfd7bd 1263Selftests have been disabled for this build.\n"));
1e5ded6c 1264#endif
1526853e
SM
1265}
1266
a1ff87d7
SM
1267/* Completer for the "maintenance selftest" command. */
1268
1269static void
1270maintenance_selftest_completer (cmd_list_element *cmd,
1271 completion_tracker &tracker,
1272 const char *text,
1273 const char *word)
1274{
1275 auto grp = make_maintenance_selftest_option_group (nullptr);
1276
c0492bea
SM
1277 if (gdb::option::complete_options
1278 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
1279 return;
1280
b88e456f 1281#if GDB_SELF_TEST
c57207c1 1282 for (const auto &test : selftests::all_selftests ())
c0492bea 1283 {
c57207c1
LS
1284 if (startswith (test.name.c_str (), text))
1285 tracker.add_completion (make_unique_xstrdup (test.name.c_str ()));
1286 }
b88e456f 1287#endif
a1ff87d7
SM
1288}
1289
1526853e 1290static void
5fed81ff 1291maintenance_info_selftests (const char *arg, int from_tty)
1526853e 1292{
1e5ded6c 1293#if GDB_SELF_TEST
6cb06a8c 1294 gdb_printf ("Registered selftests:\n");
c57207c1
LS
1295 for (const auto &test : selftests::all_selftests ())
1296 gdb_printf (" - %s\n", test.name.c_str ());
1e5ded6c 1297#else
6cb06a8c 1298 gdb_printf (_("\
8ecfd7bd 1299Selftests have been disabled for this build.\n"));
1e5ded6c 1300#endif
dcd1f979
TT
1301}
1302
bd712aed 1303\f
5fe70629 1304INIT_GDB_FILE (maint_cmds)
c906108c 1305{
439250fb
DE
1306 struct cmd_list_element *cmd;
1307
3947f654
SM
1308 cmd_list_element *maintenance_cmd
1309 = add_basic_prefix_cmd ("maintenance", class_maintenance, _("\
1bedd215 1310Commands for use by GDB maintainers.\n\
c906108c 1311Includes commands to dump specific internal GDB structures in\n\
439250fb 1312a human readable form, to cause GDB to deliberately dump core, etc."),
3947f654
SM
1313 &maintenancelist, 0,
1314 &cmdlist);
c906108c 1315
3947f654 1316 add_com_alias ("mt", maintenance_cmd, class_maintenance, 1);
c906108c 1317
5e84b7ee
SM
1318 cmd_list_element *maintenance_info_cmd
1319 = add_basic_prefix_cmd ("info", class_maintenance, _("\
1bedd215 1320Commands for showing internal info about the program being debugged."),
5e84b7ee
SM
1321 &maintenanceinfolist, 0,
1322 &maintenancelist);
1323 add_alias_cmd ("i", maintenance_info_cmd, class_maintenance, 1,
1324 &maintenancelist);
c906108c 1325
bf3386f0
AB
1326 const auto opts = make_maint_info_sections_options_def_group (nullptr);
1327 static std::string maint_info_sections_command_help
1328 = gdb::option::build_help (_("\
590042fc 1329List the BFD sections of the exec and core files.\n\
bf3386f0
AB
1330\n\
1331Usage: maintenance info sections [-all-objects] [FILTERS]\n\
1332\n\
1333FILTERS is a list of words, each word is either:\n\
1334 + A section name - any section with this name will be printed, or\n\
1335 + A section flag - any section with this flag will be printed. The\n\
287de656 1336 known flags are:\n\
bf3386f0
AB
1337 ALLOC LOAD RELOC READONLY CODE DATA ROM CONSTRUCTOR\n\
1338 HAS_CONTENTS NEVER_LOAD COFF_SHARED_LIBRARY IS_COMMON\n\
1339\n\
1340Sections matching any of the FILTERS will be listed (no FILTERS implies\n\
1341all sections should be printed).\n\
1342\n\
1343Options:\n\
1344%OPTIONS%"), opts);
1345 cmd = add_cmd ("sections", class_maintenance, maintenance_info_sections,
1346 maint_info_sections_command_help.c_str (),
1347 &maintenanceinfolist);
1348 set_cmd_completer_handle_brkchars (cmd, maint_info_sections_completer);
c906108c 1349
02a79309
AB
1350 add_cmd ("target-sections", class_maintenance,
1351 maintenance_info_target_sections, _("\
1352List GDB's internal section table.\n\
1353\n\
1354Print the current targets section list. This is a sub-set of all\n\
1355sections, from all objects currently loaded. Usually the ALLOC\n\
8def33e6 1356sections."),
02a79309
AB
1357 &maintenanceinfolist);
1358
0743fc83
TT
1359 add_basic_prefix_cmd ("print", class_maintenance,
1360 _("Maintenance command for printing GDB internal state."),
2f822da5 1361 &maintenanceprintlist, 0,
0743fc83 1362 &maintenancelist);
c906108c 1363
50a5f187
AB
1364 add_basic_prefix_cmd ("flush", class_maintenance,
1365 _("Maintenance command for flushing GDB internal caches."),
2f822da5 1366 &maintenanceflushlist, 0,
50a5f187
AB
1367 &maintenancelist);
1368
0743fc83 1369 add_basic_prefix_cmd ("set", class_maintenance, _("\
4f337972 1370Set GDB internal variables used by the GDB maintainer.\n\
1bedd215 1371Configure variables internal to GDB that aid in GDB's maintenance"),
2f822da5 1372 &maintenance_set_cmdlist,
0743fc83
TT
1373 0/*allow-unknown*/,
1374 &maintenancelist);
4f337972 1375
0743fc83 1376 add_show_prefix_cmd ("show", class_maintenance, _("\
4f337972 1377Show GDB internal variables used by the GDB maintainer.\n\
1bedd215 1378Configure variables internal to GDB that aid in GDB's maintenance"),
2f822da5 1379 &maintenance_show_cmdlist,
0743fc83
TT
1380 0/*allow-unknown*/,
1381 &maintenancelist);
4f337972 1382
fdbc9870
PA
1383 cmd = add_cmd ("with", class_maintenance, maintenance_with_cmd, _("\
1384Like \"with\", but works with \"maintenance set\" variables.\n\
1385Usage: maintenance with SETTING [VALUE] [-- COMMAND]\n\
1386With no COMMAND, repeats the last executed command.\n\
1387SETTING is any setting you can change with the \"maintenance set\"\n\
1388subcommands."),
1389 &maintenancelist);
1390 set_cmd_completer_handle_brkchars (cmd, maintenance_with_cmd_completer);
1391
c906108c 1392#ifndef _WIN32
1a966eab
AC
1393 add_cmd ("dump-me", class_maintenance, maintenance_dump_me, _("\
1394Get fatal error; make debugger dump its core.\n\
8308e54c 1395GDB sets its handling of SIGQUIT back to SIG_DFL and then sends\n\
1a966eab 1396itself a SIGQUIT signal."),
c906108c
SS
1397 &maintenancelist);
1398#endif
1399
1a966eab
AC
1400 add_cmd ("internal-error", class_maintenance,
1401 maintenance_internal_error, _("\
1402Give GDB an internal error.\n\
1403Cause GDB to behave as if an internal error was detected."),
7be570e7
JM
1404 &maintenancelist);
1405
1a966eab
AC
1406 add_cmd ("internal-warning", class_maintenance,
1407 maintenance_internal_warning, _("\
1408Give GDB an internal warning.\n\
1409Cause GDB to behave as if an internal warning was reported."),
dec43320
AC
1410 &maintenancelist);
1411
57fcfb1b
GB
1412 add_cmd ("demangler-warning", class_maintenance,
1413 maintenance_demangler_warning, _("\
1414Give GDB a demangler warning.\n\
1415Cause GDB to behave as if a demangler warning was reported."),
1416 &maintenancelist);
1417
439250fb
DE
1418 cmd = add_cmd ("demangle", class_maintenance, maintenance_demangle, _("\
1419This command has been moved to \"demangle\"."),
1420 &maintenancelist);
1421 deprecate_cmd (cmd, "demangle");
c906108c 1422
e5e619ac
TT
1423 cmd = add_cmd ("canonicalize", class_maintenance, maintenance_canonicalize,
1424 _("\
1425Show the canonical form of a C++ name.\n\
1426Usage: maintenance canonicalize NAME"),
1427 &maintenancelist);
1428
bd712aed
DE
1429 add_prefix_cmd ("per-command", class_maintenance, set_per_command_cmd, _("\
1430Per-command statistics settings."),
2f822da5 1431 &per_command_setlist,
bd712aed
DE
1432 1/*allow-unknown*/, &maintenance_set_cmdlist);
1433
0743fc83 1434 add_show_prefix_cmd ("per-command", class_maintenance, _("\
bd712aed 1435Show per-command statistics settings."),
2f822da5 1436 &per_command_showlist,
0743fc83 1437 0/*allow-unknown*/, &maintenance_show_cmdlist);
bd712aed
DE
1438
1439 add_setshow_boolean_cmd ("time", class_maintenance,
1440 &per_command_time, _("\
1441Set whether to display per-command execution time."), _("\
1442Show whether to display per-command execution time."),
1443 _("\
1444If enabled, the execution time for each command will be\n\
1445displayed following the command's output."),
42339bc4 1446 maintenance_set_command_time_cmd, NULL,
bd712aed
DE
1447 &per_command_setlist, &per_command_showlist);
1448
1449 add_setshow_boolean_cmd ("space", class_maintenance,
1450 &per_command_space, _("\
1451Set whether to display per-command space usage."), _("\
1452Show whether to display per-command space usage."),
1453 _("\
1454If enabled, the space usage for each command will be\n\
1455displayed following the command's output."),
1456 NULL, NULL,
1457 &per_command_setlist, &per_command_showlist);
1458
1459 add_setshow_boolean_cmd ("symtab", class_maintenance,
1460 &per_command_symtab, _("\
1461Set whether to display per-command symtab statistics."), _("\
1462Show whether to display per-command symtab statistics."),
1463 _("\
1464If enabled, the basic symtab statistics for each command will be\n\
1465displayed following the command's output."),
1466 NULL, NULL,
1467 &per_command_setlist, &per_command_showlist);
1468
1469 /* This is equivalent to "mt set per-command time on".
1470 Kept because some people are used to typing "mt time 1". */
1a966eab
AC
1471 add_cmd ("time", class_maintenance, maintenance_time_display, _("\
1472Set the display of time usage.\n\
c906108c 1473If nonzero, will cause the execution time for each command to be\n\
1a966eab 1474displayed, following the command's output."),
c906108c
SS
1475 &maintenancelist);
1476
bd712aed
DE
1477 /* This is equivalent to "mt set per-command space on".
1478 Kept because some people are used to typing "mt space 1". */
1a966eab
AC
1479 add_cmd ("space", class_maintenance, maintenance_space_display, _("\
1480Set the display of space usage.\n\
c906108c 1481If nonzero, will cause the execution space for each command to be\n\
1a966eab 1482displayed, following the command's output."),
c906108c
SS
1483 &maintenancelist);
1484
a4915e8d 1485 cmd = add_cmd ("type", class_maintenance, maintenance_print_type, _("\
1a966eab 1486Print a type chain for a given symbol.\n\
c906108c 1487For each node in a type chain, print the raw data for each member of\n\
1a966eab 1488the type structure, and the interpretation of the data."),
c906108c 1489 &maintenanceprintlist);
a4915e8d 1490 set_cmd_completer (cmd, expression_completer);
c906108c 1491
c906108c 1492 add_cmd ("statistics", class_maintenance, maintenance_print_statistics,
1a966eab 1493 _("Print statistics about internal gdb state."),
c906108c
SS
1494 &maintenanceprintlist);
1495
1a966eab
AC
1496 add_cmd ("architecture", class_maintenance,
1497 maintenance_print_architecture, _("\
1498Print the internal architecture configuration.\n\
1499Takes an optional file parameter."),
4b9b3959
AC
1500 &maintenanceprintlist);
1501
0743fc83 1502 add_basic_prefix_cmd ("check", class_maintenance, _("\
27d41eac 1503Commands for checking internal gdb state."),
2f822da5 1504 &maintenancechecklist, 0,
0743fc83 1505 &maintenancelist);
27d41eac 1506
3e43a32a
MS
1507 add_cmd ("translate-address", class_maintenance,
1508 maintenance_translate_address,
1a966eab 1509 _("Translate a section name and address to a symbol."),
c906108c
SS
1510 &maintenancelist);
1511
1a966eab 1512 add_cmd ("deprecate", class_maintenance, maintenance_deprecate, _("\
590042fc
PW
1513Deprecate a command (for testing purposes).\n\
1514Usage: maintenance deprecate COMMANDNAME [\"REPLACEMENT\"]\n\
1515This is used by the testsuite to check the command deprecator.\n\
1516You probably shouldn't use this,\n\
1517rather you should use the C function deprecate_cmd()."), &maintenancelist);
56382845 1518
1a966eab 1519 add_cmd ("undeprecate", class_maintenance, maintenance_undeprecate, _("\
590042fc
PW
1520Undeprecate a command (for testing purposes).\n\
1521Usage: maintenance undeprecate COMMANDNAME\n\
1522This is used by the testsuite to check the command deprecator.\n\
1523You probably shouldn't use this."),
33f91161 1524 &maintenancelist);
56382845 1525
a1ff87d7
SM
1526 cmd_list_element *maintenance_selftest_cmd
1527 = add_cmd ("selftest", class_maintenance, maintenance_selftest, _("\
dcd1f979 1528Run gdb's unit tests.\n\
590042fc 1529Usage: maintenance selftest [FILTER]\n\
dcd1f979 1530This will run any unit tests that were built in to gdb.\n\
1526853e 1531If a filter is given, only the tests with that value in their name will ran."),
a1ff87d7
SM
1532 &maintenancelist);
1533 set_cmd_completer_handle_brkchars (maintenance_selftest_cmd,
1534 maintenance_selftest_completer);
dcd1f979 1535
1526853e
SM
1536 add_cmd ("selftests", class_maintenance, maintenance_info_selftests,
1537 _("List the registered selftests."), &maintenanceinfolist);
1538
d28f9cdf 1539 add_setshow_boolean_cmd ("profile", class_maintenance,
7915a72c
AC
1540 &maintenance_profile_p, _("\
1541Set internal profiling."), _("\
1542Show internal profiling."), _("\
1543When enabled GDB is profiled."),
2c5b56ce 1544 maintenance_set_profile_cmd,
920d2a44 1545 show_maintenance_profile_p,
d28f9cdf
DJ
1546 &maintenance_set_cmdlist,
1547 &maintenance_show_cmdlist);
22138db6
TT
1548
1549 add_setshow_zuinteger_unlimited_cmd ("worker-threads",
1550 class_maintenance,
1551 &n_worker_threads, _("\
1552Set the number of worker threads GDB can use."), _("\
1553Show the number of worker threads GDB can use."), _("\
1554GDB may use multiple threads to speed up certain CPU-intensive operations,\n\
1555such as demangling symbol names."),
ee8b8845
TT
1556 maintenance_set_worker_threads,
1557 maintenance_show_worker_threads,
22138db6
TT
1558 &maintenance_set_cmdlist,
1559 &maintenance_show_cmdlist);
1560
a1ff87d7
SM
1561 /* Add the "maint set/show selftest" commands. */
1562 static cmd_list_element *set_selftest_cmdlist = nullptr;
1563 static cmd_list_element *show_selftest_cmdlist = nullptr;
1564
1565 add_setshow_prefix_cmd ("selftest", class_maintenance,
1566 _("Self tests-related settings."),
1567 _("Self tests-related settings."),
1568 &set_selftest_cmdlist, &show_selftest_cmdlist,
1569 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
1570
1571 /* Add setting commands matching "maintenance selftest" options. */
1572 gdb::option::add_setshow_cmds_for_options (class_maintenance,
1573 &user_maintenance_selftest_options,
1574 maintenance_selftest_option_defs,
1575 &set_selftest_cmdlist,
1576 &show_selftest_cmdlist);
c906108c 1577}