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