* testsuite/regress-demangle (failed test): Show result and
[binutils-gdb.git] / gdb / maint.c
blob44dcba8e18eea68ad325edc63d6763857ab6b8c8
1 /* Support for GDB maintenance commands.
2 Copyright 1992, 1993, 1994 Free Software Foundation, Inc.
3 Written by Fred Fish at Cygnus Support.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "defs.h"
24 #include <ctype.h>
25 #include <signal.h>
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "symtab.h"
29 #include "gdbtypes.h"
30 #include "demangle.h"
31 #include "gdbcore.h"
32 #include "expression.h" /* For language.h */
33 #include "language.h"
34 #include "symfile.h"
35 #include "objfiles.h"
36 #include "value.h"
38 extern void _initialize_maint_cmds (void);
40 static void maintenance_command (char *, int);
42 static void maintenance_dump_me (char *, int);
44 static void maintenance_internal_error (char *args, int from_tty);
46 static void maintenance_demangle (char *, int);
48 static void maintenance_time_display (char *, int);
50 static void maintenance_space_display (char *, int);
52 static void maintenance_info_command (char *, int);
54 static void print_section_table (bfd *, asection *, PTR);
56 static void maintenance_info_sections (char *, int);
58 static void maintenance_print_command (char *, int);
60 static void maintenance_do_deprecate (char *, int);
62 /* Set this to the maximum number of seconds to wait instead of waiting forever
63 in target_wait(). If this timer times out, then it generates an error and
64 the command is aborted. This replaces most of the need for timeouts in the
65 GDB test suite, and makes it possible to distinguish between a hung target
66 and one with slow communications. */
68 int watchdog = 0;
72 LOCAL FUNCTION
74 maintenance_command -- access the maintenance subcommands
76 SYNOPSIS
78 void maintenance_command (char *args, int from_tty)
80 DESCRIPTION
84 static void
85 maintenance_command (args, from_tty)
86 char *args;
87 int from_tty;
89 printf_unfiltered ("\"maintenance\" must be followed by the name of a maintenance command.\n");
90 help_list (maintenancelist, "maintenance ", -1, gdb_stdout);
93 #ifndef _WIN32
94 /* ARGSUSED */
95 static void
96 maintenance_dump_me (args, from_tty)
97 char *args;
98 int from_tty;
100 if (query ("Should GDB dump core? "))
102 #ifdef __DJGPP__
103 /* SIGQUIT by default is ignored, so use SIGABRT instead. */
104 signal (SIGABRT, SIG_DFL);
105 kill (getpid (), SIGABRT);
106 #else
107 signal (SIGQUIT, SIG_DFL);
108 kill (getpid (), SIGQUIT);
109 #endif
112 #endif
114 /* Stimulate the internal error mechanism that GDB uses when an
115 internal problem is detected. Allows testing of the mechanism.
116 Also useful when the user wants to drop a core file but not exit
117 GDB. */
119 static void
120 maintenance_internal_error (char *args, int from_tty)
122 internal_error ("internal maintenance");
125 /* Someday we should allow demangling for things other than just
126 explicit strings. For example, we might want to be able to specify
127 the address of a string in either GDB's process space or the
128 debuggee's process space, and have gdb fetch and demangle that
129 string. If we have a char* pointer "ptr" that points to a string,
130 we might want to be able to given just the name and have GDB
131 demangle and print what it points to, etc. (FIXME) */
133 static void
134 maintenance_demangle (args, from_tty)
135 char *args;
136 int from_tty;
138 char *demangled;
140 if (args == NULL || *args == '\0')
142 printf_unfiltered ("\"maintenance demangle\" takes an argument to demangle.\n");
144 else
146 demangled = cplus_demangle (args, DMGL_ANSI | DMGL_PARAMS);
147 if (demangled != NULL)
149 printf_unfiltered ("%s\n", demangled);
150 free (demangled);
152 else
154 printf_unfiltered ("Can't demangle \"%s\"\n", args);
159 static void
160 maintenance_time_display (args, from_tty)
161 char *args;
162 int from_tty;
164 extern int display_time;
166 if (args == NULL || *args == '\0')
167 printf_unfiltered ("\"maintenance time\" takes a numeric argument.\n");
168 else
169 display_time = strtol (args, NULL, 10);
172 static void
173 maintenance_space_display (args, from_tty)
174 char *args;
175 int from_tty;
177 extern int display_space;
179 if (args == NULL || *args == '\0')
180 printf_unfiltered ("\"maintenance space\" takes a numeric argument.\n");
181 else
182 display_space = strtol (args, NULL, 10);
185 /* The "maintenance info" command is defined as a prefix, with
186 allow_unknown 0. Therefore, its own definition is called only for
187 "maintenance info" with no args. */
189 /* ARGSUSED */
190 static void
191 maintenance_info_command (arg, from_tty)
192 char *arg;
193 int from_tty;
195 printf_unfiltered ("\"maintenance info\" must be followed by the name of an info command.\n");
196 help_list (maintenanceinfolist, "maintenance info ", -1, gdb_stdout);
199 static void
200 print_section_table (abfd, asect, ignore)
201 bfd *abfd;
202 asection *asect;
203 PTR ignore;
205 flagword flags;
207 flags = bfd_get_section_flags (abfd, asect);
209 /* FIXME-32x64: Need print_address_numeric with field width. */
210 printf_filtered (" %s",
211 local_hex_string_custom
212 ((unsigned long) bfd_section_vma (abfd, asect), "08l"));
213 printf_filtered ("->%s",
214 local_hex_string_custom
215 ((unsigned long) (bfd_section_vma (abfd, asect)
216 + bfd_section_size (abfd, asect)),
217 "08l"));
218 printf_filtered (" at %s",
219 local_hex_string_custom
220 ((unsigned long) asect->filepos, "08l"));
221 printf_filtered (": %s", bfd_section_name (abfd, asect));
223 if (flags & SEC_ALLOC)
224 printf_filtered (" ALLOC");
225 if (flags & SEC_LOAD)
226 printf_filtered (" LOAD");
227 if (flags & SEC_RELOC)
228 printf_filtered (" RELOC");
229 if (flags & SEC_READONLY)
230 printf_filtered (" READONLY");
231 if (flags & SEC_CODE)
232 printf_filtered (" CODE");
233 if (flags & SEC_DATA)
234 printf_filtered (" DATA");
235 if (flags & SEC_ROM)
236 printf_filtered (" ROM");
237 if (flags & SEC_CONSTRUCTOR)
238 printf_filtered (" CONSTRUCTOR");
239 if (flags & SEC_HAS_CONTENTS)
240 printf_filtered (" HAS_CONTENTS");
241 if (flags & SEC_NEVER_LOAD)
242 printf_filtered (" NEVER_LOAD");
243 if (flags & SEC_COFF_SHARED_LIBRARY)
244 printf_filtered (" COFF_SHARED_LIBRARY");
245 if (flags & SEC_IS_COMMON)
246 printf_filtered (" IS_COMMON");
248 printf_filtered ("\n");
251 /* ARGSUSED */
252 static void
253 maintenance_info_sections (arg, from_tty)
254 char *arg;
255 int from_tty;
257 if (exec_bfd)
259 printf_filtered ("Exec file:\n");
260 printf_filtered (" `%s', ", bfd_get_filename (exec_bfd));
261 wrap_here (" ");
262 printf_filtered ("file type %s.\n", bfd_get_target (exec_bfd));
263 bfd_map_over_sections (exec_bfd, print_section_table, 0);
266 if (core_bfd)
268 printf_filtered ("Core file:\n");
269 printf_filtered (" `%s', ", bfd_get_filename (core_bfd));
270 wrap_here (" ");
271 printf_filtered ("file type %s.\n", bfd_get_target (core_bfd));
272 bfd_map_over_sections (core_bfd, print_section_table, 0);
276 /* ARGSUSED */
277 void
278 maintenance_print_statistics (args, from_tty)
279 char *args;
280 int from_tty;
282 print_objfile_statistics ();
283 print_symbol_bcache_statistics ();
286 void
287 maintenance_print_architecture (char *args, int from_tty)
289 if (args == NULL)
290 gdbarch_dump (current_gdbarch, gdb_stdout);
291 else
293 struct ui_file *file = gdb_fopen (args, "w");
294 if (file == NULL)
295 perror_with_name ("maintenance print architecture");
296 gdbarch_dump (current_gdbarch, file);
297 ui_file_delete (file);
301 /* The "maintenance print" command is defined as a prefix, with
302 allow_unknown 0. Therefore, its own definition is called only for
303 "maintenance print" with no args. */
305 /* ARGSUSED */
306 static void
307 maintenance_print_command (arg, from_tty)
308 char *arg;
309 int from_tty;
311 printf_unfiltered ("\"maintenance print\" must be followed by the name of a print command.\n");
312 help_list (maintenanceprintlist, "maintenance print ", -1, gdb_stdout);
315 /* The "maintenance translate-address" command converts a section and address
316 to a symbol. This can be called in two ways:
317 maintenance translate-address <secname> <addr>
318 or maintenance translate-address <addr>
321 static void
322 maintenance_translate_address (arg, from_tty)
323 char *arg;
324 int from_tty;
326 CORE_ADDR address;
327 asection *sect;
328 char *p;
329 struct minimal_symbol *sym;
330 struct objfile *objfile;
332 if (arg == NULL || *arg == 0)
333 error ("requires argument (address or section + address)");
335 sect = NULL;
336 p = arg;
338 if (!isdigit (*p))
339 { /* See if we have a valid section name */
340 while (*p && !isspace (*p)) /* Find end of section name */
341 p++;
342 if (*p == '\000') /* End of command? */
343 error ("Need to specify <section-name> and <address>");
344 *p++ = '\000';
345 while (isspace (*p))
346 p++; /* Skip whitespace */
348 ALL_OBJFILES (objfile)
350 sect = bfd_get_section_by_name (objfile->obfd, arg);
351 if (sect != NULL)
352 break;
355 if (!sect)
356 error ("Unknown section %s.", arg);
359 address = parse_and_eval_address (p);
361 if (sect)
362 sym = lookup_minimal_symbol_by_pc_section (address, sect);
363 else
364 sym = lookup_minimal_symbol_by_pc (address);
366 if (sym)
367 printf_filtered ("%s+%s\n",
368 SYMBOL_SOURCE_NAME (sym),
369 paddr_u (address - SYMBOL_VALUE_ADDRESS (sym)));
370 else if (sect)
371 printf_filtered ("no symbol at %s:0x%s\n", sect->name, paddr (address));
372 else
373 printf_filtered ("no symbol at 0x%s\n", paddr (address));
375 return;
379 /* When a comamnd is deprecated the user will be warned the first time
380 the command is used. If possible, a replacement will be
381 offered. */
383 static void
384 maintenance_deprecate (char *args, int from_tty)
386 if (args == NULL || *args == '\0')
388 printf_unfiltered ("\"maintenance deprecate\" takes an argument, \n\
389 the command you want to deprecate, and optionally the replacement command \n\
390 enclosed in quotes.\n");
393 maintenance_do_deprecate (args, 1);
398 static void
399 maintenance_undeprecate (char *args, int from_tty)
401 if (args == NULL || *args == '\0')
403 printf_unfiltered ("\"maintenance undeprecate\" takes an argument, \n\
404 the command you want to undeprecate.\n");
407 maintenance_do_deprecate (args, 0);
411 /* You really shouldn't be using this. It is just for the testsuite.
412 Rather, you should use deprecate_cmd() when the command is created
413 in _initialize_blah().
415 This function deprecates a command and optionally assigns it a
416 replacement. */
418 static void
419 maintenance_do_deprecate (char *text, int deprecate)
422 struct cmd_list_element *alias = NULL;
423 struct cmd_list_element *prefix_cmd = NULL;
424 struct cmd_list_element *cmd = NULL;
426 char *start_ptr = NULL;
427 char *end_ptr = NULL;
428 int len;
429 char *replacement = NULL;
431 if (text == NULL)
432 return;
434 if (!lookup_cmd_composition (text, &alias, &prefix_cmd, &cmd))
436 printf_filtered ("Can't find command '%s' to deprecate.\n", text);
437 return;
440 if (deprecate)
442 /* look for a replacement command */
443 start_ptr = strchr (text, '\"');
444 if (start_ptr != NULL)
446 start_ptr++;
447 end_ptr = strrchr (start_ptr, '\"');
448 if (end_ptr != NULL)
450 len = end_ptr - start_ptr;
451 start_ptr[len] = '\0';
452 replacement = xstrdup (start_ptr);
457 if (!start_ptr || !end_ptr)
458 replacement = NULL;
461 /* If they used an alias, we only want to deprecate the alias.
463 Note the MALLOCED_REPLACEMENT test. If the command's replacement
464 string was allocated at compile time we don't want to free the
465 memory. */
466 if (alias)
469 if (alias->flags & MALLOCED_REPLACEMENT)
470 free (alias->replacement);
472 if (deprecate)
473 alias->flags |= (DEPRECATED_WARN_USER | CMD_DEPRECATED);
474 else
475 alias->flags &= ~(DEPRECATED_WARN_USER | CMD_DEPRECATED);
476 alias->replacement = replacement;
477 alias->flags |= MALLOCED_REPLACEMENT;
478 return;
480 else if (cmd)
482 if (cmd->flags & MALLOCED_REPLACEMENT)
483 free (cmd->replacement);
485 if (deprecate)
486 cmd->flags |= (DEPRECATED_WARN_USER | CMD_DEPRECATED);
487 else
488 cmd->flags &= ~(DEPRECATED_WARN_USER | CMD_DEPRECATED);
489 cmd->replacement = replacement;
490 cmd->flags |= MALLOCED_REPLACEMENT;
491 return;
496 void
497 _initialize_maint_cmds ()
499 add_prefix_cmd ("maintenance", class_maintenance, maintenance_command,
500 "Commands for use by GDB maintainers.\n\
501 Includes commands to dump specific internal GDB structures in\n\
502 a human readable form, to cause GDB to deliberately dump core,\n\
503 to test internal functions such as the C++ demangler, etc.",
504 &maintenancelist, "maintenance ", 0,
505 &cmdlist);
507 add_com_alias ("mt", "maintenance", class_maintenance, 1);
509 add_prefix_cmd ("info", class_maintenance, maintenance_info_command,
510 "Commands for showing internal info about the program being debugged.",
511 &maintenanceinfolist, "maintenance info ", 0,
512 &maintenancelist);
513 add_alias_cmd ("i", "info", class_maintenance, 1, &maintenancelist);
515 add_cmd ("sections", class_maintenance, maintenance_info_sections,
516 "List the BFD sections of the exec and core files.",
517 &maintenanceinfolist);
519 add_prefix_cmd ("print", class_maintenance, maintenance_print_command,
520 "Maintenance command for printing GDB internal state.",
521 &maintenanceprintlist, "maintenance print ", 0,
522 &maintenancelist);
524 #ifndef _WIN32
525 add_cmd ("dump-me", class_maintenance, maintenance_dump_me,
526 "Get fatal error; make debugger dump its core.\n\
527 GDB sets it's handling of SIGQUIT back to SIG_DFL and then sends\n\
528 itself a SIGQUIT signal.",
529 &maintenancelist);
530 #endif
532 add_cmd ("internal-error", class_maintenance, maintenance_internal_error,
533 "Give GDB an internal error.\n\
534 Cause GDB to behave as if an internal error was detected.",
535 &maintenancelist);
537 add_cmd ("demangle", class_maintenance, maintenance_demangle,
538 "Demangle a C++ mangled name.\n\
539 Call internal GDB demangler routine to demangle a C++ link name\n\
540 and prints the result.",
541 &maintenancelist);
543 add_cmd ("time", class_maintenance, maintenance_time_display,
544 "Set the display of time usage.\n\
545 If nonzero, will cause the execution time for each command to be\n\
546 displayed, following the command's output.",
547 &maintenancelist);
549 add_cmd ("space", class_maintenance, maintenance_space_display,
550 "Set the display of space usage.\n\
551 If nonzero, will cause the execution space for each command to be\n\
552 displayed, following the command's output.",
553 &maintenancelist);
555 add_cmd ("type", class_maintenance, maintenance_print_type,
556 "Print a type chain for a given symbol.\n\
557 For each node in a type chain, print the raw data for each member of\n\
558 the type structure, and the interpretation of the data.",
559 &maintenanceprintlist);
561 add_cmd ("symbols", class_maintenance, maintenance_print_symbols,
562 "Print dump of current symbol definitions.\n\
563 Entries in the full symbol table are dumped to file OUTFILE.\n\
564 If a SOURCE file is specified, dump only that file's symbols.",
565 &maintenanceprintlist);
567 add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols,
568 "Print dump of current minimal symbol definitions.\n\
569 Entries in the minimal symbol table are dumped to file OUTFILE.\n\
570 If a SOURCE file is specified, dump only that file's minimal symbols.",
571 &maintenanceprintlist);
573 add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols,
574 "Print dump of current partial symbol definitions.\n\
575 Entries in the partial symbol table are dumped to file OUTFILE.\n\
576 If a SOURCE file is specified, dump only that file's partial symbols.",
577 &maintenanceprintlist);
579 add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
580 "Print dump of current object file definitions.",
581 &maintenanceprintlist);
583 add_cmd ("statistics", class_maintenance, maintenance_print_statistics,
584 "Print statistics about internal gdb state.",
585 &maintenanceprintlist);
587 add_cmd ("architecture", class_maintenance, maintenance_print_architecture,
588 "Print the internal architecture configuration.\
589 Takes an optional file parameter.",
590 &maintenanceprintlist);
592 add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
593 "Check consistency of psymtabs and symtabs.",
594 &maintenancelist);
596 add_cmd ("translate-address", class_maintenance, maintenance_translate_address,
597 "Translate a section name and address to a symbol.",
598 &maintenancelist);
600 add_cmd ("deprecate", class_maintenance, maintenance_deprecate,
601 "Deprecate a command. Note that this is just in here so the \n\
602 testsuite can check the comamnd deprecator. You probably shouldn't use this,\n\
603 rather you should use the C function deprecate_cmd(). If you decide you \n\
604 want to use it: maintenance deprecate 'commandname' \"replacement\". The \n\
605 replacement is optional.", &maintenancelist);
607 add_cmd ("undeprecate", class_maintenance, maintenance_undeprecate,
608 "Undeprecate a command. Note that this is just in here so the \n\
609 testsuite can check the comamnd deprecator. You probably shouldn't use this,\n\
610 If you decide you want to use it: maintenance undeprecate 'commandname'",
611 &maintenancelist);
613 add_show_from_set (
614 add_set_cmd ("watchdog", class_maintenance, var_zinteger, (char *) &watchdog,
615 "Set watchdog timer.\n\
616 When non-zero, this timeout is used instead of waiting forever for a target to\n\
617 finish a low-level step or continue operation. If the specified amount of time\n\
618 passes without a response from the target, an error occurs.", &setlist),
619 &showlist);