1 /* Support for GDB maintenance commands.
3 Copyright (C) 1992-2020 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"
50 #include "gdbsupport/thread-pool.h"
53 static void maintenance_do_deprecate (const char *, int);
57 maintenance_dump_me (const char *args
, int from_tty
)
59 if (query (_("Should GDB dump core? ")))
62 /* SIGQUIT by default is ignored, so use SIGABRT instead. */
63 signal (SIGABRT
, SIG_DFL
);
64 kill (getpid (), SIGABRT
);
66 signal (SIGQUIT
, SIG_DFL
);
67 kill (getpid (), SIGQUIT
);
73 /* Stimulate the internal error mechanism that GDB uses when an
74 internal problem is detected. Allows testing of the mechanism.
75 Also useful when the user wants to drop a core file but not exit
79 maintenance_internal_error (const char *args
, int from_tty
)
81 internal_error (__FILE__
, __LINE__
, "%s", (args
== NULL
? "" : args
));
84 /* Stimulate the internal error mechanism that GDB uses when an
85 internal problem is detected. Allows testing of the mechanism.
86 Also useful when the user wants to drop a core file but not exit
90 maintenance_internal_warning (const char *args
, int from_tty
)
92 internal_warning (__FILE__
, __LINE__
, "%s", (args
== NULL
? "" : args
));
95 /* Stimulate the internal error mechanism that GDB uses when an
96 demangler problem is detected. Allows testing of the mechanism. */
99 maintenance_demangler_warning (const char *args
, int from_tty
)
101 demangler_warning (__FILE__
, __LINE__
, "%s", (args
== NULL
? "" : args
));
104 /* Old command to demangle a string. The command has been moved to "demangle".
105 It is kept for now because otherwise "mt demangle" gets interpreted as
106 "mt demangler-warning" which artificially creates an internal gdb error. */
109 maintenance_demangle (const char *args
, int from_tty
)
111 printf_filtered (_("This command has been moved to \"demangle\".\n"));
115 maintenance_time_display (const char *args
, int from_tty
)
117 if (args
== NULL
|| *args
== '\0')
118 printf_unfiltered (_("\"maintenance time\" takes a numeric argument.\n"));
120 set_per_command_time (strtol (args
, NULL
, 10));
124 maintenance_space_display (const char *args
, int from_tty
)
126 if (args
== NULL
|| *args
== '\0')
127 printf_unfiltered ("\"maintenance space\" takes a numeric argument.\n");
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. */
163 match_bfd_flags (const char *string
, flagword flags
)
165 if (flags
& SEC_ALLOC
)
166 if (match_substring (string
, "ALLOC"))
168 if (flags
& SEC_LOAD
)
169 if (match_substring (string
, "LOAD"))
171 if (flags
& SEC_RELOC
)
172 if (match_substring (string
, "RELOC"))
174 if (flags
& SEC_READONLY
)
175 if (match_substring (string
, "READONLY"))
177 if (flags
& SEC_CODE
)
178 if (match_substring (string
, "CODE"))
180 if (flags
& SEC_DATA
)
181 if (match_substring (string
, "DATA"))
184 if (match_substring (string
, "ROM"))
186 if (flags
& SEC_CONSTRUCTOR
)
187 if (match_substring (string
, "CONSTRUCTOR"))
189 if (flags
& SEC_HAS_CONTENTS
)
190 if (match_substring (string
, "HAS_CONTENTS"))
192 if (flags
& SEC_NEVER_LOAD
)
193 if (match_substring (string
, "NEVER_LOAD"))
195 if (flags
& SEC_COFF_SHARED_LIBRARY
)
196 if (match_substring (string
, "COFF_SHARED_LIBRARY"))
198 if (flags
& SEC_IS_COMMON
)
199 if (match_substring (string
, "IS_COMMON"))
206 print_bfd_flags (flagword flags
)
208 if (flags
& SEC_ALLOC
)
209 printf_filtered (" ALLOC");
210 if (flags
& SEC_LOAD
)
211 printf_filtered (" LOAD");
212 if (flags
& SEC_RELOC
)
213 printf_filtered (" RELOC");
214 if (flags
& SEC_READONLY
)
215 printf_filtered (" READONLY");
216 if (flags
& SEC_CODE
)
217 printf_filtered (" CODE");
218 if (flags
& SEC_DATA
)
219 printf_filtered (" DATA");
221 printf_filtered (" ROM");
222 if (flags
& SEC_CONSTRUCTOR
)
223 printf_filtered (" CONSTRUCTOR");
224 if (flags
& SEC_HAS_CONTENTS
)
225 printf_filtered (" HAS_CONTENTS");
226 if (flags
& SEC_NEVER_LOAD
)
227 printf_filtered (" NEVER_LOAD");
228 if (flags
& SEC_COFF_SHARED_LIBRARY
)
229 printf_filtered (" COFF_SHARED_LIBRARY");
230 if (flags
& SEC_IS_COMMON
)
231 printf_filtered (" IS_COMMON");
235 maint_print_section_info (const char *name
, flagword flags
,
236 CORE_ADDR addr
, CORE_ADDR endaddr
,
237 unsigned long filepos
, int addr_size
)
239 printf_filtered (" %s", hex_string_custom (addr
, addr_size
));
240 printf_filtered ("->%s", hex_string_custom (endaddr
, addr_size
));
241 printf_filtered (" at %s",
242 hex_string_custom ((unsigned long) filepos
, 8));
243 printf_filtered (": %s", name
);
244 print_bfd_flags (flags
);
245 printf_filtered ("\n");
248 /* Information passed between the "maintenance info sections" command, and
249 the worker function that prints each section. */
250 struct maint_print_section_data
252 /* The GDB objfile we're printing this section for. */
253 struct objfile
*objfile
;
255 /* The argument string passed by the user to the top level maintenance
256 info sections command. Used for filtering which sections are
260 /* The number of digits in the highest section index for all sections
261 from the bfd object associated with OBJFILE. Used when pretty
262 printing the index number to ensure all of the indexes line up. */
266 maint_print_section_data (struct objfile
*objfile
, const char *arg
,
271 int section_count
= gdb_bfd_count_sections (abfd
);
272 index_digits
= ((int) log10 ((float) section_count
)) + 1;
276 maint_print_section_data () = delete;
277 maint_print_section_data (const maint_print_section_data
&) = delete;
280 /* Helper function to pretty-print the section index of ASECT from ABFD.
281 The INDEX_DIGITS is the number of digits in the largest index that will
282 be printed, and is used to pretty-print the resulting string. */
285 print_section_index (bfd
*abfd
,
290 = string_printf (" [%d] ", gdb_bfd_section_index (abfd
, asect
));
291 /* The '+ 4' for the leading and trailing characters. */
292 printf_filtered ("%-*s", (index_digits
+ 4), result
.c_str ());
295 /* Print information about ASECT from ABFD. The section will be printed using
296 the VMA's from the bfd, which will not be the relocated addresses for bfds
297 that should be relocated. The information must be printed with the same
298 layout as PRINT_OBJFILE_SECTION_INFO below. */
301 print_bfd_section_info (bfd
*abfd
,
303 const maint_print_section_data
&print_data
)
305 flagword flags
= bfd_section_flags (asect
);
306 const char *name
= bfd_section_name (asect
);
307 const char *arg
= print_data
.arg
;
309 if (arg
== NULL
|| *arg
== '\0'
310 || match_substring (arg
, name
)
311 || match_bfd_flags (arg
, flags
))
313 struct gdbarch
*gdbarch
= gdbarch_from_bfd (abfd
);
314 int addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
315 CORE_ADDR addr
, endaddr
;
317 addr
= bfd_section_vma (asect
);
318 endaddr
= addr
+ bfd_section_size (asect
);
319 print_section_index (abfd
, asect
, print_data
.index_digits
);
320 maint_print_section_info (name
, flags
, addr
, endaddr
,
321 asect
->filepos
, addr_size
);
325 /* Print information about ASECT which is GDB's wrapper around a section
326 from ABFD. The information must be printed with the same layout as
327 PRINT_BFD_SECTION_INFO above. PRINT_DATA holds information used to
328 filter which sections are printed, and for formatting the output. */
331 print_objfile_section_info (bfd
*abfd
,
332 struct obj_section
*asect
,
333 const maint_print_section_data
&print_data
)
335 flagword flags
= bfd_section_flags (asect
->the_bfd_section
);
336 const char *name
= bfd_section_name (asect
->the_bfd_section
);
337 const char *string
= print_data
.arg
;
339 if (string
== NULL
|| *string
== '\0'
340 || match_substring (string
, name
)
341 || match_bfd_flags (string
, flags
))
343 struct gdbarch
*gdbarch
= gdbarch_from_bfd (abfd
);
344 int addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
346 print_section_index (abfd
, asect
->the_bfd_section
,
347 print_data
.index_digits
);
348 maint_print_section_info (name
, flags
,
349 obj_section_addr (asect
),
350 obj_section_endaddr (asect
),
351 asect
->the_bfd_section
->filepos
,
356 /* Find an obj_section, GDB's wrapper around a bfd section for ASECTION
357 from ABFD. It might be that no such wrapper exists (for example debug
358 sections don't have such wrappers) in which case nullptr is returned. */
361 maint_obj_section_from_bfd_section (bfd
*abfd
,
365 if (ofile
->sections
== nullptr)
369 = &ofile
->sections
[gdb_bfd_section_index (abfd
, asection
)];
371 if (osect
>= ofile
->sections_end
)
377 /* Print information about ASECT from ABFD. Where possible the information for
378 ASECT will print the relocated addresses of the section. */
381 print_bfd_section_info_maybe_relocated
382 (bfd
*abfd
, asection
*asect
, const maint_print_section_data
&print_data
)
384 objfile
*objfile
= print_data
.objfile
;
386 gdb_assert (objfile
->sections
!= NULL
);
388 = maint_obj_section_from_bfd_section (abfd
, asect
, objfile
);
390 if (osect
->the_bfd_section
== NULL
)
391 print_bfd_section_info (abfd
, asect
, print_data
);
393 print_objfile_section_info (abfd
, osect
, print_data
);
396 /* Implement the "maintenance info sections" command. */
399 maintenance_info_sections (const char *arg
, int from_tty
)
405 printf_filtered (_("Exec file:\n"));
406 printf_filtered (" `%s', ", bfd_get_filename (exec_bfd
));
408 printf_filtered (_("file type %s.\n"), bfd_get_target (exec_bfd
));
410 /* Only this function cares about the 'ALLOBJ' argument;
411 if 'ALLOBJ' is the only argument, discard it rather than
412 passing it down to print_objfile_section_info (which
413 wouldn't know how to handle it). */
414 if (arg
&& strcmp (arg
, "ALLOBJ") == 0)
420 for (objfile
*ofile
: current_program_space
->objfiles ())
423 printf_filtered (_(" Object file: %s\n"),
424 bfd_get_filename (ofile
->obfd
));
425 else if (ofile
->obfd
!= exec_bfd
)
428 maint_print_section_data
print_data (ofile
, arg
, ofile
->obfd
);
430 for (asection
*sect
: gdb_bfd_sections (ofile
->obfd
))
431 print_bfd_section_info_maybe_relocated (ofile
->obfd
, sect
,
438 maint_print_section_data
print_data (nullptr, arg
, core_bfd
);
440 printf_filtered (_("Core file:\n"));
441 printf_filtered (" `%s', ", bfd_get_filename (core_bfd
));
443 printf_filtered (_("file type %s.\n"), bfd_get_target (core_bfd
));
445 for (asection
*sect
: gdb_bfd_sections (core_bfd
))
446 print_bfd_section_info (core_bfd
, sect
, print_data
);
451 maintenance_print_statistics (const char *args
, int from_tty
)
453 print_objfile_statistics ();
454 print_symbol_bcache_statistics ();
458 maintenance_print_architecture (const char *args
, int from_tty
)
460 struct gdbarch
*gdbarch
= get_current_arch ();
463 gdbarch_dump (gdbarch
, gdb_stdout
);
468 if (!file
.open (args
, "w"))
469 perror_with_name (_("maintenance print architecture"));
470 gdbarch_dump (gdbarch
, &file
);
474 /* The "maintenance translate-address" command converts a section and address
475 to a symbol. This can be called in two ways:
476 maintenance translate-address <secname> <addr>
477 or maintenance translate-address <addr>. */
480 maintenance_translate_address (const char *arg
, int from_tty
)
483 struct obj_section
*sect
;
485 struct bound_minimal_symbol sym
;
487 if (arg
== NULL
|| *arg
== 0)
488 error (_("requires argument (address or section + address)"));
494 { /* See if we have a valid section name. */
495 while (*p
&& !isspace (*p
)) /* Find end of section name. */
497 if (*p
== '\000') /* End of command? */
498 error (_("Need to specify section name and address"));
500 int arg_len
= p
- arg
;
501 p
= skip_spaces (p
+ 1);
503 for (objfile
*objfile
: current_program_space
->objfiles ())
504 ALL_OBJFILE_OSECTIONS (objfile
, sect
)
506 if (strncmp (sect
->the_bfd_section
->name
, arg
, arg_len
) == 0)
510 error (_("Unknown section %s."), arg
);
514 address
= parse_and_eval_address (p
);
517 sym
= lookup_minimal_symbol_by_pc_section (address
, sect
);
519 sym
= lookup_minimal_symbol_by_pc (address
);
523 const char *symbol_name
= sym
.minsym
->print_name ();
524 const char *symbol_offset
525 = pulongest (address
- BMSYMBOL_VALUE_ADDRESS (sym
));
527 sect
= MSYMBOL_OBJ_SECTION(sym
.objfile
, sym
.minsym
);
530 const char *section_name
;
531 const char *obj_name
;
533 gdb_assert (sect
->the_bfd_section
&& sect
->the_bfd_section
->name
);
534 section_name
= sect
->the_bfd_section
->name
;
536 gdb_assert (sect
->objfile
&& objfile_name (sect
->objfile
));
537 obj_name
= objfile_name (sect
->objfile
);
539 if (current_program_space
->multi_objfile_p ())
540 printf_filtered (_("%s + %s in section %s of %s\n"),
541 symbol_name
, symbol_offset
,
542 section_name
, obj_name
);
544 printf_filtered (_("%s + %s in section %s\n"),
545 symbol_name
, symbol_offset
, section_name
);
548 printf_filtered (_("%s + %s\n"), symbol_name
, symbol_offset
);
551 printf_filtered (_("no symbol at %s:%s\n"),
552 sect
->the_bfd_section
->name
, hex_string (address
));
554 printf_filtered (_("no symbol at %s\n"), hex_string (address
));
560 /* When a command is deprecated the user will be warned the first time
561 the command is used. If possible, a replacement will be
565 maintenance_deprecate (const char *args
, int from_tty
)
567 if (args
== NULL
|| *args
== '\0')
569 printf_unfiltered (_("\"maintenance deprecate\" takes an argument,\n\
570 the command you want to deprecate, and optionally the replacement command\n\
571 enclosed in quotes.\n"));
574 maintenance_do_deprecate (args
, 1);
579 maintenance_undeprecate (const char *args
, int from_tty
)
581 if (args
== NULL
|| *args
== '\0')
583 printf_unfiltered (_("\"maintenance undeprecate\" takes an argument, \n\
584 the command you want to undeprecate.\n"));
587 maintenance_do_deprecate (args
, 0);
590 /* You really shouldn't be using this. It is just for the testsuite.
591 Rather, you should use deprecate_cmd() when the command is created
592 in _initialize_blah().
594 This function deprecates a command and optionally assigns it a
598 maintenance_do_deprecate (const char *text
, int deprecate
)
600 struct cmd_list_element
*alias
= NULL
;
601 struct cmd_list_element
*prefix_cmd
= NULL
;
602 struct cmd_list_element
*cmd
= NULL
;
604 const char *start_ptr
= NULL
;
605 const char *end_ptr
= NULL
;
607 char *replacement
= NULL
;
612 if (!lookup_cmd_composition (text
, &alias
, &prefix_cmd
, &cmd
))
614 printf_filtered (_("Can't find command '%s' to deprecate.\n"), text
);
620 /* Look for a replacement command. */
621 start_ptr
= strchr (text
, '\"');
622 if (start_ptr
!= NULL
)
625 end_ptr
= strrchr (start_ptr
, '\"');
628 len
= end_ptr
- start_ptr
;
629 replacement
= savestring (start_ptr
, len
);
634 if (!start_ptr
|| !end_ptr
)
638 /* If they used an alias, we only want to deprecate the alias.
640 Note the MALLOCED_REPLACEMENT test. If the command's replacement
641 string was allocated at compile time we don't want to free the
645 if (alias
->malloced_replacement
)
646 xfree ((char *) alias
->replacement
);
650 alias
->deprecated_warn_user
= 1;
651 alias
->cmd_deprecated
= 1;
655 alias
->deprecated_warn_user
= 0;
656 alias
->cmd_deprecated
= 0;
658 alias
->replacement
= replacement
;
659 alias
->malloced_replacement
= 1;
664 if (cmd
->malloced_replacement
)
665 xfree ((char *) cmd
->replacement
);
669 cmd
->deprecated_warn_user
= 1;
670 cmd
->cmd_deprecated
= 1;
674 cmd
->deprecated_warn_user
= 0;
675 cmd
->cmd_deprecated
= 0;
677 cmd
->replacement
= replacement
;
678 cmd
->malloced_replacement
= 1;
684 /* Maintenance set/show framework. */
686 struct cmd_list_element
*maintenance_set_cmdlist
;
687 struct cmd_list_element
*maintenance_show_cmdlist
;
689 /* "maintenance with" command. */
692 maintenance_with_cmd (const char *args
, int from_tty
)
694 with_command_1 ("maintenance set ", maintenance_set_cmdlist
, args
, from_tty
);
697 /* "maintenance with" command completer. */
700 maintenance_with_cmd_completer (struct cmd_list_element
*ignore
,
701 completion_tracker
&tracker
,
702 const char *text
, const char * /*word*/)
704 with_command_completer_1 ("maintenance set ", tracker
, text
);
707 /* Profiling support. */
709 static bool maintenance_profile_p
;
711 show_maintenance_profile_p (struct ui_file
*file
, int from_tty
,
712 struct cmd_list_element
*c
, const char *value
)
714 fprintf_filtered (file
, _("Internal profiling is %s.\n"), value
);
719 #define TEXTEND &_etext
720 #elif defined (HAVE_ETEXT)
722 #define TEXTEND &etext
725 #if defined (HAVE_MONSTARTUP) && defined (HAVE__MCLEANUP) && defined (TEXTEND)
727 static int profiling_state
;
729 EXTERN_C
void _mcleanup (void);
732 mcleanup_wrapper (void)
738 EXTERN_C
void monstartup (unsigned long, unsigned long);
742 maintenance_set_profile_cmd (const char *args
, int from_tty
,
743 struct cmd_list_element
*c
)
745 if (maintenance_profile_p
== profiling_state
)
748 profiling_state
= maintenance_profile_p
;
750 if (maintenance_profile_p
)
752 static int profiling_initialized
;
754 if (!profiling_initialized
)
756 atexit (mcleanup_wrapper
);
757 profiling_initialized
= 1;
760 /* "main" is now always the first function in the text segment, so use
761 its address for monstartup. */
762 monstartup ((unsigned long) &main
, (unsigned long) TEXTEND
);
766 extern void _mcleanup (void);
773 maintenance_set_profile_cmd (const char *args
, int from_tty
,
774 struct cmd_list_element
*c
)
776 error (_("Profiling support is not available on this system."));
780 static int n_worker_threads
= -1;
782 /* Update the thread pool for the desired number of threads. */
784 update_thread_pool_size ()
787 int n_threads
= n_worker_threads
;
790 n_threads
= std::thread::hardware_concurrency ();
792 gdb::thread_pool::g_thread_pool
->set_thread_count (n_threads
);
797 maintenance_set_worker_threads (const char *args
, int from_tty
,
798 struct cmd_list_element
*c
)
800 update_thread_pool_size ();
804 /* If true, display time usage both at startup and for each command. */
806 static bool per_command_time
;
808 /* If true, display space usage both at startup and for each command. */
810 static bool per_command_space
;
812 /* If true, display basic symtab stats for each command. */
814 static bool per_command_symtab
;
816 /* mt per-command commands. */
818 static struct cmd_list_element
*per_command_setlist
;
819 static struct cmd_list_element
*per_command_showlist
;
821 /* Set whether to display time statistics to NEW_VALUE
822 (non-zero means true). */
825 set_per_command_time (int new_value
)
827 per_command_time
= new_value
;
830 /* Set whether to display space statistics to NEW_VALUE
831 (non-zero means true). */
834 set_per_command_space (int new_value
)
836 per_command_space
= new_value
;
839 /* Count the number of symtabs and blocks. */
842 count_symtabs_and_blocks (int *nr_symtabs_ptr
, int *nr_compunit_symtabs_ptr
,
846 int nr_compunit_symtabs
= 0;
849 /* When collecting statistics during startup, this is called before
850 pretty much anything in gdb has been initialized, and thus
851 current_program_space may be NULL. */
852 if (current_program_space
!= NULL
)
854 for (objfile
*o
: current_program_space
->objfiles ())
856 for (compunit_symtab
*cu
: o
->compunits ())
858 ++nr_compunit_symtabs
;
859 nr_blocks
+= BLOCKVECTOR_NBLOCKS (COMPUNIT_BLOCKVECTOR (cu
));
860 nr_symtabs
+= std::distance (compunit_filetabs (cu
).begin (),
861 compunit_filetabs (cu
).end ());
866 *nr_symtabs_ptr
= nr_symtabs
;
867 *nr_compunit_symtabs_ptr
= nr_compunit_symtabs
;
868 *nr_blocks_ptr
= nr_blocks
;
871 /* As indicated by display_time and display_space, report GDB's
872 elapsed time and space usage from the base time and space recorded
875 scoped_command_stats::~scoped_command_stats ()
877 /* Early exit if we're not reporting any stats. It can be expensive to
878 compute the pre-command values so don't collect them at all if we're
879 not reporting stats. Alas this doesn't work in the startup case because
880 we don't know yet whether we will be reporting the stats. For the
881 startup case collect the data anyway (it should be cheap at this point),
882 and leave it to the reporter to decide whether to print them. */
885 && !per_command_space
886 && !per_command_symtab
)
889 if (m_time_enabled
&& per_command_time
)
891 print_time (_("command finished"));
893 using namespace std::chrono
;
895 run_time_clock::duration cmd_time
896 = run_time_clock::now () - m_start_cpu_time
;
898 steady_clock::duration wall_time
899 = steady_clock::now () - m_start_wall_time
;
900 /* Subtract time spend in prompt_for_continue from walltime. */
901 wall_time
-= get_prompt_for_continue_wait_time ();
903 printf_unfiltered (!m_msg_type
904 ? _("Startup time: %.6f (cpu), %.6f (wall)\n")
905 : _("Command execution time: %.6f (cpu), %.6f (wall)\n"),
906 duration
<double> (cmd_time
).count (),
907 duration
<double> (wall_time
).count ());
910 if (m_space_enabled
&& per_command_space
)
912 #ifdef HAVE_USEFUL_SBRK
913 char *lim
= (char *) sbrk (0);
915 long space_now
= lim
- lim_at_start
;
916 long space_diff
= space_now
- m_start_space
;
918 printf_unfiltered (!m_msg_type
919 ? _("Space used: %ld (%s%ld during startup)\n")
920 : _("Space used: %ld (%s%ld for this command)\n"),
922 (space_diff
>= 0 ? "+" : ""),
927 if (m_symtab_enabled
&& per_command_symtab
)
929 int nr_symtabs
, nr_compunit_symtabs
, nr_blocks
;
931 count_symtabs_and_blocks (&nr_symtabs
, &nr_compunit_symtabs
, &nr_blocks
);
932 printf_unfiltered (_("#symtabs: %d (+%d),"
933 " #compunits: %d (+%d),"
934 " #blocks: %d (+%d)\n"),
936 nr_symtabs
- m_start_nr_symtabs
,
939 - m_start_nr_compunit_symtabs
),
941 nr_blocks
- m_start_nr_blocks
);
945 scoped_command_stats::scoped_command_stats (bool msg_type
)
946 : m_msg_type (msg_type
)
948 if (!m_msg_type
|| per_command_space
)
950 #ifdef HAVE_USEFUL_SBRK
951 char *lim
= (char *) sbrk (0);
952 m_start_space
= lim
- lim_at_start
;
959 if (msg_type
== 0 || per_command_time
)
961 using namespace std::chrono
;
963 m_start_cpu_time
= run_time_clock::now ();
964 m_start_wall_time
= steady_clock::now ();
967 if (per_command_time
)
968 print_time (_("command started"));
973 if (msg_type
== 0 || per_command_symtab
)
975 int nr_symtabs
, nr_compunit_symtabs
, nr_blocks
;
977 count_symtabs_and_blocks (&nr_symtabs
, &nr_compunit_symtabs
, &nr_blocks
);
978 m_start_nr_symtabs
= nr_symtabs
;
979 m_start_nr_compunit_symtabs
= nr_compunit_symtabs
;
980 m_start_nr_blocks
= nr_blocks
;
981 m_symtab_enabled
= 1;
984 m_symtab_enabled
= 0;
986 /* Initialize timer to keep track of how long we waited for the user. */
987 reset_prompt_for_continue_wait_time ();
993 scoped_command_stats::print_time (const char *msg
)
995 using namespace std::chrono
;
997 auto now
= system_clock::now ();
998 auto ticks
= now
.time_since_epoch ().count () / (1000 * 1000);
999 auto millis
= ticks
% 1000;
1001 std::time_t as_time
= system_clock::to_time_t (now
);
1003 localtime_r (&as_time
, &tm
);
1006 strftime (out
, sizeof (out
), "%F %H:%M:%S", &tm
);
1008 printf_unfiltered ("%s.%03d - %s\n", out
, (int) millis
, msg
);
1011 /* Handle unknown "mt set per-command" arguments.
1012 In this case have "mt set per-command on|off" affect every setting. */
1015 set_per_command_cmd (const char *args
, int from_tty
)
1017 struct cmd_list_element
*list
;
1020 val
= parse_cli_boolean_value (args
);
1022 error (_("Bad value for 'mt set per-command no'."));
1024 for (list
= per_command_setlist
; list
!= NULL
; list
= list
->next
)
1025 if (list
->var_type
== var_boolean
)
1027 gdb_assert (list
->type
== set_cmd
);
1028 do_set_command (args
, from_tty
, list
);
1034 /* The "maintenance selftest" command. */
1037 maintenance_selftest (const char *args
, int from_tty
)
1040 gdb_argv
argv (args
);
1041 selftests::run_tests (argv
.as_array_view ());
1043 printf_filtered (_("\
1044 Selftests have been disabled for this build.\n"));
1049 maintenance_info_selftests (const char *arg
, int from_tty
)
1052 printf_filtered ("Registered selftests:\n");
1053 selftests::for_each_selftest ([] (const std::string
&name
) {
1054 printf_filtered (" - %s\n", name
.c_str ());
1057 printf_filtered (_("\
1058 Selftests have been disabled for this build.\n"));
1063 void _initialize_maint_cmds ();
1065 _initialize_maint_cmds ()
1067 struct cmd_list_element
*cmd
;
1069 add_basic_prefix_cmd ("maintenance", class_maintenance
, _("\
1070 Commands for use by GDB maintainers.\n\
1071 Includes commands to dump specific internal GDB structures in\n\
1072 a human readable form, to cause GDB to deliberately dump core, etc."),
1073 &maintenancelist
, "maintenance ", 0,
1076 add_com_alias ("mt", "maintenance", class_maintenance
, 1);
1078 add_basic_prefix_cmd ("info", class_maintenance
, _("\
1079 Commands for showing internal info about the program being debugged."),
1080 &maintenanceinfolist
, "maintenance info ", 0,
1082 add_alias_cmd ("i", "info", class_maintenance
, 1, &maintenancelist
);
1084 add_cmd ("sections", class_maintenance
, maintenance_info_sections
, _("\
1085 List the BFD sections of the exec and core files.\n\
1086 Arguments may be any combination of:\n\
1087 [one or more section names]\n\
1088 ALLOC LOAD RELOC READONLY CODE DATA ROM CONSTRUCTOR\n\
1089 HAS_CONTENTS NEVER_LOAD COFF_SHARED_LIBRARY IS_COMMON\n\
1090 Sections matching any argument will be listed (no argument\n\
1091 implies all sections). In addition, the special argument\n\
1093 lists all sections from all object files, including shared libraries."),
1094 &maintenanceinfolist
);
1096 add_basic_prefix_cmd ("print", class_maintenance
,
1097 _("Maintenance command for printing GDB internal state."),
1098 &maintenanceprintlist
, "maintenance print ", 0,
1101 add_basic_prefix_cmd ("set", class_maintenance
, _("\
1102 Set GDB internal variables used by the GDB maintainer.\n\
1103 Configure variables internal to GDB that aid in GDB's maintenance"),
1104 &maintenance_set_cmdlist
, "maintenance set ",
1108 add_show_prefix_cmd ("show", class_maintenance
, _("\
1109 Show GDB internal variables used by the GDB maintainer.\n\
1110 Configure variables internal to GDB that aid in GDB's maintenance"),
1111 &maintenance_show_cmdlist
, "maintenance show ",
1115 cmd
= add_cmd ("with", class_maintenance
, maintenance_with_cmd
, _("\
1116 Like \"with\", but works with \"maintenance set\" variables.\n\
1117 Usage: maintenance with SETTING [VALUE] [-- COMMAND]\n\
1118 With no COMMAND, repeats the last executed command.\n\
1119 SETTING is any setting you can change with the \"maintenance set\"\n\
1122 set_cmd_completer_handle_brkchars (cmd
, maintenance_with_cmd_completer
);
1125 add_cmd ("dump-me", class_maintenance
, maintenance_dump_me
, _("\
1126 Get fatal error; make debugger dump its core.\n\
1127 GDB sets its handling of SIGQUIT back to SIG_DFL and then sends\n\
1128 itself a SIGQUIT signal."),
1132 add_cmd ("internal-error", class_maintenance
,
1133 maintenance_internal_error
, _("\
1134 Give GDB an internal error.\n\
1135 Cause GDB to behave as if an internal error was detected."),
1138 add_cmd ("internal-warning", class_maintenance
,
1139 maintenance_internal_warning
, _("\
1140 Give GDB an internal warning.\n\
1141 Cause GDB to behave as if an internal warning was reported."),
1144 add_cmd ("demangler-warning", class_maintenance
,
1145 maintenance_demangler_warning
, _("\
1146 Give GDB a demangler warning.\n\
1147 Cause GDB to behave as if a demangler warning was reported."),
1150 cmd
= add_cmd ("demangle", class_maintenance
, maintenance_demangle
, _("\
1151 This command has been moved to \"demangle\"."),
1153 deprecate_cmd (cmd
, "demangle");
1155 add_prefix_cmd ("per-command", class_maintenance
, set_per_command_cmd
, _("\
1156 Per-command statistics settings."),
1157 &per_command_setlist
, "maintenance set per-command ",
1158 1/*allow-unknown*/, &maintenance_set_cmdlist
);
1160 add_show_prefix_cmd ("per-command", class_maintenance
, _("\
1161 Show per-command statistics settings."),
1162 &per_command_showlist
, "maintenance show per-command ",
1163 0/*allow-unknown*/, &maintenance_show_cmdlist
);
1165 add_setshow_boolean_cmd ("time", class_maintenance
,
1166 &per_command_time
, _("\
1167 Set whether to display per-command execution time."), _("\
1168 Show whether to display per-command execution time."),
1170 If enabled, the execution time for each command will be\n\
1171 displayed following the command's output."),
1173 &per_command_setlist
, &per_command_showlist
);
1175 add_setshow_boolean_cmd ("space", class_maintenance
,
1176 &per_command_space
, _("\
1177 Set whether to display per-command space usage."), _("\
1178 Show whether to display per-command space usage."),
1180 If enabled, the space usage for each command will be\n\
1181 displayed following the command's output."),
1183 &per_command_setlist
, &per_command_showlist
);
1185 add_setshow_boolean_cmd ("symtab", class_maintenance
,
1186 &per_command_symtab
, _("\
1187 Set whether to display per-command symtab statistics."), _("\
1188 Show whether to display per-command symtab statistics."),
1190 If enabled, the basic symtab statistics for each command will be\n\
1191 displayed following the command's output."),
1193 &per_command_setlist
, &per_command_showlist
);
1195 /* This is equivalent to "mt set per-command time on".
1196 Kept because some people are used to typing "mt time 1". */
1197 add_cmd ("time", class_maintenance
, maintenance_time_display
, _("\
1198 Set the display of time usage.\n\
1199 If nonzero, will cause the execution time for each command to be\n\
1200 displayed, following the command's output."),
1203 /* This is equivalent to "mt set per-command space on".
1204 Kept because some people are used to typing "mt space 1". */
1205 add_cmd ("space", class_maintenance
, maintenance_space_display
, _("\
1206 Set the display of space usage.\n\
1207 If nonzero, will cause the execution space for each command to be\n\
1208 displayed, following the command's output."),
1211 add_cmd ("type", class_maintenance
, maintenance_print_type
, _("\
1212 Print a type chain for a given symbol.\n\
1213 For each node in a type chain, print the raw data for each member of\n\
1214 the type structure, and the interpretation of the data."),
1215 &maintenanceprintlist
);
1217 add_cmd ("statistics", class_maintenance
, maintenance_print_statistics
,
1218 _("Print statistics about internal gdb state."),
1219 &maintenanceprintlist
);
1221 add_cmd ("architecture", class_maintenance
,
1222 maintenance_print_architecture
, _("\
1223 Print the internal architecture configuration.\n\
1224 Takes an optional file parameter."),
1225 &maintenanceprintlist
);
1227 add_basic_prefix_cmd ("check", class_maintenance
, _("\
1228 Commands for checking internal gdb state."),
1229 &maintenancechecklist
, "maintenance check ", 0,
1232 add_cmd ("translate-address", class_maintenance
,
1233 maintenance_translate_address
,
1234 _("Translate a section name and address to a symbol."),
1237 add_cmd ("deprecate", class_maintenance
, maintenance_deprecate
, _("\
1238 Deprecate a command (for testing purposes).\n\
1239 Usage: maintenance deprecate COMMANDNAME [\"REPLACEMENT\"]\n\
1240 This is used by the testsuite to check the command deprecator.\n\
1241 You probably shouldn't use this,\n\
1242 rather you should use the C function deprecate_cmd()."), &maintenancelist
);
1244 add_cmd ("undeprecate", class_maintenance
, maintenance_undeprecate
, _("\
1245 Undeprecate a command (for testing purposes).\n\
1246 Usage: maintenance undeprecate COMMANDNAME\n\
1247 This is used by the testsuite to check the command deprecator.\n\
1248 You probably shouldn't use this."),
1251 add_cmd ("selftest", class_maintenance
, maintenance_selftest
, _("\
1252 Run gdb's unit tests.\n\
1253 Usage: maintenance selftest [FILTER]\n\
1254 This will run any unit tests that were built in to gdb.\n\
1255 If a filter is given, only the tests with that value in their name will ran."),
1258 add_cmd ("selftests", class_maintenance
, maintenance_info_selftests
,
1259 _("List the registered selftests."), &maintenanceinfolist
);
1261 add_setshow_boolean_cmd ("profile", class_maintenance
,
1262 &maintenance_profile_p
, _("\
1263 Set internal profiling."), _("\
1264 Show internal profiling."), _("\
1265 When enabled GDB is profiled."),
1266 maintenance_set_profile_cmd
,
1267 show_maintenance_profile_p
,
1268 &maintenance_set_cmdlist
,
1269 &maintenance_show_cmdlist
);
1271 add_setshow_zuinteger_unlimited_cmd ("worker-threads",
1273 &n_worker_threads
, _("\
1274 Set the number of worker threads GDB can use."), _("\
1275 Show the number of worker threads GDB can use."), _("\
1276 GDB may use multiple threads to speed up certain CPU-intensive operations,\n\
1277 such as demangling symbol names."),
1278 maintenance_set_worker_threads
, NULL
,
1279 &maintenance_set_cmdlist
,
1280 &maintenance_show_cmdlist
);
1282 update_thread_pool_size ();