gdb: Force use of float version of log10
[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_get_section_flags (abfd, asect);
339 const char *name = bfd_section_name (abfd, 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 (abfd, asect);
352 endaddr = addr + bfd_section_size (abfd, 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_get_section_flags (abfd, asect->the_bfd_section);
370 const char *name = bfd_section_name (abfd, 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 = MSYMBOL_PRINT_NAME (sym.minsym);
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 int 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 nonzero, display time usage both at startup and for each command. */
845
846 static int per_command_time;
847
848 /* If nonzero, display space usage both at startup and for each command. */
849
850 static int per_command_space;
851
852 /* If nonzero, display basic symtab stats for each command. */
853
854 static int 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 = localtime (&as_time);
1043
1044 char out[100];
1045 strftime (out, sizeof (out), "%F %H:%M:%S", tm);
1046
1047 printf_unfiltered ("%s.%03d - %s\n", out, (int) millis, msg);
1048 }
1049
1050 /* Handle unknown "mt set per-command" arguments.
1051 In this case have "mt set per-command on|off" affect every setting. */
1052
1053 static void
1054 set_per_command_cmd (const char *args, int from_tty)
1055 {
1056 struct cmd_list_element *list;
1057 int val;
1058
1059 val = parse_cli_boolean_value (args);
1060 if (val < 0)
1061 error (_("Bad value for 'mt set per-command no'."));
1062
1063 for (list = per_command_setlist; list != NULL; list = list->next)
1064 if (list->var_type == var_boolean)
1065 {
1066 gdb_assert (list->type == set_cmd);
1067 do_set_command (args, from_tty, list);
1068 }
1069 }
1070
1071 /* Command "show per-command" displays summary of all the current
1072 "show per-command " settings. */
1073
1074 static void
1075 show_per_command_cmd (const char *args, int from_tty)
1076 {
1077 cmd_show_list (per_command_showlist, from_tty, "");
1078 }
1079 \f
1080
1081 /* The "maintenance selftest" command. */
1082
1083 static void
1084 maintenance_selftest (const char *args, int from_tty)
1085 {
1086 #if GDB_SELF_TEST
1087 selftests::run_tests (args);
1088 #else
1089 printf_filtered (_("\
1090 Selftests have been disabled for this build.\n"));
1091 #endif
1092 }
1093
1094 static void
1095 maintenance_info_selftests (const char *arg, int from_tty)
1096 {
1097 #if GDB_SELF_TEST
1098 printf_filtered ("Registered selftests:\n");
1099 selftests::for_each_selftest ([] (const std::string &name) {
1100 printf_filtered (" - %s\n", name.c_str ());
1101 });
1102 #else
1103 printf_filtered (_("\
1104 Selftests have been disabled for this build.\n"));
1105 #endif
1106 }
1107
1108 \f
1109 void
1110 _initialize_maint_cmds (void)
1111 {
1112 struct cmd_list_element *cmd;
1113
1114 add_prefix_cmd ("maintenance", class_maintenance, maintenance_command, _("\
1115 Commands for use by GDB maintainers.\n\
1116 Includes commands to dump specific internal GDB structures in\n\
1117 a human readable form, to cause GDB to deliberately dump core, etc."),
1118 &maintenancelist, "maintenance ", 0,
1119 &cmdlist);
1120
1121 add_com_alias ("mt", "maintenance", class_maintenance, 1);
1122
1123 add_prefix_cmd ("info", class_maintenance, maintenance_info_command, _("\
1124 Commands for showing internal info about the program being debugged."),
1125 &maintenanceinfolist, "maintenance info ", 0,
1126 &maintenancelist);
1127 add_alias_cmd ("i", "info", class_maintenance, 1, &maintenancelist);
1128
1129 add_cmd ("sections", class_maintenance, maintenance_info_sections, _("\
1130 List the BFD sections of the exec and core files.\n\
1131 Arguments may be any combination of:\n\
1132 [one or more section names]\n\
1133 ALLOC LOAD RELOC READONLY CODE DATA ROM CONSTRUCTOR\n\
1134 HAS_CONTENTS NEVER_LOAD COFF_SHARED_LIBRARY IS_COMMON\n\
1135 Sections matching any argument will be listed (no argument\n\
1136 implies all sections). In addition, the special argument\n\
1137 ALLOBJ\n\
1138 lists all sections from all object files, including shared libraries."),
1139 &maintenanceinfolist);
1140
1141 add_prefix_cmd ("print", class_maintenance, maintenance_print_command,
1142 _("Maintenance command for printing GDB internal state."),
1143 &maintenanceprintlist, "maintenance print ", 0,
1144 &maintenancelist);
1145
1146 add_prefix_cmd ("set", class_maintenance, maintenance_set_cmd, _("\
1147 Set GDB internal variables used by the GDB maintainer.\n\
1148 Configure variables internal to GDB that aid in GDB's maintenance"),
1149 &maintenance_set_cmdlist, "maintenance set ",
1150 0/*allow-unknown*/,
1151 &maintenancelist);
1152
1153 add_prefix_cmd ("show", class_maintenance, maintenance_show_cmd, _("\
1154 Show GDB internal variables used by the GDB maintainer.\n\
1155 Configure variables internal to GDB that aid in GDB's maintenance"),
1156 &maintenance_show_cmdlist, "maintenance show ",
1157 0/*allow-unknown*/,
1158 &maintenancelist);
1159
1160 cmd = add_cmd ("with", class_maintenance, maintenance_with_cmd, _("\
1161 Like \"with\", but works with \"maintenance set\" variables.\n\
1162 Usage: maintenance with SETTING [VALUE] [-- COMMAND]\n\
1163 With no COMMAND, repeats the last executed command.\n\
1164 SETTING is any setting you can change with the \"maintenance set\"\n\
1165 subcommands."),
1166 &maintenancelist);
1167 set_cmd_completer_handle_brkchars (cmd, maintenance_with_cmd_completer);
1168
1169 #ifndef _WIN32
1170 add_cmd ("dump-me", class_maintenance, maintenance_dump_me, _("\
1171 Get fatal error; make debugger dump its core.\n\
1172 GDB sets its handling of SIGQUIT back to SIG_DFL and then sends\n\
1173 itself a SIGQUIT signal."),
1174 &maintenancelist);
1175 #endif
1176
1177 add_cmd ("internal-error", class_maintenance,
1178 maintenance_internal_error, _("\
1179 Give GDB an internal error.\n\
1180 Cause GDB to behave as if an internal error was detected."),
1181 &maintenancelist);
1182
1183 add_cmd ("internal-warning", class_maintenance,
1184 maintenance_internal_warning, _("\
1185 Give GDB an internal warning.\n\
1186 Cause GDB to behave as if an internal warning was reported."),
1187 &maintenancelist);
1188
1189 add_cmd ("demangler-warning", class_maintenance,
1190 maintenance_demangler_warning, _("\
1191 Give GDB a demangler warning.\n\
1192 Cause GDB to behave as if a demangler warning was reported."),
1193 &maintenancelist);
1194
1195 cmd = add_cmd ("demangle", class_maintenance, maintenance_demangle, _("\
1196 This command has been moved to \"demangle\"."),
1197 &maintenancelist);
1198 deprecate_cmd (cmd, "demangle");
1199
1200 add_prefix_cmd ("per-command", class_maintenance, set_per_command_cmd, _("\
1201 Per-command statistics settings."),
1202 &per_command_setlist, "maintenance set per-command ",
1203 1/*allow-unknown*/, &maintenance_set_cmdlist);
1204
1205 add_prefix_cmd ("per-command", class_maintenance, show_per_command_cmd, _("\
1206 Show per-command statistics settings."),
1207 &per_command_showlist, "maintenance show per-command ",
1208 0/*allow-unknown*/, &maintenance_show_cmdlist);
1209
1210 add_setshow_boolean_cmd ("time", class_maintenance,
1211 &per_command_time, _("\
1212 Set whether to display per-command execution time."), _("\
1213 Show whether to display per-command execution time."),
1214 _("\
1215 If enabled, the execution time for each command will be\n\
1216 displayed following the command's output."),
1217 NULL, NULL,
1218 &per_command_setlist, &per_command_showlist);
1219
1220 add_setshow_boolean_cmd ("space", class_maintenance,
1221 &per_command_space, _("\
1222 Set whether to display per-command space usage."), _("\
1223 Show whether to display per-command space usage."),
1224 _("\
1225 If enabled, the space usage for each command will be\n\
1226 displayed following the command's output."),
1227 NULL, NULL,
1228 &per_command_setlist, &per_command_showlist);
1229
1230 add_setshow_boolean_cmd ("symtab", class_maintenance,
1231 &per_command_symtab, _("\
1232 Set whether to display per-command symtab statistics."), _("\
1233 Show whether to display per-command symtab statistics."),
1234 _("\
1235 If enabled, the basic symtab statistics for each command will be\n\
1236 displayed following the command's output."),
1237 NULL, NULL,
1238 &per_command_setlist, &per_command_showlist);
1239
1240 /* This is equivalent to "mt set per-command time on".
1241 Kept because some people are used to typing "mt time 1". */
1242 add_cmd ("time", class_maintenance, maintenance_time_display, _("\
1243 Set the display of time usage.\n\
1244 If nonzero, will cause the execution time for each command to be\n\
1245 displayed, following the command's output."),
1246 &maintenancelist);
1247
1248 /* This is equivalent to "mt set per-command space on".
1249 Kept because some people are used to typing "mt space 1". */
1250 add_cmd ("space", class_maintenance, maintenance_space_display, _("\
1251 Set the display of space usage.\n\
1252 If nonzero, will cause the execution space for each command to be\n\
1253 displayed, following the command's output."),
1254 &maintenancelist);
1255
1256 add_cmd ("type", class_maintenance, maintenance_print_type, _("\
1257 Print a type chain for a given symbol.\n\
1258 For each node in a type chain, print the raw data for each member of\n\
1259 the type structure, and the interpretation of the data."),
1260 &maintenanceprintlist);
1261
1262 add_cmd ("statistics", class_maintenance, maintenance_print_statistics,
1263 _("Print statistics about internal gdb state."),
1264 &maintenanceprintlist);
1265
1266 add_cmd ("architecture", class_maintenance,
1267 maintenance_print_architecture, _("\
1268 Print the internal architecture configuration.\n\
1269 Takes an optional file parameter."),
1270 &maintenanceprintlist);
1271
1272 add_prefix_cmd ("check", class_maintenance, maintenance_check_command, _("\
1273 Commands for checking internal gdb state."),
1274 &maintenancechecklist, "maintenance check ", 0,
1275 &maintenancelist);
1276
1277 add_cmd ("translate-address", class_maintenance,
1278 maintenance_translate_address,
1279 _("Translate a section name and address to a symbol."),
1280 &maintenancelist);
1281
1282 add_cmd ("deprecate", class_maintenance, maintenance_deprecate, _("\
1283 Deprecate a command (for testing purposes).\n\
1284 Usage: maintenance deprecate COMMANDNAME [\"REPLACEMENT\"]\n\
1285 This is used by the testsuite to check the command deprecator.\n\
1286 You probably shouldn't use this,\n\
1287 rather you should use the C function deprecate_cmd()."), &maintenancelist);
1288
1289 add_cmd ("undeprecate", class_maintenance, maintenance_undeprecate, _("\
1290 Undeprecate a command (for testing purposes).\n\
1291 Usage: maintenance undeprecate COMMANDNAME\n\
1292 This is used by the testsuite to check the command deprecator.\n\
1293 You probably shouldn't use this."),
1294 &maintenancelist);
1295
1296 add_cmd ("selftest", class_maintenance, maintenance_selftest, _("\
1297 Run gdb's unit tests.\n\
1298 Usage: maintenance selftest [FILTER]\n\
1299 This will run any unit tests that were built in to gdb.\n\
1300 If a filter is given, only the tests with that value in their name will ran."),
1301 &maintenancelist);
1302
1303 add_cmd ("selftests", class_maintenance, maintenance_info_selftests,
1304 _("List the registered selftests."), &maintenanceinfolist);
1305
1306 add_setshow_boolean_cmd ("profile", class_maintenance,
1307 &maintenance_profile_p, _("\
1308 Set internal profiling."), _("\
1309 Show internal profiling."), _("\
1310 When enabled GDB is profiled."),
1311 maintenance_set_profile_cmd,
1312 show_maintenance_profile_p,
1313 &maintenance_set_cmdlist,
1314 &maintenance_show_cmdlist);
1315 }
This page took 0.080587 seconds and 4 git commands to generate.