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