gdb/fortran: support ALLOCATED builtin
[binutils-gdb.git] / gdb / maint.c
1 /* Support for GDB maintenance commands.
2
3 Copyright (C) 1992-2021 Free Software Foundation, Inc.
4
5 Written by Fred Fish at Cygnus Support.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
22
23 #include "defs.h"
24 #include "arch-utils.h"
25 #include <ctype.h>
26 #include <cmath>
27 #include <signal.h>
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "symtab.h"
31 #include "block.h"
32 #include "gdbtypes.h"
33 #include "demangle.h"
34 #include "gdbcore.h"
35 #include "expression.h" /* For language.h */
36 #include "language.h"
37 #include "symfile.h"
38 #include "objfiles.h"
39 #include "value.h"
40 #include "top.h"
41 #include "maint.h"
42 #include "gdbsupport/selftest.h"
43
44 #include "cli/cli-decode.h"
45 #include "cli/cli-utils.h"
46 #include "cli/cli-setshow.h"
47 #include "cli/cli-cmds.h"
48
49 #if CXX_STD_THREAD
50 #include "gdbsupport/thread-pool.h"
51 #endif
52
53 static void maintenance_do_deprecate (const char *, int);
54
55 #ifndef _WIN32
56 static void
57 maintenance_dump_me (const char *args, int from_tty)
58 {
59 if (query (_("Should GDB dump core? ")))
60 {
61 #ifdef __DJGPP__
62 /* SIGQUIT by default is ignored, so use SIGABRT instead. */
63 signal (SIGABRT, SIG_DFL);
64 kill (getpid (), SIGABRT);
65 #else
66 signal (SIGQUIT, SIG_DFL);
67 kill (getpid (), SIGQUIT);
68 #endif
69 }
70 }
71 #endif
72
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
76 GDB. */
77
78 static void
79 maintenance_internal_error (const char *args, int from_tty)
80 {
81 internal_error (__FILE__, __LINE__, "%s", (args == NULL ? "" : args));
82 }
83
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
87 GDB. */
88
89 static void
90 maintenance_internal_warning (const char *args, int from_tty)
91 {
92 internal_warning (__FILE__, __LINE__, "%s", (args == NULL ? "" : args));
93 }
94
95 /* Stimulate the internal error mechanism that GDB uses when an
96 demangler problem is detected. Allows testing of the mechanism. */
97
98 static void
99 maintenance_demangler_warning (const char *args, int from_tty)
100 {
101 demangler_warning (__FILE__, __LINE__, "%s", (args == NULL ? "" : args));
102 }
103
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. */
107
108 static void
109 maintenance_demangle (const char *args, int from_tty)
110 {
111 printf_filtered (_("This command has been moved to \"demangle\".\n"));
112 }
113
114 static void
115 maintenance_time_display (const char *args, int from_tty)
116 {
117 if (args == NULL || *args == '\0')
118 printf_unfiltered (_("\"maintenance time\" takes a numeric argument.\n"));
119 else
120 set_per_command_time (strtol (args, NULL, 10));
121 }
122
123 static void
124 maintenance_space_display (const char *args, int from_tty)
125 {
126 if (args == NULL || *args == '\0')
127 printf_unfiltered ("\"maintenance space\" takes a numeric argument.\n");
128 else
129 set_per_command_space (strtol (args, NULL, 10));
130 }
131
132 /* Mini tokenizing lexer for 'maint info sections' command. */
133
134 static bool
135 match_substring (const char *string, const char *substr)
136 {
137 int substr_len = strlen (substr);
138 const char *tok;
139
140 while ((tok = strstr (string, substr)) != NULL)
141 {
142 /* Got a partial match. Is it a whole word? */
143 if (tok == string
144 || tok[-1] == ' '
145 || tok[-1] == '\t')
146 {
147 /* Token is delimited at the front... */
148 if (tok[substr_len] == ' '
149 || tok[substr_len] == '\t'
150 || tok[substr_len] == '\0')
151 {
152 /* Token is delimited at the rear. Got a whole-word match. */
153 return true;
154 }
155 }
156 /* Token didn't match as a whole word. Advance and try again. */
157 string = tok + 1;
158 }
159 return false;
160 }
161
162 /* Structure holding information about a single bfd section flag. This is
163 used by the "maintenance info sections" command to print the sections,
164 and for filtering which sections are printed. */
165
166 struct single_bfd_flag_info
167 {
168 /* The name of the section. This is what is printed for the flag, and
169 what the user enter in order to filter by flag. */
170 const char *name;
171
172 /* The bfd defined SEC_* flagword value for this flag. */
173 flagword value;
174 };
175
176 /* Vector of all the known bfd flags. */
177
178 static const single_bfd_flag_info bfd_flag_info[] =
179 {
180 { "ALLOC", SEC_ALLOC },
181 { "LOAD", SEC_LOAD },
182 { "RELOC", SEC_RELOC },
183 { "READONLY", SEC_READONLY },
184 { "CODE", SEC_CODE },
185 { "DATA", SEC_DATA },
186 { "ROM", SEC_ROM },
187 { "CONSTRUCTOR", SEC_CONSTRUCTOR },
188 { "HAS_CONTENTS", SEC_HAS_CONTENTS },
189 { "NEVER_LOAD", SEC_NEVER_LOAD },
190 { "COFF_SHARED_LIBRARY", SEC_COFF_SHARED_LIBRARY },
191 { "IS_COMMON", SEC_IS_COMMON }
192 };
193
194 /* For each flag in the global BFD_FLAG_INFO list, if FLAGS has a flag's
195 flagword value set, and STRING contains the flag's name then return
196 true, otherwise return false. STRING is never nullptr. */
197
198 static bool
199 match_bfd_flags (const char *string, flagword flags)
200 {
201 gdb_assert (string != nullptr);
202
203 for (const auto &f : bfd_flag_info)
204 {
205 if (flags & f.value
206 && match_substring (string, f.name))
207 return true;
208 }
209
210 return false;
211 }
212
213 /* Print the names of all flags set in FLAGS. The names are taken from the
214 BFD_FLAG_INFO global. */
215
216 static void
217 print_bfd_flags (flagword flags)
218 {
219 for (const auto &f : bfd_flag_info)
220 {
221 if (flags & f.value)
222 printf_filtered (" %s", f.name);
223 }
224 }
225
226 static void
227 maint_print_section_info (const char *name, flagword flags,
228 CORE_ADDR addr, CORE_ADDR endaddr,
229 unsigned long filepos, int addr_size)
230 {
231 printf_filtered (" %s", hex_string_custom (addr, addr_size));
232 printf_filtered ("->%s", hex_string_custom (endaddr, addr_size));
233 printf_filtered (" at %s",
234 hex_string_custom ((unsigned long) filepos, 8));
235 printf_filtered (": %s", name);
236 print_bfd_flags (flags);
237 printf_filtered ("\n");
238 }
239
240 /* Return the number of digits required to display COUNT in decimal.
241
242 Used when pretty printing index numbers to ensure all of the indexes line
243 up.*/
244
245 static int
246 index_digits (int count)
247 {
248 return ((int) log10 ((float) count)) + 1;
249 }
250
251 /* Helper function to pretty-print the section index of ASECT from ABFD.
252 The INDEX_DIGITS is the number of digits in the largest index that will
253 be printed, and is used to pretty-print the resulting string. */
254
255 static void
256 print_section_index (bfd *abfd,
257 asection *asect,
258 int index_digits)
259 {
260 std::string result
261 = string_printf (" [%d] ", gdb_bfd_section_index (abfd, asect));
262 /* The '+ 4' for the leading and trailing characters. */
263 printf_filtered ("%-*s", (index_digits + 4), result.c_str ());
264 }
265
266 /* Print information about ASECT from ABFD. The section will be printed using
267 the VMA's from the bfd, which will not be the relocated addresses for bfds
268 that should be relocated. The information must be printed with the same
269 layout as PRINT_OBJFILE_SECTION_INFO below.
270
271 ARG is the argument string passed by the user to the top level maintenance
272 info sections command. Used for filtering which sections are printed. */
273
274 static void
275 print_bfd_section_info (bfd *abfd, asection *asect, const char *arg,
276 int index_digits)
277 {
278 flagword flags = bfd_section_flags (asect);
279 const char *name = bfd_section_name (asect);
280
281 if (arg == NULL || *arg == '\0'
282 || match_substring (arg, name)
283 || match_bfd_flags (arg, flags))
284 {
285 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
286 int addr_size = gdbarch_addr_bit (gdbarch) / 8;
287 CORE_ADDR addr, endaddr;
288
289 addr = bfd_section_vma (asect);
290 endaddr = addr + bfd_section_size (asect);
291 print_section_index (abfd, asect, index_digits);
292 maint_print_section_info (name, flags, addr, endaddr,
293 asect->filepos, addr_size);
294 }
295 }
296
297 /* Print information about ASECT which is GDB's wrapper around a section
298 from ABFD. The information must be printed with the same layout as
299 PRINT_BFD_SECTION_INFO above. PRINT_DATA holds information used to
300 filter which sections are printed, and for formatting the output.
301
302 ARG is the argument string passed by the user to the top level maintenance
303 info sections command. Used for filtering which sections are printed. */
304
305 static void
306 print_objfile_section_info (bfd *abfd, struct obj_section *asect,
307 const char *arg, int index_digits)
308 {
309 flagword flags = bfd_section_flags (asect->the_bfd_section);
310 const char *name = bfd_section_name (asect->the_bfd_section);
311
312 if (arg == NULL || *arg == '\0'
313 || match_substring (arg, name)
314 || match_bfd_flags (arg, flags))
315 {
316 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
317 int addr_size = gdbarch_addr_bit (gdbarch) / 8;
318
319 print_section_index (abfd, asect->the_bfd_section, index_digits);
320 maint_print_section_info (name, flags,
321 obj_section_addr (asect),
322 obj_section_endaddr (asect),
323 asect->the_bfd_section->filepos,
324 addr_size);
325 }
326 }
327
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. */
331
332 static obj_section *
333 maint_obj_section_from_bfd_section (bfd *abfd,
334 asection *asection,
335 objfile *ofile)
336 {
337 if (ofile->sections == nullptr)
338 return nullptr;
339
340 obj_section *osect
341 = &ofile->sections[gdb_bfd_section_index (abfd, asection)];
342
343 if (osect >= ofile->sections_end)
344 return nullptr;
345
346 return osect;
347 }
348
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).
354
355 HEADER is a string that describes this file, e.g. 'Exec file: ', or
356 'Core file: '.
357
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. */
361
362 static void
363 maint_print_all_sections (const char *header, bfd *abfd, objfile *objfile,
364 const char *arg)
365 {
366 puts_filtered (header);
367 wrap_here (" ");
368 printf_filtered ("`%s', ", bfd_get_filename (abfd));
369 wrap_here (" ");
370 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
371
372 int section_count = gdb_bfd_count_sections (abfd);
373 int digits = index_digits (section_count);
374
375 for (asection *sect : gdb_bfd_sections (abfd))
376 {
377 obj_section *osect = nullptr;
378
379 if (objfile != nullptr)
380 {
381 gdb_assert (objfile->sections != nullptr);
382 osect
383 = maint_obj_section_from_bfd_section (abfd, sect, objfile);
384 if (osect->the_bfd_section == nullptr)
385 osect = nullptr;
386 }
387
388 if (osect == nullptr)
389 print_bfd_section_info (abfd, sect, arg, digits);
390 else
391 print_objfile_section_info (abfd, osect, arg, digits);
392 }
393 }
394
395 /* The options for the "maintenance info sections" command. */
396
397 struct maint_info_sections_opts
398 {
399 /* For "-all-objects". */
400 bool all_objects = false;
401 };
402
403 static const gdb::option::option_def maint_info_sections_option_defs[] = {
404
405 gdb::option::flag_option_def<maint_info_sections_opts> {
406 "all-objects",
407 [] (maint_info_sections_opts *opts) { return &opts->all_objects; },
408 N_("Display information from all loaded object files."),
409 },
410 };
411
412 /* Create an option_def_group for the "maintenance info sections" options,
413 with CC_OPTS as context. */
414
415 static inline gdb::option::option_def_group
416 make_maint_info_sections_options_def_group (maint_info_sections_opts *cc_opts)
417 {
418 return {{maint_info_sections_option_defs}, cc_opts};
419 }
420
421 /* Completion for the "maintenance info sections" command. */
422
423 static void
424 maint_info_sections_completer (struct cmd_list_element *cmd,
425 completion_tracker &tracker,
426 const char *text, const char * /* word */)
427 {
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))
432 return;
433 const char *word = advance_to_expression_complete_word_point (tracker, text);
434
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);
442 }
443
444 /* Implement the "maintenance info sections" command. */
445
446 static void
447 maintenance_info_sections (const char *arg, int from_tty)
448 {
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);
454
455 for (objfile *ofile : current_program_space->objfiles ())
456 {
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);
461 }
462
463 if (core_bfd)
464 maint_print_all_sections (_("Core file: "), core_bfd, nullptr, arg);
465 }
466
467 static void
468 maintenance_print_statistics (const char *args, int from_tty)
469 {
470 print_objfile_statistics ();
471 print_symbol_bcache_statistics ();
472 }
473
474 static void
475 maintenance_print_architecture (const char *args, int from_tty)
476 {
477 struct gdbarch *gdbarch = get_current_arch ();
478
479 if (args == NULL)
480 gdbarch_dump (gdbarch, gdb_stdout);
481 else
482 {
483 stdio_file file;
484
485 if (!file.open (args, "w"))
486 perror_with_name (_("maintenance print architecture"));
487 gdbarch_dump (gdbarch, &file);
488 }
489 }
490
491 /* The "maintenance translate-address" command converts a section and address
492 to a symbol. This can be called in two ways:
493 maintenance translate-address <secname> <addr>
494 or maintenance translate-address <addr>. */
495
496 static void
497 maintenance_translate_address (const char *arg, int from_tty)
498 {
499 CORE_ADDR address;
500 struct obj_section *sect;
501 const char *p;
502 struct bound_minimal_symbol sym;
503
504 if (arg == NULL || *arg == 0)
505 error (_("requires argument (address or section + address)"));
506
507 sect = NULL;
508 p = arg;
509
510 if (!isdigit (*p))
511 { /* See if we have a valid section name. */
512 while (*p && !isspace (*p)) /* Find end of section name. */
513 p++;
514 if (*p == '\000') /* End of command? */
515 error (_("Need to specify section name and address"));
516
517 int arg_len = p - arg;
518 p = skip_spaces (p + 1);
519
520 for (objfile *objfile : current_program_space->objfiles ())
521 ALL_OBJFILE_OSECTIONS (objfile, sect)
522 {
523 if (strncmp (sect->the_bfd_section->name, arg, arg_len) == 0)
524 goto found;
525 }
526
527 error (_("Unknown section %s."), arg);
528 found: ;
529 }
530
531 address = parse_and_eval_address (p);
532
533 if (sect)
534 sym = lookup_minimal_symbol_by_pc_section (address, sect);
535 else
536 sym = lookup_minimal_symbol_by_pc (address);
537
538 if (sym.minsym)
539 {
540 const char *symbol_name = sym.minsym->print_name ();
541 const char *symbol_offset
542 = pulongest (address - BMSYMBOL_VALUE_ADDRESS (sym));
543
544 sect = sym.obj_section ();
545 if (sect != NULL)
546 {
547 const char *section_name;
548 const char *obj_name;
549
550 gdb_assert (sect->the_bfd_section && sect->the_bfd_section->name);
551 section_name = sect->the_bfd_section->name;
552
553 gdb_assert (sect->objfile && objfile_name (sect->objfile));
554 obj_name = objfile_name (sect->objfile);
555
556 if (current_program_space->multi_objfile_p ())
557 printf_filtered (_("%s + %s in section %s of %s\n"),
558 symbol_name, symbol_offset,
559 section_name, obj_name);
560 else
561 printf_filtered (_("%s + %s in section %s\n"),
562 symbol_name, symbol_offset, section_name);
563 }
564 else
565 printf_filtered (_("%s + %s\n"), symbol_name, symbol_offset);
566 }
567 else if (sect)
568 printf_filtered (_("no symbol at %s:%s\n"),
569 sect->the_bfd_section->name, hex_string (address));
570 else
571 printf_filtered (_("no symbol at %s\n"), hex_string (address));
572
573 return;
574 }
575
576
577 /* When a command is deprecated the user will be warned the first time
578 the command is used. If possible, a replacement will be
579 offered. */
580
581 static void
582 maintenance_deprecate (const char *args, int from_tty)
583 {
584 if (args == NULL || *args == '\0')
585 {
586 printf_unfiltered (_("\"maintenance deprecate\" takes an argument,\n\
587 the command you want to deprecate, and optionally the replacement command\n\
588 enclosed in quotes.\n"));
589 }
590
591 maintenance_do_deprecate (args, 1);
592 }
593
594
595 static void
596 maintenance_undeprecate (const char *args, int from_tty)
597 {
598 if (args == NULL || *args == '\0')
599 {
600 printf_unfiltered (_("\"maintenance undeprecate\" takes an argument, \n\
601 the command you want to undeprecate.\n"));
602 }
603
604 maintenance_do_deprecate (args, 0);
605 }
606
607 /* You really shouldn't be using this. It is just for the testsuite.
608 Rather, you should use deprecate_cmd() when the command is created
609 in _initialize_blah().
610
611 This function deprecates a command and optionally assigns it a
612 replacement. */
613
614 static void
615 maintenance_do_deprecate (const char *text, int deprecate)
616 {
617 struct cmd_list_element *alias = NULL;
618 struct cmd_list_element *prefix_cmd = NULL;
619 struct cmd_list_element *cmd = NULL;
620
621 const char *start_ptr = NULL;
622 const char *end_ptr = NULL;
623 int len;
624 char *replacement = NULL;
625
626 if (text == NULL)
627 return;
628
629 if (!lookup_cmd_composition (text, &alias, &prefix_cmd, &cmd))
630 {
631 printf_filtered (_("Can't find command '%s' to deprecate.\n"), text);
632 return;
633 }
634
635 if (deprecate)
636 {
637 /* Look for a replacement command. */
638 start_ptr = strchr (text, '\"');
639 if (start_ptr != NULL)
640 {
641 start_ptr++;
642 end_ptr = strrchr (start_ptr, '\"');
643 if (end_ptr != NULL)
644 {
645 len = end_ptr - start_ptr;
646 replacement = savestring (start_ptr, len);
647 }
648 }
649 }
650
651 if (!start_ptr || !end_ptr)
652 replacement = NULL;
653
654
655 /* If they used an alias, we only want to deprecate the alias.
656
657 Note the MALLOCED_REPLACEMENT test. If the command's replacement
658 string was allocated at compile time we don't want to free the
659 memory. */
660 if (alias)
661 {
662 if (alias->malloced_replacement)
663 xfree ((char *) alias->replacement);
664
665 if (deprecate)
666 {
667 alias->deprecated_warn_user = 1;
668 alias->cmd_deprecated = 1;
669 }
670 else
671 {
672 alias->deprecated_warn_user = 0;
673 alias->cmd_deprecated = 0;
674 }
675 alias->replacement = replacement;
676 alias->malloced_replacement = 1;
677 return;
678 }
679 else if (cmd)
680 {
681 if (cmd->malloced_replacement)
682 xfree ((char *) cmd->replacement);
683
684 if (deprecate)
685 {
686 cmd->deprecated_warn_user = 1;
687 cmd->cmd_deprecated = 1;
688 }
689 else
690 {
691 cmd->deprecated_warn_user = 0;
692 cmd->cmd_deprecated = 0;
693 }
694 cmd->replacement = replacement;
695 cmd->malloced_replacement = 1;
696 return;
697 }
698 xfree (replacement);
699 }
700
701 /* Maintenance set/show framework. */
702
703 struct cmd_list_element *maintenance_set_cmdlist;
704 struct cmd_list_element *maintenance_show_cmdlist;
705
706 /* "maintenance with" command. */
707
708 static void
709 maintenance_with_cmd (const char *args, int from_tty)
710 {
711 with_command_1 ("maintenance set ", maintenance_set_cmdlist, args, from_tty);
712 }
713
714 /* "maintenance with" command completer. */
715
716 static void
717 maintenance_with_cmd_completer (struct cmd_list_element *ignore,
718 completion_tracker &tracker,
719 const char *text, const char * /*word*/)
720 {
721 with_command_completer_1 ("maintenance set ", tracker, text);
722 }
723
724 /* Profiling support. */
725
726 static bool maintenance_profile_p;
727 static void
728 show_maintenance_profile_p (struct ui_file *file, int from_tty,
729 struct cmd_list_element *c, const char *value)
730 {
731 fprintf_filtered (file, _("Internal profiling is %s.\n"), value);
732 }
733
734 #ifdef HAVE__ETEXT
735 extern char _etext;
736 #define TEXTEND &_etext
737 #elif defined (HAVE_ETEXT)
738 extern char etext;
739 #define TEXTEND &etext
740 #endif
741
742 #if defined (HAVE_MONSTARTUP) && defined (HAVE__MCLEANUP) && defined (TEXTEND)
743
744 static int profiling_state;
745
746 EXTERN_C void _mcleanup (void);
747
748 static void
749 mcleanup_wrapper (void)
750 {
751 if (profiling_state)
752 _mcleanup ();
753 }
754
755 EXTERN_C void monstartup (unsigned long, unsigned long);
756 extern int main ();
757
758 static void
759 maintenance_set_profile_cmd (const char *args, int from_tty,
760 struct cmd_list_element *c)
761 {
762 if (maintenance_profile_p == profiling_state)
763 return;
764
765 profiling_state = maintenance_profile_p;
766
767 if (maintenance_profile_p)
768 {
769 static int profiling_initialized;
770
771 if (!profiling_initialized)
772 {
773 atexit (mcleanup_wrapper);
774 profiling_initialized = 1;
775 }
776
777 /* "main" is now always the first function in the text segment, so use
778 its address for monstartup. */
779 monstartup ((unsigned long) &main, (unsigned long) TEXTEND);
780 }
781 else
782 {
783 extern void _mcleanup (void);
784
785 _mcleanup ();
786 }
787 }
788 #else
789 static void
790 maintenance_set_profile_cmd (const char *args, int from_tty,
791 struct cmd_list_element *c)
792 {
793 error (_("Profiling support is not available on this system."));
794 }
795 #endif
796
797 static int n_worker_threads = -1;
798
799 /* Update the thread pool for the desired number of threads. */
800 static void
801 update_thread_pool_size ()
802 {
803 #if CXX_STD_THREAD
804 int n_threads = n_worker_threads;
805
806 if (n_threads < 0)
807 n_threads = std::thread::hardware_concurrency ();
808
809 gdb::thread_pool::g_thread_pool->set_thread_count (n_threads);
810 #endif
811 }
812
813 static void
814 maintenance_set_worker_threads (const char *args, int from_tty,
815 struct cmd_list_element *c)
816 {
817 update_thread_pool_size ();
818 }
819
820 \f
821 /* If true, display time usage both at startup and for each command. */
822
823 static bool per_command_time;
824
825 /* If true, display space usage both at startup and for each command. */
826
827 static bool per_command_space;
828
829 /* If true, display basic symtab stats for each command. */
830
831 static bool per_command_symtab;
832
833 /* mt per-command commands. */
834
835 static struct cmd_list_element *per_command_setlist;
836 static struct cmd_list_element *per_command_showlist;
837
838 /* Set whether to display time statistics to NEW_VALUE
839 (non-zero means true). */
840
841 void
842 set_per_command_time (int new_value)
843 {
844 per_command_time = new_value;
845 }
846
847 /* Set whether to display space statistics to NEW_VALUE
848 (non-zero means true). */
849
850 void
851 set_per_command_space (int new_value)
852 {
853 per_command_space = new_value;
854 }
855
856 /* Count the number of symtabs and blocks. */
857
858 static void
859 count_symtabs_and_blocks (int *nr_symtabs_ptr, int *nr_compunit_symtabs_ptr,
860 int *nr_blocks_ptr)
861 {
862 int nr_symtabs = 0;
863 int nr_compunit_symtabs = 0;
864 int nr_blocks = 0;
865
866 /* When collecting statistics during startup, this is called before
867 pretty much anything in gdb has been initialized, and thus
868 current_program_space may be NULL. */
869 if (current_program_space != NULL)
870 {
871 for (objfile *o : current_program_space->objfiles ())
872 {
873 for (compunit_symtab *cu : o->compunits ())
874 {
875 ++nr_compunit_symtabs;
876 nr_blocks += BLOCKVECTOR_NBLOCKS (COMPUNIT_BLOCKVECTOR (cu));
877 nr_symtabs += std::distance (compunit_filetabs (cu).begin (),
878 compunit_filetabs (cu).end ());
879 }
880 }
881 }
882
883 *nr_symtabs_ptr = nr_symtabs;
884 *nr_compunit_symtabs_ptr = nr_compunit_symtabs;
885 *nr_blocks_ptr = nr_blocks;
886 }
887
888 /* As indicated by display_time and display_space, report GDB's
889 elapsed time and space usage from the base time and space recorded
890 in this object. */
891
892 scoped_command_stats::~scoped_command_stats ()
893 {
894 /* Early exit if we're not reporting any stats. It can be expensive to
895 compute the pre-command values so don't collect them at all if we're
896 not reporting stats. Alas this doesn't work in the startup case because
897 we don't know yet whether we will be reporting the stats. For the
898 startup case collect the data anyway (it should be cheap at this point),
899 and leave it to the reporter to decide whether to print them. */
900 if (m_msg_type
901 && !per_command_time
902 && !per_command_space
903 && !per_command_symtab)
904 return;
905
906 if (m_time_enabled && per_command_time)
907 {
908 print_time (_("command finished"));
909
910 using namespace std::chrono;
911
912 run_time_clock::duration cmd_time
913 = run_time_clock::now () - m_start_cpu_time;
914
915 steady_clock::duration wall_time
916 = steady_clock::now () - m_start_wall_time;
917 /* Subtract time spend in prompt_for_continue from walltime. */
918 wall_time -= get_prompt_for_continue_wait_time ();
919
920 printf_unfiltered (!m_msg_type
921 ? _("Startup time: %.6f (cpu), %.6f (wall)\n")
922 : _("Command execution time: %.6f (cpu), %.6f (wall)\n"),
923 duration<double> (cmd_time).count (),
924 duration<double> (wall_time).count ());
925 }
926
927 if (m_space_enabled && per_command_space)
928 {
929 #ifdef HAVE_USEFUL_SBRK
930 char *lim = (char *) sbrk (0);
931
932 long space_now = lim - lim_at_start;
933 long space_diff = space_now - m_start_space;
934
935 printf_unfiltered (!m_msg_type
936 ? _("Space used: %ld (%s%ld during startup)\n")
937 : _("Space used: %ld (%s%ld for this command)\n"),
938 space_now,
939 (space_diff >= 0 ? "+" : ""),
940 space_diff);
941 #endif
942 }
943
944 if (m_symtab_enabled && per_command_symtab)
945 {
946 int nr_symtabs, nr_compunit_symtabs, nr_blocks;
947
948 count_symtabs_and_blocks (&nr_symtabs, &nr_compunit_symtabs, &nr_blocks);
949 printf_unfiltered (_("#symtabs: %d (+%d),"
950 " #compunits: %d (+%d),"
951 " #blocks: %d (+%d)\n"),
952 nr_symtabs,
953 nr_symtabs - m_start_nr_symtabs,
954 nr_compunit_symtabs,
955 (nr_compunit_symtabs
956 - m_start_nr_compunit_symtabs),
957 nr_blocks,
958 nr_blocks - m_start_nr_blocks);
959 }
960 }
961
962 scoped_command_stats::scoped_command_stats (bool msg_type)
963 : m_msg_type (msg_type)
964 {
965 if (!m_msg_type || per_command_space)
966 {
967 #ifdef HAVE_USEFUL_SBRK
968 char *lim = (char *) sbrk (0);
969 m_start_space = lim - lim_at_start;
970 m_space_enabled = 1;
971 #endif
972 }
973 else
974 m_space_enabled = 0;
975
976 if (msg_type == 0 || per_command_time)
977 {
978 using namespace std::chrono;
979
980 m_start_cpu_time = run_time_clock::now ();
981 m_start_wall_time = steady_clock::now ();
982 m_time_enabled = 1;
983
984 if (per_command_time)
985 print_time (_("command started"));
986 }
987 else
988 m_time_enabled = 0;
989
990 if (msg_type == 0 || per_command_symtab)
991 {
992 int nr_symtabs, nr_compunit_symtabs, nr_blocks;
993
994 count_symtabs_and_blocks (&nr_symtabs, &nr_compunit_symtabs, &nr_blocks);
995 m_start_nr_symtabs = nr_symtabs;
996 m_start_nr_compunit_symtabs = nr_compunit_symtabs;
997 m_start_nr_blocks = nr_blocks;
998 m_symtab_enabled = 1;
999 }
1000 else
1001 m_symtab_enabled = 0;
1002
1003 /* Initialize timer to keep track of how long we waited for the user. */
1004 reset_prompt_for_continue_wait_time ();
1005 }
1006
1007 /* See maint.h. */
1008
1009 void
1010 scoped_command_stats::print_time (const char *msg)
1011 {
1012 using namespace std::chrono;
1013
1014 auto now = system_clock::now ();
1015 auto ticks = now.time_since_epoch ().count () / (1000 * 1000);
1016 auto millis = ticks % 1000;
1017
1018 std::time_t as_time = system_clock::to_time_t (now);
1019 struct tm tm;
1020 localtime_r (&as_time, &tm);
1021
1022 char out[100];
1023 strftime (out, sizeof (out), "%F %H:%M:%S", &tm);
1024
1025 printf_unfiltered ("%s.%03d - %s\n", out, (int) millis, msg);
1026 }
1027
1028 /* Handle unknown "mt set per-command" arguments.
1029 In this case have "mt set per-command on|off" affect every setting. */
1030
1031 static void
1032 set_per_command_cmd (const char *args, int from_tty)
1033 {
1034 struct cmd_list_element *list;
1035 int val;
1036
1037 val = parse_cli_boolean_value (args);
1038 if (val < 0)
1039 error (_("Bad value for 'mt set per-command no'."));
1040
1041 for (list = per_command_setlist; list != NULL; list = list->next)
1042 if (list->var_type == var_boolean)
1043 {
1044 gdb_assert (list->type == set_cmd);
1045 do_set_command (args, from_tty, list);
1046 }
1047 }
1048
1049 \f
1050
1051 /* The "maintenance selftest" command. */
1052
1053 static void
1054 maintenance_selftest (const char *args, int from_tty)
1055 {
1056 #if GDB_SELF_TEST
1057 gdb_argv argv (args);
1058 selftests::run_tests (argv.as_array_view ());
1059 #else
1060 printf_filtered (_("\
1061 Selftests have been disabled for this build.\n"));
1062 #endif
1063 }
1064
1065 static void
1066 maintenance_info_selftests (const char *arg, int from_tty)
1067 {
1068 #if GDB_SELF_TEST
1069 printf_filtered ("Registered selftests:\n");
1070 selftests::for_each_selftest ([] (const std::string &name) {
1071 printf_filtered (" - %s\n", name.c_str ());
1072 });
1073 #else
1074 printf_filtered (_("\
1075 Selftests have been disabled for this build.\n"));
1076 #endif
1077 }
1078
1079 \f
1080 void _initialize_maint_cmds ();
1081 void
1082 _initialize_maint_cmds ()
1083 {
1084 struct cmd_list_element *cmd;
1085
1086 add_basic_prefix_cmd ("maintenance", class_maintenance, _("\
1087 Commands for use by GDB maintainers.\n\
1088 Includes commands to dump specific internal GDB structures in\n\
1089 a human readable form, to cause GDB to deliberately dump core, etc."),
1090 &maintenancelist, "maintenance ", 0,
1091 &cmdlist);
1092
1093 add_com_alias ("mt", "maintenance", class_maintenance, 1);
1094
1095 add_basic_prefix_cmd ("info", class_maintenance, _("\
1096 Commands for showing internal info about the program being debugged."),
1097 &maintenanceinfolist, "maintenance info ", 0,
1098 &maintenancelist);
1099 add_alias_cmd ("i", "info", class_maintenance, 1, &maintenancelist);
1100
1101 const auto opts = make_maint_info_sections_options_def_group (nullptr);
1102 static std::string maint_info_sections_command_help
1103 = gdb::option::build_help (_("\
1104 List the BFD sections of the exec and core files.\n\
1105 \n\
1106 Usage: maintenance info sections [-all-objects] [FILTERS]\n\
1107 \n\
1108 FILTERS is a list of words, each word is either:\n\
1109 + A section name - any section with this name will be printed, or\n\
1110 + A section flag - any section with this flag will be printed. The\n\
1111 known flags are:\n\
1112 ALLOC LOAD RELOC READONLY CODE DATA ROM CONSTRUCTOR\n\
1113 HAS_CONTENTS NEVER_LOAD COFF_SHARED_LIBRARY IS_COMMON\n\
1114 \n\
1115 Sections matching any of the FILTERS will be listed (no FILTERS implies\n\
1116 all sections should be printed).\n\
1117 \n\
1118 Options:\n\
1119 %OPTIONS%"), opts);
1120 cmd = add_cmd ("sections", class_maintenance, maintenance_info_sections,
1121 maint_info_sections_command_help.c_str (),
1122 &maintenanceinfolist);
1123 set_cmd_completer_handle_brkchars (cmd, maint_info_sections_completer);
1124
1125 add_basic_prefix_cmd ("print", class_maintenance,
1126 _("Maintenance command for printing GDB internal state."),
1127 &maintenanceprintlist, "maintenance print ", 0,
1128 &maintenancelist);
1129
1130 add_basic_prefix_cmd ("flush", class_maintenance,
1131 _("Maintenance command for flushing GDB internal caches."),
1132 &maintenanceflushlist, "maintenance flush ", 0,
1133 &maintenancelist);
1134
1135 add_basic_prefix_cmd ("set", class_maintenance, _("\
1136 Set GDB internal variables used by the GDB maintainer.\n\
1137 Configure variables internal to GDB that aid in GDB's maintenance"),
1138 &maintenance_set_cmdlist, "maintenance set ",
1139 0/*allow-unknown*/,
1140 &maintenancelist);
1141
1142 add_show_prefix_cmd ("show", class_maintenance, _("\
1143 Show GDB internal variables used by the GDB maintainer.\n\
1144 Configure variables internal to GDB that aid in GDB's maintenance"),
1145 &maintenance_show_cmdlist, "maintenance show ",
1146 0/*allow-unknown*/,
1147 &maintenancelist);
1148
1149 cmd = add_cmd ("with", class_maintenance, maintenance_with_cmd, _("\
1150 Like \"with\", but works with \"maintenance set\" variables.\n\
1151 Usage: maintenance with SETTING [VALUE] [-- COMMAND]\n\
1152 With no COMMAND, repeats the last executed command.\n\
1153 SETTING is any setting you can change with the \"maintenance set\"\n\
1154 subcommands."),
1155 &maintenancelist);
1156 set_cmd_completer_handle_brkchars (cmd, maintenance_with_cmd_completer);
1157
1158 #ifndef _WIN32
1159 add_cmd ("dump-me", class_maintenance, maintenance_dump_me, _("\
1160 Get fatal error; make debugger dump its core.\n\
1161 GDB sets its handling of SIGQUIT back to SIG_DFL and then sends\n\
1162 itself a SIGQUIT signal."),
1163 &maintenancelist);
1164 #endif
1165
1166 add_cmd ("internal-error", class_maintenance,
1167 maintenance_internal_error, _("\
1168 Give GDB an internal error.\n\
1169 Cause GDB to behave as if an internal error was detected."),
1170 &maintenancelist);
1171
1172 add_cmd ("internal-warning", class_maintenance,
1173 maintenance_internal_warning, _("\
1174 Give GDB an internal warning.\n\
1175 Cause GDB to behave as if an internal warning was reported."),
1176 &maintenancelist);
1177
1178 add_cmd ("demangler-warning", class_maintenance,
1179 maintenance_demangler_warning, _("\
1180 Give GDB a demangler warning.\n\
1181 Cause GDB to behave as if a demangler warning was reported."),
1182 &maintenancelist);
1183
1184 cmd = add_cmd ("demangle", class_maintenance, maintenance_demangle, _("\
1185 This command has been moved to \"demangle\"."),
1186 &maintenancelist);
1187 deprecate_cmd (cmd, "demangle");
1188
1189 add_prefix_cmd ("per-command", class_maintenance, set_per_command_cmd, _("\
1190 Per-command statistics settings."),
1191 &per_command_setlist, "maintenance set per-command ",
1192 1/*allow-unknown*/, &maintenance_set_cmdlist);
1193
1194 add_show_prefix_cmd ("per-command", class_maintenance, _("\
1195 Show per-command statistics settings."),
1196 &per_command_showlist, "maintenance show per-command ",
1197 0/*allow-unknown*/, &maintenance_show_cmdlist);
1198
1199 add_setshow_boolean_cmd ("time", class_maintenance,
1200 &per_command_time, _("\
1201 Set whether to display per-command execution time."), _("\
1202 Show whether to display per-command execution time."),
1203 _("\
1204 If enabled, the execution time for each command will be\n\
1205 displayed following the command's output."),
1206 NULL, NULL,
1207 &per_command_setlist, &per_command_showlist);
1208
1209 add_setshow_boolean_cmd ("space", class_maintenance,
1210 &per_command_space, _("\
1211 Set whether to display per-command space usage."), _("\
1212 Show whether to display per-command space usage."),
1213 _("\
1214 If enabled, the space usage for each command will be\n\
1215 displayed following the command's output."),
1216 NULL, NULL,
1217 &per_command_setlist, &per_command_showlist);
1218
1219 add_setshow_boolean_cmd ("symtab", class_maintenance,
1220 &per_command_symtab, _("\
1221 Set whether to display per-command symtab statistics."), _("\
1222 Show whether to display per-command symtab statistics."),
1223 _("\
1224 If enabled, the basic symtab statistics for each command will be\n\
1225 displayed following the command's output."),
1226 NULL, NULL,
1227 &per_command_setlist, &per_command_showlist);
1228
1229 /* This is equivalent to "mt set per-command time on".
1230 Kept because some people are used to typing "mt time 1". */
1231 add_cmd ("time", class_maintenance, maintenance_time_display, _("\
1232 Set the display of time usage.\n\
1233 If nonzero, will cause the execution time for each command to be\n\
1234 displayed, following the command's output."),
1235 &maintenancelist);
1236
1237 /* This is equivalent to "mt set per-command space on".
1238 Kept because some people are used to typing "mt space 1". */
1239 add_cmd ("space", class_maintenance, maintenance_space_display, _("\
1240 Set the display of space usage.\n\
1241 If nonzero, will cause the execution space for each command to be\n\
1242 displayed, following the command's output."),
1243 &maintenancelist);
1244
1245 cmd = add_cmd ("type", class_maintenance, maintenance_print_type, _("\
1246 Print a type chain for a given symbol.\n\
1247 For each node in a type chain, print the raw data for each member of\n\
1248 the type structure, and the interpretation of the data."),
1249 &maintenanceprintlist);
1250 set_cmd_completer (cmd, expression_completer);
1251
1252 add_cmd ("statistics", class_maintenance, maintenance_print_statistics,
1253 _("Print statistics about internal gdb state."),
1254 &maintenanceprintlist);
1255
1256 add_cmd ("architecture", class_maintenance,
1257 maintenance_print_architecture, _("\
1258 Print the internal architecture configuration.\n\
1259 Takes an optional file parameter."),
1260 &maintenanceprintlist);
1261
1262 add_basic_prefix_cmd ("check", class_maintenance, _("\
1263 Commands for checking internal gdb state."),
1264 &maintenancechecklist, "maintenance check ", 0,
1265 &maintenancelist);
1266
1267 add_cmd ("translate-address", class_maintenance,
1268 maintenance_translate_address,
1269 _("Translate a section name and address to a symbol."),
1270 &maintenancelist);
1271
1272 add_cmd ("deprecate", class_maintenance, maintenance_deprecate, _("\
1273 Deprecate a command (for testing purposes).\n\
1274 Usage: maintenance deprecate COMMANDNAME [\"REPLACEMENT\"]\n\
1275 This is used by the testsuite to check the command deprecator.\n\
1276 You probably shouldn't use this,\n\
1277 rather you should use the C function deprecate_cmd()."), &maintenancelist);
1278
1279 add_cmd ("undeprecate", class_maintenance, maintenance_undeprecate, _("\
1280 Undeprecate a command (for testing purposes).\n\
1281 Usage: maintenance undeprecate COMMANDNAME\n\
1282 This is used by the testsuite to check the command deprecator.\n\
1283 You probably shouldn't use this."),
1284 &maintenancelist);
1285
1286 add_cmd ("selftest", class_maintenance, maintenance_selftest, _("\
1287 Run gdb's unit tests.\n\
1288 Usage: maintenance selftest [FILTER]\n\
1289 This will run any unit tests that were built in to gdb.\n\
1290 If a filter is given, only the tests with that value in their name will ran."),
1291 &maintenancelist);
1292
1293 add_cmd ("selftests", class_maintenance, maintenance_info_selftests,
1294 _("List the registered selftests."), &maintenanceinfolist);
1295
1296 add_setshow_boolean_cmd ("profile", class_maintenance,
1297 &maintenance_profile_p, _("\
1298 Set internal profiling."), _("\
1299 Show internal profiling."), _("\
1300 When enabled GDB is profiled."),
1301 maintenance_set_profile_cmd,
1302 show_maintenance_profile_p,
1303 &maintenance_set_cmdlist,
1304 &maintenance_show_cmdlist);
1305
1306 add_setshow_zuinteger_unlimited_cmd ("worker-threads",
1307 class_maintenance,
1308 &n_worker_threads, _("\
1309 Set the number of worker threads GDB can use."), _("\
1310 Show the number of worker threads GDB can use."), _("\
1311 GDB may use multiple threads to speed up certain CPU-intensive operations,\n\
1312 such as demangling symbol names."),
1313 maintenance_set_worker_threads, NULL,
1314 &maintenance_set_cmdlist,
1315 &maintenance_show_cmdlist);
1316
1317 update_thread_pool_size ();
1318 }