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