gdb/maint.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Support for GDB maintenance commands.

  2.    Copyright (C) 1992-2015 Free Software Foundation, Inc.

  3.    Written by Fred Fish at Cygnus Support.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include <ctype.h>
  18. #include <signal.h>
  19. #include <sys/time.h>
  20. #include <time.h>
  21. #include "command.h"
  22. #include "gdbcmd.h"
  23. #include "symtab.h"
  24. #include "block.h"
  25. #include "gdbtypes.h"
  26. #include "demangle.h"
  27. #include "gdbcore.h"
  28. #include "expression.h"                /* For language.h */
  29. #include "language.h"
  30. #include "symfile.h"
  31. #include "objfiles.h"
  32. #include "value.h"
  33. #include "top.h"
  34. #include "timeval-utils.h"
  35. #include "maint.h"

  36. #include "cli/cli-decode.h"
  37. #include "cli/cli-utils.h"
  38. #include "cli/cli-setshow.h"

  39. extern void _initialize_maint_cmds (void);

  40. static void maintenance_command (char *, int);

  41. static void maintenance_internal_error (char *args, int from_tty);

  42. static void maintenance_demangle (char *, int);

  43. static void maintenance_time_display (char *, int);

  44. static void maintenance_space_display (char *, int);

  45. static void maintenance_info_command (char *, int);

  46. static void maintenance_info_sections (char *, int);

  47. static void maintenance_print_command (char *, int);

  48. static void maintenance_do_deprecate (char *, int);

  49. /* Set this to the maximum number of seconds to wait instead of waiting forever
  50.    in target_wait().  If this timer times out, then it generates an error and
  51.    the command is aborted.  This replaces most of the need for timeouts in the
  52.    GDB test suite, and makes it possible to distinguish between a hung target
  53.    and one with slow communications.  */

  54. int watchdog = 0;
  55. static void
  56. show_watchdog (struct ui_file *file, int from_tty,
  57.                struct cmd_list_element *c, const char *value)
  58. {
  59.   fprintf_filtered (file, _("Watchdog timer is %s.\n"), value);
  60. }

  61. /* Access the maintenance subcommands.  */

  62. static void
  63. maintenance_command (char *args, int from_tty)
  64. {
  65.   printf_unfiltered (_("\"maintenance\" must be followed by "
  66.                        "the name of a maintenance command.\n"));
  67.   help_list (maintenancelist, "maintenance ", all_commands, gdb_stdout);
  68. }

  69. #ifndef _WIN32
  70. static void
  71. maintenance_dump_me (char *args, int from_tty)
  72. {
  73.   if (query (_("Should GDB dump core? ")))
  74.     {
  75. #ifdef __DJGPP__
  76.       /* SIGQUIT by default is ignored, so use SIGABRT instead.  */
  77.       signal (SIGABRT, SIG_DFL);
  78.       kill (getpid (), SIGABRT);
  79. #else
  80.       signal (SIGQUIT, SIG_DFL);
  81.       kill (getpid (), SIGQUIT);
  82. #endif
  83.     }
  84. }
  85. #endif

  86. /* Stimulate the internal error mechanism that GDB uses when an
  87.    internal problem is detected.  Allows testing of the mechanism.
  88.    Also useful when the user wants to drop a core file but not exit
  89.    GDB.  */

  90. static void
  91. maintenance_internal_error (char *args, int from_tty)
  92. {
  93.   internal_error (__FILE__, __LINE__, "%s", (args == NULL ? "" : args));
  94. }

  95. /* Stimulate the internal error mechanism that GDB uses when an
  96.    internal problem is detected.  Allows testing of the mechanism.
  97.    Also useful when the user wants to drop a core file but not exit
  98.    GDB.  */

  99. static void
  100. maintenance_internal_warning (char *args, int from_tty)
  101. {
  102.   internal_warning (__FILE__, __LINE__, "%s", (args == NULL ? "" : args));
  103. }

  104. /* Stimulate the internal error mechanism that GDB uses when an
  105.    demangler problem is detected.  Allows testing of the mechanism.  */

  106. static void
  107. maintenance_demangler_warning (char *args, int from_tty)
  108. {
  109.   demangler_warning (__FILE__, __LINE__, "%s", (args == NULL ? "" : args));
  110. }

  111. /* Someday we should allow demangling for things other than just
  112.    explicit strings.  For example, we might want to be able to specify
  113.    the address of a string in either GDB's process space or the
  114.    debuggee's process space, and have gdb fetch and demangle that
  115.    string.  If we have a char* pointer "ptr" that points to a string,
  116.    we might want to be able to given just the name and have GDB
  117.    demangle and print what it points to, etc.  (FIXME)  */

  118. static void
  119. maintenance_demangle (char *args, int from_tty)
  120. {
  121.   char *demangled;

  122.   if (args == NULL || *args == '\0')
  123.     {
  124.       printf_unfiltered (_("\"maintenance demangle\" takes "
  125.                            "an argument to demangle.\n"));
  126.     }
  127.   else
  128.     {
  129.       demangled = language_demangle (current_language, args,
  130.                                      DMGL_ANSI | DMGL_PARAMS);
  131.       if (demangled != NULL)
  132.         {
  133.           printf_unfiltered ("%s\n", demangled);
  134.           xfree (demangled);
  135.         }
  136.       else
  137.         {
  138.           printf_unfiltered (_("Can't demangle \"%s\"\n"), args);
  139.         }
  140.     }
  141. }

  142. static void
  143. maintenance_time_display (char *args, int from_tty)
  144. {
  145.   if (args == NULL || *args == '\0')
  146.     printf_unfiltered (_("\"maintenance time\" takes a numeric argument.\n"));
  147.   else
  148.     set_per_command_time (strtol (args, NULL, 10));
  149. }

  150. static void
  151. maintenance_space_display (char *args, int from_tty)
  152. {
  153.   if (args == NULL || *args == '\0')
  154.     printf_unfiltered ("\"maintenance space\" takes a numeric argument.\n");
  155.   else
  156.     set_per_command_space (strtol (args, NULL, 10));
  157. }

  158. /* The "maintenance info" command is defined as a prefix, with
  159.    allow_unknown 0.  Therefore, its own definition is called only for
  160.    "maintenance info" with no args.  */

  161. static void
  162. maintenance_info_command (char *arg, int from_tty)
  163. {
  164.   printf_unfiltered (_("\"maintenance info\" must be followed "
  165.                        "by the name of an info command.\n"));
  166.   help_list (maintenanceinfolist, "maintenance info ", all_commands,
  167.              gdb_stdout);
  168. }

  169. /* Mini tokenizing lexer for 'maint info sections' command.  */

  170. static int
  171. match_substring (const char *string, const char *substr)
  172. {
  173.   int substr_len = strlen(substr);
  174.   const char *tok;

  175.   while ((tok = strstr (string, substr)) != NULL)
  176.     {
  177.       /* Got a partial match.  Is it a whole word?  */
  178.       if (tok == string
  179.           || tok[-1] == ' '
  180.           || tok[-1] == '\t')
  181.       {
  182.         /* Token is delimited at the front...  */
  183.         if (tok[substr_len] == ' '
  184.             || tok[substr_len] == '\t'
  185.             || tok[substr_len] == '\0')
  186.         {
  187.           /* Token is delimited at the rear.  Got a whole-word match.  */
  188.           return 1;
  189.         }
  190.       }
  191.       /* Token didn't match as a whole word.  Advance and try again.  */
  192.       string = tok + 1;
  193.     }
  194.   return 0;
  195. }

  196. static int
  197. match_bfd_flags (const char *string, flagword flags)
  198. {
  199.   if (flags & SEC_ALLOC)
  200.     if (match_substring (string, "ALLOC"))
  201.       return 1;
  202.   if (flags & SEC_LOAD)
  203.     if (match_substring (string, "LOAD"))
  204.       return 1;
  205.   if (flags & SEC_RELOC)
  206.     if (match_substring (string, "RELOC"))
  207.       return 1;
  208.   if (flags & SEC_READONLY)
  209.     if (match_substring (string, "READONLY"))
  210.       return 1;
  211.   if (flags & SEC_CODE)
  212.     if (match_substring (string, "CODE"))
  213.       return 1;
  214.   if (flags & SEC_DATA)
  215.     if (match_substring (string, "DATA"))
  216.       return 1;
  217.   if (flags & SEC_ROM)
  218.     if (match_substring (string, "ROM"))
  219.       return 1;
  220.   if (flags & SEC_CONSTRUCTOR)
  221.     if (match_substring (string, "CONSTRUCTOR"))
  222.       return 1;
  223.   if (flags & SEC_HAS_CONTENTS)
  224.     if (match_substring (string, "HAS_CONTENTS"))
  225.       return 1;
  226.   if (flags & SEC_NEVER_LOAD)
  227.     if (match_substring (string, "NEVER_LOAD"))
  228.       return 1;
  229.   if (flags & SEC_COFF_SHARED_LIBRARY)
  230.     if (match_substring (string, "COFF_SHARED_LIBRARY"))
  231.       return 1;
  232.   if (flags & SEC_IS_COMMON)
  233.     if (match_substring (string, "IS_COMMON"))
  234.       return 1;

  235.   return 0;
  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. static void
  266. maint_print_section_info (const char *name, flagword flags,
  267.                           CORE_ADDR addr, CORE_ADDR endaddr,
  268.                           unsigned long filepos, int addr_size)
  269. {
  270.   printf_filtered ("    %s", hex_string_custom (addr, addr_size));
  271.   printf_filtered ("->%s", hex_string_custom (endaddr, addr_size));
  272.   printf_filtered (" at %s",
  273.                    hex_string_custom ((unsigned long) filepos, 8));
  274.   printf_filtered (": %s", name);
  275.   print_bfd_flags (flags);
  276.   printf_filtered ("\n");
  277. }

  278. static void
  279. print_bfd_section_info (bfd *abfd,
  280.                         asection *asect,
  281.                         void *datum)
  282. {
  283.   flagword flags = bfd_get_section_flags (abfd, asect);
  284.   const char *name = bfd_section_name (abfd, asect);
  285.   const char *arg = datum;

  286.   if (arg == NULL || *arg == '\0'
  287.       || match_substring (arg, name)
  288.       || match_bfd_flags (arg, flags))
  289.     {
  290.       struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
  291.       int addr_size = gdbarch_addr_bit (gdbarch) / 8;
  292.       CORE_ADDR addr, endaddr;

  293.       addr = bfd_section_vma (abfd, asect);
  294.       endaddr = addr + bfd_section_size (abfd, asect);
  295.       printf_filtered (" [%d] ", gdb_bfd_section_index (abfd, asect));
  296.       maint_print_section_info (name, flags, addr, endaddr,
  297.                                 asect->filepos, addr_size);
  298.     }
  299. }

  300. static void
  301. print_objfile_section_info (bfd *abfd,
  302.                             struct obj_section *asect,
  303.                             const char *string)
  304. {
  305.   flagword flags = bfd_get_section_flags (abfd, asect->the_bfd_section);
  306.   const char *name = bfd_section_name (abfd, asect->the_bfd_section);

  307.   if (string == NULL || *string == '\0'
  308.       || match_substring (string, name)
  309.       || match_bfd_flags (string, flags))
  310.     {
  311.       struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
  312.       int addr_size = gdbarch_addr_bit (gdbarch) / 8;

  313.       maint_print_section_info (name, flags,
  314.                                 obj_section_addr (asect),
  315.                                 obj_section_endaddr (asect),
  316.                                 asect->the_bfd_section->filepos,
  317.                                 addr_size);
  318.     }
  319. }

  320. static void
  321. maintenance_info_sections (char *arg, int from_tty)
  322. {
  323.   if (exec_bfd)
  324.     {
  325.       printf_filtered (_("Exec file:\n"));
  326.       printf_filtered ("    `%s', ", bfd_get_filename (exec_bfd));
  327.       wrap_here ("        ");
  328.       printf_filtered (_("file type %s.\n"), bfd_get_target (exec_bfd));
  329.       if (arg && *arg && match_substring (arg, "ALLOBJ"))
  330.         {
  331.           struct objfile *ofile;
  332.           struct obj_section *osect;

  333.           /* Only this function cares about the 'ALLOBJ' argument;
  334.              if 'ALLOBJ' is the only argument, discard it rather than
  335.              passing it down to print_objfile_section_info (which
  336.              wouldn't know how to handle it).  */
  337.           if (strcmp (arg, "ALLOBJ") == 0)
  338.             arg = NULL;

  339.           ALL_OBJFILES (ofile)
  340.             {
  341.               printf_filtered (_("  Object file: %s\n"),
  342.                                bfd_get_filename (ofile->obfd));
  343.               ALL_OBJFILE_OSECTIONS (ofile, osect)
  344.                 {
  345.                   print_objfile_section_info (ofile->obfd, osect, arg);
  346.                 }
  347.             }
  348.         }
  349.       else
  350.         bfd_map_over_sections (exec_bfd, print_bfd_section_info, arg);
  351.     }

  352.   if (core_bfd)
  353.     {
  354.       printf_filtered (_("Core file:\n"));
  355.       printf_filtered ("    `%s', ", bfd_get_filename (core_bfd));
  356.       wrap_here ("        ");
  357.       printf_filtered (_("file type %s.\n"), bfd_get_target (core_bfd));
  358.       bfd_map_over_sections (core_bfd, print_bfd_section_info, arg);
  359.     }
  360. }

  361. static void
  362. maintenance_print_statistics (char *args, int from_tty)
  363. {
  364.   print_objfile_statistics ();
  365.   print_symbol_bcache_statistics ();
  366. }

  367. static void
  368. maintenance_print_architecture (char *args, int from_tty)
  369. {
  370.   struct gdbarch *gdbarch = get_current_arch ();

  371.   if (args == NULL)
  372.     gdbarch_dump (gdbarch, gdb_stdout);
  373.   else
  374.     {
  375.       struct cleanup *cleanups;
  376.       struct ui_file *file = gdb_fopen (args, "w");

  377.       if (file == NULL)
  378.         perror_with_name (_("maintenance print architecture"));
  379.       cleanups = make_cleanup_ui_file_delete (file);
  380.       gdbarch_dump (gdbarch, file);
  381.       do_cleanups (cleanups);
  382.     }
  383. }

  384. /* The "maintenance print" command is defined as a prefix, with
  385.    allow_unknown 0.  Therefore, its own definition is called only for
  386.    "maintenance print" with no args.  */

  387. static void
  388. maintenance_print_command (char *arg, int from_tty)
  389. {
  390.   printf_unfiltered (_("\"maintenance print\" must be followed "
  391.                        "by the name of a print command.\n"));
  392.   help_list (maintenanceprintlist, "maintenance print ", all_commands,
  393.              gdb_stdout);
  394. }

  395. /* The "maintenance translate-address" command converts a section and address
  396.    to a symbol.  This can be called in two ways:
  397.    maintenance translate-address <secname> <addr>
  398.    or   maintenance translate-address <addr>.  */

  399. static void
  400. maintenance_translate_address (char *arg, int from_tty)
  401. {
  402.   CORE_ADDR address;
  403.   struct obj_section *sect;
  404.   char *p;
  405.   struct bound_minimal_symbol sym;
  406.   struct objfile *objfile;

  407.   if (arg == NULL || *arg == 0)
  408.     error (_("requires argument (address or section + address)"));

  409.   sect = NULL;
  410.   p = arg;

  411.   if (!isdigit (*p))
  412.     {                                /* See if we have a valid section name.  */
  413.       while (*p && !isspace (*p))        /* Find end of section name.  */
  414.         p++;
  415.       if (*p == '\000')                /* End of command?  */
  416.         error (_("Need to specify <section-name> and <address>"));
  417.       *p++ = '\000';
  418.       p = skip_spaces (p);

  419.       ALL_OBJSECTIONS (objfile, sect)
  420.       {
  421.         if (strcmp (sect->the_bfd_section->name, arg) == 0)
  422.           break;
  423.       }

  424.       if (!objfile)
  425.         error (_("Unknown section %s."), arg);
  426.     }

  427.   address = parse_and_eval_address (p);

  428.   if (sect)
  429.     sym = lookup_minimal_symbol_by_pc_section (address, sect);
  430.   else
  431.     sym = lookup_minimal_symbol_by_pc (address);

  432.   if (sym.minsym)
  433.     {
  434.       const char *symbol_name = MSYMBOL_PRINT_NAME (sym.minsym);
  435.       const char *symbol_offset
  436.         = pulongest (address - BMSYMBOL_VALUE_ADDRESS (sym));

  437.       sect = MSYMBOL_OBJ_SECTION(sym.objfile, sym.minsym);
  438.       if (sect != NULL)
  439.         {
  440.           const char *section_name;
  441.           const char *obj_name;

  442.           gdb_assert (sect->the_bfd_section && sect->the_bfd_section->name);
  443.           section_name = sect->the_bfd_section->name;

  444.           gdb_assert (sect->objfile && objfile_name (sect->objfile));
  445.           obj_name = objfile_name (sect->objfile);

  446.           if (MULTI_OBJFILE_P ())
  447.             printf_filtered (_("%s + %s in section %s of %s\n"),
  448.                              symbol_name, symbol_offset,
  449.                              section_name, obj_name);
  450.           else
  451.             printf_filtered (_("%s + %s in section %s\n"),
  452.                              symbol_name, symbol_offset, section_name);
  453.         }
  454.       else
  455.         printf_filtered (_("%s + %s\n"), symbol_name, symbol_offset);
  456.     }
  457.   else if (sect)
  458.     printf_filtered (_("no symbol at %s:%s\n"),
  459.                      sect->the_bfd_section->name, hex_string (address));
  460.   else
  461.     printf_filtered (_("no symbol at %s\n"), hex_string (address));

  462.   return;
  463. }


  464. /* When a command is deprecated the user will be warned the first time
  465.    the command is used.  If possible, a replacement will be
  466.    offered.  */

  467. static void
  468. maintenance_deprecate (char *args, int from_tty)
  469. {
  470.   if (args == NULL || *args == '\0')
  471.     {
  472.       printf_unfiltered (_("\"maintenance deprecate\" takes an argument,\n\
  473. the command you want to deprecate, and optionally the replacement command\n\
  474. enclosed in quotes.\n"));
  475.     }

  476.   maintenance_do_deprecate (args, 1);

  477. }


  478. static void
  479. maintenance_undeprecate (char *args, int from_tty)
  480. {
  481.   if (args == NULL || *args == '\0')
  482.     {
  483.       printf_unfiltered (_("\"maintenance undeprecate\" takes an argument, \n\
  484. the command you want to undeprecate.\n"));
  485.     }

  486.   maintenance_do_deprecate (args, 0);

  487. }

  488. /* You really shouldn't be using this.  It is just for the testsuite.
  489.    Rather, you should use deprecate_cmd() when the command is created
  490.    in _initialize_blah().

  491.    This function deprecates a command and optionally assigns it a
  492.    replacement.  */

  493. static void
  494. maintenance_do_deprecate (char *text, int deprecate)
  495. {
  496.   struct cmd_list_element *alias = NULL;
  497.   struct cmd_list_element *prefix_cmd = NULL;
  498.   struct cmd_list_element *cmd = NULL;

  499.   char *start_ptr = NULL;
  500.   char *end_ptr = NULL;
  501.   int len;
  502.   char *replacement = NULL;

  503.   if (text == NULL)
  504.     return;

  505.   if (!lookup_cmd_composition (text, &alias, &prefix_cmd, &cmd))
  506.     {
  507.       printf_filtered (_("Can't find command '%s' to deprecate.\n"), text);
  508.       return;
  509.     }

  510.   if (deprecate)
  511.     {
  512.       /* Look for a replacement command.  */
  513.       start_ptr = strchr (text, '\"');
  514.       if (start_ptr != NULL)
  515.         {
  516.           start_ptr++;
  517.           end_ptr = strrchr (start_ptr, '\"');
  518.           if (end_ptr != NULL)
  519.             {
  520.               len = end_ptr - start_ptr;
  521.               start_ptr[len] = '\0';
  522.               replacement = xstrdup (start_ptr);
  523.             }
  524.         }
  525.     }

  526.   if (!start_ptr || !end_ptr)
  527.     replacement = NULL;


  528.   /* If they used an alias, we only want to deprecate the alias.

  529.      Note the MALLOCED_REPLACEMENT test.  If the command's replacement
  530.      string was allocated at compile time we don't want to free the
  531.      memory.  */
  532.   if (alias)
  533.     {
  534.       if (alias->malloced_replacement)
  535.         xfree ((char *) alias->replacement);

  536.       if (deprecate)
  537.         {
  538.           alias->deprecated_warn_user = 1;
  539.           alias->cmd_deprecated = 1;
  540.         }
  541.       else
  542.         {
  543.           alias->deprecated_warn_user = 0;
  544.           alias->cmd_deprecated = 0;
  545.         }
  546.       alias->replacement = replacement;
  547.       alias->malloced_replacement = 1;
  548.       return;
  549.     }
  550.   else if (cmd)
  551.     {
  552.       if (cmd->malloced_replacement)
  553.         xfree ((char *) cmd->replacement);

  554.       if (deprecate)
  555.         {
  556.           cmd->deprecated_warn_user = 1;
  557.           cmd->cmd_deprecated = 1;
  558.         }
  559.       else
  560.         {
  561.           cmd->deprecated_warn_user = 0;
  562.           cmd->cmd_deprecated = 0;
  563.         }
  564.       cmd->replacement = replacement;
  565.       cmd->malloced_replacement = 1;
  566.       return;
  567.     }
  568.   xfree (replacement);
  569. }

  570. /* Maintenance set/show framework.  */

  571. struct cmd_list_element *maintenance_set_cmdlist;
  572. struct cmd_list_element *maintenance_show_cmdlist;

  573. static void
  574. maintenance_set_cmd (char *args, int from_tty)
  575. {
  576.   printf_unfiltered (_("\"maintenance set\" must be followed "
  577.                        "by the name of a set command.\n"));
  578.   help_list (maintenance_set_cmdlist, "maintenance set ", all_commands,
  579.              gdb_stdout);
  580. }

  581. static void
  582. maintenance_show_cmd (char *args, int from_tty)
  583. {
  584.   cmd_show_list (maintenance_show_cmdlist, from_tty, "");
  585. }

  586. /* Profiling support.  */

  587. static int maintenance_profile_p;
  588. static void
  589. show_maintenance_profile_p (struct ui_file *file, int from_tty,
  590.                             struct cmd_list_element *c, const char *value)
  591. {
  592.   fprintf_filtered (file, _("Internal profiling is %s.\n"), value);
  593. }

  594. #ifdef HAVE__ETEXT
  595. extern char _etext;
  596. #define TEXTEND &_etext
  597. #elif defined (HAVE_ETEXT)
  598. extern char etext;
  599. #define TEXTEND &etext
  600. #endif

  601. #if defined (HAVE_MONSTARTUP) && defined (HAVE__MCLEANUP) && defined (TEXTEND)

  602. static int profiling_state;

  603. static void
  604. mcleanup_wrapper (void)
  605. {
  606.   extern void _mcleanup (void);

  607.   if (profiling_state)
  608.     _mcleanup ();
  609. }

  610. static void
  611. maintenance_set_profile_cmd (char *args, int from_tty,
  612.                              struct cmd_list_element *c)
  613. {
  614.   if (maintenance_profile_p == profiling_state)
  615.     return;

  616.   profiling_state = maintenance_profile_p;

  617.   if (maintenance_profile_p)
  618.     {
  619.       static int profiling_initialized;

  620.       extern void monstartup (unsigned long, unsigned long);
  621.       extern int main();

  622.       if (!profiling_initialized)
  623.         {
  624.           atexit (mcleanup_wrapper);
  625.           profiling_initialized = 1;
  626.         }

  627.       /* "main" is now always the first function in the text segment, so use
  628.          its address for monstartup.  */
  629.       monstartup ((unsigned long) &main, (unsigned long) TEXTEND);
  630.     }
  631.   else
  632.     {
  633.       extern void _mcleanup (void);

  634.       _mcleanup ();
  635.     }
  636. }
  637. #else
  638. static void
  639. maintenance_set_profile_cmd (char *args, int from_tty,
  640.                              struct cmd_list_element *c)
  641. {
  642.   error (_("Profiling support is not available on this system."));
  643. }
  644. #endif

  645. /* If nonzero, display time usage both at startup and for each command.  */

  646. static int per_command_time;

  647. /* If nonzero, display space usage both at startup and for each command.  */

  648. static int per_command_space;

  649. /* If nonzero, display basic symtab stats for each command.  */

  650. static int per_command_symtab;

  651. /* mt per-command commands.  */

  652. static struct cmd_list_element *per_command_setlist;
  653. static struct cmd_list_element *per_command_showlist;

  654. /* Records a run time and space usage to be used as a base for
  655.    reporting elapsed time or change in space.  */

  656. struct cmd_stats
  657. {
  658.   /* Zero if the saved time is from the beginning of GDB execution.
  659.      One if from the beginning of an individual command execution.  */
  660.   int msg_type;
  661.   /* Track whether the stat was enabled at the start of the command
  662.      so that we can avoid printing anything if it gets turned on by
  663.      the current command.  */
  664.   int time_enabled : 1;
  665.   int space_enabled : 1;
  666.   int symtab_enabled : 1;
  667.   long start_cpu_time;
  668.   struct timeval start_wall_time;
  669.   long start_space;
  670.   /* Total number of symtabs (over all objfiles).  */
  671.   int start_nr_symtabs;
  672.   /* A count of the compunits.  */
  673.   int start_nr_compunit_symtabs;
  674.   /* Total number of blocks.  */
  675.   int start_nr_blocks;
  676. };

  677. /* Set whether to display time statistics to NEW_VALUE
  678.    (non-zero means true).  */

  679. void
  680. set_per_command_time (int new_value)
  681. {
  682.   per_command_time = new_value;
  683. }

  684. /* Set whether to display space statistics to NEW_VALUE
  685.    (non-zero means true).  */

  686. void
  687. set_per_command_space (int new_value)
  688. {
  689.   per_command_space = new_value;
  690. }

  691. /* Count the number of symtabs and blocks.  */

  692. static void
  693. count_symtabs_and_blocks (int *nr_symtabs_ptr, int *nr_compunit_symtabs_ptr,
  694.                           int *nr_blocks_ptr)
  695. {
  696.   struct objfile *o;
  697.   struct compunit_symtab *cu;
  698.   struct symtab *s;
  699.   int nr_symtabs = 0;
  700.   int nr_compunit_symtabs = 0;
  701.   int nr_blocks = 0;

  702.   /* When collecting statistics during startup, this is called before
  703.      pretty much anything in gdb has been initialized, and thus
  704.      current_program_space may be NULL.  */
  705.   if (current_program_space != NULL)
  706.     {
  707.       ALL_COMPUNITS (o, cu)
  708.         {
  709.           ++nr_compunit_symtabs;
  710.           nr_blocks += BLOCKVECTOR_NBLOCKS (COMPUNIT_BLOCKVECTOR (cu));
  711.           ALL_COMPUNIT_FILETABS (cu, s)
  712.             ++nr_symtabs;
  713.         }
  714.     }

  715.   *nr_symtabs_ptr = nr_symtabs;
  716.   *nr_compunit_symtabs_ptr = nr_compunit_symtabs;
  717.   *nr_blocks_ptr = nr_blocks;
  718. }

  719. /* As indicated by display_time and display_space, report GDB's elapsed time
  720.    and space usage from the base time and space provided in ARG, which
  721.    must be a pointer to a struct cmd_stat.  This function is intended
  722.    to be called as a cleanup.  */

  723. static void
  724. report_command_stats (void *arg)
  725. {
  726.   struct cmd_stats *start_stats = (struct cmd_stats *) arg;
  727.   int msg_type = start_stats->msg_type;

  728.   if (start_stats->time_enabled && per_command_time)
  729.     {
  730.       long cmd_time = get_run_time () - start_stats->start_cpu_time;
  731.       struct timeval now_wall_time, delta_wall_time, wait_time;

  732.       gettimeofday (&now_wall_time, NULL);
  733.       timeval_sub (&delta_wall_time,
  734.                    &now_wall_time, &start_stats->start_wall_time);

  735.       /* Subtract time spend in prompt_for_continue from walltime.  */
  736.       wait_time = get_prompt_for_continue_wait_time ();
  737.       timeval_sub (&delta_wall_time, &delta_wall_time, &wait_time);

  738.       printf_unfiltered (msg_type == 0
  739.                          ? _("Startup time: %ld.%06ld (cpu), %ld.%06ld (wall)\n")
  740.                          : _("Command execution time: %ld.%06ld (cpu), %ld.%06ld (wall)\n"),
  741.                          cmd_time / 1000000, cmd_time % 1000000,
  742.                          (long) delta_wall_time.tv_sec,
  743.                          (long) delta_wall_time.tv_usec);
  744.     }

  745.   if (start_stats->space_enabled && per_command_space)
  746.     {
  747. #ifdef HAVE_SBRK
  748.       char *lim = (char *) sbrk (0);

  749.       long space_now = lim - lim_at_start;
  750.       long space_diff = space_now - start_stats->start_space;

  751.       printf_unfiltered (msg_type == 0
  752.                          ? _("Space used: %ld (%s%ld during startup)\n")
  753.                          : _("Space used: %ld (%s%ld for this command)\n"),
  754.                          space_now,
  755.                          (space_diff >= 0 ? "+" : ""),
  756.                          space_diff);
  757. #endif
  758.     }

  759.   if (start_stats->symtab_enabled && per_command_symtab)
  760.     {
  761.       int nr_symtabs, nr_compunit_symtabs, nr_blocks;

  762.       count_symtabs_and_blocks (&nr_symtabs, &nr_compunit_symtabs, &nr_blocks);
  763.       printf_unfiltered (_("#symtabs: %d (+%d),"
  764.                            " #compunits: %d (+%d),"
  765.                            " #blocks: %d (+%d)\n"),
  766.                          nr_symtabs,
  767.                          nr_symtabs - start_stats->start_nr_symtabs,
  768.                          nr_compunit_symtabs,
  769.                          (nr_compunit_symtabs
  770.                           - start_stats->start_nr_compunit_symtabs),
  771.                          nr_blocks,
  772.                          nr_blocks - start_stats->start_nr_blocks);
  773.     }
  774. }

  775. /* Create a cleanup that reports time and space used since its creation.
  776.    MSG_TYPE is zero for gdb startup, otherwise it is one(1) to report
  777.    data for individual commands.  */

  778. struct cleanup *
  779. make_command_stats_cleanup (int msg_type)
  780. {
  781.   struct cmd_stats *new_stat;

  782.   /* Early exit if we're not reporting any stats.  It can be expensive to
  783.      compute the pre-command values so don't collect them at all if we're
  784.      not reporting stats.  Alas this doesn't work in the startup case because
  785.      we don't know yet whether we will be reporting the stats.  For the
  786.      startup case collect the data anyway (it should be cheap at this point),
  787.      and leave it to the reporter to decide whether to print them.  */
  788.   if (msg_type != 0
  789.       && !per_command_time
  790.       && !per_command_space
  791.       && !per_command_symtab)
  792.     return make_cleanup (null_cleanup, 0);

  793.   new_stat = XCNEW (struct cmd_stats);

  794.   new_stat->msg_type = msg_type;

  795.   if (msg_type == 0 || per_command_space)
  796.     {
  797. #ifdef HAVE_SBRK
  798.       char *lim = (char *) sbrk (0);
  799.       new_stat->start_space = lim - lim_at_start;
  800.       new_stat->space_enabled = 1;
  801. #endif
  802.     }

  803.   if (msg_type == 0 || per_command_time)
  804.     {
  805.       new_stat->start_cpu_time = get_run_time ();
  806.       gettimeofday (&new_stat->start_wall_time, NULL);
  807.       new_stat->time_enabled = 1;
  808.     }

  809.   if (msg_type == 0 || per_command_symtab)
  810.     {
  811.       int nr_symtabs, nr_compunit_symtabs, nr_blocks;

  812.       count_symtabs_and_blocks (&nr_symtabs, &nr_compunit_symtabs, &nr_blocks);
  813.       new_stat->start_nr_symtabs = nr_symtabs;
  814.       new_stat->start_nr_compunit_symtabs = nr_compunit_symtabs;
  815.       new_stat->start_nr_blocks = nr_blocks;
  816.       new_stat->symtab_enabled = 1;
  817.     }

  818.   /* Initalize timer to keep track of how long we waited for the user.  */
  819.   reset_prompt_for_continue_wait_time ();

  820.   return make_cleanup_dtor (report_command_stats, new_stat, xfree);
  821. }

  822. /* Handle unknown "mt set per-command" arguments.
  823.    In this case have "mt set per-command on|off" affect every setting.  */

  824. static void
  825. set_per_command_cmd (char *args, int from_tty)
  826. {
  827.   struct cmd_list_element *list;
  828.   size_t length;
  829.   int val;

  830.   val = parse_cli_boolean_value (args);
  831.   if (val < 0)
  832.     error (_("Bad value for 'mt set per-command no'."));

  833.   for (list = per_command_setlist; list != NULL; list = list->next)
  834.     if (list->var_type == var_boolean)
  835.       {
  836.         gdb_assert (list->type == set_cmd);
  837.         do_set_command (args, from_tty, list);
  838.       }
  839. }

  840. /* Command "show per-command" displays summary of all the current
  841.    "show per-command " settings.  */

  842. static void
  843. show_per_command_cmd (char *args, int from_tty)
  844. {
  845.   cmd_show_list (per_command_showlist, from_tty, "");
  846. }

  847. void
  848. _initialize_maint_cmds (void)
  849. {
  850.   add_prefix_cmd ("maintenance", class_maintenance, maintenance_command, _("\
  851. Commands for use by GDB maintainers.\n\
  852. Includes commands to dump specific internal GDB structures in\n\
  853. a human readable form, to cause GDB to deliberately dump core,\n\
  854. to test internal functions such as the C++/ObjC demangler, etc."),
  855.                   &maintenancelist, "maintenance ", 0,
  856.                   &cmdlist);

  857.   add_com_alias ("mt", "maintenance", class_maintenance, 1);

  858.   add_prefix_cmd ("info", class_maintenance, maintenance_info_command, _("\
  859. Commands for showing internal info about the program being debugged."),
  860.                   &maintenanceinfolist, "maintenance info ", 0,
  861.                   &maintenancelist);
  862.   add_alias_cmd ("i", "info", class_maintenance, 1, &maintenancelist);

  863.   add_cmd ("sections", class_maintenance, maintenance_info_sections, _("\
  864. List the BFD sections of the exec and core files. \n\
  865. Arguments may be any combination of:\n\
  866.         [one or more section names]\n\
  867.         ALLOC LOAD RELOC READONLY CODE DATA ROM CONSTRUCTOR\n\
  868.         HAS_CONTENTS NEVER_LOAD COFF_SHARED_LIBRARY IS_COMMON\n\
  869. Sections matching any argument will be listed (no argument\n\
  870. implies all sections).  In addition, the special argument\n\
  871.         ALLOBJ\n\
  872. lists all sections from all object files, including shared libraries."),
  873.            &maintenanceinfolist);

  874.   add_prefix_cmd ("print", class_maintenance, maintenance_print_command,
  875.                   _("Maintenance command for printing GDB internal state."),
  876.                   &maintenanceprintlist, "maintenance print ", 0,
  877.                   &maintenancelist);

  878.   add_prefix_cmd ("set", class_maintenance, maintenance_set_cmd, _("\
  879. Set GDB internal variables used by the GDB maintainer.\n\
  880. Configure variables internal to GDB that aid in GDB's maintenance"),
  881.                   &maintenance_set_cmdlist, "maintenance set ",
  882.                   0/*allow-unknown*/,
  883.                   &maintenancelist);

  884.   add_prefix_cmd ("show", class_maintenance, maintenance_show_cmd, _("\
  885. Show GDB internal variables used by the GDB maintainer.\n\
  886. Configure variables internal to GDB that aid in GDB's maintenance"),
  887.                   &maintenance_show_cmdlist, "maintenance show ",
  888.                   0/*allow-unknown*/,
  889.                   &maintenancelist);

  890. #ifndef _WIN32
  891.   add_cmd ("dump-me", class_maintenance, maintenance_dump_me, _("\
  892. Get fatal error; make debugger dump its core.\n\
  893. GDB sets its handling of SIGQUIT back to SIG_DFL and then sends\n\
  894. itself a SIGQUIT signal."),
  895.            &maintenancelist);
  896. #endif

  897.   add_cmd ("internal-error", class_maintenance,
  898.            maintenance_internal_error, _("\
  899. Give GDB an internal error.\n\
  900. Cause GDB to behave as if an internal error was detected."),
  901.            &maintenancelist);

  902.   add_cmd ("internal-warning", class_maintenance,
  903.            maintenance_internal_warning, _("\
  904. Give GDB an internal warning.\n\
  905. Cause GDB to behave as if an internal warning was reported."),
  906.            &maintenancelist);

  907.   add_cmd ("demangler-warning", class_maintenance,
  908.            maintenance_demangler_warning, _("\
  909. Give GDB a demangler warning.\n\
  910. Cause GDB to behave as if a demangler warning was reported."),
  911.            &maintenancelist);

  912.   add_cmd ("demangle", class_maintenance, maintenance_demangle, _("\
  913. Demangle a C++/ObjC mangled name.\n\
  914. Call internal GDB demangler routine to demangle a C++ link name\n\
  915. and prints the result."),
  916.            &maintenancelist);

  917.   add_prefix_cmd ("per-command", class_maintenance, set_per_command_cmd, _("\
  918. Per-command statistics settings."),
  919.                     &per_command_setlist, "set per-command ",
  920.                     1/*allow-unknown*/, &maintenance_set_cmdlist);

  921.   add_prefix_cmd ("per-command", class_maintenance, show_per_command_cmd, _("\
  922. Show per-command statistics settings."),
  923.                     &per_command_showlist, "show per-command ",
  924.                     0/*allow-unknown*/, &maintenance_show_cmdlist);

  925.   add_setshow_boolean_cmd ("time", class_maintenance,
  926.                            &per_command_time, _("\
  927. Set whether to display per-command execution time."), _("\
  928. Show whether to display per-command execution time."),
  929.                            _("\
  930. If enabled, the execution time for each command will be\n\
  931. displayed following the command's output."),
  932.                            NULL, NULL,
  933.                            &per_command_setlist, &per_command_showlist);

  934.   add_setshow_boolean_cmd ("space", class_maintenance,
  935.                            &per_command_space, _("\
  936. Set whether to display per-command space usage."), _("\
  937. Show whether to display per-command space usage."),
  938.                            _("\
  939. If enabled, the space usage for each command will be\n\
  940. displayed following the command's output."),
  941.                            NULL, NULL,
  942.                            &per_command_setlist, &per_command_showlist);

  943.   add_setshow_boolean_cmd ("symtab", class_maintenance,
  944.                            &per_command_symtab, _("\
  945. Set whether to display per-command symtab statistics."), _("\
  946. Show whether to display per-command symtab statistics."),
  947.                            _("\
  948. If enabled, the basic symtab statistics for each command will be\n\
  949. displayed following the command's output."),
  950.                            NULL, NULL,
  951.                            &per_command_setlist, &per_command_showlist);

  952.   /* This is equivalent to "mt set per-command time on".
  953.      Kept because some people are used to typing "mt time 1".  */
  954.   add_cmd ("time", class_maintenance, maintenance_time_display, _("\
  955. Set the display of time usage.\n\
  956. If nonzero, will cause the execution time for each command to be\n\
  957. displayed, following the command's output."),
  958.            &maintenancelist);

  959.   /* This is equivalent to "mt set per-command space on".
  960.      Kept because some people are used to typing "mt space 1".  */
  961.   add_cmd ("space", class_maintenance, maintenance_space_display, _("\
  962. Set the display of space usage.\n\
  963. If nonzero, will cause the execution space for each command to be\n\
  964. displayed, following the command's output."),
  965.            &maintenancelist);

  966.   add_cmd ("type", class_maintenance, maintenance_print_type, _("\
  967. Print a type chain for a given symbol.\n\
  968. For each node in a type chain, print the raw data for each member of\n\
  969. the type structure, and the interpretation of the data."),
  970.            &maintenanceprintlist);

  971.   add_cmd ("statistics", class_maintenance, maintenance_print_statistics,
  972.            _("Print statistics about internal gdb state."),
  973.            &maintenanceprintlist);

  974.   add_cmd ("architecture", class_maintenance,
  975.            maintenance_print_architecture, _("\
  976. Print the internal architecture configuration.\n\
  977. Takes an optional file parameter."),
  978.            &maintenanceprintlist);

  979.   add_cmd ("translate-address", class_maintenance,
  980.            maintenance_translate_address,
  981.            _("Translate a section name and address to a symbol."),
  982.            &maintenancelist);

  983.   add_cmd ("deprecate", class_maintenance, maintenance_deprecate, _("\
  984. Deprecate a command.  Note that this is just in here so the \n\
  985. testsuite can check the command deprecator. You probably shouldn't use this,\n\
  986. rather you should use the C function deprecate_cmd().  If you decide you \n\
  987. want to use it: maintenance deprecate 'commandname' \"replacement\". The \n\
  988. replacement is optional."), &maintenancelist);

  989.   add_cmd ("undeprecate", class_maintenance, maintenance_undeprecate, _("\
  990. Undeprecate a command.  Note that this is just in here so the \n\
  991. testsuite can check the command deprecator. You probably shouldn't use this,\n\
  992. If you decide you want to use it: maintenance undeprecate 'commandname'"),
  993.            &maintenancelist);

  994.   add_setshow_zinteger_cmd ("watchdog", class_maintenance, &watchdog, _("\
  995. Set watchdog timer."), _("\
  996. Show watchdog timer."), _("\
  997. When non-zero, this timeout is used instead of waiting forever for a target\n\
  998. to finish a low-level step or continue operation.  If the specified amount\n\
  999. of time passes without a response from the target, an error occurs."),
  1000.                             NULL,
  1001.                             show_watchdog,
  1002.                             &setlist, &showlist);

  1003.   add_setshow_boolean_cmd ("profile", class_maintenance,
  1004.                            &maintenance_profile_p, _("\
  1005. Set internal profiling."), _("\
  1006. Show internal profiling."), _("\
  1007. When enabled GDB is profiled."),
  1008.                            maintenance_set_profile_cmd,
  1009.                            show_maintenance_profile_p,
  1010.                            &maintenance_set_cmdlist,
  1011.                            &maintenance_show_cmdlist);
  1012. }