1 /* Support for GDB maintenance commands.
3 Copyright (C) 1992-2021 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/>. */
24 #include "arch-utils.h"
35 #include "expression.h" /* For language.h */
42 #include "gdbsupport/selftest.h"
45 #include "cli/cli-decode.h"
46 #include "cli/cli-utils.h"
47 #include "cli/cli-setshow.h"
48 #include "cli/cli-cmds.h"
51 #include "gdbsupport/thread-pool.h"
54 static void maintenance_do_deprecate (const char *, int);
58 maintenance_dump_me (const char *args
, int from_tty
)
60 if (query (_("Should GDB dump core? ")))
63 /* SIGQUIT by default is ignored, so use SIGABRT instead. */
64 signal (SIGABRT
, SIG_DFL
);
65 kill (getpid (), SIGABRT
);
67 signal (SIGQUIT
, SIG_DFL
);
68 kill (getpid (), SIGQUIT
);
74 /* Stimulate the internal error mechanism that GDB uses when an
75 internal problem is detected. Allows testing of the mechanism.
76 Also useful when the user wants to drop a core file but not exit
80 maintenance_internal_error (const char *args
, int from_tty
)
82 internal_error (__FILE__
, __LINE__
, "%s", (args
== NULL
? "" : args
));
85 /* Stimulate the internal error mechanism that GDB uses when an
86 internal problem is detected. Allows testing of the mechanism.
87 Also useful when the user wants to drop a core file but not exit
91 maintenance_internal_warning (const char *args
, int from_tty
)
93 internal_warning (__FILE__
, __LINE__
, "%s", (args
== NULL
? "" : args
));
96 /* Stimulate the internal error mechanism that GDB uses when an
97 demangler problem is detected. Allows testing of the mechanism. */
100 maintenance_demangler_warning (const char *args
, int from_tty
)
102 demangler_warning (__FILE__
, __LINE__
, "%s", (args
== NULL
? "" : args
));
105 /* Old command to demangle a string. The command has been moved to "demangle".
106 It is kept for now because otherwise "mt demangle" gets interpreted as
107 "mt demangler-warning" which artificially creates an internal gdb error. */
110 maintenance_demangle (const char *args
, int from_tty
)
112 printf_filtered (_("This command has been moved to \"demangle\".\n"));
116 maintenance_time_display (const char *args
, int from_tty
)
118 if (args
== NULL
|| *args
== '\0')
119 printf_unfiltered (_("\"maintenance time\" takes a numeric argument.\n"));
121 set_per_command_time (strtol (args
, NULL
, 10));
125 maintenance_space_display (const char *args
, int from_tty
)
127 if (args
== NULL
|| *args
== '\0')
128 printf_unfiltered ("\"maintenance space\" takes a numeric argument.\n");
130 set_per_command_space (strtol (args
, NULL
, 10));
133 /* Mini tokenizing lexer for 'maint info sections' command. */
136 match_substring (const char *string
, const char *substr
)
138 int substr_len
= strlen (substr
);
141 while ((tok
= strstr (string
, substr
)) != NULL
)
143 /* Got a partial match. Is it a whole word? */
148 /* Token is delimited at the front... */
149 if (tok
[substr_len
] == ' '
150 || tok
[substr_len
] == '\t'
151 || tok
[substr_len
] == '\0')
153 /* Token is delimited at the rear. Got a whole-word match. */
157 /* Token didn't match as a whole word. Advance and try again. */
163 /* Structure holding information about a single bfd section flag. This is
164 used by the "maintenance info sections" command to print the sections,
165 and for filtering which sections are printed. */
167 struct single_bfd_flag_info
169 /* The name of the section. This is what is printed for the flag, and
170 what the user enter in order to filter by flag. */
173 /* The bfd defined SEC_* flagword value for this flag. */
177 /* Vector of all the known bfd flags. */
179 static const single_bfd_flag_info bfd_flag_info
[] =
181 { "ALLOC", SEC_ALLOC
},
182 { "LOAD", SEC_LOAD
},
183 { "RELOC", SEC_RELOC
},
184 { "READONLY", SEC_READONLY
},
185 { "CODE", SEC_CODE
},
186 { "DATA", SEC_DATA
},
188 { "CONSTRUCTOR", SEC_CONSTRUCTOR
},
189 { "HAS_CONTENTS", SEC_HAS_CONTENTS
},
190 { "NEVER_LOAD", SEC_NEVER_LOAD
},
191 { "COFF_SHARED_LIBRARY", SEC_COFF_SHARED_LIBRARY
},
192 { "IS_COMMON", SEC_IS_COMMON
}
195 /* For each flag in the global BFD_FLAG_INFO list, if FLAGS has a flag's
196 flagword value set, and STRING contains the flag's name then return
197 true, otherwise return false. STRING is never nullptr. */
200 match_bfd_flags (const char *string
, flagword flags
)
202 gdb_assert (string
!= nullptr);
204 for (const auto &f
: bfd_flag_info
)
207 && match_substring (string
, f
.name
))
214 /* Print the names of all flags set in FLAGS. The names are taken from the
215 BFD_FLAG_INFO global. */
218 print_bfd_flags (flagword flags
)
220 for (const auto &f
: bfd_flag_info
)
223 printf_filtered (" %s", f
.name
);
228 maint_print_section_info (const char *name
, flagword flags
,
229 CORE_ADDR addr
, CORE_ADDR endaddr
,
230 unsigned long filepos
, int addr_size
)
232 printf_filtered (" %s", hex_string_custom (addr
, addr_size
));
233 printf_filtered ("->%s", hex_string_custom (endaddr
, addr_size
));
234 printf_filtered (" at %s",
235 hex_string_custom ((unsigned long) filepos
, 8));
236 printf_filtered (": %s", name
);
237 print_bfd_flags (flags
);
238 printf_filtered ("\n");
241 /* Return the number of digits required to display COUNT in decimal.
243 Used when pretty printing index numbers to ensure all of the indexes line
247 index_digits (int count
)
249 return ((int) log10 ((float) count
)) + 1;
252 /* Helper function to pretty-print the section index of ASECT from ABFD.
253 The INDEX_DIGITS is the number of digits in the largest index that will
254 be printed, and is used to pretty-print the resulting string. */
257 print_section_index (bfd
*abfd
,
262 = string_printf (" [%d] ", gdb_bfd_section_index (abfd
, asect
));
263 /* The '+ 4' for the leading and trailing characters. */
264 printf_filtered ("%-*s", (index_digits
+ 4), result
.c_str ());
267 /* Print information about ASECT from ABFD. The section will be printed using
268 the VMA's from the bfd, which will not be the relocated addresses for bfds
269 that should be relocated. The information must be printed with the same
270 layout as PRINT_OBJFILE_SECTION_INFO below.
272 ARG is the argument string passed by the user to the top level maintenance
273 info sections command. Used for filtering which sections are printed. */
276 print_bfd_section_info (bfd
*abfd
, asection
*asect
, const char *arg
,
279 flagword flags
= bfd_section_flags (asect
);
280 const char *name
= bfd_section_name (asect
);
282 if (arg
== NULL
|| *arg
== '\0'
283 || match_substring (arg
, name
)
284 || match_bfd_flags (arg
, flags
))
286 struct gdbarch
*gdbarch
= gdbarch_from_bfd (abfd
);
287 int addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
288 CORE_ADDR addr
, endaddr
;
290 addr
= bfd_section_vma (asect
);
291 endaddr
= addr
+ bfd_section_size (asect
);
292 print_section_index (abfd
, asect
, index_digits
);
293 maint_print_section_info (name
, flags
, addr
, endaddr
,
294 asect
->filepos
, addr_size
);
298 /* Print information about ASECT which is GDB's wrapper around a section
299 from ABFD. The information must be printed with the same layout as
300 PRINT_BFD_SECTION_INFO above. PRINT_DATA holds information used to
301 filter which sections are printed, and for formatting the output.
303 ARG is the argument string passed by the user to the top level maintenance
304 info sections command. Used for filtering which sections are printed. */
307 print_objfile_section_info (bfd
*abfd
, struct obj_section
*asect
,
308 const char *arg
, int index_digits
)
310 flagword flags
= bfd_section_flags (asect
->the_bfd_section
);
311 const char *name
= bfd_section_name (asect
->the_bfd_section
);
313 if (arg
== NULL
|| *arg
== '\0'
314 || match_substring (arg
, name
)
315 || match_bfd_flags (arg
, flags
))
317 struct gdbarch
*gdbarch
= gdbarch_from_bfd (abfd
);
318 int addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
320 print_section_index (abfd
, asect
->the_bfd_section
, index_digits
);
321 maint_print_section_info (name
, flags
,
322 asect
->addr (), asect
->endaddr (),
323 asect
->the_bfd_section
->filepos
,
328 /* Find an obj_section, GDB's wrapper around a bfd section for ASECTION
329 from ABFD. It might be that no such wrapper exists (for example debug
330 sections don't have such wrappers) in which case nullptr is returned. */
333 maint_obj_section_from_bfd_section (bfd
*abfd
,
337 if (ofile
->sections
== nullptr)
341 = &ofile
->sections
[gdb_bfd_section_index (abfd
, asection
)];
343 if (osect
>= ofile
->sections_end
)
349 /* Print information about all sections from ABFD, which is the bfd
350 corresponding to OBJFILE. It is fine for OBJFILE to be nullptr, but
351 ABFD must never be nullptr. If OBJFILE is provided then the sections of
352 ABFD will (potentially) be displayed relocated (i.e. the object file was
353 loaded with add-symbol-file and custom offsets were provided).
355 HEADER is a string that describes this file, e.g. 'Exec file: ', or
358 ARG is a string used for filtering which sections are printed, this can
359 be nullptr for no filtering. See the top level 'maint info sections'
360 for a fuller description of the possible filtering strings. */
363 maint_print_all_sections (const char *header
, bfd
*abfd
, objfile
*objfile
,
366 puts_filtered (header
);
368 printf_filtered ("`%s', ", bfd_get_filename (abfd
));
370 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd
));
372 int section_count
= gdb_bfd_count_sections (abfd
);
373 int digits
= index_digits (section_count
);
375 for (asection
*sect
: gdb_bfd_sections (abfd
))
377 obj_section
*osect
= nullptr;
379 if (objfile
!= nullptr)
381 gdb_assert (objfile
->sections
!= nullptr);
383 = maint_obj_section_from_bfd_section (abfd
, sect
, objfile
);
384 if (osect
->the_bfd_section
== nullptr)
388 if (osect
== nullptr)
389 print_bfd_section_info (abfd
, sect
, arg
, digits
);
391 print_objfile_section_info (abfd
, osect
, arg
, digits
);
395 /* The options for the "maintenance info sections" command. */
397 struct maint_info_sections_opts
399 /* For "-all-objects". */
400 bool all_objects
= false;
403 static const gdb::option::option_def maint_info_sections_option_defs
[] = {
405 gdb::option::flag_option_def
<maint_info_sections_opts
> {
407 [] (maint_info_sections_opts
*opts
) { return &opts
->all_objects
; },
408 N_("Display information from all loaded object files."),
412 /* Create an option_def_group for the "maintenance info sections" options,
413 with CC_OPTS as context. */
415 static inline gdb::option::option_def_group
416 make_maint_info_sections_options_def_group (maint_info_sections_opts
*cc_opts
)
418 return {{maint_info_sections_option_defs
}, cc_opts
};
421 /* Completion for the "maintenance info sections" command. */
424 maint_info_sections_completer (struct cmd_list_element
*cmd
,
425 completion_tracker
&tracker
,
426 const char *text
, const char * /* word */)
428 /* Complete command options. */
429 const auto group
= make_maint_info_sections_options_def_group (nullptr);
430 if (gdb::option::complete_options
431 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
))
433 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
435 /* Offer completion for section flags, but not section names. This is
436 only a maintenance command after all, no point going over the top. */
437 std::vector
<const char *> flags
;
438 for (const auto &f
: bfd_flag_info
)
439 flags
.push_back (f
.name
);
440 flags
.push_back (nullptr);
441 complete_on_enum (tracker
, flags
.data (), text
, word
);
444 /* Implement the "maintenance info sections" command. */
447 maintenance_info_sections (const char *arg
, int from_tty
)
449 /* Check if the "-all-objects" flag was passed. */
450 maint_info_sections_opts opts
;
451 const auto group
= make_maint_info_sections_options_def_group (&opts
);
452 gdb::option::process_options
453 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
);
455 for (objfile
*ofile
: current_program_space
->objfiles ())
457 if (ofile
->obfd
== current_program_space
->exec_bfd ())
458 maint_print_all_sections (_("Exec file: "), ofile
->obfd
, ofile
, arg
);
459 else if (opts
.all_objects
)
460 maint_print_all_sections (_("Object file: "), ofile
->obfd
, ofile
, arg
);
464 maint_print_all_sections (_("Core file: "), core_bfd
, nullptr, arg
);
467 /* Implement the "maintenance info target-sections" command. */
470 maintenance_info_target_sections (const char *arg
, int from_tty
)
474 const target_section_table
*table
475 = target_get_section_table (current_inferior ()->top_target ());
476 if (table
== nullptr)
479 for (const target_section
&sec
: *table
)
481 if (abfd
== nullptr || sec
.the_bfd_section
->owner
!= abfd
)
483 abfd
= sec
.the_bfd_section
->owner
;
484 digits
= std::max (index_digits (gdb_bfd_count_sections (abfd
)),
489 struct gdbarch
*gdbarch
= nullptr;
492 for (const target_section
&sec
: *table
)
494 if (sec
.the_bfd_section
->owner
!= abfd
)
496 abfd
= sec
.the_bfd_section
->owner
;
497 gdbarch
= gdbarch_from_bfd (abfd
);
498 addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
500 printf_filtered (_("From '%s', file type %s:\n"),
501 bfd_get_filename (abfd
), bfd_get_target (abfd
));
503 print_bfd_section_info (abfd
,
507 /* The magic '8 + digits' here ensures that the 'Start' is aligned
508 with the output of print_bfd_section_info. */
509 printf_filtered ("%*sStart: %s, End: %s, Owner token: %p\n",
511 hex_string_custom (sec
.addr
, addr_size
),
512 hex_string_custom (sec
.endaddr
, addr_size
),
518 maintenance_print_statistics (const char *args
, int from_tty
)
520 print_objfile_statistics ();
524 maintenance_print_architecture (const char *args
, int from_tty
)
526 struct gdbarch
*gdbarch
= get_current_arch ();
529 gdbarch_dump (gdbarch
, gdb_stdout
);
534 if (!file
.open (args
, "w"))
535 perror_with_name (_("maintenance print architecture"));
536 gdbarch_dump (gdbarch
, &file
);
540 /* The "maintenance translate-address" command converts a section and address
541 to a symbol. This can be called in two ways:
542 maintenance translate-address <secname> <addr>
543 or maintenance translate-address <addr>. */
546 maintenance_translate_address (const char *arg
, int from_tty
)
549 struct obj_section
*sect
;
551 struct bound_minimal_symbol sym
;
553 if (arg
== NULL
|| *arg
== 0)
554 error (_("requires argument (address or section + address)"));
560 { /* See if we have a valid section name. */
561 while (*p
&& !isspace (*p
)) /* Find end of section name. */
563 if (*p
== '\000') /* End of command? */
564 error (_("Need to specify section name and address"));
566 int arg_len
= p
- arg
;
567 p
= skip_spaces (p
+ 1);
569 for (objfile
*objfile
: current_program_space
->objfiles ())
570 ALL_OBJFILE_OSECTIONS (objfile
, sect
)
572 if (strncmp (sect
->the_bfd_section
->name
, arg
, arg_len
) == 0)
576 error (_("Unknown section %s."), arg
);
580 address
= parse_and_eval_address (p
);
583 sym
= lookup_minimal_symbol_by_pc_section (address
, sect
);
585 sym
= lookup_minimal_symbol_by_pc (address
);
589 const char *symbol_name
= sym
.minsym
->print_name ();
590 const char *symbol_offset
591 = pulongest (address
- BMSYMBOL_VALUE_ADDRESS (sym
));
593 sect
= sym
.obj_section ();
596 const char *section_name
;
597 const char *obj_name
;
599 gdb_assert (sect
->the_bfd_section
&& sect
->the_bfd_section
->name
);
600 section_name
= sect
->the_bfd_section
->name
;
602 gdb_assert (sect
->objfile
&& objfile_name (sect
->objfile
));
603 obj_name
= objfile_name (sect
->objfile
);
605 if (current_program_space
->multi_objfile_p ())
606 printf_filtered (_("%s + %s in section %s of %s\n"),
607 symbol_name
, symbol_offset
,
608 section_name
, obj_name
);
610 printf_filtered (_("%s + %s in section %s\n"),
611 symbol_name
, symbol_offset
, section_name
);
614 printf_filtered (_("%s + %s\n"), symbol_name
, symbol_offset
);
617 printf_filtered (_("no symbol at %s:%s\n"),
618 sect
->the_bfd_section
->name
, hex_string (address
));
620 printf_filtered (_("no symbol at %s\n"), hex_string (address
));
626 /* When a command is deprecated the user will be warned the first time
627 the command is used. If possible, a replacement will be
631 maintenance_deprecate (const char *args
, int from_tty
)
633 if (args
== NULL
|| *args
== '\0')
635 printf_unfiltered (_("\"maintenance deprecate\" takes an argument,\n\
636 the command you want to deprecate, and optionally the replacement command\n\
637 enclosed in quotes.\n"));
640 maintenance_do_deprecate (args
, 1);
645 maintenance_undeprecate (const char *args
, int from_tty
)
647 if (args
== NULL
|| *args
== '\0')
649 printf_unfiltered (_("\"maintenance undeprecate\" takes an argument, \n\
650 the command you want to undeprecate.\n"));
653 maintenance_do_deprecate (args
, 0);
656 /* You really shouldn't be using this. It is just for the testsuite.
657 Rather, you should use deprecate_cmd() when the command is created
658 in _initialize_blah().
660 This function deprecates a command and optionally assigns it a
664 maintenance_do_deprecate (const char *text
, int deprecate
)
666 struct cmd_list_element
*alias
= NULL
;
667 struct cmd_list_element
*prefix_cmd
= NULL
;
668 struct cmd_list_element
*cmd
= NULL
;
670 const char *start_ptr
= NULL
;
671 const char *end_ptr
= NULL
;
673 char *replacement
= NULL
;
678 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
680 printf_filtered (_("Can't find command '%s' to deprecate.\n"), text
);
686 /* Look for a replacement command. */
687 start_ptr
= strchr (text
, '\"');
688 if (start_ptr
!= NULL
)
691 end_ptr
= strrchr (start_ptr
, '\"');
694 len
= end_ptr
- start_ptr
;
695 replacement
= savestring (start_ptr
, len
);
700 if (!start_ptr
|| !end_ptr
)
704 /* If they used an alias, we only want to deprecate the alias.
706 Note the MALLOCED_REPLACEMENT test. If the command's replacement
707 string was allocated at compile time we don't want to free the
711 if (alias
->malloced_replacement
)
712 xfree ((char *) alias
->replacement
);
716 alias
->deprecated_warn_user
= 1;
717 alias
->cmd_deprecated
= 1;
721 alias
->deprecated_warn_user
= 0;
722 alias
->cmd_deprecated
= 0;
724 alias
->replacement
= replacement
;
725 alias
->malloced_replacement
= 1;
730 if (cmd
->malloced_replacement
)
731 xfree ((char *) cmd
->replacement
);
735 cmd
->deprecated_warn_user
= 1;
736 cmd
->cmd_deprecated
= 1;
740 cmd
->deprecated_warn_user
= 0;
741 cmd
->cmd_deprecated
= 0;
743 cmd
->replacement
= replacement
;
744 cmd
->malloced_replacement
= 1;
750 /* Maintenance set/show framework. */
752 struct cmd_list_element
*maintenance_set_cmdlist
;
753 struct cmd_list_element
*maintenance_show_cmdlist
;
755 /* "maintenance with" command. */
758 maintenance_with_cmd (const char *args
, int from_tty
)
760 with_command_1 ("maintenance set ", maintenance_set_cmdlist
, args
, from_tty
);
763 /* "maintenance with" command completer. */
766 maintenance_with_cmd_completer (struct cmd_list_element
*ignore
,
767 completion_tracker
&tracker
,
768 const char *text
, const char * /*word*/)
770 with_command_completer_1 ("maintenance set ", tracker
, text
);
773 /* Profiling support. */
775 static bool maintenance_profile_p
;
777 show_maintenance_profile_p (struct ui_file
*file
, int from_tty
,
778 struct cmd_list_element
*c
, const char *value
)
780 fprintf_filtered (file
, _("Internal profiling is %s.\n"), value
);
785 #define TEXTEND &_etext
786 #elif defined (HAVE_ETEXT)
788 #define TEXTEND &etext
791 #if defined (HAVE_MONSTARTUP) && defined (HAVE__MCLEANUP) && defined (TEXTEND)
793 static int profiling_state
;
795 EXTERN_C
void _mcleanup (void);
798 mcleanup_wrapper (void)
804 EXTERN_C
void monstartup (unsigned long, unsigned long);
808 maintenance_set_profile_cmd (const char *args
, int from_tty
,
809 struct cmd_list_element
*c
)
811 if (maintenance_profile_p
== profiling_state
)
814 profiling_state
= maintenance_profile_p
;
816 if (maintenance_profile_p
)
818 static int profiling_initialized
;
820 if (!profiling_initialized
)
822 atexit (mcleanup_wrapper
);
823 profiling_initialized
= 1;
826 /* "main" is now always the first function in the text segment, so use
827 its address for monstartup. */
828 monstartup ((unsigned long) &main
, (unsigned long) TEXTEND
);
832 extern void _mcleanup (void);
839 maintenance_set_profile_cmd (const char *args
, int from_tty
,
840 struct cmd_list_element
*c
)
842 error (_("Profiling support is not available on this system."));
846 static int n_worker_threads
= -1;
848 /* Update the thread pool for the desired number of threads. */
850 update_thread_pool_size ()
853 int n_threads
= n_worker_threads
;
856 n_threads
= std::thread::hardware_concurrency ();
858 gdb::thread_pool::g_thread_pool
->set_thread_count (n_threads
);
863 maintenance_set_worker_threads (const char *args
, int from_tty
,
864 struct cmd_list_element
*c
)
866 update_thread_pool_size ();
870 maintenance_show_worker_threads (struct ui_file
*file
, int from_tty
,
871 struct cmd_list_element
*c
,
874 if (n_worker_threads
== -1)
875 fprintf_filtered (file
, _("The number of worker threads GDB "
876 "can use is unlimited (currently %zu).\n"),
877 gdb::thread_pool::g_thread_pool
->thread_count ());
879 fprintf_filtered (file
, _("The number of worker threads GDB "
885 /* If true, display time usage both at startup and for each command. */
887 static bool per_command_time
;
889 /* If true, display space usage both at startup and for each command. */
891 static bool per_command_space
;
893 /* If true, display basic symtab stats for each command. */
895 static bool per_command_symtab
;
897 /* mt per-command commands. */
899 static struct cmd_list_element
*per_command_setlist
;
900 static struct cmd_list_element
*per_command_showlist
;
902 /* Set whether to display time statistics to NEW_VALUE
903 (non-zero means true). */
906 set_per_command_time (int new_value
)
908 per_command_time
= new_value
;
911 /* Set whether to display space statistics to NEW_VALUE
912 (non-zero means true). */
915 set_per_command_space (int new_value
)
917 per_command_space
= new_value
;
920 /* Count the number of symtabs and blocks. */
923 count_symtabs_and_blocks (int *nr_symtabs_ptr
, int *nr_compunit_symtabs_ptr
,
927 int nr_compunit_symtabs
= 0;
930 /* When collecting statistics during startup, this is called before
931 pretty much anything in gdb has been initialized, and thus
932 current_program_space may be NULL. */
933 if (current_program_space
!= NULL
)
935 for (objfile
*o
: current_program_space
->objfiles ())
937 for (compunit_symtab
*cu
: o
->compunits ())
939 ++nr_compunit_symtabs
;
940 nr_blocks
+= BLOCKVECTOR_NBLOCKS (COMPUNIT_BLOCKVECTOR (cu
));
941 nr_symtabs
+= std::distance (compunit_filetabs (cu
).begin (),
942 compunit_filetabs (cu
).end ());
947 *nr_symtabs_ptr
= nr_symtabs
;
948 *nr_compunit_symtabs_ptr
= nr_compunit_symtabs
;
949 *nr_blocks_ptr
= nr_blocks
;
952 /* As indicated by display_time and display_space, report GDB's
953 elapsed time and space usage from the base time and space recorded
956 scoped_command_stats::~scoped_command_stats ()
958 /* Early exit if we're not reporting any stats. It can be expensive to
959 compute the pre-command values so don't collect them at all if we're
960 not reporting stats. Alas this doesn't work in the startup case because
961 we don't know yet whether we will be reporting the stats. For the
962 startup case collect the data anyway (it should be cheap at this point),
963 and leave it to the reporter to decide whether to print them. */
966 && !per_command_space
967 && !per_command_symtab
)
970 if (m_time_enabled
&& per_command_time
)
972 print_time (_("command finished"));
974 using namespace std::chrono
;
976 run_time_clock::duration cmd_time
977 = run_time_clock::now () - m_start_cpu_time
;
979 steady_clock::duration wall_time
980 = steady_clock::now () - m_start_wall_time
;
981 /* Subtract time spend in prompt_for_continue from walltime. */
982 wall_time
-= get_prompt_for_continue_wait_time ();
984 printf_unfiltered (!m_msg_type
985 ? _("Startup time: %.6f (cpu), %.6f (wall)\n")
986 : _("Command execution time: %.6f (cpu), %.6f (wall)\n"),
987 duration
<double> (cmd_time
).count (),
988 duration
<double> (wall_time
).count ());
991 if (m_space_enabled
&& per_command_space
)
993 #ifdef HAVE_USEFUL_SBRK
994 char *lim
= (char *) sbrk (0);
996 long space_now
= lim
- lim_at_start
;
997 long space_diff
= space_now
- m_start_space
;
999 printf_unfiltered (!m_msg_type
1000 ? _("Space used: %ld (%s%ld during startup)\n")
1001 : _("Space used: %ld (%s%ld for this command)\n"),
1003 (space_diff
>= 0 ? "+" : ""),
1008 if (m_symtab_enabled
&& per_command_symtab
)
1010 int nr_symtabs
, nr_compunit_symtabs
, nr_blocks
;
1012 count_symtabs_and_blocks (&nr_symtabs
, &nr_compunit_symtabs
, &nr_blocks
);
1013 printf_unfiltered (_("#symtabs: %d (+%d),"
1014 " #compunits: %d (+%d),"
1015 " #blocks: %d (+%d)\n"),
1017 nr_symtabs
- m_start_nr_symtabs
,
1018 nr_compunit_symtabs
,
1019 (nr_compunit_symtabs
1020 - m_start_nr_compunit_symtabs
),
1022 nr_blocks
- m_start_nr_blocks
);
1026 scoped_command_stats::scoped_command_stats (bool msg_type
)
1027 : m_msg_type (msg_type
)
1029 if (!m_msg_type
|| per_command_space
)
1031 #ifdef HAVE_USEFUL_SBRK
1032 char *lim
= (char *) sbrk (0);
1033 m_start_space
= lim
- lim_at_start
;
1034 m_space_enabled
= 1;
1038 m_space_enabled
= 0;
1040 if (msg_type
== 0 || per_command_time
)
1042 using namespace std::chrono
;
1044 m_start_cpu_time
= run_time_clock::now ();
1045 m_start_wall_time
= steady_clock::now ();
1048 if (per_command_time
)
1049 print_time (_("command started"));
1054 if (msg_type
== 0 || per_command_symtab
)
1056 int nr_symtabs
, nr_compunit_symtabs
, nr_blocks
;
1058 count_symtabs_and_blocks (&nr_symtabs
, &nr_compunit_symtabs
, &nr_blocks
);
1059 m_start_nr_symtabs
= nr_symtabs
;
1060 m_start_nr_compunit_symtabs
= nr_compunit_symtabs
;
1061 m_start_nr_blocks
= nr_blocks
;
1062 m_symtab_enabled
= 1;
1065 m_symtab_enabled
= 0;
1067 /* Initialize timer to keep track of how long we waited for the user. */
1068 reset_prompt_for_continue_wait_time ();
1074 scoped_command_stats::print_time (const char *msg
)
1076 using namespace std::chrono
;
1078 auto now
= system_clock::now ();
1079 auto ticks
= now
.time_since_epoch ().count () / (1000 * 1000);
1080 auto millis
= ticks
% 1000;
1082 std::time_t as_time
= system_clock::to_time_t (now
);
1084 localtime_r (&as_time
, &tm
);
1087 strftime (out
, sizeof (out
), "%F %H:%M:%S", &tm
);
1089 printf_unfiltered ("%s.%03d - %s\n", out
, (int) millis
, msg
);
1092 /* Handle unknown "mt set per-command" arguments.
1093 In this case have "mt set per-command on|off" affect every setting. */
1096 set_per_command_cmd (const char *args
, int from_tty
)
1098 struct cmd_list_element
*list
;
1101 val
= parse_cli_boolean_value (args
);
1103 error (_("Bad value for 'mt set per-command no'."));
1105 for (list
= per_command_setlist
; list
!= NULL
; list
= list
->next
)
1106 if (list
->var_type
== var_boolean
)
1108 gdb_assert (list
->type
== set_cmd
);
1109 do_set_command (args
, from_tty
, list
);
1115 /* The "maintenance selftest" command. */
1118 maintenance_selftest (const char *args
, int from_tty
)
1121 gdb_argv
argv (args
);
1122 selftests::run_tests (argv
.as_array_view ());
1124 printf_filtered (_("\
1125 Selftests have been disabled for this build.\n"));
1130 maintenance_info_selftests (const char *arg
, int from_tty
)
1133 printf_filtered ("Registered selftests:\n");
1134 selftests::for_each_selftest ([] (const std::string
&name
) {
1135 printf_filtered (" - %s\n", name
.c_str ());
1138 printf_filtered (_("\
1139 Selftests have been disabled for this build.\n"));
1144 void _initialize_maint_cmds ();
1146 _initialize_maint_cmds ()
1148 struct cmd_list_element
*cmd
;
1150 cmd_list_element
*maintenance_cmd
1151 = add_basic_prefix_cmd ("maintenance", class_maintenance
, _("\
1152 Commands for use by GDB maintainers.\n\
1153 Includes commands to dump specific internal GDB structures in\n\
1154 a human readable form, to cause GDB to deliberately dump core, etc."),
1155 &maintenancelist
, 0,
1158 add_com_alias ("mt", maintenance_cmd
, class_maintenance
, 1);
1160 cmd_list_element
*maintenance_info_cmd
1161 = add_basic_prefix_cmd ("info", class_maintenance
, _("\
1162 Commands for showing internal info about the program being debugged."),
1163 &maintenanceinfolist
, 0,
1165 add_alias_cmd ("i", maintenance_info_cmd
, class_maintenance
, 1,
1168 const auto opts
= make_maint_info_sections_options_def_group (nullptr);
1169 static std::string maint_info_sections_command_help
1170 = gdb::option::build_help (_("\
1171 List the BFD sections of the exec and core files.\n\
1173 Usage: maintenance info sections [-all-objects] [FILTERS]\n\
1175 FILTERS is a list of words, each word is either:\n\
1176 + A section name - any section with this name will be printed, or\n\
1177 + A section flag - any section with this flag will be printed. The\n\
1179 ALLOC LOAD RELOC READONLY CODE DATA ROM CONSTRUCTOR\n\
1180 HAS_CONTENTS NEVER_LOAD COFF_SHARED_LIBRARY IS_COMMON\n\
1182 Sections matching any of the FILTERS will be listed (no FILTERS implies\n\
1183 all sections should be printed).\n\
1187 cmd
= add_cmd ("sections", class_maintenance
, maintenance_info_sections
,
1188 maint_info_sections_command_help
.c_str (),
1189 &maintenanceinfolist
);
1190 set_cmd_completer_handle_brkchars (cmd
, maint_info_sections_completer
);
1192 add_cmd ("target-sections", class_maintenance
,
1193 maintenance_info_target_sections
, _("\
1194 List GDB's internal section table.\n\
1196 Print the current targets section list. This is a sub-set of all\n\
1197 sections, from all objects currently loaded. Usually the ALLOC\n\
1199 &maintenanceinfolist
);
1201 add_basic_prefix_cmd ("print", class_maintenance
,
1202 _("Maintenance command for printing GDB internal state."),
1203 &maintenanceprintlist
, 0,
1206 add_basic_prefix_cmd ("flush", class_maintenance
,
1207 _("Maintenance command for flushing GDB internal caches."),
1208 &maintenanceflushlist
, 0,
1211 add_basic_prefix_cmd ("set", class_maintenance
, _("\
1212 Set GDB internal variables used by the GDB maintainer.\n\
1213 Configure variables internal to GDB that aid in GDB's maintenance"),
1214 &maintenance_set_cmdlist
,
1218 add_show_prefix_cmd ("show", class_maintenance
, _("\
1219 Show GDB internal variables used by the GDB maintainer.\n\
1220 Configure variables internal to GDB that aid in GDB's maintenance"),
1221 &maintenance_show_cmdlist
,
1225 cmd
= add_cmd ("with", class_maintenance
, maintenance_with_cmd
, _("\
1226 Like \"with\", but works with \"maintenance set\" variables.\n\
1227 Usage: maintenance with SETTING [VALUE] [-- COMMAND]\n\
1228 With no COMMAND, repeats the last executed command.\n\
1229 SETTING is any setting you can change with the \"maintenance set\"\n\
1232 set_cmd_completer_handle_brkchars (cmd
, maintenance_with_cmd_completer
);
1235 add_cmd ("dump-me", class_maintenance
, maintenance_dump_me
, _("\
1236 Get fatal error; make debugger dump its core.\n\
1237 GDB sets its handling of SIGQUIT back to SIG_DFL and then sends\n\
1238 itself a SIGQUIT signal."),
1242 add_cmd ("internal-error", class_maintenance
,
1243 maintenance_internal_error
, _("\
1244 Give GDB an internal error.\n\
1245 Cause GDB to behave as if an internal error was detected."),
1248 add_cmd ("internal-warning", class_maintenance
,
1249 maintenance_internal_warning
, _("\
1250 Give GDB an internal warning.\n\
1251 Cause GDB to behave as if an internal warning was reported."),
1254 add_cmd ("demangler-warning", class_maintenance
,
1255 maintenance_demangler_warning
, _("\
1256 Give GDB a demangler warning.\n\
1257 Cause GDB to behave as if a demangler warning was reported."),
1260 cmd
= add_cmd ("demangle", class_maintenance
, maintenance_demangle
, _("\
1261 This command has been moved to \"demangle\"."),
1263 deprecate_cmd (cmd
, "demangle");
1265 add_prefix_cmd ("per-command", class_maintenance
, set_per_command_cmd
, _("\
1266 Per-command statistics settings."),
1267 &per_command_setlist
,
1268 1/*allow-unknown*/, &maintenance_set_cmdlist
);
1270 add_show_prefix_cmd ("per-command", class_maintenance
, _("\
1271 Show per-command statistics settings."),
1272 &per_command_showlist
,
1273 0/*allow-unknown*/, &maintenance_show_cmdlist
);
1275 add_setshow_boolean_cmd ("time", class_maintenance
,
1276 &per_command_time
, _("\
1277 Set whether to display per-command execution time."), _("\
1278 Show whether to display per-command execution time."),
1280 If enabled, the execution time for each command will be\n\
1281 displayed following the command's output."),
1283 &per_command_setlist
, &per_command_showlist
);
1285 add_setshow_boolean_cmd ("space", class_maintenance
,
1286 &per_command_space
, _("\
1287 Set whether to display per-command space usage."), _("\
1288 Show whether to display per-command space usage."),
1290 If enabled, the space usage for each command will be\n\
1291 displayed following the command's output."),
1293 &per_command_setlist
, &per_command_showlist
);
1295 add_setshow_boolean_cmd ("symtab", class_maintenance
,
1296 &per_command_symtab
, _("\
1297 Set whether to display per-command symtab statistics."), _("\
1298 Show whether to display per-command symtab statistics."),
1300 If enabled, the basic symtab statistics for each command will be\n\
1301 displayed following the command's output."),
1303 &per_command_setlist
, &per_command_showlist
);
1305 /* This is equivalent to "mt set per-command time on".
1306 Kept because some people are used to typing "mt time 1". */
1307 add_cmd ("time", class_maintenance
, maintenance_time_display
, _("\
1308 Set the display of time usage.\n\
1309 If nonzero, will cause the execution time for each command to be\n\
1310 displayed, following the command's output."),
1313 /* This is equivalent to "mt set per-command space on".
1314 Kept because some people are used to typing "mt space 1". */
1315 add_cmd ("space", class_maintenance
, maintenance_space_display
, _("\
1316 Set the display of space usage.\n\
1317 If nonzero, will cause the execution space for each command to be\n\
1318 displayed, following the command's output."),
1321 cmd
= add_cmd ("type", class_maintenance
, maintenance_print_type
, _("\
1322 Print a type chain for a given symbol.\n\
1323 For each node in a type chain, print the raw data for each member of\n\
1324 the type structure, and the interpretation of the data."),
1325 &maintenanceprintlist
);
1326 set_cmd_completer (cmd
, expression_completer
);
1328 add_cmd ("statistics", class_maintenance
, maintenance_print_statistics
,
1329 _("Print statistics about internal gdb state."),
1330 &maintenanceprintlist
);
1332 add_cmd ("architecture", class_maintenance
,
1333 maintenance_print_architecture
, _("\
1334 Print the internal architecture configuration.\n\
1335 Takes an optional file parameter."),
1336 &maintenanceprintlist
);
1338 add_basic_prefix_cmd ("check", class_maintenance
, _("\
1339 Commands for checking internal gdb state."),
1340 &maintenancechecklist
, 0,
1343 add_cmd ("translate-address", class_maintenance
,
1344 maintenance_translate_address
,
1345 _("Translate a section name and address to a symbol."),
1348 add_cmd ("deprecate", class_maintenance
, maintenance_deprecate
, _("\
1349 Deprecate a command (for testing purposes).\n\
1350 Usage: maintenance deprecate COMMANDNAME [\"REPLACEMENT\"]\n\
1351 This is used by the testsuite to check the command deprecator.\n\
1352 You probably shouldn't use this,\n\
1353 rather you should use the C function deprecate_cmd()."), &maintenancelist
);
1355 add_cmd ("undeprecate", class_maintenance
, maintenance_undeprecate
, _("\
1356 Undeprecate a command (for testing purposes).\n\
1357 Usage: maintenance undeprecate COMMANDNAME\n\
1358 This is used by the testsuite to check the command deprecator.\n\
1359 You probably shouldn't use this."),
1362 add_cmd ("selftest", class_maintenance
, maintenance_selftest
, _("\
1363 Run gdb's unit tests.\n\
1364 Usage: maintenance selftest [FILTER]\n\
1365 This will run any unit tests that were built in to gdb.\n\
1366 If a filter is given, only the tests with that value in their name will ran."),
1369 add_cmd ("selftests", class_maintenance
, maintenance_info_selftests
,
1370 _("List the registered selftests."), &maintenanceinfolist
);
1372 add_setshow_boolean_cmd ("profile", class_maintenance
,
1373 &maintenance_profile_p
, _("\
1374 Set internal profiling."), _("\
1375 Show internal profiling."), _("\
1376 When enabled GDB is profiled."),
1377 maintenance_set_profile_cmd
,
1378 show_maintenance_profile_p
,
1379 &maintenance_set_cmdlist
,
1380 &maintenance_show_cmdlist
);
1382 add_setshow_zuinteger_unlimited_cmd ("worker-threads",
1384 &n_worker_threads
, _("\
1385 Set the number of worker threads GDB can use."), _("\
1386 Show the number of worker threads GDB can use."), _("\
1387 GDB may use multiple threads to speed up certain CPU-intensive operations,\n\
1388 such as demangling symbol names."),
1389 maintenance_set_worker_threads
,
1390 maintenance_show_worker_threads
,
1391 &maintenance_set_cmdlist
,
1392 &maintenance_show_cmdlist
);
1394 update_thread_pool_size ();