[BFD, AArch64] Fix PT_GNU_PROPERTY alignment issue
[deliverable/binutils-gdb.git] / gdb / maint.c
CommitLineData
c906108c 1/* Support for GDB maintenance commands.
c6f0559b 2
42a4f53d 3 Copyright (C) 1992-2019 Free Software Foundation, Inc.
c6f0559b 4
c906108c
SS
5 Written by Fred Fish at Cygnus Support.
6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
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.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22
23#include "defs.h"
e17c207e 24#include "arch-utils.h"
c906108c
SS
25#include <ctype.h>
26#include <signal.h>
27#include "command.h"
28#include "gdbcmd.h"
29#include "symtab.h"
bd712aed 30#include "block.h"
c906108c
SS
31#include "gdbtypes.h"
32#include "demangle.h"
33#include "gdbcore.h"
c5aa993b 34#include "expression.h" /* For language.h */
c906108c
SS
35#include "language.h"
36#include "symfile.h"
37#include "objfiles.h"
38#include "value.h"
bd712aed 39#include "top.h"
bd712aed 40#include "maint.h"
0747795c 41#include "common/selftest.h"
c906108c 42
18a642a1 43#include "cli/cli-decode.h"
529480d0 44#include "cli/cli-utils.h"
bd712aed 45#include "cli/cli-setshow.h"
18a642a1 46
58971144 47static void maintenance_do_deprecate (const char *, int);
1c689132 48
7f86f058 49/* Access the maintenance subcommands. */
c906108c
SS
50
51static void
981a3fb3 52maintenance_command (const char *args, int from_tty)
c906108c 53{
3e43a32a
MS
54 printf_unfiltered (_("\"maintenance\" must be followed by "
55 "the name of a maintenance command.\n"));
635c7e8a 56 help_list (maintenancelist, "maintenance ", all_commands, gdb_stdout);
c906108c
SS
57}
58
59#ifndef _WIN32
c906108c 60static void
58971144 61maintenance_dump_me (const char *args, int from_tty)
c906108c 62{
9e2f0ad4 63 if (query (_("Should GDB dump core? ")))
c906108c 64 {
7be570e7
JM
65#ifdef __DJGPP__
66 /* SIGQUIT by default is ignored, so use SIGABRT instead. */
67 signal (SIGABRT, SIG_DFL);
68 kill (getpid (), SIGABRT);
69#else
c906108c
SS
70 signal (SIGQUIT, SIG_DFL);
71 kill (getpid (), SIGQUIT);
7be570e7 72#endif
c906108c
SS
73 }
74}
75#endif
76
7be570e7
JM
77/* Stimulate the internal error mechanism that GDB uses when an
78 internal problem is detected. Allows testing of the mechanism.
79 Also useful when the user wants to drop a core file but not exit
025bb325 80 GDB. */
7be570e7
JM
81
82static void
5fed81ff 83maintenance_internal_error (const char *args, int from_tty)
7be570e7 84{
dec43320
AC
85 internal_error (__FILE__, __LINE__, "%s", (args == NULL ? "" : args));
86}
87
88/* Stimulate the internal error mechanism that GDB uses when an
89 internal problem is detected. Allows testing of the mechanism.
90 Also useful when the user wants to drop a core file but not exit
025bb325 91 GDB. */
dec43320
AC
92
93static void
5fed81ff 94maintenance_internal_warning (const char *args, int from_tty)
dec43320
AC
95{
96 internal_warning (__FILE__, __LINE__, "%s", (args == NULL ? "" : args));
7be570e7
JM
97}
98
57fcfb1b
GB
99/* Stimulate the internal error mechanism that GDB uses when an
100 demangler problem is detected. Allows testing of the mechanism. */
101
102static void
5fed81ff 103maintenance_demangler_warning (const char *args, int from_tty)
57fcfb1b
GB
104{
105 demangler_warning (__FILE__, __LINE__, "%s", (args == NULL ? "" : args));
106}
107
439250fb
DE
108/* Old command to demangle a string. The command has been moved to "demangle".
109 It is kept for now because otherwise "mt demangle" gets interpreted as
110 "mt demangler-warning" which artificially creates an internal gdb error. */
c906108c
SS
111
112static void
58971144 113maintenance_demangle (const char *args, int from_tty)
c906108c 114{
439250fb 115 printf_filtered (_("This command has been moved to \"demangle\".\n"));
c906108c
SS
116}
117
118static void
58971144 119maintenance_time_display (const char *args, int from_tty)
c906108c 120{
c906108c 121 if (args == NULL || *args == '\0')
a3f17187 122 printf_unfiltered (_("\"maintenance time\" takes a numeric argument.\n"));
c906108c 123 else
bd712aed 124 set_per_command_time (strtol (args, NULL, 10));
c906108c
SS
125}
126
127static void
5fed81ff 128maintenance_space_display (const char *args, int from_tty)
c906108c 129{
c906108c
SS
130 if (args == NULL || *args == '\0')
131 printf_unfiltered ("\"maintenance space\" takes a numeric argument.\n");
132 else
bd712aed 133 set_per_command_space (strtol (args, NULL, 10));
c906108c
SS
134}
135
33f91161
AC
136/* The "maintenance info" command is defined as a prefix, with
137 allow_unknown 0. Therefore, its own definition is called only for
138 "maintenance info" with no args. */
c906108c 139
c906108c 140static void
981a3fb3 141maintenance_info_command (const char *arg, int from_tty)
c906108c 142{
3e43a32a
MS
143 printf_unfiltered (_("\"maintenance info\" must be followed "
144 "by the name of an info command.\n"));
635c7e8a
TT
145 help_list (maintenanceinfolist, "maintenance info ", all_commands,
146 gdb_stdout);
c906108c
SS
147}
148
27d41eac
YQ
149/* The "maintenance check" command is defined as a prefix, with
150 allow_unknown 0. Therefore, its own definition is called only for
151 "maintenance check" with no args. */
152
153static void
981a3fb3 154maintenance_check_command (const char *arg, int from_tty)
27d41eac
YQ
155{
156 printf_unfiltered (_("\"maintenance check\" must be followed "
157 "by the name of a check command.\n"));
158 help_list (maintenancechecklist, "maintenance check ", all_commands,
159 gdb_stdout);
160}
161
a532ca62
MS
162/* Mini tokenizing lexer for 'maint info sections' command. */
163
164static int
473e38f3 165match_substring (const char *string, const char *substr)
a532ca62
MS
166{
167 int substr_len = strlen(substr);
473e38f3 168 const char *tok;
a532ca62
MS
169
170 while ((tok = strstr (string, substr)) != NULL)
171 {
025bb325 172 /* Got a partial match. Is it a whole word? */
b01d807c
MS
173 if (tok == string
174 || tok[-1] == ' '
175 || tok[-1] == '\t')
a532ca62 176 {
025bb325 177 /* Token is delimited at the front... */
b01d807c
MS
178 if (tok[substr_len] == ' '
179 || tok[substr_len] == '\t'
180 || tok[substr_len] == '\0')
a532ca62
MS
181 {
182 /* Token is delimited at the rear. Got a whole-word match. */
183 return 1;
184 }
185 }
186 /* Token didn't match as a whole word. Advance and try again. */
187 string = tok + 1;
188 }
189 return 0;
190}
191
43155bc1 192static int
fc4baa5e 193match_bfd_flags (const char *string, flagword flags)
c906108c 194{
43155bc1 195 if (flags & SEC_ALLOC)
a532ca62 196 if (match_substring (string, "ALLOC"))
43155bc1
MS
197 return 1;
198 if (flags & SEC_LOAD)
a532ca62 199 if (match_substring (string, "LOAD"))
43155bc1
MS
200 return 1;
201 if (flags & SEC_RELOC)
a532ca62 202 if (match_substring (string, "RELOC"))
43155bc1
MS
203 return 1;
204 if (flags & SEC_READONLY)
a532ca62 205 if (match_substring (string, "READONLY"))
43155bc1
MS
206 return 1;
207 if (flags & SEC_CODE)
a532ca62 208 if (match_substring (string, "CODE"))
43155bc1
MS
209 return 1;
210 if (flags & SEC_DATA)
a532ca62 211 if (match_substring (string, "DATA"))
43155bc1
MS
212 return 1;
213 if (flags & SEC_ROM)
a532ca62 214 if (match_substring (string, "ROM"))
43155bc1
MS
215 return 1;
216 if (flags & SEC_CONSTRUCTOR)
a532ca62 217 if (match_substring (string, "CONSTRUCTOR"))
43155bc1
MS
218 return 1;
219 if (flags & SEC_HAS_CONTENTS)
a532ca62 220 if (match_substring (string, "HAS_CONTENTS"))
43155bc1
MS
221 return 1;
222 if (flags & SEC_NEVER_LOAD)
a532ca62 223 if (match_substring (string, "NEVER_LOAD"))
43155bc1
MS
224 return 1;
225 if (flags & SEC_COFF_SHARED_LIBRARY)
a532ca62 226 if (match_substring (string, "COFF_SHARED_LIBRARY"))
43155bc1
MS
227 return 1;
228 if (flags & SEC_IS_COMMON)
a532ca62 229 if (match_substring (string, "IS_COMMON"))
43155bc1 230 return 1;
c906108c 231
43155bc1
MS
232 return 0;
233}
c906108c 234
43155bc1
MS
235static void
236print_bfd_flags (flagword flags)
237{
c906108c
SS
238 if (flags & SEC_ALLOC)
239 printf_filtered (" ALLOC");
240 if (flags & SEC_LOAD)
241 printf_filtered (" LOAD");
242 if (flags & SEC_RELOC)
243 printf_filtered (" RELOC");
244 if (flags & SEC_READONLY)
245 printf_filtered (" READONLY");
246 if (flags & SEC_CODE)
247 printf_filtered (" CODE");
248 if (flags & SEC_DATA)
249 printf_filtered (" DATA");
250 if (flags & SEC_ROM)
251 printf_filtered (" ROM");
252 if (flags & SEC_CONSTRUCTOR)
253 printf_filtered (" CONSTRUCTOR");
254 if (flags & SEC_HAS_CONTENTS)
255 printf_filtered (" HAS_CONTENTS");
256 if (flags & SEC_NEVER_LOAD)
257 printf_filtered (" NEVER_LOAD");
258 if (flags & SEC_COFF_SHARED_LIBRARY)
259 printf_filtered (" COFF_SHARED_LIBRARY");
260 if (flags & SEC_IS_COMMON)
261 printf_filtered (" IS_COMMON");
43155bc1
MS
262}
263
264static void
67a2b77e
AC
265maint_print_section_info (const char *name, flagword flags,
266 CORE_ADDR addr, CORE_ADDR endaddr,
5af949e3 267 unsigned long filepos, int addr_size)
43155bc1 268{
5af949e3
UW
269 printf_filtered (" %s", hex_string_custom (addr, addr_size));
270 printf_filtered ("->%s", hex_string_custom (endaddr, addr_size));
3ab13650 271 printf_filtered (" at %s",
bb599908 272 hex_string_custom ((unsigned long) filepos, 8));
e3d3bfda
MS
273 printf_filtered (": %s", name);
274 print_bfd_flags (flags);
275 printf_filtered ("\n");
276}
c906108c 277
e3d3bfda
MS
278static void
279print_bfd_section_info (bfd *abfd,
280 asection *asect,
fc4baa5e 281 void *datum)
e3d3bfda
MS
282{
283 flagword flags = bfd_get_section_flags (abfd, asect);
284 const char *name = bfd_section_name (abfd, asect);
19ba03f4 285 const char *arg = (const char *) datum;
e3d3bfda 286
fc4baa5e
TT
287 if (arg == NULL || *arg == '\0'
288 || match_substring (arg, name)
289 || match_bfd_flags (arg, flags))
e3d3bfda 290 {
5af949e3
UW
291 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
292 int addr_size = gdbarch_addr_bit (gdbarch) / 8;
e3d3bfda
MS
293 CORE_ADDR addr, endaddr;
294
295 addr = bfd_section_vma (abfd, asect);
296 endaddr = addr + bfd_section_size (abfd, asect);
8625fc1b 297 printf_filtered (" [%d] ", gdb_bfd_section_index (abfd, asect));
5af949e3
UW
298 maint_print_section_info (name, flags, addr, endaddr,
299 asect->filepos, addr_size);
e3d3bfda
MS
300 }
301}
302
303static void
304print_objfile_section_info (bfd *abfd,
305 struct obj_section *asect,
fc4baa5e 306 const char *string)
e3d3bfda
MS
307{
308 flagword flags = bfd_get_section_flags (abfd, asect->the_bfd_section);
309 const char *name = bfd_section_name (abfd, asect->the_bfd_section);
43155bc1 310
b01d807c
MS
311 if (string == NULL || *string == '\0'
312 || match_substring (string, name)
313 || match_bfd_flags (string, flags))
43155bc1 314 {
5af949e3
UW
315 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
316 int addr_size = gdbarch_addr_bit (gdbarch) / 8;
b8d56208 317
f1f6aadf
PA
318 maint_print_section_info (name, flags,
319 obj_section_addr (asect),
320 obj_section_endaddr (asect),
5af949e3
UW
321 asect->the_bfd_section->filepos,
322 addr_size);
43155bc1 323 }
c906108c
SS
324}
325
c906108c 326static void
58971144 327maintenance_info_sections (const char *arg, int from_tty)
c906108c
SS
328{
329 if (exec_bfd)
330 {
a3f17187 331 printf_filtered (_("Exec file:\n"));
c5aa993b 332 printf_filtered (" `%s', ", bfd_get_filename (exec_bfd));
c906108c 333 wrap_here (" ");
a3f17187 334 printf_filtered (_("file type %s.\n"), bfd_get_target (exec_bfd));
a532ca62 335 if (arg && *arg && match_substring (arg, "ALLOBJ"))
e3d3bfda 336 {
e3d3bfda
MS
337 struct obj_section *osect;
338
339 /* Only this function cares about the 'ALLOBJ' argument;
340 if 'ALLOBJ' is the only argument, discard it rather than
341 passing it down to print_objfile_section_info (which
342 wouldn't know how to handle it). */
343 if (strcmp (arg, "ALLOBJ") == 0)
344 arg = NULL;
345
2030c079 346 for (objfile *ofile : current_program_space->objfiles ())
e3d3bfda 347 {
a3f17187 348 printf_filtered (_(" Object file: %s\n"),
e3d3bfda
MS
349 bfd_get_filename (ofile->obfd));
350 ALL_OBJFILE_OSECTIONS (ofile, osect)
351 {
352 print_objfile_section_info (ofile->obfd, osect, arg);
353 }
354 }
355 }
356 else
58971144 357 bfd_map_over_sections (exec_bfd, print_bfd_section_info, (void *) arg);
c906108c
SS
358 }
359
360 if (core_bfd)
361 {
a3f17187 362 printf_filtered (_("Core file:\n"));
c5aa993b 363 printf_filtered (" `%s', ", bfd_get_filename (core_bfd));
c906108c 364 wrap_here (" ");
a3f17187 365 printf_filtered (_("file type %s.\n"), bfd_get_target (core_bfd));
58971144 366 bfd_map_over_sections (core_bfd, print_bfd_section_info, (void *) arg);
c906108c
SS
367 }
368}
369
025cfdb2 370static void
58971144 371maintenance_print_statistics (const char *args, int from_tty)
c906108c
SS
372{
373 print_objfile_statistics ();
374 print_symbol_bcache_statistics ();
375}
376
b9362cc7 377static void
5fed81ff 378maintenance_print_architecture (const char *args, int from_tty)
4b9b3959 379{
e17c207e
UW
380 struct gdbarch *gdbarch = get_current_arch ();
381
4b9b3959 382 if (args == NULL)
e17c207e 383 gdbarch_dump (gdbarch, gdb_stdout);
4b9b3959
AC
384 else
385 {
d7e74731 386 stdio_file file;
b8d56208 387
d7e74731 388 if (!file.open (args, "w"))
e2e0b3e5 389 perror_with_name (_("maintenance print architecture"));
d7e74731 390 gdbarch_dump (gdbarch, &file);
4b9b3959
AC
391 }
392}
393
33f91161
AC
394/* The "maintenance print" command is defined as a prefix, with
395 allow_unknown 0. Therefore, its own definition is called only for
396 "maintenance print" with no args. */
c906108c 397
c906108c 398static void
981a3fb3 399maintenance_print_command (const char *arg, int from_tty)
c906108c 400{
3e43a32a
MS
401 printf_unfiltered (_("\"maintenance print\" must be followed "
402 "by the name of a print command.\n"));
635c7e8a
TT
403 help_list (maintenanceprintlist, "maintenance print ", all_commands,
404 gdb_stdout);
c906108c
SS
405}
406
407/* The "maintenance translate-address" command converts a section and address
408 to a symbol. This can be called in two ways:
c5aa993b 409 maintenance translate-address <secname> <addr>
025bb325 410 or maintenance translate-address <addr>. */
c906108c
SS
411
412static void
5fed81ff 413maintenance_translate_address (const char *arg, int from_tty)
c906108c
SS
414{
415 CORE_ADDR address;
714835d5 416 struct obj_section *sect;
5fed81ff 417 const char *p;
7cbd4a93 418 struct bound_minimal_symbol sym;
c906108c
SS
419
420 if (arg == NULL || *arg == 0)
8a3fe4f8 421 error (_("requires argument (address or section + address)"));
c906108c
SS
422
423 sect = NULL;
424 p = arg;
425
426 if (!isdigit (*p))
025bb325
MS
427 { /* See if we have a valid section name. */
428 while (*p && !isspace (*p)) /* Find end of section name. */
c906108c 429 p++;
025bb325 430 if (*p == '\000') /* End of command? */
65e65158 431 error (_("Need to specify section name and address"));
5fed81ff
TT
432
433 int arg_len = p - arg;
434 p = skip_spaces (p + 1);
c906108c 435
2030c079 436 for (objfile *objfile : current_program_space->objfiles ())
3b9d3ac2
TT
437 ALL_OBJFILE_OSECTIONS (objfile, sect)
438 {
439 if (strncmp (sect->the_bfd_section->name, arg, arg_len) == 0)
440 goto found;
441 }
c906108c 442
3b9d3ac2
TT
443 error (_("Unknown section %s."), arg);
444 found: ;
c906108c
SS
445 }
446
447 address = parse_and_eval_address (p);
448
449 if (sect)
450 sym = lookup_minimal_symbol_by_pc_section (address, sect);
451 else
452 sym = lookup_minimal_symbol_by_pc (address);
453
7cbd4a93 454 if (sym.minsym)
c14c28ba 455 {
efd66ac6 456 const char *symbol_name = MSYMBOL_PRINT_NAME (sym.minsym);
3e43a32a 457 const char *symbol_offset
77e371c0 458 = pulongest (address - BMSYMBOL_VALUE_ADDRESS (sym));
c14c28ba 459
efd66ac6 460 sect = MSYMBOL_OBJ_SECTION(sym.objfile, sym.minsym);
c14c28ba
PP
461 if (sect != NULL)
462 {
463 const char *section_name;
464 const char *obj_name;
465
466 gdb_assert (sect->the_bfd_section && sect->the_bfd_section->name);
467 section_name = sect->the_bfd_section->name;
468
4262abfb
JK
469 gdb_assert (sect->objfile && objfile_name (sect->objfile));
470 obj_name = objfile_name (sect->objfile);
c14c28ba
PP
471
472 if (MULTI_OBJFILE_P ())
473 printf_filtered (_("%s + %s in section %s of %s\n"),
3e43a32a
MS
474 symbol_name, symbol_offset,
475 section_name, obj_name);
c14c28ba
PP
476 else
477 printf_filtered (_("%s + %s in section %s\n"),
478 symbol_name, symbol_offset, section_name);
479 }
480 else
481 printf_filtered (_("%s + %s\n"), symbol_name, symbol_offset);
482 }
c906108c 483 else if (sect)
5af949e3
UW
484 printf_filtered (_("no symbol at %s:%s\n"),
485 sect->the_bfd_section->name, hex_string (address));
c906108c 486 else
5af949e3 487 printf_filtered (_("no symbol at %s\n"), hex_string (address));
c906108c
SS
488
489 return;
490}
491
56382845 492
c114dcd5 493/* When a command is deprecated the user will be warned the first time
33f91161 494 the command is used. If possible, a replacement will be
025bb325 495 offered. */
56382845
FN
496
497static void
58971144 498maintenance_deprecate (const char *args, int from_tty)
56382845
FN
499{
500 if (args == NULL || *args == '\0')
501 {
cce7e648
PA
502 printf_unfiltered (_("\"maintenance deprecate\" takes an argument,\n\
503the command you want to deprecate, and optionally the replacement command\n\
a3f17187 504enclosed in quotes.\n"));
56382845 505 }
33f91161 506
56382845 507 maintenance_do_deprecate (args, 1);
56382845
FN
508}
509
510
511static void
58971144 512maintenance_undeprecate (const char *args, int from_tty)
56382845
FN
513{
514 if (args == NULL || *args == '\0')
515 {
a3f17187
AC
516 printf_unfiltered (_("\"maintenance undeprecate\" takes an argument, \n\
517the command you want to undeprecate.\n"));
56382845 518 }
33f91161 519
56382845 520 maintenance_do_deprecate (args, 0);
56382845
FN
521}
522
025bb325 523/* You really shouldn't be using this. It is just for the testsuite.
33f91161
AC
524 Rather, you should use deprecate_cmd() when the command is created
525 in _initialize_blah().
526
527 This function deprecates a command and optionally assigns it a
528 replacement. */
529
8399535b 530static void
58971144 531maintenance_do_deprecate (const char *text, int deprecate)
33f91161 532{
33f91161
AC
533 struct cmd_list_element *alias = NULL;
534 struct cmd_list_element *prefix_cmd = NULL;
535 struct cmd_list_element *cmd = NULL;
536
58971144
TT
537 const char *start_ptr = NULL;
538 const char *end_ptr = NULL;
56382845 539 int len;
33f91161
AC
540 char *replacement = NULL;
541
1c689132
DB
542 if (text == NULL)
543 return;
56382845 544
33f91161
AC
545 if (!lookup_cmd_composition (text, &alias, &prefix_cmd, &cmd))
546 {
a3f17187 547 printf_filtered (_("Can't find command '%s' to deprecate.\n"), text);
33f91161
AC
548 return;
549 }
56382845 550
56382845
FN
551 if (deprecate)
552 {
025bb325 553 /* Look for a replacement command. */
80ce1ecb
AC
554 start_ptr = strchr (text, '\"');
555 if (start_ptr != NULL)
33f91161
AC
556 {
557 start_ptr++;
80ce1ecb
AC
558 end_ptr = strrchr (start_ptr, '\"');
559 if (end_ptr != NULL)
33f91161
AC
560 {
561 len = end_ptr - start_ptr;
58971144 562 replacement = savestring (start_ptr, len);
33f91161
AC
563 }
564 }
56382845 565 }
33f91161 566
56382845
FN
567 if (!start_ptr || !end_ptr)
568 replacement = NULL;
33f91161
AC
569
570
56382845 571 /* If they used an alias, we only want to deprecate the alias.
33f91161 572
56382845
FN
573 Note the MALLOCED_REPLACEMENT test. If the command's replacement
574 string was allocated at compile time we don't want to free the
025bb325 575 memory. */
56382845
FN
576 if (alias)
577 {
1f2bdf09 578 if (alias->malloced_replacement)
429e55ea 579 xfree ((char *) alias->replacement);
33f91161 580
56382845 581 if (deprecate)
1f2bdf09
TT
582 {
583 alias->deprecated_warn_user = 1;
584 alias->cmd_deprecated = 1;
585 }
56382845 586 else
1f2bdf09
TT
587 {
588 alias->deprecated_warn_user = 0;
589 alias->cmd_deprecated = 0;
590 }
33f91161 591 alias->replacement = replacement;
1f2bdf09 592 alias->malloced_replacement = 1;
56382845
FN
593 return;
594 }
595 else if (cmd)
596 {
1f2bdf09 597 if (cmd->malloced_replacement)
429e55ea 598 xfree ((char *) cmd->replacement);
56382845
FN
599
600 if (deprecate)
1f2bdf09
TT
601 {
602 cmd->deprecated_warn_user = 1;
603 cmd->cmd_deprecated = 1;
604 }
56382845 605 else
1f2bdf09
TT
606 {
607 cmd->deprecated_warn_user = 0;
608 cmd->cmd_deprecated = 0;
609 }
33f91161 610 cmd->replacement = replacement;
1f2bdf09 611 cmd->malloced_replacement = 1;
56382845
FN
612 return;
613 }
240f9570 614 xfree (replacement);
56382845
FN
615}
616
4f337972
AC
617/* Maintenance set/show framework. */
618
ae038cb0
DJ
619struct cmd_list_element *maintenance_set_cmdlist;
620struct cmd_list_element *maintenance_show_cmdlist;
4f337972
AC
621
622static void
981a3fb3 623maintenance_set_cmd (const char *args, int from_tty)
4f337972 624{
3e43a32a
MS
625 printf_unfiltered (_("\"maintenance set\" must be followed "
626 "by the name of a set command.\n"));
635c7e8a
TT
627 help_list (maintenance_set_cmdlist, "maintenance set ", all_commands,
628 gdb_stdout);
4f337972
AC
629}
630
631static void
981a3fb3 632maintenance_show_cmd (const char *args, int from_tty)
4f337972
AC
633{
634 cmd_show_list (maintenance_show_cmdlist, from_tty, "");
635}
636
4f337972
AC
637/* Profiling support. */
638
639static int maintenance_profile_p;
920d2a44
AC
640static void
641show_maintenance_profile_p (struct ui_file *file, int from_tty,
642 struct cmd_list_element *c, const char *value)
643{
644 fprintf_filtered (file, _("Internal profiling is %s.\n"), value);
645}
d9feb4e7 646
b0b1c2c0
MK
647#ifdef HAVE__ETEXT
648extern char _etext;
649#define TEXTEND &_etext
01fe12f6 650#elif defined (HAVE_ETEXT)
b0b1c2c0
MK
651extern char etext;
652#define TEXTEND &etext
653#endif
654
01fe12f6
JB
655#if defined (HAVE_MONSTARTUP) && defined (HAVE__MCLEANUP) && defined (TEXTEND)
656
d28f9cdf
DJ
657static int profiling_state;
658
56000a98
PA
659EXTERN_C void _mcleanup (void);
660
d28f9cdf
DJ
661static void
662mcleanup_wrapper (void)
663{
d28f9cdf
DJ
664 if (profiling_state)
665 _mcleanup ();
666}
4f337972 667
56000a98
PA
668EXTERN_C void monstartup (unsigned long, unsigned long);
669extern int main ();
670
4f337972 671static void
eb4c3f4a 672maintenance_set_profile_cmd (const char *args, int from_tty,
3e43a32a 673 struct cmd_list_element *c)
4f337972 674{
d28f9cdf
DJ
675 if (maintenance_profile_p == profiling_state)
676 return;
677
678 profiling_state = maintenance_profile_p;
679
680 if (maintenance_profile_p)
681 {
682 static int profiling_initialized;
683
d28f9cdf
DJ
684 if (!profiling_initialized)
685 {
686 atexit (mcleanup_wrapper);
687 profiling_initialized = 1;
688 }
689
690 /* "main" is now always the first function in the text segment, so use
691 its address for monstartup. */
b0b1c2c0 692 monstartup ((unsigned long) &main, (unsigned long) TEXTEND);
d28f9cdf
DJ
693 }
694 else
695 {
696 extern void _mcleanup (void);
b8d56208 697
d28f9cdf
DJ
698 _mcleanup ();
699 }
4f337972 700}
d9feb4e7
DJ
701#else
702static void
eb4c3f4a 703maintenance_set_profile_cmd (const char *args, int from_tty,
3e43a32a 704 struct cmd_list_element *c)
d9feb4e7 705{
8a3fe4f8 706 error (_("Profiling support is not available on this system."));
d9feb4e7
DJ
707}
708#endif
bd712aed
DE
709\f
710/* If nonzero, display time usage both at startup and for each command. */
56382845 711
bd712aed
DE
712static int per_command_time;
713
714/* If nonzero, display space usage both at startup and for each command. */
715
716static int per_command_space;
717
718/* If nonzero, display basic symtab stats for each command. */
719
720static int per_command_symtab;
721
722/* mt per-command commands. */
723
724static struct cmd_list_element *per_command_setlist;
725static struct cmd_list_element *per_command_showlist;
726
bd712aed
DE
727/* Set whether to display time statistics to NEW_VALUE
728 (non-zero means true). */
729
730void
731set_per_command_time (int new_value)
732{
733 per_command_time = new_value;
734}
735
736/* Set whether to display space statistics to NEW_VALUE
737 (non-zero means true). */
738
739void
740set_per_command_space (int new_value)
741{
742 per_command_space = new_value;
743}
744
745/* Count the number of symtabs and blocks. */
746
747static void
43f3e411 748count_symtabs_and_blocks (int *nr_symtabs_ptr, int *nr_compunit_symtabs_ptr,
bd712aed
DE
749 int *nr_blocks_ptr)
750{
bd712aed 751 int nr_symtabs = 0;
43f3e411 752 int nr_compunit_symtabs = 0;
bd712aed
DE
753 int nr_blocks = 0;
754
b8b8facf
DE
755 /* When collecting statistics during startup, this is called before
756 pretty much anything in gdb has been initialized, and thus
757 current_program_space may be NULL. */
758 if (current_program_space != NULL)
bd712aed 759 {
2030c079 760 for (objfile *o : current_program_space->objfiles ())
bd712aed 761 {
b669c953 762 for (compunit_symtab *cu : o->compunits ())
d8aeb77f
TT
763 {
764 ++nr_compunit_symtabs;
765 nr_blocks += BLOCKVECTOR_NBLOCKS (COMPUNIT_BLOCKVECTOR (cu));
5accd1a0
TT
766 nr_symtabs += std::distance (compunit_filetabs (cu).begin (),
767 compunit_filetabs (cu).end ());
d8aeb77f 768 }
bd712aed
DE
769 }
770 }
771
772 *nr_symtabs_ptr = nr_symtabs;
43f3e411 773 *nr_compunit_symtabs_ptr = nr_compunit_symtabs;
bd712aed
DE
774 *nr_blocks_ptr = nr_blocks;
775}
776
1e3b796d
TT
777/* As indicated by display_time and display_space, report GDB's
778 elapsed time and space usage from the base time and space recorded
779 in this object. */
bd712aed 780
1e3b796d 781scoped_command_stats::~scoped_command_stats ()
bd712aed 782{
1e3b796d
TT
783 /* Early exit if we're not reporting any stats. It can be expensive to
784 compute the pre-command values so don't collect them at all if we're
785 not reporting stats. Alas this doesn't work in the startup case because
786 we don't know yet whether we will be reporting the stats. For the
787 startup case collect the data anyway (it should be cheap at this point),
788 and leave it to the reporter to decide whether to print them. */
789 if (m_msg_type
790 && !per_command_time
791 && !per_command_space
792 && !per_command_symtab)
793 return;
bd712aed 794
1e3b796d 795 if (m_time_enabled && per_command_time)
bd712aed 796 {
dcb07cfa 797 using namespace std::chrono;
bd712aed 798
dcb07cfa
PA
799 run_time_clock::duration cmd_time
800 = run_time_clock::now () - m_start_cpu_time;
bd712aed 801
dcb07cfa
PA
802 steady_clock::duration wall_time
803 = steady_clock::now () - m_start_wall_time;
bd712aed 804 /* Subtract time spend in prompt_for_continue from walltime. */
dcb07cfa 805 wall_time -= get_prompt_for_continue_wait_time ();
bd712aed 806
1e3b796d 807 printf_unfiltered (!m_msg_type
dcb07cfa
PA
808 ? _("Startup time: %.6f (cpu), %.6f (wall)\n")
809 : _("Command execution time: %.6f (cpu), %.6f (wall)\n"),
810 duration<double> (cmd_time).count (),
811 duration<double> (wall_time).count ());
bd712aed
DE
812 }
813
1e3b796d 814 if (m_space_enabled && per_command_space)
bd712aed 815 {
6242c6a6 816#ifdef HAVE_USEFUL_SBRK
bd712aed
DE
817 char *lim = (char *) sbrk (0);
818
819 long space_now = lim - lim_at_start;
1e3b796d 820 long space_diff = space_now - m_start_space;
bd712aed 821
1e3b796d 822 printf_unfiltered (!m_msg_type
bd712aed
DE
823 ? _("Space used: %ld (%s%ld during startup)\n")
824 : _("Space used: %ld (%s%ld for this command)\n"),
825 space_now,
826 (space_diff >= 0 ? "+" : ""),
827 space_diff);
828#endif
829 }
830
1e3b796d 831 if (m_symtab_enabled && per_command_symtab)
bd712aed 832 {
43f3e411 833 int nr_symtabs, nr_compunit_symtabs, nr_blocks;
bd712aed 834
43f3e411 835 count_symtabs_and_blocks (&nr_symtabs, &nr_compunit_symtabs, &nr_blocks);
bd712aed 836 printf_unfiltered (_("#symtabs: %d (+%d),"
43f3e411 837 " #compunits: %d (+%d),"
bd712aed
DE
838 " #blocks: %d (+%d)\n"),
839 nr_symtabs,
1e3b796d 840 nr_symtabs - m_start_nr_symtabs,
43f3e411
DE
841 nr_compunit_symtabs,
842 (nr_compunit_symtabs
1e3b796d 843 - m_start_nr_compunit_symtabs),
bd712aed 844 nr_blocks,
1e3b796d 845 nr_blocks - m_start_nr_blocks);
bd712aed
DE
846 }
847}
848
1e3b796d
TT
849scoped_command_stats::scoped_command_stats (bool msg_type)
850: m_msg_type (msg_type)
bd712aed 851{
1e3b796d 852 if (!m_msg_type || per_command_space)
bd712aed 853 {
6242c6a6 854#ifdef HAVE_USEFUL_SBRK
bd712aed 855 char *lim = (char *) sbrk (0);
1e3b796d
TT
856 m_start_space = lim - lim_at_start;
857 m_space_enabled = 1;
bd712aed
DE
858#endif
859 }
44d83468
PA
860 else
861 m_space_enabled = 0;
bd712aed 862
b8b8facf 863 if (msg_type == 0 || per_command_time)
bd712aed 864 {
dcb07cfa
PA
865 using namespace std::chrono;
866
867 m_start_cpu_time = run_time_clock::now ();
868 m_start_wall_time = steady_clock::now ();
1e3b796d 869 m_time_enabled = 1;
bd712aed 870 }
44d83468
PA
871 else
872 m_time_enabled = 0;
bd712aed 873
b8b8facf 874 if (msg_type == 0 || per_command_symtab)
bd712aed 875 {
43f3e411 876 int nr_symtabs, nr_compunit_symtabs, nr_blocks;
bd712aed 877
43f3e411 878 count_symtabs_and_blocks (&nr_symtabs, &nr_compunit_symtabs, &nr_blocks);
1e3b796d
TT
879 m_start_nr_symtabs = nr_symtabs;
880 m_start_nr_compunit_symtabs = nr_compunit_symtabs;
881 m_start_nr_blocks = nr_blocks;
882 m_symtab_enabled = 1;
bd712aed 883 }
44d83468
PA
884 else
885 m_symtab_enabled = 0;
bd712aed 886
26c4b26f 887 /* Initialize timer to keep track of how long we waited for the user. */
bd712aed 888 reset_prompt_for_continue_wait_time ();
bd712aed
DE
889}
890
891/* Handle unknown "mt set per-command" arguments.
892 In this case have "mt set per-command on|off" affect every setting. */
893
894static void
981a3fb3 895set_per_command_cmd (const char *args, int from_tty)
bd712aed
DE
896{
897 struct cmd_list_element *list;
bd712aed
DE
898 int val;
899
900 val = parse_cli_boolean_value (args);
901 if (val < 0)
902 error (_("Bad value for 'mt set per-command no'."));
903
904 for (list = per_command_setlist; list != NULL; list = list->next)
905 if (list->var_type == var_boolean)
906 {
907 gdb_assert (list->type == set_cmd);
908 do_set_command (args, from_tty, list);
909 }
910}
911
912/* Command "show per-command" displays summary of all the current
913 "show per-command " settings. */
914
915static void
981a3fb3 916show_per_command_cmd (const char *args, int from_tty)
bd712aed
DE
917{
918 cmd_show_list (per_command_showlist, from_tty, "");
919}
dcd1f979
TT
920\f
921
922/* The "maintenance selftest" command. */
923
924static void
58971144 925maintenance_selftest (const char *args, int from_tty)
dcd1f979 926{
1e5ded6c 927#if GDB_SELF_TEST
1526853e 928 selftests::run_tests (args);
1e5ded6c
YQ
929#else
930 printf_filtered (_("\
8ecfd7bd 931Selftests have been disabled for this build.\n"));
1e5ded6c 932#endif
1526853e
SM
933}
934
935static void
5fed81ff 936maintenance_info_selftests (const char *arg, int from_tty)
1526853e 937{
1e5ded6c 938#if GDB_SELF_TEST
1526853e
SM
939 printf_filtered ("Registered selftests:\n");
940 selftests::for_each_selftest ([] (const std::string &name) {
941 printf_filtered (" - %s\n", name.c_str ());
942 });
1e5ded6c
YQ
943#else
944 printf_filtered (_("\
8ecfd7bd 945Selftests have been disabled for this build.\n"));
1e5ded6c 946#endif
dcd1f979
TT
947}
948
bd712aed 949\f
c906108c 950void
fba45db2 951_initialize_maint_cmds (void)
c906108c 952{
439250fb
DE
953 struct cmd_list_element *cmd;
954
1bedd215
AC
955 add_prefix_cmd ("maintenance", class_maintenance, maintenance_command, _("\
956Commands for use by GDB maintainers.\n\
c906108c 957Includes commands to dump specific internal GDB structures in\n\
439250fb 958a human readable form, to cause GDB to deliberately dump core, etc."),
c906108c
SS
959 &maintenancelist, "maintenance ", 0,
960 &cmdlist);
961
962 add_com_alias ("mt", "maintenance", class_maintenance, 1);
963
1bedd215
AC
964 add_prefix_cmd ("info", class_maintenance, maintenance_info_command, _("\
965Commands for showing internal info about the program being debugged."),
c906108c
SS
966 &maintenanceinfolist, "maintenance info ", 0,
967 &maintenancelist);
90515c23 968 add_alias_cmd ("i", "info", class_maintenance, 1, &maintenancelist);
c906108c 969
1a966eab
AC
970 add_cmd ("sections", class_maintenance, maintenance_info_sections, _("\
971List the BFD sections of the exec and core files. \n\
e3d3bfda
MS
972Arguments may be any combination of:\n\
973 [one or more section names]\n\
974 ALLOC LOAD RELOC READONLY CODE DATA ROM CONSTRUCTOR\n\
975 HAS_CONTENTS NEVER_LOAD COFF_SHARED_LIBRARY IS_COMMON\n\
976Sections matching any argument will be listed (no argument\n\
977implies all sections). In addition, the special argument\n\
978 ALLOBJ\n\
1a966eab 979lists all sections from all object files, including shared libraries."),
c906108c
SS
980 &maintenanceinfolist);
981
982 add_prefix_cmd ("print", class_maintenance, maintenance_print_command,
1bedd215 983 _("Maintenance command for printing GDB internal state."),
c906108c
SS
984 &maintenanceprintlist, "maintenance print ", 0,
985 &maintenancelist);
986
1bedd215 987 add_prefix_cmd ("set", class_maintenance, maintenance_set_cmd, _("\
4f337972 988Set GDB internal variables used by the GDB maintainer.\n\
1bedd215 989Configure variables internal to GDB that aid in GDB's maintenance"),
4f337972
AC
990 &maintenance_set_cmdlist, "maintenance set ",
991 0/*allow-unknown*/,
992 &maintenancelist);
993
1bedd215 994 add_prefix_cmd ("show", class_maintenance, maintenance_show_cmd, _("\
4f337972 995Show GDB internal variables used by the GDB maintainer.\n\
1bedd215 996Configure variables internal to GDB that aid in GDB's maintenance"),
4f337972
AC
997 &maintenance_show_cmdlist, "maintenance show ",
998 0/*allow-unknown*/,
999 &maintenancelist);
1000
c906108c 1001#ifndef _WIN32
1a966eab
AC
1002 add_cmd ("dump-me", class_maintenance, maintenance_dump_me, _("\
1003Get fatal error; make debugger dump its core.\n\
8308e54c 1004GDB sets its handling of SIGQUIT back to SIG_DFL and then sends\n\
1a966eab 1005itself a SIGQUIT signal."),
c906108c
SS
1006 &maintenancelist);
1007#endif
1008
1a966eab
AC
1009 add_cmd ("internal-error", class_maintenance,
1010 maintenance_internal_error, _("\
1011Give GDB an internal error.\n\
1012Cause GDB to behave as if an internal error was detected."),
7be570e7
JM
1013 &maintenancelist);
1014
1a966eab
AC
1015 add_cmd ("internal-warning", class_maintenance,
1016 maintenance_internal_warning, _("\
1017Give GDB an internal warning.\n\
1018Cause GDB to behave as if an internal warning was reported."),
dec43320
AC
1019 &maintenancelist);
1020
57fcfb1b
GB
1021 add_cmd ("demangler-warning", class_maintenance,
1022 maintenance_demangler_warning, _("\
1023Give GDB a demangler warning.\n\
1024Cause GDB to behave as if a demangler warning was reported."),
1025 &maintenancelist);
1026
439250fb
DE
1027 cmd = add_cmd ("demangle", class_maintenance, maintenance_demangle, _("\
1028This command has been moved to \"demangle\"."),
1029 &maintenancelist);
1030 deprecate_cmd (cmd, "demangle");
c906108c 1031
bd712aed
DE
1032 add_prefix_cmd ("per-command", class_maintenance, set_per_command_cmd, _("\
1033Per-command statistics settings."),
387cd15b 1034 &per_command_setlist, "maintenance set per-command ",
bd712aed
DE
1035 1/*allow-unknown*/, &maintenance_set_cmdlist);
1036
1037 add_prefix_cmd ("per-command", class_maintenance, show_per_command_cmd, _("\
1038Show per-command statistics settings."),
387cd15b 1039 &per_command_showlist, "maintenance show per-command ",
bd712aed
DE
1040 0/*allow-unknown*/, &maintenance_show_cmdlist);
1041
1042 add_setshow_boolean_cmd ("time", class_maintenance,
1043 &per_command_time, _("\
1044Set whether to display per-command execution time."), _("\
1045Show whether to display per-command execution time."),
1046 _("\
1047If enabled, the execution time for each command will be\n\
1048displayed following the command's output."),
1049 NULL, NULL,
1050 &per_command_setlist, &per_command_showlist);
1051
1052 add_setshow_boolean_cmd ("space", class_maintenance,
1053 &per_command_space, _("\
1054Set whether to display per-command space usage."), _("\
1055Show whether to display per-command space usage."),
1056 _("\
1057If enabled, the space usage for each command will be\n\
1058displayed following the command's output."),
1059 NULL, NULL,
1060 &per_command_setlist, &per_command_showlist);
1061
1062 add_setshow_boolean_cmd ("symtab", class_maintenance,
1063 &per_command_symtab, _("\
1064Set whether to display per-command symtab statistics."), _("\
1065Show whether to display per-command symtab statistics."),
1066 _("\
1067If enabled, the basic symtab statistics for each command will be\n\
1068displayed following the command's output."),
1069 NULL, NULL,
1070 &per_command_setlist, &per_command_showlist);
1071
1072 /* This is equivalent to "mt set per-command time on".
1073 Kept because some people are used to typing "mt time 1". */
1a966eab
AC
1074 add_cmd ("time", class_maintenance, maintenance_time_display, _("\
1075Set the display of time usage.\n\
c906108c 1076If nonzero, will cause the execution time for each command to be\n\
1a966eab 1077displayed, following the command's output."),
c906108c
SS
1078 &maintenancelist);
1079
bd712aed
DE
1080 /* This is equivalent to "mt set per-command space on".
1081 Kept because some people are used to typing "mt space 1". */
1a966eab
AC
1082 add_cmd ("space", class_maintenance, maintenance_space_display, _("\
1083Set the display of space usage.\n\
c906108c 1084If nonzero, will cause the execution space for each command to be\n\
1a966eab 1085displayed, following the command's output."),
c906108c
SS
1086 &maintenancelist);
1087
1a966eab
AC
1088 add_cmd ("type", class_maintenance, maintenance_print_type, _("\
1089Print a type chain for a given symbol.\n\
c906108c 1090For each node in a type chain, print the raw data for each member of\n\
1a966eab 1091the type structure, and the interpretation of the data."),
c906108c
SS
1092 &maintenanceprintlist);
1093
c906108c 1094 add_cmd ("statistics", class_maintenance, maintenance_print_statistics,
1a966eab 1095 _("Print statistics about internal gdb state."),
c906108c
SS
1096 &maintenanceprintlist);
1097
1a966eab
AC
1098 add_cmd ("architecture", class_maintenance,
1099 maintenance_print_architecture, _("\
1100Print the internal architecture configuration.\n\
1101Takes an optional file parameter."),
4b9b3959
AC
1102 &maintenanceprintlist);
1103
27d41eac
YQ
1104 add_prefix_cmd ("check", class_maintenance, maintenance_check_command, _("\
1105Commands for checking internal gdb state."),
1106 &maintenancechecklist, "maintenance check ", 0,
1107 &maintenancelist);
1108
3e43a32a
MS
1109 add_cmd ("translate-address", class_maintenance,
1110 maintenance_translate_address,
1a966eab 1111 _("Translate a section name and address to a symbol."),
c906108c
SS
1112 &maintenancelist);
1113
1a966eab
AC
1114 add_cmd ("deprecate", class_maintenance, maintenance_deprecate, _("\
1115Deprecate a command. Note that this is just in here so the \n\
ef4cb050 1116testsuite can check the command deprecator. You probably shouldn't use this,\n\
56382845 1117rather you should use the C function deprecate_cmd(). If you decide you \n\
6f122dc9 1118want to use it: maintenance deprecate 'commandname' \"replacement\". The \n\
1a966eab 1119replacement is optional."), &maintenancelist);
56382845 1120
1a966eab
AC
1121 add_cmd ("undeprecate", class_maintenance, maintenance_undeprecate, _("\
1122Undeprecate a command. Note that this is just in here so the \n\
ef4cb050 1123testsuite can check the command deprecator. You probably shouldn't use this,\n\
1a966eab 1124If you decide you want to use it: maintenance undeprecate 'commandname'"),
33f91161 1125 &maintenancelist);
56382845 1126
dcd1f979
TT
1127 add_cmd ("selftest", class_maintenance, maintenance_selftest, _("\
1128Run gdb's unit tests.\n\
1526853e 1129Usage: maintenance selftest [filter]\n\
dcd1f979 1130This will run any unit tests that were built in to gdb.\n\
1526853e 1131If a filter is given, only the tests with that value in their name will ran."),
dcd1f979
TT
1132 &maintenancelist);
1133
1526853e
SM
1134 add_cmd ("selftests", class_maintenance, maintenance_info_selftests,
1135 _("List the registered selftests."), &maintenanceinfolist);
1136
d28f9cdf 1137 add_setshow_boolean_cmd ("profile", class_maintenance,
7915a72c
AC
1138 &maintenance_profile_p, _("\
1139Set internal profiling."), _("\
1140Show internal profiling."), _("\
1141When enabled GDB is profiled."),
2c5b56ce 1142 maintenance_set_profile_cmd,
920d2a44 1143 show_maintenance_profile_p,
d28f9cdf
DJ
1144 &maintenance_set_cmdlist,
1145 &maintenance_show_cmdlist);
c906108c 1146}
This page took 1.753118 seconds and 4 git commands to generate.