GDB (API)
/home/stan/gdb/src/gdb/cli/cli-cmds.c
Go to the documentation of this file.
00001 /* GDB CLI commands.
00002 
00003    Copyright (C) 2000-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "exceptions.h"
00022 #include "arch-utils.h"
00023 #include "dyn-string.h"
00024 #include "readline/readline.h"
00025 #include "readline/tilde.h"
00026 #include "completer.h"
00027 #include "target.h"     /* For baud_rate, remote_debug and remote_timeout.  */
00028 #include "gdb_wait.h"   /* For shell escape implementation.  */
00029 #include "gdb_regex.h"  /* Used by apropos_command.  */
00030 #include "gdb_string.h"
00031 #include "gdb_vfork.h"
00032 #include "linespec.h"
00033 #include "expression.h"
00034 #include "frame.h"
00035 #include "value.h"
00036 #include "language.h"
00037 #include "filenames.h"  /* For DOSish file names.  */
00038 #include "objfiles.h"
00039 #include "source.h"
00040 #include "disasm.h"
00041 #include "tracepoint.h"
00042 #include "filestuff.h"
00043 
00044 #include "ui-out.h"
00045 
00046 #include "top.h"
00047 #include "cli/cli-decode.h"
00048 #include "cli/cli-script.h"
00049 #include "cli/cli-setshow.h"
00050 #include "cli/cli-cmds.h"
00051 #include "cli/cli-utils.h"
00052 
00053 #include "python/python.h"
00054 
00055 #ifdef TUI
00056 #include "tui/tui.h"    /* For tui_active et.al.  */
00057 #endif
00058 
00059 #include <fcntl.h>
00060 
00061 /* Prototypes for local command functions */
00062 
00063 static void complete_command (char *, int);
00064 
00065 static void echo_command (char *, int);
00066 
00067 static void pwd_command (char *, int);
00068 
00069 static void show_version (char *, int);
00070 
00071 static void help_command (char *, int);
00072 
00073 static void show_command (char *, int);
00074 
00075 static void info_command (char *, int);
00076 
00077 static void show_debug (char *, int);
00078 
00079 static void set_debug (char *, int);
00080 
00081 static void show_user (char *, int);
00082 
00083 static void make_command (char *, int);
00084 
00085 static void shell_escape (char *, int);
00086 
00087 static void edit_command (char *, int);
00088 
00089 static void list_command (char *, int);
00090 
00091 /* Prototypes for local utility functions */
00092 
00093 static void ambiguous_line_spec (struct symtabs_and_lines *);
00094 
00095 static void filter_sals (struct symtabs_and_lines *);
00096 
00097 
00098 /* Limit the call depth of user-defined commands */
00099 unsigned int max_user_call_depth;
00100 
00101 /* Define all cmd_list_elements.  */
00102 
00103 /* Chain containing all defined commands.  */
00104 
00105 struct cmd_list_element *cmdlist;
00106 
00107 /* Chain containing all defined info subcommands.  */
00108 
00109 struct cmd_list_element *infolist;
00110 
00111 /* Chain containing all defined enable subcommands.  */
00112 
00113 struct cmd_list_element *enablelist;
00114 
00115 /* Chain containing all defined disable subcommands.  */
00116 
00117 struct cmd_list_element *disablelist;
00118 
00119 /* Chain containing all defined stop subcommands.  */
00120 
00121 struct cmd_list_element *stoplist;
00122 
00123 /* Chain containing all defined delete subcommands.  */
00124 
00125 struct cmd_list_element *deletelist;
00126 
00127 /* Chain containing all defined detach subcommands.  */
00128 
00129 struct cmd_list_element *detachlist;
00130 
00131 /* Chain containing all defined kill subcommands.  */
00132 
00133 struct cmd_list_element *killlist;
00134 
00135 /* Chain containing all defined set subcommands */
00136 
00137 struct cmd_list_element *setlist;
00138 
00139 /* Chain containing all defined unset subcommands */
00140 
00141 struct cmd_list_element *unsetlist;
00142 
00143 /* Chain containing all defined show subcommands.  */
00144 
00145 struct cmd_list_element *showlist;
00146 
00147 /* Chain containing all defined \"set history\".  */
00148 
00149 struct cmd_list_element *sethistlist;
00150 
00151 /* Chain containing all defined \"show history\".  */
00152 
00153 struct cmd_list_element *showhistlist;
00154 
00155 /* Chain containing all defined \"unset history\".  */
00156 
00157 struct cmd_list_element *unsethistlist;
00158 
00159 /* Chain containing all defined maintenance subcommands.  */
00160 
00161 struct cmd_list_element *maintenancelist;
00162 
00163 /* Chain containing all defined "maintenance info" subcommands.  */
00164 
00165 struct cmd_list_element *maintenanceinfolist;
00166 
00167 /* Chain containing all defined "maintenance print" subcommands.  */
00168 
00169 struct cmd_list_element *maintenanceprintlist;
00170 
00171 struct cmd_list_element *setprintlist;
00172 
00173 struct cmd_list_element *showprintlist;
00174 
00175 struct cmd_list_element *setdebuglist;
00176 
00177 struct cmd_list_element *showdebuglist;
00178 
00179 struct cmd_list_element *setchecklist;
00180 
00181 struct cmd_list_element *showchecklist;
00182 
00183 /* Command tracing state.  */
00184 
00185 int source_verbose = 0;
00186 int trace_commands = 0;
00187 
00188 /* 'script-extension' option support.  */
00189 
00190 static const char script_ext_off[] = "off";
00191 static const char script_ext_soft[] = "soft";
00192 static const char script_ext_strict[] = "strict";
00193 
00194 static const char *const script_ext_enums[] = {
00195   script_ext_off,
00196   script_ext_soft,
00197   script_ext_strict,
00198   NULL
00199 };
00200 
00201 static const char *script_ext_mode = script_ext_soft;
00202 
00203 /* Utility used everywhere when at least one argument is needed and
00204    none is supplied.  */
00205 
00206 void
00207 error_no_arg (char *why)
00208 {
00209   error (_("Argument required (%s)."), why);
00210 }
00211 
00212 /* The "info" command is defined as a prefix, with allow_unknown = 0.
00213    Therefore, its own definition is called only for "info" with no
00214    args.  */
00215 
00216 static void
00217 info_command (char *arg, int from_tty)
00218 {
00219   printf_unfiltered (_("\"info\" must be followed by "
00220                        "the name of an info command.\n"));
00221   help_list (infolist, "info ", -1, gdb_stdout);
00222 }
00223 
00224 /* The "show" command with no arguments shows all the settings.  */
00225 
00226 static void
00227 show_command (char *arg, int from_tty)
00228 {
00229   cmd_show_list (showlist, from_tty, "");
00230 }
00231 
00232 /* Provide documentation on command or list given by COMMAND.  FROM_TTY
00233    is ignored.  */
00234 
00235 static void
00236 help_command (char *command, int from_tty)
00237 {
00238   help_cmd (command, gdb_stdout);
00239 }
00240 
00241 /* The "complete" command is used by Emacs to implement completion.  */
00242 
00243 static void
00244 complete_command (char *arg, int from_tty)
00245 {
00246   int argpoint;
00247   char *point, *arg_prefix;
00248   VEC (char_ptr) *completions;
00249 
00250   dont_repeat ();
00251 
00252   if (arg == NULL)
00253     arg = "";
00254   argpoint = strlen (arg);
00255 
00256   /* complete_line assumes that its first argument is somewhere
00257      within, and except for filenames at the beginning of, the word to
00258      be completed.  The following crude imitation of readline's
00259      word-breaking tries to accomodate this.  */
00260   point = arg + argpoint;
00261   while (point > arg)
00262     {
00263       if (strchr (rl_completer_word_break_characters, point[-1]) != 0)
00264         break;
00265       point--;
00266     }
00267 
00268   arg_prefix = alloca (point - arg + 1);
00269   memcpy (arg_prefix, arg, point - arg);
00270   arg_prefix[point - arg] = 0;
00271 
00272   completions = complete_line (point, arg, argpoint);
00273 
00274   if (completions)
00275     {
00276       int ix, size = VEC_length (char_ptr, completions);
00277       char *item, *prev = NULL;
00278 
00279       qsort (VEC_address (char_ptr, completions), size,
00280              sizeof (char *), compare_strings);
00281 
00282       /* We do extra processing here since we only want to print each
00283          unique item once.  */
00284       for (ix = 0; VEC_iterate (char_ptr, completions, ix, item); ++ix)
00285         {
00286           if (prev == NULL || strcmp (item, prev) != 0)
00287             {
00288               printf_unfiltered ("%s%s\n", arg_prefix, item);
00289               xfree (prev);
00290               prev = item;
00291             }
00292           else
00293             xfree (item);
00294         }
00295 
00296       xfree (prev);
00297       VEC_free (char_ptr, completions);
00298     }
00299 }
00300 
00301 int
00302 is_complete_command (struct cmd_list_element *c)
00303 {
00304   return cmd_cfunc_eq (c, complete_command);
00305 }
00306 
00307 static void
00308 show_version (char *args, int from_tty)
00309 {
00310   print_gdb_version (gdb_stdout);
00311   printf_filtered ("\n");
00312 }
00313 
00314 static void
00315 show_configuration (char *args, int from_tty)
00316 {
00317   print_gdb_configuration (gdb_stdout);
00318 }
00319 
00320 /* Handle the quit command.  */
00321 
00322 void
00323 quit_command (char *args, int from_tty)
00324 {
00325   if (!quit_confirm ())
00326     error (_("Not confirmed."));
00327 
00328   query_if_trace_running (from_tty);
00329 
00330   quit_force (args, from_tty);
00331 }
00332 
00333 static void
00334 pwd_command (char *args, int from_tty)
00335 {
00336   if (args)
00337     error (_("The \"pwd\" command does not take an argument: %s"), args);
00338   if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
00339     error (_("Error finding name of working directory: %s"),
00340            safe_strerror (errno));
00341 
00342   if (strcmp (gdb_dirbuf, current_directory) != 0)
00343     printf_unfiltered (_("Working directory %s\n (canonically %s).\n"),
00344                        current_directory, gdb_dirbuf);
00345   else
00346     printf_unfiltered (_("Working directory %s.\n"), current_directory);
00347 }
00348 
00349 void
00350 cd_command (char *dir, int from_tty)
00351 {
00352   int len;
00353   /* Found something other than leading repetitions of "/..".  */
00354   int found_real_path;
00355   char *p;
00356   struct cleanup *cleanup;
00357 
00358   /* If the new directory is absolute, repeat is a no-op; if relative,
00359      repeat might be useful but is more likely to be a mistake.  */
00360   dont_repeat ();
00361 
00362   if (dir == 0)
00363     dir = "~";
00364 
00365   dir = tilde_expand (dir);
00366   cleanup = make_cleanup (xfree, dir);
00367 
00368   if (chdir (dir) < 0)
00369     perror_with_name (dir);
00370 
00371 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
00372   /* There's too much mess with DOSish names like "d:", "d:.",
00373      "d:./foo" etc.  Instead of having lots of special #ifdef'ed code,
00374      simply get the canonicalized name of the current directory.  */
00375   dir = getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
00376 #endif
00377 
00378   len = strlen (dir);
00379   if (IS_DIR_SEPARATOR (dir[len - 1]))
00380     {
00381       /* Remove the trailing slash unless this is a root directory
00382          (including a drive letter on non-Unix systems).  */
00383       if (!(len == 1)           /* "/" */
00384 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
00385           && !(len == 3 && dir[1] == ':') /* "d:/" */
00386 #endif
00387           )
00388         len--;
00389     }
00390 
00391   dir = savestring (dir, len);
00392   if (IS_ABSOLUTE_PATH (dir))
00393     current_directory = dir;
00394   else
00395     {
00396       if (IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1]))
00397         current_directory = concat (current_directory, dir, (char *)NULL);
00398       else
00399         current_directory = concat (current_directory, SLASH_STRING,
00400                                     dir, (char *)NULL);
00401       xfree (dir);
00402     }
00403 
00404   /* Now simplify any occurrences of `.' and `..' in the pathname.  */
00405 
00406   found_real_path = 0;
00407   for (p = current_directory; *p;)
00408     {
00409       if (IS_DIR_SEPARATOR (p[0]) && p[1] == '.'
00410           && (p[2] == 0 || IS_DIR_SEPARATOR (p[2])))
00411         memmove (p, p + 2, strlen (p + 2) + 1);
00412       else if (IS_DIR_SEPARATOR (p[0]) && p[1] == '.' && p[2] == '.'
00413                && (p[3] == 0 || IS_DIR_SEPARATOR (p[3])))
00414         {
00415           if (found_real_path)
00416             {
00417               /* Search backwards for the directory just before the "/.."
00418                  and obliterate it and the "/..".  */
00419               char *q = p;
00420 
00421               while (q != current_directory && !IS_DIR_SEPARATOR (q[-1]))
00422                 --q;
00423 
00424               if (q == current_directory)
00425                 /* current_directory is
00426                    a relative pathname ("can't happen"--leave it alone).  */
00427                 ++p;
00428               else
00429                 {
00430                   memmove (q - 1, p + 3, strlen (p + 3) + 1);
00431                   p = q - 1;
00432                 }
00433             }
00434           else
00435             /* We are dealing with leading repetitions of "/..", for
00436                example "/../..", which is the Mach super-root.  */
00437             p += 3;
00438         }
00439       else
00440         {
00441           found_real_path = 1;
00442           ++p;
00443         }
00444     }
00445 
00446   forget_cached_source_info ();
00447 
00448   if (from_tty)
00449     pwd_command ((char *) 0, 1);
00450 
00451   do_cleanups (cleanup);
00452 }
00453 
00454 /* Show the current value of the 'script-extension' option.  */
00455 
00456 static void
00457 show_script_ext_mode (struct ui_file *file, int from_tty,
00458                      struct cmd_list_element *c, const char *value)
00459 {
00460   fprintf_filtered (file,
00461                     _("Script filename extension recognition is \"%s\".\n"),
00462                     value);
00463 }
00464 
00465 /* Try to open SCRIPT_FILE.
00466    If successful, the full path name is stored in *FULL_PATHP,
00467    the stream is stored in *STREAMP, and return 1.
00468    The caller is responsible for freeing *FULL_PATHP.
00469    If not successful, return 0; errno is set for the last file
00470    we tried to open.
00471 
00472    If SEARCH_PATH is non-zero, and the file isn't found in cwd,
00473    search for it in the source search path.  */
00474 
00475 int
00476 find_and_open_script (const char *script_file, int search_path,
00477                       FILE **streamp, char **full_pathp)
00478 {
00479   char *file;
00480   int fd;
00481   struct cleanup *old_cleanups;
00482   int search_flags = OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH;
00483 
00484   file = tilde_expand (script_file);
00485   old_cleanups = make_cleanup (xfree, file);
00486 
00487   if (search_path)
00488     search_flags |= OPF_SEARCH_IN_PATH;
00489 
00490   /* Search for and open 'file' on the search path used for source
00491      files.  Put the full location in *FULL_PATHP.  */
00492   fd = openp (source_path, search_flags,
00493               file, O_RDONLY, full_pathp);
00494 
00495   if (fd == -1)
00496     {
00497       int save_errno = errno;
00498       do_cleanups (old_cleanups);
00499       errno = save_errno;
00500       return 0;
00501     }
00502 
00503   do_cleanups (old_cleanups);
00504 
00505   *streamp = fdopen (fd, FOPEN_RT);
00506   if (*streamp == NULL)
00507     {
00508       int save_errno = errno;
00509 
00510       close (fd);
00511       if (full_pathp)
00512         xfree (*full_pathp);
00513       errno = save_errno;
00514       return 0;
00515     }
00516 
00517   return 1;
00518 }
00519 
00520 /* Load script FILE, which has already been opened as STREAM.  */
00521 
00522 static void
00523 source_script_from_stream (FILE *stream, const char *file)
00524 {
00525   if (script_ext_mode != script_ext_off
00526       && strlen (file) > 3 && !strcmp (&file[strlen (file) - 3], ".py"))
00527     {
00528       volatile struct gdb_exception e;
00529 
00530       TRY_CATCH (e, RETURN_MASK_ERROR)
00531         {
00532           source_python_script (stream, file);
00533         }
00534       if (e.reason < 0)
00535         {
00536           /* Should we fallback to ye olde GDB script mode?  */
00537           if (script_ext_mode == script_ext_soft
00538               && e.reason == RETURN_ERROR && e.error == UNSUPPORTED_ERROR)
00539             {
00540               fseek (stream, 0, SEEK_SET);
00541               script_from_file (stream, (char*) file);
00542             }
00543           else
00544             {
00545               /* Nope, just punt.  */
00546               throw_exception (e);
00547             }
00548         }
00549     }
00550   else
00551     script_from_file (stream, file);
00552 }
00553 
00554 /* Worker to perform the "source" command.
00555    Load script FILE.
00556    If SEARCH_PATH is non-zero, and the file isn't found in cwd,
00557    search for it in the source search path.  */
00558 
00559 static void
00560 source_script_with_search (const char *file, int from_tty, int search_path)
00561 {
00562   FILE *stream;
00563   char *full_path;
00564   struct cleanup *old_cleanups;
00565 
00566   if (file == NULL || *file == 0)
00567     error (_("source command requires file name of file to source."));
00568 
00569   if (!find_and_open_script (file, search_path, &stream, &full_path))
00570     {
00571       /* The script wasn't found, or was otherwise inaccessible.
00572          If the source command was invoked interactively, throw an
00573          error.  Otherwise (e.g. if it was invoked by a script),
00574          just emit a warning, rather than cause an error.  */
00575       if (from_tty)
00576         perror_with_name (file);
00577       else
00578         {
00579           perror_warning_with_name (file);
00580           return;
00581         }
00582     }
00583 
00584   old_cleanups = make_cleanup (xfree, full_path);
00585   make_cleanup_fclose (stream);
00586   /* The python support reopens the file, so we need to pass full_path here
00587      in case the file was found on the search path.  It's useful to do this
00588      anyway so that error messages show the actual file used.  But only do
00589      this if we (may have) used search_path, as printing the full path in
00590      errors for the non-search case can be more noise than signal.  */
00591   source_script_from_stream (stream, search_path ? full_path : file);
00592   do_cleanups (old_cleanups);
00593 }
00594 
00595 /* Wrapper around source_script_with_search to export it to main.c
00596    for use in loading .gdbinit scripts.  */
00597 
00598 void
00599 source_script (const char *file, int from_tty)
00600 {
00601   source_script_with_search (file, from_tty, 0);
00602 }
00603 
00604 /* Return the source_verbose global variable to its previous state
00605    on exit from the source command, by whatever means.  */
00606 static void
00607 source_verbose_cleanup (void *old_value)
00608 {
00609   source_verbose = *(int *)old_value;
00610   xfree (old_value);
00611 }
00612 
00613 static void
00614 source_command (char *args, int from_tty)
00615 {
00616   struct cleanup *old_cleanups;
00617   char *file = args;
00618   int *old_source_verbose = xmalloc (sizeof(int));
00619   int search_path = 0;
00620 
00621   *old_source_verbose = source_verbose;
00622   old_cleanups = make_cleanup (source_verbose_cleanup, 
00623                                old_source_verbose);
00624 
00625   /* -v causes the source command to run in verbose mode.
00626      -s causes the file to be searched in the source search path,
00627      even if the file name contains a '/'.
00628      We still have to be able to handle filenames with spaces in a
00629      backward compatible way, so buildargv is not appropriate.  */
00630 
00631   if (args)
00632     {
00633       while (args[0] != '\0')
00634         {
00635           /* Make sure leading white space does not break the
00636              comparisons.  */
00637           args = skip_spaces (args);
00638 
00639           if (args[0] != '-')
00640             break;
00641 
00642           if (args[1] == 'v' && isspace (args[2]))
00643             {
00644               source_verbose = 1;
00645 
00646               /* Skip passed -v.  */
00647               args = &args[3];
00648             }
00649           else if (args[1] == 's' && isspace (args[2]))
00650             {
00651               search_path = 1;
00652 
00653               /* Skip passed -s.  */
00654               args = &args[3];
00655             }
00656           else
00657             break;
00658         }
00659 
00660       file = skip_spaces (args);
00661     }
00662 
00663   source_script_with_search (file, from_tty, search_path);
00664 
00665   do_cleanups (old_cleanups);
00666 }
00667 
00668 
00669 static void
00670 echo_command (char *text, int from_tty)
00671 {
00672   const char *p = text;
00673   int c;
00674 
00675   if (text)
00676     while ((c = *p++) != '\0')
00677       {
00678         if (c == '\\')
00679           {
00680             /* \ at end of argument is used after spaces
00681                so they won't be lost.  */
00682             if (*p == 0)
00683               return;
00684 
00685             c = parse_escape (get_current_arch (), &p);
00686             if (c >= 0)
00687               printf_filtered ("%c", c);
00688           }
00689         else
00690           printf_filtered ("%c", c);
00691       }
00692 
00693   /* Force this output to appear now.  */
00694   wrap_here ("");
00695   gdb_flush (gdb_stdout);
00696 }
00697 
00698 static void
00699 shell_escape (char *arg, int from_tty)
00700 {
00701 #if defined(CANT_FORK) || \
00702       (!defined(HAVE_WORKING_VFORK) && !defined(HAVE_WORKING_FORK))
00703   /* If ARG is NULL, they want an inferior shell, but `system' just
00704      reports if the shell is available when passed a NULL arg.  */
00705   int rc = system (arg ? arg : "");
00706 
00707   if (!arg)
00708     arg = "inferior shell";
00709 
00710   if (rc == -1)
00711     {
00712       fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
00713                           safe_strerror (errno));
00714       gdb_flush (gdb_stderr);
00715     }
00716   else if (rc)
00717     {
00718       fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
00719       gdb_flush (gdb_stderr);
00720     }
00721 #ifdef GLOBAL_CURDIR
00722   /* Make sure to return to the directory GDB thinks it is, in case
00723      the shell command we just ran changed it.  */
00724   chdir (current_directory);
00725 #endif
00726 #else /* Can fork.  */
00727   int status, pid;
00728 
00729   if ((pid = vfork ()) == 0)
00730     {
00731       const char *p, *user_shell;
00732 
00733       close_most_fds ();
00734 
00735       if ((user_shell = (char *) getenv ("SHELL")) == NULL)
00736         user_shell = "/bin/sh";
00737 
00738       /* Get the name of the shell for arg0.  */
00739       p = lbasename (user_shell);
00740 
00741       if (!arg)
00742         execl (user_shell, p, (char *) 0);
00743       else
00744         execl (user_shell, p, "-c", arg, (char *) 0);
00745 
00746       fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
00747                           safe_strerror (errno));
00748       gdb_flush (gdb_stderr);
00749       _exit (0177);
00750     }
00751 
00752   if (pid != -1)
00753     waitpid (pid, &status, 0);
00754   else
00755     error (_("Fork failed"));
00756 #endif /* Can fork.  */
00757 }
00758 
00759 static void
00760 edit_command (char *arg, int from_tty)
00761 {
00762   struct symtabs_and_lines sals;
00763   struct symtab_and_line sal;
00764   struct symbol *sym;
00765   char *arg1;
00766   char *editor;
00767   char *p;
00768   const char *fn;
00769 
00770   /* Pull in the current default source line if necessary.  */
00771   if (arg == 0)
00772     {
00773       set_default_source_symtab_and_line ();
00774       sal = get_current_source_symtab_and_line ();
00775     }
00776 
00777   /* Bare "edit" edits file with present line.  */
00778 
00779   if (arg == 0)
00780     {
00781       if (sal.symtab == 0)
00782         error (_("No default source file yet."));
00783       sal.line += get_lines_to_list () / 2;
00784     }
00785   else
00786     {
00787       /* Now should only be one argument -- decode it in SAL.  */
00788 
00789       arg1 = arg;
00790       sals = decode_line_1 (&arg1, DECODE_LINE_LIST_MODE, 0, 0);
00791 
00792       filter_sals (&sals);
00793       if (! sals.nelts)
00794         {
00795           /*  C++  */
00796           return;
00797         }
00798       if (sals.nelts > 1)
00799         {
00800           ambiguous_line_spec (&sals);
00801           xfree (sals.sals);
00802           return;
00803         }
00804 
00805       sal = sals.sals[0];
00806       xfree (sals.sals);
00807 
00808       if (*arg1)
00809         error (_("Junk at end of line specification."));
00810 
00811       /* If line was specified by address, first print exactly which
00812          line, and which file.  In this case, sal.symtab == 0 means
00813          address is outside of all known source files, not that user
00814          failed to give a filename.  */
00815       if (*arg == '*')
00816         {
00817           struct gdbarch *gdbarch;
00818 
00819           if (sal.symtab == 0)
00820             /* FIXME-32x64--assumes sal.pc fits in long.  */
00821             error (_("No source file for address %s."),
00822                    hex_string ((unsigned long) sal.pc));
00823 
00824           gdbarch = get_objfile_arch (sal.symtab->objfile);
00825           sym = find_pc_function (sal.pc);
00826           if (sym)
00827             printf_filtered ("%s is in %s (%s:%d).\n",
00828                              paddress (gdbarch, sal.pc),
00829                              SYMBOL_PRINT_NAME (sym),
00830                              symtab_to_filename_for_display (sal.symtab),
00831                              sal.line);
00832           else
00833             printf_filtered ("%s is at %s:%d.\n",
00834                              paddress (gdbarch, sal.pc),
00835                              symtab_to_filename_for_display (sal.symtab),
00836                              sal.line);
00837         }
00838 
00839       /* If what was given does not imply a symtab, it must be an
00840          undebuggable symbol which means no source code.  */
00841 
00842       if (sal.symtab == 0)
00843         error (_("No line number known for %s."), arg);
00844     }
00845 
00846   if ((editor = (char *) getenv ("EDITOR")) == NULL)
00847       editor = "/bin/ex";
00848 
00849   fn = symtab_to_fullname (sal.symtab);
00850 
00851   /* Quote the file name, in case it has whitespace or other special
00852      characters.  */
00853   p = xstrprintf ("%s +%d \"%s\"", editor, sal.line, fn);
00854   shell_escape (p, from_tty);
00855   xfree (p);
00856 }
00857 
00858 static void
00859 list_command (char *arg, int from_tty)
00860 {
00861   struct symtabs_and_lines sals, sals_end;
00862   struct symtab_and_line sal = { 0 };
00863   struct symtab_and_line sal_end = { 0 };
00864   struct symtab_and_line cursal = { 0 };
00865   struct symbol *sym;
00866   char *arg1;
00867   int no_end = 1;
00868   int dummy_end = 0;
00869   int dummy_beg = 0;
00870   int linenum_beg = 0;
00871   char *p;
00872 
00873   /* Pull in the current default source line if necessary.  */
00874   if (arg == 0 || arg[0] == '+' || arg[0] == '-')
00875     {
00876       set_default_source_symtab_and_line ();
00877       cursal = get_current_source_symtab_and_line ();
00878     }
00879 
00880   /* "l" or "l +" lists next ten lines.  */
00881 
00882   if (arg == 0 || strcmp (arg, "+") == 0)
00883     {
00884       print_source_lines (cursal.symtab, cursal.line,
00885                           cursal.line + get_lines_to_list (), 0);
00886       return;
00887     }
00888 
00889   /* "l -" lists previous ten lines, the ones before the ten just
00890      listed.  */
00891   if (strcmp (arg, "-") == 0)
00892     {
00893       print_source_lines (cursal.symtab,
00894                           max (get_first_line_listed () 
00895                                - get_lines_to_list (), 1),
00896                           get_first_line_listed (), 0);
00897       return;
00898     }
00899 
00900   /* Now if there is only one argument, decode it in SAL
00901      and set NO_END.
00902      If there are two arguments, decode them in SAL and SAL_END
00903      and clear NO_END; however, if one of the arguments is blank,
00904      set DUMMY_BEG or DUMMY_END to record that fact.  */
00905 
00906   if (!have_full_symbols () && !have_partial_symbols ())
00907     error (_("No symbol table is loaded.  Use the \"file\" command."));
00908 
00909   arg1 = arg;
00910   if (*arg1 == ',')
00911     dummy_beg = 1;
00912   else
00913     {
00914       sals = decode_line_1 (&arg1, DECODE_LINE_LIST_MODE, 0, 0);
00915 
00916       filter_sals (&sals);
00917       if (!sals.nelts)
00918         return;                 /*  C++  */
00919       if (sals.nelts > 1)
00920         {
00921           ambiguous_line_spec (&sals);
00922           xfree (sals.sals);
00923           return;
00924         }
00925 
00926       sal = sals.sals[0];
00927       xfree (sals.sals);
00928     }
00929 
00930   /* Record whether the BEG arg is all digits.  */
00931 
00932   for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
00933   linenum_beg = (p == arg1);
00934 
00935   while (*arg1 == ' ' || *arg1 == '\t')
00936     arg1++;
00937   if (*arg1 == ',')
00938     {
00939       no_end = 0;
00940       arg1++;
00941       while (*arg1 == ' ' || *arg1 == '\t')
00942         arg1++;
00943       if (*arg1 == 0)
00944         dummy_end = 1;
00945       else
00946         {
00947           if (dummy_beg)
00948             sals_end = decode_line_1 (&arg1, DECODE_LINE_LIST_MODE, 0, 0);
00949           else
00950             sals_end = decode_line_1 (&arg1, DECODE_LINE_LIST_MODE,
00951                                       sal.symtab, sal.line);
00952           filter_sals (&sals_end);
00953           if (sals_end.nelts == 0)
00954             return;
00955           if (sals_end.nelts > 1)
00956             {
00957               ambiguous_line_spec (&sals_end);
00958               xfree (sals_end.sals);
00959               return;
00960             }
00961           sal_end = sals_end.sals[0];
00962           xfree (sals_end.sals);
00963         }
00964     }
00965 
00966   if (*arg1)
00967     error (_("Junk at end of line specification."));
00968 
00969   if (!no_end && !dummy_beg && !dummy_end
00970       && sal.symtab != sal_end.symtab)
00971     error (_("Specified start and end are in different files."));
00972   if (dummy_beg && dummy_end)
00973     error (_("Two empty args do not say what lines to list."));
00974 
00975   /* If line was specified by address,
00976      first print exactly which line, and which file.
00977 
00978      In this case, sal.symtab == 0 means address is outside of all
00979      known source files, not that user failed to give a filename.  */
00980   if (*arg == '*')
00981     {
00982       struct gdbarch *gdbarch;
00983 
00984       if (sal.symtab == 0)
00985         /* FIXME-32x64--assumes sal.pc fits in long.  */
00986         error (_("No source file for address %s."),
00987                hex_string ((unsigned long) sal.pc));
00988 
00989       gdbarch = get_objfile_arch (sal.symtab->objfile);
00990       sym = find_pc_function (sal.pc);
00991       if (sym)
00992         printf_filtered ("%s is in %s (%s:%d).\n",
00993                          paddress (gdbarch, sal.pc),
00994                          SYMBOL_PRINT_NAME (sym),
00995                          symtab_to_filename_for_display (sal.symtab), sal.line);
00996       else
00997         printf_filtered ("%s is at %s:%d.\n",
00998                          paddress (gdbarch, sal.pc),
00999                          symtab_to_filename_for_display (sal.symtab), sal.line);
01000     }
01001 
01002   /* If line was not specified by just a line number, and it does not
01003      imply a symtab, it must be an undebuggable symbol which means no
01004      source code.  */
01005 
01006   if (!linenum_beg && sal.symtab == 0)
01007     error (_("No line number known for %s."), arg);
01008 
01009   /* If this command is repeated with RET,
01010      turn it into the no-arg variant.  */
01011 
01012   if (from_tty)
01013     *arg = 0;
01014 
01015   if (dummy_beg && sal_end.symtab == 0)
01016     error (_("No default source file yet.  Do \"help list\"."));
01017   if (dummy_beg)
01018     print_source_lines (sal_end.symtab,
01019                         max (sal_end.line - (get_lines_to_list () - 1), 1),
01020                         sal_end.line + 1, 0);
01021   else if (sal.symtab == 0)
01022     error (_("No default source file yet.  Do \"help list\"."));
01023   else if (no_end)
01024     {
01025       int first_line = sal.line - get_lines_to_list () / 2;
01026 
01027       if (first_line < 1) first_line = 1;
01028 
01029       print_source_lines (sal.symtab,
01030                           first_line,
01031                           first_line + get_lines_to_list (),
01032                           0);
01033     }
01034   else
01035     print_source_lines (sal.symtab, sal.line,
01036                         (dummy_end
01037                          ? sal.line + get_lines_to_list ()
01038                          : sal_end.line + 1),
01039                         0);
01040 }
01041 
01042 /* Subroutine of disassemble_command to simplify it.
01043    Perform the disassembly.
01044    NAME is the name of the function if known, or NULL.
01045    [LOW,HIGH) are the range of addresses to disassemble.
01046    MIXED is non-zero to print source with the assembler.  */
01047 
01048 static void
01049 print_disassembly (struct gdbarch *gdbarch, const char *name,
01050                    CORE_ADDR low, CORE_ADDR high, int flags)
01051 {
01052 #if defined(TUI)
01053   if (!tui_is_window_visible (DISASSEM_WIN))
01054 #endif
01055     {
01056       printf_filtered ("Dump of assembler code ");
01057       if (name != NULL)
01058         printf_filtered ("for function %s:\n", name);
01059       else
01060         printf_filtered ("from %s to %s:\n",
01061                          paddress (gdbarch, low), paddress (gdbarch, high));
01062 
01063       /* Dump the specified range.  */
01064       gdb_disassembly (gdbarch, current_uiout, 0, flags, -1, low, high);
01065 
01066       printf_filtered ("End of assembler dump.\n");
01067       gdb_flush (gdb_stdout);
01068     }
01069 #if defined(TUI)
01070   else
01071     {
01072       tui_show_assembly (gdbarch, low);
01073     }
01074 #endif
01075 }
01076 
01077 /* Subroutine of disassemble_command to simplify it.
01078    Print a disassembly of the current function according to FLAGS.  */
01079 
01080 static void
01081 disassemble_current_function (int flags)
01082 {
01083   struct frame_info *frame;
01084   struct gdbarch *gdbarch;
01085   CORE_ADDR low, high, pc;
01086   const char *name;
01087 
01088   frame = get_selected_frame (_("No frame selected."));
01089   gdbarch = get_frame_arch (frame);
01090   pc = get_frame_address_in_block (frame);
01091   if (find_pc_partial_function (pc, &name, &low, &high) == 0)
01092     error (_("No function contains program counter for selected frame."));
01093 #if defined(TUI)
01094   /* NOTE: cagney/2003-02-13 The `tui_active' was previously
01095      `tui_version'.  */
01096   if (tui_active)
01097     /* FIXME: cagney/2004-02-07: This should be an observer.  */
01098     low = tui_get_low_disassembly_address (gdbarch, low, pc);
01099 #endif
01100   low += gdbarch_deprecated_function_start_offset (gdbarch);
01101 
01102   print_disassembly (gdbarch, name, low, high, flags);
01103 }
01104 
01105 /* Dump a specified section of assembly code.
01106 
01107    Usage:
01108      disassemble [/mr]
01109        - dump the assembly code for the function of the current pc
01110      disassemble [/mr] addr
01111        - dump the assembly code for the function at ADDR
01112      disassemble [/mr] low,high
01113      disassemble [/mr] low,+length
01114        - dump the assembly code in the range [LOW,HIGH), or [LOW,LOW+length)
01115 
01116    A /m modifier will include source code with the assembly.
01117    A /r modifier will include raw instructions in hex with the assembly.  */
01118 
01119 static void
01120 disassemble_command (char *arg, int from_tty)
01121 {
01122   struct gdbarch *gdbarch = get_current_arch ();
01123   CORE_ADDR low, high;
01124   const char *name;
01125   CORE_ADDR pc;
01126   int flags;
01127   const char *p;
01128 
01129   p = arg;
01130   name = NULL;
01131   flags = 0;
01132 
01133   if (p && *p == '/')
01134     {
01135       ++p;
01136 
01137       if (*p == '\0')
01138         error (_("Missing modifier."));
01139 
01140       while (*p && ! isspace (*p))
01141         {
01142           switch (*p++)
01143             {
01144             case 'm':
01145               flags |= DISASSEMBLY_SOURCE;
01146               break;
01147             case 'r':
01148               flags |= DISASSEMBLY_RAW_INSN;
01149               break;
01150             default:
01151               error (_("Invalid disassembly modifier."));
01152             }
01153         }
01154 
01155       p = skip_spaces_const (p);
01156     }
01157 
01158   if (! p || ! *p)
01159     {
01160       flags |= DISASSEMBLY_OMIT_FNAME;
01161       disassemble_current_function (flags);
01162       return;
01163     }
01164 
01165   pc = value_as_address (parse_to_comma_and_eval (&p));
01166   if (p[0] == ',')
01167     ++p;
01168   if (p[0] == '\0')
01169     {
01170       /* One argument.  */
01171       if (find_pc_partial_function (pc, &name, &low, &high) == 0)
01172         error (_("No function contains specified address."));
01173 #if defined(TUI)
01174       /* NOTE: cagney/2003-02-13 The `tui_active' was previously
01175          `tui_version'.  */
01176       if (tui_active)
01177         /* FIXME: cagney/2004-02-07: This should be an observer.  */
01178         low = tui_get_low_disassembly_address (gdbarch, low, pc);
01179 #endif
01180       low += gdbarch_deprecated_function_start_offset (gdbarch);
01181       flags |= DISASSEMBLY_OMIT_FNAME;
01182     }
01183   else
01184     {
01185       /* Two arguments.  */
01186       int incl_flag = 0;
01187       low = pc;
01188       p = skip_spaces_const (p);
01189       if (p[0] == '+')
01190         {
01191           ++p;
01192           incl_flag = 1;
01193         }
01194       high = parse_and_eval_address (p);
01195       if (incl_flag)
01196         high += low;
01197     }
01198 
01199   print_disassembly (gdbarch, name, low, high, flags);
01200 }
01201 
01202 static void
01203 make_command (char *arg, int from_tty)
01204 {
01205   char *p;
01206 
01207   if (arg == 0)
01208     p = "make";
01209   else
01210     {
01211       p = xmalloc (sizeof ("make ") + strlen (arg));
01212       strcpy (p, "make ");
01213       strcpy (p + sizeof ("make ") - 1, arg);
01214     }
01215 
01216   shell_escape (p, from_tty);
01217 }
01218 
01219 static void
01220 show_user (char *args, int from_tty)
01221 {
01222   struct cmd_list_element *c;
01223   extern struct cmd_list_element *cmdlist;
01224 
01225   if (args)
01226     {
01227       const char *comname = args;
01228 
01229       c = lookup_cmd (&comname, cmdlist, "", 0, 1);
01230       /* c->user_commands would be NULL if it's a python command.  */
01231       if (c->class != class_user || !c->user_commands)
01232         error (_("Not a user command."));
01233       show_user_1 (c, "", args, gdb_stdout);
01234     }
01235   else
01236     {
01237       for (c = cmdlist; c; c = c->next)
01238         {
01239           if (c->class == class_user || c->prefixlist != NULL)
01240             show_user_1 (c, "", c->name, gdb_stdout);
01241         }
01242     }
01243 }
01244 
01245 /* Search through names of commands and documentations for a certain
01246    regular expression.  */
01247 
01248 static void 
01249 apropos_command (char *searchstr, int from_tty)
01250 {
01251   regex_t pattern;
01252   int code;
01253 
01254   if (searchstr == NULL)
01255     error (_("REGEXP string is empty"));
01256 
01257   code = regcomp (&pattern, searchstr, REG_ICASE);
01258   if (code == 0)
01259     {
01260       struct cleanup *cleanups;
01261 
01262       cleanups = make_regfree_cleanup (&pattern);
01263       apropos_cmd (gdb_stdout, cmdlist, &pattern, "");
01264       do_cleanups (cleanups);
01265     }
01266   else
01267     {
01268       char *err = get_regcomp_error (code, &pattern);
01269 
01270       make_cleanup (xfree, err);
01271       error (_("Error in regular expression: %s"), err);
01272     }
01273 }
01274 
01275 /* Subroutine of alias_command to simplify it.
01276    Return the first N elements of ARGV flattened back to a string
01277    with a space separating each element.
01278    ARGV may not be NULL.
01279    This does not take care of quoting elements in case they contain spaces
01280    on purpose.  */
01281 
01282 static dyn_string_t
01283 argv_to_dyn_string (char **argv, int n)
01284 {
01285   int i;
01286   dyn_string_t result = dyn_string_new (10);
01287 
01288   gdb_assert (argv != NULL);
01289   gdb_assert (n >= 0 && n <= countargv (argv));
01290 
01291   for (i = 0; i < n; ++i)
01292     {
01293       if (i > 0)
01294         dyn_string_append_char (result, ' ');
01295       dyn_string_append_cstr (result, argv[i]);
01296     }
01297 
01298   return result;
01299 }
01300 
01301 /* Subroutine of alias_command to simplify it.
01302    Return TRUE if COMMAND exists, unambiguously.  Otherwise FALSE.  */
01303 
01304 static int
01305 valid_command_p (const char *command)
01306 {
01307   struct cmd_list_element *c;
01308 
01309   c = lookup_cmd_1 (& command, cmdlist, NULL, 1);
01310 
01311   if (c == NULL || c == (struct cmd_list_element *) -1)
01312     return FALSE;
01313 
01314   /* This is the slightly tricky part.
01315      lookup_cmd_1 will return a pointer to the last part of COMMAND
01316      to match, leaving COMMAND pointing at the remainder.  */
01317   while (*command == ' ' || *command == '\t')
01318     ++command;
01319   return *command == '\0';
01320 }
01321 
01322 /* Make an alias of an existing command.  */
01323 
01324 static void
01325 alias_command (char *args, int from_tty)
01326 {
01327   int i, alias_argc, command_argc;
01328   int abbrev_flag = 0;
01329   char *args2, *equals, *alias, *command;
01330   char **alias_argv, **command_argv;
01331   dyn_string_t alias_dyn_string, command_dyn_string;
01332   struct cleanup *cleanup;
01333   static const char usage[] = N_("Usage: alias [-a] [--] ALIAS = COMMAND");
01334 
01335   if (args == NULL || strchr (args, '=') == NULL)
01336     error (_(usage));
01337 
01338   args2 = xstrdup (args);
01339   cleanup = make_cleanup (xfree, args2);
01340   equals = strchr (args2, '=');
01341   *equals = '\0';
01342   alias_argv = gdb_buildargv (args2);
01343   make_cleanup_freeargv (alias_argv);
01344   command_argv = gdb_buildargv (equals + 1);
01345   make_cleanup_freeargv (command_argv);
01346 
01347   for (i = 0; alias_argv[i] != NULL; )
01348     {
01349       if (strcmp (alias_argv[i], "-a") == 0)
01350         {
01351           ++alias_argv;
01352           abbrev_flag = 1;
01353         }
01354       else if (strcmp (alias_argv[i], "--") == 0)
01355         {
01356           ++alias_argv;
01357           break;
01358         }
01359       else
01360         break;
01361     }
01362 
01363   if (alias_argv[0] == NULL || command_argv[0] == NULL
01364       || *alias_argv[0] == '\0' || *command_argv[0] == '\0')
01365     error (_(usage));
01366 
01367   for (i = 0; alias_argv[i] != NULL; ++i)
01368     {
01369       if (! valid_user_defined_cmd_name_p (alias_argv[i]))
01370         {
01371           if (i == 0)
01372             error (_("Invalid command name: %s"), alias_argv[i]);
01373           else
01374             error (_("Invalid command element name: %s"), alias_argv[i]);
01375         }
01376     }
01377 
01378   alias_argc = countargv (alias_argv);
01379   command_argc = countargv (command_argv);
01380 
01381   /* COMMAND must exist.
01382      Reconstruct the command to remove any extraneous spaces,
01383      for better error messages.  */
01384   command_dyn_string = argv_to_dyn_string (command_argv, command_argc);
01385   make_cleanup_dyn_string_delete (command_dyn_string);
01386   command = dyn_string_buf (command_dyn_string);
01387   if (! valid_command_p (command))
01388     error (_("Invalid command to alias to: %s"), command);
01389 
01390   /* ALIAS must not exist.  */
01391   alias_dyn_string = argv_to_dyn_string (alias_argv, alias_argc);
01392   make_cleanup_dyn_string_delete (alias_dyn_string);
01393   alias = dyn_string_buf (alias_dyn_string);
01394   if (valid_command_p (alias))
01395     error (_("Alias already exists: %s"), alias);
01396 
01397   /* If ALIAS is one word, it is an alias for the entire COMMAND.
01398      Example: alias spe = set print elements
01399 
01400      Otherwise ALIAS and COMMAND must have the same number of words,
01401      and every word except the last must match; and the last word of
01402      ALIAS is made an alias of the last word of COMMAND.
01403      Example: alias set print elms = set pr elem
01404      Note that unambiguous abbreviations are allowed.  */
01405 
01406   if (alias_argc == 1)
01407     {
01408       /* add_cmd requires *we* allocate space for name, hence the xstrdup.  */
01409       add_com_alias (xstrdup (alias_argv[0]), command, class_alias,
01410                      abbrev_flag);
01411     }
01412   else
01413     {
01414       dyn_string_t alias_prefix_dyn_string, command_prefix_dyn_string;
01415       const char *alias_prefix, *command_prefix;
01416       struct cmd_list_element *c_alias, *c_command;
01417 
01418       if (alias_argc != command_argc)
01419         error (_("Mismatched command length between ALIAS and COMMAND."));
01420 
01421       /* Create copies of ALIAS and COMMAND without the last word,
01422          and use that to verify the leading elements match.  */
01423       alias_prefix_dyn_string =
01424         argv_to_dyn_string (alias_argv, alias_argc - 1);
01425       make_cleanup_dyn_string_delete (alias_prefix_dyn_string);
01426       command_prefix_dyn_string =
01427         argv_to_dyn_string (alias_argv, command_argc - 1);
01428       make_cleanup_dyn_string_delete (command_prefix_dyn_string);
01429       alias_prefix = dyn_string_buf (alias_prefix_dyn_string);
01430       command_prefix = dyn_string_buf (command_prefix_dyn_string);
01431 
01432       c_command = lookup_cmd_1 (& command_prefix, cmdlist, NULL, 1);
01433       /* We've already tried to look up COMMAND.  */
01434       gdb_assert (c_command != NULL
01435                   && c_command != (struct cmd_list_element *) -1);
01436       gdb_assert (c_command->prefixlist != NULL);
01437       c_alias = lookup_cmd_1 (& alias_prefix, cmdlist, NULL, 1);
01438       if (c_alias != c_command)
01439         error (_("ALIAS and COMMAND prefixes do not match."));
01440 
01441       /* add_cmd requires *we* allocate space for name, hence the xstrdup.  */
01442       add_alias_cmd (xstrdup (alias_argv[alias_argc - 1]),
01443                      command_argv[command_argc - 1],
01444                      class_alias, abbrev_flag, c_command->prefixlist);
01445     }
01446 
01447   do_cleanups (cleanup);
01448 }
01449 
01450 /* Print a list of files and line numbers which a user may choose from
01451    in order to list a function which was specified ambiguously (as
01452    with `list classname::overloadedfuncname', for example).  The
01453    vector in SALS provides the filenames and line numbers.  */
01454 
01455 static void
01456 ambiguous_line_spec (struct symtabs_and_lines *sals)
01457 {
01458   int i;
01459 
01460   for (i = 0; i < sals->nelts; ++i)
01461     printf_filtered (_("file: \"%s\", line number: %d\n"),
01462                      symtab_to_filename_for_display (sals->sals[i].symtab),
01463                      sals->sals[i].line);
01464 }
01465 
01466 /* Sort function for filter_sals.  */
01467 
01468 static int
01469 compare_symtabs (const void *a, const void *b)
01470 {
01471   const struct symtab_and_line *sala = a;
01472   const struct symtab_and_line *salb = b;
01473   int r;
01474 
01475   if (!sala->symtab->dirname)
01476     {
01477       if (salb->symtab->dirname)
01478         return -1;
01479     }
01480   else if (!salb->symtab->dirname)
01481     {
01482       if (sala->symtab->dirname)
01483         return 1;
01484     }
01485   else
01486     {
01487       r = filename_cmp (sala->symtab->dirname, salb->symtab->dirname);
01488       if (r)
01489         return r;
01490     }
01491 
01492   r = filename_cmp (sala->symtab->filename, salb->symtab->filename);
01493   if (r)
01494     return r;
01495 
01496   if (sala->line < salb->line)
01497     return -1;
01498   return sala->line == salb->line ? 0 : 1;
01499 }
01500 
01501 /* Remove any SALs that do not match the current program space, or
01502    which appear to be "file:line" duplicates.  */
01503 
01504 static void
01505 filter_sals (struct symtabs_and_lines *sals)
01506 {
01507   int i, out, prev;
01508 
01509   out = 0;
01510   for (i = 0; i < sals->nelts; ++i)
01511     {
01512       if (sals->sals[i].pspace == current_program_space
01513           && sals->sals[i].symtab != NULL)
01514         {
01515           sals->sals[out] = sals->sals[i];
01516           ++out;
01517         }
01518     }
01519   sals->nelts = out;
01520 
01521   qsort (sals->sals, sals->nelts, sizeof (struct symtab_and_line),
01522          compare_symtabs);
01523 
01524   out = 1;
01525   prev = 0;
01526   for (i = 1; i < sals->nelts; ++i)
01527     {
01528       if (compare_symtabs (&sals->sals[prev], &sals->sals[i]))
01529         {
01530           /* Symtabs differ.  */
01531           sals->sals[out] = sals->sals[i];
01532           prev = out;
01533           ++out;
01534         }
01535     }
01536 
01537   if (sals->nelts == 0)
01538     {
01539       xfree (sals->sals);
01540       sals->sals = NULL;
01541     }
01542   else
01543     sals->nelts = out;
01544 }
01545 
01546 static void
01547 set_debug (char *arg, int from_tty)
01548 {
01549   printf_unfiltered (_("\"set debug\" must be followed by "
01550                        "the name of a debug subcommand.\n"));
01551   help_list (setdebuglist, "set debug ", -1, gdb_stdout);
01552 }
01553 
01554 static void
01555 show_debug (char *args, int from_tty)
01556 {
01557   cmd_show_list (showdebuglist, from_tty, "");
01558 }
01559 
01560 void
01561 init_cmd_lists (void)
01562 {
01563   max_user_call_depth = 1024;
01564 }
01565 
01566 static void
01567 show_info_verbose (struct ui_file *file, int from_tty,
01568                    struct cmd_list_element *c,
01569                    const char *value)
01570 {
01571   if (info_verbose)
01572     fprintf_filtered (file,
01573                       _("Verbose printing of informational messages is %s.\n"),
01574                       value);
01575   else
01576     fprintf_filtered (file, _("Verbosity is %s.\n"), value);
01577 }
01578 
01579 static void
01580 show_history_expansion_p (struct ui_file *file, int from_tty,
01581                           struct cmd_list_element *c, const char *value)
01582 {
01583   fprintf_filtered (file, _("History expansion on command input is %s.\n"),
01584                     value);
01585 }
01586 
01587 static void
01588 show_remote_debug (struct ui_file *file, int from_tty,
01589                    struct cmd_list_element *c, const char *value)
01590 {
01591   fprintf_filtered (file, _("Debugging of remote protocol is %s.\n"),
01592                     value);
01593 }
01594 
01595 static void
01596 show_remote_timeout (struct ui_file *file, int from_tty,
01597                      struct cmd_list_element *c, const char *value)
01598 {
01599   fprintf_filtered (file,
01600                     _("Timeout limit to wait for target to respond is %s.\n"),
01601                     value);
01602 }
01603 
01604 static void
01605 show_max_user_call_depth (struct ui_file *file, int from_tty,
01606                           struct cmd_list_element *c, const char *value)
01607 {
01608   fprintf_filtered (file,
01609                     _("The max call depth for user-defined commands is %s.\n"),
01610                     value);
01611 }
01612 
01613 
01614 
01615 initialize_file_ftype _initialize_cli_cmds;
01616 
01617 void
01618 _initialize_cli_cmds (void)
01619 {
01620   struct cmd_list_element *c;
01621 
01622   /* Define the classes of commands.
01623      They will appear in the help list in alphabetical order.  */
01624 
01625   add_cmd ("internals", class_maintenance, NULL, _("\
01626 Maintenance commands.\n\
01627 Some gdb commands are provided just for use by gdb maintainers.\n\
01628 These commands are subject to frequent change, and may not be as\n\
01629 well documented as user commands."),
01630            &cmdlist);
01631   add_cmd ("obscure", class_obscure, NULL, _("Obscure features."), &cmdlist);
01632   add_cmd ("aliases", class_alias, NULL,
01633            _("Aliases of other commands."), &cmdlist);
01634   add_cmd ("user-defined", class_user, NULL, _("\
01635 User-defined commands.\n\
01636 The commands in this class are those defined by the user.\n\
01637 Use the \"define\" command to define a command."), &cmdlist);
01638   add_cmd ("support", class_support, NULL, _("Support facilities."), &cmdlist);
01639   if (!dbx_commands)
01640     add_cmd ("status", class_info, NULL, _("Status inquiries."), &cmdlist);
01641   add_cmd ("files", class_files, NULL, _("Specifying and examining files."),
01642            &cmdlist);
01643   add_cmd ("breakpoints", class_breakpoint, NULL,
01644            _("Making program stop at certain points."), &cmdlist);
01645   add_cmd ("data", class_vars, NULL, _("Examining data."), &cmdlist);
01646   add_cmd ("stack", class_stack, NULL, _("\
01647 Examining the stack.\n\
01648 The stack is made up of stack frames.  Gdb assigns numbers to stack frames\n\
01649 counting from zero for the innermost (currently executing) frame.\n\n\
01650 At any time gdb identifies one frame as the \"selected\" frame.\n\
01651 Variable lookups are done with respect to the selected frame.\n\
01652 When the program being debugged stops, gdb selects the innermost frame.\n\
01653 The commands below can be used to select other frames by number or address."),
01654            &cmdlist);
01655   add_cmd ("running", class_run, NULL, _("Running the program."), &cmdlist);
01656 
01657   /* Define general commands.  */
01658 
01659   add_com ("pwd", class_files, pwd_command, _("\
01660 Print working directory.  This is used for your program as well."));
01661 
01662   c = add_cmd ("cd", class_files, cd_command, _("\
01663 Set working directory to DIR for debugger and program being debugged.\n\
01664 The change does not take effect for the program being debugged\n\
01665 until the next time it is started."), &cmdlist);
01666   set_cmd_completer (c, filename_completer);
01667 
01668   add_com ("echo", class_support, echo_command, _("\
01669 Print a constant string.  Give string as argument.\n\
01670 C escape sequences may be used in the argument.\n\
01671 No newline is added at the end of the argument;\n\
01672 use \"\\n\" if you want a newline to be printed.\n\
01673 Since leading and trailing whitespace are ignored in command arguments,\n\
01674 if you want to print some you must use \"\\\" before leading whitespace\n\
01675 to be printed or after trailing whitespace."));
01676 
01677   add_setshow_enum_cmd ("script-extension", class_support,
01678                         script_ext_enums, &script_ext_mode, _("\
01679 Set mode for script filename extension recognition."), _("\
01680 Show mode for script filename extension recognition."), _("\
01681 off  == no filename extension recognition (all sourced files are GDB scripts)\n\
01682 soft == evaluate script according to filename extension, fallback to GDB script"
01683   "\n\
01684 strict == evaluate script according to filename extension, error if not supported"
01685   ),
01686                         NULL,
01687                         show_script_ext_mode,
01688                         &setlist, &showlist);
01689 
01690   add_com ("quit", class_support, quit_command, _("Exit gdb."));
01691   c = add_com ("help", class_support, help_command,
01692                _("Print list of commands."));
01693   set_cmd_completer (c, command_completer);
01694   add_com_alias ("q", "quit", class_support, 1);
01695   add_com_alias ("h", "help", class_support, 1);
01696 
01697   add_setshow_boolean_cmd ("verbose", class_support, &info_verbose, _("\
01698 Set verbosity."), _("\
01699 Show verbosity."), NULL,
01700                            set_verbose,
01701                            show_info_verbose,
01702                            &setlist, &showlist);
01703 
01704   add_prefix_cmd ("history", class_support, set_history,
01705                   _("Generic command for setting command history parameters."),
01706                   &sethistlist, "set history ", 0, &setlist);
01707   add_prefix_cmd ("history", class_support, show_history,
01708                   _("Generic command for showing command history parameters."),
01709                   &showhistlist, "show history ", 0, &showlist);
01710 
01711   add_setshow_boolean_cmd ("expansion", no_class, &history_expansion_p, _("\
01712 Set history expansion on command input."), _("\
01713 Show history expansion on command input."), _("\
01714 Without an argument, history expansion is enabled."),
01715                            NULL,
01716                            show_history_expansion_p,
01717                            &sethistlist, &showhistlist);
01718 
01719   add_prefix_cmd ("info", class_info, info_command, _("\
01720 Generic command for showing things about the program being debugged."),
01721                   &infolist, "info ", 0, &cmdlist);
01722   add_com_alias ("i", "info", class_info, 1);
01723   add_com_alias ("inf", "info", class_info, 1);
01724 
01725   add_com ("complete", class_obscure, complete_command,
01726            _("List the completions for the rest of the line as a command."));
01727 
01728   add_prefix_cmd ("show", class_info, show_command, _("\
01729 Generic command for showing things about the debugger."),
01730                   &showlist, "show ", 0, &cmdlist);
01731   /* Another way to get at the same thing.  */
01732   add_info ("set", show_command, _("Show all GDB settings."));
01733 
01734   add_cmd ("commands", no_set_class, show_commands, _("\
01735 Show the history of commands you typed.\n\
01736 You can supply a command number to start with, or a `+' to start after\n\
01737 the previous command number shown."),
01738            &showlist);
01739 
01740   add_cmd ("version", no_set_class, show_version,
01741            _("Show what version of GDB this is."), &showlist);
01742 
01743   add_cmd ("configuration", no_set_class, show_configuration,
01744            _("Show how GDB was configured at build time."), &showlist);
01745 
01746   add_setshow_zinteger_cmd ("remote", no_class, &remote_debug, _("\
01747 Set debugging of remote protocol."), _("\
01748 Show debugging of remote protocol."), _("\
01749 When enabled, each packet sent or received with the remote target\n\
01750 is displayed."),
01751                             NULL,
01752                             show_remote_debug,
01753                             &setdebuglist, &showdebuglist);
01754 
01755   add_setshow_zuinteger_unlimited_cmd ("remotetimeout", no_class,
01756                                        &remote_timeout, _("\
01757 Set timeout limit to wait for target to respond."), _("\
01758 Show timeout limit to wait for target to respond."), _("\
01759 This value is used to set the time limit for gdb to wait for a response\n\
01760 from the target."),
01761                                        NULL,
01762                                        show_remote_timeout,
01763                                        &setlist, &showlist);
01764 
01765   add_prefix_cmd ("debug", no_class, set_debug,
01766                   _("Generic command for setting gdb debugging flags"),
01767                   &setdebuglist, "set debug ", 0, &setlist);
01768 
01769   add_prefix_cmd ("debug", no_class, show_debug,
01770                   _("Generic command for showing gdb debugging flags"),
01771                   &showdebuglist, "show debug ", 0, &showlist);
01772 
01773   c = add_com ("shell", class_support, shell_escape, _("\
01774 Execute the rest of the line as a shell command.\n\
01775 With no arguments, run an inferior shell."));
01776   set_cmd_completer (c, filename_completer);
01777 
01778   c = add_com ("edit", class_files, edit_command, _("\
01779 Edit specified file or function.\n\
01780 With no argument, edits file containing most recent line listed.\n\
01781 Editing targets can be specified in these ways:\n\
01782   FILE:LINENUM, to edit at that line in that file,\n\
01783   FUNCTION, to edit at the beginning of that function,\n\
01784   FILE:FUNCTION, to distinguish among like-named static functions.\n\
01785   *ADDRESS, to edit at the line containing that address.\n\
01786 Uses EDITOR environment variable contents as editor (or ex as default)."));
01787 
01788   c->completer = location_completer;
01789 
01790   add_com ("list", class_files, list_command, _("\
01791 List specified function or line.\n\
01792 With no argument, lists ten more lines after or around previous listing.\n\
01793 \"list -\" lists the ten lines before a previous ten-line listing.\n\
01794 One argument specifies a line, and ten lines are listed around that line.\n\
01795 Two arguments with comma between specify starting and ending lines to list.\n\
01796 Lines can be specified in these ways:\n\
01797   LINENUM, to list around that line in current file,\n\
01798   FILE:LINENUM, to list around that line in that file,\n\
01799   FUNCTION, to list around beginning of that function,\n\
01800   FILE:FUNCTION, to distinguish among like-named static functions.\n\
01801   *ADDRESS, to list around the line containing that address.\n\
01802 With two args if one is empty it stands for ten lines away from \
01803 the other arg."));
01804 
01805   if (!xdb_commands)
01806     add_com_alias ("l", "list", class_files, 1);
01807   else
01808     add_com_alias ("v", "list", class_files, 1);
01809 
01810   if (dbx_commands)
01811     add_com_alias ("file", "list", class_files, 1);
01812 
01813   c = add_com ("disassemble", class_vars, disassemble_command, _("\
01814 Disassemble a specified section of memory.\n\
01815 Default is the function surrounding the pc of the selected frame.\n\
01816 With a /m modifier, source lines are included (if available).\n\
01817 With a /r modifier, raw instructions in hex are included.\n\
01818 With a single argument, the function surrounding that address is dumped.\n\
01819 Two arguments (separated by a comma) are taken as a range of memory to dump,\n\
01820   in the form of \"start,end\", or \"start,+length\".\n\
01821 \n\
01822 Note that the address is interpreted as an expression, not as a location\n\
01823 like in the \"break\" command.\n\
01824 So, for example, if you want to disassemble function bar in file foo.c\n\
01825 you must type \"disassemble 'foo.c'::bar\" and not \"disassemble foo.c:bar\"."));
01826   set_cmd_completer (c, location_completer);
01827   if (xdb_commands)
01828     add_com_alias ("va", "disassemble", class_xdb, 0);
01829 
01830   add_com_alias ("!", "shell", class_support, 0);
01831 
01832   c = add_com ("make", class_support, make_command, _("\
01833 Run the ``make'' program using the rest of the line as arguments."));
01834   set_cmd_completer (c, filename_completer);
01835   add_cmd ("user", no_class, show_user, _("\
01836 Show definitions of non-python user defined commands.\n\
01837 Argument is the name of the user defined command.\n\
01838 With no argument, show definitions of all user defined commands."), &showlist);
01839   add_com ("apropos", class_support, apropos_command,
01840            _("Search for commands matching a REGEXP"));
01841 
01842   add_setshow_uinteger_cmd ("max-user-call-depth", no_class,
01843                            &max_user_call_depth, _("\
01844 Set the max call depth for non-python user-defined commands."), _("\
01845 Show the max call depth for non-python user-defined commands."), NULL,
01846                             NULL,
01847                             show_max_user_call_depth,
01848                             &setlist, &showlist);
01849 
01850   add_setshow_boolean_cmd ("trace-commands", no_class, &trace_commands, _("\
01851 Set tracing of GDB CLI commands."), _("\
01852 Show state of GDB CLI command tracing."), _("\
01853 When 'on', each command is displayed as it is executed."),
01854                            NULL,
01855                            NULL,
01856                            &setlist, &showlist);
01857 
01858   c = add_com ("alias", class_support, alias_command, _("\
01859 Define a new command that is an alias of an existing command.\n\
01860 Usage: alias [-a] [--] ALIAS = COMMAND\n\
01861 ALIAS is the name of the alias command to create.\n\
01862 COMMAND is the command being aliased to.\n\
01863 If \"-a\" is specified, the command is an abbreviation,\n\
01864 and will not appear in help command list output.\n\
01865 \n\
01866 Examples:\n\
01867 Make \"spe\" an alias of \"set print elements\":\n\
01868   alias spe = set print elements\n\
01869 Make \"elms\" an alias of \"elements\" in the \"set print\" command:\n\
01870   alias -a set print elms = set print elements"));
01871 }
01872 
01873 void
01874 init_cli_cmds (void)
01875 {
01876   struct cmd_list_element *c;
01877   char *source_help_text;
01878 
01879   source_help_text = xstrprintf (_("\
01880 Read commands from a file named FILE.\n\
01881 \n\
01882 Usage: source [-s] [-v] FILE\n\
01883 -s: search for the script in the source search path,\n\
01884     even if FILE contains directories.\n\
01885 -v: each command in FILE is echoed as it is executed.\n\
01886 \n\
01887 Note that the file \"%s\" is read automatically in this way\n\
01888 when GDB is started."), gdbinit);
01889   c = add_cmd ("source", class_support, source_command,
01890                source_help_text, &cmdlist);
01891   set_cmd_completer (c, filename_completer);
01892 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines