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