1 /* Support for GDB maintenance commands.
3 Copyright (C) 1992-2024 Free Software Foundation, Inc.
5 Written by Fred Fish at Cygnus Support.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "arch-utils.h"
33 #include "expression.h"
40 #include "gdbsupport/selftest.h"
42 #include "gdbsupport/thread-pool.h"
44 #include "cli/cli-decode.h"
45 #include "cli/cli-utils.h"
46 #include "cli/cli-setshow.h"
47 #include "cli/cli-style.h"
48 #include "cli/cli-cmds.h"
50 static void maintenance_do_deprecate (const char *, int);
54 maintenance_dump_me (const char *args
, int from_tty
)
56 if (query (_("Should GDB dump core? ")))
59 /* SIGQUIT by default is ignored, so use SIGABRT instead. */
60 signal (SIGABRT
, SIG_DFL
);
61 kill (getpid (), SIGABRT
);
63 signal (SIGQUIT
, SIG_DFL
);
64 kill (getpid (), SIGQUIT
);
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
76 maintenance_internal_error (const char *args
, int from_tty
)
78 internal_error ("%s", (args
== NULL
? "" : args
));
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
87 maintenance_internal_warning (const char *args
, int from_tty
)
89 internal_warning ("%s", (args
== NULL
? "" : args
));
92 /* Stimulate the internal error mechanism that GDB uses when an
93 demangler problem is detected. Allows testing of the mechanism. */
96 maintenance_demangler_warning (const char *args
, int from_tty
)
98 demangler_warning (__FILE__
, __LINE__
, "%s", (args
== NULL
? "" : args
));
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. */
106 maintenance_demangle (const char *args
, int from_tty
)
108 gdb_printf (_("This command has been moved to \"%ps\".\n"),
109 styled_string (command_style
.style (), "demangle"));
113 maintenance_time_display (const char *args
, int from_tty
)
115 if (args
== NULL
|| *args
== '\0')
116 gdb_printf (_("\"%ps\" takes a numeric argument.\n"),
117 styled_string (command_style
.style (), "maintenance time"));
119 set_per_command_time (strtol (args
, NULL
, 10));
123 maintenance_space_display (const char *args
, int from_tty
)
125 if (args
== NULL
|| *args
== '\0')
126 gdb_printf ("\"%ps\" takes a numeric argument.\n",
127 styled_string (command_style
.style (), "maintenance space"));
129 set_per_command_space (strtol (args
, NULL
, 10));
132 /* Mini tokenizing lexer for 'maint info sections' command. */
135 match_substring (const char *string
, const char *substr
)
137 int substr_len
= strlen (substr
);
140 while ((tok
= strstr (string
, substr
)) != NULL
)
142 /* Got a partial match. Is it a whole word? */
147 /* Token is delimited at the front... */
148 if (tok
[substr_len
] == ' '
149 || tok
[substr_len
] == '\t'
150 || tok
[substr_len
] == '\0')
152 /* Token is delimited at the rear. Got a whole-word match. */
156 /* Token didn't match as a whole word. Advance and try again. */
162 /* Structure holding information about a single bfd section flag. This is
163 used by the "maintenance info sections" command to print the sections,
164 and for filtering which sections are printed. */
166 struct single_bfd_flag_info
168 /* The name of the section. This is what is printed for the flag, and
169 what the user enter in order to filter by flag. */
172 /* The bfd defined SEC_* flagword value for this flag. */
176 /* Vector of all the known bfd flags. */
178 static const single_bfd_flag_info bfd_flag_info
[] =
180 { "ALLOC", SEC_ALLOC
},
181 { "LOAD", SEC_LOAD
},
182 { "RELOC", SEC_RELOC
},
183 { "READONLY", SEC_READONLY
},
184 { "CODE", SEC_CODE
},
185 { "DATA", SEC_DATA
},
187 { "CONSTRUCTOR", SEC_CONSTRUCTOR
},
188 { "HAS_CONTENTS", SEC_HAS_CONTENTS
},
189 { "NEVER_LOAD", SEC_NEVER_LOAD
},
190 { "COFF_SHARED_LIBRARY", SEC_COFF_SHARED_LIBRARY
},
191 { "IS_COMMON", SEC_IS_COMMON
}
194 /* For each flag in the global BFD_FLAG_INFO list, if FLAGS has a flag's
195 flagword value set, and STRING contains the flag's name then return
196 true, otherwise return false. STRING is never nullptr. */
199 match_bfd_flags (const char *string
, flagword flags
)
201 gdb_assert (string
!= nullptr);
203 for (const auto &f
: bfd_flag_info
)
206 && match_substring (string
, f
.name
))
213 /* Print the names of all flags set in FLAGS. The names are taken from the
214 BFD_FLAG_INFO global. */
217 print_bfd_flags (flagword flags
)
219 for (const auto &f
: bfd_flag_info
)
222 gdb_printf (" %s", f
.name
);
227 maint_print_section_info (const char *name
, flagword flags
,
228 CORE_ADDR addr
, CORE_ADDR endaddr
,
229 unsigned long filepos
, int addr_size
)
231 gdb_printf (" %s", hex_string_custom (addr
, addr_size
));
232 gdb_printf ("->%s", hex_string_custom (endaddr
, addr_size
));
233 gdb_printf (" at %s",
234 hex_string_custom ((unsigned long) filepos
, 8));
235 gdb_printf (": %s", name
);
236 print_bfd_flags (flags
);
240 /* Return the number of digits required to display COUNT in decimal.
242 Used when pretty printing index numbers to ensure all of the indexes line
246 index_digits (int count
)
248 return ((int) log10 ((float) count
)) + 1;
251 /* Helper function to pretty-print the section index of ASECT from ABFD.
252 The INDEX_DIGITS is the number of digits in the largest index that will
253 be printed, and is used to pretty-print the resulting string. */
256 print_section_index (bfd
*abfd
,
261 = string_printf (" [%d] ", gdb_bfd_section_index (abfd
, asect
));
262 /* The '+ 4' for the leading and trailing characters. */
263 gdb_printf ("%-*s", (index_digits
+ 4), result
.c_str ());
266 /* Print information about ASECT from ABFD. The section will be printed using
267 the VMA's from the bfd, which will not be the relocated addresses for bfds
268 that should be relocated. The information must be printed with the same
269 layout as PRINT_OBJFILE_SECTION_INFO below.
271 ARG is the argument string passed by the user to the top level maintenance
272 info sections command. Used for filtering which sections are printed. */
275 print_bfd_section_info (bfd
*abfd
, asection
*asect
, const char *arg
,
278 flagword flags
= bfd_section_flags (asect
);
279 const char *name
= bfd_section_name (asect
);
281 if (arg
== NULL
|| *arg
== '\0'
282 || match_substring (arg
, name
)
283 || match_bfd_flags (arg
, flags
))
285 struct gdbarch
*gdbarch
= gdbarch_from_bfd (abfd
);
286 int addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
287 CORE_ADDR addr
, endaddr
;
289 addr
= bfd_section_vma (asect
);
290 endaddr
= addr
+ bfd_section_size (asect
);
291 print_section_index (abfd
, asect
, index_digits
);
292 maint_print_section_info (name
, flags
, addr
, endaddr
,
293 asect
->filepos
, addr_size
);
297 /* Print information about ASECT which is GDB's wrapper around a section
298 from ABFD. The information must be printed with the same layout as
299 PRINT_BFD_SECTION_INFO above. PRINT_DATA holds information used to
300 filter which sections are printed, and for formatting the output.
302 ARG is the argument string passed by the user to the top level maintenance
303 info sections command. Used for filtering which sections are printed. */
306 print_objfile_section_info (bfd
*abfd
, struct obj_section
*asect
,
307 const char *arg
, int index_digits
)
309 flagword flags
= bfd_section_flags (asect
->the_bfd_section
);
310 const char *name
= bfd_section_name (asect
->the_bfd_section
);
312 if (arg
== NULL
|| *arg
== '\0'
313 || match_substring (arg
, name
)
314 || match_bfd_flags (arg
, flags
))
316 struct gdbarch
*gdbarch
= gdbarch_from_bfd (abfd
);
317 int addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
319 print_section_index (abfd
, asect
->the_bfd_section
, index_digits
);
320 maint_print_section_info (name
, flags
,
321 asect
->addr (), asect
->endaddr (),
322 asect
->the_bfd_section
->filepos
,
327 /* Find an obj_section, GDB's wrapper around a bfd section for ASECTION
328 from ABFD. It might be that no such wrapper exists (for example debug
329 sections don't have such wrappers) in which case nullptr is returned. */
332 maint_obj_section_from_bfd_section (bfd
*abfd
,
336 if (ofile
->sections_start
== nullptr)
340 = &ofile
->sections_start
[gdb_bfd_section_index (abfd
, asection
)];
342 if (osect
>= ofile
->sections_end
)
348 /* Print information about all sections from ABFD, which is the bfd
349 corresponding to OBJFILE. It is fine for OBJFILE to be nullptr, but
350 ABFD must never be nullptr. If OBJFILE is provided then the sections of
351 ABFD will (potentially) be displayed relocated (i.e. the object file was
352 loaded with add-symbol-file and custom offsets were provided).
354 HEADER is a string that describes this file, e.g. 'Exec file: ', or
357 ARG is a string used for filtering which sections are printed, this can
358 be nullptr for no filtering. See the top level 'maint info sections'
359 for a fuller description of the possible filtering strings. */
362 maint_print_all_sections (const char *header
, bfd
*abfd
, objfile
*objfile
,
366 gdb_stdout
->wrap_here (8);
367 gdb_printf ("`%s', ", bfd_get_filename (abfd
));
368 gdb_stdout
->wrap_here (8);
369 gdb_printf (_("file type %s.\n"), bfd_get_target (abfd
));
371 int section_count
= gdb_bfd_count_sections (abfd
);
372 int digits
= index_digits (section_count
);
374 for (asection
*sect
: gdb_bfd_sections (abfd
))
376 obj_section
*osect
= nullptr;
378 if (objfile
!= nullptr)
380 gdb_assert (objfile
->sections_start
!= nullptr);
382 = maint_obj_section_from_bfd_section (abfd
, sect
, objfile
);
383 if (osect
->the_bfd_section
== nullptr)
387 if (osect
== nullptr)
388 print_bfd_section_info (abfd
, sect
, arg
, digits
);
390 print_objfile_section_info (abfd
, osect
, arg
, digits
);
394 /* The options for the "maintenance info sections" command. */
396 struct maint_info_sections_opts
398 /* For "-all-objects". */
399 bool all_objects
= false;
402 static const gdb::option::option_def maint_info_sections_option_defs
[] = {
404 gdb::option::flag_option_def
<maint_info_sections_opts
> {
406 [] (maint_info_sections_opts
*opts
) { return &opts
->all_objects
; },
407 N_("Display information from all loaded object files."),
411 /* Create an option_def_group for the "maintenance info sections" options,
412 with CC_OPTS as context. */
414 static inline gdb::option::option_def_group
415 make_maint_info_sections_options_def_group (maint_info_sections_opts
*cc_opts
)
417 return {{maint_info_sections_option_defs
}, cc_opts
};
420 /* Completion for the "maintenance info sections" command. */
423 maint_info_sections_completer (struct cmd_list_element
*cmd
,
424 completion_tracker
&tracker
,
425 const char *text
, const char * /* word */)
427 /* Complete command options. */
428 const auto group
= make_maint_info_sections_options_def_group (nullptr);
429 if (gdb::option::complete_options
430 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
))
432 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
434 /* Offer completion for section flags, but not section names. This is
435 only a maintenance command after all, no point going over the top. */
436 std::vector
<const char *> flags
;
437 for (const auto &f
: bfd_flag_info
)
438 flags
.push_back (f
.name
);
439 flags
.push_back (nullptr);
440 complete_on_enum (tracker
, flags
.data (), text
, word
);
443 /* Implement the "maintenance info sections" command. */
446 maintenance_info_sections (const char *arg
, int from_tty
)
448 /* Check if the "-all-objects" flag was passed. */
449 maint_info_sections_opts opts
;
450 const auto group
= make_maint_info_sections_options_def_group (&opts
);
451 gdb::option::process_options
452 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
);
454 for (objfile
*ofile
: current_program_space
->objfiles ())
456 if (ofile
->obfd
== current_program_space
->exec_bfd ())
457 maint_print_all_sections (_("Exec file: "), ofile
->obfd
.get (),
459 else if (opts
.all_objects
)
460 maint_print_all_sections (_("Object file: "), ofile
->obfd
.get (),
464 if (current_program_space
->core_bfd () != nullptr)
465 maint_print_all_sections (_("Core file: "),
466 current_program_space
->core_bfd (), nullptr, arg
);
469 /* Implement the "maintenance info target-sections" command. */
472 maintenance_info_target_sections (const char *arg
, int from_tty
)
476 const std::vector
<target_section
> *table
477 = target_get_section_table (current_inferior ()->top_target ());
478 if (table
== nullptr)
481 for (const target_section
&sec
: *table
)
483 if (abfd
== nullptr || sec
.the_bfd_section
->owner
!= abfd
)
485 abfd
= sec
.the_bfd_section
->owner
;
486 digits
= std::max (index_digits (gdb_bfd_count_sections (abfd
)),
491 struct gdbarch
*gdbarch
= nullptr;
494 for (const target_section
&sec
: *table
)
496 if (sec
.the_bfd_section
->owner
!= abfd
)
498 abfd
= sec
.the_bfd_section
->owner
;
499 gdbarch
= gdbarch_from_bfd (abfd
);
500 addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
502 gdb_printf (_("From '%s', file type %s:\n"),
503 bfd_get_filename (abfd
), bfd_get_target (abfd
));
505 print_bfd_section_info (abfd
,
509 /* The magic '8 + digits' here ensures that the 'Start' is aligned
510 with the output of print_bfd_section_info. */
511 gdb_printf ("%*sStart: %s, End: %s, Owner token: %p\n",
513 hex_string_custom (sec
.addr
, addr_size
),
514 hex_string_custom (sec
.endaddr
, addr_size
),
520 maintenance_print_statistics (const char *args
, int from_tty
)
522 print_objfile_statistics ();
526 maintenance_print_architecture (const char *args
, int from_tty
)
528 struct gdbarch
*gdbarch
= get_current_arch ();
531 gdbarch_dump (gdbarch
, gdb_stdout
);
536 if (!file
.open (args
, "w"))
537 perror_with_name (_("maintenance print architecture"));
538 gdbarch_dump (gdbarch
, &file
);
542 /* The "maintenance translate-address" command converts a section and address
543 to a symbol. This can be called in two ways:
544 maintenance translate-address <secname> <addr>
545 or maintenance translate-address <addr>. */
548 maintenance_translate_address (const char *arg
, int from_tty
)
551 struct obj_section
*sect
;
554 if (arg
== NULL
|| *arg
== 0)
555 error (_("requires argument (address or section + address)"));
561 { /* See if we have a valid section name. */
562 while (*p
&& !isspace (*p
)) /* Find end of section name. */
564 if (*p
== '\000') /* End of command? */
565 error (_("Need to specify section name and address"));
567 int arg_len
= p
- arg
;
568 p
= skip_spaces (p
+ 1);
570 for (objfile
*objfile
: current_program_space
->objfiles ())
571 for (obj_section
*iter
: objfile
->sections ())
573 if (strncmp (iter
->the_bfd_section
->name
, arg
, arg_len
) == 0)
577 error (_("Unknown section %s."), arg
);
581 address
= parse_and_eval_address (p
);
583 bound_minimal_symbol sym
;
585 sym
= lookup_minimal_symbol_by_pc_section (address
, sect
);
587 sym
= lookup_minimal_symbol_by_pc (address
);
591 const char *symbol_name
= sym
.minsym
->print_name ();
592 const char *symbol_offset
593 = pulongest (address
- sym
.value_address ());
595 sect
= sym
.obj_section ();
598 const char *section_name
;
599 const char *obj_name
;
601 gdb_assert (sect
->the_bfd_section
&& sect
->the_bfd_section
->name
);
602 section_name
= sect
->the_bfd_section
->name
;
604 gdb_assert (sect
->objfile
&& objfile_name (sect
->objfile
));
605 obj_name
= objfile_name (sect
->objfile
);
607 if (current_program_space
->multi_objfile_p ())
608 gdb_printf (_("%s + %s in section %s of %s\n"),
609 symbol_name
, symbol_offset
,
610 section_name
, obj_name
);
612 gdb_printf (_("%s + %s in section %s\n"),
613 symbol_name
, symbol_offset
, section_name
);
616 gdb_printf (_("%s + %s\n"), symbol_name
, symbol_offset
);
619 gdb_printf (_("no symbol at %s:%s\n"),
620 sect
->the_bfd_section
->name
, hex_string (address
));
622 gdb_printf (_("no symbol at %s\n"), hex_string (address
));
628 /* When a command is deprecated the user will be warned the first time
629 the command is used. If possible, a replacement will be
633 maintenance_deprecate (const char *args
, int from_tty
)
635 if (args
== NULL
|| *args
== '\0')
637 gdb_printf (_("\"%ps\" takes an argument,\n\
638 the command you want to deprecate, and optionally the replacement command\n\
639 enclosed in quotes.\n"),
640 styled_string (command_style
.style (),
641 "maintenance deprecate"));
644 maintenance_do_deprecate (args
, 1);
649 maintenance_undeprecate (const char *args
, int from_tty
)
651 if (args
== NULL
|| *args
== '\0')
653 gdb_printf (_("\"%ps\" takes an argument, \n\
654 the command you want to undeprecate.\n"),
655 styled_string (command_style
.style (),
656 "maintenance undeprecate"));
659 maintenance_do_deprecate (args
, 0);
662 /* You really shouldn't be using this. It is just for the testsuite.
663 Rather, you should use deprecate_cmd() when the command is created
664 in _initialize_blah().
666 This function deprecates a command and optionally assigns it a
670 maintenance_do_deprecate (const char *text
, int deprecate
)
672 struct cmd_list_element
*alias
= NULL
;
673 struct cmd_list_element
*prefix_cmd
= NULL
;
674 struct cmd_list_element
*cmd
= NULL
;
676 const char *start_ptr
= NULL
;
677 const char *end_ptr
= NULL
;
679 char *replacement
= NULL
;
684 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
686 gdb_printf (_("Can't find command '%s' to deprecate.\n"), text
);
692 /* Look for a replacement command. */
693 start_ptr
= strchr (text
, '\"');
694 if (start_ptr
!= NULL
)
697 end_ptr
= strrchr (start_ptr
, '\"');
700 len
= end_ptr
- start_ptr
;
701 replacement
= savestring (start_ptr
, len
);
706 if (!start_ptr
|| !end_ptr
)
710 /* If they used an alias, we only want to deprecate the alias.
712 Note the MALLOCED_REPLACEMENT test. If the command's replacement
713 string was allocated at compile time we don't want to free the
717 if (alias
->malloced_replacement
)
718 xfree ((char *) alias
->replacement
);
722 alias
->deprecated_warn_user
= 1;
723 alias
->cmd_deprecated
= 1;
727 alias
->deprecated_warn_user
= 0;
728 alias
->cmd_deprecated
= 0;
730 alias
->replacement
= replacement
;
731 alias
->malloced_replacement
= 1;
736 if (cmd
->malloced_replacement
)
737 xfree ((char *) cmd
->replacement
);
741 cmd
->deprecated_warn_user
= 1;
742 cmd
->cmd_deprecated
= 1;
746 cmd
->deprecated_warn_user
= 0;
747 cmd
->cmd_deprecated
= 0;
749 cmd
->replacement
= replacement
;
750 cmd
->malloced_replacement
= 1;
756 /* Maintenance set/show framework. */
758 struct cmd_list_element
*maintenance_set_cmdlist
;
759 struct cmd_list_element
*maintenance_show_cmdlist
;
761 /* "maintenance with" command. */
764 maintenance_with_cmd (const char *args
, int from_tty
)
766 with_command_1 ("maintenance set ", maintenance_set_cmdlist
, args
, from_tty
);
769 /* "maintenance with" command completer. */
772 maintenance_with_cmd_completer (struct cmd_list_element
*ignore
,
773 completion_tracker
&tracker
,
774 const char *text
, const char * /*word*/)
776 with_command_completer_1 ("maintenance set ", tracker
, text
);
779 /* Profiling support. */
781 static bool maintenance_profile_p
;
783 show_maintenance_profile_p (struct ui_file
*file
, int from_tty
,
784 struct cmd_list_element
*c
, const char *value
)
786 gdb_printf (file
, _("Internal profiling is %s.\n"), value
);
791 #define TEXTEND &_etext
792 #elif defined (HAVE_ETEXT)
794 #define TEXTEND &etext
797 #if defined (HAVE_MONSTARTUP) && defined (HAVE__MCLEANUP) && defined (TEXTEND)
799 static int profiling_state
;
801 extern "C" void _mcleanup (void);
804 mcleanup_wrapper (void)
810 extern "C" void monstartup (unsigned long, unsigned long);
811 extern int main (int, char **);
814 maintenance_set_profile_cmd (const char *args
, int from_tty
,
815 struct cmd_list_element
*c
)
817 if (maintenance_profile_p
== profiling_state
)
820 profiling_state
= maintenance_profile_p
;
822 if (maintenance_profile_p
)
824 static int profiling_initialized
;
826 if (!profiling_initialized
)
828 atexit (mcleanup_wrapper
);
829 profiling_initialized
= 1;
832 /* "main" is now always the first function in the text segment, so use
833 its address for monstartup. */
834 monstartup ((unsigned long) &main
, (unsigned long) TEXTEND
);
838 extern void _mcleanup (void);
845 maintenance_set_profile_cmd (const char *args
, int from_tty
,
846 struct cmd_list_element
*c
)
848 error (_("Profiling support is not available on this system."));
852 static int n_worker_threads
= -1;
857 update_thread_pool_size ()
860 int n_threads
= n_worker_threads
;
864 const int hardware_threads
= std::thread::hardware_concurrency ();
865 /* Testing in PR gdb/29959 indicates that parallel efficiency drops
866 between n_threads=5 to 8. Therefore, use no more than 8 threads
867 to avoid an excessive number of threads in the pool on many-core
869 const int max_thread_count
= 8;
870 n_threads
= std::min (hardware_threads
, max_thread_count
);
873 gdb::thread_pool::g_thread_pool
->set_thread_count (n_threads
);
878 maintenance_set_worker_threads (const char *args
, int from_tty
,
879 struct cmd_list_element
*c
)
881 update_thread_pool_size ();
885 maintenance_show_worker_threads (struct ui_file
*file
, int from_tty
,
886 struct cmd_list_element
*c
,
890 if (n_worker_threads
== -1)
892 gdb_printf (file
, _("The number of worker threads GDB "
893 "can use is the default (currently %zu).\n"),
894 gdb::thread_pool::g_thread_pool
->thread_count ());
899 int report_threads
= 0;
901 report_threads
= n_worker_threads
;
903 gdb_printf (file
, _("The number of worker threads GDB "
909 /* If true, display time usage both at startup and for each command. */
911 static bool per_command_time
;
913 /* If true, display space usage both at startup and for each command. */
915 static bool per_command_space
;
917 /* If true, display basic symtab stats for each command. */
919 static bool per_command_symtab
;
921 /* mt per-command commands. */
923 static struct cmd_list_element
*per_command_setlist
;
924 static struct cmd_list_element
*per_command_showlist
;
926 /* Set whether to display time statistics to NEW_VALUE
927 (non-zero means true). */
930 set_per_command_time (int new_value
)
932 per_command_time
= new_value
;
935 /* Set whether to display space statistics to NEW_VALUE
936 (non-zero means true). */
939 set_per_command_space (int new_value
)
941 per_command_space
= new_value
;
944 /* Count the number of symtabs and blocks. */
947 count_symtabs_and_blocks (int *nr_symtabs_ptr
, int *nr_compunit_symtabs_ptr
,
951 int nr_compunit_symtabs
= 0;
954 /* When collecting statistics during startup, this is called before
955 pretty much anything in gdb has been initialized, and thus
956 current_program_space may be NULL. */
957 if (current_program_space
!= NULL
)
959 for (objfile
*o
: current_program_space
->objfiles ())
961 for (compunit_symtab
*cu
: o
->compunits ())
963 ++nr_compunit_symtabs
;
964 nr_blocks
+= cu
->blockvector ()->num_blocks ();
965 nr_symtabs
+= std::distance (cu
->filetabs ().begin (),
966 cu
->filetabs ().end ());
971 *nr_symtabs_ptr
= nr_symtabs
;
972 *nr_compunit_symtabs_ptr
= nr_compunit_symtabs
;
973 *nr_blocks_ptr
= nr_blocks
;
976 /* As indicated by display_time and display_space, report GDB's
977 elapsed time and space usage from the base time and space recorded
980 scoped_command_stats::~scoped_command_stats ()
982 /* Early exit if we're not reporting any stats. It can be expensive to
983 compute the pre-command values so don't collect them at all if we're
984 not reporting stats. Alas this doesn't work in the startup case because
985 we don't know yet whether we will be reporting the stats. For the
986 startup case collect the data anyway (it should be cheap at this point),
987 and leave it to the reporter to decide whether to print them. */
990 && !per_command_space
991 && !per_command_symtab
)
994 if (m_time_enabled
&& per_command_time
)
996 print_time (_("command finished"));
998 using namespace std::chrono
;
1000 run_time_clock::duration cmd_time
1001 = run_time_clock::now () - m_start_cpu_time
;
1003 steady_clock::duration wall_time
1004 = steady_clock::now () - m_start_wall_time
;
1005 /* Subtract time spend in prompt_for_continue from walltime. */
1006 wall_time
-= get_prompt_for_continue_wait_time ();
1008 gdb_printf (gdb_stdlog
,
1010 ? _("Startup time: %.6f (cpu), %.6f (wall)\n")
1011 : _("Command execution time: %.6f (cpu), %.6f (wall)\n"),
1012 duration
<double> (cmd_time
).count (),
1013 duration
<double> (wall_time
).count ());
1016 if (m_space_enabled
&& per_command_space
)
1018 #ifdef HAVE_USEFUL_SBRK
1019 char *lim
= (char *) sbrk (0);
1021 long space_now
= lim
- lim_at_start
;
1022 long space_diff
= space_now
- m_start_space
;
1024 gdb_printf (gdb_stdlog
,
1026 ? _("Space used: %ld (%s%ld during startup)\n")
1027 : _("Space used: %ld (%s%ld for this command)\n"),
1029 (space_diff
>= 0 ? "+" : ""),
1034 if (m_symtab_enabled
&& per_command_symtab
)
1036 int nr_symtabs
, nr_compunit_symtabs
, nr_blocks
;
1038 count_symtabs_and_blocks (&nr_symtabs
, &nr_compunit_symtabs
, &nr_blocks
);
1039 gdb_printf (gdb_stdlog
,
1040 _("#symtabs: %d (+%d),"
1041 " #compunits: %d (+%d),"
1042 " #blocks: %d (+%d)\n"),
1044 nr_symtabs
- m_start_nr_symtabs
,
1045 nr_compunit_symtabs
,
1046 (nr_compunit_symtabs
1047 - m_start_nr_compunit_symtabs
),
1049 nr_blocks
- m_start_nr_blocks
);
1053 scoped_command_stats::scoped_command_stats (bool msg_type
)
1054 : m_msg_type (msg_type
)
1056 if (!m_msg_type
|| per_command_space
)
1058 #ifdef HAVE_USEFUL_SBRK
1059 char *lim
= (char *) sbrk (0);
1060 m_start_space
= lim
- lim_at_start
;
1061 m_space_enabled
= true;
1065 m_space_enabled
= false;
1067 if (msg_type
== 0 || per_command_time
)
1069 using namespace std::chrono
;
1071 m_start_cpu_time
= run_time_clock::now ();
1072 m_start_wall_time
= steady_clock::now ();
1073 m_time_enabled
= true;
1075 if (per_command_time
)
1076 print_time (_("command started"));
1079 m_time_enabled
= false;
1081 if (msg_type
== 0 || per_command_symtab
)
1083 int nr_symtabs
, nr_compunit_symtabs
, nr_blocks
;
1085 count_symtabs_and_blocks (&nr_symtabs
, &nr_compunit_symtabs
, &nr_blocks
);
1086 m_start_nr_symtabs
= nr_symtabs
;
1087 m_start_nr_compunit_symtabs
= nr_compunit_symtabs
;
1088 m_start_nr_blocks
= nr_blocks
;
1089 m_symtab_enabled
= true;
1092 m_symtab_enabled
= false;
1094 /* Initialize timer to keep track of how long we waited for the user. */
1095 reset_prompt_for_continue_wait_time ();
1101 scoped_command_stats::print_time (const char *msg
)
1103 using namespace std::chrono
;
1105 auto now
= system_clock::now ();
1106 auto ticks
= now
.time_since_epoch ().count () / (1000 * 1000);
1107 auto millis
= ticks
% 1000;
1109 std::time_t as_time
= system_clock::to_time_t (now
);
1111 localtime_r (&as_time
, &tm
);
1114 strftime (out
, sizeof (out
), "%F %H:%M:%S", &tm
);
1116 gdb_printf (gdb_stdlog
, "%s.%03d - %s\n", out
, (int) millis
, msg
);
1119 /* Handle unknown "mt set per-command" arguments.
1120 In this case have "mt set per-command on|off" affect every setting. */
1123 set_per_command_cmd (const char *args
, int from_tty
)
1125 struct cmd_list_element
*list
;
1128 val
= parse_cli_boolean_value (args
);
1130 error (_("Bad value for 'mt set per-command no'."));
1132 for (list
= per_command_setlist
; list
!= NULL
; list
= list
->next
)
1133 if (list
->var
->type () == var_boolean
)
1135 gdb_assert (list
->type
== set_cmd
);
1136 do_set_command (args
, from_tty
, list
);
1140 /* Options affecting the "maintenance selftest" command. */
1142 struct maintenance_selftest_options
1144 bool verbose
= false;
1145 } user_maintenance_selftest_options
;
1147 static const gdb::option::option_def maintenance_selftest_option_defs
[] = {
1148 gdb::option::boolean_option_def
<maintenance_selftest_options
> {
1150 [] (maintenance_selftest_options
*opt
) { return &opt
->verbose
; },
1152 N_("Set whether selftests run in verbose mode."),
1153 N_("Show whether selftests run in verbose mode."),
1155 When on, selftests may print verbose information."),
1159 /* Make option groups for the "maintenance selftest" command. */
1161 static std::array
<gdb::option::option_def_group
, 1>
1162 make_maintenance_selftest_option_group (maintenance_selftest_options
*opts
)
1165 {{maintenance_selftest_option_defs
}, opts
},
1169 /* The "maintenance selftest" command. */
1172 maintenance_selftest (const char *args
, int from_tty
)
1175 maintenance_selftest_options opts
= user_maintenance_selftest_options
;
1176 auto grp
= make_maintenance_selftest_option_group (&opts
);
1177 gdb::option::process_options
1178 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1179 const gdb_argv
argv (args
);
1180 selftests::run_tests (argv
.as_array_view (), opts
.verbose
);
1183 Selftests have been disabled for this build.\n"));
1187 /* Completer for the "maintenance selftest" command. */
1190 maintenance_selftest_completer (cmd_list_element
*cmd
,
1191 completion_tracker
&tracker
,
1195 auto grp
= make_maintenance_selftest_option_group (nullptr);
1197 if (gdb::option::complete_options
1198 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1202 for (const auto &test
: selftests::all_selftests ())
1204 if (startswith (test
.name
.c_str (), text
))
1205 tracker
.add_completion (make_unique_xstrdup (test
.name
.c_str ()));
1211 maintenance_info_selftests (const char *arg
, int from_tty
)
1214 gdb_printf ("Registered selftests:\n");
1215 for (const auto &test
: selftests::all_selftests ())
1216 gdb_printf (" - %s\n", test
.name
.c_str ());
1219 Selftests have been disabled for this build.\n"));
1224 void _initialize_maint_cmds ();
1226 _initialize_maint_cmds ()
1228 struct cmd_list_element
*cmd
;
1230 cmd_list_element
*maintenance_cmd
1231 = add_basic_prefix_cmd ("maintenance", class_maintenance
, _("\
1232 Commands for use by GDB maintainers.\n\
1233 Includes commands to dump specific internal GDB structures in\n\
1234 a human readable form, to cause GDB to deliberately dump core, etc."),
1235 &maintenancelist
, 0,
1238 add_com_alias ("mt", maintenance_cmd
, class_maintenance
, 1);
1240 cmd_list_element
*maintenance_info_cmd
1241 = add_basic_prefix_cmd ("info", class_maintenance
, _("\
1242 Commands for showing internal info about the program being debugged."),
1243 &maintenanceinfolist
, 0,
1245 add_alias_cmd ("i", maintenance_info_cmd
, class_maintenance
, 1,
1248 const auto opts
= make_maint_info_sections_options_def_group (nullptr);
1249 static std::string maint_info_sections_command_help
1250 = gdb::option::build_help (_("\
1251 List the BFD sections of the exec and core files.\n\
1253 Usage: maintenance info sections [-all-objects] [FILTERS]\n\
1255 FILTERS is a list of words, each word is either:\n\
1256 + A section name - any section with this name will be printed, or\n\
1257 + A section flag - any section with this flag will be printed. The\n\
1259 ALLOC LOAD RELOC READONLY CODE DATA ROM CONSTRUCTOR\n\
1260 HAS_CONTENTS NEVER_LOAD COFF_SHARED_LIBRARY IS_COMMON\n\
1262 Sections matching any of the FILTERS will be listed (no FILTERS implies\n\
1263 all sections should be printed).\n\
1267 cmd
= add_cmd ("sections", class_maintenance
, maintenance_info_sections
,
1268 maint_info_sections_command_help
.c_str (),
1269 &maintenanceinfolist
);
1270 set_cmd_completer_handle_brkchars (cmd
, maint_info_sections_completer
);
1272 add_cmd ("target-sections", class_maintenance
,
1273 maintenance_info_target_sections
, _("\
1274 List GDB's internal section table.\n\
1276 Print the current targets section list. This is a sub-set of all\n\
1277 sections, from all objects currently loaded. Usually the ALLOC\n\
1279 &maintenanceinfolist
);
1281 add_basic_prefix_cmd ("print", class_maintenance
,
1282 _("Maintenance command for printing GDB internal state."),
1283 &maintenanceprintlist
, 0,
1286 add_basic_prefix_cmd ("flush", class_maintenance
,
1287 _("Maintenance command for flushing GDB internal caches."),
1288 &maintenanceflushlist
, 0,
1291 add_basic_prefix_cmd ("set", class_maintenance
, _("\
1292 Set GDB internal variables used by the GDB maintainer.\n\
1293 Configure variables internal to GDB that aid in GDB's maintenance"),
1294 &maintenance_set_cmdlist
,
1298 add_show_prefix_cmd ("show", class_maintenance
, _("\
1299 Show GDB internal variables used by the GDB maintainer.\n\
1300 Configure variables internal to GDB that aid in GDB's maintenance"),
1301 &maintenance_show_cmdlist
,
1305 cmd
= add_cmd ("with", class_maintenance
, maintenance_with_cmd
, _("\
1306 Like \"with\", but works with \"maintenance set\" variables.\n\
1307 Usage: maintenance with SETTING [VALUE] [-- COMMAND]\n\
1308 With no COMMAND, repeats the last executed command.\n\
1309 SETTING is any setting you can change with the \"maintenance set\"\n\
1312 set_cmd_completer_handle_brkchars (cmd
, maintenance_with_cmd_completer
);
1315 add_cmd ("dump-me", class_maintenance
, maintenance_dump_me
, _("\
1316 Get fatal error; make debugger dump its core.\n\
1317 GDB sets its handling of SIGQUIT back to SIG_DFL and then sends\n\
1318 itself a SIGQUIT signal."),
1322 add_cmd ("internal-error", class_maintenance
,
1323 maintenance_internal_error
, _("\
1324 Give GDB an internal error.\n\
1325 Cause GDB to behave as if an internal error was detected."),
1328 add_cmd ("internal-warning", class_maintenance
,
1329 maintenance_internal_warning
, _("\
1330 Give GDB an internal warning.\n\
1331 Cause GDB to behave as if an internal warning was reported."),
1334 add_cmd ("demangler-warning", class_maintenance
,
1335 maintenance_demangler_warning
, _("\
1336 Give GDB a demangler warning.\n\
1337 Cause GDB to behave as if a demangler warning was reported."),
1340 cmd
= add_cmd ("demangle", class_maintenance
, maintenance_demangle
, _("\
1341 This command has been moved to \"demangle\"."),
1343 deprecate_cmd (cmd
, "demangle");
1345 add_prefix_cmd ("per-command", class_maintenance
, set_per_command_cmd
, _("\
1346 Per-command statistics settings."),
1347 &per_command_setlist
,
1348 1/*allow-unknown*/, &maintenance_set_cmdlist
);
1350 add_show_prefix_cmd ("per-command", class_maintenance
, _("\
1351 Show per-command statistics settings."),
1352 &per_command_showlist
,
1353 0/*allow-unknown*/, &maintenance_show_cmdlist
);
1355 add_setshow_boolean_cmd ("time", class_maintenance
,
1356 &per_command_time
, _("\
1357 Set whether to display per-command execution time."), _("\
1358 Show whether to display per-command execution time."),
1360 If enabled, the execution time for each command will be\n\
1361 displayed following the command's output."),
1363 &per_command_setlist
, &per_command_showlist
);
1365 add_setshow_boolean_cmd ("space", class_maintenance
,
1366 &per_command_space
, _("\
1367 Set whether to display per-command space usage."), _("\
1368 Show whether to display per-command space usage."),
1370 If enabled, the space usage for each command will be\n\
1371 displayed following the command's output."),
1373 &per_command_setlist
, &per_command_showlist
);
1375 add_setshow_boolean_cmd ("symtab", class_maintenance
,
1376 &per_command_symtab
, _("\
1377 Set whether to display per-command symtab statistics."), _("\
1378 Show whether to display per-command symtab statistics."),
1380 If enabled, the basic symtab statistics for each command will be\n\
1381 displayed following the command's output."),
1383 &per_command_setlist
, &per_command_showlist
);
1385 /* This is equivalent to "mt set per-command time on".
1386 Kept because some people are used to typing "mt time 1". */
1387 add_cmd ("time", class_maintenance
, maintenance_time_display
, _("\
1388 Set the display of time usage.\n\
1389 If nonzero, will cause the execution time for each command to be\n\
1390 displayed, following the command's output."),
1393 /* This is equivalent to "mt set per-command space on".
1394 Kept because some people are used to typing "mt space 1". */
1395 add_cmd ("space", class_maintenance
, maintenance_space_display
, _("\
1396 Set the display of space usage.\n\
1397 If nonzero, will cause the execution space for each command to be\n\
1398 displayed, following the command's output."),
1401 cmd
= add_cmd ("type", class_maintenance
, maintenance_print_type
, _("\
1402 Print a type chain for a given symbol.\n\
1403 For each node in a type chain, print the raw data for each member of\n\
1404 the type structure, and the interpretation of the data."),
1405 &maintenanceprintlist
);
1406 set_cmd_completer (cmd
, expression_completer
);
1408 add_cmd ("statistics", class_maintenance
, maintenance_print_statistics
,
1409 _("Print statistics about internal gdb state."),
1410 &maintenanceprintlist
);
1412 add_cmd ("architecture", class_maintenance
,
1413 maintenance_print_architecture
, _("\
1414 Print the internal architecture configuration.\n\
1415 Takes an optional file parameter."),
1416 &maintenanceprintlist
);
1418 add_basic_prefix_cmd ("check", class_maintenance
, _("\
1419 Commands for checking internal gdb state."),
1420 &maintenancechecklist
, 0,
1423 add_cmd ("translate-address", class_maintenance
,
1424 maintenance_translate_address
,
1425 _("Translate a section name and address to a symbol."),
1428 add_cmd ("deprecate", class_maintenance
, maintenance_deprecate
, _("\
1429 Deprecate a command (for testing purposes).\n\
1430 Usage: maintenance deprecate COMMANDNAME [\"REPLACEMENT\"]\n\
1431 This is used by the testsuite to check the command deprecator.\n\
1432 You probably shouldn't use this,\n\
1433 rather you should use the C function deprecate_cmd()."), &maintenancelist
);
1435 add_cmd ("undeprecate", class_maintenance
, maintenance_undeprecate
, _("\
1436 Undeprecate a command (for testing purposes).\n\
1437 Usage: maintenance undeprecate COMMANDNAME\n\
1438 This is used by the testsuite to check the command deprecator.\n\
1439 You probably shouldn't use this."),
1442 cmd_list_element
*maintenance_selftest_cmd
1443 = add_cmd ("selftest", class_maintenance
, maintenance_selftest
, _("\
1444 Run gdb's unit tests.\n\
1445 Usage: maintenance selftest [FILTER]\n\
1446 This will run any unit tests that were built in to gdb.\n\
1447 If a filter is given, only the tests with that value in their name will ran."),
1449 set_cmd_completer_handle_brkchars (maintenance_selftest_cmd
,
1450 maintenance_selftest_completer
);
1452 add_cmd ("selftests", class_maintenance
, maintenance_info_selftests
,
1453 _("List the registered selftests."), &maintenanceinfolist
);
1455 add_setshow_boolean_cmd ("profile", class_maintenance
,
1456 &maintenance_profile_p
, _("\
1457 Set internal profiling."), _("\
1458 Show internal profiling."), _("\
1459 When enabled GDB is profiled."),
1460 maintenance_set_profile_cmd
,
1461 show_maintenance_profile_p
,
1462 &maintenance_set_cmdlist
,
1463 &maintenance_show_cmdlist
);
1465 add_setshow_zuinteger_unlimited_cmd ("worker-threads",
1467 &n_worker_threads
, _("\
1468 Set the number of worker threads GDB can use."), _("\
1469 Show the number of worker threads GDB can use."), _("\
1470 GDB may use multiple threads to speed up certain CPU-intensive operations,\n\
1471 such as demangling symbol names."),
1472 maintenance_set_worker_threads
,
1473 maintenance_show_worker_threads
,
1474 &maintenance_set_cmdlist
,
1475 &maintenance_show_cmdlist
);
1477 /* Add the "maint set/show selftest" commands. */
1478 static cmd_list_element
*set_selftest_cmdlist
= nullptr;
1479 static cmd_list_element
*show_selftest_cmdlist
= nullptr;
1481 add_setshow_prefix_cmd ("selftest", class_maintenance
,
1482 _("Self tests-related settings."),
1483 _("Self tests-related settings."),
1484 &set_selftest_cmdlist
, &show_selftest_cmdlist
,
1485 &maintenance_set_cmdlist
, &maintenance_show_cmdlist
);
1487 /* Add setting commands matching "maintenance selftest" options. */
1488 gdb::option::add_setshow_cmds_for_options (class_maintenance
,
1489 &user_maintenance_selftest_options
,
1490 maintenance_selftest_option_defs
,
1491 &set_selftest_cmdlist
,
1492 &show_selftest_cmdlist
);