gdb/cli/cli-cmds.c - gdb

Global variables defined

Functions defined

Source code

  1. /* GDB CLI commands.

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

  3.    This file is part of GDB.

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

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

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

  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "dyn-string.h"
  17. #include "readline/readline.h"
  18. #include "readline/tilde.h"
  19. #include "completer.h"
  20. #include "target.h"        /* For baud_rate, remote_debug and remote_timeout.  */
  21. #include "gdb_wait.h"        /* For shell escape implementation.  */
  22. #include "gdb_regex.h"        /* Used by apropos_command.  */
  23. #include "gdb_vfork.h"
  24. #include "linespec.h"
  25. #include "expression.h"
  26. #include "frame.h"
  27. #include "value.h"
  28. #include "language.h"
  29. #include "filenames.h"        /* For DOSish file names.  */
  30. #include "objfiles.h"
  31. #include "source.h"
  32. #include "disasm.h"
  33. #include "tracepoint.h"
  34. #include "filestuff.h"

  35. #include "ui-out.h"

  36. #include "top.h"
  37. #include "cli/cli-decode.h"
  38. #include "cli/cli-script.h"
  39. #include "cli/cli-setshow.h"
  40. #include "cli/cli-cmds.h"
  41. #include "cli/cli-utils.h"

  42. #include "extension.h"

  43. #ifdef TUI
  44. #include "tui/tui.h"        /* For tui_active et.al.  */
  45. #endif

  46. #include <fcntl.h>

  47. /* Prototypes for local command functions */

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

  49. static void echo_command (char *, int);

  50. static void pwd_command (char *, int);

  51. static void show_version (char *, int);

  52. static void help_command (char *, int);

  53. static void show_command (char *, int);

  54. static void info_command (char *, int);

  55. static void show_debug (char *, int);

  56. static void set_debug (char *, int);

  57. static void show_user (char *, int);

  58. static void make_command (char *, int);

  59. static void shell_escape (char *, int);

  60. static void edit_command (char *, int);

  61. static void list_command (char *, int);

  62. /* Prototypes for local utility functions */

  63. static void ambiguous_line_spec (struct symtabs_and_lines *);

  64. static void filter_sals (struct symtabs_and_lines *);


  65. /* Limit the call depth of user-defined commands */
  66. unsigned int max_user_call_depth;

  67. /* Define all cmd_list_elements.  */

  68. /* Chain containing all defined commands.  */

  69. struct cmd_list_element *cmdlist;

  70. /* Chain containing all defined info subcommands.  */

  71. struct cmd_list_element *infolist;

  72. /* Chain containing all defined enable subcommands.  */

  73. struct cmd_list_element *enablelist;

  74. /* Chain containing all defined disable subcommands.  */

  75. struct cmd_list_element *disablelist;

  76. /* Chain containing all defined stop subcommands.  */

  77. struct cmd_list_element *stoplist;

  78. /* Chain containing all defined delete subcommands.  */

  79. struct cmd_list_element *deletelist;

  80. /* Chain containing all defined detach subcommands.  */

  81. struct cmd_list_element *detachlist;

  82. /* Chain containing all defined kill subcommands.  */

  83. struct cmd_list_element *killlist;

  84. /* Chain containing all defined set subcommands */

  85. struct cmd_list_element *setlist;

  86. /* Chain containing all defined unset subcommands */

  87. struct cmd_list_element *unsetlist;

  88. /* Chain containing all defined show subcommands.  */

  89. struct cmd_list_element *showlist;

  90. /* Chain containing all defined \"set history\".  */

  91. struct cmd_list_element *sethistlist;

  92. /* Chain containing all defined \"show history\".  */

  93. struct cmd_list_element *showhistlist;

  94. /* Chain containing all defined \"unset history\".  */

  95. struct cmd_list_element *unsethistlist;

  96. /* Chain containing all defined maintenance subcommands.  */

  97. struct cmd_list_element *maintenancelist;

  98. /* Chain containing all defined "maintenance info" subcommands.  */

  99. struct cmd_list_element *maintenanceinfolist;

  100. /* Chain containing all defined "maintenance print" subcommands.  */

  101. struct cmd_list_element *maintenanceprintlist;

  102. struct cmd_list_element *setprintlist;

  103. struct cmd_list_element *showprintlist;

  104. struct cmd_list_element *setdebuglist;

  105. struct cmd_list_element *showdebuglist;

  106. struct cmd_list_element *setchecklist;

  107. struct cmd_list_element *showchecklist;

  108. /* Command tracing state.  */

  109. int source_verbose = 0;
  110. int trace_commands = 0;

  111. /* 'script-extension' option support.  */

  112. static const char script_ext_off[] = "off";
  113. static const char script_ext_soft[] = "soft";
  114. static const char script_ext_strict[] = "strict";

  115. static const char *const script_ext_enums[] = {
  116.   script_ext_off,
  117.   script_ext_soft,
  118.   script_ext_strict,
  119.   NULL
  120. };

  121. static const char *script_ext_mode = script_ext_soft;

  122. /* Utility used everywhere when at least one argument is needed and
  123.    none is supplied.  */

  124. void
  125. error_no_arg (const char *why)
  126. {
  127.   error (_("Argument required (%s)."), why);
  128. }

  129. /* The "info" command is defined as a prefix, with allow_unknown = 0.
  130.    Therefore, its own definition is called only for "info" with no
  131.    args.  */

  132. static void
  133. info_command (char *arg, int from_tty)
  134. {
  135.   printf_unfiltered (_("\"info\" must be followed by "
  136.                        "the name of an info command.\n"));
  137.   help_list (infolist, "info ", all_commands, gdb_stdout);
  138. }

  139. /* The "show" command with no arguments shows all the settings.  */

  140. static void
  141. show_command (char *arg, int from_tty)
  142. {
  143.   cmd_show_list (showlist, from_tty, "");
  144. }

  145. /* Provide documentation on command or list given by COMMAND.  FROM_TTY
  146.    is ignored.  */

  147. static void
  148. help_command (char *command, int from_tty)
  149. {
  150.   help_cmd (command, gdb_stdout);
  151. }

  152. /* The "complete" command is used by Emacs to implement completion.  */

  153. static void
  154. complete_command (char *arg, int from_tty)
  155. {
  156.   int argpoint;
  157.   char *point, *arg_prefix;
  158.   VEC (char_ptr) *completions;

  159.   dont_repeat ();

  160.   if (arg == NULL)
  161.     arg = "";
  162.   argpoint = strlen (arg);

  163.   /* complete_line assumes that its first argument is somewhere
  164.      within, and except for filenames at the beginning of, the word to
  165.      be completed.  The following crude imitation of readline's
  166.      word-breaking tries to accomodate this.  */
  167.   point = arg + argpoint;
  168.   while (point > arg)
  169.     {
  170.       if (strchr (rl_completer_word_break_characters, point[-1]) != 0)
  171.         break;
  172.       point--;
  173.     }

  174.   arg_prefix = alloca (point - arg + 1);
  175.   memcpy (arg_prefix, arg, point - arg);
  176.   arg_prefix[point - arg] = 0;

  177.   completions = complete_line (point, arg, argpoint);

  178.   if (completions)
  179.     {
  180.       int ix, size = VEC_length (char_ptr, completions);
  181.       char *item, *prev = NULL;

  182.       qsort (VEC_address (char_ptr, completions), size,
  183.              sizeof (char *), compare_strings);

  184.       /* We do extra processing here since we only want to print each
  185.          unique item once.  */
  186.       for (ix = 0; VEC_iterate (char_ptr, completions, ix, item); ++ix)
  187.         {
  188.           if (prev == NULL || strcmp (item, prev) != 0)
  189.             {
  190.               printf_unfiltered ("%s%s\n", arg_prefix, item);
  191.               xfree (prev);
  192.               prev = item;
  193.             }
  194.           else
  195.             xfree (item);
  196.         }

  197.       xfree (prev);
  198.       VEC_free (char_ptr, completions);
  199.     }
  200. }

  201. int
  202. is_complete_command (struct cmd_list_element *c)
  203. {
  204.   return cmd_cfunc_eq (c, complete_command);
  205. }

  206. static void
  207. show_version (char *args, int from_tty)
  208. {
  209.   print_gdb_version (gdb_stdout);
  210.   printf_filtered ("\n");
  211. }

  212. static void
  213. show_configuration (char *args, int from_tty)
  214. {
  215.   print_gdb_configuration (gdb_stdout);
  216. }

  217. /* Handle the quit command.  */

  218. void
  219. quit_command (char *args, int from_tty)
  220. {
  221.   if (!quit_confirm ())
  222.     error (_("Not confirmed."));

  223.   query_if_trace_running (from_tty);

  224.   quit_force (args, from_tty);
  225. }

  226. static void
  227. pwd_command (char *args, int from_tty)
  228. {
  229.   if (args)
  230.     error (_("The \"pwd\" command does not take an argument: %s"), args);
  231.   if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
  232.     error (_("Error finding name of working directory: %s"),
  233.            safe_strerror (errno));

  234.   if (strcmp (gdb_dirbuf, current_directory) != 0)
  235.     printf_unfiltered (_("Working directory %s\n (canonically %s).\n"),
  236.                        current_directory, gdb_dirbuf);
  237.   else
  238.     printf_unfiltered (_("Working directory %s.\n"), current_directory);
  239. }

  240. void
  241. cd_command (char *dir, int from_tty)
  242. {
  243.   int len;
  244.   /* Found something other than leading repetitions of "/..".  */
  245.   int found_real_path;
  246.   char *p;
  247.   struct cleanup *cleanup;

  248.   /* If the new directory is absolute, repeat is a no-op; if relative,
  249.      repeat might be useful but is more likely to be a mistake.  */
  250.   dont_repeat ();

  251.   if (dir == 0)
  252.     dir = "~";

  253.   dir = tilde_expand (dir);
  254.   cleanup = make_cleanup (xfree, dir);

  255.   if (chdir (dir) < 0)
  256.     perror_with_name (dir);

  257. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  258.   /* There's too much mess with DOSish names like "d:", "d:.",
  259.      "d:./foo" etc.  Instead of having lots of special #ifdef'ed code,
  260.      simply get the canonicalized name of the current directory.  */
  261.   dir = getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
  262. #endif

  263.   len = strlen (dir);
  264.   if (IS_DIR_SEPARATOR (dir[len - 1]))
  265.     {
  266.       /* Remove the trailing slash unless this is a root directory
  267.          (including a drive letter on non-Unix systems).  */
  268.       if (!(len == 1)                /* "/" */
  269. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  270.           && !(len == 3 && dir[1] == ':') /* "d:/" */
  271. #endif
  272.           )
  273.         len--;
  274.     }

  275.   dir = savestring (dir, len);
  276.   if (IS_ABSOLUTE_PATH (dir))
  277.     current_directory = dir;
  278.   else
  279.     {
  280.       if (IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1]))
  281.         current_directory = concat (current_directory, dir, (char *)NULL);
  282.       else
  283.         current_directory = concat (current_directory, SLASH_STRING,
  284.                                     dir, (char *)NULL);
  285.       xfree (dir);
  286.     }

  287.   /* Now simplify any occurrences of `.' and `..' in the pathname.  */

  288.   found_real_path = 0;
  289.   for (p = current_directory; *p;)
  290.     {
  291.       if (IS_DIR_SEPARATOR (p[0]) && p[1] == '.'
  292.           && (p[2] == 0 || IS_DIR_SEPARATOR (p[2])))
  293.         memmove (p, p + 2, strlen (p + 2) + 1);
  294.       else if (IS_DIR_SEPARATOR (p[0]) && p[1] == '.' && p[2] == '.'
  295.                && (p[3] == 0 || IS_DIR_SEPARATOR (p[3])))
  296.         {
  297.           if (found_real_path)
  298.             {
  299.               /* Search backwards for the directory just before the "/.."
  300.                  and obliterate it and the "/..".  */
  301.               char *q = p;

  302.               while (q != current_directory && !IS_DIR_SEPARATOR (q[-1]))
  303.                 --q;

  304.               if (q == current_directory)
  305.                 /* current_directory is
  306.                    a relative pathname ("can't happen"--leave it alone).  */
  307.                 ++p;
  308.               else
  309.                 {
  310.                   memmove (q - 1, p + 3, strlen (p + 3) + 1);
  311.                   p = q - 1;
  312.                 }
  313.             }
  314.           else
  315.             /* We are dealing with leading repetitions of "/..", for
  316.                example "/../..", which is the Mach super-root.  */
  317.             p += 3;
  318.         }
  319.       else
  320.         {
  321.           found_real_path = 1;
  322.           ++p;
  323.         }
  324.     }

  325.   forget_cached_source_info ();

  326.   if (from_tty)
  327.     pwd_command ((char *) 0, 1);

  328.   do_cleanups (cleanup);
  329. }

  330. /* Show the current value of the 'script-extension' option.  */

  331. static void
  332. show_script_ext_mode (struct ui_file *file, int from_tty,
  333.                      struct cmd_list_element *c, const char *value)
  334. {
  335.   fprintf_filtered (file,
  336.                     _("Script filename extension recognition is \"%s\".\n"),
  337.                     value);
  338. }

  339. /* Try to open SCRIPT_FILE.
  340.    If successful, the full path name is stored in *FULL_PATHP,
  341.    the stream is stored in *STREAMP, and return 1.
  342.    The caller is responsible for freeing *FULL_PATHP.
  343.    If not successful, return 0; errno is set for the last file
  344.    we tried to open.

  345.    If SEARCH_PATH is non-zero, and the file isn't found in cwd,
  346.    search for it in the source search path.  */

  347. int
  348. find_and_open_script (const char *script_file, int search_path,
  349.                       FILE **streamp, char **full_pathp)
  350. {
  351.   char *file;
  352.   int fd;
  353.   struct cleanup *old_cleanups;
  354.   int search_flags = OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH;

  355.   file = tilde_expand (script_file);
  356.   old_cleanups = make_cleanup (xfree, file);

  357.   if (search_path)
  358.     search_flags |= OPF_SEARCH_IN_PATH;

  359.   /* Search for and open 'file' on the search path used for source
  360.      files.  Put the full location in *FULL_PATHP.  */
  361.   fd = openp (source_path, search_flags,
  362.               file, O_RDONLY, full_pathp);

  363.   if (fd == -1)
  364.     {
  365.       int save_errno = errno;
  366.       do_cleanups (old_cleanups);
  367.       errno = save_errno;
  368.       return 0;
  369.     }

  370.   do_cleanups (old_cleanups);

  371.   *streamp = fdopen (fd, FOPEN_RT);
  372.   if (*streamp == NULL)
  373.     {
  374.       int save_errno = errno;

  375.       close (fd);
  376.       if (full_pathp)
  377.         xfree (*full_pathp);
  378.       errno = save_errno;
  379.       return 0;
  380.     }

  381.   return 1;
  382. }

  383. /* Load script FILE, which has already been opened as STREAM.  */

  384. static void
  385. source_script_from_stream (FILE *stream, const char *file)
  386. {
  387.   if (script_ext_mode != script_ext_off)
  388.     {
  389.       const struct extension_language_defn *extlang
  390.         = get_ext_lang_of_file (file);

  391.       if (extlang != NULL)
  392.         {
  393.           if (ext_lang_present_p (extlang))
  394.             {
  395.               script_sourcer_func *sourcer
  396.                 = ext_lang_script_sourcer (extlang);

  397.               gdb_assert (sourcer != NULL);
  398.               sourcer (extlang, stream, file);
  399.               return;
  400.             }
  401.           else if (script_ext_mode == script_ext_soft)
  402.             {
  403.               /* Assume the file is a gdb script.
  404.                  This is handled below.  */
  405.             }
  406.           else
  407.             throw_ext_lang_unsupported (extlang);
  408.         }
  409.     }

  410.   script_from_file (stream, file);
  411. }

  412. /* Worker to perform the "source" command.
  413.    Load script FILE.
  414.    If SEARCH_PATH is non-zero, and the file isn't found in cwd,
  415.    search for it in the source search path.  */

  416. static void
  417. source_script_with_search (const char *file, int from_tty, int search_path)
  418. {
  419.   FILE *stream;
  420.   char *full_path;
  421.   struct cleanup *old_cleanups;

  422.   if (file == NULL || *file == 0)
  423.     error (_("source command requires file name of file to source."));

  424.   if (!find_and_open_script (file, search_path, &stream, &full_path))
  425.     {
  426.       /* The script wasn't found, or was otherwise inaccessible.
  427.          If the source command was invoked interactively, throw an
  428.          error.  Otherwise (e.g. if it was invoked by a script),
  429.          just emit a warning, rather than cause an error.  */
  430.       if (from_tty)
  431.         perror_with_name (file);
  432.       else
  433.         {
  434.           perror_warning_with_name (file);
  435.           return;
  436.         }
  437.     }

  438.   old_cleanups = make_cleanup (xfree, full_path);
  439.   make_cleanup_fclose (stream);
  440.   /* The python support reopens the file, so we need to pass full_path here
  441.      in case the file was found on the search path.  It's useful to do this
  442.      anyway so that error messages show the actual file used.  But only do
  443.      this if we (may have) used search_path, as printing the full path in
  444.      errors for the non-search case can be more noise than signal.  */
  445.   source_script_from_stream (stream, search_path ? full_path : file);
  446.   do_cleanups (old_cleanups);
  447. }

  448. /* Wrapper around source_script_with_search to export it to main.c
  449.    for use in loading .gdbinit scripts.  */

  450. void
  451. source_script (const char *file, int from_tty)
  452. {
  453.   source_script_with_search (file, from_tty, 0);
  454. }

  455. /* Return the source_verbose global variable to its previous state
  456.    on exit from the source command, by whatever means.  */
  457. static void
  458. source_verbose_cleanup (void *old_value)
  459. {
  460.   source_verbose = *(int *)old_value;
  461.   xfree (old_value);
  462. }

  463. static void
  464. source_command (char *args, int from_tty)
  465. {
  466.   struct cleanup *old_cleanups;
  467.   char *file = args;
  468.   int *old_source_verbose = xmalloc (sizeof(int));
  469.   int search_path = 0;

  470.   *old_source_verbose = source_verbose;
  471.   old_cleanups = make_cleanup (source_verbose_cleanup,
  472.                                old_source_verbose);

  473.   /* -v causes the source command to run in verbose mode.
  474.      -s causes the file to be searched in the source search path,
  475.      even if the file name contains a '/'.
  476.      We still have to be able to handle filenames with spaces in a
  477.      backward compatible way, so buildargv is not appropriate.  */

  478.   if (args)
  479.     {
  480.       while (args[0] != '\0')
  481.         {
  482.           /* Make sure leading white space does not break the
  483.              comparisons.  */
  484.           args = skip_spaces (args);

  485.           if (args[0] != '-')
  486.             break;

  487.           if (args[1] == 'v' && isspace (args[2]))
  488.             {
  489.               source_verbose = 1;

  490.               /* Skip passed -v.  */
  491.               args = &args[3];
  492.             }
  493.           else if (args[1] == 's' && isspace (args[2]))
  494.             {
  495.               search_path = 1;

  496.               /* Skip passed -s.  */
  497.               args = &args[3];
  498.             }
  499.           else
  500.             break;
  501.         }

  502.       file = skip_spaces (args);
  503.     }

  504.   source_script_with_search (file, from_tty, search_path);

  505.   do_cleanups (old_cleanups);
  506. }


  507. static void
  508. echo_command (char *text, int from_tty)
  509. {
  510.   const char *p = text;
  511.   int c;

  512.   if (text)
  513.     while ((c = *p++) != '\0')
  514.       {
  515.         if (c == '\\')
  516.           {
  517.             /* \ at end of argument is used after spaces
  518.                so they won't be lost.  */
  519.             if (*p == 0)
  520.               return;

  521.             c = parse_escape (get_current_arch (), &p);
  522.             if (c >= 0)
  523.               printf_filtered ("%c", c);
  524.           }
  525.         else
  526.           printf_filtered ("%c", c);
  527.       }

  528.   /* Force this output to appear now.  */
  529.   wrap_here ("");
  530.   gdb_flush (gdb_stdout);
  531. }

  532. static void
  533. shell_escape (char *arg, int from_tty)
  534. {
  535. #if defined(CANT_FORK) || \
  536.       (!defined(HAVE_WORKING_VFORK) && !defined(HAVE_WORKING_FORK))
  537.   /* If ARG is NULL, they want an inferior shell, but `system' just
  538.      reports if the shell is available when passed a NULL arg.  */
  539.   int rc = system (arg ? arg : "");

  540.   if (!arg)
  541.     arg = "inferior shell";

  542.   if (rc == -1)
  543.     {
  544.       fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
  545.                           safe_strerror (errno));
  546.       gdb_flush (gdb_stderr);
  547.     }
  548.   else if (rc)
  549.     {
  550.       fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
  551.       gdb_flush (gdb_stderr);
  552.     }
  553. #ifdef GLOBAL_CURDIR
  554.   /* Make sure to return to the directory GDB thinks it is, in case
  555.      the shell command we just ran changed it.  */
  556.   chdir (current_directory);
  557. #endif
  558. #else /* Can fork.  */
  559.   int status, pid;

  560.   if ((pid = vfork ()) == 0)
  561.     {
  562.       const char *p, *user_shell;

  563.       close_most_fds ();

  564.       if ((user_shell = (char *) getenv ("SHELL")) == NULL)
  565.         user_shell = "/bin/sh";

  566.       /* Get the name of the shell for arg0.  */
  567.       p = lbasename (user_shell);

  568.       if (!arg)
  569.         execl (user_shell, p, (char *) 0);
  570.       else
  571.         execl (user_shell, p, "-c", arg, (char *) 0);

  572.       fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
  573.                           safe_strerror (errno));
  574.       gdb_flush (gdb_stderr);
  575.       _exit (0177);
  576.     }

  577.   if (pid != -1)
  578.     waitpid (pid, &status, 0);
  579.   else
  580.     error (_("Fork failed"));
  581. #endif /* Can fork.  */
  582. }

  583. static void
  584. edit_command (char *arg, int from_tty)
  585. {
  586.   struct symtabs_and_lines sals;
  587.   struct symtab_and_line sal;
  588.   struct symbol *sym;
  589.   char *arg1;
  590.   char *editor;
  591.   char *p;
  592.   const char *fn;

  593.   /* Pull in the current default source line if necessary.  */
  594.   if (arg == 0)
  595.     {
  596.       set_default_source_symtab_and_line ();
  597.       sal = get_current_source_symtab_and_line ();
  598.     }

  599.   /* Bare "edit" edits file with present line.  */

  600.   if (arg == 0)
  601.     {
  602.       if (sal.symtab == 0)
  603.         error (_("No default source file yet."));
  604.       sal.line += get_lines_to_list () / 2;
  605.     }
  606.   else
  607.     {
  608.       /* Now should only be one argument -- decode it in SAL.  */

  609.       arg1 = arg;
  610.       sals = decode_line_1 (&arg1, DECODE_LINE_LIST_MODE, 0, 0);

  611.       filter_sals (&sals);
  612.       if (! sals.nelts)
  613.         {
  614.           /*  C++  */
  615.           return;
  616.         }
  617.       if (sals.nelts > 1)
  618.         {
  619.           ambiguous_line_spec (&sals);
  620.           xfree (sals.sals);
  621.           return;
  622.         }

  623.       sal = sals.sals[0];
  624.       xfree (sals.sals);

  625.       if (*arg1)
  626.         error (_("Junk at end of line specification."));

  627.       /* If line was specified by address, first print exactly which
  628.          line, and which file.  In this case, sal.symtab == 0 means
  629.          address is outside of all known source files, not that user
  630.          failed to give a filename.  */
  631.       if (*arg == '*')
  632.         {
  633.           struct gdbarch *gdbarch;

  634.           if (sal.symtab == 0)
  635.             error (_("No source file for address %s."),
  636.                    paddress (get_current_arch (), sal.pc));

  637.           gdbarch = get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
  638.           sym = find_pc_function (sal.pc);
  639.           if (sym)
  640.             printf_filtered ("%s is in %s (%s:%d).\n",
  641.                              paddress (gdbarch, sal.pc),
  642.                              SYMBOL_PRINT_NAME (sym),
  643.                              symtab_to_filename_for_display (sal.symtab),
  644.                              sal.line);
  645.           else
  646.             printf_filtered ("%s is at %s:%d.\n",
  647.                              paddress (gdbarch, sal.pc),
  648.                              symtab_to_filename_for_display (sal.symtab),
  649.                              sal.line);
  650.         }

  651.       /* If what was given does not imply a symtab, it must be an
  652.          undebuggable symbol which means no source code.  */

  653.       if (sal.symtab == 0)
  654.         error (_("No line number known for %s."), arg);
  655.     }

  656.   if ((editor = (char *) getenv ("EDITOR")) == NULL)
  657.       editor = "/bin/ex";

  658.   fn = symtab_to_fullname (sal.symtab);

  659.   /* Quote the file name, in case it has whitespace or other special
  660.      characters.  */
  661.   p = xstrprintf ("%s +%d \"%s\"", editor, sal.line, fn);
  662.   shell_escape (p, from_tty);
  663.   xfree (p);
  664. }

  665. static void
  666. list_command (char *arg, int from_tty)
  667. {
  668.   struct symtabs_and_lines sals, sals_end;
  669.   struct symtab_and_line sal = { 0 };
  670.   struct symtab_and_line sal_end = { 0 };
  671.   struct symtab_and_line cursal = { 0 };
  672.   struct symbol *sym;
  673.   char *arg1;
  674.   int no_end = 1;
  675.   int dummy_end = 0;
  676.   int dummy_beg = 0;
  677.   int linenum_beg = 0;
  678.   char *p;

  679.   /* Pull in the current default source line if necessary.  */
  680.   if (arg == 0 || arg[0] == '+' || arg[0] == '-')
  681.     {
  682.       set_default_source_symtab_and_line ();
  683.       cursal = get_current_source_symtab_and_line ();

  684.       /* If this is the first "list" since we've set the current
  685.          source line, center the listing around that line.  */
  686.       if (get_first_line_listed () == 0)
  687.         {
  688.           int first;

  689.           first = max (cursal.line - get_lines_to_list () / 2, 1);

  690.           /* A small special case --- if listing backwards, and we
  691.              should list only one line, list the preceding line,
  692.              instead of the exact line we've just shown after e.g.,
  693.              stopping for a breakpoint.  */
  694.           if (arg != NULL && arg[0] == '-'
  695.               && get_lines_to_list () == 1 && first > 1)
  696.             first -= 1;

  697.           print_source_lines (cursal.symtab, first,
  698.                               first + get_lines_to_list (), 0);
  699.           return;
  700.         }
  701.     }

  702.   /* "l" or "l +" lists next ten lines.  */

  703.   if (arg == 0 || strcmp (arg, "+") == 0)
  704.     {
  705.       print_source_lines (cursal.symtab, cursal.line,
  706.                           cursal.line + get_lines_to_list (), 0);
  707.       return;
  708.     }

  709.   /* "l -" lists previous ten lines, the ones before the ten just
  710.      listed.  */
  711.   if (strcmp (arg, "-") == 0)
  712.     {
  713.       print_source_lines (cursal.symtab,
  714.                           max (get_first_line_listed ()
  715.                                - get_lines_to_list (), 1),
  716.                           get_first_line_listed (), 0);
  717.       return;
  718.     }

  719.   /* Now if there is only one argument, decode it in SAL
  720.      and set NO_END.
  721.      If there are two arguments, decode them in SAL and SAL_END
  722.      and clear NO_END; however, if one of the arguments is blank,
  723.      set DUMMY_BEG or DUMMY_END to record that fact.  */

  724.   if (!have_full_symbols () && !have_partial_symbols ())
  725.     error (_("No symbol table is loaded.  Use the \"file\" command."));

  726.   arg1 = arg;
  727.   if (*arg1 == ',')
  728.     dummy_beg = 1;
  729.   else
  730.     {
  731.       sals = decode_line_1 (&arg1, DECODE_LINE_LIST_MODE, 0, 0);

  732.       filter_sals (&sals);
  733.       if (!sals.nelts)
  734.         return;                        /*  C++  */
  735.       if (sals.nelts > 1)
  736.         {
  737.           ambiguous_line_spec (&sals);
  738.           xfree (sals.sals);
  739.           return;
  740.         }

  741.       sal = sals.sals[0];
  742.       xfree (sals.sals);
  743.     }

  744.   /* Record whether the BEG arg is all digits.  */

  745.   for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
  746.   linenum_beg = (p == arg1);

  747.   while (*arg1 == ' ' || *arg1 == '\t')
  748.     arg1++;
  749.   if (*arg1 == ',')
  750.     {
  751.       no_end = 0;
  752.       arg1++;
  753.       while (*arg1 == ' ' || *arg1 == '\t')
  754.         arg1++;
  755.       if (*arg1 == 0)
  756.         dummy_end = 1;
  757.       else
  758.         {
  759.           if (dummy_beg)
  760.             sals_end = decode_line_1 (&arg1, DECODE_LINE_LIST_MODE, 0, 0);
  761.           else
  762.             sals_end = decode_line_1 (&arg1, DECODE_LINE_LIST_MODE,
  763.                                       sal.symtab, sal.line);
  764.           filter_sals (&sals_end);
  765.           if (sals_end.nelts == 0)
  766.             return;
  767.           if (sals_end.nelts > 1)
  768.             {
  769.               ambiguous_line_spec (&sals_end);
  770.               xfree (sals_end.sals);
  771.               return;
  772.             }
  773.           sal_end = sals_end.sals[0];
  774.           xfree (sals_end.sals);
  775.         }
  776.     }

  777.   if (*arg1)
  778.     error (_("Junk at end of line specification."));

  779.   if (!no_end && !dummy_beg && !dummy_end
  780.       && sal.symtab != sal_end.symtab)
  781.     error (_("Specified start and end are in different files."));
  782.   if (dummy_beg && dummy_end)
  783.     error (_("Two empty args do not say what lines to list."));

  784.   /* If line was specified by address,
  785.      first print exactly which line, and which file.

  786.      In this case, sal.symtab == 0 means address is outside of all
  787.      known source files, not that user failed to give a filename.  */
  788.   if (*arg == '*')
  789.     {
  790.       struct gdbarch *gdbarch;

  791.       if (sal.symtab == 0)
  792.         error (_("No source file for address %s."),
  793.                paddress (get_current_arch (), sal.pc));

  794.       gdbarch = get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
  795.       sym = find_pc_function (sal.pc);
  796.       if (sym)
  797.         printf_filtered ("%s is in %s (%s:%d).\n",
  798.                          paddress (gdbarch, sal.pc),
  799.                          SYMBOL_PRINT_NAME (sym),
  800.                          symtab_to_filename_for_display (sal.symtab), sal.line);
  801.       else
  802.         printf_filtered ("%s is at %s:%d.\n",
  803.                          paddress (gdbarch, sal.pc),
  804.                          symtab_to_filename_for_display (sal.symtab), sal.line);
  805.     }

  806.   /* If line was not specified by just a line number, and it does not
  807.      imply a symtab, it must be an undebuggable symbol which means no
  808.      source code.  */

  809.   if (!linenum_beg && sal.symtab == 0)
  810.     error (_("No line number known for %s."), arg);

  811.   /* If this command is repeated with RET,
  812.      turn it into the no-arg variant.  */

  813.   if (from_tty)
  814.     *arg = 0;

  815.   if (dummy_beg && sal_end.symtab == 0)
  816.     error (_("No default source file yet.  Do \"help list\"."));
  817.   if (dummy_beg)
  818.     print_source_lines (sal_end.symtab,
  819.                         max (sal_end.line - (get_lines_to_list () - 1), 1),
  820.                         sal_end.line + 1, 0);
  821.   else if (sal.symtab == 0)
  822.     error (_("No default source file yet.  Do \"help list\"."));
  823.   else if (no_end)
  824.     {
  825.       int first_line = sal.line - get_lines_to_list () / 2;

  826.       if (first_line < 1) first_line = 1;

  827.       print_source_lines (sal.symtab,
  828.                           first_line,
  829.                           first_line + get_lines_to_list (),
  830.                           0);
  831.     }
  832.   else
  833.     print_source_lines (sal.symtab, sal.line,
  834.                         (dummy_end
  835.                          ? sal.line + get_lines_to_list ()
  836.                          : sal_end.line + 1),
  837.                         0);
  838. }

  839. /* Subroutine of disassemble_command to simplify it.
  840.    Perform the disassembly.
  841.    NAME is the name of the function if known, or NULL.
  842.    [LOW,HIGH) are the range of addresses to disassemble.
  843.    MIXED is non-zero to print source with the assembler.  */

  844. static void
  845. print_disassembly (struct gdbarch *gdbarch, const char *name,
  846.                    CORE_ADDR low, CORE_ADDR high, int flags)
  847. {
  848. #if defined(TUI)
  849.   if (!tui_is_window_visible (DISASSEM_WIN))
  850. #endif
  851.     {
  852.       printf_filtered ("Dump of assembler code ");
  853.       if (name != NULL)
  854.         printf_filtered ("for function %s:\n", name);
  855.       else
  856.         printf_filtered ("from %s to %s:\n",
  857.                          paddress (gdbarch, low), paddress (gdbarch, high));

  858.       /* Dump the specified range.  */
  859.       gdb_disassembly (gdbarch, current_uiout, 0, flags, -1, low, high);

  860.       printf_filtered ("End of assembler dump.\n");
  861.       gdb_flush (gdb_stdout);
  862.     }
  863. #if defined(TUI)
  864.   else
  865.     {
  866.       tui_show_assembly (gdbarch, low);
  867.     }
  868. #endif
  869. }

  870. /* Subroutine of disassemble_command to simplify it.
  871.    Print a disassembly of the current function according to FLAGS.  */

  872. static void
  873. disassemble_current_function (int flags)
  874. {
  875.   struct frame_info *frame;
  876.   struct gdbarch *gdbarch;
  877.   CORE_ADDR low, high, pc;
  878.   const char *name;

  879.   frame = get_selected_frame (_("No frame selected."));
  880.   gdbarch = get_frame_arch (frame);
  881.   pc = get_frame_address_in_block (frame);
  882.   if (find_pc_partial_function (pc, &name, &low, &high) == 0)
  883.     error (_("No function contains program counter for selected frame."));
  884. #if defined(TUI)
  885.   /* NOTE: cagney/2003-02-13 The `tui_active' was previously
  886.      `tui_version'.  */
  887.   if (tui_active)
  888.     /* FIXME: cagney/2004-02-07: This should be an observer.  */
  889.     low = tui_get_low_disassembly_address (gdbarch, low, pc);
  890. #endif
  891.   low += gdbarch_deprecated_function_start_offset (gdbarch);

  892.   print_disassembly (gdbarch, name, low, high, flags);
  893. }

  894. /* Dump a specified section of assembly code.

  895.    Usage:
  896.      disassemble [/mr]
  897.        - dump the assembly code for the function of the current pc
  898.      disassemble [/mr] addr
  899.        - dump the assembly code for the function at ADDR
  900.      disassemble [/mr] low,high
  901.      disassemble [/mr] low,+length
  902.        - dump the assembly code in the range [LOW,HIGH), or [LOW,LOW+length)

  903.    A /m modifier will include source code with the assembly.
  904.    A /r modifier will include raw instructions in hex with the assembly.  */

  905. static void
  906. disassemble_command (char *arg, int from_tty)
  907. {
  908.   struct gdbarch *gdbarch = get_current_arch ();
  909.   CORE_ADDR low, high;
  910.   const char *name;
  911.   CORE_ADDR pc;
  912.   int flags;
  913.   const char *p;

  914.   p = arg;
  915.   name = NULL;
  916.   flags = 0;

  917.   if (p && *p == '/')
  918.     {
  919.       ++p;

  920.       if (*p == '\0')
  921.         error (_("Missing modifier."));

  922.       while (*p && ! isspace (*p))
  923.         {
  924.           switch (*p++)
  925.             {
  926.             case 'm':
  927.               flags |= DISASSEMBLY_SOURCE;
  928.               break;
  929.             case 'r':
  930.               flags |= DISASSEMBLY_RAW_INSN;
  931.               break;
  932.             default:
  933.               error (_("Invalid disassembly modifier."));
  934.             }
  935.         }

  936.       p = skip_spaces_const (p);
  937.     }

  938.   if (! p || ! *p)
  939.     {
  940.       flags |= DISASSEMBLY_OMIT_FNAME;
  941.       disassemble_current_function (flags);
  942.       return;
  943.     }

  944.   pc = value_as_address (parse_to_comma_and_eval (&p));
  945.   if (p[0] == ',')
  946.     ++p;
  947.   if (p[0] == '\0')
  948.     {
  949.       /* One argument.  */
  950.       if (find_pc_partial_function (pc, &name, &low, &high) == 0)
  951.         error (_("No function contains specified address."));
  952. #if defined(TUI)
  953.       /* NOTE: cagney/2003-02-13 The `tui_active' was previously
  954.          `tui_version'.  */
  955.       if (tui_active)
  956.         /* FIXME: cagney/2004-02-07: This should be an observer.  */
  957.         low = tui_get_low_disassembly_address (gdbarch, low, pc);
  958. #endif
  959.       low += gdbarch_deprecated_function_start_offset (gdbarch);
  960.       flags |= DISASSEMBLY_OMIT_FNAME;
  961.     }
  962.   else
  963.     {
  964.       /* Two arguments.  */
  965.       int incl_flag = 0;
  966.       low = pc;
  967.       p = skip_spaces_const (p);
  968.       if (p[0] == '+')
  969.         {
  970.           ++p;
  971.           incl_flag = 1;
  972.         }
  973.       high = parse_and_eval_address (p);
  974.       if (incl_flag)
  975.         high += low;
  976.     }

  977.   print_disassembly (gdbarch, name, low, high, flags);
  978. }

  979. static void
  980. make_command (char *arg, int from_tty)
  981. {
  982.   char *p;

  983.   if (arg == 0)
  984.     p = "make";
  985.   else
  986.     {
  987.       p = xmalloc (sizeof ("make ") + strlen (arg));
  988.       strcpy (p, "make ");
  989.       strcpy (p + sizeof ("make ") - 1, arg);
  990.     }

  991.   shell_escape (p, from_tty);
  992. }

  993. static void
  994. show_user (char *args, int from_tty)
  995. {
  996.   struct cmd_list_element *c;
  997.   extern struct cmd_list_element *cmdlist;

  998.   if (args)
  999.     {
  1000.       const char *comname = args;

  1001.       c = lookup_cmd (&comname, cmdlist, "", 0, 1);
  1002.       if (!cli_user_command_p (c))
  1003.         error (_("Not a user command."));
  1004.       show_user_1 (c, "", args, gdb_stdout);
  1005.     }
  1006.   else
  1007.     {
  1008.       for (c = cmdlist; c; c = c->next)
  1009.         {
  1010.           if (cli_user_command_p (c) || c->prefixlist != NULL)
  1011.             show_user_1 (c, "", c->name, gdb_stdout);
  1012.         }
  1013.     }
  1014. }

  1015. /* Search through names of commands and documentations for a certain
  1016.    regular expression.  */

  1017. static void
  1018. apropos_command (char *searchstr, int from_tty)
  1019. {
  1020.   regex_t pattern;
  1021.   int code;

  1022.   if (searchstr == NULL)
  1023.     error (_("REGEXP string is empty"));

  1024.   code = regcomp (&pattern, searchstr, REG_ICASE);
  1025.   if (code == 0)
  1026.     {
  1027.       struct cleanup *cleanups;

  1028.       cleanups = make_regfree_cleanup (&pattern);
  1029.       apropos_cmd (gdb_stdout, cmdlist, &pattern, "");
  1030.       do_cleanups (cleanups);
  1031.     }
  1032.   else
  1033.     {
  1034.       char *err = get_regcomp_error (code, &pattern);

  1035.       make_cleanup (xfree, err);
  1036.       error (_("Error in regular expression: %s"), err);
  1037.     }
  1038. }

  1039. /* Subroutine of alias_command to simplify it.
  1040.    Return the first N elements of ARGV flattened back to a string
  1041.    with a space separating each element.
  1042.    ARGV may not be NULL.
  1043.    This does not take care of quoting elements in case they contain spaces
  1044.    on purpose.  */

  1045. static dyn_string_t
  1046. argv_to_dyn_string (char **argv, int n)
  1047. {
  1048.   int i;
  1049.   dyn_string_t result = dyn_string_new (10);

  1050.   gdb_assert (argv != NULL);
  1051.   gdb_assert (n >= 0 && n <= countargv (argv));

  1052.   for (i = 0; i < n; ++i)
  1053.     {
  1054.       if (i > 0)
  1055.         dyn_string_append_char (result, ' ');
  1056.       dyn_string_append_cstr (result, argv[i]);
  1057.     }

  1058.   return result;
  1059. }

  1060. /* Subroutine of alias_command to simplify it.
  1061.    Return TRUE if COMMAND exists, unambiguously.  Otherwise FALSE.  */

  1062. static int
  1063. valid_command_p (const char *command)
  1064. {
  1065.   struct cmd_list_element *c;

  1066.   c = lookup_cmd_1 (& command, cmdlist, NULL, 1);

  1067.   if (c == NULL || c == (struct cmd_list_element *) -1)
  1068.     return FALSE;

  1069.   /* This is the slightly tricky part.
  1070.      lookup_cmd_1 will return a pointer to the last part of COMMAND
  1071.      to match, leaving COMMAND pointing at the remainder.  */
  1072.   while (*command == ' ' || *command == '\t')
  1073.     ++command;
  1074.   return *command == '\0';
  1075. }

  1076. /* Make an alias of an existing command.  */

  1077. static void
  1078. alias_command (char *args, int from_tty)
  1079. {
  1080.   int i, alias_argc, command_argc;
  1081.   int abbrev_flag = 0;
  1082.   char *args2, *equals, *alias, *command;
  1083.   char **alias_argv, **command_argv;
  1084.   dyn_string_t alias_dyn_string, command_dyn_string;
  1085.   struct cleanup *cleanup;
  1086.   static const char usage[] = N_("Usage: alias [-a] [--] ALIAS = COMMAND");

  1087.   if (args == NULL || strchr (args, '=') == NULL)
  1088.     error (_(usage));

  1089.   args2 = xstrdup (args);
  1090.   cleanup = make_cleanup (xfree, args2);
  1091.   equals = strchr (args2, '=');
  1092.   *equals = '\0';
  1093.   alias_argv = gdb_buildargv (args2);
  1094.   make_cleanup_freeargv (alias_argv);
  1095.   command_argv = gdb_buildargv (equals + 1);
  1096.   make_cleanup_freeargv (command_argv);

  1097.   for (i = 0; alias_argv[i] != NULL; )
  1098.     {
  1099.       if (strcmp (alias_argv[i], "-a") == 0)
  1100.         {
  1101.           ++alias_argv;
  1102.           abbrev_flag = 1;
  1103.         }
  1104.       else if (strcmp (alias_argv[i], "--") == 0)
  1105.         {
  1106.           ++alias_argv;
  1107.           break;
  1108.         }
  1109.       else
  1110.         break;
  1111.     }

  1112.   if (alias_argv[0] == NULL || command_argv[0] == NULL
  1113.       || *alias_argv[0] == '\0' || *command_argv[0] == '\0')
  1114.     error (_(usage));

  1115.   for (i = 0; alias_argv[i] != NULL; ++i)
  1116.     {
  1117.       if (! valid_user_defined_cmd_name_p (alias_argv[i]))
  1118.         {
  1119.           if (i == 0)
  1120.             error (_("Invalid command name: %s"), alias_argv[i]);
  1121.           else
  1122.             error (_("Invalid command element name: %s"), alias_argv[i]);
  1123.         }
  1124.     }

  1125.   alias_argc = countargv (alias_argv);
  1126.   command_argc = countargv (command_argv);

  1127.   /* COMMAND must exist.
  1128.      Reconstruct the command to remove any extraneous spaces,
  1129.      for better error messages.  */
  1130.   command_dyn_string = argv_to_dyn_string (command_argv, command_argc);
  1131.   make_cleanup_dyn_string_delete (command_dyn_string);
  1132.   command = dyn_string_buf (command_dyn_string);
  1133.   if (! valid_command_p (command))
  1134.     error (_("Invalid command to alias to: %s"), command);

  1135.   /* ALIAS must not exist.  */
  1136.   alias_dyn_string = argv_to_dyn_string (alias_argv, alias_argc);
  1137.   make_cleanup_dyn_string_delete (alias_dyn_string);
  1138.   alias = dyn_string_buf (alias_dyn_string);
  1139.   if (valid_command_p (alias))
  1140.     error (_("Alias already exists: %s"), alias);

  1141.   /* If ALIAS is one word, it is an alias for the entire COMMAND.
  1142.      Example: alias spe = set print elements

  1143.      Otherwise ALIAS and COMMAND must have the same number of words,
  1144.      and every word except the last must match; and the last word of
  1145.      ALIAS is made an alias of the last word of COMMAND.
  1146.      Example: alias set print elms = set pr elem
  1147.      Note that unambiguous abbreviations are allowed.  */

  1148.   if (alias_argc == 1)
  1149.     {
  1150.       /* add_cmd requires *we* allocate space for name, hence the xstrdup.  */
  1151.       add_com_alias (xstrdup (alias_argv[0]), command, class_alias,
  1152.                      abbrev_flag);
  1153.     }
  1154.   else
  1155.     {
  1156.       dyn_string_t alias_prefix_dyn_string, command_prefix_dyn_string;
  1157.       const char *alias_prefix, *command_prefix;
  1158.       struct cmd_list_element *c_alias, *c_command;

  1159.       if (alias_argc != command_argc)
  1160.         error (_("Mismatched command length between ALIAS and COMMAND."));

  1161.       /* Create copies of ALIAS and COMMAND without the last word,
  1162.          and use that to verify the leading elements match.  */
  1163.       alias_prefix_dyn_string =
  1164.         argv_to_dyn_string (alias_argv, alias_argc - 1);
  1165.       make_cleanup_dyn_string_delete (alias_prefix_dyn_string);
  1166.       command_prefix_dyn_string =
  1167.         argv_to_dyn_string (alias_argv, command_argc - 1);
  1168.       make_cleanup_dyn_string_delete (command_prefix_dyn_string);
  1169.       alias_prefix = dyn_string_buf (alias_prefix_dyn_string);
  1170.       command_prefix = dyn_string_buf (command_prefix_dyn_string);

  1171.       c_command = lookup_cmd_1 (& command_prefix, cmdlist, NULL, 1);
  1172.       /* We've already tried to look up COMMAND.  */
  1173.       gdb_assert (c_command != NULL
  1174.                   && c_command != (struct cmd_list_element *) -1);
  1175.       gdb_assert (c_command->prefixlist != NULL);
  1176.       c_alias = lookup_cmd_1 (& alias_prefix, cmdlist, NULL, 1);
  1177.       if (c_alias != c_command)
  1178.         error (_("ALIAS and COMMAND prefixes do not match."));

  1179.       /* add_cmd requires *we* allocate space for name, hence the xstrdup.  */
  1180.       add_alias_cmd (xstrdup (alias_argv[alias_argc - 1]),
  1181.                      command_argv[command_argc - 1],
  1182.                      class_alias, abbrev_flag, c_command->prefixlist);
  1183.     }

  1184.   do_cleanups (cleanup);
  1185. }

  1186. /* Print a list of files and line numbers which a user may choose from
  1187.    in order to list a function which was specified ambiguously (as
  1188.    with `list classname::overloadedfuncname', for example).  The
  1189.    vector in SALS provides the filenames and line numbers.  */

  1190. static void
  1191. ambiguous_line_spec (struct symtabs_and_lines *sals)
  1192. {
  1193.   int i;

  1194.   for (i = 0; i < sals->nelts; ++i)
  1195.     printf_filtered (_("file: \"%s\", line number: %d\n"),
  1196.                      symtab_to_filename_for_display (sals->sals[i].symtab),
  1197.                      sals->sals[i].line);
  1198. }

  1199. /* Sort function for filter_sals.  */

  1200. static int
  1201. compare_symtabs (const void *a, const void *b)
  1202. {
  1203.   const struct symtab_and_line *sala = a;
  1204.   const struct symtab_and_line *salb = b;
  1205.   const char *dira = SYMTAB_DIRNAME (sala->symtab);
  1206.   const char *dirb = SYMTAB_DIRNAME (salb->symtab);
  1207.   int r;

  1208.   if (dira == NULL)
  1209.     {
  1210.       if (dirb != NULL)
  1211.         return -1;
  1212.     }
  1213.   else if (dirb == NULL)
  1214.     {
  1215.       if (dira != NULL)
  1216.         return 1;
  1217.     }
  1218.   else
  1219.     {
  1220.       r = filename_cmp (dira, dirb);
  1221.       if (r)
  1222.         return r;
  1223.     }

  1224.   r = filename_cmp (sala->symtab->filename, salb->symtab->filename);
  1225.   if (r)
  1226.     return r;

  1227.   if (sala->line < salb->line)
  1228.     return -1;
  1229.   return sala->line == salb->line ? 0 : 1;
  1230. }

  1231. /* Remove any SALs that do not match the current program space, or
  1232.    which appear to be "file:line" duplicates.  */

  1233. static void
  1234. filter_sals (struct symtabs_and_lines *sals)
  1235. {
  1236.   int i, out, prev;

  1237.   out = 0;
  1238.   for (i = 0; i < sals->nelts; ++i)
  1239.     {
  1240.       if (sals->sals[i].pspace == current_program_space
  1241.           && sals->sals[i].symtab != NULL)
  1242.         {
  1243.           sals->sals[out] = sals->sals[i];
  1244.           ++out;
  1245.         }
  1246.     }
  1247.   sals->nelts = out;

  1248.   qsort (sals->sals, sals->nelts, sizeof (struct symtab_and_line),
  1249.          compare_symtabs);

  1250.   out = 1;
  1251.   prev = 0;
  1252.   for (i = 1; i < sals->nelts; ++i)
  1253.     {
  1254.       if (compare_symtabs (&sals->sals[prev], &sals->sals[i]))
  1255.         {
  1256.           /* Symtabs differ.  */
  1257.           sals->sals[out] = sals->sals[i];
  1258.           prev = out;
  1259.           ++out;
  1260.         }
  1261.     }

  1262.   if (sals->nelts == 0)
  1263.     {
  1264.       xfree (sals->sals);
  1265.       sals->sals = NULL;
  1266.     }
  1267.   else
  1268.     sals->nelts = out;
  1269. }

  1270. static void
  1271. set_debug (char *arg, int from_tty)
  1272. {
  1273.   printf_unfiltered (_("\"set debug\" must be followed by "
  1274.                        "the name of a debug subcommand.\n"));
  1275.   help_list (setdebuglist, "set debug ", all_commands, gdb_stdout);
  1276. }

  1277. static void
  1278. show_debug (char *args, int from_tty)
  1279. {
  1280.   cmd_show_list (showdebuglist, from_tty, "");
  1281. }

  1282. void
  1283. init_cmd_lists (void)
  1284. {
  1285.   max_user_call_depth = 1024;
  1286. }

  1287. static void
  1288. show_info_verbose (struct ui_file *file, int from_tty,
  1289.                    struct cmd_list_element *c,
  1290.                    const char *value)
  1291. {
  1292.   if (info_verbose)
  1293.     fprintf_filtered (file,
  1294.                       _("Verbose printing of informational messages is %s.\n"),
  1295.                       value);
  1296.   else
  1297.     fprintf_filtered (file, _("Verbosity is %s.\n"), value);
  1298. }

  1299. static void
  1300. show_history_expansion_p (struct ui_file *file, int from_tty,
  1301.                           struct cmd_list_element *c, const char *value)
  1302. {
  1303.   fprintf_filtered (file, _("History expansion on command input is %s.\n"),
  1304.                     value);
  1305. }

  1306. static void
  1307. show_remote_debug (struct ui_file *file, int from_tty,
  1308.                    struct cmd_list_element *c, const char *value)
  1309. {
  1310.   fprintf_filtered (file, _("Debugging of remote protocol is %s.\n"),
  1311.                     value);
  1312. }

  1313. static void
  1314. show_remote_timeout (struct ui_file *file, int from_tty,
  1315.                      struct cmd_list_element *c, const char *value)
  1316. {
  1317.   fprintf_filtered (file,
  1318.                     _("Timeout limit to wait for target to respond is %s.\n"),
  1319.                     value);
  1320. }

  1321. static void
  1322. show_max_user_call_depth (struct ui_file *file, int from_tty,
  1323.                           struct cmd_list_element *c, const char *value)
  1324. {
  1325.   fprintf_filtered (file,
  1326.                     _("The max call depth for user-defined commands is %s.\n"),
  1327.                     value);
  1328. }



  1329. initialize_file_ftype _initialize_cli_cmds;

  1330. void
  1331. _initialize_cli_cmds (void)
  1332. {
  1333.   struct cmd_list_element *c;

  1334.   /* Define the classes of commands.
  1335.      They will appear in the help list in alphabetical order.  */

  1336.   add_cmd ("internals", class_maintenance, NULL, _("\
  1337. Maintenance commands.\n\
  1338. Some gdb commands are provided just for use by gdb maintainers.\n\
  1339. These commands are subject to frequent change, and may not be as\n\
  1340. well documented as user commands."),
  1341.            &cmdlist);
  1342.   add_cmd ("obscure", class_obscure, NULL, _("Obscure features."), &cmdlist);
  1343.   add_cmd ("aliases", class_alias, NULL,
  1344.            _("Aliases of other commands."), &cmdlist);
  1345.   add_cmd ("user-defined", class_user, NULL, _("\
  1346. User-defined commands.\n\
  1347. The commands in this class are those defined by the user.\n\
  1348. Use the \"define\" command to define a command."), &cmdlist);
  1349.   add_cmd ("support", class_support, NULL, _("Support facilities."), &cmdlist);
  1350.   if (!dbx_commands)
  1351.     add_cmd ("status", class_info, NULL, _("Status inquiries."), &cmdlist);
  1352.   add_cmd ("files", class_files, NULL, _("Specifying and examining files."),
  1353.            &cmdlist);
  1354.   add_cmd ("breakpoints", class_breakpoint, NULL,
  1355.            _("Making program stop at certain points."), &cmdlist);
  1356.   add_cmd ("data", class_vars, NULL, _("Examining data."), &cmdlist);
  1357.   add_cmd ("stack", class_stack, NULL, _("\
  1358. Examining the stack.\n\
  1359. The stack is made up of stack frames.  Gdb assigns numbers to stack frames\n\
  1360. counting from zero for the innermost (currently executing) frame.\n\n\
  1361. At any time gdb identifies one frame as the \"selected\" frame.\n\
  1362. Variable lookups are done with respect to the selected frame.\n\
  1363. When the program being debugged stops, gdb selects the innermost frame.\n\
  1364. The commands below can be used to select other frames by number or address."),
  1365.            &cmdlist);
  1366.   add_cmd ("running", class_run, NULL, _("Running the program."), &cmdlist);

  1367.   /* Define general commands.  */

  1368.   add_com ("pwd", class_files, pwd_command, _("\
  1369. Print working directory.  This is used for your program as well."));

  1370.   c = add_cmd ("cd", class_files, cd_command, _("\
  1371. Set working directory to DIR for debugger and program being debugged.\n\
  1372. The change does not take effect for the program being debugged\n\
  1373. until the next time it is started."), &cmdlist);
  1374.   set_cmd_completer (c, filename_completer);

  1375.   add_com ("echo", class_support, echo_command, _("\
  1376. Print a constant string.  Give string as argument.\n\
  1377. C escape sequences may be used in the argument.\n\
  1378. No newline is added at the end of the argument;\n\
  1379. use \"\\n\" if you want a newline to be printed.\n\
  1380. Since leading and trailing whitespace are ignored in command arguments,\n\
  1381. if you want to print some you must use \"\\\" before leading whitespace\n\
  1382. to be printed or after trailing whitespace."));

  1383.   add_setshow_enum_cmd ("script-extension", class_support,
  1384.                         script_ext_enums, &script_ext_mode, _("\
  1385. Set mode for script filename extension recognition."), _("\
  1386. Show mode for script filename extension recognition."), _("\
  1387. off  == no filename extension recognition (all sourced files are GDB scripts)\n\
  1388. soft == evaluate script according to filename extension, fallback to GDB script"
  1389.   "\n\
  1390. strict == evaluate script according to filename extension, error if not supported"
  1391.   ),
  1392.                         NULL,
  1393.                         show_script_ext_mode,
  1394.                         &setlist, &showlist);

  1395.   add_com ("quit", class_support, quit_command, _("\
  1396. Exit gdb.\n\
  1397. Usage: quit [EXPR]\n\
  1398. The optional expression EXPR, if present, is evaluated and the result\n\
  1399. used as GDB's exit code.  The default is zero."));
  1400.   c = add_com ("help", class_support, help_command,
  1401.                _("Print list of commands."));
  1402.   set_cmd_completer (c, command_completer);
  1403.   add_com_alias ("q", "quit", class_support, 1);
  1404.   add_com_alias ("h", "help", class_support, 1);

  1405.   add_setshow_boolean_cmd ("verbose", class_support, &info_verbose, _("\
  1406. Set verbosity."), _("\
  1407. Show verbosity."), NULL,
  1408.                            set_verbose,
  1409.                            show_info_verbose,
  1410.                            &setlist, &showlist);

  1411.   add_prefix_cmd ("history", class_support, set_history,
  1412.                   _("Generic command for setting command history parameters."),
  1413.                   &sethistlist, "set history ", 0, &setlist);
  1414.   add_prefix_cmd ("history", class_support, show_history,
  1415.                   _("Generic command for showing command history parameters."),
  1416.                   &showhistlist, "show history ", 0, &showlist);

  1417.   add_setshow_boolean_cmd ("expansion", no_class, &history_expansion_p, _("\
  1418. Set history expansion on command input."), _("\
  1419. Show history expansion on command input."), _("\
  1420. Without an argument, history expansion is enabled."),
  1421.                            NULL,
  1422.                            show_history_expansion_p,
  1423.                            &sethistlist, &showhistlist);

  1424.   add_prefix_cmd ("info", class_info, info_command, _("\
  1425. Generic command for showing things about the program being debugged."),
  1426.                   &infolist, "info ", 0, &cmdlist);
  1427.   add_com_alias ("i", "info", class_info, 1);
  1428.   add_com_alias ("inf", "info", class_info, 1);

  1429.   add_com ("complete", class_obscure, complete_command,
  1430.            _("List the completions for the rest of the line as a command."));

  1431.   add_prefix_cmd ("show", class_info, show_command, _("\
  1432. Generic command for showing things about the debugger."),
  1433.                   &showlist, "show ", 0, &cmdlist);
  1434.   /* Another way to get at the same thing.  */
  1435.   add_info ("set", show_command, _("Show all GDB settings."));

  1436.   add_cmd ("commands", no_set_class, show_commands, _("\
  1437. Show the history of commands you typed.\n\
  1438. You can supply a command number to start with, or a `+' to start after\n\
  1439. the previous command number shown."),
  1440.            &showlist);

  1441.   add_cmd ("version", no_set_class, show_version,
  1442.            _("Show what version of GDB this is."), &showlist);

  1443.   add_cmd ("configuration", no_set_class, show_configuration,
  1444.            _("Show how GDB was configured at build time."), &showlist);

  1445.   add_setshow_zinteger_cmd ("remote", no_class, &remote_debug, _("\
  1446. Set debugging of remote protocol."), _("\
  1447. Show debugging of remote protocol."), _("\
  1448. When enabled, each packet sent or received with the remote target\n\
  1449. is displayed."),
  1450.                             NULL,
  1451.                             show_remote_debug,
  1452.                             &setdebuglist, &showdebuglist);

  1453.   add_setshow_zuinteger_unlimited_cmd ("remotetimeout", no_class,
  1454.                                        &remote_timeout, _("\
  1455. Set timeout limit to wait for target to respond."), _("\
  1456. Show timeout limit to wait for target to respond."), _("\
  1457. This value is used to set the time limit for gdb to wait for a response\n\
  1458. from the target."),
  1459.                                        NULL,
  1460.                                        show_remote_timeout,
  1461.                                        &setlist, &showlist);

  1462.   add_prefix_cmd ("debug", no_class, set_debug,
  1463.                   _("Generic command for setting gdb debugging flags"),
  1464.                   &setdebuglist, "set debug ", 0, &setlist);

  1465.   add_prefix_cmd ("debug", no_class, show_debug,
  1466.                   _("Generic command for showing gdb debugging flags"),
  1467.                   &showdebuglist, "show debug ", 0, &showlist);

  1468.   c = add_com ("shell", class_support, shell_escape, _("\
  1469. Execute the rest of the line as a shell command.\n\
  1470. With no arguments, run an inferior shell."));
  1471.   set_cmd_completer (c, filename_completer);

  1472.   c = add_com ("edit", class_files, edit_command, _("\
  1473. Edit specified file or function.\n\
  1474. With no argument, edits file containing most recent line listed.\n\
  1475. Editing targets can be specified in these ways:\n\
  1476.   FILE:LINENUM, to edit at that line in that file,\n\
  1477.   FUNCTION, to edit at the beginning of that function,\n\
  1478.   FILE:FUNCTION, to distinguish among like-named static functions.\n\
  1479.   *ADDRESS, to edit at the line containing that address.\n\
  1480. Uses EDITOR environment variable contents as editor (or ex as default)."));

  1481.   c->completer = location_completer;

  1482.   add_com ("list", class_files, list_command, _("\
  1483. List specified function or line.\n\
  1484. With no argument, lists ten more lines after or around previous listing.\n\
  1485. \"list -\" lists the ten lines before a previous ten-line listing.\n\
  1486. One argument specifies a line, and ten lines are listed around that line.\n\
  1487. Two arguments with comma between specify starting and ending lines to list.\n\
  1488. Lines can be specified in these ways:\n\
  1489.   LINENUM, to list around that line in current file,\n\
  1490.   FILE:LINENUM, to list around that line in that file,\n\
  1491.   FUNCTION, to list around beginning of that function,\n\
  1492.   FILE:FUNCTION, to distinguish among like-named static functions.\n\
  1493.   *ADDRESS, to list around the line containing that address.\n\
  1494. With two args if one is empty it stands for ten lines away from \
  1495. the other arg."));

  1496.   if (!xdb_commands)
  1497.     add_com_alias ("l", "list", class_files, 1);
  1498.   else
  1499.     add_com_alias ("v", "list", class_files, 1);

  1500.   if (dbx_commands)
  1501.     add_com_alias ("file", "list", class_files, 1);

  1502.   c = add_com ("disassemble", class_vars, disassemble_command, _("\
  1503. Disassemble a specified section of memory.\n\
  1504. Default is the function surrounding the pc of the selected frame.\n\
  1505. With a /m modifier, source lines are included (if available).\n\
  1506. With a /r modifier, raw instructions in hex are included.\n\
  1507. With a single argument, the function surrounding that address is dumped.\n\
  1508. Two arguments (separated by a comma) are taken as a range of memory to dump,\n\
  1509.   in the form of \"start,end\", or \"start,+length\".\n\
  1510. \n\
  1511. Note that the address is interpreted as an expression, not as a location\n\
  1512. like in the \"break\" command.\n\
  1513. So, for example, if you want to disassemble function bar in file foo.c\n\
  1514. you must type \"disassemble 'foo.c'::bar\" and not \"disassemble foo.c:bar\"."));
  1515.   set_cmd_completer (c, location_completer);
  1516.   if (xdb_commands)
  1517.     add_com_alias ("va", "disassemble", class_xdb, 0);

  1518.   add_com_alias ("!", "shell", class_support, 0);

  1519.   c = add_com ("make", class_support, make_command, _("\
  1520. Run the ``make'' program using the rest of the line as arguments."));
  1521.   set_cmd_completer (c, filename_completer);
  1522.   add_cmd ("user", no_class, show_user, _("\
  1523. Show definitions of non-python/scheme user defined commands.\n\
  1524. Argument is the name of the user defined command.\n\
  1525. With no argument, show definitions of all user defined commands."), &showlist);
  1526.   add_com ("apropos", class_support, apropos_command,
  1527.            _("Search for commands matching a REGEXP"));

  1528.   add_setshow_uinteger_cmd ("max-user-call-depth", no_class,
  1529.                            &max_user_call_depth, _("\
  1530. Set the max call depth for non-python/scheme user-defined commands."), _("\
  1531. Show the max call depth for non-python/scheme user-defined commands."), NULL,
  1532.                             NULL,
  1533.                             show_max_user_call_depth,
  1534.                             &setlist, &showlist);

  1535.   add_setshow_boolean_cmd ("trace-commands", no_class, &trace_commands, _("\
  1536. Set tracing of GDB CLI commands."), _("\
  1537. Show state of GDB CLI command tracing."), _("\
  1538. When 'on', each command is displayed as it is executed."),
  1539.                            NULL,
  1540.                            NULL,
  1541.                            &setlist, &showlist);

  1542.   c = add_com ("alias", class_support, alias_command, _("\
  1543. Define a new command that is an alias of an existing command.\n\
  1544. Usage: alias [-a] [--] ALIAS = COMMAND\n\
  1545. ALIAS is the name of the alias command to create.\n\
  1546. COMMAND is the command being aliased to.\n\
  1547. If \"-a\" is specified, the command is an abbreviation,\n\
  1548. and will not appear in help command list output.\n\
  1549. \n\
  1550. Examples:\n\
  1551. Make \"spe\" an alias of \"set print elements\":\n\
  1552.   alias spe = set print elements\n\
  1553. Make \"elms\" an alias of \"elements\" in the \"set print\" command:\n\
  1554.   alias -a set print elms = set print elements"));
  1555. }

  1556. void
  1557. init_cli_cmds (void)
  1558. {
  1559.   struct cmd_list_element *c;
  1560.   char *source_help_text;

  1561.   source_help_text = xstrprintf (_("\
  1562. Read commands from a file named FILE.\n\
  1563. \n\
  1564. Usage: source [-s] [-v] FILE\n\
  1565. -s: search for the script in the source search path,\n\
  1566.     even if FILE contains directories.\n\
  1567. -v: each command in FILE is echoed as it is executed.\n\
  1568. \n\
  1569. Note that the file \"%s\" is read automatically in this way\n\
  1570. when GDB is started."), gdbinit);
  1571.   c = add_cmd ("source", class_support, source_command,
  1572.                source_help_text, &cmdlist);
  1573.   set_cmd_completer (c, filename_completer);
  1574. }