3 Copyright (C) 2000-2024 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/>. */
20 #include "arch-utils.h"
21 #include "readline/tilde.h"
22 #include "completer.h"
24 #include "gdbsupport/gdb_wait.h"
25 #include "gdbsupport/gdb_regex.h"
26 #include "gdb_vfork.h"
28 #include "expression.h"
32 #include "filenames.h"
36 #include "tracepoint.h"
37 #include "gdbsupport/filestuff.h"
47 #include "cli/cli-decode.h"
48 #include "cli/cli-script.h"
49 #include "cli/cli-setshow.h"
50 #include "cli/cli-cmds.h"
51 #include "cli/cli-style.h"
52 #include "cli/cli-utils.h"
53 #include "cli/cli-style.h"
55 #include "extension.h"
56 #include "gdbsupport/pathstuff.h"
57 #include "gdbsupport/gdb_tilde_expand.h"
67 /* Prototypes for local utility functions */
69 static void print_sal_location (const symtab_and_line
&sal
);
71 static void ambiguous_line_spec (gdb::array_view
<const symtab_and_line
> sals
,
72 const char *format
, ...)
73 ATTRIBUTE_PRINTF (2, 3);
75 static void filter_sals (std::vector
<symtab_and_line
> &);
79 unsigned int max_user_call_depth
= 1024;
81 /* Define all cmd_list_elements. */
83 /* Chain containing all defined commands. */
85 struct cmd_list_element
*cmdlist
;
87 /* Chain containing all defined info subcommands. */
89 struct cmd_list_element
*infolist
;
91 /* Chain containing all defined enable subcommands. */
93 struct cmd_list_element
*enablelist
;
95 /* Chain containing all defined disable subcommands. */
97 struct cmd_list_element
*disablelist
;
99 /* Chain containing all defined stop subcommands. */
101 struct cmd_list_element
*stoplist
;
103 /* Chain containing all defined delete subcommands. */
105 struct cmd_list_element
*deletelist
;
107 /* Chain containing all defined detach subcommands. */
109 struct cmd_list_element
*detachlist
;
111 /* Chain containing all defined kill subcommands. */
113 struct cmd_list_element
*killlist
;
115 /* Chain containing all defined set subcommands */
117 struct cmd_list_element
*setlist
;
119 /* Chain containing all defined unset subcommands */
121 struct cmd_list_element
*unsetlist
;
123 /* Chain containing all defined show subcommands. */
125 struct cmd_list_element
*showlist
;
127 /* Chain containing all defined \"set history\". */
129 struct cmd_list_element
*sethistlist
;
131 /* Chain containing all defined \"show history\". */
133 struct cmd_list_element
*showhistlist
;
135 /* Chain containing all defined \"unset history\". */
137 struct cmd_list_element
*unsethistlist
;
139 /* Chain containing all defined maintenance subcommands. */
141 struct cmd_list_element
*maintenancelist
;
143 /* Chain containing all defined "maintenance info" subcommands. */
145 struct cmd_list_element
*maintenanceinfolist
;
147 /* Chain containing all defined "maintenance print" subcommands. */
149 struct cmd_list_element
*maintenanceprintlist
;
151 /* Chain containing all defined "maintenance check" subcommands. */
153 struct cmd_list_element
*maintenancechecklist
;
155 /* Chain containing all defined "maintenance flush" subcommands. */
157 struct cmd_list_element
*maintenanceflushlist
;
159 struct cmd_list_element
*setprintlist
;
161 struct cmd_list_element
*showprintlist
;
163 struct cmd_list_element
*setdebuglist
;
165 struct cmd_list_element
*showdebuglist
;
167 struct cmd_list_element
*setchecklist
;
169 struct cmd_list_element
*showchecklist
;
171 struct cmd_list_element
*setsourcelist
;
173 struct cmd_list_element
*showsourcelist
;
175 /* Command tracing state. */
177 int source_verbose
= 0;
178 bool trace_commands
= false;
180 /* 'script-extension' option support. */
182 static const char script_ext_off
[] = "off";
183 static const char script_ext_soft
[] = "soft";
184 static const char script_ext_strict
[] = "strict";
186 static const char *const script_ext_enums
[] = {
193 static const char *script_ext_mode
= script_ext_soft
;
196 /* User-controllable flag to suppress event notification on CLI. */
198 static bool user_wants_cli_suppress_notification
= false;
200 /* Utility used everywhere when at least one argument is needed and
204 error_no_arg (const char *why
)
206 error (_("Argument required (%s)."), why
);
209 /* This implements the "info" prefix command. Normally such commands
210 are automatically handled by add_basic_prefix_cmd, but in this case
211 a separate command is used so that it can be hooked into by
215 info_command (const char *arg
, int from_tty
)
217 help_list (infolist
, "info ", all_commands
, gdb_stdout
);
220 /* See cli/cli-cmds.h. */
223 with_command_1 (const char *set_cmd_prefix
,
224 cmd_list_element
*setlist
, const char *args
, int from_tty
)
227 error (_("Missing arguments."));
229 const char *delim
= strstr (args
, "--");
230 const char *nested_cmd
= nullptr;
233 error (_("Missing setting before '--' delimiter"));
235 if (delim
== nullptr || *skip_spaces (&delim
[2]) == '\0')
236 nested_cmd
= repeat_previous ();
238 cmd_list_element
*set_cmd
= lookup_cmd (&args
, setlist
, set_cmd_prefix
,
240 /*allow_unknown=*/ 0,
241 /*ignore_help_classes=*/ 1);
242 gdb_assert (set_cmd
!= nullptr);
244 if (!set_cmd
->var
.has_value ())
245 error (_("Cannot use this setting with the \"with\" command"));
247 std::string temp_value
248 = (delim
== nullptr ? args
: std::string (args
, delim
- args
));
250 if (nested_cmd
== nullptr)
251 nested_cmd
= skip_spaces (delim
+ 2);
253 gdb_assert (set_cmd
->var
.has_value ());
254 std::string org_value
= get_setshow_command_value_string (*set_cmd
->var
);
256 /* Tweak the setting to the new temporary value. */
257 do_set_command (temp_value
.c_str (), from_tty
, set_cmd
);
261 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
263 /* Execute the nested command. */
264 execute_command (nested_cmd
, from_tty
);
266 catch (const gdb_exception
&ex
)
268 /* Restore the setting and rethrow. If restoring the setting
269 throws, swallow the new exception and warn. There's nothing
270 else we can reasonably do. */
273 do_set_command (org_value
.c_str (), from_tty
, set_cmd
);
275 catch (const gdb_exception
&ex2
)
277 warning (_("Couldn't restore setting: %s"), ex2
.what ());
283 /* Restore the setting. */
284 do_set_command (org_value
.c_str (), from_tty
, set_cmd
);
287 /* See cli/cli-cmds.h. */
290 with_command_completer_1 (const char *set_cmd_prefix
,
291 completion_tracker
&tracker
,
294 tracker
.set_use_custom_word_point (true);
296 const char *delim
= strstr (text
, "--");
298 /* If we're still not past the "--" delimiter, complete the "with"
299 command as if it was a "set" command. */
302 || !isspace (delim
[-1])
303 || !(isspace (delim
[2]) || delim
[2] == '\0'))
305 std::string new_text
= std::string (set_cmd_prefix
) + text
;
306 tracker
.advance_custom_word_point_by (-(int) strlen (set_cmd_prefix
));
307 complete_nested_command_line (tracker
, new_text
.c_str ());
311 /* We're past the "--" delimiter. Complete on the sub command. */
312 const char *nested_cmd
= skip_spaces (delim
+ 2);
313 tracker
.advance_custom_word_point_by (nested_cmd
- text
);
314 complete_nested_command_line (tracker
, nested_cmd
);
317 /* The "with" command. */
320 with_command (const char *args
, int from_tty
)
322 with_command_1 ("set ", setlist
, args
, from_tty
);
325 /* "with" command completer. */
328 with_command_completer (struct cmd_list_element
*ignore
,
329 completion_tracker
&tracker
,
330 const char *text
, const char * /*word*/)
332 with_command_completer_1 ("set ", tracker
, text
);
335 /* Look up the contents of TEXT as a command usable with default args.
336 Throws an error if no such command is found.
337 Return the found command and advances TEXT past the found command.
338 If the found command is a postfix command, set *PREFIX_CMD to its
341 static struct cmd_list_element
*
342 lookup_cmd_for_default_args (const char **text
,
343 struct cmd_list_element
**prefix_cmd
)
345 const char *orig_text
= *text
;
346 struct cmd_list_element
*lcmd
;
348 if (*text
== nullptr || skip_spaces (*text
) == nullptr)
349 error (_("ALIAS missing."));
351 /* We first use lookup_cmd to verify TEXT unambiguously identifies
353 lcmd
= lookup_cmd (text
, cmdlist
, "", NULL
,
354 /*allow_unknown=*/ 0,
355 /*ignore_help_classes=*/ 1);
357 /* Note that we accept default args for prefix commands,
358 as a prefix command can also be a valid usable
359 command accepting some arguments.
360 For example, "thread apply" applies a command to a
361 list of thread ids, and is also the prefix command for
364 /* We have an unambiguous command for which default args
365 can be specified. What remains after having found LCMD
366 is either spaces, or the default args character. */
368 /* We then use lookup_cmd_composition to detect if the user
369 has specified an alias, and find the possible prefix_cmd
371 struct cmd_list_element
*alias
, *cmd
;
372 lookup_cmd_composition
373 (std::string (orig_text
, *text
- orig_text
).c_str (),
374 &alias
, prefix_cmd
, &cmd
);
375 gdb_assert (cmd
!= nullptr);
376 gdb_assert (cmd
== lcmd
);
377 if (alias
!= nullptr)
383 /* Provide documentation on command or list given by COMMAND. FROM_TTY
387 help_command (const char *command
, int from_tty
)
389 help_cmd (command
, gdb_stdout
);
393 /* Note: The "complete" command is used by Emacs to implement completion.
394 [Is that why this function writes output with *_unfiltered?] */
397 complete_command (const char *arg
, int from_tty
)
401 if (max_completions
== 0)
403 /* Only print this for non-mi frontends. An MI frontend may not
404 be able to handle this. */
405 if (!current_uiout
->is_mi_like_p ())
407 printf_unfiltered (_("max-completions is zero,"
408 " completion is disabled.\n"));
416 int quote_char
= '\0';
419 completion_result result
= complete (arg
, &word
, "e_char
);
421 if (result
.number_matches
!= 0)
423 std::string
arg_prefix (arg
, word
- arg
);
425 if (result
.number_matches
== 1)
426 printf_unfiltered ("%s%s\n", arg_prefix
.c_str (), result
.match_list
[0]);
429 result
.sort_match_list ();
431 for (size_t i
= 0; i
< result
.number_matches
; i
++)
433 printf_unfiltered ("%s%s",
435 result
.match_list
[i
+ 1]);
437 printf_unfiltered ("%c", quote_char
);
438 printf_unfiltered ("\n");
442 if (result
.number_matches
== max_completions
)
444 /* ARG_PREFIX and WORD are included in the output so that emacs
445 will include the message in the output. */
446 printf_unfiltered (_("%s%s %s\n"),
447 arg_prefix
.c_str (), word
,
448 get_max_completions_reached_message ());
454 is_complete_command (struct cmd_list_element
*c
)
456 return cmd_simple_func_eq (c
, complete_command
);
460 show_version (const char *args
, int from_tty
)
462 print_gdb_version (gdb_stdout
, true);
467 show_configuration (const char *args
, int from_tty
)
469 print_gdb_configuration (gdb_stdout
);
472 /* Handle the quit command. */
475 quit_command (const char *args
, int from_tty
)
479 /* An optional expression may be used to cause gdb to terminate with
480 the value of that expression. */
483 struct value
*val
= parse_and_eval (args
);
485 exit_code
= (int) value_as_long (val
);
488 if (!quit_confirm ())
489 error (_("Not confirmed."));
493 query_if_trace_running (from_tty
);
495 catch (const gdb_exception_error
&ex
)
497 if (ex
.error
== TARGET_CLOSE_ERROR
)
498 /* We don't care about this since we're quitting anyway, so keep
500 exception_print (gdb_stderr
, ex
);
502 /* Rethrow, to properly handle error (_("Not confirmed.")). */
506 quit_force (args
? &exit_code
: NULL
, from_tty
);
510 pwd_command (const char *args
, int from_tty
)
513 error (_("The \"pwd\" command does not take an argument: %s"), args
);
515 gdb::unique_xmalloc_ptr
<char> cwd (getcwd (NULL
, 0));
518 error (_("Error finding name of working directory: %s"),
519 safe_strerror (errno
));
521 if (strcmp (cwd
.get (), current_directory
) != 0)
522 gdb_printf (_("Working directory %ps\n (canonically %ps).\n"),
523 styled_string (file_name_style
.style (),
525 styled_string (file_name_style
.style (), cwd
.get ()));
527 gdb_printf (_("Working directory %ps.\n"),
528 styled_string (file_name_style
.style (),
533 cd_command (const char *dir
, int from_tty
)
536 /* Found something other than leading repetitions of "/..". */
540 /* If the new directory is absolute, repeat is a no-op; if relative,
541 repeat might be useful but is more likely to be a mistake. */
544 gdb::unique_xmalloc_ptr
<char> dir_holder
545 (tilde_expand (dir
!= NULL
? dir
: "~"));
546 dir
= dir_holder
.get ();
549 perror_with_name (dir
);
551 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
552 /* There's too much mess with DOSish names like "d:", "d:.",
553 "d:./foo" etc. Instead of having lots of special #ifdef'ed code,
554 simply get the canonicalized name of the current directory. */
555 gdb::unique_xmalloc_ptr
<char> cwd (getcwd (NULL
, 0));
560 if (IS_DIR_SEPARATOR (dir
[len
- 1]))
562 /* Remove the trailing slash unless this is a root directory
563 (including a drive letter on non-Unix systems). */
564 if (!(len
== 1) /* "/" */
565 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
566 && !(len
== 3 && dir
[1] == ':') /* "d:/" */
572 dir_holder
.reset (savestring (dir
, len
));
573 if (IS_ABSOLUTE_PATH (dir_holder
.get ()))
575 xfree (current_directory
);
576 current_directory
= dir_holder
.release ();
580 if (IS_DIR_SEPARATOR (current_directory
[strlen (current_directory
) - 1]))
581 current_directory
= concat (current_directory
, dir_holder
.get (),
584 current_directory
= concat (current_directory
, SLASH_STRING
,
585 dir_holder
.get (), (char *) NULL
);
588 /* Now simplify any occurrences of `.' and `..' in the pathname. */
591 for (p
= current_directory
; *p
;)
593 if (IS_DIR_SEPARATOR (p
[0]) && p
[1] == '.'
594 && (p
[2] == 0 || IS_DIR_SEPARATOR (p
[2])))
595 memmove (p
, p
+ 2, strlen (p
+ 2) + 1);
596 else if (IS_DIR_SEPARATOR (p
[0]) && p
[1] == '.' && p
[2] == '.'
597 && (p
[3] == 0 || IS_DIR_SEPARATOR (p
[3])))
601 /* Search backwards for the directory just before the "/.."
602 and obliterate it and the "/..". */
605 while (q
!= current_directory
&& !IS_DIR_SEPARATOR (q
[-1]))
608 if (q
== current_directory
)
609 /* current_directory is
610 a relative pathname ("can't happen"--leave it alone). */
614 memmove (q
- 1, p
+ 3, strlen (p
+ 3) + 1);
619 /* We are dealing with leading repetitions of "/..", for
620 example "/../..", which is the Mach super-root. */
630 forget_cached_source_info ();
633 pwd_command ((char *) 0, 1);
636 /* Show the current value of the 'script-extension' option. */
639 show_script_ext_mode (struct ui_file
*file
, int from_tty
,
640 struct cmd_list_element
*c
, const char *value
)
643 _("Script filename extension recognition is \"%s\".\n"),
647 /* Try to open SCRIPT_FILE.
648 If successful, the full path name is stored in *FULL_PATHP,
649 and the stream is returned.
650 If not successful, return NULL; errno is set for the last file
653 If SEARCH_PATH is non-zero, and the file isn't found in cwd,
654 search for it in the source search path. */
656 std::optional
<open_script
>
657 find_and_open_script (const char *script_file
, int search_path
)
660 openp_flags search_flags
= OPF_TRY_CWD_FIRST
| OPF_RETURN_REALPATH
;
661 std::optional
<open_script
> opened
;
663 gdb::unique_xmalloc_ptr
<char> file (tilde_expand (script_file
));
666 search_flags
|= OPF_SEARCH_IN_PATH
;
668 /* Search for and open 'file' on the search path used for source
669 files. Put the full location in *FULL_PATHP. */
670 gdb::unique_xmalloc_ptr
<char> full_path
;
671 fd
= openp (source_path
.c_str (), search_flags
,
672 file
.get (), O_RDONLY
, &full_path
);
677 FILE *result
= fdopen (fd
, FOPEN_RT
);
680 int save_errno
= errno
;
686 opened
.emplace (gdb_file_up (result
), std::move (full_path
));
691 /* Load script FILE, which has already been opened as STREAM.
692 FILE_TO_OPEN is the form of FILE to use if one needs to open the file.
693 This is provided as FILE may have been found via the source search path.
694 An important thing to note here is that FILE may be a symlink to a file
695 with a different or non-existing suffix, and thus one cannot infer the
696 extension language from FILE_TO_OPEN. */
699 source_script_from_stream (FILE *stream
, const char *file
,
700 const char *file_to_open
)
702 if (script_ext_mode
!= script_ext_off
)
704 const struct extension_language_defn
*extlang
705 = get_ext_lang_of_file (file
);
709 if (ext_lang_present_p (extlang
))
711 script_sourcer_func
*sourcer
712 = ext_lang_script_sourcer (extlang
);
714 gdb_assert (sourcer
!= NULL
);
715 sourcer (extlang
, stream
, file_to_open
);
718 else if (script_ext_mode
== script_ext_soft
)
720 /* Assume the file is a gdb script.
721 This is handled below. */
724 throw_ext_lang_unsupported (extlang
);
728 script_from_file (stream
, file
);
731 /* Worker to perform the "source" command.
733 If SEARCH_PATH is non-zero, and the file isn't found in cwd,
734 search for it in the source search path. */
737 source_script_with_search (const char *file
, int from_tty
, int search_path
)
740 if (file
== NULL
|| *file
== 0)
741 error (_("source command requires file name of file to source."));
743 std::optional
<open_script
> opened
= find_and_open_script (file
, search_path
);
746 /* The script wasn't found, or was otherwise inaccessible.
747 If the source command was invoked interactively, throw an
748 error. Otherwise (e.g. if it was invoked by a script),
749 just emit a warning, rather than cause an error. */
751 perror_with_name (file
);
754 perror_warning_with_name (file
);
759 /* The python support reopens the file, so we need to pass full_path here
760 in case the file was found on the search path. It's useful to do this
761 anyway so that error messages show the actual file used. But only do
762 this if we (may have) used search_path, as printing the full path in
763 errors for the non-search case can be more noise than signal. */
764 const char *file_to_open
;
765 std::string tilde_expanded_file
;
767 file_to_open
= opened
->full_path
.get ();
770 tilde_expanded_file
= gdb_tilde_expand (file
);
771 file_to_open
= tilde_expanded_file
.c_str ();
773 source_script_from_stream (opened
->stream
.get (), file
, file_to_open
);
776 /* Wrapper around source_script_with_search to export it to main.c
777 for use in loading .gdbinit scripts. */
780 source_script (const char *file
, int from_tty
)
782 source_script_with_search (file
, from_tty
, 0);
786 source_command (const char *args
, int from_tty
)
788 const char *file
= args
;
791 scoped_restore save_source_verbose
= make_scoped_restore (&source_verbose
);
793 /* -v causes the source command to run in verbose mode.
794 -s causes the file to be searched in the source search path,
795 even if the file name contains a '/'.
796 We still have to be able to handle filenames with spaces in a
797 backward compatible way, so buildargv is not appropriate. */
801 while (args
[0] != '\0')
803 /* Make sure leading white space does not break the
805 args
= skip_spaces (args
);
810 if (args
[1] == 'v' && isspace (args
[2]))
814 /* Skip passed -v. */
817 else if (args
[1] == 's' && isspace (args
[2]))
821 /* Skip passed -s. */
828 file
= skip_spaces (args
);
831 source_script_with_search (file
, from_tty
, search_path
);
836 echo_command (const char *text
, int from_tty
)
838 const char *p
= text
;
842 while ((c
= *p
++) != '\0')
846 /* \ at end of argument is used after spaces
847 so they won't be lost. */
851 c
= parse_escape (get_current_arch (), &p
);
853 gdb_printf ("%c", c
);
856 gdb_printf ("%c", c
);
859 gdb_stdout
->reset_style ();
861 /* Force this output to appear now. */
862 gdb_flush (gdb_stdout
);
865 /* Sets the last launched shell command convenience variables based on
869 exit_status_set_internal_vars (int exit_status
)
871 struct internalvar
*var_code
= lookup_internalvar ("_shell_exitcode");
872 struct internalvar
*var_signal
= lookup_internalvar ("_shell_exitsignal");
874 clear_internalvar (var_code
);
875 clear_internalvar (var_signal
);
877 /* Keep the logic here in sync with shell_internal_fn. */
879 if (WIFEXITED (exit_status
))
880 set_internalvar_integer (var_code
, WEXITSTATUS (exit_status
));
882 else if (WIFSIGNALED (exit_status
) && WTERMSIG (exit_status
) == -1)
884 /* The -1 condition can happen on MinGW, if we don't recognize
885 the fatal exception code encoded in the exit status; see
886 gdbsupport/gdb_wait.c. We don't want to lose information in
887 the exit status in that case. Record it as a normal exit
888 with the full exit status, including the higher 0xC0000000
890 set_internalvar_integer (var_code
, exit_status
);
893 else if (WIFSIGNALED (exit_status
))
894 set_internalvar_integer (var_signal
, WTERMSIG (exit_status
));
896 warning (_("unexpected shell command exit status %d"), exit_status
);
899 /* Run ARG under the shell, and return the exit status. If ARG is
900 NULL, run an interactive shell. */
903 run_under_shell (const char *arg
, int from_tty
)
905 #if defined(CANT_FORK) || \
906 (!defined(HAVE_WORKING_VFORK) && !defined(HAVE_WORKING_FORK))
907 /* If ARG is NULL, they want an inferior shell, but `system' just
908 reports if the shell is available when passed a NULL arg. */
909 int rc
= system (arg
? arg
: "");
912 arg
= "inferior shell";
915 gdb_printf (gdb_stderr
, "Cannot execute %s: %s\n", arg
,
916 safe_strerror (errno
));
918 gdb_printf (gdb_stderr
, "%s exited with status %d\n", arg
, rc
);
920 /* Make sure to return to the directory GDB thinks it is, in case
921 the shell command we just ran changed it. */
922 chdir (current_directory
);
925 #else /* Can fork. */
928 if ((pid
= vfork ()) == 0)
930 const char *p
, *user_shell
= get_shell ();
934 /* Get the name of the shell for arg0. */
935 p
= lbasename (user_shell
);
938 execl (user_shell
, p
, (char *) 0);
940 execl (user_shell
, p
, "-c", arg
, (char *) 0);
942 gdb_printf (gdb_stderr
, "Cannot execute %s: %s\n", user_shell
,
943 safe_strerror (errno
));
948 waitpid (pid
, &status
, 0);
950 error (_("Fork failed"));
952 #endif /* Can fork. */
955 /* Escape out to the shell to run ARG. If ARG is NULL, launch and
956 interactive shell. Sets $_shell_exitcode and $_shell_exitsignal
957 convenience variables based on the exits status. */
960 shell_escape (const char *arg
, int from_tty
)
962 int status
= run_under_shell (arg
, from_tty
);
963 exit_status_set_internal_vars (status
);
966 /* Implementation of the "shell" command. */
969 shell_command (const char *arg
, int from_tty
)
971 shell_escape (arg
, from_tty
);
975 edit_command (const char *arg
, int from_tty
)
977 struct symtab_and_line sal
;
982 /* Pull in the current default source line if necessary. */
985 set_default_source_symtab_and_line ();
986 sal
= get_current_source_symtab_and_line ();
989 /* Bare "edit" edits file with present line. */
994 error (_("No default source file yet."));
995 sal
.line
+= get_lines_to_list () / 2;
1001 /* Now should only be one argument -- decode it in SAL. */
1003 location_spec_up locspec
= string_to_location_spec (&arg1
,
1007 error (_("Junk at end of line specification."));
1009 std::vector
<symtab_and_line
> sals
= decode_line_1 (locspec
.get (),
1010 DECODE_LINE_LIST_MODE
,
1019 if (sals
.size () > 1)
1021 ambiguous_line_spec (sals
,
1022 _("Specified line is ambiguous:\n"));
1028 /* If line was specified by address, first print exactly which
1029 line, and which file. In this case, sal.symtab == 0 means
1030 address is outside of all known source files, not that user
1031 failed to give a filename. */
1034 struct gdbarch
*gdbarch
;
1036 if (sal
.symtab
== 0)
1037 error (_("No source file for address %s."),
1038 paddress (get_current_arch (), sal
.pc
));
1040 gdbarch
= sal
.symtab
->compunit ()->objfile ()->arch ();
1041 sym
= find_pc_function (sal
.pc
);
1043 gdb_printf ("%s is in %s (%s:%d).\n",
1044 paddress (gdbarch
, sal
.pc
),
1046 symtab_to_filename_for_display (sal
.symtab
),
1049 gdb_printf ("%s is at %s:%d.\n",
1050 paddress (gdbarch
, sal
.pc
),
1051 symtab_to_filename_for_display (sal
.symtab
),
1055 /* If what was given does not imply a symtab, it must be an
1056 undebuggable symbol which means no source code. */
1058 if (sal
.symtab
== 0)
1059 error (_("No line number known for %s."), arg
);
1062 if ((editor
= getenv ("EDITOR")) == NULL
)
1065 fn
= symtab_to_fullname (sal
.symtab
);
1067 /* Quote the file name, in case it has whitespace or other special
1069 gdb::unique_xmalloc_ptr
<char> p
1070 = xstrprintf ("%s +%d \"%s\"", editor
, sal
.line
, fn
);
1071 shell_escape (p
.get (), from_tty
);
1074 /* The options for the "pipe" command. */
1076 struct pipe_cmd_opts
1079 std::string delimiter
;
1082 static const gdb::option::option_def pipe_cmd_option_defs
[] = {
1084 gdb::option::string_option_def
<pipe_cmd_opts
> {
1086 [] (pipe_cmd_opts
*opts
) { return &opts
->delimiter
; },
1088 N_("Indicates to use the specified delimiter string to separate\n\
1089 COMMAND from SHELL_COMMAND, in alternative to |. This is useful in\n\
1090 case COMMAND contains a | character."),
1095 /* Create an option_def_group for the "pipe" command's options, with
1098 static inline gdb::option::option_def_group
1099 make_pipe_cmd_options_def_group (pipe_cmd_opts
*opts
)
1101 return {{pipe_cmd_option_defs
}, opts
};
1104 /* Implementation of the "pipe" command. */
1107 pipe_command (const char *arg
, int from_tty
)
1111 auto grp
= make_pipe_cmd_options_def_group (&opts
);
1112 gdb::option::process_options
1113 (&arg
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
);
1115 const char *delim
= "|";
1116 if (!opts
.delimiter
.empty ())
1117 delim
= opts
.delimiter
.c_str ();
1119 const char *command
= arg
;
1120 if (command
== nullptr)
1121 error (_("Missing COMMAND"));
1123 arg
= strstr (arg
, delim
);
1126 error (_("Missing delimiter before SHELL_COMMAND"));
1128 std::string
gdb_cmd (command
, arg
- command
);
1130 arg
+= strlen (delim
); /* Skip the delimiter. */
1132 if (gdb_cmd
.empty ())
1133 gdb_cmd
= repeat_previous ();
1135 const char *shell_command
= skip_spaces (arg
);
1136 if (*shell_command
== '\0')
1137 error (_("Missing SHELL_COMMAND"));
1139 FILE *to_shell_command
= popen (shell_command
, "w");
1141 if (to_shell_command
== nullptr)
1142 error (_("Error launching \"%s\""), shell_command
);
1146 stdio_file
pipe_file (to_shell_command
);
1148 execute_command_to_ui_file (&pipe_file
, gdb_cmd
.c_str (), from_tty
);
1152 pclose (to_shell_command
);
1156 int exit_status
= pclose (to_shell_command
);
1158 if (exit_status
< 0)
1159 error (_("shell command \"%s\" failed: %s"), shell_command
,
1160 safe_strerror (errno
));
1161 exit_status_set_internal_vars (exit_status
);
1164 /* Completer for the pipe command. */
1167 pipe_command_completer (struct cmd_list_element
*ignore
,
1168 completion_tracker
&tracker
,
1169 const char *text
, const char *word_ignored
)
1173 const char *org_text
= text
;
1174 auto grp
= make_pipe_cmd_options_def_group (&opts
);
1175 if (gdb::option::complete_options
1176 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, grp
))
1179 const char *delimiter
= "|";
1180 if (!opts
.delimiter
.empty ())
1181 delimiter
= opts
.delimiter
.c_str ();
1183 /* Check if we're past option values already. */
1184 if (text
> org_text
&& !isspace (text
[-1]))
1187 const char *delim
= strstr (text
, delimiter
);
1189 /* If we're still not past the delimiter, complete the gdb
1191 if (delim
== nullptr || delim
== text
)
1193 complete_nested_command_line (tracker
, text
);
1197 /* We're past the delimiter. What follows is a shell command, which
1198 we don't know how to complete. */
1201 /* Helper for the list_command function. Prints the lines around (and
1202 including) line stored in CURSAL. ARG contains the arguments used in
1203 the command invocation, and is used to determine a special case when
1204 printing backwards. */
1206 list_around_line (const char *arg
, symtab_and_line cursal
)
1210 first
= std::max (cursal
.line
- get_lines_to_list () / 2, 1);
1212 /* A small special case --- if listing backwards, and we
1213 should list only one line, list the preceding line,
1214 instead of the exact line we've just shown after e.g.,
1215 stopping for a breakpoint. */
1216 if (arg
!= NULL
&& arg
[0] == '-'
1217 && get_lines_to_list () == 1 && first
> 1)
1220 print_source_lines (cursal
.symtab
, source_lines_range (first
), 0);
1224 list_command (const char *arg
, int from_tty
)
1231 int linenum_beg
= 0;
1234 /* Pull in the current default source line if necessary. */
1235 if (arg
== NULL
|| ((arg
[0] == '+' || arg
[0] == '-' || arg
[0] == '.') && arg
[1] == '\0'))
1237 set_default_source_symtab_and_line ();
1238 symtab_and_line cursal
= get_current_source_symtab_and_line ();
1240 /* If this is the first "list" since we've set the current
1241 source line, center the listing around that line. */
1242 if (get_first_line_listed () == 0 && (arg
== nullptr || arg
[0] != '.'))
1244 list_around_line (arg
, cursal
);
1247 /* "l" and "l +" lists the next few lines, unless we're listing past
1248 the end of the file. */
1249 else if (arg
== nullptr || arg
[0] == '+')
1251 if (last_symtab_line (cursal
.symtab
) >= cursal
.line
)
1252 print_source_lines (cursal
.symtab
,
1253 source_lines_range (cursal
.line
), 0);
1256 error (_("End of the file was already reached, use \"list .\" to"
1257 " list the current location again"));
1261 /* "l -" lists previous ten lines, the ones before the ten just
1263 else if (arg
[0] == '-')
1265 if (get_first_line_listed () == 1)
1266 error (_("Already at the start of %s."),
1267 symtab_to_filename_for_display (cursal
.symtab
));
1268 source_lines_range
range (get_first_line_listed (),
1269 source_lines_range::BACKWARD
);
1270 print_source_lines (cursal
.symtab
, range
, 0);
1273 /* "list ." lists the default location again. */
1274 else if (arg
[0] == '.')
1276 if (target_has_stack ())
1278 /* Find the current line by getting the PC of the currently
1279 selected frame, and finding the line associated to it. */
1280 frame_info_ptr frame
= get_selected_frame (nullptr);
1281 CORE_ADDR curr_pc
= get_frame_pc (frame
);
1282 cursal
= find_pc_line (curr_pc
, 0);
1286 /* The inferior is not running, so reset the current source
1287 location to the default (usually the main function). */
1288 clear_current_source_symtab_and_line ();
1289 set_default_source_symtab_and_line ();
1290 cursal
= get_current_source_symtab_and_line ();
1292 if (cursal
.symtab
== nullptr)
1293 error (_("No debug information available to print source lines."));
1294 list_around_line (arg
, cursal
);
1295 /* Set the repeat args so just pressing "enter" after using "list ."
1296 will print the following lines instead of the same lines again. */
1298 set_repeat_arguments ("");
1304 /* Now if there is only one argument, decode it in SAL
1306 If there are two arguments, decode them in SAL and SAL_END
1307 and clear NO_END; however, if one of the arguments is blank,
1308 set DUMMY_BEG or DUMMY_END to record that fact. */
1310 if (!have_full_symbols () && !have_partial_symbols ())
1311 error (_("No symbol table is loaded. Use the \"file\" command."));
1313 std::vector
<symtab_and_line
> sals
;
1314 symtab_and_line sal
, sal_end
;
1321 location_spec_up locspec
1322 = string_to_location_spec (&arg1
, current_language
);
1324 /* We know that the ARG string is not empty, yet the attempt to
1325 parse a location spec from the string consumed no characters.
1326 This most likely means that the first thing in ARG looks like
1327 a location spec condition, and so the string_to_location_spec
1328 call stopped parsing. */
1330 error (_("Junk at end of line specification."));
1332 sals
= decode_line_1 (locspec
.get (), DECODE_LINE_LIST_MODE
,
1344 /* Record whether the BEG arg is all digits. */
1346 for (p
= arg
; p
!= arg1
&& *p
>= '0' && *p
<= '9'; p
++);
1347 linenum_beg
= (p
== arg1
);
1349 /* Save the range of the first argument, in case we need to let the
1350 user know it was ambiguous. */
1351 const char *beg
= arg
;
1352 size_t beg_len
= arg1
- beg
;
1354 while (*arg1
== ' ' || *arg1
== '\t')
1359 if (sals
.size () > 1)
1361 ambiguous_line_spec (sals
,
1362 _("Specified first line '%.*s' is ambiguous:\n"),
1363 (int) beg_len
, beg
);
1367 while (*arg1
== ' ' || *arg1
== '\t')
1373 /* Save the last argument, in case we need to let the user
1374 know it was ambiguous. */
1375 const char *end_arg
= arg1
;
1377 location_spec_up locspec
1378 = string_to_location_spec (&arg1
, current_language
);
1381 error (_("Junk at end of line specification."));
1383 std::vector
<symtab_and_line
> sals_end
1385 ? decode_line_1 (locspec
.get (), DECODE_LINE_LIST_MODE
,
1387 : decode_line_1 (locspec
.get (), DECODE_LINE_LIST_MODE
,
1388 NULL
, sal
.symtab
, sal
.line
));
1390 filter_sals (sals_end
);
1391 if (sals_end
.empty ())
1393 if (sals_end
.size () > 1)
1395 ambiguous_line_spec (sals_end
,
1396 _("Specified last line '%s' is ambiguous:\n"),
1400 sal_end
= sals_end
[0];
1405 error (_("Junk at end of line specification."));
1407 if (!no_end
&& !dummy_beg
&& !dummy_end
1408 && sal
.symtab
!= sal_end
.symtab
)
1409 error (_("Specified first and last lines are in different files."));
1410 if (dummy_beg
&& dummy_end
)
1411 error (_("Two empty args do not say what lines to list."));
1413 /* If line was specified by address,
1414 first print exactly which line, and which file.
1416 In this case, sal.symtab == 0 means address is outside of all
1417 known source files, not that user failed to give a filename. */
1420 struct gdbarch
*gdbarch
;
1422 if (sal
.symtab
== 0)
1423 error (_("No source file for address %s."),
1424 paddress (get_current_arch (), sal
.pc
));
1426 gdbarch
= sal
.symtab
->compunit ()->objfile ()->arch ();
1427 sym
= find_pc_function (sal
.pc
);
1429 gdb_printf ("%s is in %s (%s:%d).\n",
1430 paddress (gdbarch
, sal
.pc
),
1432 symtab_to_filename_for_display (sal
.symtab
), sal
.line
);
1434 gdb_printf ("%s is at %s:%d.\n",
1435 paddress (gdbarch
, sal
.pc
),
1436 symtab_to_filename_for_display (sal
.symtab
), sal
.line
);
1439 /* If line was not specified by just a line number, and it does not
1440 imply a symtab, it must be an undebuggable symbol which means no
1443 if (!linenum_beg
&& sal
.symtab
== 0)
1444 error (_("No line number known for %s."), arg
);
1446 /* If this command is repeated with RET,
1447 turn it into the no-arg variant. */
1450 set_repeat_arguments ("");
1452 if (dummy_beg
&& sal_end
.symtab
== 0)
1453 error (_("No default source file yet. Do \"help list\"."));
1456 source_lines_range
range (sal_end
.line
+ 1,
1457 source_lines_range::BACKWARD
);
1458 print_source_lines (sal_end
.symtab
, range
, 0);
1460 else if (sal
.symtab
== 0)
1461 error (_("No default source file yet. Do \"help list\"."));
1464 for (int i
= 0; i
< sals
.size (); i
++)
1467 int first_line
= sal
.line
- get_lines_to_list () / 2;
1470 if (sals
.size () > 1)
1471 print_sal_location (sal
);
1472 print_source_lines (sal
.symtab
, source_lines_range (first_line
), 0);
1476 print_source_lines (sal
.symtab
, source_lines_range (sal
.line
), 0);
1478 print_source_lines (sal
.symtab
,
1479 source_lines_range (sal
.line
, (sal_end
.line
+ 1)),
1483 /* Subroutine of disassemble_command to simplify it.
1484 Perform the disassembly.
1485 NAME is the name of the function if known, or NULL.
1486 [LOW,HIGH) are the range of addresses to disassemble.
1487 BLOCK is the block to disassemble; it needs to be provided
1488 when non-contiguous blocks are disassembled; otherwise
1490 MIXED is non-zero to print source with the assembler. */
1493 print_disassembly (struct gdbarch
*gdbarch
, const char *name
,
1494 CORE_ADDR low
, CORE_ADDR high
,
1495 const struct block
*block
,
1496 gdb_disassembly_flags flags
)
1499 if (tui_is_window_visible (DISASSEM_WIN
))
1500 tui_show_assembly (gdbarch
, low
);
1504 gdb_printf (_("Dump of assembler code "));
1506 gdb_printf (_("for function %ps:\n"),
1507 styled_string (function_name_style
.style (), name
));
1508 if (block
== nullptr || block
->is_contiguous ())
1511 gdb_printf (_("from %ps to %ps:\n"),
1512 styled_string (address_style
.style (),
1513 paddress (gdbarch
, low
)),
1514 styled_string (address_style
.style (),
1515 paddress (gdbarch
, high
)));
1517 /* Dump the specified range. */
1518 gdb_disassembly (gdbarch
, current_uiout
, flags
, -1, low
, high
);
1522 for (const blockrange
&range
: block
->ranges ())
1524 CORE_ADDR range_low
= range
.start ();
1525 CORE_ADDR range_high
= range
.end ();
1527 gdb_printf (_("Address range %ps to %ps:\n"),
1528 styled_string (address_style
.style (),
1529 paddress (gdbarch
, range_low
)),
1530 styled_string (address_style
.style (),
1531 paddress (gdbarch
, range_high
)));
1532 gdb_disassembly (gdbarch
, current_uiout
, flags
, -1,
1533 range_low
, range_high
);
1536 gdb_printf (_("End of assembler dump.\n"));
1540 /* Subroutine of disassemble_command to simplify it.
1541 Print a disassembly of the current function according to FLAGS. */
1544 disassemble_current_function (gdb_disassembly_flags flags
)
1546 frame_info_ptr frame
;
1547 struct gdbarch
*gdbarch
;
1548 CORE_ADDR low
, high
, pc
;
1550 const struct block
*block
;
1552 frame
= get_selected_frame (_("No frame selected."));
1553 gdbarch
= get_frame_arch (frame
);
1554 pc
= get_frame_address_in_block (frame
);
1555 if (find_pc_partial_function (pc
, &name
, &low
, &high
, &block
) == 0)
1556 error (_("No function contains program counter for selected frame."));
1558 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1561 /* FIXME: cagney/2004-02-07: This should be an observer. */
1562 low
= tui_get_low_disassembly_address (gdbarch
, low
, pc
);
1564 low
+= gdbarch_deprecated_function_start_offset (gdbarch
);
1566 print_disassembly (gdbarch
, name
, low
, high
, block
, flags
);
1569 /* Dump a specified section of assembly code.
1573 - dump the assembly code for the function of the current pc
1574 disassemble [/mrs] addr
1575 - dump the assembly code for the function at ADDR
1576 disassemble [/mrs] low,high
1577 disassemble [/mrs] low,+length
1578 - dump the assembly code in the range [LOW,HIGH), or [LOW,LOW+length)
1580 A /m modifier will include source code with the assembly in a
1581 "source centric" view. This view lists only the file of the first insn,
1582 even if other source files are involved (e.g., inlined functions), and
1583 the output is in source order, even with optimized code. This view is
1584 considered deprecated as it hasn't been useful in practice.
1586 A /r modifier will include raw instructions in hex with the assembly.
1588 A /b modifier is similar to /r except the instruction bytes are printed
1589 as separate bytes with no grouping, or endian switching.
1591 A /s modifier will include source code with the assembly, like /m, with
1592 two important differences:
1593 1) The output is still in pc address order.
1594 2) File names and contents for all relevant source files are displayed. */
1597 disassemble_command (const char *arg
, int from_tty
)
1599 struct gdbarch
*gdbarch
= get_current_arch ();
1600 CORE_ADDR low
, high
;
1601 const general_symbol_info
*symbol
= nullptr;
1604 gdb_disassembly_flags flags
;
1606 const struct block
*block
= nullptr;
1617 error (_("Missing modifier."));
1619 while (*p
&& ! isspace (*p
))
1624 flags
|= DISASSEMBLY_SOURCE_DEPRECATED
;
1627 flags
|= DISASSEMBLY_RAW_INSN
;
1630 flags
|= DISASSEMBLY_RAW_BYTES
;
1633 flags
|= DISASSEMBLY_SOURCE
;
1636 error (_("Invalid disassembly modifier."));
1640 p
= skip_spaces (p
);
1643 if ((flags
& (DISASSEMBLY_SOURCE_DEPRECATED
| DISASSEMBLY_SOURCE
))
1644 == (DISASSEMBLY_SOURCE_DEPRECATED
| DISASSEMBLY_SOURCE
))
1645 error (_("Cannot specify both /m and /s."));
1647 if ((flags
& (DISASSEMBLY_RAW_INSN
| DISASSEMBLY_RAW_BYTES
))
1648 == (DISASSEMBLY_RAW_INSN
| DISASSEMBLY_RAW_BYTES
))
1649 error (_("Cannot specify both /r and /b."));
1653 flags
|= DISASSEMBLY_OMIT_FNAME
;
1654 disassemble_current_function (flags
);
1658 pc
= value_as_address (parse_to_comma_and_eval (&p
));
1664 if (!find_pc_partial_function_sym (pc
, &symbol
, &low
, &high
, &block
))
1665 error (_("No function contains specified address."));
1668 name
= symbol
->print_name ();
1670 name
= symbol
->linkage_name ();
1673 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1676 /* FIXME: cagney/2004-02-07: This should be an observer. */
1677 low
= tui_get_low_disassembly_address (gdbarch
, low
, pc
);
1679 low
+= gdbarch_deprecated_function_start_offset (gdbarch
);
1680 flags
|= DISASSEMBLY_OMIT_FNAME
;
1684 /* Two arguments. */
1687 p
= skip_spaces (p
);
1693 high
= parse_and_eval_address (p
);
1698 print_disassembly (gdbarch
, name
, low
, high
, block
, flags
);
1701 /* Command completion for the disassemble command. */
1704 disassemble_command_completer (struct cmd_list_element
*ignore
,
1705 completion_tracker
&tracker
,
1706 const char *text
, const char * /* word */)
1708 if (skip_over_slash_fmt (tracker
, &text
))
1711 const char *word
= advance_to_expression_complete_word_point (tracker
, text
);
1712 expression_completer (ignore
, tracker
, text
, word
);
1716 make_command (const char *arg
, int from_tty
)
1719 shell_escape ("make", from_tty
);
1722 std::string cmd
= std::string ("make ") + arg
;
1724 shell_escape (cmd
.c_str (), from_tty
);
1729 show_user (const char *args
, int from_tty
)
1731 struct cmd_list_element
*c
;
1735 const char *comname
= args
;
1737 c
= lookup_cmd (&comname
, cmdlist
, "", NULL
, 0, 1);
1738 if (!cli_user_command_p (c
))
1739 error (_("Not a user command."));
1740 show_user_1 (c
, "", args
, gdb_stdout
);
1744 for (c
= cmdlist
; c
; c
= c
->next
)
1746 if (cli_user_command_p (c
) || c
->is_prefix ())
1747 show_user_1 (c
, "", c
->name
, gdb_stdout
);
1752 /* Return true if COMMAND or any of its sub-commands is a user defined command.
1753 This is a helper function for show_user_completer. */
1756 has_user_subcmd (struct cmd_list_element
*command
)
1758 if (cli_user_command_p (command
))
1761 /* Alias command can yield false positive. Ignore them as the targeted
1762 command should be reachable anyway. */
1763 if (command
->is_alias ())
1766 if (command
->is_prefix ())
1767 for (struct cmd_list_element
*subcommand
= *command
->subcommands
;
1768 subcommand
!= nullptr;
1769 subcommand
= subcommand
->next
)
1770 if (has_user_subcmd (subcommand
))
1776 /* Implement completer for the 'show user' command. */
1779 show_user_completer (cmd_list_element
*,
1780 completion_tracker
&tracker
, const char *text
,
1783 struct cmd_list_element
*cmd_group
= cmdlist
;
1785 /* TEXT can contain a chain of commands and subcommands. Follow the
1786 commands chain until we reach the point where the user wants a
1790 const char *curr_cmd
= text
;
1791 const char *after
= skip_to_space (text
);
1792 const size_t curr_cmd_len
= after
- text
;
1793 text
= skip_spaces (after
);
1795 for (struct cmd_list_element
*c
= cmd_group
; c
!= nullptr; c
= c
->next
)
1797 if (strlen (c
->name
) == curr_cmd_len
1798 && strncmp (c
->name
, curr_cmd
, curr_cmd_len
) == 0)
1800 if (c
->subcommands
== nullptr)
1801 /* We arrived after a command with no child, so nothing more
1805 cmd_group
= *c
->subcommands
;
1811 const int wordlen
= strlen (word
);
1812 for (struct cmd_list_element
*c
= cmd_group
; c
!= nullptr; c
= c
->next
)
1813 if (has_user_subcmd (c
))
1815 if (strncmp (c
->name
, word
, wordlen
) == 0)
1816 tracker
.add_completion
1817 (gdb::unique_xmalloc_ptr
<char> (xstrdup (c
->name
)));
1821 /* Search through names of commands and documentations for a certain
1822 regular expression. */
1825 apropos_command (const char *arg
, int from_tty
)
1827 bool verbose
= arg
&& check_for_argument (&arg
, "-v", 2);
1829 if (arg
== NULL
|| *arg
== '\0')
1830 error (_("REGEXP string is empty"));
1832 compiled_regex
pattern (arg
, REG_ICASE
,
1833 _("Error in regular expression"));
1835 apropos_cmd (gdb_stdout
, cmdlist
, verbose
, pattern
);
1838 /* The options for the "alias" command. */
1843 bool abbrev_flag
= false;
1846 static const gdb::option::option_def alias_option_defs
[] = {
1848 gdb::option::flag_option_def
<alias_opts
> {
1850 [] (alias_opts
*opts
) { return &opts
->abbrev_flag
; },
1851 N_("Specify that ALIAS is an abbreviation of COMMAND.\n\
1852 Abbreviations are not used in command completion."),
1857 /* Create an option_def_group for the "alias" options, with
1858 A_OPTS as context. */
1860 static gdb::option::option_def_group
1861 make_alias_options_def_group (alias_opts
*a_opts
)
1863 return {{alias_option_defs
}, a_opts
};
1866 /* Completer for the "alias_command". */
1869 alias_command_completer (struct cmd_list_element
*ignore
,
1870 completion_tracker
&tracker
,
1871 const char *text
, const char *word
)
1873 const auto grp
= make_alias_options_def_group (nullptr);
1875 tracker
.set_use_custom_word_point (true);
1877 if (gdb::option::complete_options
1878 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
))
1881 const char *delim
= strchr (text
, '=');
1883 /* If we're past the "=" delimiter, complete the
1884 "alias ALIAS = COMMAND [DEFAULT-ARGS...]" as if the user is
1885 typing COMMAND DEFAULT-ARGS... */
1888 && isspace (delim
[-1])
1889 && (isspace (delim
[1]) || delim
[1] == '\0'))
1891 std::string new_text
= std::string (delim
+ 1);
1893 tracker
.advance_custom_word_point_by (delim
+ 1 - text
);
1894 complete_nested_command_line (tracker
, new_text
.c_str ());
1898 /* We're not yet past the "=" delimiter. Complete a command, as
1899 the user might type an alias following a prefix command. */
1900 complete_nested_command_line (tracker
, text
);
1903 /* Subroutine of alias_command to simplify it.
1904 Return the first N elements of ARGV flattened back to a string
1905 with a space separating each element.
1906 ARGV may not be NULL.
1907 This does not take care of quoting elements in case they contain spaces
1911 argv_to_string (char **argv
, int n
)
1916 gdb_assert (argv
!= NULL
);
1917 gdb_assert (n
>= 0 && n
<= countargv (argv
));
1919 for (i
= 0; i
< n
; ++i
)
1929 /* Subroutine of alias_command to simplify it.
1930 Verifies that COMMAND can have an alias:
1932 COMMAND must not have default args.
1933 This last condition is to avoid the following:
1934 alias aaa = backtrace -full
1935 alias bbb = aaa -past-main
1936 as (at least currently), alias default args are not cumulative
1937 and the user would expect bbb to execute 'backtrace -full -past-main'
1938 while it will execute 'backtrace -past-main'. */
1940 static cmd_list_element
*
1941 validate_aliased_command (const char *command
)
1943 std::string default_args
;
1945 = lookup_cmd_1 (& command
, cmdlist
, NULL
, &default_args
, 1);
1947 if (c
== NULL
|| c
== (struct cmd_list_element
*) -1)
1948 error (_("Invalid command to alias to: %s"), command
);
1950 if (!default_args
.empty ())
1951 error (_("Cannot define an alias of an alias that has default args"));
1956 /* Called when "alias" was incorrectly used. */
1959 alias_usage_error (void)
1961 error (_("Usage: alias [-a] [--] ALIAS = COMMAND [DEFAULT-ARGS...]"));
1964 /* Make an alias of an existing command. */
1967 alias_command (const char *args
, int from_tty
)
1971 auto grp
= make_alias_options_def_group (&a_opts
);
1972 gdb::option::process_options
1973 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, grp
);
1975 int i
, alias_argc
, command_argc
;
1977 const char *alias
, *command
;
1979 if (args
== NULL
|| strchr (args
, '=') == NULL
)
1980 alias_usage_error ();
1982 equals
= strchr (args
, '=');
1983 std::string
args2 (args
, equals
- args
);
1985 gdb_argv
built_alias_argv (args2
.c_str ());
1987 const char *default_args
= equals
+ 1;
1988 struct cmd_list_element
*c_command_prefix
;
1990 lookup_cmd_for_default_args (&default_args
, &c_command_prefix
);
1991 std::string
command_argv_str (equals
+ 1,
1992 default_args
== nullptr
1993 ? strlen (equals
+ 1)
1994 : default_args
- equals
- 1);
1995 gdb_argv
command_argv (command_argv_str
.c_str ());
1997 char **alias_argv
= built_alias_argv
.get ();
1999 if (alias_argv
[0] == NULL
|| command_argv
[0] == NULL
2000 || *alias_argv
[0] == '\0' || *command_argv
[0] == '\0')
2001 alias_usage_error ();
2003 for (i
= 0; alias_argv
[i
] != NULL
; ++i
)
2005 if (! valid_user_defined_cmd_name_p (alias_argv
[i
]))
2008 error (_("Invalid command name: %s"), alias_argv
[i
]);
2010 error (_("Invalid command element name: %s"), alias_argv
[i
]);
2014 alias_argc
= countargv (alias_argv
);
2015 command_argc
= command_argv
.count ();
2017 /* COMMAND must exist, and cannot have default args.
2018 Reconstruct the command to remove any extraneous spaces,
2019 for better error messages. */
2020 std::string
command_string (argv_to_string (command_argv
.get (),
2022 command
= command_string
.c_str ();
2023 cmd_list_element
*target_cmd
= validate_aliased_command (command
);
2025 /* ALIAS must not exist. */
2026 std::string
alias_string (argv_to_string (alias_argv
, alias_argc
));
2027 alias
= alias_string
.c_str ();
2029 cmd_list_element
*alias_cmd
, *prefix_cmd
, *cmd
;
2031 if (lookup_cmd_composition (alias
, &alias_cmd
, &prefix_cmd
, &cmd
))
2033 const char *alias_name
= alias_argv
[alias_argc
-1];
2035 /* If we found an existing ALIAS_CMD, check that the prefix differ or
2038 if (alias_cmd
!= nullptr
2039 && alias_cmd
->prefix
== prefix_cmd
2040 && strcmp (alias_name
, alias_cmd
->name
) == 0)
2041 error (_("Alias already exists: %s"), alias
);
2043 /* Check ALIAS differs from the found CMD. */
2045 if (cmd
->prefix
== prefix_cmd
2046 && strcmp (alias_name
, cmd
->name
) == 0)
2047 error (_("Alias %s is the name of an existing command"), alias
);
2052 struct cmd_list_element
*alias_cmd
;
2054 /* If ALIAS is one word, it is an alias for the entire COMMAND.
2055 Example: alias spe = set print elements
2057 Otherwise ALIAS and COMMAND must have the same number of words,
2058 and every word except the last must identify the same prefix command;
2059 and the last word of ALIAS is made an alias of the last word of COMMAND.
2060 Example: alias set print elms = set pr elem
2061 Note that unambiguous abbreviations are allowed. */
2063 if (alias_argc
== 1)
2065 /* add_cmd requires *we* allocate space for name, hence the xstrdup. */
2066 alias_cmd
= add_com_alias (xstrdup (alias_argv
[0]), target_cmd
,
2067 class_alias
, a_opts
.abbrev_flag
);
2071 const char *alias_prefix
, *command_prefix
;
2072 struct cmd_list_element
*c_alias
, *c_command
;
2074 if (alias_argc
!= command_argc
)
2075 error (_("Mismatched command length between ALIAS and COMMAND."));
2077 /* Create copies of ALIAS and COMMAND without the last word,
2078 and use that to verify the leading elements give the same
2080 std::string
alias_prefix_string (argv_to_string (alias_argv
,
2082 std::string
command_prefix_string (argv_to_string (command_argv
.get (),
2084 alias_prefix
= alias_prefix_string
.c_str ();
2085 command_prefix
= command_prefix_string
.c_str ();
2087 c_command
= lookup_cmd_1 (& command_prefix
, cmdlist
, NULL
, NULL
, 1);
2088 /* We've already tried to look up COMMAND. */
2089 gdb_assert (c_command
!= NULL
2090 && c_command
!= (struct cmd_list_element
*) -1);
2091 gdb_assert (c_command
->is_prefix ());
2092 c_alias
= lookup_cmd_1 (& alias_prefix
, cmdlist
, NULL
, NULL
, 1);
2093 if (c_alias
!= c_command
)
2094 error (_("ALIAS and COMMAND prefixes do not match."));
2096 /* add_cmd requires *we* allocate space for name, hence the xstrdup. */
2097 alias_cmd
= add_alias_cmd (xstrdup (alias_argv
[alias_argc
- 1]),
2098 target_cmd
, class_alias
, a_opts
.abbrev_flag
,
2099 c_command
->subcommands
);
2102 gdb_assert (alias_cmd
!= nullptr);
2103 gdb_assert (alias_cmd
->default_args
.empty ());
2104 if (default_args
!= nullptr)
2106 default_args
= skip_spaces (default_args
);
2108 alias_cmd
->default_args
= default_args
;
2112 /* Print the file / line number / symbol name of the location
2113 specified by SAL. */
2116 print_sal_location (const symtab_and_line
&sal
)
2118 scoped_restore_current_program_space restore_pspace
;
2119 set_current_program_space (sal
.pspace
);
2121 const char *sym_name
= NULL
;
2122 if (sal
.symbol
!= NULL
)
2123 sym_name
= sal
.symbol
->print_name ();
2124 gdb_printf (_("file: \"%s\", line number: %d, symbol: \"%s\"\n"),
2125 symtab_to_filename_for_display (sal
.symtab
),
2126 sal
.line
, sym_name
!= NULL
? sym_name
: "???");
2129 /* Print a list of files and line numbers which a user may choose from
2130 in order to list a function which was specified ambiguously (as
2131 with `list classname::overloadedfuncname', for example). The SALS
2132 array provides the filenames and line numbers. FORMAT is a
2133 printf-style format string used to tell the user what was
2137 ambiguous_line_spec (gdb::array_view
<const symtab_and_line
> sals
,
2138 const char *format
, ...)
2141 va_start (ap
, format
);
2142 gdb_vprintf (format
, ap
);
2145 for (const auto &sal
: sals
)
2146 print_sal_location (sal
);
2149 /* Comparison function for filter_sals. Returns a qsort-style
2153 cmp_symtabs (const symtab_and_line
&sala
, const symtab_and_line
&salb
)
2155 const char *dira
= sala
.symtab
->compunit ()->dirname ();
2156 const char *dirb
= salb
.symtab
->compunit ()->dirname ();
2164 else if (dirb
== NULL
)
2171 r
= filename_cmp (dira
, dirb
);
2176 r
= filename_cmp (sala
.symtab
->filename
, salb
.symtab
->filename
);
2180 if (sala
.line
< salb
.line
)
2182 return sala
.line
== salb
.line
? 0 : 1;
2185 /* Remove any SALs that do not match the current program space, or
2186 which appear to be "file:line" duplicates. */
2189 filter_sals (std::vector
<symtab_and_line
> &sals
)
2191 /* Remove SALs that do not match. */
2192 auto from
= std::remove_if (sals
.begin (), sals
.end (),
2193 [&] (const symtab_and_line
&sal
)
2194 { return (sal
.pspace
!= current_program_space
|| sal
.symtab
== NULL
); });
2197 std::sort (sals
.begin (), from
,
2198 [] (const symtab_and_line
&sala
, const symtab_and_line
&salb
)
2199 { return cmp_symtabs (sala
, salb
) < 0; });
2201 from
= std::unique (sals
.begin (), from
,
2202 [&] (const symtab_and_line
&sala
,
2203 const symtab_and_line
&salb
)
2204 { return cmp_symtabs (sala
, salb
) == 0; });
2206 sals
.erase (from
, sals
.end ());
2210 show_info_verbose (struct ui_file
*file
, int from_tty
,
2211 struct cmd_list_element
*c
,
2216 _("Verbose printing of informational messages is %s.\n"),
2219 gdb_printf (file
, _("Verbosity is %s.\n"), value
);
2223 show_history_expansion_p (struct ui_file
*file
, int from_tty
,
2224 struct cmd_list_element
*c
, const char *value
)
2226 gdb_printf (file
, _("History expansion on command input is %s.\n"),
2231 show_max_user_call_depth (struct ui_file
*file
, int from_tty
,
2232 struct cmd_list_element
*c
, const char *value
)
2235 _("The max call depth for user-defined commands is %s.\n"),
2239 /* Implement 'show suppress-cli-notifications'. */
2242 show_suppress_cli_notifications (ui_file
*file
, int from_tty
,
2243 cmd_list_element
*c
, const char *value
)
2245 gdb_printf (file
, _("Suppression of printing CLI notifications "
2246 "is %s.\n"), value
);
2249 /* Implement 'set suppress-cli-notifications'. */
2252 set_suppress_cli_notifications (const char *args
, int from_tty
,
2253 cmd_list_element
*c
)
2255 cli_suppress_notification
.user_selected_context
2256 = user_wants_cli_suppress_notification
;
2257 cli_suppress_notification
.normal_stop
2258 = user_wants_cli_suppress_notification
;
2261 /* Returns the cmd_list_element in SHOWLIST corresponding to the first
2262 argument of ARGV, which must contain one single value.
2263 Throws an error if no value provided, or value not correct.
2264 FNNAME is used in the error message. */
2266 static cmd_list_element
*
2267 setting_cmd (const char *fnname
, struct cmd_list_element
*showlist
,
2268 int argc
, struct value
**argv
)
2271 error (_("You must provide an argument to %s"), fnname
);
2273 error (_("You can only provide one argument to %s"), fnname
);
2275 struct type
*type0
= check_typedef (argv
[0]->type ());
2277 if (type0
->code () != TYPE_CODE_ARRAY
2278 && type0
->code () != TYPE_CODE_STRING
)
2279 error (_("First argument of %s must be a string."), fnname
);
2281 /* Not all languages null-terminate their strings, by moving the string
2282 content into a std::string we ensure that a null-terminator is added.
2283 For languages that do add a null-terminator the std::string might end
2284 up with two null characters at the end, but that's harmless. */
2285 const std::string
setting ((const char *) argv
[0]->contents ().data (),
2287 const char *a0
= setting
.c_str ();
2288 cmd_list_element
*cmd
= lookup_cmd (&a0
, showlist
, "", NULL
, -1, 0);
2290 if (cmd
== nullptr || cmd
->type
!= show_cmd
)
2292 gdb_assert (showlist
->prefix
!= nullptr);
2293 std::vector
<std::string
> components
2294 = showlist
->prefix
->command_components ();
2295 std::string full_name
= components
[0];
2296 for (int i
= 1; i
< components
.size (); ++i
)
2297 full_name
+= " " + components
[i
];
2298 error (_("First argument of %s must be a valid setting of the "
2299 "'%s' command."), fnname
, full_name
.c_str ());
2305 /* Builds a value from the show CMD. */
2307 static struct value
*
2308 value_from_setting (const setting
&var
, struct gdbarch
*gdbarch
)
2310 switch (var
.type ())
2317 = (var
.type () == var_uinteger
2318 ? static_cast<LONGEST
> (var
.get
<unsigned int> ())
2319 : static_cast<LONGEST
> (var
.get
<int> ()));
2321 if (var
.extra_literals () != nullptr)
2322 for (const literal_def
*l
= var
.extra_literals ();
2323 l
->literal
!= nullptr;
2325 if (value
== l
->use
)
2327 if (l
->val
.has_value ())
2330 return value::allocate (builtin_type (gdbarch
)->builtin_void
);
2334 if (var
.type () == var_uinteger
)
2336 value_from_ulongest (builtin_type (gdbarch
)->builtin_unsigned_int
,
2337 static_cast<unsigned int> (value
));
2340 value_from_longest (builtin_type (gdbarch
)->builtin_int
,
2341 static_cast<int> (value
));
2344 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
2345 var
.get
<bool> () ? 1 : 0);
2346 case var_auto_boolean
:
2350 switch (var
.get
<enum auto_boolean
> ())
2352 case AUTO_BOOLEAN_TRUE
:
2355 case AUTO_BOOLEAN_FALSE
:
2358 case AUTO_BOOLEAN_AUTO
:
2362 gdb_assert_not_reached ("invalid var_auto_boolean");
2364 return value_from_longest (builtin_type (gdbarch
)->builtin_int
,
2368 case var_string_noescape
:
2369 case var_optional_filename
:
2375 if (var
.type () == var_enum
)
2377 value
= var
.get
<const char *> ();
2378 len
= strlen (value
);
2382 const std::string
&st
= var
.get
<std::string
> ();
2383 value
= st
.c_str ();
2387 return current_language
->value_string (gdbarch
, value
, len
);
2390 gdb_assert_not_reached ("bad var_type");
2394 /* Implementation of the convenience function $_gdb_setting. */
2396 static struct value
*
2397 gdb_setting_internal_fn (struct gdbarch
*gdbarch
,
2398 const struct language_defn
*language
,
2399 void *cookie
, int argc
, struct value
**argv
)
2401 cmd_list_element
*show_cmd
2402 = setting_cmd ("$_gdb_setting", showlist
, argc
, argv
);
2404 gdb_assert (show_cmd
->var
.has_value ());
2406 return value_from_setting (*show_cmd
->var
, gdbarch
);
2409 /* Implementation of the convenience function $_gdb_maint_setting. */
2411 static struct value
*
2412 gdb_maint_setting_internal_fn (struct gdbarch
*gdbarch
,
2413 const struct language_defn
*language
,
2414 void *cookie
, int argc
, struct value
**argv
)
2416 cmd_list_element
*show_cmd
2417 = setting_cmd ("$_gdb_maint_setting", maintenance_show_cmdlist
, argc
, argv
);
2419 gdb_assert (show_cmd
->var
.has_value ());
2421 return value_from_setting (*show_cmd
->var
, gdbarch
);
2424 /* Builds a string value from the show CMD. */
2426 static struct value
*
2427 str_value_from_setting (const setting
&var
, struct gdbarch
*gdbarch
)
2429 switch (var
.type ())
2435 case var_auto_boolean
:
2437 std::string cmd_val
= get_setshow_command_value_string (var
);
2439 return current_language
->value_string (gdbarch
, cmd_val
.c_str (),
2444 case var_string_noescape
:
2445 case var_optional_filename
:
2448 /* For these cases, we do not use get_setshow_command_value_string,
2449 as this function handle some characters specially, e.g. by
2450 escaping quotevar. So, we directly use the var string value,
2451 similarly to the value_from_setting code for these casevar. */
2455 if (var
.type () == var_enum
)
2457 value
= var
.get
<const char *> ();
2458 len
= strlen (value
);
2462 const std::string
&st
= var
.get
<std::string
> ();
2463 value
= st
.c_str ();
2467 return current_language
->value_string (gdbarch
, value
, len
);
2470 gdb_assert_not_reached ("bad var_type");
2474 /* Implementation of the convenience function $_gdb_setting_str. */
2476 static struct value
*
2477 gdb_setting_str_internal_fn (struct gdbarch
*gdbarch
,
2478 const struct language_defn
*language
,
2479 void *cookie
, int argc
, struct value
**argv
)
2481 cmd_list_element
*show_cmd
2482 = setting_cmd ("$_gdb_setting_str", showlist
, argc
, argv
);
2484 gdb_assert (show_cmd
->var
.has_value ());
2486 return str_value_from_setting (*show_cmd
->var
, gdbarch
);
2490 /* Implementation of the convenience function $_gdb_maint_setting_str. */
2492 static struct value
*
2493 gdb_maint_setting_str_internal_fn (struct gdbarch
*gdbarch
,
2494 const struct language_defn
*language
,
2495 void *cookie
, int argc
, struct value
**argv
)
2497 cmd_list_element
*show_cmd
2498 = setting_cmd ("$_gdb_maint_setting_str", maintenance_show_cmdlist
, argc
,
2501 gdb_assert (show_cmd
->var
.has_value ());
2503 return str_value_from_setting (*show_cmd
->var
, gdbarch
);
2506 /* Implementation of the convenience function $_shell. */
2508 static struct value
*
2509 shell_internal_fn (struct gdbarch
*gdbarch
,
2510 const struct language_defn
*language
,
2511 void *cookie
, int argc
, struct value
**argv
)
2514 error (_("You must provide one argument for $_shell."));
2516 value
*val
= argv
[0];
2517 struct type
*type
= check_typedef (val
->type ());
2519 if (!language
->is_string_type_p (type
))
2520 error (_("Argument must be a string."));
2522 value_print_options opts
;
2523 get_no_prettyformat_print_options (&opts
);
2526 value_print (val
, &stream
, &opts
);
2528 /* We should always have two quote chars, which we'll strip. */
2529 gdb_assert (stream
.size () >= 2);
2531 /* Now strip them. We don't need the original string, so it's
2532 cheaper to do it in place, avoiding a string allocation. */
2533 std::string str
= stream
.release ();
2534 str
[str
.size () - 1] = 0;
2535 const char *command
= str
.c_str () + 1;
2537 int exit_status
= run_under_shell (command
, 0);
2539 struct type
*int_type
= builtin_type (gdbarch
)->builtin_int
;
2541 /* Keep the logic here in sync with
2542 exit_status_set_internal_vars. */
2544 if (WIFEXITED (exit_status
))
2545 return value_from_longest (int_type
, WEXITSTATUS (exit_status
));
2547 else if (WIFSIGNALED (exit_status
) && WTERMSIG (exit_status
) == -1)
2549 /* See exit_status_set_internal_vars. */
2550 return value_from_longest (int_type
, exit_status
);
2553 else if (WIFSIGNALED (exit_status
))
2555 /* (0x80 | SIGNO) is what most (all?) POSIX-like shells set as
2556 exit code on fatal signal termination. */
2557 return value_from_longest (int_type
, 0x80 | WTERMSIG (exit_status
));
2560 return value::allocate_optimized_out (int_type
);
2563 void _initialize_cli_cmds ();
2565 _initialize_cli_cmds ()
2567 struct cmd_list_element
*c
;
2569 /* Define the classes of commands.
2570 They will appear in the help list in alphabetical order. */
2572 add_cmd ("internals", class_maintenance
, _("\
2573 Maintenance commands.\n\
2574 Some gdb commands are provided just for use by gdb maintainers.\n\
2575 These commands are subject to frequent change, and may not be as\n\
2576 well documented as user commands."),
2578 add_cmd ("obscure", class_obscure
, _("Obscure features."), &cmdlist
);
2579 add_cmd ("aliases", class_alias
,
2580 _("User-defined aliases of other commands."), &cmdlist
);
2581 add_cmd ("user-defined", class_user
, _("\
2582 User-defined commands.\n\
2583 The commands in this class are those defined by the user.\n\
2584 Use the \"define\" command to define a command."), &cmdlist
);
2585 add_cmd ("support", class_support
, _("Support facilities."), &cmdlist
);
2586 add_cmd ("status", class_info
, _("Status inquiries."), &cmdlist
);
2587 add_cmd ("files", class_files
, _("Specifying and examining files."),
2589 add_cmd ("breakpoints", class_breakpoint
,
2590 _("Making program stop at certain points."), &cmdlist
);
2591 add_cmd ("data", class_vars
, _("Examining data."), &cmdlist
);
2592 add_cmd ("stack", class_stack
, _("\
2593 Examining the stack.\n\
2594 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2595 counting from zero for the innermost (currently executing) frame.\n\n\
2596 At any time gdb identifies one frame as the \"selected\" frame.\n\
2597 Variable lookups are done with respect to the selected frame.\n\
2598 When the program being debugged stops, gdb selects the innermost frame.\n\
2599 The commands below can be used to select other frames by number or address."),
2602 add_cmd ("text-user-interface", class_tui
,
2603 _("TUI is the GDB text based interface.\n\
2604 In TUI mode, GDB can display several text windows showing\n\
2605 the source file, the processor registers, the program disassembly, ..."), &cmdlist
);
2607 add_cmd ("running", class_run
, _("Running the program."), &cmdlist
);
2609 /* Define general commands. */
2611 add_com ("pwd", class_files
, pwd_command
, _("\
2612 Print working directory.\n\
2613 This is used for your program as well."));
2615 c
= add_cmd ("cd", class_files
, cd_command
, _("\
2616 Set working directory to DIR for debugger.\n\
2617 The debugger's current working directory specifies where scripts and other\n\
2618 files that can be loaded by GDB are located.\n\
2619 In order to change the inferior's current working directory, the recommended\n\
2620 way is to use the \"set cwd\" command."), &cmdlist
);
2621 set_cmd_completer (c
, filename_completer
);
2623 add_com ("echo", class_support
, echo_command
, _("\
2624 Print a constant string. Give string as argument.\n\
2625 C escape sequences may be used in the argument.\n\
2626 No newline is added at the end of the argument;\n\
2627 use \"\\n\" if you want a newline to be printed.\n\
2628 Since leading and trailing whitespace are ignored in command arguments,\n\
2629 if you want to print some you must use \"\\\" before leading whitespace\n\
2630 to be printed or after trailing whitespace."));
2632 add_setshow_enum_cmd ("script-extension", class_support
,
2633 script_ext_enums
, &script_ext_mode
, _("\
2634 Set mode for script filename extension recognition."), _("\
2635 Show mode for script filename extension recognition."), _("\
2636 off == no filename extension recognition (all sourced files are GDB scripts)\n\
2637 soft == evaluate script according to filename extension, fallback to GDB script"
2639 strict == evaluate script according to filename extension, error if not supported"
2642 show_script_ext_mode
,
2643 &setlist
, &showlist
);
2645 cmd_list_element
*quit_cmd
2646 = add_com ("quit", class_support
, quit_command
, _("\
2648 Usage: quit [EXPR] or exit [EXPR]\n\
2649 The optional expression EXPR, if present, is evaluated and the result\n\
2650 used as GDB's exit code. The default is zero."));
2651 cmd_list_element
*help_cmd
2652 = add_com ("help", class_support
, help_command
,
2653 _("Print list of commands."));
2654 set_cmd_completer (help_cmd
, command_completer
);
2655 add_com_alias ("q", quit_cmd
, class_support
, 1);
2656 add_com_alias ("exit", quit_cmd
, class_support
, 1);
2657 add_com_alias ("h", help_cmd
, class_support
, 1);
2659 add_setshow_boolean_cmd ("verbose", class_support
, &info_verbose
, _("\
2660 Set verbosity."), _("\
2661 Show verbosity."), NULL
,
2664 &setlist
, &showlist
);
2666 add_setshow_prefix_cmd
2667 ("history", class_support
,
2668 _("Generic command for setting command history parameters."),
2669 _("Generic command for showing command history parameters."),
2670 &sethistlist
, &showhistlist
, &setlist
, &showlist
);
2672 add_setshow_boolean_cmd ("expansion", no_class
, &history_expansion_p
, _("\
2673 Set history expansion on command input."), _("\
2674 Show history expansion on command input."), _("\
2675 Without an argument, history expansion is enabled."),
2677 show_history_expansion_p
,
2678 &sethistlist
, &showhistlist
);
2680 cmd_list_element
*info_cmd
2681 = add_prefix_cmd ("info", class_info
, info_command
, _("\
2682 Generic command for showing things about the program being debugged."),
2683 &infolist
, 0, &cmdlist
);
2684 add_com_alias ("i", info_cmd
, class_info
, 1);
2685 add_com_alias ("inf", info_cmd
, class_info
, 1);
2687 add_com ("complete", class_obscure
, complete_command
,
2688 _("List the completions for the rest of the line as a command."));
2690 c
= add_show_prefix_cmd ("show", class_info
, _("\
2691 Generic command for showing things about the debugger."),
2692 &showlist
, 0, &cmdlist
);
2693 /* Another way to get at the same thing. */
2694 add_alias_cmd ("set", c
, class_info
, 0, &infolist
);
2696 cmd_list_element
*with_cmd
2697 = add_com ("with", class_vars
, with_command
, _("\
2698 Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.\n\
2699 Usage: with SETTING [VALUE] [-- COMMAND]\n\
2700 Usage: w SETTING [VALUE] [-- COMMAND]\n\
2701 With no COMMAND, repeats the last executed command.\n\
2703 SETTING is any setting you can change with the \"set\" subcommands.\n\
2705 with language pascal -- print obj\n\
2706 with print elements unlimited -- print obj\n\
2708 You can change multiple settings using nested with, and use\n\
2709 abbreviations for commands and/or values. E.g.:\n\
2710 w la p -- w p el u -- p obj"));
2711 set_cmd_completer_handle_brkchars (with_cmd
, with_command_completer
);
2712 add_com_alias ("w", with_cmd
, class_vars
, 1);
2714 add_internal_function ("_gdb_setting_str", _("\
2715 $_gdb_setting_str - returns the value of a GDB setting as a string.\n\
2716 Usage: $_gdb_setting_str (setting)\n\
2718 auto-boolean values are \"off\", \"on\", \"auto\".\n\
2719 boolean values are \"off\", \"on\".\n\
2720 Some integer settings accept an unlimited value, returned\n\
2721 as \"unlimited\"."),
2722 gdb_setting_str_internal_fn
, NULL
);
2724 add_internal_function ("_gdb_setting", _("\
2725 $_gdb_setting - returns the value of a GDB setting.\n\
2726 Usage: $_gdb_setting (setting)\n\
2727 auto-boolean values are \"off\", \"on\", \"auto\".\n\
2728 boolean values are \"off\", \"on\".\n\
2729 Some integer settings accept an unlimited value, returned\n\
2730 as 0 or -1 depending on the setting."),
2731 gdb_setting_internal_fn
, NULL
);
2733 add_internal_function ("_gdb_maint_setting_str", _("\
2734 $_gdb_maint_setting_str - returns the value of a GDB maintenance setting as a string.\n\
2735 Usage: $_gdb_maint_setting_str (setting)\n\
2737 auto-boolean values are \"off\", \"on\", \"auto\".\n\
2738 boolean values are \"off\", \"on\".\n\
2739 Some integer settings accept an unlimited value, returned\n\
2740 as \"unlimited\"."),
2741 gdb_maint_setting_str_internal_fn
, NULL
);
2743 add_internal_function ("_gdb_maint_setting", _("\
2744 $_gdb_maint_setting - returns the value of a GDB maintenance setting.\n\
2745 Usage: $_gdb_maint_setting (setting)\n\
2746 auto-boolean values are \"off\", \"on\", \"auto\".\n\
2747 boolean values are \"off\", \"on\".\n\
2748 Some integer settings accept an unlimited value, returned\n\
2749 as 0 or -1 depending on the setting."),
2750 gdb_maint_setting_internal_fn
, NULL
);
2752 add_internal_function ("_shell", _("\
2753 $_shell - execute a shell command and return the result.\n\
2755 Usage: $_shell (COMMAND)\n\
2759 COMMAND: The command to execute. Must be a string.\n\
2762 The command's exit code: zero on success, non-zero otherwise."),
2763 shell_internal_fn
, NULL
);
2765 add_cmd ("commands", no_set_class
, show_commands
, _("\
2766 Show the history of commands you typed.\n\
2767 You can supply a command number to start with, or a `+' to start after\n\
2768 the previous command number shown."),
2771 add_cmd ("version", no_set_class
, show_version
,
2772 _("Show what version of GDB this is."), &showlist
);
2774 add_cmd ("configuration", no_set_class
, show_configuration
,
2775 _("Show how GDB was configured at build time."), &showlist
);
2777 add_setshow_prefix_cmd ("debug", no_class
,
2778 _("Generic command for setting gdb debugging flags."),
2779 _("Generic command for showing gdb debugging flags."),
2780 &setdebuglist
, &showdebuglist
,
2781 &setlist
, &showlist
);
2783 cmd_list_element
*shell_cmd
2784 = add_com ("shell", class_support
, shell_command
, _("\
2785 Execute the rest of the line as a shell command.\n\
2786 With no arguments, run an inferior shell."));
2787 set_cmd_completer (shell_cmd
, filename_completer
);
2789 add_com_alias ("!", shell_cmd
, class_support
, 0);
2791 c
= add_com ("edit", class_files
, edit_command
, _("\
2792 Edit specified file or function.\n\
2793 With no argument, edits file containing most recent line listed.\n\
2794 Editing targets can be specified in these ways:\n\
2795 FILE:LINENUM, to edit at that line in that file,\n\
2796 FUNCTION, to edit at the beginning of that function,\n\
2797 FILE:FUNCTION, to distinguish among like-named static functions.\n\
2798 *ADDRESS, to edit at the line containing that address.\n\
2799 Uses EDITOR environment variable contents as editor (or ex as default)."));
2801 c
->completer
= location_completer
;
2803 cmd_list_element
*pipe_cmd
2804 = add_com ("pipe", class_support
, pipe_command
, _("\
2805 Send the output of a gdb command to a shell command.\n\
2806 Usage: | [COMMAND] | SHELL_COMMAND\n\
2807 Usage: | -d DELIM COMMAND DELIM SHELL_COMMAND\n\
2808 Usage: pipe [COMMAND] | SHELL_COMMAND\n\
2809 Usage: pipe -d DELIM COMMAND DELIM SHELL_COMMAND\n\
2811 Executes COMMAND and sends its output to SHELL_COMMAND.\n\
2813 The -d option indicates to use the string DELIM to separate COMMAND\n\
2814 from SHELL_COMMAND, in alternative to |. This is useful in\n\
2815 case COMMAND contains a | character.\n\
2817 With no COMMAND, repeat the last executed command\n\
2818 and send its output to SHELL_COMMAND."));
2819 set_cmd_completer_handle_brkchars (pipe_cmd
, pipe_command_completer
);
2820 add_com_alias ("|", pipe_cmd
, class_support
, 0);
2822 cmd_list_element
*list_cmd
2823 = add_com ("list", class_files
, list_command
, _("\
2824 List specified function or line.\n\
2825 With no argument, lists ten more lines after or around previous listing.\n\
2826 \"list +\" lists the ten lines following a previous ten-line listing.\n\
2827 \"list -\" lists the ten lines before a previous ten-line listing.\n\
2828 \"list .\" lists ten lines around the point of execution in the current frame.\n\
2829 One argument specifies a line, and ten lines are listed around that line.\n\
2830 Two arguments with comma between specify starting and ending lines to list.\n\
2831 Lines can be specified in these ways:\n\
2832 LINENUM, to list around that line in current file,\n\
2833 FILE:LINENUM, to list around that line in that file,\n\
2834 FUNCTION, to list around beginning of that function,\n\
2835 FILE:FUNCTION, to distinguish among like-named static functions.\n\
2836 *ADDRESS, to list around the line containing that address.\n\
2837 With two args, if one is empty, it stands for ten lines away from\n\
2840 By default, when a single location is given, display ten lines.\n\
2841 This can be changed using \"set listsize\", and the current value\n\
2842 can be shown using \"show listsize\"."));
2844 add_com_alias ("l", list_cmd
, class_files
, 1);
2846 c
= add_com ("disassemble", class_vars
, disassemble_command
, _("\
2847 Disassemble a specified section of memory.\n\
2848 Usage: disassemble[/m|/r|/s] START [, END]\n\
2849 Default is the function surrounding the pc of the selected frame.\n\
2851 With a /s modifier, source lines are included (if available).\n\
2852 In this mode, the output is displayed in PC address order, and\n\
2853 file names and contents for all relevant source files are displayed.\n\
2855 With a /m modifier, source lines are included (if available).\n\
2856 This view is \"source centric\": the output is in source line order,\n\
2857 regardless of any optimization that is present. Only the main source file\n\
2858 is displayed, not those of, e.g., any inlined functions.\n\
2859 This modifier hasn't proved useful in practice and is deprecated\n\
2862 With a /r modifier, raw instructions in hex are included.\n\
2864 With a single argument, the function surrounding that address is dumped.\n\
2865 Two arguments (separated by a comma) are taken as a range of memory to dump,\n\
2866 in the form of \"start,end\", or \"start,+length\".\n\
2868 Note that the address is interpreted as an expression, not as a location\n\
2869 like in the \"break\" command.\n\
2870 So, for example, if you want to disassemble function bar in file foo.c\n\
2871 you must type \"disassemble 'foo.c'::bar\" and not \"disassemble foo.c:bar\"."));
2872 set_cmd_completer_handle_brkchars (c
, disassemble_command_completer
);
2874 c
= add_com ("make", class_support
, make_command
, _("\
2875 Run the ``make'' program using the rest of the line as arguments."));
2876 set_cmd_completer (c
, filename_completer
);
2877 c
= add_cmd ("user", no_class
, show_user
, _("\
2878 Show definitions of non-python/scheme user defined commands.\n\
2879 Argument is the name of the user defined command.\n\
2880 With no argument, show definitions of all user defined commands."), &showlist
);
2881 set_cmd_completer (c
, show_user_completer
);
2882 add_com ("apropos", class_support
, apropos_command
, _("\
2883 Search for commands matching a REGEXP.\n\
2884 Usage: apropos [-v] REGEXP\n\
2885 Flag -v indicates to produce a verbose output, showing full documentation\n\
2886 of the matching commands."));
2888 add_setshow_uinteger_cmd ("max-user-call-depth", no_class
,
2889 &max_user_call_depth
, _("\
2890 Set the max call depth for non-python/scheme user-defined commands."), _("\
2891 Show the max call depth for non-python/scheme user-defined commands."), NULL
,
2893 show_max_user_call_depth
,
2894 &setlist
, &showlist
);
2896 add_setshow_boolean_cmd ("trace-commands", no_class
, &trace_commands
, _("\
2897 Set tracing of GDB CLI commands."), _("\
2898 Show state of GDB CLI command tracing."), _("\
2899 When 'on', each command is displayed as it is executed."),
2902 &setlist
, &showlist
);
2904 const auto alias_opts
= make_alias_options_def_group (nullptr);
2906 static std::string alias_help
2907 = gdb::option::build_help (_("\
2908 Define a new command that is an alias of an existing command.\n\
2909 Usage: alias [-a] [--] ALIAS = COMMAND [DEFAULT-ARGS...]\n\
2910 ALIAS is the name of the alias command to create.\n\
2911 COMMAND is the command being aliased to.\n\
2916 GDB will automatically prepend the provided DEFAULT-ARGS to the list\n\
2917 of arguments explicitly provided when using ALIAS.\n\
2918 Use \"help aliases\" to list all user defined aliases and their default args.\n\
2921 Make \"spe\" an alias of \"set print elements\":\n\
2922 alias spe = set print elements\n\
2923 Make \"elms\" an alias of \"elements\" in the \"set print\" command:\n\
2924 alias -a set print elms = set print elements\n\
2925 Make \"btf\" an alias of \"backtrace -full -past-entry -past-main\" :\n\
2926 alias btf = backtrace -full -past-entry -past-main\n\
2927 Make \"wLapPeu\" an alias of 2 nested \"with\":\n\
2928 alias wLapPeu = with language pascal -- with print elements unlimited --"),
2931 c
= add_com ("alias", class_support
, alias_command
,
2932 alias_help
.c_str ());
2934 set_cmd_completer_handle_brkchars (c
, alias_command_completer
);
2936 add_setshow_boolean_cmd ("suppress-cli-notifications", no_class
,
2937 &user_wants_cli_suppress_notification
,
2939 Set whether printing notifications on CLI is suppressed."), _("\
2940 Show whether printing notifications on CLI is suppressed."), _("\
2941 When on, printing notifications (such as inferior/thread switch)\n\
2942 on CLI is suppressed."),
2943 set_suppress_cli_notifications
,
2944 show_suppress_cli_notifications
,
2948 const char *source_help_text
= xstrprintf (_("\
2949 Read commands from a file named FILE.\n\
2951 Usage: source [-s] [-v] FILE\n\
2952 -s: search for the script in the source search path,\n\
2953 even if FILE contains directories.\n\
2954 -v: each command in FILE is echoed as it is executed.\n\
2956 Note that the file \"%s\" is read automatically in this way\n\
2957 when GDB is started."), GDBINIT
).release ();
2958 c
= add_cmd ("source", class_support
, source_command
,
2959 source_help_text
, &cmdlist
);
2960 set_cmd_completer (c
, filename_completer
);