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