1 /* Support for GDB maintenance commands.
3 Copyright (C) 1992-2019 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"
44 #include "cli/cli-decode.h"
45 #include "cli/cli-utils.h"
46 #include "cli/cli-setshow.h"
47 #include "cli/cli-cmds.h"
49 static void maintenance_do_deprecate (const char *, int);
51 /* Access the maintenance subcommands. */
54 maintenance_command (const char *args
, int from_tty
)
56 printf_unfiltered (_("\"maintenance\" must be followed by "
57 "the name of a maintenance command.\n"));
58 help_list (maintenancelist
, "maintenance ", all_commands
, gdb_stdout
);
63 maintenance_dump_me (const char *args
, int from_tty
)
65 if (query (_("Should GDB dump core? ")))
68 /* SIGQUIT by default is ignored, so use SIGABRT instead. */
69 signal (SIGABRT
, SIG_DFL
);
70 kill (getpid (), SIGABRT
);
72 signal (SIGQUIT
, SIG_DFL
);
73 kill (getpid (), SIGQUIT
);
79 /* Stimulate the internal error mechanism that GDB uses when an
80 internal problem is detected. Allows testing of the mechanism.
81 Also useful when the user wants to drop a core file but not exit
85 maintenance_internal_error (const char *args
, int from_tty
)
87 internal_error (__FILE__
, __LINE__
, "%s", (args
== NULL
? "" : args
));
90 /* Stimulate the internal error mechanism that GDB uses when an
91 internal problem is detected. Allows testing of the mechanism.
92 Also useful when the user wants to drop a core file but not exit
96 maintenance_internal_warning (const char *args
, int from_tty
)
98 internal_warning (__FILE__
, __LINE__
, "%s", (args
== NULL
? "" : args
));
101 /* Stimulate the internal error mechanism that GDB uses when an
102 demangler problem is detected. Allows testing of the mechanism. */
105 maintenance_demangler_warning (const char *args
, int from_tty
)
107 demangler_warning (__FILE__
, __LINE__
, "%s", (args
== NULL
? "" : args
));
110 /* Old command to demangle a string. The command has been moved to "demangle".
111 It is kept for now because otherwise "mt demangle" gets interpreted as
112 "mt demangler-warning" which artificially creates an internal gdb error. */
115 maintenance_demangle (const char *args
, int from_tty
)
117 printf_filtered (_("This command has been moved to \"demangle\".\n"));
121 maintenance_time_display (const char *args
, int from_tty
)
123 if (args
== NULL
|| *args
== '\0')
124 printf_unfiltered (_("\"maintenance time\" takes a numeric argument.\n"));
126 set_per_command_time (strtol (args
, NULL
, 10));
130 maintenance_space_display (const char *args
, int from_tty
)
132 if (args
== NULL
|| *args
== '\0')
133 printf_unfiltered ("\"maintenance space\" takes a numeric argument.\n");
135 set_per_command_space (strtol (args
, NULL
, 10));
138 /* The "maintenance info" command is defined as a prefix, with
139 allow_unknown 0. Therefore, its own definition is called only for
140 "maintenance info" with no args. */
143 maintenance_info_command (const char *arg
, int from_tty
)
145 printf_unfiltered (_("\"maintenance info\" must be followed "
146 "by the name of an info command.\n"));
147 help_list (maintenanceinfolist
, "maintenance info ", all_commands
,
151 /* The "maintenance check" command is defined as a prefix, with
152 allow_unknown 0. Therefore, its own definition is called only for
153 "maintenance check" with no args. */
156 maintenance_check_command (const char *arg
, int from_tty
)
158 printf_unfiltered (_("\"maintenance check\" must be followed "
159 "by the name of a check command.\n"));
160 help_list (maintenancechecklist
, "maintenance check ", all_commands
,
164 /* Mini tokenizing lexer for 'maint info sections' command. */
167 match_substring (const char *string
, const char *substr
)
169 int substr_len
= strlen(substr
);
172 while ((tok
= strstr (string
, substr
)) != NULL
)
174 /* Got a partial match. Is it a whole word? */
179 /* Token is delimited at the front... */
180 if (tok
[substr_len
] == ' '
181 || tok
[substr_len
] == '\t'
182 || tok
[substr_len
] == '\0')
184 /* Token is delimited at the rear. Got a whole-word match. */
188 /* Token didn't match as a whole word. Advance and try again. */
195 match_bfd_flags (const char *string
, flagword flags
)
197 if (flags
& SEC_ALLOC
)
198 if (match_substring (string
, "ALLOC"))
200 if (flags
& SEC_LOAD
)
201 if (match_substring (string
, "LOAD"))
203 if (flags
& SEC_RELOC
)
204 if (match_substring (string
, "RELOC"))
206 if (flags
& SEC_READONLY
)
207 if (match_substring (string
, "READONLY"))
209 if (flags
& SEC_CODE
)
210 if (match_substring (string
, "CODE"))
212 if (flags
& SEC_DATA
)
213 if (match_substring (string
, "DATA"))
216 if (match_substring (string
, "ROM"))
218 if (flags
& SEC_CONSTRUCTOR
)
219 if (match_substring (string
, "CONSTRUCTOR"))
221 if (flags
& SEC_HAS_CONTENTS
)
222 if (match_substring (string
, "HAS_CONTENTS"))
224 if (flags
& SEC_NEVER_LOAD
)
225 if (match_substring (string
, "NEVER_LOAD"))
227 if (flags
& SEC_COFF_SHARED_LIBRARY
)
228 if (match_substring (string
, "COFF_SHARED_LIBRARY"))
230 if (flags
& SEC_IS_COMMON
)
231 if (match_substring (string
, "IS_COMMON"))
238 print_bfd_flags (flagword flags
)
240 if (flags
& SEC_ALLOC
)
241 printf_filtered (" ALLOC");
242 if (flags
& SEC_LOAD
)
243 printf_filtered (" LOAD");
244 if (flags
& SEC_RELOC
)
245 printf_filtered (" RELOC");
246 if (flags
& SEC_READONLY
)
247 printf_filtered (" READONLY");
248 if (flags
& SEC_CODE
)
249 printf_filtered (" CODE");
250 if (flags
& SEC_DATA
)
251 printf_filtered (" DATA");
253 printf_filtered (" ROM");
254 if (flags
& SEC_CONSTRUCTOR
)
255 printf_filtered (" CONSTRUCTOR");
256 if (flags
& SEC_HAS_CONTENTS
)
257 printf_filtered (" HAS_CONTENTS");
258 if (flags
& SEC_NEVER_LOAD
)
259 printf_filtered (" NEVER_LOAD");
260 if (flags
& SEC_COFF_SHARED_LIBRARY
)
261 printf_filtered (" COFF_SHARED_LIBRARY");
262 if (flags
& SEC_IS_COMMON
)
263 printf_filtered (" IS_COMMON");
267 maint_print_section_info (const char *name
, flagword flags
,
268 CORE_ADDR addr
, CORE_ADDR endaddr
,
269 unsigned long filepos
, int addr_size
)
271 printf_filtered (" %s", hex_string_custom (addr
, addr_size
));
272 printf_filtered ("->%s", hex_string_custom (endaddr
, addr_size
));
273 printf_filtered (" at %s",
274 hex_string_custom ((unsigned long) filepos
, 8));
275 printf_filtered (": %s", name
);
276 print_bfd_flags (flags
);
277 printf_filtered ("\n");
280 /* Information passed between the "maintenance info sections" command, and
281 the worker function that prints each section. */
282 struct maint_print_section_data
284 /* The GDB objfile we're printing this section for. */
285 struct objfile
*objfile
;
287 /* The argument string passed by the user to the top level maintenance
288 info sections command. Used for filtering which sections are
292 /* The number of digits in the highest section index for all sections
293 from the bfd object associated with OBJFILE. Used when pretty
294 printing the index number to ensure all of the indexes line up. */
298 maint_print_section_data (struct objfile
*objfile
, const char *arg
,
303 int section_count
= gdb_bfd_count_sections (abfd
);
304 index_digits
= ((int) log10 ((float) section_count
)) + 1;
308 maint_print_section_data () = delete;
309 maint_print_section_data (const maint_print_section_data
&) = delete;
312 /* Helper function to pretty-print the section index of ASECT from ABFD.
313 The INDEX_DIGITS is the number of digits in the largest index that will
314 be printed, and is used to pretty-print the resulting string. */
317 print_section_index (bfd
*abfd
,
322 = string_printf (" [%d] ", gdb_bfd_section_index (abfd
, asect
));
323 /* The '+ 4' for the leading and trailing characters. */
324 printf_filtered ("%-*s", (index_digits
+ 4), result
.c_str ());
327 /* Print information about ASECT from ABFD. DATUM holds a pointer to a
328 maint_print_section_data object. The section will be printed using the
329 VMA's from the bfd, which will not be the relocated addresses for bfds
330 that should be relocated. The information must be printed with the
331 same layout as PRINT_OBJFILE_SECTION_INFO below. */
334 print_bfd_section_info (bfd
*abfd
,
338 flagword flags
= bfd_section_flags (asect
);
339 const char *name
= bfd_section_name (asect
);
340 maint_print_section_data
*print_data
= (maint_print_section_data
*) datum
;
341 const char *arg
= print_data
->arg
;
343 if (arg
== NULL
|| *arg
== '\0'
344 || match_substring (arg
, name
)
345 || match_bfd_flags (arg
, flags
))
347 struct gdbarch
*gdbarch
= gdbarch_from_bfd (abfd
);
348 int addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
349 CORE_ADDR addr
, endaddr
;
351 addr
= bfd_section_vma (asect
);
352 endaddr
= addr
+ bfd_section_size (asect
);
353 print_section_index (abfd
, asect
, print_data
->index_digits
);
354 maint_print_section_info (name
, flags
, addr
, endaddr
,
355 asect
->filepos
, addr_size
);
359 /* Print information about ASECT which is GDB's wrapper around a section
360 from ABFD. The information must be printed with the same layout as
361 PRINT_BFD_SECTION_INFO above. PRINT_DATA holds information used to
362 filter which sections are printed, and for formatting the output. */
365 print_objfile_section_info (bfd
*abfd
,
366 struct obj_section
*asect
,
367 maint_print_section_data
*print_data
)
369 flagword flags
= bfd_section_flags (asect
->the_bfd_section
);
370 const char *name
= bfd_section_name (asect
->the_bfd_section
);
371 const char *string
= print_data
->arg
;
373 if (string
== NULL
|| *string
== '\0'
374 || match_substring (string
, name
)
375 || match_bfd_flags (string
, flags
))
377 struct gdbarch
*gdbarch
= gdbarch_from_bfd (abfd
);
378 int addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
380 print_section_index (abfd
, asect
->the_bfd_section
,
381 print_data
->index_digits
);
382 maint_print_section_info (name
, flags
,
383 obj_section_addr (asect
),
384 obj_section_endaddr (asect
),
385 asect
->the_bfd_section
->filepos
,
390 /* Find an obj_section, GDB's wrapper around a bfd section for ASECTION
391 from ABFD. It might be that no such wrapper exists (for example debug
392 sections don't have such wrappers) in which case nullptr is returned. */
395 maint_obj_section_from_bfd_section (bfd
*abfd
,
399 if (ofile
->sections
== nullptr)
403 = &ofile
->sections
[gdb_bfd_section_index (abfd
, asection
)];
405 if (osect
>= ofile
->sections_end
)
411 /* Print information about ASECT from ABFD. DATUM holds a pointer to a
412 maint_print_section_data object. Where possible the information for
413 ASECT will print the relocated addresses of the section. */
416 print_bfd_section_info_maybe_relocated (bfd
*abfd
,
420 maint_print_section_data
*print_data
= (maint_print_section_data
*) datum
;
421 objfile
*objfile
= print_data
->objfile
;
423 gdb_assert (objfile
->sections
!= NULL
);
425 = maint_obj_section_from_bfd_section (abfd
, asect
, objfile
);
427 if (osect
->the_bfd_section
== NULL
)
428 print_bfd_section_info (abfd
, asect
, datum
);
430 print_objfile_section_info (abfd
, osect
, print_data
);
433 /* Implement the "maintenance info sections" command. */
436 maintenance_info_sections (const char *arg
, int from_tty
)
442 printf_filtered (_("Exec file:\n"));
443 printf_filtered (" `%s', ", bfd_get_filename (exec_bfd
));
445 printf_filtered (_("file type %s.\n"), bfd_get_target (exec_bfd
));
447 /* Only this function cares about the 'ALLOBJ' argument;
448 if 'ALLOBJ' is the only argument, discard it rather than
449 passing it down to print_objfile_section_info (which
450 wouldn't know how to handle it). */
451 if (arg
&& strcmp (arg
, "ALLOBJ") == 0)
457 for (objfile
*ofile
: current_program_space
->objfiles ())
460 printf_filtered (_(" Object file: %s\n"),
461 bfd_get_filename (ofile
->obfd
));
462 else if (ofile
->obfd
!= exec_bfd
)
465 maint_print_section_data
print_data (ofile
, arg
, ofile
->obfd
);
467 bfd_map_over_sections (ofile
->obfd
,
468 print_bfd_section_info_maybe_relocated
,
469 (void *) &print_data
);
475 maint_print_section_data
print_data (nullptr, arg
, core_bfd
);
477 printf_filtered (_("Core file:\n"));
478 printf_filtered (" `%s', ", bfd_get_filename (core_bfd
));
480 printf_filtered (_("file type %s.\n"), bfd_get_target (core_bfd
));
481 bfd_map_over_sections (core_bfd
, print_bfd_section_info
,
482 (void *) &print_data
);
487 maintenance_print_statistics (const char *args
, int from_tty
)
489 print_objfile_statistics ();
490 print_symbol_bcache_statistics ();
494 maintenance_print_architecture (const char *args
, int from_tty
)
496 struct gdbarch
*gdbarch
= get_current_arch ();
499 gdbarch_dump (gdbarch
, gdb_stdout
);
504 if (!file
.open (args
, "w"))
505 perror_with_name (_("maintenance print architecture"));
506 gdbarch_dump (gdbarch
, &file
);
510 /* The "maintenance print" command is defined as a prefix, with
511 allow_unknown 0. Therefore, its own definition is called only for
512 "maintenance print" with no args. */
515 maintenance_print_command (const char *arg
, int from_tty
)
517 printf_unfiltered (_("\"maintenance print\" must be followed "
518 "by the name of a print command.\n"));
519 help_list (maintenanceprintlist
, "maintenance print ", all_commands
,
523 /* The "maintenance translate-address" command converts a section and address
524 to a symbol. This can be called in two ways:
525 maintenance translate-address <secname> <addr>
526 or maintenance translate-address <addr>. */
529 maintenance_translate_address (const char *arg
, int from_tty
)
532 struct obj_section
*sect
;
534 struct bound_minimal_symbol sym
;
536 if (arg
== NULL
|| *arg
== 0)
537 error (_("requires argument (address or section + address)"));
543 { /* See if we have a valid section name. */
544 while (*p
&& !isspace (*p
)) /* Find end of section name. */
546 if (*p
== '\000') /* End of command? */
547 error (_("Need to specify section name and address"));
549 int arg_len
= p
- arg
;
550 p
= skip_spaces (p
+ 1);
552 for (objfile
*objfile
: current_program_space
->objfiles ())
553 ALL_OBJFILE_OSECTIONS (objfile
, sect
)
555 if (strncmp (sect
->the_bfd_section
->name
, arg
, arg_len
) == 0)
559 error (_("Unknown section %s."), arg
);
563 address
= parse_and_eval_address (p
);
566 sym
= lookup_minimal_symbol_by_pc_section (address
, sect
);
568 sym
= lookup_minimal_symbol_by_pc (address
);
572 const char *symbol_name
= sym
.minsym
->print_name ();
573 const char *symbol_offset
574 = pulongest (address
- BMSYMBOL_VALUE_ADDRESS (sym
));
576 sect
= MSYMBOL_OBJ_SECTION(sym
.objfile
, sym
.minsym
);
579 const char *section_name
;
580 const char *obj_name
;
582 gdb_assert (sect
->the_bfd_section
&& sect
->the_bfd_section
->name
);
583 section_name
= sect
->the_bfd_section
->name
;
585 gdb_assert (sect
->objfile
&& objfile_name (sect
->objfile
));
586 obj_name
= objfile_name (sect
->objfile
);
588 if (MULTI_OBJFILE_P ())
589 printf_filtered (_("%s + %s in section %s of %s\n"),
590 symbol_name
, symbol_offset
,
591 section_name
, obj_name
);
593 printf_filtered (_("%s + %s in section %s\n"),
594 symbol_name
, symbol_offset
, section_name
);
597 printf_filtered (_("%s + %s\n"), symbol_name
, symbol_offset
);
600 printf_filtered (_("no symbol at %s:%s\n"),
601 sect
->the_bfd_section
->name
, hex_string (address
));
603 printf_filtered (_("no symbol at %s\n"), hex_string (address
));
609 /* When a command is deprecated the user will be warned the first time
610 the command is used. If possible, a replacement will be
614 maintenance_deprecate (const char *args
, int from_tty
)
616 if (args
== NULL
|| *args
== '\0')
618 printf_unfiltered (_("\"maintenance deprecate\" takes an argument,\n\
619 the command you want to deprecate, and optionally the replacement command\n\
620 enclosed in quotes.\n"));
623 maintenance_do_deprecate (args
, 1);
628 maintenance_undeprecate (const char *args
, int from_tty
)
630 if (args
== NULL
|| *args
== '\0')
632 printf_unfiltered (_("\"maintenance undeprecate\" takes an argument, \n\
633 the command you want to undeprecate.\n"));
636 maintenance_do_deprecate (args
, 0);
639 /* You really shouldn't be using this. It is just for the testsuite.
640 Rather, you should use deprecate_cmd() when the command is created
641 in _initialize_blah().
643 This function deprecates a command and optionally assigns it a
647 maintenance_do_deprecate (const char *text
, int deprecate
)
649 struct cmd_list_element
*alias
= NULL
;
650 struct cmd_list_element
*prefix_cmd
= NULL
;
651 struct cmd_list_element
*cmd
= NULL
;
653 const char *start_ptr
= NULL
;
654 const char *end_ptr
= NULL
;
656 char *replacement
= NULL
;
661 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
663 printf_filtered (_("Can't find command '%s' to deprecate.\n"), text
);
669 /* Look for a replacement command. */
670 start_ptr
= strchr (text
, '\"');
671 if (start_ptr
!= NULL
)
674 end_ptr
= strrchr (start_ptr
, '\"');
677 len
= end_ptr
- start_ptr
;
678 replacement
= savestring (start_ptr
, len
);
683 if (!start_ptr
|| !end_ptr
)
687 /* If they used an alias, we only want to deprecate the alias.
689 Note the MALLOCED_REPLACEMENT test. If the command's replacement
690 string was allocated at compile time we don't want to free the
694 if (alias
->malloced_replacement
)
695 xfree ((char *) alias
->replacement
);
699 alias
->deprecated_warn_user
= 1;
700 alias
->cmd_deprecated
= 1;
704 alias
->deprecated_warn_user
= 0;
705 alias
->cmd_deprecated
= 0;
707 alias
->replacement
= replacement
;
708 alias
->malloced_replacement
= 1;
713 if (cmd
->malloced_replacement
)
714 xfree ((char *) cmd
->replacement
);
718 cmd
->deprecated_warn_user
= 1;
719 cmd
->cmd_deprecated
= 1;
723 cmd
->deprecated_warn_user
= 0;
724 cmd
->cmd_deprecated
= 0;
726 cmd
->replacement
= replacement
;
727 cmd
->malloced_replacement
= 1;
733 /* Maintenance set/show framework. */
735 struct cmd_list_element
*maintenance_set_cmdlist
;
736 struct cmd_list_element
*maintenance_show_cmdlist
;
739 maintenance_set_cmd (const char *args
, int from_tty
)
741 printf_unfiltered (_("\"maintenance set\" must be followed "
742 "by the name of a set command.\n"));
743 help_list (maintenance_set_cmdlist
, "maintenance set ", all_commands
,
748 maintenance_show_cmd (const char *args
, int from_tty
)
750 cmd_show_list (maintenance_show_cmdlist
, from_tty
, "");
753 /* "maintenance with" command. */
756 maintenance_with_cmd (const char *args
, int from_tty
)
758 with_command_1 ("maintenance set ", maintenance_set_cmdlist
, args
, from_tty
);
761 /* "maintenance with" command completer. */
764 maintenance_with_cmd_completer (struct cmd_list_element
*ignore
,
765 completion_tracker
&tracker
,
766 const char *text
, const char * /*word*/)
768 with_command_completer_1 ("maintenance set ", tracker
, text
);
771 /* Profiling support. */
773 static bool maintenance_profile_p
;
775 show_maintenance_profile_p (struct ui_file
*file
, int from_tty
,
776 struct cmd_list_element
*c
, const char *value
)
778 fprintf_filtered (file
, _("Internal profiling is %s.\n"), value
);
783 #define TEXTEND &_etext
784 #elif defined (HAVE_ETEXT)
786 #define TEXTEND &etext
789 #if defined (HAVE_MONSTARTUP) && defined (HAVE__MCLEANUP) && defined (TEXTEND)
791 static int profiling_state
;
793 EXTERN_C
void _mcleanup (void);
796 mcleanup_wrapper (void)
802 EXTERN_C
void monstartup (unsigned long, unsigned long);
806 maintenance_set_profile_cmd (const char *args
, int from_tty
,
807 struct cmd_list_element
*c
)
809 if (maintenance_profile_p
== profiling_state
)
812 profiling_state
= maintenance_profile_p
;
814 if (maintenance_profile_p
)
816 static int profiling_initialized
;
818 if (!profiling_initialized
)
820 atexit (mcleanup_wrapper
);
821 profiling_initialized
= 1;
824 /* "main" is now always the first function in the text segment, so use
825 its address for monstartup. */
826 monstartup ((unsigned long) &main
, (unsigned long) TEXTEND
);
830 extern void _mcleanup (void);
837 maintenance_set_profile_cmd (const char *args
, int from_tty
,
838 struct cmd_list_element
*c
)
840 error (_("Profiling support is not available on this system."));
844 /* If true, display time usage both at startup and for each command. */
846 static bool per_command_time
;
848 /* If true, display space usage both at startup and for each command. */
850 static bool per_command_space
;
852 /* If true, display basic symtab stats for each command. */
854 static bool per_command_symtab
;
856 /* mt per-command commands. */
858 static struct cmd_list_element
*per_command_setlist
;
859 static struct cmd_list_element
*per_command_showlist
;
861 /* Set whether to display time statistics to NEW_VALUE
862 (non-zero means true). */
865 set_per_command_time (int new_value
)
867 per_command_time
= new_value
;
870 /* Set whether to display space statistics to NEW_VALUE
871 (non-zero means true). */
874 set_per_command_space (int new_value
)
876 per_command_space
= new_value
;
879 /* Count the number of symtabs and blocks. */
882 count_symtabs_and_blocks (int *nr_symtabs_ptr
, int *nr_compunit_symtabs_ptr
,
886 int nr_compunit_symtabs
= 0;
889 /* When collecting statistics during startup, this is called before
890 pretty much anything in gdb has been initialized, and thus
891 current_program_space may be NULL. */
892 if (current_program_space
!= NULL
)
894 for (objfile
*o
: current_program_space
->objfiles ())
896 for (compunit_symtab
*cu
: o
->compunits ())
898 ++nr_compunit_symtabs
;
899 nr_blocks
+= BLOCKVECTOR_NBLOCKS (COMPUNIT_BLOCKVECTOR (cu
));
900 nr_symtabs
+= std::distance (compunit_filetabs (cu
).begin (),
901 compunit_filetabs (cu
).end ());
906 *nr_symtabs_ptr
= nr_symtabs
;
907 *nr_compunit_symtabs_ptr
= nr_compunit_symtabs
;
908 *nr_blocks_ptr
= nr_blocks
;
911 /* As indicated by display_time and display_space, report GDB's
912 elapsed time and space usage from the base time and space recorded
915 scoped_command_stats::~scoped_command_stats ()
917 /* Early exit if we're not reporting any stats. It can be expensive to
918 compute the pre-command values so don't collect them at all if we're
919 not reporting stats. Alas this doesn't work in the startup case because
920 we don't know yet whether we will be reporting the stats. For the
921 startup case collect the data anyway (it should be cheap at this point),
922 and leave it to the reporter to decide whether to print them. */
925 && !per_command_space
926 && !per_command_symtab
)
929 if (m_time_enabled
&& per_command_time
)
931 print_time (_("command finished"));
933 using namespace std::chrono
;
935 run_time_clock::duration cmd_time
936 = run_time_clock::now () - m_start_cpu_time
;
938 steady_clock::duration wall_time
939 = steady_clock::now () - m_start_wall_time
;
940 /* Subtract time spend in prompt_for_continue from walltime. */
941 wall_time
-= get_prompt_for_continue_wait_time ();
943 printf_unfiltered (!m_msg_type
944 ? _("Startup time: %.6f (cpu), %.6f (wall)\n")
945 : _("Command execution time: %.6f (cpu), %.6f (wall)\n"),
946 duration
<double> (cmd_time
).count (),
947 duration
<double> (wall_time
).count ());
950 if (m_space_enabled
&& per_command_space
)
952 #ifdef HAVE_USEFUL_SBRK
953 char *lim
= (char *) sbrk (0);
955 long space_now
= lim
- lim_at_start
;
956 long space_diff
= space_now
- m_start_space
;
958 printf_unfiltered (!m_msg_type
959 ? _("Space used: %ld (%s%ld during startup)\n")
960 : _("Space used: %ld (%s%ld for this command)\n"),
962 (space_diff
>= 0 ? "+" : ""),
967 if (m_symtab_enabled
&& per_command_symtab
)
969 int nr_symtabs
, nr_compunit_symtabs
, nr_blocks
;
971 count_symtabs_and_blocks (&nr_symtabs
, &nr_compunit_symtabs
, &nr_blocks
);
972 printf_unfiltered (_("#symtabs: %d (+%d),"
973 " #compunits: %d (+%d),"
974 " #blocks: %d (+%d)\n"),
976 nr_symtabs
- m_start_nr_symtabs
,
979 - m_start_nr_compunit_symtabs
),
981 nr_blocks
- m_start_nr_blocks
);
985 scoped_command_stats::scoped_command_stats (bool msg_type
)
986 : m_msg_type (msg_type
)
988 if (!m_msg_type
|| per_command_space
)
990 #ifdef HAVE_USEFUL_SBRK
991 char *lim
= (char *) sbrk (0);
992 m_start_space
= lim
- lim_at_start
;
999 if (msg_type
== 0 || per_command_time
)
1001 using namespace std::chrono
;
1003 m_start_cpu_time
= run_time_clock::now ();
1004 m_start_wall_time
= steady_clock::now ();
1007 if (per_command_time
)
1008 print_time (_("command started"));
1013 if (msg_type
== 0 || per_command_symtab
)
1015 int nr_symtabs
, nr_compunit_symtabs
, nr_blocks
;
1017 count_symtabs_and_blocks (&nr_symtabs
, &nr_compunit_symtabs
, &nr_blocks
);
1018 m_start_nr_symtabs
= nr_symtabs
;
1019 m_start_nr_compunit_symtabs
= nr_compunit_symtabs
;
1020 m_start_nr_blocks
= nr_blocks
;
1021 m_symtab_enabled
= 1;
1024 m_symtab_enabled
= 0;
1026 /* Initialize timer to keep track of how long we waited for the user. */
1027 reset_prompt_for_continue_wait_time ();
1033 scoped_command_stats::print_time (const char *msg
)
1035 using namespace std::chrono
;
1037 auto now
= system_clock::now ();
1038 auto ticks
= now
.time_since_epoch ().count () / (1000 * 1000);
1039 auto millis
= ticks
% 1000;
1041 std::time_t as_time
= system_clock::to_time_t (now
);
1043 localtime_r (&as_time
, &tm
);
1046 strftime (out
, sizeof (out
), "%F %H:%M:%S", &tm
);
1048 printf_unfiltered ("%s.%03d - %s\n", out
, (int) millis
, msg
);
1051 /* Handle unknown "mt set per-command" arguments.
1052 In this case have "mt set per-command on|off" affect every setting. */
1055 set_per_command_cmd (const char *args
, int from_tty
)
1057 struct cmd_list_element
*list
;
1060 val
= parse_cli_boolean_value (args
);
1062 error (_("Bad value for 'mt set per-command no'."));
1064 for (list
= per_command_setlist
; list
!= NULL
; list
= list
->next
)
1065 if (list
->var_type
== var_boolean
)
1067 gdb_assert (list
->type
== set_cmd
);
1068 do_set_command (args
, from_tty
, list
);
1072 /* Command "show per-command" displays summary of all the current
1073 "show per-command " settings. */
1076 show_per_command_cmd (const char *args
, int from_tty
)
1078 cmd_show_list (per_command_showlist
, from_tty
, "");
1082 /* The "maintenance selftest" command. */
1085 maintenance_selftest (const char *args
, int from_tty
)
1088 selftests::run_tests (args
);
1090 printf_filtered (_("\
1091 Selftests have been disabled for this build.\n"));
1096 maintenance_info_selftests (const char *arg
, int from_tty
)
1099 printf_filtered ("Registered selftests:\n");
1100 selftests::for_each_selftest ([] (const std::string
&name
) {
1101 printf_filtered (" - %s\n", name
.c_str ());
1104 printf_filtered (_("\
1105 Selftests have been disabled for this build.\n"));
1111 _initialize_maint_cmds (void)
1113 struct cmd_list_element
*cmd
;
1115 add_prefix_cmd ("maintenance", class_maintenance
, maintenance_command
, _("\
1116 Commands for use by GDB maintainers.\n\
1117 Includes commands to dump specific internal GDB structures in\n\
1118 a human readable form, to cause GDB to deliberately dump core, etc."),
1119 &maintenancelist
, "maintenance ", 0,
1122 add_com_alias ("mt", "maintenance", class_maintenance
, 1);
1124 add_prefix_cmd ("info", class_maintenance
, maintenance_info_command
, _("\
1125 Commands for showing internal info about the program being debugged."),
1126 &maintenanceinfolist
, "maintenance info ", 0,
1128 add_alias_cmd ("i", "info", class_maintenance
, 1, &maintenancelist
);
1130 add_cmd ("sections", class_maintenance
, maintenance_info_sections
, _("\
1131 List the BFD sections of the exec and core files.\n\
1132 Arguments may be any combination of:\n\
1133 [one or more section names]\n\
1134 ALLOC LOAD RELOC READONLY CODE DATA ROM CONSTRUCTOR\n\
1135 HAS_CONTENTS NEVER_LOAD COFF_SHARED_LIBRARY IS_COMMON\n\
1136 Sections matching any argument will be listed (no argument\n\
1137 implies all sections). In addition, the special argument\n\
1139 lists all sections from all object files, including shared libraries."),
1140 &maintenanceinfolist
);
1142 add_prefix_cmd ("print", class_maintenance
, maintenance_print_command
,
1143 _("Maintenance command for printing GDB internal state."),
1144 &maintenanceprintlist
, "maintenance print ", 0,
1147 add_prefix_cmd ("set", class_maintenance
, maintenance_set_cmd
, _("\
1148 Set GDB internal variables used by the GDB maintainer.\n\
1149 Configure variables internal to GDB that aid in GDB's maintenance"),
1150 &maintenance_set_cmdlist
, "maintenance set ",
1154 add_prefix_cmd ("show", class_maintenance
, maintenance_show_cmd
, _("\
1155 Show GDB internal variables used by the GDB maintainer.\n\
1156 Configure variables internal to GDB that aid in GDB's maintenance"),
1157 &maintenance_show_cmdlist
, "maintenance show ",
1161 cmd
= add_cmd ("with", class_maintenance
, maintenance_with_cmd
, _("\
1162 Like \"with\", but works with \"maintenance set\" variables.\n\
1163 Usage: maintenance with SETTING [VALUE] [-- COMMAND]\n\
1164 With no COMMAND, repeats the last executed command.\n\
1165 SETTING is any setting you can change with the \"maintenance set\"\n\
1168 set_cmd_completer_handle_brkchars (cmd
, maintenance_with_cmd_completer
);
1171 add_cmd ("dump-me", class_maintenance
, maintenance_dump_me
, _("\
1172 Get fatal error; make debugger dump its core.\n\
1173 GDB sets its handling of SIGQUIT back to SIG_DFL and then sends\n\
1174 itself a SIGQUIT signal."),
1178 add_cmd ("internal-error", class_maintenance
,
1179 maintenance_internal_error
, _("\
1180 Give GDB an internal error.\n\
1181 Cause GDB to behave as if an internal error was detected."),
1184 add_cmd ("internal-warning", class_maintenance
,
1185 maintenance_internal_warning
, _("\
1186 Give GDB an internal warning.\n\
1187 Cause GDB to behave as if an internal warning was reported."),
1190 add_cmd ("demangler-warning", class_maintenance
,
1191 maintenance_demangler_warning
, _("\
1192 Give GDB a demangler warning.\n\
1193 Cause GDB to behave as if a demangler warning was reported."),
1196 cmd
= add_cmd ("demangle", class_maintenance
, maintenance_demangle
, _("\
1197 This command has been moved to \"demangle\"."),
1199 deprecate_cmd (cmd
, "demangle");
1201 add_prefix_cmd ("per-command", class_maintenance
, set_per_command_cmd
, _("\
1202 Per-command statistics settings."),
1203 &per_command_setlist
, "maintenance set per-command ",
1204 1/*allow-unknown*/, &maintenance_set_cmdlist
);
1206 add_prefix_cmd ("per-command", class_maintenance
, show_per_command_cmd
, _("\
1207 Show per-command statistics settings."),
1208 &per_command_showlist
, "maintenance show per-command ",
1209 0/*allow-unknown*/, &maintenance_show_cmdlist
);
1211 add_setshow_boolean_cmd ("time", class_maintenance
,
1212 &per_command_time
, _("\
1213 Set whether to display per-command execution time."), _("\
1214 Show whether to display per-command execution time."),
1216 If enabled, the execution time for each command will be\n\
1217 displayed following the command's output."),
1219 &per_command_setlist
, &per_command_showlist
);
1221 add_setshow_boolean_cmd ("space", class_maintenance
,
1222 &per_command_space
, _("\
1223 Set whether to display per-command space usage."), _("\
1224 Show whether to display per-command space usage."),
1226 If enabled, the space usage for each command will be\n\
1227 displayed following the command's output."),
1229 &per_command_setlist
, &per_command_showlist
);
1231 add_setshow_boolean_cmd ("symtab", class_maintenance
,
1232 &per_command_symtab
, _("\
1233 Set whether to display per-command symtab statistics."), _("\
1234 Show whether to display per-command symtab statistics."),
1236 If enabled, the basic symtab statistics for each command will be\n\
1237 displayed following the command's output."),
1239 &per_command_setlist
, &per_command_showlist
);
1241 /* This is equivalent to "mt set per-command time on".
1242 Kept because some people are used to typing "mt time 1". */
1243 add_cmd ("time", class_maintenance
, maintenance_time_display
, _("\
1244 Set the display of time usage.\n\
1245 If nonzero, will cause the execution time for each command to be\n\
1246 displayed, following the command's output."),
1249 /* This is equivalent to "mt set per-command space on".
1250 Kept because some people are used to typing "mt space 1". */
1251 add_cmd ("space", class_maintenance
, maintenance_space_display
, _("\
1252 Set the display of space usage.\n\
1253 If nonzero, will cause the execution space for each command to be\n\
1254 displayed, following the command's output."),
1257 add_cmd ("type", class_maintenance
, maintenance_print_type
, _("\
1258 Print a type chain for a given symbol.\n\
1259 For each node in a type chain, print the raw data for each member of\n\
1260 the type structure, and the interpretation of the data."),
1261 &maintenanceprintlist
);
1263 add_cmd ("statistics", class_maintenance
, maintenance_print_statistics
,
1264 _("Print statistics about internal gdb state."),
1265 &maintenanceprintlist
);
1267 add_cmd ("architecture", class_maintenance
,
1268 maintenance_print_architecture
, _("\
1269 Print the internal architecture configuration.\n\
1270 Takes an optional file parameter."),
1271 &maintenanceprintlist
);
1273 add_prefix_cmd ("check", class_maintenance
, maintenance_check_command
, _("\
1274 Commands for checking internal gdb state."),
1275 &maintenancechecklist
, "maintenance check ", 0,
1278 add_cmd ("translate-address", class_maintenance
,
1279 maintenance_translate_address
,
1280 _("Translate a section name and address to a symbol."),
1283 add_cmd ("deprecate", class_maintenance
, maintenance_deprecate
, _("\
1284 Deprecate a command (for testing purposes).\n\
1285 Usage: maintenance deprecate COMMANDNAME [\"REPLACEMENT\"]\n\
1286 This is used by the testsuite to check the command deprecator.\n\
1287 You probably shouldn't use this,\n\
1288 rather you should use the C function deprecate_cmd()."), &maintenancelist
);
1290 add_cmd ("undeprecate", class_maintenance
, maintenance_undeprecate
, _("\
1291 Undeprecate a command (for testing purposes).\n\
1292 Usage: maintenance undeprecate COMMANDNAME\n\
1293 This is used by the testsuite to check the command deprecator.\n\
1294 You probably shouldn't use this."),
1297 add_cmd ("selftest", class_maintenance
, maintenance_selftest
, _("\
1298 Run gdb's unit tests.\n\
1299 Usage: maintenance selftest [FILTER]\n\
1300 This will run any unit tests that were built in to gdb.\n\
1301 If a filter is given, only the tests with that value in their name will ran."),
1304 add_cmd ("selftests", class_maintenance
, maintenance_info_selftests
,
1305 _("List the registered selftests."), &maintenanceinfolist
);
1307 add_setshow_boolean_cmd ("profile", class_maintenance
,
1308 &maintenance_profile_p
, _("\
1309 Set internal profiling."), _("\
1310 Show internal profiling."), _("\
1311 When enabled GDB is profiled."),
1312 maintenance_set_profile_cmd
,
1313 show_maintenance_profile_p
,
1314 &maintenance_set_cmdlist
,
1315 &maintenance_show_cmdlist
);