3 Copyright (C) 2000-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "arch-utils.h"
22 #include "readline/tilde.h"
23 #include "completer.h"
24 #include "target.h" /* For baud_rate, remote_debug and remote_timeout. */
25 #include "gdbsupport/gdb_wait.h" /* For shell escape implementation. */
27 #include "gdbsupport/gdb_regex.h" /* Used by apropos_command. */
28 #include "gdb_vfork.h"
30 #include "expression.h"
34 #include "filenames.h" /* For DOSish file names. */
38 #include "tracepoint.h"
39 #include "gdbsupport/filestuff.h"
48 #include "cli/cli-decode.h"
49 #include "cli/cli-script.h"
50 #include "cli/cli-setshow.h"
51 #include "cli/cli-cmds.h"
52 #include "cli/cli-style.h"
53 #include "cli/cli-utils.h"
54 #include "cli/cli-style.h"
56 #include "extension.h"
57 #include "gdbsupport/pathstuff.h"
58 #include "gdbsupport/gdb_tilde_expand.h"
61 #include "tui/tui.h" /* For tui_active et.al. */
68 /* Prototypes for local utility functions */
70 static void print_sal_location (const symtab_and_line
&sal
);
72 static void ambiguous_line_spec (gdb::array_view
<const symtab_and_line
> sals
,
73 const char *format
, ...)
74 ATTRIBUTE_PRINTF (2, 3);
76 static void filter_sals (std::vector
<symtab_and_line
> &);
80 unsigned int max_user_call_depth
= 1024;
82 /* Define all cmd_list_elements. */
84 /* Chain containing all defined commands. */
86 struct cmd_list_element
*cmdlist
;
88 /* Chain containing all defined info subcommands. */
90 struct cmd_list_element
*infolist
;
92 /* Chain containing all defined enable subcommands. */
94 struct cmd_list_element
*enablelist
;
96 /* Chain containing all defined disable subcommands. */
98 struct cmd_list_element
*disablelist
;
100 /* Chain containing all defined stop subcommands. */
102 struct cmd_list_element
*stoplist
;
104 /* Chain containing all defined delete subcommands. */
106 struct cmd_list_element
*deletelist
;
108 /* Chain containing all defined detach subcommands. */
110 struct cmd_list_element
*detachlist
;
112 /* Chain containing all defined kill subcommands. */
114 struct cmd_list_element
*killlist
;
116 /* Chain containing all defined set subcommands */
118 struct cmd_list_element
*setlist
;
120 /* Chain containing all defined unset subcommands */
122 struct cmd_list_element
*unsetlist
;
124 /* Chain containing all defined show subcommands. */
126 struct cmd_list_element
*showlist
;
128 /* Chain containing all defined \"set history\". */
130 struct cmd_list_element
*sethistlist
;
132 /* Chain containing all defined \"show history\". */
134 struct cmd_list_element
*showhistlist
;
136 /* Chain containing all defined \"unset history\". */
138 struct cmd_list_element
*unsethistlist
;
140 /* Chain containing all defined maintenance subcommands. */
142 struct cmd_list_element
*maintenancelist
;
144 /* Chain containing all defined "maintenance info" subcommands. */
146 struct cmd_list_element
*maintenanceinfolist
;
148 /* Chain containing all defined "maintenance print" subcommands. */
150 struct cmd_list_element
*maintenanceprintlist
;
152 /* Chain containing all defined "maintenance check" subcommands. */
154 struct cmd_list_element
*maintenancechecklist
;
156 /* Chain containing all defined "maintenance flush" subcommands. */
158 struct cmd_list_element
*maintenanceflushlist
;
160 struct cmd_list_element
*setprintlist
;
162 struct cmd_list_element
*showprintlist
;
164 struct cmd_list_element
*setdebuglist
;
166 struct cmd_list_element
*showdebuglist
;
168 struct cmd_list_element
*setchecklist
;
170 struct cmd_list_element
*showchecklist
;
172 struct cmd_list_element
*setsourcelist
;
174 struct cmd_list_element
*showsourcelist
;
176 /* Command tracing state. */
178 int source_verbose
= 0;
179 bool trace_commands
= false;
181 /* 'script-extension' option support. */
183 static const char script_ext_off
[] = "off";
184 static const char script_ext_soft
[] = "soft";
185 static const char script_ext_strict
[] = "strict";
187 static const char *const script_ext_enums
[] = {
194 static const char *script_ext_mode
= script_ext_soft
;
197 /* User-controllable flag to suppress event notification on CLI. */
199 static bool user_wants_cli_suppress_notification
= false;
201 /* Utility used everywhere when at least one argument is needed and
205 error_no_arg (const char *why
)
207 error (_("Argument required (%s)."), why
);
210 /* This implements the "info" prefix command. Normally such commands
211 are automatically handled by add_basic_prefix_cmd, but in this case
212 a separate command is used so that it can be hooked into by
216 info_command (const char *arg
, int from_tty
)
218 help_list (infolist
, "info ", all_commands
, gdb_stdout
);
221 /* See cli/cli-cmds.h. */
224 with_command_1 (const char *set_cmd_prefix
,
225 cmd_list_element
*setlist
, const char *args
, int from_tty
)
228 error (_("Missing arguments."));
230 const char *delim
= strstr (args
, "--");
231 const char *nested_cmd
= nullptr;
234 error (_("Missing setting before '--' delimiter"));
236 if (delim
== nullptr || *skip_spaces (&delim
[2]) == '\0')
237 nested_cmd
= repeat_previous ();
239 cmd_list_element
*set_cmd
= lookup_cmd (&args
, setlist
, set_cmd_prefix
,
241 /*allow_unknown=*/ 0,
242 /*ignore_help_classes=*/ 1);
243 gdb_assert (set_cmd
!= nullptr);
245 if (!set_cmd
->var
.has_value ())
246 error (_("Cannot use this setting with the \"with\" command"));
248 std::string temp_value
249 = (delim
== nullptr ? args
: std::string (args
, delim
- args
));
251 if (nested_cmd
== nullptr)
252 nested_cmd
= skip_spaces (delim
+ 2);
254 gdb_assert (set_cmd
->var
.has_value ());
255 std::string org_value
= get_setshow_command_value_string (*set_cmd
->var
);
257 /* Tweak the setting to the new temporary value. */
258 do_set_command (temp_value
.c_str (), from_tty
, set_cmd
);
262 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
264 /* Execute the nested command. */
265 execute_command (nested_cmd
, from_tty
);
267 catch (const gdb_exception
&ex
)
269 /* Restore the setting and rethrow. If restoring the setting
270 throws, swallow the new exception and warn. There's nothing
271 else we can reasonably do. */
274 do_set_command (org_value
.c_str (), from_tty
, set_cmd
);
276 catch (const gdb_exception
&ex2
)
278 warning (_("Couldn't restore setting: %s"), ex2
.what ());
284 /* Restore the setting. */
285 do_set_command (org_value
.c_str (), from_tty
, set_cmd
);
288 /* See cli/cli-cmds.h. */
291 with_command_completer_1 (const char *set_cmd_prefix
,
292 completion_tracker
&tracker
,
295 tracker
.set_use_custom_word_point (true);
297 const char *delim
= strstr (text
, "--");
299 /* If we're still not past the "--" delimiter, complete the "with"
300 command as if it was a "set" command. */
303 || !isspace (delim
[-1])
304 || !(isspace (delim
[2]) || delim
[2] == '\0'))
306 std::string new_text
= std::string (set_cmd_prefix
) + text
;
307 tracker
.advance_custom_word_point_by (-(int) strlen (set_cmd_prefix
));
308 complete_nested_command_line (tracker
, new_text
.c_str ());
312 /* We're past the "--" delimiter. Complete on the sub command. */
313 const char *nested_cmd
= skip_spaces (delim
+ 2);
314 tracker
.advance_custom_word_point_by (nested_cmd
- text
);
315 complete_nested_command_line (tracker
, nested_cmd
);
318 /* The "with" command. */
321 with_command (const char *args
, int from_tty
)
323 with_command_1 ("set ", setlist
, args
, from_tty
);
326 /* "with" command completer. */
329 with_command_completer (struct cmd_list_element
*ignore
,
330 completion_tracker
&tracker
,
331 const char *text
, const char * /*word*/)
333 with_command_completer_1 ("set ", tracker
, text
);
336 /* Look up the contents of TEXT as a command usable with default args.
337 Throws an error if no such command is found.
338 Return the found command and advances TEXT past the found command.
339 If the found command is a postfix command, set *PREFIX_CMD to its
342 static struct cmd_list_element
*
343 lookup_cmd_for_default_args (const char **text
,
344 struct cmd_list_element
**prefix_cmd
)
346 const char *orig_text
= *text
;
347 struct cmd_list_element
*lcmd
;
349 if (*text
== nullptr || skip_spaces (*text
) == nullptr)
350 error (_("ALIAS missing."));
352 /* We first use lookup_cmd to verify TEXT unambiguously identifies
354 lcmd
= lookup_cmd (text
, cmdlist
, "", NULL
,
355 /*allow_unknown=*/ 0,
356 /*ignore_help_classes=*/ 1);
358 /* Note that we accept default args for prefix commands,
359 as a prefix command can also be a valid usable
360 command accepting some arguments.
361 For example, "thread apply" applies a command to a
362 list of thread ids, and is also the prefix command for
365 /* We have an unambiguous command for which default args
366 can be specified. What remains after having found LCMD
367 is either spaces, or the default args character. */
369 /* We then use lookup_cmd_composition to detect if the user
370 has specified an alias, and find the possible prefix_cmd
372 struct cmd_list_element
*alias
, *cmd
;
373 lookup_cmd_composition
374 (std::string (orig_text
, *text
- orig_text
).c_str (),
375 &alias
, prefix_cmd
, &cmd
);
376 gdb_assert (cmd
!= nullptr);
377 gdb_assert (cmd
== lcmd
);
378 if (alias
!= nullptr)
384 /* Provide documentation on command or list given by COMMAND. FROM_TTY
388 help_command (const char *command
, int from_tty
)
390 help_cmd (command
, gdb_stdout
);
394 /* Note: The "complete" command is used by Emacs to implement completion.
395 [Is that why this function writes output with *_unfiltered?] */
398 complete_command (const char *arg
, int from_tty
)
402 if (max_completions
== 0)
404 /* Only print this for non-mi frontends. An MI frontend may not
405 be able to handle this. */
406 if (!current_uiout
->is_mi_like_p ())
408 printf_unfiltered (_("max-completions is zero,"
409 " completion is disabled.\n"));
417 int quote_char
= '\0';
420 completion_result result
= complete (arg
, &word
, "e_char
);
422 if (result
.number_matches
!= 0)
424 std::string
arg_prefix (arg
, word
- arg
);
426 if (result
.number_matches
== 1)
427 printf_unfiltered ("%s%s\n", arg_prefix
.c_str (), result
.match_list
[0]);
430 result
.sort_match_list ();
432 for (size_t i
= 0; i
< result
.number_matches
; i
++)
434 printf_unfiltered ("%s%s",
436 result
.match_list
[i
+ 1]);
438 printf_unfiltered ("%c", quote_char
);
439 printf_unfiltered ("\n");
443 if (result
.number_matches
== max_completions
)
445 /* ARG_PREFIX and WORD are included in the output so that emacs
446 will include the message in the output. */
447 printf_unfiltered (_("%s%s %s\n"),
448 arg_prefix
.c_str (), word
,
449 get_max_completions_reached_message ());
455 is_complete_command (struct cmd_list_element
*c
)
457 return cmd_simple_func_eq (c
, complete_command
);
461 show_version (const char *args
, int from_tty
)
463 print_gdb_version (gdb_stdout
, true);
468 show_configuration (const char *args
, int from_tty
)
470 print_gdb_configuration (gdb_stdout
);
473 /* Handle the quit command. */
476 quit_command (const char *args
, int from_tty
)
480 /* An optional expression may be used to cause gdb to terminate with
481 the value of that expression. */
484 struct value
*val
= parse_and_eval (args
);
486 exit_code
= (int) value_as_long (val
);
489 if (!quit_confirm ())
490 error (_("Not confirmed."));
494 query_if_trace_running (from_tty
);
496 catch (const gdb_exception_error
&ex
)
498 if (ex
.error
== TARGET_CLOSE_ERROR
)
499 /* We don't care about this since we're quitting anyway, so keep
501 exception_print (gdb_stderr
, ex
);
503 /* Rethrow, to properly handle error (_("Not confirmed.")). */
507 quit_force (args
? &exit_code
: NULL
, from_tty
);
511 pwd_command (const char *args
, int from_tty
)
514 error (_("The \"pwd\" command does not take an argument: %s"), args
);
516 gdb::unique_xmalloc_ptr
<char> cwd (getcwd (NULL
, 0));
519 error (_("Error finding name of working directory: %s"),
520 safe_strerror (errno
));
522 if (strcmp (cwd
.get (), current_directory
) != 0)
523 gdb_printf (_("Working directory %ps\n (canonically %ps).\n"),
524 styled_string (file_name_style
.style (),
526 styled_string (file_name_style
.style (), cwd
.get ()));
528 gdb_printf (_("Working directory %ps.\n"),
529 styled_string (file_name_style
.style (),
534 cd_command (const char *dir
, int from_tty
)
537 /* Found something other than leading repetitions of "/..". */
541 /* If the new directory is absolute, repeat is a no-op; if relative,
542 repeat might be useful but is more likely to be a mistake. */
545 gdb::unique_xmalloc_ptr
<char> dir_holder
546 (tilde_expand (dir
!= NULL
? dir
: "~"));
547 dir
= dir_holder
.get ();
550 perror_with_name (dir
);
552 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
553 /* There's too much mess with DOSish names like "d:", "d:.",
554 "d:./foo" etc. Instead of having lots of special #ifdef'ed code,
555 simply get the canonicalized name of the current directory. */
556 gdb::unique_xmalloc_ptr
<char> cwd (getcwd (NULL
, 0));
561 if (IS_DIR_SEPARATOR (dir
[len
- 1]))
563 /* Remove the trailing slash unless this is a root directory
564 (including a drive letter on non-Unix systems). */
565 if (!(len
== 1) /* "/" */
566 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
567 && !(len
== 3 && dir
[1] == ':') /* "d:/" */
573 dir_holder
.reset (savestring (dir
, len
));
574 if (IS_ABSOLUTE_PATH (dir_holder
.get ()))
576 xfree (current_directory
);
577 current_directory
= dir_holder
.release ();
581 if (IS_DIR_SEPARATOR (current_directory
[strlen (current_directory
) - 1]))
582 current_directory
= concat (current_directory
, dir_holder
.get (),
585 current_directory
= concat (current_directory
, SLASH_STRING
,
586 dir_holder
.get (), (char *) NULL
);
589 /* Now simplify any occurrences of `.' and `..' in the pathname. */
592 for (p
= current_directory
; *p
;)
594 if (IS_DIR_SEPARATOR (p
[0]) && p
[1] == '.'
595 && (p
[2] == 0 || IS_DIR_SEPARATOR (p
[2])))
596 memmove (p
, p
+ 2, strlen (p
+ 2) + 1);
597 else if (IS_DIR_SEPARATOR (p
[0]) && p
[1] == '.' && p
[2] == '.'
598 && (p
[3] == 0 || IS_DIR_SEPARATOR (p
[3])))
602 /* Search backwards for the directory just before the "/.."
603 and obliterate it and the "/..". */
606 while (q
!= current_directory
&& !IS_DIR_SEPARATOR (q
[-1]))
609 if (q
== current_directory
)
610 /* current_directory is
611 a relative pathname ("can't happen"--leave it alone). */
615 memmove (q
- 1, p
+ 3, strlen (p
+ 3) + 1);
620 /* We are dealing with leading repetitions of "/..", for
621 example "/../..", which is the Mach super-root. */
631 forget_cached_source_info ();
634 pwd_command ((char *) 0, 1);
637 /* Show the current value of the 'script-extension' option. */
640 show_script_ext_mode (struct ui_file
*file
, int from_tty
,
641 struct cmd_list_element
*c
, const char *value
)
644 _("Script filename extension recognition is \"%s\".\n"),
648 /* Try to open SCRIPT_FILE.
649 If successful, the full path name is stored in *FULL_PATHP,
650 and the stream is returned.
651 If not successful, return NULL; errno is set for the last file
654 If SEARCH_PATH is non-zero, and the file isn't found in cwd,
655 search for it in the source search path. */
657 gdb::optional
<open_script
>
658 find_and_open_script (const char *script_file
, int search_path
)
661 openp_flags search_flags
= OPF_TRY_CWD_FIRST
| OPF_RETURN_REALPATH
;
662 gdb::optional
<open_script
> opened
;
664 gdb::unique_xmalloc_ptr
<char> file (tilde_expand (script_file
));
667 search_flags
|= OPF_SEARCH_IN_PATH
;
669 /* Search for and open 'file' on the search path used for source
670 files. Put the full location in *FULL_PATHP. */
671 gdb::unique_xmalloc_ptr
<char> full_path
;
672 fd
= openp (source_path
.c_str (), search_flags
,
673 file
.get (), O_RDONLY
, &full_path
);
678 FILE *result
= fdopen (fd
, FOPEN_RT
);
681 int save_errno
= errno
;
687 opened
.emplace (gdb_file_up (result
), std::move (full_path
));
692 /* Load script FILE, which has already been opened as STREAM.
693 FILE_TO_OPEN is the form of FILE to use if one needs to open the file.
694 This is provided as FILE may have been found via the source search path.
695 An important thing to note here is that FILE may be a symlink to a file
696 with a different or non-existing suffix, and thus one cannot infer the
697 extension language from FILE_TO_OPEN. */
700 source_script_from_stream (FILE *stream
, const char *file
,
701 const char *file_to_open
)
703 if (script_ext_mode
!= script_ext_off
)
705 const struct extension_language_defn
*extlang
706 = get_ext_lang_of_file (file
);
710 if (ext_lang_present_p (extlang
))
712 script_sourcer_func
*sourcer
713 = ext_lang_script_sourcer (extlang
);
715 gdb_assert (sourcer
!= NULL
);
716 sourcer (extlang
, stream
, file_to_open
);
719 else if (script_ext_mode
== script_ext_soft
)
721 /* Assume the file is a gdb script.
722 This is handled below. */
725 throw_ext_lang_unsupported (extlang
);
729 script_from_file (stream
, file
);
732 /* Worker to perform the "source" command.
734 If SEARCH_PATH is non-zero, and the file isn't found in cwd,
735 search for it in the source search path. */
738 source_script_with_search (const char *file
, int from_tty
, int search_path
)
741 if (file
== NULL
|| *file
== 0)
742 error (_("source command requires file name of file to source."));
744 gdb::optional
<open_script
> opened
= find_and_open_script (file
, search_path
);
747 /* The script wasn't found, or was otherwise inaccessible.
748 If the source command was invoked interactively, throw an
749 error. Otherwise (e.g. if it was invoked by a script),
750 just emit a warning, rather than cause an error. */
752 perror_with_name (file
);
755 perror_warning_with_name (file
);
760 /* The python support reopens the file, so we need to pass full_path here
761 in case the file was found on the search path. It's useful to do this
762 anyway so that error messages show the actual file used. But only do
763 this if we (may have) used search_path, as printing the full path in
764 errors for the non-search case can be more noise than signal. */
765 const char *file_to_open
;
766 std::string tilde_expanded_file
;
768 file_to_open
= opened
->full_path
.get ();
771 tilde_expanded_file
= gdb_tilde_expand (file
);
772 file_to_open
= tilde_expanded_file
.c_str ();
774 source_script_from_stream (opened
->stream
.get (), file
, file_to_open
);
777 /* Wrapper around source_script_with_search to export it to main.c
778 for use in loading .gdbinit scripts. */
781 source_script (const char *file
, int from_tty
)
783 source_script_with_search (file
, from_tty
, 0);
787 source_command (const char *args
, int from_tty
)
789 const char *file
= args
;
792 scoped_restore save_source_verbose
= make_scoped_restore (&source_verbose
);
794 /* -v causes the source command to run in verbose mode.
795 -s causes the file to be searched in the source search path,
796 even if the file name contains a '/'.
797 We still have to be able to handle filenames with spaces in a
798 backward compatible way, so buildargv is not appropriate. */
802 while (args
[0] != '\0')
804 /* Make sure leading white space does not break the
806 args
= skip_spaces (args
);
811 if (args
[1] == 'v' && isspace (args
[2]))
815 /* Skip passed -v. */
818 else if (args
[1] == 's' && isspace (args
[2]))
822 /* Skip passed -s. */
829 file
= skip_spaces (args
);
832 source_script_with_search (file
, from_tty
, search_path
);
837 echo_command (const char *text
, int from_tty
)
839 const char *p
= text
;
843 while ((c
= *p
++) != '\0')
847 /* \ at end of argument is used after spaces
848 so they won't be lost. */
852 c
= parse_escape (get_current_arch (), &p
);
854 gdb_printf ("%c", c
);
857 gdb_printf ("%c", c
);
860 gdb_stdout
->reset_style ();
862 /* Force this output to appear now. */
863 gdb_flush (gdb_stdout
);
866 /* Sets the last launched shell command convenience variables based on
870 exit_status_set_internal_vars (int exit_status
)
872 struct internalvar
*var_code
= lookup_internalvar ("_shell_exitcode");
873 struct internalvar
*var_signal
= lookup_internalvar ("_shell_exitsignal");
875 clear_internalvar (var_code
);
876 clear_internalvar (var_signal
);
878 /* Keep the logic here in sync with shell_internal_fn. */
880 if (WIFEXITED (exit_status
))
881 set_internalvar_integer (var_code
, WEXITSTATUS (exit_status
));
883 else if (WIFSIGNALED (exit_status
) && WTERMSIG (exit_status
) == -1)
885 /* The -1 condition can happen on MinGW, if we don't recognize
886 the fatal exception code encoded in the exit status; see
887 gdbsupport/gdb_wait.c. We don't want to lose information in
888 the exit status in that case. Record it as a normal exit
889 with the full exit status, including the higher 0xC0000000
891 set_internalvar_integer (var_code
, exit_status
);
894 else if (WIFSIGNALED (exit_status
))
895 set_internalvar_integer (var_signal
, WTERMSIG (exit_status
));
897 warning (_("unexpected shell command exit status %d"), exit_status
);
900 /* Run ARG under the shell, and return the exit status. If ARG is
901 NULL, run an interactive shell. */
904 run_under_shell (const char *arg
, int from_tty
)
906 #if defined(CANT_FORK) || \
907 (!defined(HAVE_WORKING_VFORK) && !defined(HAVE_WORKING_FORK))
908 /* If ARG is NULL, they want an inferior shell, but `system' just
909 reports if the shell is available when passed a NULL arg. */
910 int rc
= system (arg
? arg
: "");
913 arg
= "inferior shell";
916 gdb_printf (gdb_stderr
, "Cannot execute %s: %s\n", arg
,
917 safe_strerror (errno
));
919 gdb_printf (gdb_stderr
, "%s exited with status %d\n", arg
, rc
);
921 /* Make sure to return to the directory GDB thinks it is, in case
922 the shell command we just ran changed it. */
923 chdir (current_directory
);
926 #else /* Can fork. */
929 if ((pid
= vfork ()) == 0)
931 const char *p
, *user_shell
= get_shell ();
935 /* Get the name of the shell for arg0. */
936 p
= lbasename (user_shell
);
939 execl (user_shell
, p
, (char *) 0);
941 execl (user_shell
, p
, "-c", arg
, (char *) 0);
943 gdb_printf (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
944 safe_strerror (errno
));
949 waitpid (pid
, &status
, 0);
951 error (_("Fork failed"));
953 #endif /* Can fork. */
956 /* Escape out to the shell to run ARG. If ARG is NULL, launch and
957 interactive shell. Sets $_shell_exitcode and $_shell_exitsignal
958 convenience variables based on the exits status. */
961 shell_escape (const char *arg
, int from_tty
)
963 int status
= run_under_shell (arg
, from_tty
);
964 exit_status_set_internal_vars (status
);
967 /* Implementation of the "shell" command. */
970 shell_command (const char *arg
, int from_tty
)
972 shell_escape (arg
, from_tty
);
976 edit_command (const char *arg
, int from_tty
)
978 struct symtab_and_line sal
;
983 /* Pull in the current default source line if necessary. */
986 set_default_source_symtab_and_line ();
987 sal
= get_current_source_symtab_and_line ();
990 /* Bare "edit" edits file with present line. */
995 error (_("No default source file yet."));
996 sal
.line
+= get_lines_to_list () / 2;
1002 /* Now should only be one argument -- decode it in SAL. */
1004 location_spec_up locspec
= string_to_location_spec (&arg1
,
1008 error (_("Junk at end of line specification."));
1010 std::vector
<symtab_and_line
> sals
= decode_line_1 (locspec
.get (),
1011 DECODE_LINE_LIST_MODE
,
1020 if (sals
.size () > 1)
1022 ambiguous_line_spec (sals
,
1023 _("Specified line is ambiguous:\n"));
1029 /* If line was specified by address, first print exactly which
1030 line, and which file. In this case, sal.symtab == 0 means
1031 address is outside of all known source files, not that user
1032 failed to give a filename. */
1035 struct gdbarch
*gdbarch
;
1037 if (sal
.symtab
== 0)
1038 error (_("No source file for address %s."),
1039 paddress (get_current_arch (), sal
.pc
));
1041 gdbarch
= sal
.symtab
->compunit ()->objfile ()->arch ();
1042 sym
= find_pc_function (sal
.pc
);
1044 gdb_printf ("%s is in %s (%s:%d).\n",
1045 paddress (gdbarch
, sal
.pc
),
1047 symtab_to_filename_for_display (sal
.symtab
),
1050 gdb_printf ("%s is at %s:%d.\n",
1051 paddress (gdbarch
, sal
.pc
),
1052 symtab_to_filename_for_display (sal
.symtab
),
1056 /* If what was given does not imply a symtab, it must be an
1057 undebuggable symbol which means no source code. */
1059 if (sal
.symtab
== 0)
1060 error (_("No line number known for %s."), arg
);
1063 if ((editor
= getenv ("EDITOR")) == NULL
)
1066 fn
= symtab_to_fullname (sal
.symtab
);
1068 /* Quote the file name, in case it has whitespace or other special
1070 gdb::unique_xmalloc_ptr
<char> p
1071 = xstrprintf ("%s +%d \"%s\"", editor
, sal
.line
, fn
);
1072 shell_escape (p
.get (), from_tty
);
1075 /* The options for the "pipe" command. */
1077 struct pipe_cmd_opts
1080 std::string delimiter
;
1083 static const gdb::option::option_def pipe_cmd_option_defs
[] = {
1085 gdb::option::string_option_def
<pipe_cmd_opts
> {
1087 [] (pipe_cmd_opts
*opts
) { return &opts
->delimiter
; },
1089 N_("Indicates to use the specified delimiter string to separate\n\
1090 COMMAND from SHELL_COMMAND, in alternative to |. This is useful in\n\
1091 case COMMAND contains a | character."),
1096 /* Create an option_def_group for the "pipe" command's options, with
1099 static inline gdb::option::option_def_group
1100 make_pipe_cmd_options_def_group (pipe_cmd_opts
*opts
)
1102 return {{pipe_cmd_option_defs
}, opts
};
1105 /* Implementation of the "pipe" command. */
1108 pipe_command (const char *arg
, int from_tty
)
1112 auto grp
= make_pipe_cmd_options_def_group (&opts
);
1113 gdb::option::process_options
1114 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
1116 const char *delim
= "|";
1117 if (!opts
.delimiter
.empty ())
1118 delim
= opts
.delimiter
.c_str ();
1120 const char *command
= arg
;
1121 if (command
== nullptr)
1122 error (_("Missing COMMAND"));
1124 arg
= strstr (arg
, delim
);
1127 error (_("Missing delimiter before SHELL_COMMAND"));
1129 std::string
gdb_cmd (command
, arg
- command
);
1131 arg
+= strlen (delim
); /* Skip the delimiter. */
1133 if (gdb_cmd
.empty ())
1134 gdb_cmd
= repeat_previous ();
1136 const char *shell_command
= skip_spaces (arg
);
1137 if (*shell_command
== '\0')
1138 error (_("Missing SHELL_COMMAND"));
1140 FILE *to_shell_command
= popen (shell_command
, "w");
1142 if (to_shell_command
== nullptr)
1143 error (_("Error launching \"%s\""), shell_command
);
1147 stdio_file
pipe_file (to_shell_command
);
1149 execute_command_to_ui_file (&pipe_file
, gdb_cmd
.c_str (), from_tty
);
1153 pclose (to_shell_command
);
1157 int exit_status
= pclose (to_shell_command
);
1159 if (exit_status
< 0)
1160 error (_("shell command \"%s\" failed: %s"), shell_command
,
1161 safe_strerror (errno
));
1162 exit_status_set_internal_vars (exit_status
);
1165 /* Completer for the pipe command. */
1168 pipe_command_completer (struct cmd_list_element
*ignore
,
1169 completion_tracker
&tracker
,
1170 const char *text
, const char *word_ignored
)
1174 const char *org_text
= text
;
1175 auto grp
= make_pipe_cmd_options_def_group (&opts
);
1176 if (gdb::option::complete_options
1177 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
))
1180 const char *delimiter
= "|";
1181 if (!opts
.delimiter
.empty ())
1182 delimiter
= opts
.delimiter
.c_str ();
1184 /* Check if we're past option values already. */
1185 if (text
> org_text
&& !isspace (text
[-1]))
1188 const char *delim
= strstr (text
, delimiter
);
1190 /* If we're still not past the delimiter, complete the gdb
1192 if (delim
== nullptr || delim
== text
)
1194 complete_nested_command_line (tracker
, text
);
1198 /* We're past the delimiter. What follows is a shell command, which
1199 we don't know how to complete. */
1203 list_command (const char *arg
, int from_tty
)
1210 int linenum_beg
= 0;
1213 /* Pull in the current default source line if necessary. */
1214 if (arg
== NULL
|| ((arg
[0] == '+' || arg
[0] == '-') && arg
[1] == '\0'))
1216 set_default_source_symtab_and_line ();
1217 symtab_and_line cursal
= get_current_source_symtab_and_line ();
1219 /* If this is the first "list" since we've set the current
1220 source line, center the listing around that line. */
1221 if (get_first_line_listed () == 0)
1225 first
= std::max (cursal
.line
- get_lines_to_list () / 2, 1);
1227 /* A small special case --- if listing backwards, and we
1228 should list only one line, list the preceding line,
1229 instead of the exact line we've just shown after e.g.,
1230 stopping for a breakpoint. */
1231 if (arg
!= NULL
&& arg
[0] == '-'
1232 && get_lines_to_list () == 1 && first
> 1)
1235 print_source_lines (cursal
.symtab
, source_lines_range (first
), 0);
1238 /* "l" or "l +" lists next ten lines. */
1239 else if (arg
== NULL
|| arg
[0] == '+')
1240 print_source_lines (cursal
.symtab
,
1241 source_lines_range (cursal
.line
), 0);
1243 /* "l -" lists previous ten lines, the ones before the ten just
1245 else if (arg
[0] == '-')
1247 if (get_first_line_listed () == 1)
1248 error (_("Already at the start of %s."),
1249 symtab_to_filename_for_display (cursal
.symtab
));
1250 source_lines_range
range (get_first_line_listed (),
1251 source_lines_range::BACKWARD
);
1252 print_source_lines (cursal
.symtab
, range
, 0);
1258 /* Now if there is only one argument, decode it in SAL
1260 If there are two arguments, decode them in SAL and SAL_END
1261 and clear NO_END; however, if one of the arguments is blank,
1262 set DUMMY_BEG or DUMMY_END to record that fact. */
1264 if (!have_full_symbols () && !have_partial_symbols ())
1265 error (_("No symbol table is loaded. Use the \"file\" command."));
1267 std::vector
<symtab_and_line
> sals
;
1268 symtab_and_line sal
, sal_end
;
1275 location_spec_up locspec
1276 = string_to_location_spec (&arg1
, current_language
);
1278 /* We know that the ARG string is not empty, yet the attempt to
1279 parse a location spec from the string consumed no characters.
1280 This most likely means that the first thing in ARG looks like
1281 a location spec condition, and so the string_to_location_spec
1282 call stopped parsing. */
1284 error (_("Junk at end of line specification."));
1286 sals
= decode_line_1 (locspec
.get (), DECODE_LINE_LIST_MODE
,
1298 /* Record whether the BEG arg is all digits. */
1300 for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
1301 linenum_beg
= (p
== arg1
);
1303 /* Save the range of the first argument, in case we need to let the
1304 user know it was ambiguous. */
1305 const char *beg
= arg
;
1306 size_t beg_len
= arg1
- beg
;
1308 while (*arg1
== ' ' || *arg1
== '\t')
1313 if (sals
.size () > 1)
1315 ambiguous_line_spec (sals
,
1316 _("Specified first line '%.*s' is ambiguous:\n"),
1317 (int) beg_len
, beg
);
1321 while (*arg1
== ' ' || *arg1
== '\t')
1327 /* Save the last argument, in case we need to let the user
1328 know it was ambiguous. */
1329 const char *end_arg
= arg1
;
1331 location_spec_up locspec
1332 = string_to_location_spec (&arg1
, current_language
);
1335 error (_("Junk at end of line specification."));
1337 std::vector
<symtab_and_line
> sals_end
1339 ? decode_line_1 (locspec
.get (), DECODE_LINE_LIST_MODE
,
1341 : decode_line_1 (locspec
.get (), DECODE_LINE_LIST_MODE
,
1342 NULL
, sal
.symtab
, sal
.line
));
1344 filter_sals (sals_end
);
1345 if (sals_end
.empty ())
1347 if (sals_end
.size () > 1)
1349 ambiguous_line_spec (sals_end
,
1350 _("Specified last line '%s' is ambiguous:\n"),
1354 sal_end
= sals_end
[0];
1359 error (_("Junk at end of line specification."));
1361 if (!no_end
&& !dummy_beg
&& !dummy_end
1362 && sal
.symtab
!= sal_end
.symtab
)
1363 error (_("Specified first and last lines are in different files."));
1364 if (dummy_beg
&& dummy_end
)
1365 error (_("Two empty args do not say what lines to list."));
1367 /* If line was specified by address,
1368 first print exactly which line, and which file.
1370 In this case, sal.symtab == 0 means address is outside of all
1371 known source files, not that user failed to give a filename. */
1374 struct gdbarch
*gdbarch
;
1376 if (sal
.symtab
== 0)
1377 error (_("No source file for address %s."),
1378 paddress (get_current_arch (), sal
.pc
));
1380 gdbarch
= sal
.symtab
->compunit ()->objfile ()->arch ();
1381 sym
= find_pc_function (sal
.pc
);
1383 gdb_printf ("%s is in %s (%s:%d).\n",
1384 paddress (gdbarch
, sal
.pc
),
1386 symtab_to_filename_for_display (sal
.symtab
), sal
.line
);
1388 gdb_printf ("%s is at %s:%d.\n",
1389 paddress (gdbarch
, sal
.pc
),
1390 symtab_to_filename_for_display (sal
.symtab
), sal
.line
);
1393 /* If line was not specified by just a line number, and it does not
1394 imply a symtab, it must be an undebuggable symbol which means no
1397 if (!linenum_beg
&& sal
.symtab
== 0)
1398 error (_("No line number known for %s."), arg
);
1400 /* If this command is repeated with RET,
1401 turn it into the no-arg variant. */
1404 set_repeat_arguments ("");
1406 if (dummy_beg
&& sal_end
.symtab
== 0)
1407 error (_("No default source file yet. Do \"help list\"."));
1410 source_lines_range
range (sal_end
.line
+ 1,
1411 source_lines_range::BACKWARD
);
1412 print_source_lines (sal_end
.symtab
, range
, 0);
1414 else if (sal
.symtab
== 0)
1415 error (_("No default source file yet. Do \"help list\"."));
1418 for (int i
= 0; i
< sals
.size (); i
++)
1421 int first_line
= sal
.line
- get_lines_to_list () / 2;
1424 if (sals
.size () > 1)
1425 print_sal_location (sal
);
1426 print_source_lines (sal
.symtab
, source_lines_range (first_line
), 0);
1430 print_source_lines (sal
.symtab
, source_lines_range (sal
.line
), 0);
1432 print_source_lines (sal
.symtab
,
1433 source_lines_range (sal
.line
, (sal_end
.line
+ 1)),
1437 /* Subroutine of disassemble_command to simplify it.
1438 Perform the disassembly.
1439 NAME is the name of the function if known, or NULL.
1440 [LOW,HIGH) are the range of addresses to disassemble.
1441 BLOCK is the block to disassemble; it needs to be provided
1442 when non-contiguous blocks are disassembled; otherwise
1444 MIXED is non-zero to print source with the assembler. */
1447 print_disassembly (struct gdbarch
*gdbarch
, const char *name
,
1448 CORE_ADDR low
, CORE_ADDR high
,
1449 const struct block
*block
,
1450 gdb_disassembly_flags flags
)
1453 if (tui_is_window_visible (DISASSEM_WIN
))
1454 tui_show_assembly (gdbarch
, low
);
1458 gdb_printf (_("Dump of assembler code "));
1460 gdb_printf (_("for function %ps:\n"),
1461 styled_string (function_name_style
.style (), name
));
1462 if (block
== nullptr || block
->is_contiguous ())
1465 gdb_printf (_("from %ps to %ps:\n"),
1466 styled_string (address_style
.style (),
1467 paddress (gdbarch
, low
)),
1468 styled_string (address_style
.style (),
1469 paddress (gdbarch
, high
)));
1471 /* Dump the specified range. */
1472 gdb_disassembly (gdbarch
, current_uiout
, flags
, -1, low
, high
);
1476 for (const blockrange
&range
: block
->ranges ())
1478 CORE_ADDR range_low
= range
.start ();
1479 CORE_ADDR range_high
= range
.end ();
1481 gdb_printf (_("Address range %ps to %ps:\n"),
1482 styled_string (address_style
.style (),
1483 paddress (gdbarch
, range_low
)),
1484 styled_string (address_style
.style (),
1485 paddress (gdbarch
, range_high
)));
1486 gdb_disassembly (gdbarch
, current_uiout
, flags
, -1,
1487 range_low
, range_high
);
1490 gdb_printf (_("End of assembler dump.\n"));
1494 /* Subroutine of disassemble_command to simplify it.
1495 Print a disassembly of the current function according to FLAGS. */
1498 disassemble_current_function (gdb_disassembly_flags flags
)
1500 frame_info_ptr frame
;
1501 struct gdbarch
*gdbarch
;
1502 CORE_ADDR low
, high
, pc
;
1504 const struct block
*block
;
1506 frame
= get_selected_frame (_("No frame selected."));
1507 gdbarch
= get_frame_arch (frame
);
1508 pc
= get_frame_address_in_block (frame
);
1509 if (find_pc_partial_function (pc
, &name
, &low
, &high
, &block
) == 0)
1510 error (_("No function contains program counter for selected frame."));
1512 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1515 /* FIXME: cagney/2004-02-07: This should be an observer. */
1516 low
= tui_get_low_disassembly_address (gdbarch
, low
, pc
);
1518 low
+= gdbarch_deprecated_function_start_offset (gdbarch
);
1520 print_disassembly (gdbarch
, name
, low
, high
, block
, flags
);
1523 /* Dump a specified section of assembly code.
1527 - dump the assembly code for the function of the current pc
1528 disassemble [/mrs] addr
1529 - dump the assembly code for the function at ADDR
1530 disassemble [/mrs] low,high
1531 disassemble [/mrs] low,+length
1532 - dump the assembly code in the range [LOW,HIGH), or [LOW,LOW+length)
1534 A /m modifier will include source code with the assembly in a
1535 "source centric" view. This view lists only the file of the first insn,
1536 even if other source files are involved (e.g., inlined functions), and
1537 the output is in source order, even with optimized code. This view is
1538 considered deprecated as it hasn't been useful in practice.
1540 A /r modifier will include raw instructions in hex with the assembly.
1542 A /b modifier is similar to /r except the instruction bytes are printed
1543 as separate bytes with no grouping, or endian switching.
1545 A /s modifier will include source code with the assembly, like /m, with
1546 two important differences:
1547 1) The output is still in pc address order.
1548 2) File names and contents for all relevant source files are displayed. */
1551 disassemble_command (const char *arg
, int from_tty
)
1553 struct gdbarch
*gdbarch
= get_current_arch ();
1554 CORE_ADDR low
, high
;
1555 const general_symbol_info
*symbol
= nullptr;
1558 gdb_disassembly_flags flags
;
1560 const struct block
*block
= nullptr;
1571 error (_("Missing modifier."));
1573 while (*p
&& ! isspace (*p
))
1578 flags
|= DISASSEMBLY_SOURCE_DEPRECATED
;
1581 flags
|= DISASSEMBLY_RAW_INSN
;
1584 flags
|= DISASSEMBLY_RAW_BYTES
;
1587 flags
|= DISASSEMBLY_SOURCE
;
1590 error (_("Invalid disassembly modifier."));
1594 p
= skip_spaces (p
);
1597 if ((flags
& (DISASSEMBLY_SOURCE_DEPRECATED
| DISASSEMBLY_SOURCE
))
1598 == (DISASSEMBLY_SOURCE_DEPRECATED
| DISASSEMBLY_SOURCE
))
1599 error (_("Cannot specify both /m and /s."));
1603 flags
|= DISASSEMBLY_OMIT_FNAME
;
1604 disassemble_current_function (flags
);
1608 pc
= value_as_address (parse_to_comma_and_eval (&p
));
1614 if (!find_pc_partial_function_sym (pc
, &symbol
, &low
, &high
, &block
))
1615 error (_("No function contains specified address."));
1618 name
= symbol
->print_name ();
1620 name
= symbol
->linkage_name ();
1623 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1626 /* FIXME: cagney/2004-02-07: This should be an observer. */
1627 low
= tui_get_low_disassembly_address (gdbarch
, low
, pc
);
1629 low
+= gdbarch_deprecated_function_start_offset (gdbarch
);
1630 flags
|= DISASSEMBLY_OMIT_FNAME
;
1634 /* Two arguments. */
1637 p
= skip_spaces (p
);
1643 high
= parse_and_eval_address (p
);
1648 print_disassembly (gdbarch
, name
, low
, high
, block
, flags
);
1652 make_command (const char *arg
, int from_tty
)
1655 shell_escape ("make", from_tty
);
1658 std::string cmd
= std::string ("make ") + arg
;
1660 shell_escape (cmd
.c_str (), from_tty
);
1665 show_user (const char *args
, int from_tty
)
1667 struct cmd_list_element
*c
;
1671 const char *comname
= args
;
1673 c
= lookup_cmd (&comname
, cmdlist
, "", NULL
, 0, 1);
1674 if (!cli_user_command_p (c
))
1675 error (_("Not a user command."));
1676 show_user_1 (c
, "", args
, gdb_stdout
);
1680 for (c
= cmdlist
; c
; c
= c
->next
)
1682 if (cli_user_command_p (c
) || c
->is_prefix ())
1683 show_user_1 (c
, "", c
->name
, gdb_stdout
);
1688 /* Return true if COMMAND or any of its sub-commands is a user defined command.
1689 This is a helper function for show_user_completer. */
1692 has_user_subcmd (struct cmd_list_element
*command
)
1694 if (cli_user_command_p (command
))
1697 /* Alias command can yield false positive. Ignore them as the targeted
1698 command should be reachable anyway. */
1699 if (command
->is_alias ())
1702 if (command
->is_prefix ())
1703 for (struct cmd_list_element
*subcommand
= *command
->subcommands
;
1704 subcommand
!= nullptr;
1705 subcommand
= subcommand
->next
)
1706 if (has_user_subcmd (subcommand
))
1712 /* Implement completer for the 'show user' command. */
1715 show_user_completer (cmd_list_element
*,
1716 completion_tracker
&tracker
, const char *text
,
1719 struct cmd_list_element
*cmd_group
= cmdlist
;
1721 /* TEXT can contain a chain of commands and subcommands. Follow the
1722 commands chain until we reach the point where the user wants a
1726 const char *curr_cmd
= text
;
1727 const char *after
= skip_to_space (text
);
1728 const size_t curr_cmd_len
= after
- text
;
1729 text
= skip_spaces (after
);
1731 for (struct cmd_list_element
*c
= cmd_group
; c
!= nullptr; c
= c
->next
)
1733 if (strlen (c
->name
) == curr_cmd_len
1734 && strncmp (c
->name
, curr_cmd
, curr_cmd_len
) == 0)
1736 if (c
->subcommands
== nullptr)
1737 /* We arrived after a command with no child, so nothing more
1741 cmd_group
= *c
->subcommands
;
1747 const int wordlen
= strlen (word
);
1748 for (struct cmd_list_element
*c
= cmd_group
; c
!= nullptr; c
= c
->next
)
1749 if (has_user_subcmd (c
))
1751 if (strncmp (c
->name
, word
, wordlen
) == 0)
1752 tracker
.add_completion
1753 (gdb::unique_xmalloc_ptr
<char> (xstrdup (c
->name
)));
1757 /* Search through names of commands and documentations for a certain
1758 regular expression. */
1761 apropos_command (const char *arg
, int from_tty
)
1763 bool verbose
= arg
&& check_for_argument (&arg
, "-v", 2);
1765 if (arg
== NULL
|| *arg
== '\0')
1766 error (_("REGEXP string is empty"));
1768 compiled_regex
pattern (arg
, REG_ICASE
,
1769 _("Error in regular expression"));
1771 apropos_cmd (gdb_stdout
, cmdlist
, verbose
, pattern
);
1774 /* The options for the "alias" command. */
1779 bool abbrev_flag
= false;
1782 static const gdb::option::option_def alias_option_defs
[] = {
1784 gdb::option::flag_option_def
<alias_opts
> {
1786 [] (alias_opts
*opts
) { return &opts
->abbrev_flag
; },
1787 N_("Specify that ALIAS is an abbreviation of COMMAND.\n\
1788 Abbreviations are not used in command completion."),
1793 /* Create an option_def_group for the "alias" options, with
1794 A_OPTS as context. */
1796 static gdb::option::option_def_group
1797 make_alias_options_def_group (alias_opts
*a_opts
)
1799 return {{alias_option_defs
}, a_opts
};
1802 /* Completer for the "alias_command". */
1805 alias_command_completer (struct cmd_list_element
*ignore
,
1806 completion_tracker
&tracker
,
1807 const char *text
, const char *word
)
1809 const auto grp
= make_alias_options_def_group (nullptr);
1811 tracker
.set_use_custom_word_point (true);
1813 if (gdb::option::complete_options
1814 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1817 const char *delim
= strchr (text
, '=');
1819 /* If we're past the "=" delimiter, complete the
1820 "alias ALIAS = COMMAND [DEFAULT-ARGS...]" as if the user is
1821 typing COMMAND DEFAULT-ARGS... */
1824 && isspace (delim
[-1])
1825 && (isspace (delim
[1]) || delim
[1] == '\0'))
1827 std::string new_text
= std::string (delim
+ 1);
1829 tracker
.advance_custom_word_point_by (delim
+ 1 - text
);
1830 complete_nested_command_line (tracker
, new_text
.c_str ());
1834 /* We're not yet past the "=" delimiter. Complete a command, as
1835 the user might type an alias following a prefix command. */
1836 complete_nested_command_line (tracker
, text
);
1839 /* Subroutine of alias_command to simplify it.
1840 Return the first N elements of ARGV flattened back to a string
1841 with a space separating each element.
1842 ARGV may not be NULL.
1843 This does not take care of quoting elements in case they contain spaces
1847 argv_to_string (char **argv
, int n
)
1852 gdb_assert (argv
!= NULL
);
1853 gdb_assert (n
>= 0 && n
<= countargv (argv
));
1855 for (i
= 0; i
< n
; ++i
)
1865 /* Subroutine of alias_command to simplify it.
1866 Verifies that COMMAND can have an alias:
1868 COMMAND must not have default args.
1869 This last condition is to avoid the following:
1870 alias aaa = backtrace -full
1871 alias bbb = aaa -past-main
1872 as (at least currently), alias default args are not cumulative
1873 and the user would expect bbb to execute 'backtrace -full -past-main'
1874 while it will execute 'backtrace -past-main'. */
1876 static cmd_list_element
*
1877 validate_aliased_command (const char *command
)
1879 std::string default_args
;
1881 = lookup_cmd_1 (& command
, cmdlist
, NULL
, &default_args
, 1);
1883 if (c
== NULL
|| c
== (struct cmd_list_element
*) -1)
1884 error (_("Invalid command to alias to: %s"), command
);
1886 if (!default_args
.empty ())
1887 error (_("Cannot define an alias of an alias that has default args"));
1892 /* Called when "alias" was incorrectly used. */
1895 alias_usage_error (void)
1897 error (_("Usage: alias [-a] [--] ALIAS = COMMAND [DEFAULT-ARGS...]"));
1900 /* Make an alias of an existing command. */
1903 alias_command (const char *args
, int from_tty
)
1907 auto grp
= make_alias_options_def_group (&a_opts
);
1908 gdb::option::process_options
1909 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1911 int i
, alias_argc
, command_argc
;
1913 const char *alias
, *command
;
1915 if (args
== NULL
|| strchr (args
, '=') == NULL
)
1916 alias_usage_error ();
1918 equals
= strchr (args
, '=');
1919 std::string
args2 (args
, equals
- args
);
1921 gdb_argv
built_alias_argv (args2
.c_str ());
1923 const char *default_args
= equals
+ 1;
1924 struct cmd_list_element
*c_command_prefix
;
1926 lookup_cmd_for_default_args (&default_args
, &c_command_prefix
);
1927 std::string
command_argv_str (equals
+ 1,
1928 default_args
== nullptr
1929 ? strlen (equals
+ 1)
1930 : default_args
- equals
- 1);
1931 gdb_argv
command_argv (command_argv_str
.c_str ());
1933 char **alias_argv
= built_alias_argv
.get ();
1935 if (alias_argv
[0] == NULL
|| command_argv
[0] == NULL
1936 || *alias_argv
[0] == '\0' || *command_argv
[0] == '\0')
1937 alias_usage_error ();
1939 for (i
= 0; alias_argv
[i
] != NULL
; ++i
)
1941 if (! valid_user_defined_cmd_name_p (alias_argv
[i
]))
1944 error (_("Invalid command name: %s"), alias_argv
[i
]);
1946 error (_("Invalid command element name: %s"), alias_argv
[i
]);
1950 alias_argc
= countargv (alias_argv
);
1951 command_argc
= command_argv
.count ();
1953 /* COMMAND must exist, and cannot have default args.
1954 Reconstruct the command to remove any extraneous spaces,
1955 for better error messages. */
1956 std::string
command_string (argv_to_string (command_argv
.get (),
1958 command
= command_string
.c_str ();
1959 cmd_list_element
*target_cmd
= validate_aliased_command (command
);
1961 /* ALIAS must not exist. */
1962 std::string
alias_string (argv_to_string (alias_argv
, alias_argc
));
1963 alias
= alias_string
.c_str ();
1965 cmd_list_element
*alias_cmd
, *prefix_cmd
, *cmd
;
1967 if (lookup_cmd_composition (alias
, &alias_cmd
, &prefix_cmd
, &cmd
))
1969 const char *alias_name
= alias_argv
[alias_argc
-1];
1971 /* If we found an existing ALIAS_CMD, check that the prefix differ or
1974 if (alias_cmd
!= nullptr
1975 && alias_cmd
->prefix
== prefix_cmd
1976 && strcmp (alias_name
, alias_cmd
->name
) == 0)
1977 error (_("Alias already exists: %s"), alias
);
1979 /* Check ALIAS differs from the found CMD. */
1981 if (cmd
->prefix
== prefix_cmd
1982 && strcmp (alias_name
, cmd
->name
) == 0)
1983 error (_("Alias %s is the name of an existing command"), alias
);
1988 struct cmd_list_element
*alias_cmd
;
1990 /* If ALIAS is one word, it is an alias for the entire COMMAND.
1991 Example: alias spe = set print elements
1993 Otherwise ALIAS and COMMAND must have the same number of words,
1994 and every word except the last must identify the same prefix command;
1995 and the last word of ALIAS is made an alias of the last word of COMMAND.
1996 Example: alias set print elms = set pr elem
1997 Note that unambiguous abbreviations are allowed. */
1999 if (alias_argc
== 1)
2001 /* add_cmd requires *we* allocate space for name, hence the xstrdup. */
2002 alias_cmd
= add_com_alias (xstrdup (alias_argv
[0]), target_cmd
,
2003 class_alias
, a_opts
.abbrev_flag
);
2007 const char *alias_prefix
, *command_prefix
;
2008 struct cmd_list_element
*c_alias
, *c_command
;
2010 if (alias_argc
!= command_argc
)
2011 error (_("Mismatched command length between ALIAS and COMMAND."));
2013 /* Create copies of ALIAS and COMMAND without the last word,
2014 and use that to verify the leading elements give the same
2016 std::string
alias_prefix_string (argv_to_string (alias_argv
,
2018 std::string
command_prefix_string (argv_to_string (command_argv
.get (),
2020 alias_prefix
= alias_prefix_string
.c_str ();
2021 command_prefix
= command_prefix_string
.c_str ();
2023 c_command
= lookup_cmd_1 (& command_prefix
, cmdlist
, NULL
, NULL
, 1);
2024 /* We've already tried to look up COMMAND. */
2025 gdb_assert (c_command
!= NULL
2026 && c_command
!= (struct cmd_list_element
*) -1);
2027 gdb_assert (c_command
->is_prefix ());
2028 c_alias
= lookup_cmd_1 (& alias_prefix
, cmdlist
, NULL
, NULL
, 1);
2029 if (c_alias
!= c_command
)
2030 error (_("ALIAS and COMMAND prefixes do not match."));
2032 /* add_cmd requires *we* allocate space for name, hence the xstrdup. */
2033 alias_cmd
= add_alias_cmd (xstrdup (alias_argv
[alias_argc
- 1]),
2034 target_cmd
, class_alias
, a_opts
.abbrev_flag
,
2035 c_command
->subcommands
);
2038 gdb_assert (alias_cmd
!= nullptr);
2039 gdb_assert (alias_cmd
->default_args
.empty ());
2040 if (default_args
!= nullptr)
2042 default_args
= skip_spaces (default_args
);
2044 alias_cmd
->default_args
= default_args
;
2048 /* Print the file / line number / symbol name of the location
2049 specified by SAL. */
2052 print_sal_location (const symtab_and_line
&sal
)
2054 scoped_restore_current_program_space restore_pspace
;
2055 set_current_program_space (sal
.pspace
);
2057 const char *sym_name
= NULL
;
2058 if (sal
.symbol
!= NULL
)
2059 sym_name
= sal
.symbol
->print_name ();
2060 gdb_printf (_("file: \"%s\", line number: %d, symbol: \"%s\"\n"),
2061 symtab_to_filename_for_display (sal
.symtab
),
2062 sal
.line
, sym_name
!= NULL
? sym_name
: "???");
2065 /* Print a list of files and line numbers which a user may choose from
2066 in order to list a function which was specified ambiguously (as
2067 with `list classname::overloadedfuncname', for example). The SALS
2068 array provides the filenames and line numbers. FORMAT is a
2069 printf-style format string used to tell the user what was
2073 ambiguous_line_spec (gdb::array_view
<const symtab_and_line
> sals
,
2074 const char *format
, ...)
2077 va_start (ap
, format
);
2078 gdb_vprintf (format
, ap
);
2081 for (const auto &sal
: sals
)
2082 print_sal_location (sal
);
2085 /* Comparison function for filter_sals. Returns a qsort-style
2089 cmp_symtabs (const symtab_and_line
&sala
, const symtab_and_line
&salb
)
2091 const char *dira
= sala
.symtab
->compunit ()->dirname ();
2092 const char *dirb
= salb
.symtab
->compunit ()->dirname ();
2100 else if (dirb
== NULL
)
2107 r
= filename_cmp (dira
, dirb
);
2112 r
= filename_cmp (sala
.symtab
->filename
, salb
.symtab
->filename
);
2116 if (sala
.line
< salb
.line
)
2118 return sala
.line
== salb
.line
? 0 : 1;
2121 /* Remove any SALs that do not match the current program space, or
2122 which appear to be "file:line" duplicates. */
2125 filter_sals (std::vector
<symtab_and_line
> &sals
)
2127 /* Remove SALs that do not match. */
2128 auto from
= std::remove_if (sals
.begin (), sals
.end (),
2129 [&] (const symtab_and_line
&sal
)
2130 { return (sal
.pspace
!= current_program_space
|| sal
.symtab
== NULL
); });
2133 std::sort (sals
.begin (), from
,
2134 [] (const symtab_and_line
&sala
, const symtab_and_line
&salb
)
2135 { return cmp_symtabs (sala
, salb
) < 0; });
2137 from
= std::unique (sals
.begin (), from
,
2138 [&] (const symtab_and_line
&sala
,
2139 const symtab_and_line
&salb
)
2140 { return cmp_symtabs (sala
, salb
) == 0; });
2142 sals
.erase (from
, sals
.end ());
2146 show_info_verbose (struct ui_file
*file
, int from_tty
,
2147 struct cmd_list_element
*c
,
2152 _("Verbose printing of informational messages is %s.\n"),
2155 gdb_printf (file
, _("Verbosity is %s.\n"), value
);
2159 show_history_expansion_p (struct ui_file
*file
, int from_tty
,
2160 struct cmd_list_element
*c
, const char *value
)
2162 gdb_printf (file
, _("History expansion on command input is %s.\n"),
2167 show_max_user_call_depth (struct ui_file
*file
, int from_tty
,
2168 struct cmd_list_element
*c
, const char *value
)
2171 _("The max call depth for user-defined commands is %s.\n"),
2175 /* Implement 'show suppress-cli-notifications'. */
2178 show_suppress_cli_notifications (ui_file
*file
, int from_tty
,
2179 cmd_list_element
*c
, const char *value
)
2181 gdb_printf (file
, _("Suppression of printing CLI notifications "
2182 "is %s.\n"), value
);
2185 /* Implement 'set suppress-cli-notifications'. */
2188 set_suppress_cli_notifications (const char *args
, int from_tty
,
2189 cmd_list_element
*c
)
2191 cli_suppress_notification
.user_selected_context
2192 = user_wants_cli_suppress_notification
;
2193 cli_suppress_notification
.normal_stop
2194 = user_wants_cli_suppress_notification
;
2197 /* Returns the cmd_list_element in SHOWLIST corresponding to the first
2198 argument of ARGV, which must contain one single value.
2199 Throws an error if no value provided, or value not correct.
2200 FNNAME is used in the error message. */
2202 static cmd_list_element
*
2203 setting_cmd (const char *fnname
, struct cmd_list_element
*showlist
,
2204 int argc
, struct value
**argv
)
2207 error (_("You must provide an argument to %s"), fnname
);
2209 error (_("You can only provide one argument to %s"), fnname
);
2211 struct type
*type0
= check_typedef (argv
[0]->type ());
2213 if (type0
->code () != TYPE_CODE_ARRAY
2214 && type0
->code () != TYPE_CODE_STRING
)
2215 error (_("First argument of %s must be a string."), fnname
);
2217 const char *a0
= (const char *) argv
[0]->contents ().data ();
2218 cmd_list_element
*cmd
= lookup_cmd (&a0
, showlist
, "", NULL
, -1, 0);
2220 if (cmd
== nullptr || cmd
->type
!= show_cmd
)
2221 error (_("First argument of %s must be a "
2222 "valid setting of the 'show' command."), fnname
);
2227 /* Builds a value from the show CMD. */
2229 static struct value
*
2230 value_from_setting (const setting
&var
, struct gdbarch
*gdbarch
)
2232 switch (var
.type ())
2239 = (var
.type () == var_uinteger
2240 ? static_cast<LONGEST
> (var
.get
<unsigned int> ())
2241 : static_cast<LONGEST
> (var
.get
<int> ()));
2243 if (var
.extra_literals () != nullptr)
2244 for (const literal_def
*l
= var
.extra_literals ();
2245 l
->literal
!= nullptr;
2247 if (value
== l
->use
)
2249 if (l
->val
.has_value ())
2252 return value::allocate (builtin_type (gdbarch
)->builtin_void
);
2256 if (var
.type () == var_uinteger
)
2258 value_from_ulongest (builtin_type (gdbarch
)->builtin_unsigned_int
,
2259 static_cast<unsigned int> (value
));
2262 value_from_longest (builtin_type (gdbarch
)->builtin_int
,
2263 static_cast<int> (value
));
2266 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
2267 var
.get
<bool> () ? 1 : 0);
2268 case var_auto_boolean
:
2272 switch (var
.get
<enum auto_boolean
> ())
2274 case AUTO_BOOLEAN_TRUE
:
2277 case AUTO_BOOLEAN_FALSE
:
2280 case AUTO_BOOLEAN_AUTO
:
2284 gdb_assert_not_reached ("invalid var_auto_boolean");
2286 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
2290 case var_string_noescape
:
2291 case var_optional_filename
:
2297 if (var
.type () == var_enum
)
2299 value
= var
.get
<const char *> ();
2300 len
= strlen (value
);
2304 const std::string
&st
= var
.get
<std::string
> ();
2305 value
= st
.c_str ();
2310 return value_cstring (value
, len
,
2311 builtin_type (gdbarch
)->builtin_char
);
2313 return value_cstring ("", 1,
2314 builtin_type (gdbarch
)->builtin_char
);
2317 gdb_assert_not_reached ("bad var_type");
2321 /* Implementation of the convenience function $_gdb_setting. */
2323 static struct value
*
2324 gdb_setting_internal_fn (struct gdbarch
*gdbarch
,
2325 const struct language_defn
*language
,
2326 void *cookie
, int argc
, struct value
**argv
)
2328 cmd_list_element
*show_cmd
2329 = setting_cmd ("$_gdb_setting", showlist
, argc
, argv
);
2331 gdb_assert (show_cmd
->var
.has_value ());
2333 return value_from_setting (*show_cmd
->var
, gdbarch
);
2336 /* Implementation of the convenience function $_gdb_maint_setting. */
2338 static struct value
*
2339 gdb_maint_setting_internal_fn (struct gdbarch
*gdbarch
,
2340 const struct language_defn
*language
,
2341 void *cookie
, int argc
, struct value
**argv
)
2343 cmd_list_element
*show_cmd
2344 = setting_cmd ("$_gdb_maint_setting", maintenance_show_cmdlist
, argc
, argv
);
2346 gdb_assert (show_cmd
->var
.has_value ());
2348 return value_from_setting (*show_cmd
->var
, gdbarch
);
2351 /* Builds a string value from the show CMD. */
2353 static struct value
*
2354 str_value_from_setting (const setting
&var
, struct gdbarch
*gdbarch
)
2356 switch (var
.type ())
2362 case var_auto_boolean
:
2364 std::string cmd_val
= get_setshow_command_value_string (var
);
2366 return value_cstring (cmd_val
.c_str (), cmd_val
.size (),
2367 builtin_type (gdbarch
)->builtin_char
);
2371 case var_string_noescape
:
2372 case var_optional_filename
:
2375 /* For these cases, we do not use get_setshow_command_value_string,
2376 as this function handle some characters specially, e.g. by
2377 escaping quotevar. So, we directly use the var string value,
2378 similarly to the value_from_setting code for these casevar. */
2382 if (var
.type () == var_enum
)
2384 value
= var
.get
<const char *> ();
2385 len
= strlen (value
);
2389 const std::string
&st
= var
.get
<std::string
> ();
2390 value
= st
.c_str ();
2395 return value_cstring (value
, len
,
2396 builtin_type (gdbarch
)->builtin_char
);
2398 return value_cstring ("", 1,
2399 builtin_type (gdbarch
)->builtin_char
);
2402 gdb_assert_not_reached ("bad var_type");
2406 /* Implementation of the convenience function $_gdb_setting_str. */
2408 static struct value
*
2409 gdb_setting_str_internal_fn (struct gdbarch
*gdbarch
,
2410 const struct language_defn
*language
,
2411 void *cookie
, int argc
, struct value
**argv
)
2413 cmd_list_element
*show_cmd
2414 = setting_cmd ("$_gdb_setting_str", showlist
, argc
, argv
);
2416 gdb_assert (show_cmd
->var
.has_value ());
2418 return str_value_from_setting (*show_cmd
->var
, gdbarch
);
2422 /* Implementation of the convenience function $_gdb_maint_setting_str. */
2424 static struct value
*
2425 gdb_maint_setting_str_internal_fn (struct gdbarch
*gdbarch
,
2426 const struct language_defn
*language
,
2427 void *cookie
, int argc
, struct value
**argv
)
2429 cmd_list_element
*show_cmd
2430 = setting_cmd ("$_gdb_maint_setting_str", maintenance_show_cmdlist
, argc
,
2433 gdb_assert (show_cmd
->var
.has_value ());
2435 return str_value_from_setting (*show_cmd
->var
, gdbarch
);
2438 /* Implementation of the convenience function $_shell. */
2440 static struct value
*
2441 shell_internal_fn (struct gdbarch
*gdbarch
,
2442 const struct language_defn
*language
,
2443 void *cookie
, int argc
, struct value
**argv
)
2446 error (_("You must provide one argument for $_shell."));
2448 value
*val
= argv
[0];
2449 struct type
*type
= check_typedef (val
->type ());
2451 if (!language
->is_string_type_p (type
))
2452 error (_("Argument must be a string."));
2454 value_print_options opts
;
2455 get_no_prettyformat_print_options (&opts
);
2458 value_print (val
, &stream
, &opts
);
2460 /* We should always have two quote chars, which we'll strip. */
2461 gdb_assert (stream
.size () >= 2);
2463 /* Now strip them. We don't need the original string, so it's
2464 cheaper to do it in place, avoiding a string allocation. */
2465 std::string str
= stream
.release ();
2466 str
[str
.size () - 1] = 0;
2467 const char *command
= str
.c_str () + 1;
2469 int exit_status
= run_under_shell (command
, 0);
2471 struct type
*int_type
= builtin_type (gdbarch
)->builtin_int
;
2473 /* Keep the logic here in sync with
2474 exit_status_set_internal_vars. */
2476 if (WIFEXITED (exit_status
))
2477 return value_from_longest (int_type
, WEXITSTATUS (exit_status
));
2479 else if (WIFSIGNALED (exit_status
) && WTERMSIG (exit_status
) == -1)
2481 /* See exit_status_set_internal_vars. */
2482 return value_from_longest (int_type
, exit_status
);
2485 else if (WIFSIGNALED (exit_status
))
2487 /* (0x80 | SIGNO) is what most (all?) POSIX-like shells set as
2488 exit code on fatal signal termination. */
2489 return value_from_longest (int_type
, 0x80 | WTERMSIG (exit_status
));
2492 return value::allocate_optimized_out (int_type
);
2495 void _initialize_cli_cmds ();
2497 _initialize_cli_cmds ()
2499 struct cmd_list_element
*c
;
2501 /* Define the classes of commands.
2502 They will appear in the help list in alphabetical order. */
2504 add_cmd ("internals", class_maintenance
, _("\
2505 Maintenance commands.\n\
2506 Some gdb commands are provided just for use by gdb maintainers.\n\
2507 These commands are subject to frequent change, and may not be as\n\
2508 well documented as user commands."),
2510 add_cmd ("obscure", class_obscure
, _("Obscure features."), &cmdlist
);
2511 add_cmd ("aliases", class_alias
,
2512 _("User-defined aliases of other commands."), &cmdlist
);
2513 add_cmd ("user-defined", class_user
, _("\
2514 User-defined commands.\n\
2515 The commands in this class are those defined by the user.\n\
2516 Use the \"define\" command to define a command."), &cmdlist
);
2517 add_cmd ("support", class_support
, _("Support facilities."), &cmdlist
);
2518 add_cmd ("status", class_info
, _("Status inquiries."), &cmdlist
);
2519 add_cmd ("files", class_files
, _("Specifying and examining files."),
2521 add_cmd ("breakpoints", class_breakpoint
,
2522 _("Making program stop at certain points."), &cmdlist
);
2523 add_cmd ("data", class_vars
, _("Examining data."), &cmdlist
);
2524 add_cmd ("stack", class_stack
, _("\
2525 Examining the stack.\n\
2526 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2527 counting from zero for the innermost (currently executing) frame.\n\n\
2528 At any time gdb identifies one frame as the \"selected\" frame.\n\
2529 Variable lookups are done with respect to the selected frame.\n\
2530 When the program being debugged stops, gdb selects the innermost frame.\n\
2531 The commands below can be used to select other frames by number or address."),
2534 add_cmd ("text-user-interface", class_tui
,
2535 _("TUI is the GDB text based interface.\n\
2536 In TUI mode, GDB can display several text windows showing\n\
2537 the source file, the processor registers, the program disassembly, ..."), &cmdlist
);
2539 add_cmd ("running", class_run
, _("Running the program."), &cmdlist
);
2541 /* Define general commands. */
2543 add_com ("pwd", class_files
, pwd_command
, _("\
2544 Print working directory.\n\
2545 This is used for your program as well."));
2547 c
= add_cmd ("cd", class_files
, cd_command
, _("\
2548 Set working directory to DIR for debugger.\n\
2549 The debugger's current working directory specifies where scripts and other\n\
2550 files that can be loaded by GDB are located.\n\
2551 In order to change the inferior's current working directory, the recommended\n\
2552 way is to use the \"set cwd\" command."), &cmdlist
);
2553 set_cmd_completer (c
, filename_completer
);
2555 add_com ("echo", class_support
, echo_command
, _("\
2556 Print a constant string. Give string as argument.\n\
2557 C escape sequences may be used in the argument.\n\
2558 No newline is added at the end of the argument;\n\
2559 use \"\\n\" if you want a newline to be printed.\n\
2560 Since leading and trailing whitespace are ignored in command arguments,\n\
2561 if you want to print some you must use \"\\\" before leading whitespace\n\
2562 to be printed or after trailing whitespace."));
2564 add_setshow_enum_cmd ("script-extension", class_support
,
2565 script_ext_enums
, &script_ext_mode
, _("\
2566 Set mode for script filename extension recognition."), _("\
2567 Show mode for script filename extension recognition."), _("\
2568 off == no filename extension recognition (all sourced files are GDB scripts)\n\
2569 soft == evaluate script according to filename extension, fallback to GDB script"
2571 strict == evaluate script according to filename extension, error if not supported"
2574 show_script_ext_mode
,
2575 &setlist
, &showlist
);
2577 cmd_list_element
*quit_cmd
2578 = add_com ("quit", class_support
, quit_command
, _("\
2580 Usage: quit [EXPR] or exit [EXPR]\n\
2581 The optional expression EXPR, if present, is evaluated and the result\n\
2582 used as GDB's exit code. The default is zero."));
2583 cmd_list_element
*help_cmd
2584 = add_com ("help", class_support
, help_command
,
2585 _("Print list of commands."));
2586 set_cmd_completer (help_cmd
, command_completer
);
2587 add_com_alias ("q", quit_cmd
, class_support
, 1);
2588 add_com_alias ("exit", quit_cmd
, class_support
, 1);
2589 add_com_alias ("h", help_cmd
, class_support
, 1);
2591 add_setshow_boolean_cmd ("verbose", class_support
, &info_verbose
, _("\
2592 Set verbosity."), _("\
2593 Show verbosity."), NULL
,
2596 &setlist
, &showlist
);
2598 add_setshow_prefix_cmd
2599 ("history", class_support
,
2600 _("Generic command for setting command history parameters."),
2601 _("Generic command for showing command history parameters."),
2602 &sethistlist
, &showhistlist
, &setlist
, &showlist
);
2604 add_setshow_boolean_cmd ("expansion", no_class
, &history_expansion_p
, _("\
2605 Set history expansion on command input."), _("\
2606 Show history expansion on command input."), _("\
2607 Without an argument, history expansion is enabled."),
2609 show_history_expansion_p
,
2610 &sethistlist
, &showhistlist
);
2612 cmd_list_element
*info_cmd
2613 = add_prefix_cmd ("info", class_info
, info_command
, _("\
2614 Generic command for showing things about the program being debugged."),
2615 &infolist
, 0, &cmdlist
);
2616 add_com_alias ("i", info_cmd
, class_info
, 1);
2617 add_com_alias ("inf", info_cmd
, class_info
, 1);
2619 add_com ("complete", class_obscure
, complete_command
,
2620 _("List the completions for the rest of the line as a command."));
2622 c
= add_show_prefix_cmd ("show", class_info
, _("\
2623 Generic command for showing things about the debugger."),
2624 &showlist
, 0, &cmdlist
);
2625 /* Another way to get at the same thing. */
2626 add_alias_cmd ("set", c
, class_info
, 0, &infolist
);
2628 cmd_list_element
*with_cmd
2629 = add_com ("with", class_vars
, with_command
, _("\
2630 Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.\n\
2631 Usage: with SETTING [VALUE] [-- COMMAND]\n\
2632 Usage: w SETTING [VALUE] [-- COMMAND]\n\
2633 With no COMMAND, repeats the last executed command.\n\
2635 SETTING is any setting you can change with the \"set\" subcommands.\n\
2637 with language pascal -- print obj\n\
2638 with print elements unlimited -- print obj\n\
2640 You can change multiple settings using nested with, and use\n\
2641 abbreviations for commands and/or values. E.g.:\n\
2642 w la p -- w p el u -- p obj"));
2643 set_cmd_completer_handle_brkchars (with_cmd
, with_command_completer
);
2644 add_com_alias ("w", with_cmd
, class_vars
, 1);
2646 add_internal_function ("_gdb_setting_str", _("\
2647 $_gdb_setting_str - returns the value of a GDB setting as a string.\n\
2648 Usage: $_gdb_setting_str (setting)\n\
2650 auto-boolean values are \"off\", \"on\", \"auto\".\n\
2651 boolean values are \"off\", \"on\".\n\
2652 Some integer settings accept an unlimited value, returned\n\
2653 as \"unlimited\"."),
2654 gdb_setting_str_internal_fn
, NULL
);
2656 add_internal_function ("_gdb_setting", _("\
2657 $_gdb_setting - returns the value of a GDB setting.\n\
2658 Usage: $_gdb_setting (setting)\n\
2659 auto-boolean values are \"off\", \"on\", \"auto\".\n\
2660 boolean values are \"off\", \"on\".\n\
2661 Some integer settings accept an unlimited value, returned\n\
2662 as 0 or -1 depending on the setting."),
2663 gdb_setting_internal_fn
, NULL
);
2665 add_internal_function ("_gdb_maint_setting_str", _("\
2666 $_gdb_maint_setting_str - returns the value of a GDB maintenance setting as a string.\n\
2667 Usage: $_gdb_maint_setting_str (setting)\n\
2669 auto-boolean values are \"off\", \"on\", \"auto\".\n\
2670 boolean values are \"off\", \"on\".\n\
2671 Some integer settings accept an unlimited value, returned\n\
2672 as \"unlimited\"."),
2673 gdb_maint_setting_str_internal_fn
, NULL
);
2675 add_internal_function ("_gdb_maint_setting", _("\
2676 $_gdb_maint_setting - returns the value of a GDB maintenance setting.\n\
2677 Usage: $_gdb_maint_setting (setting)\n\
2678 auto-boolean values are \"off\", \"on\", \"auto\".\n\
2679 boolean values are \"off\", \"on\".\n\
2680 Some integer settings accept an unlimited value, returned\n\
2681 as 0 or -1 depending on the setting."),
2682 gdb_maint_setting_internal_fn
, NULL
);
2684 add_internal_function ("_shell", _("\
2685 $_shell - execute a shell command and return the result.\n\
2687 Usage: $_shell (COMMAND)\n\
2691 COMMAND: The command to execute. Must be a string.\n\
2694 The command's exit code: zero on success, non-zero otherwise."),
2695 shell_internal_fn
, NULL
);
2697 add_cmd ("commands", no_set_class
, show_commands
, _("\
2698 Show the history of commands you typed.\n\
2699 You can supply a command number to start with, or a `+' to start after\n\
2700 the previous command number shown."),
2703 add_cmd ("version", no_set_class
, show_version
,
2704 _("Show what version of GDB this is."), &showlist
);
2706 add_cmd ("configuration", no_set_class
, show_configuration
,
2707 _("Show how GDB was configured at build time."), &showlist
);
2709 add_setshow_prefix_cmd ("debug", no_class
,
2710 _("Generic command for setting gdb debugging flags."),
2711 _("Generic command for showing gdb debugging flags."),
2712 &setdebuglist
, &showdebuglist
,
2713 &setlist
, &showlist
);
2715 cmd_list_element
*shell_cmd
2716 = add_com ("shell", class_support
, shell_command
, _("\
2717 Execute the rest of the line as a shell command.\n\
2718 With no arguments, run an inferior shell."));
2719 set_cmd_completer (shell_cmd
, filename_completer
);
2721 add_com_alias ("!", shell_cmd
, class_support
, 0);
2723 c
= add_com ("edit", class_files
, edit_command
, _("\
2724 Edit specified file or function.\n\
2725 With no argument, edits file containing most recent line listed.\n\
2726 Editing targets can be specified in these ways:\n\
2727 FILE:LINENUM, to edit at that line in that file,\n\
2728 FUNCTION, to edit at the beginning of that function,\n\
2729 FILE:FUNCTION, to distinguish among like-named static functions.\n\
2730 *ADDRESS, to edit at the line containing that address.\n\
2731 Uses EDITOR environment variable contents as editor (or ex as default)."));
2733 c
->completer
= location_completer
;
2735 cmd_list_element
*pipe_cmd
2736 = add_com ("pipe", class_support
, pipe_command
, _("\
2737 Send the output of a gdb command to a shell command.\n\
2738 Usage: | [COMMAND] | SHELL_COMMAND\n\
2739 Usage: | -d DELIM COMMAND DELIM SHELL_COMMAND\n\
2740 Usage: pipe [COMMAND] | SHELL_COMMAND\n\
2741 Usage: pipe -d DELIM COMMAND DELIM SHELL_COMMAND\n\
2743 Executes COMMAND and sends its output to SHELL_COMMAND.\n\
2745 The -d option indicates to use the string DELIM to separate COMMAND\n\
2746 from SHELL_COMMAND, in alternative to |. This is useful in\n\
2747 case COMMAND contains a | character.\n\
2749 With no COMMAND, repeat the last executed command\n\
2750 and send its output to SHELL_COMMAND."));
2751 set_cmd_completer_handle_brkchars (pipe_cmd
, pipe_command_completer
);
2752 add_com_alias ("|", pipe_cmd
, class_support
, 0);
2754 cmd_list_element
*list_cmd
2755 = add_com ("list", class_files
, list_command
, _("\
2756 List specified function or line.\n\
2757 With no argument, lists ten more lines after or around previous listing.\n\
2758 \"list -\" lists the ten lines before a previous ten-line listing.\n\
2759 One argument specifies a line, and ten lines are listed around that line.\n\
2760 Two arguments with comma between specify starting and ending lines to list.\n\
2761 Lines can be specified in these ways:\n\
2762 LINENUM, to list around that line in current file,\n\
2763 FILE:LINENUM, to list around that line in that file,\n\
2764 FUNCTION, to list around beginning of that function,\n\
2765 FILE:FUNCTION, to distinguish among like-named static functions.\n\
2766 *ADDRESS, to list around the line containing that address.\n\
2767 With two args, if one is empty, it stands for ten lines away from\n\
2770 By default, when a single location is given, display ten lines.\n\
2771 This can be changed using \"set listsize\", and the current value\n\
2772 can be shown using \"show listsize\"."));
2774 add_com_alias ("l", list_cmd
, class_files
, 1);
2776 c
= add_com ("disassemble", class_vars
, disassemble_command
, _("\
2777 Disassemble a specified section of memory.\n\
2778 Usage: disassemble[/m|/r|/s] START [, END]\n\
2779 Default is the function surrounding the pc of the selected frame.\n\
2781 With a /s modifier, source lines are included (if available).\n\
2782 In this mode, the output is displayed in PC address order, and\n\
2783 file names and contents for all relevant source files are displayed.\n\
2785 With a /m modifier, source lines are included (if available).\n\
2786 This view is \"source centric\": the output is in source line order,\n\
2787 regardless of any optimization that is present. Only the main source file\n\
2788 is displayed, not those of, e.g., any inlined functions.\n\
2789 This modifier hasn't proved useful in practice and is deprecated\n\
2792 With a /r modifier, raw instructions in hex are included.\n\
2794 With a single argument, the function surrounding that address is dumped.\n\
2795 Two arguments (separated by a comma) are taken as a range of memory to dump,\n\
2796 in the form of \"start,end\", or \"start,+length\".\n\
2798 Note that the address is interpreted as an expression, not as a location\n\
2799 like in the \"break\" command.\n\
2800 So, for example, if you want to disassemble function bar in file foo.c\n\
2801 you must type \"disassemble 'foo.c'::bar\" and not \"disassemble foo.c:bar\"."));
2802 set_cmd_completer (c
, location_completer
);
2804 c
= add_com ("make", class_support
, make_command
, _("\
2805 Run the ``make'' program using the rest of the line as arguments."));
2806 set_cmd_completer (c
, filename_completer
);
2807 c
= add_cmd ("user", no_class
, show_user
, _("\
2808 Show definitions of non-python/scheme user defined commands.\n\
2809 Argument is the name of the user defined command.\n\
2810 With no argument, show definitions of all user defined commands."), &showlist
);
2811 set_cmd_completer (c
, show_user_completer
);
2812 add_com ("apropos", class_support
, apropos_command
, _("\
2813 Search for commands matching a REGEXP.\n\
2814 Usage: apropos [-v] REGEXP\n\
2815 Flag -v indicates to produce a verbose output, showing full documentation\n\
2816 of the matching commands."));
2818 add_setshow_uinteger_cmd ("max-user-call-depth", no_class
,
2819 &max_user_call_depth
, _("\
2820 Set the max call depth for non-python/scheme user-defined commands."), _("\
2821 Show the max call depth for non-python/scheme user-defined commands."), NULL
,
2823 show_max_user_call_depth
,
2824 &setlist
, &showlist
);
2826 add_setshow_boolean_cmd ("trace-commands", no_class
, &trace_commands
, _("\
2827 Set tracing of GDB CLI commands."), _("\
2828 Show state of GDB CLI command tracing."), _("\
2829 When 'on', each command is displayed as it is executed."),
2832 &setlist
, &showlist
);
2834 const auto alias_opts
= make_alias_options_def_group (nullptr);
2836 static std::string alias_help
2837 = gdb::option::build_help (_("\
2838 Define a new command that is an alias of an existing command.\n\
2839 Usage: alias [-a] [--] ALIAS = COMMAND [DEFAULT-ARGS...]\n\
2840 ALIAS is the name of the alias command to create.\n\
2841 COMMAND is the command being aliased to.\n\
2846 GDB will automatically prepend the provided DEFAULT-ARGS to the list\n\
2847 of arguments explicitly provided when using ALIAS.\n\
2848 Use \"help aliases\" to list all user defined aliases and their default args.\n\
2851 Make \"spe\" an alias of \"set print elements\":\n\
2852 alias spe = set print elements\n\
2853 Make \"elms\" an alias of \"elements\" in the \"set print\" command:\n\
2854 alias -a set print elms = set print elements\n\
2855 Make \"btf\" an alias of \"backtrace -full -past-entry -past-main\" :\n\
2856 alias btf = backtrace -full -past-entry -past-main\n\
2857 Make \"wLapPeu\" an alias of 2 nested \"with\":\n\
2858 alias wLapPeu = with language pascal -- with print elements unlimited --"),
2861 c
= add_com ("alias", class_support
, alias_command
,
2862 alias_help
.c_str ());
2864 set_cmd_completer_handle_brkchars (c
, alias_command_completer
);
2866 add_setshow_boolean_cmd ("suppress-cli-notifications", no_class
,
2867 &user_wants_cli_suppress_notification
,
2869 Set whether printing notifications on CLI is suppressed."), _("\
2870 Show whether printing notifications on CLI is suppressed."), _("\
2871 When on, printing notifications (such as inferior/thread switch)\n\
2872 on CLI is suppressed."),
2873 set_suppress_cli_notifications
,
2874 show_suppress_cli_notifications
,
2878 const char *source_help_text
= xstrprintf (_("\
2879 Read commands from a file named FILE.\n\
2881 Usage: source [-s] [-v] FILE\n\
2882 -s: search for the script in the source search path,\n\
2883 even if FILE contains directories.\n\
2884 -v: each command in FILE is echoed as it is executed.\n\
2886 Note that the file \"%s\" is read automatically in this way\n\
2887 when GDB is started."), GDBINIT
).release ();
2888 c
= add_cmd ("source", class_support
, source_command
,
2889 source_help_text
, &cmdlist
);
2890 set_cmd_completer (c
, filename_completer
);