]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/cli/cli-cmds.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / cli / cli-cmds.c
CommitLineData
d318976c 1/* GDB CLI commands.
8926118c 2
1d506c26 3 Copyright (C) 2000-2024 Free Software Foundation, Inc.
d318976c
FN
4
5 This file is part of GDB.
6
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
d318976c
FN
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d318976c 19
13274fc3 20#include "arch-utils.h"
dbda9972 21#include "readline/tilde.h"
d318976c 22#include "completer.h"
ef0f16cc
TT
23#include "target.h"
24#include "gdbsupport/gdb_wait.h"
ef0f16cc 25#include "gdbsupport/gdb_regex.h"
325ed089 26#include "gdb_vfork.h"
0378c332
FN
27#include "linespec.h"
28#include "expression.h"
83c31e7d
FN
29#include "frame.h"
30#include "value.h"
0378c332 31#include "language.h"
ef0f16cc 32#include "filenames.h"
0378c332
FN
33#include "objfiles.h"
34#include "source.h"
83c31e7d 35#include "disasm.h"
33da3f1c 36#include "tracepoint.h"
268a13a5 37#include "gdbsupport/filestuff.h"
f00aae0f 38#include "location.h"
e9480230 39#include "block.h"
91265a7d 40#include "valprint.h"
d318976c 41
d318976c 42#include "ui-out.h"
947d3946 43#include "interps.h"
d318976c
FN
44
45#include "top.h"
13d03262 46#include "ui.h"
d318976c
FN
47#include "cli/cli-decode.h"
48#include "cli/cli-script.h"
49#include "cli/cli-setshow.h"
50#include "cli/cli-cmds.h"
14309bb6 51#include "cli/cli-style.h"
529480d0 52#include "cli/cli-utils.h"
cf00cd6f 53#include "cli/cli-style.h"
d318976c 54
6dddc817 55#include "extension.h"
268a13a5 56#include "gdbsupport/pathstuff.h"
1845e254 57#include "gdbsupport/gdb_tilde_expand.h"
973817a3 58
6a83354a 59#ifdef TUI
ef0f16cc 60#include "tui/tui.h"
6a83354a
AC
61#endif
62
4b505b12 63#include <fcntl.h>
325fac50 64#include <algorithm>
a97e29d2 65#include <string>
4b505b12 66
0378c332
FN
67/* Prototypes for local utility functions */
68
06871ae8
PA
69static void print_sal_location (const symtab_and_line &sal);
70
6c5b2ebe 71static void ambiguous_line_spec (gdb::array_view<const symtab_and_line> sals,
e439fa14
PA
72 const char *format, ...)
73 ATTRIBUTE_PRINTF (2, 3);
f8eba3c6 74
6c5b2ebe 75static void filter_sals (std::vector<symtab_and_line> &);
f8eba3c6 76
d318976c 77\f
cc8dee1f 78/* See cli-cmds.h. */
37163dcf 79unsigned int max_user_call_depth = 1024;
20f01a46 80
d318976c
FN
81/* Define all cmd_list_elements. */
82
83/* Chain containing all defined commands. */
84
85struct cmd_list_element *cmdlist;
86
87/* Chain containing all defined info subcommands. */
88
89struct cmd_list_element *infolist;
90
ebcd3b23 91/* Chain containing all defined enable subcommands. */
d318976c
FN
92
93struct cmd_list_element *enablelist;
94
ebcd3b23 95/* Chain containing all defined disable subcommands. */
d318976c
FN
96
97struct cmd_list_element *disablelist;
98
ebcd3b23 99/* Chain containing all defined stop subcommands. */
d318976c
FN
100
101struct cmd_list_element *stoplist;
102
ebcd3b23 103/* Chain containing all defined delete subcommands. */
d318976c
FN
104
105struct cmd_list_element *deletelist;
106
ebcd3b23 107/* Chain containing all defined detach subcommands. */
f73adfeb
AS
108
109struct cmd_list_element *detachlist;
110
ebcd3b23 111/* Chain containing all defined kill subcommands. */
2277426b
PA
112
113struct cmd_list_element *killlist;
114
d318976c
FN
115/* Chain containing all defined set subcommands */
116
117struct cmd_list_element *setlist;
118
119/* Chain containing all defined unset subcommands */
120
121struct cmd_list_element *unsetlist;
122
123/* Chain containing all defined show subcommands. */
124
125struct cmd_list_element *showlist;
126
127/* Chain containing all defined \"set history\". */
128
129struct cmd_list_element *sethistlist;
130
131/* Chain containing all defined \"show history\". */
132
133struct cmd_list_element *showhistlist;
134
135/* Chain containing all defined \"unset history\". */
136
137struct cmd_list_element *unsethistlist;
138
ebcd3b23 139/* Chain containing all defined maintenance subcommands. */
d318976c
FN
140
141struct cmd_list_element *maintenancelist;
142
ebcd3b23 143/* Chain containing all defined "maintenance info" subcommands. */
d318976c
FN
144
145struct cmd_list_element *maintenanceinfolist;
146
ebcd3b23 147/* Chain containing all defined "maintenance print" subcommands. */
d318976c
FN
148
149struct cmd_list_element *maintenanceprintlist;
150
27d41eac
YQ
151/* Chain containing all defined "maintenance check" subcommands. */
152
153struct cmd_list_element *maintenancechecklist;
154
50a5f187
AB
155/* Chain containing all defined "maintenance flush" subcommands. */
156
157struct cmd_list_element *maintenanceflushlist;
158
d318976c
FN
159struct cmd_list_element *setprintlist;
160
161struct cmd_list_element *showprintlist;
162
163struct cmd_list_element *setdebuglist;
164
165struct cmd_list_element *showdebuglist;
166
167struct cmd_list_element *setchecklist;
168
169struct cmd_list_element *showchecklist;
16026cd7 170
fde1a9a3
AB
171struct cmd_list_element *setsourcelist;
172
173struct cmd_list_element *showsourcelist;
174
16026cd7
AS
175/* Command tracing state. */
176
177int source_verbose = 0;
491144b5 178bool trace_commands = false;
d318976c 179\f
973817a3
JB
180/* 'script-extension' option support. */
181
182static const char script_ext_off[] = "off";
183static const char script_ext_soft[] = "soft";
184static const char script_ext_strict[] = "strict";
185
40478521 186static const char *const script_ext_enums[] = {
973817a3
JB
187 script_ext_off,
188 script_ext_soft,
189 script_ext_strict,
190 NULL
191};
192
193static const char *script_ext_mode = script_ext_soft;
194\f
61fb7376
TBA
195
196/* User-controllable flag to suppress event notification on CLI. */
197
198static bool user_wants_cli_suppress_notification = false;
199
d318976c 200/* Utility used everywhere when at least one argument is needed and
ebcd3b23 201 none is supplied. */
d318976c
FN
202
203void
5b10184c 204error_no_arg (const char *why)
d318976c 205{
8a3fe4f8 206 error (_("Argument required (%s)."), why);
d318976c
FN
207}
208
4fd6c7e8
TT
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
212 gdb-gdb.gdb. */
213
214static void
215info_command (const char *arg, int from_tty)
216{
217 help_list (infolist, "info ", all_commands, gdb_stdout);
218}
219
fdbc9870
PA
220/* See cli/cli-cmds.h. */
221
222void
223with_command_1 (const char *set_cmd_prefix,
224 cmd_list_element *setlist, const char *args, int from_tty)
225{
26c957f1
PA
226 if (args == nullptr)
227 error (_("Missing arguments."));
228
fdbc9870
PA
229 const char *delim = strstr (args, "--");
230 const char *nested_cmd = nullptr;
231
232 if (delim == args)
233 error (_("Missing setting before '--' delimiter"));
234
235 if (delim == nullptr || *skip_spaces (&delim[2]) == '\0')
236 nested_cmd = repeat_previous ();
237
238 cmd_list_element *set_cmd = lookup_cmd (&args, setlist, set_cmd_prefix,
cf00cd6f 239 nullptr,
fdbc9870
PA
240 /*allow_unknown=*/ 0,
241 /*ignore_help_classes=*/ 1);
242 gdb_assert (set_cmd != nullptr);
243
1d7fe7f0 244 if (!set_cmd->var.has_value ())
fdbc9870
PA
245 error (_("Cannot use this setting with the \"with\" command"));
246
247 std::string temp_value
248 = (delim == nullptr ? args : std::string (args, delim - args));
249
250 if (nested_cmd == nullptr)
251 nested_cmd = skip_spaces (delim + 2);
252
1d7fe7f0
LS
253 gdb_assert (set_cmd->var.has_value ());
254 std::string org_value = get_setshow_command_value_string (*set_cmd->var);
fdbc9870
PA
255
256 /* Tweak the setting to the new temporary value. */
257 do_set_command (temp_value.c_str (), from_tty, set_cmd);
258
259 try
260 {
261 scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
262
263 /* Execute the nested command. */
264 execute_command (nested_cmd, from_tty);
265 }
266 catch (const gdb_exception &ex)
267 {
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. */
271 try
272 {
273 do_set_command (org_value.c_str (), from_tty, set_cmd);
274 }
275 catch (const gdb_exception &ex2)
276 {
277 warning (_("Couldn't restore setting: %s"), ex2.what ());
278 }
279
280 throw;
281 }
282
283 /* Restore the setting. */
284 do_set_command (org_value.c_str (), from_tty, set_cmd);
285}
286
287/* See cli/cli-cmds.h. */
288
289void
290with_command_completer_1 (const char *set_cmd_prefix,
291 completion_tracker &tracker,
292 const char *text)
293{
294 tracker.set_use_custom_word_point (true);
295
296 const char *delim = strstr (text, "--");
297
298 /* If we're still not past the "--" delimiter, complete the "with"
299 command as if it was a "set" command. */
300 if (delim == text
301 || delim == nullptr
302 || !isspace (delim[-1])
303 || !(isspace (delim[2]) || delim[2] == '\0'))
304 {
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 ());
308 return;
309 }
310
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);
315}
316
317/* The "with" command. */
318
319static void
320with_command (const char *args, int from_tty)
321{
322 with_command_1 ("set ", setlist, args, from_tty);
323}
324
325/* "with" command completer. */
326
327static void
328with_command_completer (struct cmd_list_element *ignore,
329 completion_tracker &tracker,
330 const char *text, const char * /*word*/)
331{
332 with_command_completer_1 ("set ", tracker, text);
333}
334
cf00cd6f
PW
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
339 prefix command. */
340
341static struct cmd_list_element *
342lookup_cmd_for_default_args (const char **text,
343 struct cmd_list_element **prefix_cmd)
344{
345 const char *orig_text = *text;
346 struct cmd_list_element *lcmd;
347
348 if (*text == nullptr || skip_spaces (*text) == nullptr)
349 error (_("ALIAS missing."));
350
351 /* We first use lookup_cmd to verify TEXT unambiguously identifies
352 a command. */
353 lcmd = lookup_cmd (text, cmdlist, "", NULL,
354 /*allow_unknown=*/ 0,
355 /*ignore_help_classes=*/ 1);
356
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
362 thread apply all. */
363
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. */
367
368 /* We then use lookup_cmd_composition to detect if the user
369 has specified an alias, and find the possible prefix_cmd
370 of 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)
378 cmd = alias;
379
380 return cmd;
381}
382
d318976c
FN
383/* Provide documentation on command or list given by COMMAND. FROM_TTY
384 is ignored. */
385
d318976c 386static void
0b39b52e 387help_command (const char *command, int from_tty)
d318976c
FN
388{
389 help_cmd (command, gdb_stdout);
390}
391\f
eb3ff9a5 392
ef0b411a
GB
393/* Note: The "complete" command is used by Emacs to implement completion.
394 [Is that why this function writes output with *_unfiltered?] */
d318976c 395
d318976c 396static void
0b39b52e 397complete_command (const char *arg, int from_tty)
d318976c 398{
d318976c
FN
399 dont_repeat ();
400
ef0b411a
GB
401 if (max_completions == 0)
402 {
403 /* Only print this for non-mi frontends. An MI frontend may not
404 be able to handle this. */
112e8700 405 if (!current_uiout->is_mi_like_p ())
ef0b411a
GB
406 {
407 printf_unfiltered (_("max-completions is zero,"
408 " completion is disabled.\n"));
409 }
410 return;
411 }
412
d318976c
FN
413 if (arg == NULL)
414 arg = "";
d9b52655 415
6a2c1b87
PA
416 int quote_char = '\0';
417 const char *word;
418
6e035501 419 completion_result result = complete (arg, &word, &quote_char);
eb3ff9a5 420
eb3ff9a5
PA
421 if (result.number_matches != 0)
422 {
fb7806c7
TV
423 std::string arg_prefix (arg, word - arg);
424
eb3ff9a5
PA
425 if (result.number_matches == 1)
426 printf_unfiltered ("%s%s\n", arg_prefix.c_str (), result.match_list[0]);
427 else
83d31a92 428 {
eb3ff9a5
PA
429 result.sort_match_list ();
430
431 for (size_t i = 0; i < result.number_matches; i++)
83d31a92 432 {
eb3ff9a5
PA
433 printf_unfiltered ("%s%s",
434 arg_prefix.c_str (),
435 result.match_list[i + 1]);
6a2c1b87
PA
436 if (quote_char)
437 printf_unfiltered ("%c", quote_char);
eb3ff9a5 438 printf_unfiltered ("\n");
83d31a92 439 }
83d31a92
TT
440 }
441
eb3ff9a5 442 if (result.number_matches == max_completions)
ef0b411a 443 {
6a2c1b87 444 /* ARG_PREFIX and WORD are included in the output so that emacs
ef0b411a
GB
445 will include the message in the output. */
446 printf_unfiltered (_("%s%s %s\n"),
6a2c1b87 447 arg_prefix.c_str (), word,
ef0b411a
GB
448 get_max_completions_reached_message ());
449 }
d318976c
FN
450 }
451}
452
bbaca940
AC
453int
454is_complete_command (struct cmd_list_element *c)
d318976c 455{
3a553c80 456 return cmd_simple_func_eq (c, complete_command);
d318976c
FN
457}
458
d318976c 459static void
dede02ce 460show_version (const char *args, int from_tty)
d318976c 461{
c61b06a1 462 print_gdb_version (gdb_stdout, true);
6cb06a8c 463 gdb_printf ("\n");
d318976c
FN
464}
465
6eaaf48b 466static void
dede02ce 467show_configuration (const char *args, int from_tty)
6eaaf48b
EZ
468{
469 print_gdb_configuration (gdb_stdout);
470}
471
d318976c
FN
472/* Handle the quit command. */
473
474void
0b39b52e 475quit_command (const char *args, int from_tty)
d318976c 476{
36cf1806
TT
477 int exit_code = 0;
478
479 /* An optional expression may be used to cause gdb to terminate with
480 the value of that expression. */
481 if (args)
482 {
483 struct value *val = parse_and_eval (args);
484
485 exit_code = (int) value_as_long (val);
486 }
487
d318976c 488 if (!quit_confirm ())
8a3fe4f8 489 error (_("Not confirmed."));
d5551862 490
be6a2dca
TV
491 try
492 {
493 query_if_trace_running (from_tty);
494 }
495 catch (const gdb_exception_error &ex)
496 {
497 if (ex.error == TARGET_CLOSE_ERROR)
498 /* We don't care about this since we're quitting anyway, so keep
499 quitting. */
500 exception_print (gdb_stderr, ex);
501 else
502 /* Rethrow, to properly handle error (_("Not confirmed.")). */
503 throw;
504 }
d5551862 505
36cf1806 506 quit_force (args ? &exit_code : NULL, from_tty);
d318976c
FN
507}
508
d318976c 509static void
0b39b52e 510pwd_command (const char *args, int from_tty)
d318976c
FN
511{
512 if (args)
8a3fe4f8 513 error (_("The \"pwd\" command does not take an argument: %s"), args);
43573013
SDJ
514
515 gdb::unique_xmalloc_ptr<char> cwd (getcwd (NULL, 0));
516
517 if (cwd == NULL)
bf1d7d9c 518 error (_("Error finding name of working directory: %s"),
dda83cd7 519 safe_strerror (errno));
d318976c 520
43573013 521 if (strcmp (cwd.get (), current_directory) != 0)
6cb06a8c
TT
522 gdb_printf (_("Working directory %ps\n (canonically %ps).\n"),
523 styled_string (file_name_style.style (),
524 current_directory),
525 styled_string (file_name_style.style (), cwd.get ()));
d318976c 526 else
6cb06a8c
TT
527 gdb_printf (_("Working directory %ps.\n"),
528 styled_string (file_name_style.style (),
529 current_directory));
d318976c
FN
530}
531
532void
5e93d4c6 533cd_command (const char *dir, int from_tty)
d318976c
FN
534{
535 int len;
536 /* Found something other than leading repetitions of "/..". */
537 int found_real_path;
538 char *p;
539
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. */
542 dont_repeat ();
543
6eecf35f
TT
544 gdb::unique_xmalloc_ptr<char> dir_holder
545 (tilde_expand (dir != NULL ? dir : "~"));
546 dir = dir_holder.get ();
d318976c
FN
547
548 if (chdir (dir) < 0)
549 perror_with_name (dir);
550
c3690141 551#ifdef HAVE_DOS_BASED_FILE_SYSTEM
d318976c
FN
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. */
43573013
SDJ
555 gdb::unique_xmalloc_ptr<char> cwd (getcwd (NULL, 0));
556 dir = cwd.get ();
d318976c
FN
557#endif
558
559 len = strlen (dir);
fe4e3eb8 560 if (IS_DIR_SEPARATOR (dir[len - 1]))
d318976c
FN
561 {
562 /* Remove the trailing slash unless this is a root directory
dda83cd7 563 (including a drive letter on non-Unix systems). */
d318976c 564 if (!(len == 1) /* "/" */
c3690141 565#ifdef HAVE_DOS_BASED_FILE_SYSTEM
fe4e3eb8 566 && !(len == 3 && dir[1] == ':') /* "d:/" */
d318976c
FN
567#endif
568 )
569 len--;
570 }
571
6eecf35f
TT
572 dir_holder.reset (savestring (dir, len));
573 if (IS_ABSOLUTE_PATH (dir_holder.get ()))
43573013
SDJ
574 {
575 xfree (current_directory);
576 current_directory = dir_holder.release ();
577 }
d318976c
FN
578 else
579 {
fe4e3eb8 580 if (IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1]))
6eecf35f
TT
581 current_directory = concat (current_directory, dir_holder.get (),
582 (char *) NULL);
d318976c 583 else
1754f103 584 current_directory = concat (current_directory, SLASH_STRING,
6eecf35f 585 dir_holder.get (), (char *) NULL);
d318976c
FN
586 }
587
588 /* Now simplify any occurrences of `.' and `..' in the pathname. */
589
590 found_real_path = 0;
591 for (p = current_directory; *p;)
592 {
fe4e3eb8
EZ
593 if (IS_DIR_SEPARATOR (p[0]) && p[1] == '.'
594 && (p[2] == 0 || IS_DIR_SEPARATOR (p[2])))
b2a3b509 595 memmove (p, p + 2, strlen (p + 2) + 1);
fe4e3eb8
EZ
596 else if (IS_DIR_SEPARATOR (p[0]) && p[1] == '.' && p[2] == '.'
597 && (p[3] == 0 || IS_DIR_SEPARATOR (p[3])))
d318976c
FN
598 {
599 if (found_real_path)
600 {
601 /* Search backwards for the directory just before the "/.."
dda83cd7 602 and obliterate it and the "/..". */
d318976c 603 char *q = p;
cdb27c12 604
fe4e3eb8 605 while (q != current_directory && !IS_DIR_SEPARATOR (q[-1]))
d318976c
FN
606 --q;
607
608 if (q == current_directory)
609 /* current_directory is
610 a relative pathname ("can't happen"--leave it alone). */
611 ++p;
612 else
613 {
b2a3b509 614 memmove (q - 1, p + 3, strlen (p + 3) + 1);
d318976c
FN
615 p = q - 1;
616 }
617 }
618 else
ebcd3b23
MS
619 /* We are dealing with leading repetitions of "/..", for
620 example "/../..", which is the Mach super-root. */
d318976c
FN
621 p += 3;
622 }
623 else
624 {
625 found_real_path = 1;
626 ++p;
627 }
628 }
629
630 forget_cached_source_info ();
631
632 if (from_tty)
633 pwd_command ((char *) 0, 1);
634}
635\f
973817a3
JB
636/* Show the current value of the 'script-extension' option. */
637
638static void
639show_script_ext_mode (struct ui_file *file, int from_tty,
640 struct cmd_list_element *c, const char *value)
d318976c 641{
6cb06a8c
TT
642 gdb_printf (file,
643 _("Script filename extension recognition is \"%s\".\n"),
644 value);
973817a3
JB
645}
646
3f7b2faa
DE
647/* Try to open SCRIPT_FILE.
648 If successful, the full path name is stored in *FULL_PATHP,
ed166945
TT
649 and the stream is returned.
650 If not successful, return NULL; errno is set for the last file
3f7b2faa
DE
651 we tried to open.
652
653 If SEARCH_PATH is non-zero, and the file isn't found in cwd,
f5b95b50 654 search for it in the source search path. */
3f7b2faa 655
6b09f134 656std::optional<open_script>
ed166945 657find_and_open_script (const char *script_file, int search_path)
973817a3 658{
4b505b12 659 int fd;
24b9144d 660 openp_flags search_flags = OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH;
6b09f134 661 std::optional<open_script> opened;
d318976c 662
ee0c3293 663 gdb::unique_xmalloc_ptr<char> file (tilde_expand (script_file));
d318976c 664
3f7b2faa
DE
665 if (search_path)
666 search_flags |= OPF_SEARCH_IN_PATH;
4b505b12 667
3f7b2faa
DE
668 /* Search for and open 'file' on the search path used for source
669 files. Put the full location in *FULL_PATHP. */
e0cc99a6 670 gdb::unique_xmalloc_ptr<char> full_path;
e0700ba4 671 fd = openp (source_path.c_str (), search_flags,
e0cc99a6 672 file.get (), O_RDONLY, &full_path);
4b505b12
AS
673
674 if (fd == -1)
ee0c3293 675 return opened;
973817a3 676
ed166945
TT
677 FILE *result = fdopen (fd, FOPEN_RT);
678 if (result == NULL)
77a35dd8
JK
679 {
680 int save_errno = errno;
681
682 close (fd);
77a35dd8 683 errno = save_errno;
77a35dd8 684 }
ed166945
TT
685 else
686 opened.emplace (gdb_file_up (result), std::move (full_path));
77a35dd8 687
ed166945 688 return opened;
973817a3
JB
689}
690
1a70ae97
DE
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. */
973817a3 697
3f7b2faa 698static void
1a70ae97
DE
699source_script_from_stream (FILE *stream, const char *file,
700 const char *file_to_open)
3f7b2faa 701{
6dddc817 702 if (script_ext_mode != script_ext_off)
973817a3 703 {
6dddc817
DE
704 const struct extension_language_defn *extlang
705 = get_ext_lang_of_file (file);
706
707 if (extlang != NULL)
973817a3 708 {
6dddc817
DE
709 if (ext_lang_present_p (extlang))
710 {
711 script_sourcer_func *sourcer
712 = ext_lang_script_sourcer (extlang);
713
714 gdb_assert (sourcer != NULL);
1a70ae97 715 sourcer (extlang, stream, file_to_open);
6dddc817
DE
716 return;
717 }
718 else if (script_ext_mode == script_ext_soft)
719 {
720 /* Assume the file is a gdb script.
721 This is handled below. */
722 }
723 else
724 throw_ext_lang_unsupported (extlang);
973817a3
JB
725 }
726 }
6dddc817
DE
727
728 script_from_file (stream, file);
3f7b2faa 729}
d318976c 730
3f7b2faa
DE
731/* Worker to perform the "source" command.
732 Load script FILE.
733 If SEARCH_PATH is non-zero, and the file isn't found in cwd,
734 search for it in the source search path. */
735
736static void
737source_script_with_search (const char *file, int from_tty, int search_path)
738{
3f7b2faa
DE
739
740 if (file == NULL || *file == 0)
741 error (_("source command requires file name of file to source."));
742
6b09f134 743 std::optional<open_script> opened = find_and_open_script (file, search_path);
ed166945 744 if (!opened)
3f7b2faa 745 {
d234ef5c 746 /* The script wasn't found, or was otherwise inaccessible.
dda83cd7 747 If the source command was invoked interactively, throw an
ebcd3b23 748 error. Otherwise (e.g. if it was invoked by a script),
7c647d61 749 just emit a warning, rather than cause an error. */
3f7b2faa
DE
750 if (from_tty)
751 perror_with_name (file);
752 else
7c647d61
JB
753 {
754 perror_warning_with_name (file);
755 return;
756 }
3f7b2faa
DE
757 }
758
d234ef5c
DE
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. */
1845e254 764 const char *file_to_open;
e0c34637 765 std::string tilde_expanded_file;
1845e254
AB
766 if (search_path)
767 file_to_open = opened->full_path.get ();
768 else
769 {
e0c34637
SM
770 tilde_expanded_file = gdb_tilde_expand (file);
771 file_to_open = tilde_expanded_file.c_str ();
1845e254
AB
772 }
773 source_script_from_stream (opened->stream.get (), file, file_to_open);
d318976c
FN
774}
775
3f7b2faa
DE
776/* Wrapper around source_script_with_search to export it to main.c
777 for use in loading .gdbinit scripts. */
778
779void
50dd9793 780source_script (const char *file, int from_tty)
3f7b2faa
DE
781{
782 source_script_with_search (file, from_tty, 0);
783}
784
16026cd7 785static void
dede02ce 786source_command (const char *args, int from_tty)
16026cd7 787{
dede02ce 788 const char *file = args;
3f7b2faa 789 int search_path = 0;
16026cd7 790
2ec845e7 791 scoped_restore save_source_verbose = make_scoped_restore (&source_verbose);
16026cd7
AS
792
793 /* -v causes the source command to run in verbose mode.
3f7b2faa
DE
794 -s causes the file to be searched in the source search path,
795 even if the file name contains a '/'.
16026cd7
AS
796 We still have to be able to handle filenames with spaces in a
797 backward compatible way, so buildargv is not appropriate. */
798
799 if (args)
800 {
3f7b2faa 801 while (args[0] != '\0')
16026cd7 802 {
ebcd3b23
MS
803 /* Make sure leading white space does not break the
804 comparisons. */
529480d0 805 args = skip_spaces (args);
3f7b2faa
DE
806
807 if (args[0] != '-')
808 break;
809
810 if (args[1] == 'v' && isspace (args[2]))
811 {
812 source_verbose = 1;
813
814 /* Skip passed -v. */
815 args = &args[3];
816 }
817 else if (args[1] == 's' && isspace (args[2]))
818 {
819 search_path = 1;
16026cd7 820
3f7b2faa
DE
821 /* Skip passed -s. */
822 args = &args[3];
823 }
824 else
825 break;
16026cd7 826 }
3f7b2faa 827
529480d0 828 file = skip_spaces (args);
16026cd7
AS
829 }
830
3f7b2faa 831 source_script_with_search (file, from_tty, search_path);
16026cd7
AS
832}
833
834
d318976c 835static void
0b39b52e 836echo_command (const char *text, int from_tty)
d318976c 837{
d7561cbb 838 const char *p = text;
d5b5ac79 839 int c;
d318976c
FN
840
841 if (text)
842 while ((c = *p++) != '\0')
843 {
844 if (c == '\\')
845 {
846 /* \ at end of argument is used after spaces
847 so they won't be lost. */
848 if (*p == 0)
849 return;
850
f870a310 851 c = parse_escape (get_current_arch (), &p);
d318976c 852 if (c >= 0)
6cb06a8c 853 gdb_printf ("%c", c);
d318976c
FN
854 }
855 else
6cb06a8c 856 gdb_printf ("%c", c);
d318976c
FN
857 }
858
3cd52293 859 gdb_stdout->reset_style ();
ef1dfa36 860
d318976c 861 /* Force this output to appear now. */
d318976c
FN
862 gdb_flush (gdb_stdout);
863}
864
947d3946
PW
865/* Sets the last launched shell command convenience variables based on
866 EXIT_STATUS. */
867
868static void
869exit_status_set_internal_vars (int exit_status)
870{
871 struct internalvar *var_code = lookup_internalvar ("_shell_exitcode");
872 struct internalvar *var_signal = lookup_internalvar ("_shell_exitsignal");
873
874 clear_internalvar (var_code);
875 clear_internalvar (var_signal);
91265a7d
PA
876
877 /* Keep the logic here in sync with shell_internal_fn. */
878
947d3946
PW
879 if (WIFEXITED (exit_status))
880 set_internalvar_integer (var_code, WEXITSTATUS (exit_status));
559e7e50
EZ
881#ifdef __MINGW32__
882 else if (WIFSIGNALED (exit_status) && WTERMSIG (exit_status) == -1)
883 {
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
889 bits. */
890 set_internalvar_integer (var_code, exit_status);
891 }
892#endif
947d3946
PW
893 else if (WIFSIGNALED (exit_status))
894 set_internalvar_integer (var_signal, WTERMSIG (exit_status));
895 else
422186a9 896 warning (_("unexpected shell command exit status %d"), exit_status);
947d3946
PW
897}
898
91265a7d
PA
899/* Run ARG under the shell, and return the exit status. If ARG is
900 NULL, run an interactive shell. */
901
902static int
903run_under_shell (const char *arg, int from_tty)
d318976c 904{
9b265ec2
MM
905#if defined(CANT_FORK) || \
906 (!defined(HAVE_WORKING_VFORK) && !defined(HAVE_WORKING_FORK))
d318976c
FN
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 : "");
910
911 if (!arg)
912 arg = "inferior shell";
913
914 if (rc == -1)
6cb06a8c
TT
915 gdb_printf (gdb_stderr, "Cannot execute %s: %s\n", arg,
916 safe_strerror (errno));
d318976c 917 else if (rc)
6cb06a8c 918 gdb_printf (gdb_stderr, "%s exited with status %d\n", arg, rc);
2584159e 919#ifdef GLOBAL_CURDIR
ebcd3b23
MS
920 /* Make sure to return to the directory GDB thinks it is, in case
921 the shell command we just ran changed it. */
d318976c
FN
922 chdir (current_directory);
923#endif
91265a7d 924 return rc;
d318976c 925#else /* Can fork. */
5be4dfca 926 int status, pid;
d318976c 927
325ed089 928 if ((pid = vfork ()) == 0)
d318976c 929 {
974e6844 930 const char *p, *user_shell = get_shell ();
30e94205 931
614c279d
TT
932 close_most_fds ();
933
ebcd3b23 934 /* Get the name of the shell for arg0. */
9f37bbcc 935 p = lbasename (user_shell);
30e94205 936
d318976c 937 if (!arg)
36662fde 938 execl (user_shell, p, (char *) 0);
d318976c 939 else
36662fde 940 execl (user_shell, p, "-c", arg, (char *) 0);
d318976c 941
6cb06a8c
TT
942 gdb_printf (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
943 safe_strerror (errno));
d318976c
FN
944 _exit (0177);
945 }
946
947 if (pid != -1)
5be4dfca 948 waitpid (pid, &status, 0);
d318976c 949 else
8a3fe4f8 950 error (_("Fork failed"));
91265a7d 951 return status;
d318976c
FN
952#endif /* Can fork. */
953}
954
91265a7d
PA
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. */
958
959static void
960shell_escape (const char *arg, int from_tty)
961{
962 int status = run_under_shell (arg, from_tty);
963 exit_status_set_internal_vars (status);
964}
965
be47f9e8
PA
966/* Implementation of the "shell" command. */
967
968static void
0b39b52e 969shell_command (const char *arg, int from_tty)
be47f9e8
PA
970{
971 shell_escape (arg, from_tty);
972}
973
0378c332 974static void
0b39b52e 975edit_command (const char *arg, int from_tty)
0378c332 976{
0378c332
FN
977 struct symtab_and_line sal;
978 struct symbol *sym;
a121b7c1 979 const char *editor;
0b0865da 980 const char *fn;
0378c332 981
d5529a84 982 /* Pull in the current default source line if necessary. */
0378c332 983 if (arg == 0)
53cb0458
FN
984 {
985 set_default_source_symtab_and_line ();
986 sal = get_current_source_symtab_and_line ();
987 }
0378c332 988
ebcd3b23 989 /* Bare "edit" edits file with present line. */
0378c332
FN
990
991 if (arg == 0)
992 {
993 if (sal.symtab == 0)
8a3fe4f8 994 error (_("No default source file yet."));
0378c332
FN
995 sal.line += get_lines_to_list () / 2;
996 }
997 else
998 {
f2fc3015 999 const char *arg1;
0378c332 1000
f00aae0f 1001 /* Now should only be one argument -- decode it in SAL. */
0378c332 1002 arg1 = arg;
264f9890
PA
1003 location_spec_up locspec = string_to_location_spec (&arg1,
1004 current_language);
82d0a72c
AB
1005
1006 if (*arg1)
1007 error (_("Junk at end of line specification."));
1008
264f9890 1009 std::vector<symtab_and_line> sals = decode_line_1 (locspec.get (),
6c5b2ebe
PA
1010 DECODE_LINE_LIST_MODE,
1011 NULL, NULL, 0);
0378c332 1012
6c5b2ebe
PA
1013 filter_sals (sals);
1014 if (sals.empty ())
d5529a84
TT
1015 {
1016 /* C++ */
1017 return;
1018 }
6c5b2ebe 1019 if (sals.size () > 1)
d5529a84 1020 {
6c5b2ebe 1021 ambiguous_line_spec (sals,
e439fa14 1022 _("Specified line is ambiguous:\n"));
d5529a84
TT
1023 return;
1024 }
0378c332 1025
6c5b2ebe 1026 sal = sals[0];
0378c332 1027
ebcd3b23 1028 /* If line was specified by address, first print exactly which
dda83cd7
SM
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. */
0378c332 1032 if (*arg == '*')
dda83cd7 1033 {
5af949e3 1034 struct gdbarch *gdbarch;
cdb27c12 1035
dda83cd7 1036 if (sal.symtab == 0)
8a3fe4f8 1037 error (_("No source file for address %s."),
2b69941d 1038 paddress (get_current_arch (), sal.pc));
5af949e3 1039
3c86fae3 1040 gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
dda83cd7
SM
1041 sym = find_pc_function (sal.pc);
1042 if (sym)
6cb06a8c
TT
1043 gdb_printf ("%s is in %s (%s:%d).\n",
1044 paddress (gdbarch, sal.pc),
1045 sym->print_name (),
1046 symtab_to_filename_for_display (sal.symtab),
1047 sal.line);
dda83cd7 1048 else
6cb06a8c
TT
1049 gdb_printf ("%s is at %s:%d.\n",
1050 paddress (gdbarch, sal.pc),
1051 symtab_to_filename_for_display (sal.symtab),
1052 sal.line);
dda83cd7 1053 }
0378c332 1054
ebcd3b23 1055 /* If what was given does not imply a symtab, it must be an
dda83cd7 1056 undebuggable symbol which means no source code. */
0378c332
FN
1057
1058 if (sal.symtab == 0)
dda83cd7 1059 error (_("No line number known for %s."), arg);
0378c332
FN
1060 }
1061
081bca4d
TT
1062 if ((editor = getenv ("EDITOR")) == NULL)
1063 editor = "/bin/ex";
a955ca71 1064
f35a17b5 1065 fn = symtab_to_fullname (sal.symtab);
0378c332 1066
a955ca71
EZ
1067 /* Quote the file name, in case it has whitespace or other special
1068 characters. */
8579fd13
AB
1069 gdb::unique_xmalloc_ptr<char> p
1070 = xstrprintf ("%s +%d \"%s\"", editor, sal.line, fn);
1071 shell_escape (p.get (), from_tty);
0378c332
FN
1072}
1073
5f4ba3e7
PA
1074/* The options for the "pipe" command. */
1075
1076struct pipe_cmd_opts
1077{
1078 /* For "-d". */
e0700ba4 1079 std::string delimiter;
5f4ba3e7
PA
1080};
1081
1082static const gdb::option::option_def pipe_cmd_option_defs[] = {
1083
1084 gdb::option::string_option_def<pipe_cmd_opts> {
1085 "d",
1086 [] (pipe_cmd_opts *opts) { return &opts->delimiter; },
1087 nullptr,
1088 N_("Indicates to use the specified delimiter string to separate\n\
1089COMMAND from SHELL_COMMAND, in alternative to |. This is useful in\n\
1090case COMMAND contains a | character."),
1091 },
1092
1093};
1094
1095/* Create an option_def_group for the "pipe" command's options, with
1096 OPTS as context. */
1097
1098static inline gdb::option::option_def_group
1099make_pipe_cmd_options_def_group (pipe_cmd_opts *opts)
1100{
1101 return {{pipe_cmd_option_defs}, opts};
1102}
1103
947d3946
PW
1104/* Implementation of the "pipe" command. */
1105
1106static void
1107pipe_command (const char *arg, int from_tty)
1108{
5f4ba3e7 1109 pipe_cmd_opts opts;
947d3946 1110
5f4ba3e7
PA
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);
1114
1115 const char *delim = "|";
e0700ba4
SM
1116 if (!opts.delimiter.empty ())
1117 delim = opts.delimiter.c_str ();
947d3946
PW
1118
1119 const char *command = arg;
1120 if (command == nullptr)
1121 error (_("Missing COMMAND"));
1122
5f4ba3e7 1123 arg = strstr (arg, delim);
947d3946
PW
1124
1125 if (arg == nullptr)
1126 error (_("Missing delimiter before SHELL_COMMAND"));
1127
1128 std::string gdb_cmd (command, arg - command);
1129
5f4ba3e7 1130 arg += strlen (delim); /* Skip the delimiter. */
947d3946
PW
1131
1132 if (gdb_cmd.empty ())
fdbc9870 1133 gdb_cmd = repeat_previous ();
947d3946
PW
1134
1135 const char *shell_command = skip_spaces (arg);
1136 if (*shell_command == '\0')
1137 error (_("Missing SHELL_COMMAND"));
1138
1139 FILE *to_shell_command = popen (shell_command, "w");
1140
1141 if (to_shell_command == nullptr)
1142 error (_("Error launching \"%s\""), shell_command);
1143
1144 try
1145 {
1146 stdio_file pipe_file (to_shell_command);
1147
1148 execute_command_to_ui_file (&pipe_file, gdb_cmd.c_str (), from_tty);
1149 }
1150 catch (...)
1151 {
1152 pclose (to_shell_command);
1153 throw;
1154 }
1155
1156 int exit_status = pclose (to_shell_command);
1157
1158 if (exit_status < 0)
1159 error (_("shell command \"%s\" failed: %s"), shell_command,
dda83cd7 1160 safe_strerror (errno));
947d3946
PW
1161 exit_status_set_internal_vars (exit_status);
1162}
1163
5f4ba3e7
PA
1164/* Completer for the pipe command. */
1165
1166static void
1167pipe_command_completer (struct cmd_list_element *ignore,
1168 completion_tracker &tracker,
1169 const char *text, const char *word_ignored)
1170{
1171 pipe_cmd_opts opts;
1172
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))
1177 return;
1178
1179 const char *delimiter = "|";
e0700ba4
SM
1180 if (!opts.delimiter.empty ())
1181 delimiter = opts.delimiter.c_str ();
5f4ba3e7
PA
1182
1183 /* Check if we're past option values already. */
1184 if (text > org_text && !isspace (text[-1]))
1185 return;
1186
1187 const char *delim = strstr (text, delimiter);
1188
1189 /* If we're still not past the delimiter, complete the gdb
1190 command. */
1191 if (delim == nullptr || delim == text)
1192 {
1193 complete_nested_command_line (tracker, text);
1194 return;
1195 }
1196
1197 /* We're past the delimiter. What follows is a shell command, which
1198 we don't know how to complete. */
1199}
1200
0f819434
BL
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. */
1205static void
1206list_around_line (const char *arg, symtab_and_line cursal)
1207{
1208 int first;
1209
1210 first = std::max (cursal.line - get_lines_to_list () / 2, 1);
1211
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)
1218 first -= 1;
1219
1220 print_source_lines (cursal.symtab, source_lines_range (first), 0);
1221}
1222
0378c332 1223static void
0b39b52e 1224list_command (const char *arg, int from_tty)
0378c332 1225{
0378c332 1226 struct symbol *sym;
f2fc3015 1227 const char *arg1;
0378c332
FN
1228 int no_end = 1;
1229 int dummy_end = 0;
1230 int dummy_beg = 0;
1231 int linenum_beg = 0;
0b39b52e 1232 const char *p;
0378c332 1233
ebcd3b23 1234 /* Pull in the current default source line if necessary. */
3e3a1874 1235 if (arg == NULL || ((arg[0] == '+' || arg[0] == '-' || arg[0] == '.') && arg[1] == '\0'))
53cb0458 1236 {
5166082f
PA
1237 /* If this is the first "list" since we've set the current
1238 source line, center the listing around that line. */
3e3a1874 1239 if (get_first_line_listed () == 0 && (arg == nullptr || arg[0] != '.'))
5166082f 1240 {
e61c7092
GL
1241 set_default_source_symtab_and_line ();
1242 list_around_line (arg, get_current_source_symtab_and_line ());
5166082f 1243 }
0378c332 1244
f52625f1
BL
1245 /* "l" and "l +" lists the next few lines, unless we're listing past
1246 the end of the file. */
1247 else if (arg == nullptr || arg[0] == '+')
1248 {
e61c7092
GL
1249 set_default_source_symtab_and_line ();
1250 const symtab_and_line cursal = get_current_source_symtab_and_line ();
f52625f1
BL
1251 if (last_symtab_line (cursal.symtab) >= cursal.line)
1252 print_source_lines (cursal.symtab,
1253 source_lines_range (cursal.line), 0);
1254 else
e61c7092
GL
1255 error (_("End of the file was already reached, use \"list .\" to"
1256 " list the current location again"));
f52625f1 1257 }
0378c332 1258
1a48ce76
AB
1259 /* "l -" lists previous ten lines, the ones before the ten just
1260 listed. */
a0def019 1261 else if (arg[0] == '-')
3b2464a8 1262 {
e61c7092
GL
1263 set_default_source_symtab_and_line ();
1264 const symtab_and_line cursal = get_current_source_symtab_and_line ();
1265
3b2464a8
AB
1266 if (get_first_line_listed () == 1)
1267 error (_("Already at the start of %s."),
1268 symtab_to_filename_for_display (cursal.symtab));
e61c7092 1269
0e2a2133
AB
1270 source_lines_range range (get_first_line_listed (),
1271 source_lines_range::BACKWARD);
1272 print_source_lines (cursal.symtab, range, 0);
3b2464a8 1273 }
0378c332 1274
12f567bc 1275 /* "list ." lists the default location again. */
3e3a1874
BL
1276 else if (arg[0] == '.')
1277 {
e61c7092 1278 symtab_and_line cursal;
12f567bc 1279 if (target_has_stack ())
3e3a1874
BL
1280 {
1281 /* Find the current line by getting the PC of the currently
1282 selected frame, and finding the line associated to it. */
1283 frame_info_ptr frame = get_selected_frame (nullptr);
1284 CORE_ADDR curr_pc = get_frame_pc (frame);
1285 cursal = find_pc_line (curr_pc, 0);
e61c7092
GL
1286
1287 if (cursal.symtab == nullptr)
1288 error
1289 (_("Insufficient debug info for showing source lines at "
1290 "current PC (%s)."), paddress (get_frame_arch (frame),
1291 curr_pc));
3e3a1874 1292 }
12f567bc 1293 else
3e3a1874 1294 {
12f567bc
GL
1295 /* The inferior is not running, so reset the current source
1296 location to the default (usually the main function). */
3e3a1874 1297 clear_current_source_symtab_and_line ();
e61c7092
GL
1298 try
1299 {
1300 set_default_source_symtab_and_line ();
1301 }
1302 catch (const gdb_exception &e)
1303 {
1304 error (_("Insufficient debug info for showing source "
1305 "lines at default location"));
1306 }
3e3a1874 1307 cursal = get_current_source_symtab_and_line ();
e61c7092
GL
1308
1309 gdb_assert (cursal.symtab != nullptr);
3e3a1874 1310 }
e61c7092 1311
3e3a1874 1312 list_around_line (arg, cursal);
e61c7092 1313
12f567bc 1314 /* Set the repeat args so just pressing "enter" after using "list ."
3e3a1874 1315 will print the following lines instead of the same lines again. */
12f567bc
GL
1316 if (from_tty)
1317 set_repeat_arguments ("");
3e3a1874
BL
1318 }
1319
0378c332
FN
1320 return;
1321 }
1322
1323 /* Now if there is only one argument, decode it in SAL
1324 and set NO_END.
1325 If there are two arguments, decode them in SAL and SAL_END
1326 and clear NO_END; however, if one of the arguments is blank,
1327 set DUMMY_BEG or DUMMY_END to record that fact. */
1328
1329 if (!have_full_symbols () && !have_partial_symbols ())
8a3fe4f8 1330 error (_("No symbol table is loaded. Use the \"file\" command."));
0378c332 1331
6c5b2ebe 1332 std::vector<symtab_and_line> sals;
51abb421 1333 symtab_and_line sal, sal_end;
6c5b2ebe 1334
0378c332
FN
1335 arg1 = arg;
1336 if (*arg1 == ',')
1337 dummy_beg = 1;
1338 else
1339 {
264f9890
PA
1340 location_spec_up locspec
1341 = string_to_location_spec (&arg1, current_language);
1342
1343 /* We know that the ARG string is not empty, yet the attempt to
1344 parse a location spec from the string consumed no characters.
1345 This most likely means that the first thing in ARG looks like
1346 a location spec condition, and so the string_to_location_spec
1347 call stopped parsing. */
3c5fcec6
AB
1348 if (arg1 == arg)
1349 error (_("Junk at end of line specification."));
1350
264f9890 1351 sals = decode_line_1 (locspec.get (), DECODE_LINE_LIST_MODE,
ffc2605c 1352 NULL, NULL, 0);
6c5b2ebe
PA
1353 filter_sals (sals);
1354 if (sals.empty ())
f00aae0f
KS
1355 {
1356 /* C++ */
f00aae0f
KS
1357 return;
1358 }
0378c332 1359
6c5b2ebe 1360 sal = sals[0];
0378c332
FN
1361 }
1362
1363 /* Record whether the BEG arg is all digits. */
1364
1365 for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
1366 linenum_beg = (p == arg1);
1367
e439fa14
PA
1368 /* Save the range of the first argument, in case we need to let the
1369 user know it was ambiguous. */
1370 const char *beg = arg;
1371 size_t beg_len = arg1 - beg;
1372
0378c332
FN
1373 while (*arg1 == ' ' || *arg1 == '\t')
1374 arg1++;
1375 if (*arg1 == ',')
1376 {
1377 no_end = 0;
6c5b2ebe 1378 if (sals.size () > 1)
0d999a6e 1379 {
6c5b2ebe 1380 ambiguous_line_spec (sals,
e439fa14
PA
1381 _("Specified first line '%.*s' is ambiguous:\n"),
1382 (int) beg_len, beg);
0d999a6e
ZZ
1383 return;
1384 }
0378c332
FN
1385 arg1++;
1386 while (*arg1 == ' ' || *arg1 == '\t')
1387 arg1++;
1388 if (*arg1 == 0)
1389 dummy_end = 1;
1390 else
1391 {
e439fa14
PA
1392 /* Save the last argument, in case we need to let the user
1393 know it was ambiguous. */
1394 const char *end_arg = arg1;
1395
264f9890
PA
1396 location_spec_up locspec
1397 = string_to_location_spec (&arg1, current_language);
f00aae0f 1398
3c5fcec6
AB
1399 if (*arg1)
1400 error (_("Junk at end of line specification."));
1401
6c5b2ebe
PA
1402 std::vector<symtab_and_line> sals_end
1403 = (dummy_beg
264f9890 1404 ? decode_line_1 (locspec.get (), DECODE_LINE_LIST_MODE,
6c5b2ebe 1405 NULL, NULL, 0)
264f9890 1406 : decode_line_1 (locspec.get (), DECODE_LINE_LIST_MODE,
6c5b2ebe
PA
1407 NULL, sal.symtab, sal.line));
1408
1409 filter_sals (sals_end);
1410 if (sals_end.empty ())
1411 return;
1412 if (sals_end.size () > 1)
0d999a6e 1413 {
6c5b2ebe 1414 ambiguous_line_spec (sals_end,
e439fa14
PA
1415 _("Specified last line '%s' is ambiguous:\n"),
1416 end_arg);
0378c332
FN
1417 return;
1418 }
6c5b2ebe 1419 sal_end = sals_end[0];
0378c332
FN
1420 }
1421 }
1422
1423 if (*arg1)
8a3fe4f8 1424 error (_("Junk at end of line specification."));
0378c332
FN
1425
1426 if (!no_end && !dummy_beg && !dummy_end
1427 && sal.symtab != sal_end.symtab)
e439fa14 1428 error (_("Specified first and last lines are in different files."));
0378c332 1429 if (dummy_beg && dummy_end)
8a3fe4f8 1430 error (_("Two empty args do not say what lines to list."));
0378c332 1431
ebcd3b23 1432 /* If line was specified by address,
0378c332 1433 first print exactly which line, and which file.
ebcd3b23
MS
1434
1435 In this case, sal.symtab == 0 means address is outside of all
1436 known source files, not that user failed to give a filename. */
0378c332
FN
1437 if (*arg == '*')
1438 {
5af949e3 1439 struct gdbarch *gdbarch;
cdb27c12 1440
0378c332 1441 if (sal.symtab == 0)
8a3fe4f8 1442 error (_("No source file for address %s."),
2b69941d 1443 paddress (get_current_arch (), sal.pc));
5af949e3 1444
3c86fae3 1445 gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
0378c332
FN
1446 sym = find_pc_function (sal.pc);
1447 if (sym)
6cb06a8c
TT
1448 gdb_printf ("%s is in %s (%s:%d).\n",
1449 paddress (gdbarch, sal.pc),
1450 sym->print_name (),
1451 symtab_to_filename_for_display (sal.symtab), sal.line);
0378c332 1452 else
6cb06a8c
TT
1453 gdb_printf ("%s is at %s:%d.\n",
1454 paddress (gdbarch, sal.pc),
1455 symtab_to_filename_for_display (sal.symtab), sal.line);
0378c332
FN
1456 }
1457
ebcd3b23
MS
1458 /* If line was not specified by just a line number, and it does not
1459 imply a symtab, it must be an undebuggable symbol which means no
1460 source code. */
0378c332
FN
1461
1462 if (!linenum_beg && sal.symtab == 0)
8a3fe4f8 1463 error (_("No line number known for %s."), arg);
0378c332
FN
1464
1465 /* If this command is repeated with RET,
1466 turn it into the no-arg variant. */
1467
1468 if (from_tty)
85c4be7c 1469 set_repeat_arguments ("");
0378c332
FN
1470
1471 if (dummy_beg && sal_end.symtab == 0)
8a3fe4f8 1472 error (_("No default source file yet. Do \"help list\"."));
0378c332 1473 if (dummy_beg)
0e2a2133
AB
1474 {
1475 source_lines_range range (sal_end.line + 1,
1476 source_lines_range::BACKWARD);
1477 print_source_lines (sal_end.symtab, range, 0);
1478 }
0378c332 1479 else if (sal.symtab == 0)
8a3fe4f8 1480 error (_("No default source file yet. Do \"help list\"."));
0378c332
FN
1481 else if (no_end)
1482 {
6c5b2ebe 1483 for (int i = 0; i < sals.size (); i++)
0d999a6e 1484 {
6c5b2ebe 1485 sal = sals[i];
0d999a6e
ZZ
1486 int first_line = sal.line - get_lines_to_list () / 2;
1487 if (first_line < 1)
1488 first_line = 1;
6c5b2ebe 1489 if (sals.size () > 1)
06871ae8 1490 print_sal_location (sal);
0e2a2133 1491 print_source_lines (sal.symtab, source_lines_range (first_line), 0);
0d999a6e 1492 }
0378c332 1493 }
0e2a2133
AB
1494 else if (dummy_end)
1495 print_source_lines (sal.symtab, source_lines_range (sal.line), 0);
0378c332 1496 else
0e2a2133
AB
1497 print_source_lines (sal.symtab,
1498 source_lines_range (sal.line, (sal_end.line + 1)),
0378c332
FN
1499 0);
1500}
1501
d14508fe
DE
1502/* Subroutine of disassemble_command to simplify it.
1503 Perform the disassembly.
1504 NAME is the name of the function if known, or NULL.
1505 [LOW,HIGH) are the range of addresses to disassemble.
e9480230
KB
1506 BLOCK is the block to disassemble; it needs to be provided
1507 when non-contiguous blocks are disassembled; otherwise
1508 it can be NULL.
d14508fe
DE
1509 MIXED is non-zero to print source with the assembler. */
1510
1511static void
13274fc3 1512print_disassembly (struct gdbarch *gdbarch, const char *name,
9a24775b 1513 CORE_ADDR low, CORE_ADDR high,
e9480230 1514 const struct block *block,
9a24775b 1515 gdb_disassembly_flags flags)
d14508fe
DE
1516{
1517#if defined(TUI)
f9ba974d
TT
1518 if (tui_is_window_visible (DISASSEM_WIN))
1519 tui_show_assembly (gdbarch, low);
1520 else
d14508fe
DE
1521#endif
1522 {
6cb06a8c 1523 gdb_printf (_("Dump of assembler code "));
d14508fe 1524 if (name != NULL)
6cb06a8c
TT
1525 gdb_printf (_("for function %ps:\n"),
1526 styled_string (function_name_style.style (), name));
086d03c9 1527 if (block == nullptr || block->is_contiguous ())
dda83cd7 1528 {
e9480230 1529 if (name == NULL)
6cb06a8c
TT
1530 gdb_printf (_("from %ps to %ps:\n"),
1531 styled_string (address_style.style (),
1532 paddress (gdbarch, low)),
1533 styled_string (address_style.style (),
1534 paddress (gdbarch, high)));
d14508fe 1535
e9480230
KB
1536 /* Dump the specified range. */
1537 gdb_disassembly (gdbarch, current_uiout, flags, -1, low, high);
1538 }
1539 else
dda83cd7 1540 {
f73b4922 1541 for (const blockrange &range : block->ranges ())
e9480230 1542 {
f73b4922
SM
1543 CORE_ADDR range_low = range.start ();
1544 CORE_ADDR range_high = range.end ();
1545
6cb06a8c
TT
1546 gdb_printf (_("Address range %ps to %ps:\n"),
1547 styled_string (address_style.style (),
1548 paddress (gdbarch, range_low)),
1549 styled_string (address_style.style (),
1550 paddress (gdbarch, range_high)));
b926417a
TT
1551 gdb_disassembly (gdbarch, current_uiout, flags, -1,
1552 range_low, range_high);
e9480230
KB
1553 }
1554 }
6cb06a8c 1555 gdb_printf (_("End of assembler dump.\n"));
d14508fe 1556 }
d14508fe
DE
1557}
1558
1559/* Subroutine of disassemble_command to simplify it.
9c419145 1560 Print a disassembly of the current function according to FLAGS. */
d14508fe
DE
1561
1562static void
9a24775b 1563disassemble_current_function (gdb_disassembly_flags flags)
d14508fe 1564{
bd2b40ac 1565 frame_info_ptr frame;
13274fc3 1566 struct gdbarch *gdbarch;
d14508fe 1567 CORE_ADDR low, high, pc;
2c02bd72 1568 const char *name;
e9480230 1569 const struct block *block;
d14508fe 1570
13274fc3
UW
1571 frame = get_selected_frame (_("No frame selected."));
1572 gdbarch = get_frame_arch (frame);
9bf4bce9 1573 pc = get_frame_address_in_block (frame);
e9480230 1574 if (find_pc_partial_function (pc, &name, &low, &high, &block) == 0)
d14508fe
DE
1575 error (_("No function contains program counter for selected frame."));
1576#if defined(TUI)
1577 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1578 `tui_version'. */
1579 if (tui_active)
1580 /* FIXME: cagney/2004-02-07: This should be an observer. */
13274fc3 1581 low = tui_get_low_disassembly_address (gdbarch, low, pc);
d14508fe 1582#endif
13274fc3 1583 low += gdbarch_deprecated_function_start_offset (gdbarch);
d14508fe 1584
e9480230 1585 print_disassembly (gdbarch, name, low, high, block, flags);
d14508fe
DE
1586}
1587
1588/* Dump a specified section of assembly code.
1589
1590 Usage:
6ff0ba5f 1591 disassemble [/mrs]
d14508fe 1592 - dump the assembly code for the function of the current pc
6ff0ba5f 1593 disassemble [/mrs] addr
d14508fe 1594 - dump the assembly code for the function at ADDR
6ff0ba5f
DE
1595 disassemble [/mrs] low,high
1596 disassemble [/mrs] low,+length
53a71c06 1597 - dump the assembly code in the range [LOW,HIGH), or [LOW,LOW+length)
d14508fe 1598
6ff0ba5f
DE
1599 A /m modifier will include source code with the assembly in a
1600 "source centric" view. This view lists only the file of the first insn,
1601 even if other source files are involved (e.g., inlined functions), and
1602 the output is in source order, even with optimized code. This view is
1603 considered deprecated as it hasn't been useful in practice.
1604
1605 A /r modifier will include raw instructions in hex with the assembly.
1606
d4ce49b7
AB
1607 A /b modifier is similar to /r except the instruction bytes are printed
1608 as separate bytes with no grouping, or endian switching.
1609
6ff0ba5f
DE
1610 A /s modifier will include source code with the assembly, like /m, with
1611 two important differences:
1612 1) The output is still in pc address order.
1613 2) File names and contents for all relevant source files are displayed. */
83c31e7d 1614
83c31e7d 1615static void
0b39b52e 1616disassemble_command (const char *arg, int from_tty)
83c31e7d 1617{
13274fc3 1618 struct gdbarch *gdbarch = get_current_arch ();
83c31e7d 1619 CORE_ADDR low, high;
f75a0693 1620 const general_symbol_info *symbol = nullptr;
2c02bd72 1621 const char *name;
d36fc00b 1622 CORE_ADDR pc;
9a24775b 1623 gdb_disassembly_flags flags;
bbc13ae3 1624 const char *p;
e9480230 1625 const struct block *block = nullptr;
83c31e7d 1626
bbc13ae3 1627 p = arg;
83c31e7d 1628 name = NULL;
e6158f16 1629 flags = 0;
d14508fe 1630
bbc13ae3 1631 if (p && *p == '/')
83c31e7d 1632 {
bbc13ae3 1633 ++p;
d14508fe 1634
bbc13ae3 1635 if (*p == '\0')
d14508fe
DE
1636 error (_("Missing modifier."));
1637
bbc13ae3 1638 while (*p && ! isspace (*p))
d14508fe 1639 {
bbc13ae3 1640 switch (*p++)
d14508fe
DE
1641 {
1642 case 'm':
6ff0ba5f 1643 flags |= DISASSEMBLY_SOURCE_DEPRECATED;
e6158f16
HZ
1644 break;
1645 case 'r':
1646 flags |= DISASSEMBLY_RAW_INSN;
d14508fe 1647 break;
d4ce49b7
AB
1648 case 'b':
1649 flags |= DISASSEMBLY_RAW_BYTES;
1650 break;
6ff0ba5f
DE
1651 case 's':
1652 flags |= DISASSEMBLY_SOURCE;
1653 break;
d14508fe
DE
1654 default:
1655 error (_("Invalid disassembly modifier."));
1656 }
1657 }
1658
f1735a53 1659 p = skip_spaces (p);
d14508fe
DE
1660 }
1661
6ff0ba5f
DE
1662 if ((flags & (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
1663 == (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
1664 error (_("Cannot specify both /m and /s."));
1665
f3a8a979
AB
1666 if ((flags & (DISASSEMBLY_RAW_INSN | DISASSEMBLY_RAW_BYTES))
1667 == (DISASSEMBLY_RAW_INSN | DISASSEMBLY_RAW_BYTES))
1668 error (_("Cannot specify both /r and /b."));
1669
bbc13ae3 1670 if (! p || ! *p)
d14508fe 1671 {
9c419145 1672 flags |= DISASSEMBLY_OMIT_FNAME;
e6158f16 1673 disassemble_current_function (flags);
d14508fe 1674 return;
83c31e7d 1675 }
d14508fe 1676
bbc13ae3
KS
1677 pc = value_as_address (parse_to_comma_and_eval (&p));
1678 if (p[0] == ',')
1679 ++p;
1680 if (p[0] == '\0')
83c31e7d
FN
1681 {
1682 /* One argument. */
f75a0693 1683 if (!find_pc_partial_function_sym (pc, &symbol, &low, &high, &block))
8a3fe4f8 1684 error (_("No function contains specified address."));
f75a0693
AB
1685
1686 if (asm_demangle)
1687 name = symbol->print_name ();
1688 else
1689 name = symbol->linkage_name ();
1690
83c31e7d 1691#if defined(TUI)
021e7609
AC
1692 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1693 `tui_version'. */
22940a24
AC
1694 if (tui_active)
1695 /* FIXME: cagney/2004-02-07: This should be an observer. */
13274fc3 1696 low = tui_get_low_disassembly_address (gdbarch, low, pc);
83c31e7d 1697#endif
13274fc3 1698 low += gdbarch_deprecated_function_start_offset (gdbarch);
9c419145 1699 flags |= DISASSEMBLY_OMIT_FNAME;
83c31e7d
FN
1700 }
1701 else
1702 {
1703 /* Two arguments. */
53a71c06 1704 int incl_flag = 0;
21a0512e 1705 low = pc;
f1735a53 1706 p = skip_spaces (p);
bbc13ae3 1707 if (p[0] == '+')
53a71c06 1708 {
bbc13ae3 1709 ++p;
53a71c06
CR
1710 incl_flag = 1;
1711 }
bbc13ae3 1712 high = parse_and_eval_address (p);
53a71c06
CR
1713 if (incl_flag)
1714 high += low;
83c31e7d
FN
1715 }
1716
e9480230 1717 print_disassembly (gdbarch, name, low, high, block, flags);
83c31e7d
FN
1718}
1719
b20885b0
AB
1720/* Command completion for the disassemble command. */
1721
1722static void
1723disassemble_command_completer (struct cmd_list_element *ignore,
1724 completion_tracker &tracker,
1725 const char *text, const char * /* word */)
1726{
1727 if (skip_over_slash_fmt (tracker, &text))
1728 return;
1729
1730 const char *word = advance_to_expression_complete_word_point (tracker, text);
1731 expression_completer (ignore, tracker, text, word);
1732}
1733
d318976c 1734static void
0b39b52e 1735make_command (const char *arg, int from_tty)
d318976c 1736{
d318976c 1737 if (arg == 0)
be47f9e8 1738 shell_escape ("make", from_tty);
d318976c
FN
1739 else
1740 {
be47f9e8 1741 std::string cmd = std::string ("make ") + arg;
d318976c 1742
be47f9e8
PA
1743 shell_escape (cmd.c_str (), from_tty);
1744 }
d318976c
FN
1745}
1746
d318976c 1747static void
dede02ce 1748show_user (const char *args, int from_tty)
d318976c
FN
1749{
1750 struct cmd_list_element *c;
d318976c
FN
1751
1752 if (args)
1753 {
6f937416 1754 const char *comname = args;
cdb27c12 1755
cf00cd6f 1756 c = lookup_cmd (&comname, cmdlist, "", NULL, 0, 1);
a9f116cb 1757 if (!cli_user_command_p (c))
8a3fe4f8 1758 error (_("Not a user command."));
adb483fe 1759 show_user_1 (c, "", args, gdb_stdout);
d318976c
FN
1760 }
1761 else
1762 {
1763 for (c = cmdlist; c; c = c->next)
1764 {
3d0b3564 1765 if (cli_user_command_p (c) || c->is_prefix ())
adb483fe 1766 show_user_1 (c, "", c->name, gdb_stdout);
d318976c
FN
1767 }
1768 }
1769}
1770
cc81bc2d
LS
1771/* Return true if COMMAND or any of its sub-commands is a user defined command.
1772 This is a helper function for show_user_completer. */
1773
1774static bool
1775has_user_subcmd (struct cmd_list_element *command)
1776{
1777 if (cli_user_command_p (command))
1778 return true;
1779
1780 /* Alias command can yield false positive. Ignore them as the targeted
1781 command should be reachable anyway. */
1782 if (command->is_alias ())
1783 return false;
1784
1785 if (command->is_prefix ())
1786 for (struct cmd_list_element *subcommand = *command->subcommands;
1787 subcommand != nullptr;
1788 subcommand = subcommand->next)
1789 if (has_user_subcmd (subcommand))
1790 return true;
1791
1792 return false;
1793}
1794
1795/* Implement completer for the 'show user' command. */
1796
1797static void
1798show_user_completer (cmd_list_element *,
1799 completion_tracker &tracker, const char *text,
1800 const char *word)
1801{
1802 struct cmd_list_element *cmd_group = cmdlist;
1803
1804 /* TEXT can contain a chain of commands and subcommands. Follow the
1805 commands chain until we reach the point where the user wants a
1806 completion. */
1807 while (word > text)
1808 {
1809 const char *curr_cmd = text;
1810 const char *after = skip_to_space (text);
1811 const size_t curr_cmd_len = after - text;
1812 text = skip_spaces (after);
1813
1814 for (struct cmd_list_element *c = cmd_group; c != nullptr; c = c->next)
1815 {
1816 if (strlen (c->name) == curr_cmd_len
1817 && strncmp (c->name, curr_cmd, curr_cmd_len) == 0)
1818 {
1819 if (c->subcommands == nullptr)
1820 /* We arrived after a command with no child, so nothing more
1821 to complete. */
1822 return;
1823
1824 cmd_group = *c->subcommands;
1825 break;
1826 }
1827 }
1828 }
1829
1830 const int wordlen = strlen (word);
1831 for (struct cmd_list_element *c = cmd_group; c != nullptr; c = c->next)
1832 if (has_user_subcmd (c))
1833 {
1834 if (strncmp (c->name, word, wordlen) == 0)
1835 tracker.add_completion
1836 (gdb::unique_xmalloc_ptr<char> (xstrdup (c->name)));
1837 }
1838}
1839
d318976c 1840/* Search through names of commands and documentations for a certain
ebcd3b23
MS
1841 regular expression. */
1842
66d8c862
PW
1843static void
1844apropos_command (const char *arg, int from_tty)
d318976c 1845{
66d8c862
PW
1846 bool verbose = arg && check_for_argument (&arg, "-v", 2);
1847
66d8c862 1848 if (arg == NULL || *arg == '\0')
f55af66d 1849 error (_("REGEXP string is empty"));
d318976c 1850
66d8c862 1851 compiled_regex pattern (arg, REG_ICASE,
2d7cc5c7 1852 _("Error in regular expression"));
dc92e161 1853
c4e37fa8 1854 apropos_cmd (gdb_stdout, cmdlist, verbose, pattern);
d318976c 1855}
5a56e9c5 1856
cf00cd6f
PW
1857/* The options for the "alias" command. */
1858
1859struct alias_opts
1860{
1861 /* For "-a". */
1862 bool abbrev_flag = false;
1863};
1864
1865static const gdb::option::option_def alias_option_defs[] = {
1866
1867 gdb::option::flag_option_def<alias_opts> {
1868 "a",
1869 [] (alias_opts *opts) { return &opts->abbrev_flag; },
1870 N_("Specify that ALIAS is an abbreviation of COMMAND.\n\
1871Abbreviations are not used in command completion."),
1872 },
1873
1874};
1875
1876/* Create an option_def_group for the "alias" options, with
1877 A_OPTS as context. */
1878
1879static gdb::option::option_def_group
1880make_alias_options_def_group (alias_opts *a_opts)
1881{
1882 return {{alias_option_defs}, a_opts};
1883}
1884
1885/* Completer for the "alias_command". */
1886
1887static void
1888alias_command_completer (struct cmd_list_element *ignore,
1889 completion_tracker &tracker,
1890 const char *text, const char *word)
1891{
1892 const auto grp = make_alias_options_def_group (nullptr);
1893
1894 tracker.set_use_custom_word_point (true);
1895
1896 if (gdb::option::complete_options
1897 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
1898 return;
1899
1900 const char *delim = strchr (text, '=');
1901
1902 /* If we're past the "=" delimiter, complete the
1903 "alias ALIAS = COMMAND [DEFAULT-ARGS...]" as if the user is
1904 typing COMMAND DEFAULT-ARGS... */
1905 if (delim != text
1906 && delim != nullptr
1907 && isspace (delim[-1])
1908 && (isspace (delim[1]) || delim[1] == '\0'))
1909 {
1910 std::string new_text = std::string (delim + 1);
1911
1912 tracker.advance_custom_word_point_by (delim + 1 - text);
1913 complete_nested_command_line (tracker, new_text.c_str ());
1914 return;
1915 }
1916
1917 /* We're not yet past the "=" delimiter. Complete a command, as
1918 the user might type an alias following a prefix command. */
1919 complete_nested_command_line (tracker, text);
1920}
1921
5a56e9c5
DE
1922/* Subroutine of alias_command to simplify it.
1923 Return the first N elements of ARGV flattened back to a string
1924 with a space separating each element.
1925 ARGV may not be NULL.
1926 This does not take care of quoting elements in case they contain spaces
1927 on purpose. */
1928
a97e29d2
TT
1929static std::string
1930argv_to_string (char **argv, int n)
5a56e9c5
DE
1931{
1932 int i;
a97e29d2 1933 std::string result;
5a56e9c5
DE
1934
1935 gdb_assert (argv != NULL);
1936 gdb_assert (n >= 0 && n <= countargv (argv));
1937
1938 for (i = 0; i < n; ++i)
1939 {
1940 if (i > 0)
a97e29d2
TT
1941 result += " ";
1942 result += argv[i];
5a56e9c5
DE
1943 }
1944
1945 return result;
1946}
1947
1948/* Subroutine of alias_command to simplify it.
cf00cd6f
PW
1949 Verifies that COMMAND can have an alias:
1950 COMMAND must exist.
1951 COMMAND must not have default args.
1952 This last condition is to avoid the following:
1953 alias aaa = backtrace -full
1954 alias bbb = aaa -past-main
1955 as (at least currently), alias default args are not cumulative
1956 and the user would expect bbb to execute 'backtrace -full -past-main'
1957 while it will execute 'backtrace -past-main'. */
5a56e9c5 1958
3947f654 1959static cmd_list_element *
cf00cd6f 1960validate_aliased_command (const char *command)
5a56e9c5 1961{
cf00cd6f 1962 std::string default_args;
3947f654
SM
1963 cmd_list_element *c
1964 = lookup_cmd_1 (& command, cmdlist, NULL, &default_args, 1);
5a56e9c5
DE
1965
1966 if (c == NULL || c == (struct cmd_list_element *) -1)
cf00cd6f
PW
1967 error (_("Invalid command to alias to: %s"), command);
1968
1969 if (!default_args.empty ())
1970 error (_("Cannot define an alias of an alias that has default args"));
3947f654
SM
1971
1972 return c;
5a56e9c5
DE
1973}
1974
7f31862a
PA
1975/* Called when "alias" was incorrectly used. */
1976
1977static void
1978alias_usage_error (void)
1979{
cf00cd6f 1980 error (_("Usage: alias [-a] [--] ALIAS = COMMAND [DEFAULT-ARGS...]"));
7f31862a
PA
1981}
1982
5a56e9c5
DE
1983/* Make an alias of an existing command. */
1984
1985static void
0b39b52e 1986alias_command (const char *args, int from_tty)
5a56e9c5 1987{
cf00cd6f
PW
1988 alias_opts a_opts;
1989
1990 auto grp = make_alias_options_def_group (&a_opts);
1991 gdb::option::process_options
1992 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
1993
5a56e9c5 1994 int i, alias_argc, command_argc;
0b39b52e 1995 const char *equals;
a97e29d2 1996 const char *alias, *command;
5a56e9c5
DE
1997
1998 if (args == NULL || strchr (args, '=') == NULL)
7f31862a 1999 alias_usage_error ();
5a56e9c5 2000
773a1edc
TT
2001 equals = strchr (args, '=');
2002 std::string args2 (args, equals - args);
2003
2004 gdb_argv built_alias_argv (args2.c_str ());
cf00cd6f
PW
2005
2006 const char *default_args = equals + 1;
2007 struct cmd_list_element *c_command_prefix;
2008
2009 lookup_cmd_for_default_args (&default_args, &c_command_prefix);
2010 std::string command_argv_str (equals + 1,
2011 default_args == nullptr
2012 ? strlen (equals + 1)
2013 : default_args - equals - 1);
2014 gdb_argv command_argv (command_argv_str.c_str ());
5a56e9c5 2015
773a1edc 2016 char **alias_argv = built_alias_argv.get ();
5a56e9c5
DE
2017
2018 if (alias_argv[0] == NULL || command_argv[0] == NULL
2019 || *alias_argv[0] == '\0' || *command_argv[0] == '\0')
7f31862a 2020 alias_usage_error ();
5a56e9c5
DE
2021
2022 for (i = 0; alias_argv[i] != NULL; ++i)
2023 {
2024 if (! valid_user_defined_cmd_name_p (alias_argv[i]))
2025 {
2026 if (i == 0)
2027 error (_("Invalid command name: %s"), alias_argv[i]);
2028 else
2029 error (_("Invalid command element name: %s"), alias_argv[i]);
2030 }
2031 }
2032
2033 alias_argc = countargv (alias_argv);
773a1edc 2034 command_argc = command_argv.count ();
5a56e9c5 2035
cf00cd6f 2036 /* COMMAND must exist, and cannot have default args.
5a56e9c5
DE
2037 Reconstruct the command to remove any extraneous spaces,
2038 for better error messages. */
773a1edc
TT
2039 std::string command_string (argv_to_string (command_argv.get (),
2040 command_argc));
a97e29d2 2041 command = command_string.c_str ();
3947f654 2042 cmd_list_element *target_cmd = validate_aliased_command (command);
5a56e9c5
DE
2043
2044 /* ALIAS must not exist. */
a97e29d2
TT
2045 std::string alias_string (argv_to_string (alias_argv, alias_argc));
2046 alias = alias_string.c_str ();
0605465f
PW
2047 {
2048 cmd_list_element *alias_cmd, *prefix_cmd, *cmd;
2049
2050 if (lookup_cmd_composition (alias, &alias_cmd, &prefix_cmd, &cmd))
2051 {
2052 const char *alias_name = alias_argv[alias_argc-1];
2053
2054 /* If we found an existing ALIAS_CMD, check that the prefix differ or
2055 the name differ. */
2056
2057 if (alias_cmd != nullptr
2058 && alias_cmd->prefix == prefix_cmd
2059 && strcmp (alias_name, alias_cmd->name) == 0)
2060 error (_("Alias already exists: %s"), alias);
2061
2062 /* Check ALIAS differs from the found CMD. */
2063
2064 if (cmd->prefix == prefix_cmd
2065 && strcmp (alias_name, cmd->name) == 0)
2066 error (_("Alias %s is the name of an existing command"), alias);
2067 }
2068 }
2069
5a56e9c5 2070
cf00cd6f
PW
2071 struct cmd_list_element *alias_cmd;
2072
5a56e9c5
DE
2073 /* If ALIAS is one word, it is an alias for the entire COMMAND.
2074 Example: alias spe = set print elements
2075
2076 Otherwise ALIAS and COMMAND must have the same number of words,
b65b566c
PW
2077 and every word except the last must identify the same prefix command;
2078 and the last word of ALIAS is made an alias of the last word of COMMAND.
5a56e9c5
DE
2079 Example: alias set print elms = set pr elem
2080 Note that unambiguous abbreviations are allowed. */
2081
2082 if (alias_argc == 1)
2083 {
2084 /* add_cmd requires *we* allocate space for name, hence the xstrdup. */
3947f654
SM
2085 alias_cmd = add_com_alias (xstrdup (alias_argv[0]), target_cmd,
2086 class_alias, a_opts.abbrev_flag);
5a56e9c5
DE
2087 }
2088 else
2089 {
6f937416 2090 const char *alias_prefix, *command_prefix;
5a56e9c5
DE
2091 struct cmd_list_element *c_alias, *c_command;
2092
2093 if (alias_argc != command_argc)
2094 error (_("Mismatched command length between ALIAS and COMMAND."));
2095
2096 /* Create copies of ALIAS and COMMAND without the last word,
b65b566c
PW
2097 and use that to verify the leading elements give the same
2098 prefix command. */
a97e29d2
TT
2099 std::string alias_prefix_string (argv_to_string (alias_argv,
2100 alias_argc - 1));
b65b566c 2101 std::string command_prefix_string (argv_to_string (command_argv.get (),
a97e29d2
TT
2102 command_argc - 1));
2103 alias_prefix = alias_prefix_string.c_str ();
2104 command_prefix = command_prefix_string.c_str ();
5a56e9c5 2105
cf00cd6f 2106 c_command = lookup_cmd_1 (& command_prefix, cmdlist, NULL, NULL, 1);
5a56e9c5
DE
2107 /* We've already tried to look up COMMAND. */
2108 gdb_assert (c_command != NULL
2109 && c_command != (struct cmd_list_element *) -1);
3d0b3564 2110 gdb_assert (c_command->is_prefix ());
cf00cd6f 2111 c_alias = lookup_cmd_1 (& alias_prefix, cmdlist, NULL, NULL, 1);
5a56e9c5
DE
2112 if (c_alias != c_command)
2113 error (_("ALIAS and COMMAND prefixes do not match."));
2114
2115 /* add_cmd requires *we* allocate space for name, hence the xstrdup. */
cf00cd6f 2116 alias_cmd = add_alias_cmd (xstrdup (alias_argv[alias_argc - 1]),
5e84b7ee 2117 target_cmd, class_alias, a_opts.abbrev_flag,
14b42fc4 2118 c_command->subcommands);
cf00cd6f
PW
2119 }
2120
2121 gdb_assert (alias_cmd != nullptr);
2122 gdb_assert (alias_cmd->default_args.empty ());
2123 if (default_args != nullptr)
2124 {
2125 default_args = skip_spaces (default_args);
2126
2127 alias_cmd->default_args = default_args;
5a56e9c5
DE
2128 }
2129}
d318976c 2130\f
06871ae8
PA
2131/* Print the file / line number / symbol name of the location
2132 specified by SAL. */
2133
2134static void
2135print_sal_location (const symtab_and_line &sal)
2136{
2137 scoped_restore_current_program_space restore_pspace;
2138 set_current_program_space (sal.pspace);
2139
2140 const char *sym_name = NULL;
2141 if (sal.symbol != NULL)
987012b8 2142 sym_name = sal.symbol->print_name ();
6cb06a8c
TT
2143 gdb_printf (_("file: \"%s\", line number: %d, symbol: \"%s\"\n"),
2144 symtab_to_filename_for_display (sal.symtab),
2145 sal.line, sym_name != NULL ? sym_name : "???");
06871ae8
PA
2146}
2147
0378c332 2148/* Print a list of files and line numbers which a user may choose from
ebcd3b23 2149 in order to list a function which was specified ambiguously (as
6c5b2ebe
PA
2150 with `list classname::overloadedfuncname', for example). The SALS
2151 array provides the filenames and line numbers. FORMAT is a
2152 printf-style format string used to tell the user what was
e439fa14 2153 ambiguous. */
0378c332
FN
2154
2155static void
6c5b2ebe
PA
2156ambiguous_line_spec (gdb::array_view<const symtab_and_line> sals,
2157 const char *format, ...)
0378c332 2158{
e439fa14
PA
2159 va_list ap;
2160 va_start (ap, format);
19a7b8ab 2161 gdb_vprintf (format, ap);
e439fa14
PA
2162 va_end (ap);
2163
6c5b2ebe 2164 for (const auto &sal : sals)
06871ae8 2165 print_sal_location (sal);
0378c332
FN
2166}
2167
6c5b2ebe
PA
2168/* Comparison function for filter_sals. Returns a qsort-style
2169 result. */
f8eba3c6
TT
2170
2171static int
6c5b2ebe 2172cmp_symtabs (const symtab_and_line &sala, const symtab_and_line &salb)
f8eba3c6 2173{
e4730328
SM
2174 const char *dira = sala.symtab->compunit ()->dirname ();
2175 const char *dirb = salb.symtab->compunit ()->dirname ();
f8eba3c6
TT
2176 int r;
2177
ee6f8984 2178 if (dira == NULL)
f8eba3c6 2179 {
ee6f8984 2180 if (dirb != NULL)
f8eba3c6
TT
2181 return -1;
2182 }
ee6f8984 2183 else if (dirb == NULL)
f8eba3c6 2184 {
ee6f8984 2185 if (dira != NULL)
f8eba3c6
TT
2186 return 1;
2187 }
2188 else
2189 {
ee6f8984 2190 r = filename_cmp (dira, dirb);
f8eba3c6
TT
2191 if (r)
2192 return r;
2193 }
2194
6c5b2ebe 2195 r = filename_cmp (sala.symtab->filename, salb.symtab->filename);
f8eba3c6
TT
2196 if (r)
2197 return r;
2198
6c5b2ebe 2199 if (sala.line < salb.line)
f8eba3c6 2200 return -1;
6c5b2ebe 2201 return sala.line == salb.line ? 0 : 1;
f8eba3c6
TT
2202}
2203
2204/* Remove any SALs that do not match the current program space, or
2205 which appear to be "file:line" duplicates. */
2206
2207static void
6c5b2ebe 2208filter_sals (std::vector<symtab_and_line> &sals)
f8eba3c6 2209{
6c5b2ebe
PA
2210 /* Remove SALs that do not match. */
2211 auto from = std::remove_if (sals.begin (), sals.end (),
2212 [&] (const symtab_and_line &sal)
2213 { return (sal.pspace != current_program_space || sal.symtab == NULL); });
2214
2215 /* Remove dups. */
2216 std::sort (sals.begin (), from,
2217 [] (const symtab_and_line &sala, const symtab_and_line &salb)
2218 { return cmp_symtabs (sala, salb) < 0; });
2219
2220 from = std::unique (sals.begin (), from,
2221 [&] (const symtab_and_line &sala,
2222 const symtab_and_line &salb)
2223 { return cmp_symtabs (sala, salb) == 0; });
2224
2225 sals.erase (from, sals.end ());
f8eba3c6
TT
2226}
2227
920d2a44
AC
2228static void
2229show_info_verbose (struct ui_file *file, int from_tty,
2230 struct cmd_list_element *c,
2231 const char *value)
2232{
2233 if (info_verbose)
6cb06a8c
TT
2234 gdb_printf (file,
2235 _("Verbose printing of informational messages is %s.\n"),
2236 value);
920d2a44 2237 else
6cb06a8c 2238 gdb_printf (file, _("Verbosity is %s.\n"), value);
920d2a44
AC
2239}
2240
2241static void
2242show_history_expansion_p (struct ui_file *file, int from_tty,
2243 struct cmd_list_element *c, const char *value)
2244{
6cb06a8c
TT
2245 gdb_printf (file, _("History expansion on command input is %s.\n"),
2246 value);
920d2a44
AC
2247}
2248
920d2a44
AC
2249static void
2250show_max_user_call_depth (struct ui_file *file, int from_tty,
2251 struct cmd_list_element *c, const char *value)
2252{
6cb06a8c
TT
2253 gdb_printf (file,
2254 _("The max call depth for user-defined commands is %s.\n"),
2255 value);
920d2a44
AC
2256}
2257
61fb7376
TBA
2258/* Implement 'show suppress-cli-notifications'. */
2259
2260static void
2261show_suppress_cli_notifications (ui_file *file, int from_tty,
2262 cmd_list_element *c, const char *value)
2263{
6cb06a8c
TT
2264 gdb_printf (file, _("Suppression of printing CLI notifications "
2265 "is %s.\n"), value);
61fb7376
TBA
2266}
2267
2268/* Implement 'set suppress-cli-notifications'. */
2269
2270static void
2271set_suppress_cli_notifications (const char *args, int from_tty,
2272 cmd_list_element *c)
2273{
2274 cli_suppress_notification.user_selected_context
2275 = user_wants_cli_suppress_notification;
2276 cli_suppress_notification.normal_stop
2277 = user_wants_cli_suppress_notification;
2278}
2279
9ad9b77d
PW
2280/* Returns the cmd_list_element in SHOWLIST corresponding to the first
2281 argument of ARGV, which must contain one single value.
2282 Throws an error if no value provided, or value not correct.
2283 FNNAME is used in the error message. */
2284
2285static cmd_list_element *
2286setting_cmd (const char *fnname, struct cmd_list_element *showlist,
2287 int argc, struct value **argv)
2288{
2289 if (argc == 0)
2290 error (_("You must provide an argument to %s"), fnname);
2291 if (argc != 1)
2292 error (_("You can only provide one argument to %s"), fnname);
2293
d0c97917 2294 struct type *type0 = check_typedef (argv[0]->type ());
9ad9b77d 2295
78134374
SM
2296 if (type0->code () != TYPE_CODE_ARRAY
2297 && type0->code () != TYPE_CODE_STRING)
9ad9b77d
PW
2298 error (_("First argument of %s must be a string."), fnname);
2299
ec5e9488
AB
2300 /* Not all languages null-terminate their strings, by moving the string
2301 content into a std::string we ensure that a null-terminator is added.
2302 For languages that do add a null-terminator the std::string might end
2303 up with two null characters at the end, but that's harmless. */
2304 const std::string setting ((const char *) argv[0]->contents ().data (),
2305 type0->length ());
2306 const char *a0 = setting.c_str ();
cf00cd6f 2307 cmd_list_element *cmd = lookup_cmd (&a0, showlist, "", NULL, -1, 0);
9ad9b77d 2308
cd4c4c07 2309 if (cmd == nullptr || cmd->type != show_cmd)
a02fcd08
AB
2310 {
2311 gdb_assert (showlist->prefix != nullptr);
2312 std::vector<std::string> components
2313 = showlist->prefix->command_components ();
2314 std::string full_name = components[0];
2315 for (int i = 1; i < components.size (); ++i)
2316 full_name += " " + components[i];
2317 error (_("First argument of %s must be a valid setting of the "
2318 "'%s' command."), fnname, full_name.c_str ());
2319 }
9ad9b77d
PW
2320
2321 return cmd;
2322}
2323
2324/* Builds a value from the show CMD. */
2325
2326static struct value *
1d7fe7f0 2327value_from_setting (const setting &var, struct gdbarch *gdbarch)
9ad9b77d 2328{
1d7fe7f0 2329 switch (var.type ())
9ad9b77d 2330 {
7aeb03e2 2331 case var_uinteger:
9ad9b77d 2332 case var_integer:
7aeb03e2
MR
2333 case var_pinteger:
2334 {
2335 LONGEST value
2336 = (var.type () == var_uinteger
2337 ? static_cast<LONGEST> (var.get<unsigned int> ())
2338 : static_cast<LONGEST> (var.get<int> ()));
2339
2340 if (var.extra_literals () != nullptr)
2341 for (const literal_def *l = var.extra_literals ();
2342 l->literal != nullptr;
2343 l++)
2344 if (value == l->use)
2345 {
2346 if (l->val.has_value ())
2347 value = *l->val;
2348 else
317c3ed9 2349 return value::allocate (builtin_type (gdbarch)->builtin_void);
7aeb03e2
MR
2350 break;
2351 }
2352
2353 if (var.type () == var_uinteger)
2354 return
2355 value_from_ulongest (builtin_type (gdbarch)->builtin_unsigned_int,
2356 static_cast<unsigned int> (value));
2357 else
2358 return
2359 value_from_longest (builtin_type (gdbarch)->builtin_int,
2360 static_cast<int> (value));
2361 }
9ad9b77d
PW
2362 case var_boolean:
2363 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1d7fe7f0 2364 var.get<bool> () ? 1 : 0);
9ad9b77d
PW
2365 case var_auto_boolean:
2366 {
2367 int val;
2368
1d7fe7f0 2369 switch (var.get<enum auto_boolean> ())
9ad9b77d
PW
2370 {
2371 case AUTO_BOOLEAN_TRUE:
2372 val = 1;
2373 break;
2374 case AUTO_BOOLEAN_FALSE:
2375 val = 0;
2376 break;
2377 case AUTO_BOOLEAN_AUTO:
2378 val = -1;
2379 break;
2380 default:
2381 gdb_assert_not_reached ("invalid var_auto_boolean");
2382 }
2383 return value_from_longest (builtin_type (gdbarch)->builtin_int,
2384 val);
2385 }
9ad9b77d
PW
2386 case var_string:
2387 case var_string_noescape:
2388 case var_optional_filename:
2389 case var_filename:
2390 case var_enum:
1d7fe7f0
LS
2391 {
2392 const char *value;
e0700ba4 2393 size_t len;
1d7fe7f0 2394 if (var.type () == var_enum)
e0700ba4
SM
2395 {
2396 value = var.get<const char *> ();
2397 len = strlen (value);
2398 }
1d7fe7f0 2399 else
e0700ba4
SM
2400 {
2401 const std::string &st = var.get<std::string> ();
2402 value = st.c_str ();
2403 len = st.length ();
2404 }
1d7fe7f0 2405
baab3753 2406 return current_language->value_string (gdbarch, value, len);
1d7fe7f0 2407 }
9ad9b77d
PW
2408 default:
2409 gdb_assert_not_reached ("bad var_type");
2410 }
2411}
2412
2413/* Implementation of the convenience function $_gdb_setting. */
2414
2415static struct value *
2416gdb_setting_internal_fn (struct gdbarch *gdbarch,
2417 const struct language_defn *language,
2418 void *cookie, int argc, struct value **argv)
2419{
1d7fe7f0
LS
2420 cmd_list_element *show_cmd
2421 = setting_cmd ("$_gdb_setting", showlist, argc, argv);
2422
2423 gdb_assert (show_cmd->var.has_value ());
2424
2425 return value_from_setting (*show_cmd->var, gdbarch);
9ad9b77d
PW
2426}
2427
2428/* Implementation of the convenience function $_gdb_maint_setting. */
2429
2430static struct value *
2431gdb_maint_setting_internal_fn (struct gdbarch *gdbarch,
2432 const struct language_defn *language,
2433 void *cookie, int argc, struct value **argv)
2434{
1d7fe7f0
LS
2435 cmd_list_element *show_cmd
2436 = setting_cmd ("$_gdb_maint_setting", maintenance_show_cmdlist, argc, argv);
2437
2438 gdb_assert (show_cmd->var.has_value ());
2439
2440 return value_from_setting (*show_cmd->var, gdbarch);
9ad9b77d
PW
2441}
2442
2443/* Builds a string value from the show CMD. */
2444
2445static struct value *
1d7fe7f0 2446str_value_from_setting (const setting &var, struct gdbarch *gdbarch)
9ad9b77d 2447{
1d7fe7f0 2448 switch (var.type ())
9ad9b77d 2449 {
7aeb03e2 2450 case var_uinteger:
9ad9b77d 2451 case var_integer:
7aeb03e2 2452 case var_pinteger:
9ad9b77d 2453 case var_boolean:
9ad9b77d 2454 case var_auto_boolean:
9ad9b77d 2455 {
1d7fe7f0 2456 std::string cmd_val = get_setshow_command_value_string (var);
9ad9b77d 2457
baab3753
AB
2458 return current_language->value_string (gdbarch, cmd_val.c_str (),
2459 cmd_val.size ());
9ad9b77d
PW
2460 }
2461
2462 case var_string:
2463 case var_string_noescape:
2464 case var_optional_filename:
2465 case var_filename:
2466 case var_enum:
2467 /* For these cases, we do not use get_setshow_command_value_string,
2468 as this function handle some characters specially, e.g. by
1d7fe7f0
LS
2469 escaping quotevar. So, we directly use the var string value,
2470 similarly to the value_from_setting code for these casevar. */
2471 {
2472 const char *value;
e0700ba4 2473 size_t len;
1d7fe7f0 2474 if (var.type () == var_enum)
e0700ba4
SM
2475 {
2476 value = var.get<const char *> ();
2477 len = strlen (value);
2478 }
1d7fe7f0 2479 else
e0700ba4
SM
2480 {
2481 const std::string &st = var.get<std::string> ();
2482 value = st.c_str ();
2483 len = st.length ();
2484 }
9ad9b77d 2485
baab3753 2486 return current_language->value_string (gdbarch, value, len);
1d7fe7f0 2487 }
9ad9b77d
PW
2488 default:
2489 gdb_assert_not_reached ("bad var_type");
2490 }
2491}
2492
2493/* Implementation of the convenience function $_gdb_setting_str. */
2494
2495static struct value *
2496gdb_setting_str_internal_fn (struct gdbarch *gdbarch,
2497 const struct language_defn *language,
2498 void *cookie, int argc, struct value **argv)
2499{
1d7fe7f0
LS
2500 cmd_list_element *show_cmd
2501 = setting_cmd ("$_gdb_setting_str", showlist, argc, argv);
2502
2503 gdb_assert (show_cmd->var.has_value ());
2504
2505 return str_value_from_setting (*show_cmd->var, gdbarch);
9ad9b77d
PW
2506}
2507
2508
2509/* Implementation of the convenience function $_gdb_maint_setting_str. */
2510
2511static struct value *
2512gdb_maint_setting_str_internal_fn (struct gdbarch *gdbarch,
2513 const struct language_defn *language,
2514 void *cookie, int argc, struct value **argv)
2515{
1d7fe7f0
LS
2516 cmd_list_element *show_cmd
2517 = setting_cmd ("$_gdb_maint_setting_str", maintenance_show_cmdlist, argc,
2518 argv);
2519
2520 gdb_assert (show_cmd->var.has_value ());
2521
2522 return str_value_from_setting (*show_cmd->var, gdbarch);
9ad9b77d
PW
2523}
2524
91265a7d
PA
2525/* Implementation of the convenience function $_shell. */
2526
2527static struct value *
2528shell_internal_fn (struct gdbarch *gdbarch,
2529 const struct language_defn *language,
2530 void *cookie, int argc, struct value **argv)
2531{
2532 if (argc != 1)
2533 error (_("You must provide one argument for $_shell."));
2534
2535 value *val = argv[0];
2536 struct type *type = check_typedef (val->type ());
2537
2538 if (!language->is_string_type_p (type))
2539 error (_("Argument must be a string."));
2540
2541 value_print_options opts;
2542 get_no_prettyformat_print_options (&opts);
2543
2544 string_file stream;
2545 value_print (val, &stream, &opts);
2546
2547 /* We should always have two quote chars, which we'll strip. */
2548 gdb_assert (stream.size () >= 2);
2549
2550 /* Now strip them. We don't need the original string, so it's
2551 cheaper to do it in place, avoiding a string allocation. */
2552 std::string str = stream.release ();
2553 str[str.size () - 1] = 0;
2554 const char *command = str.c_str () + 1;
2555
2556 int exit_status = run_under_shell (command, 0);
2557
2558 struct type *int_type = builtin_type (gdbarch)->builtin_int;
2559
2560 /* Keep the logic here in sync with
2561 exit_status_set_internal_vars. */
2562
2563 if (WIFEXITED (exit_status))
2564 return value_from_longest (int_type, WEXITSTATUS (exit_status));
2565#ifdef __MINGW32__
2566 else if (WIFSIGNALED (exit_status) && WTERMSIG (exit_status) == -1)
2567 {
2568 /* See exit_status_set_internal_vars. */
2569 return value_from_longest (int_type, exit_status);
2570 }
2571#endif
2572 else if (WIFSIGNALED (exit_status))
2573 {
2574 /* (0x80 | SIGNO) is what most (all?) POSIX-like shells set as
2575 exit code on fatal signal termination. */
2576 return value_from_longest (int_type, 0x80 | WTERMSIG (exit_status));
2577 }
2578 else
2579 return value::allocate_optimized_out (int_type);
2580}
2581
6c265988 2582void _initialize_cli_cmds ();
d318976c 2583void
6c265988 2584_initialize_cli_cmds ()
d318976c
FN
2585{
2586 struct cmd_list_element *c;
2587
2588 /* Define the classes of commands.
1bfeeb0f 2589 They will appear in the help list in alphabetical order. */
d318976c 2590
0450cc4c 2591 add_cmd ("internals", class_maintenance, _("\
1a966eab 2592Maintenance commands.\n\
d318976c
FN
2593Some gdb commands are provided just for use by gdb maintainers.\n\
2594These commands are subject to frequent change, and may not be as\n\
1a966eab 2595well documented as user commands."),
d318976c 2596 &cmdlist);
0450cc4c
TT
2597 add_cmd ("obscure", class_obscure, _("Obscure features."), &cmdlist);
2598 add_cmd ("aliases", class_alias,
cf00cd6f 2599 _("User-defined aliases of other commands."), &cmdlist);
0450cc4c 2600 add_cmd ("user-defined", class_user, _("\
1a966eab 2601User-defined commands.\n\
d318976c 2602The commands in this class are those defined by the user.\n\
1a966eab 2603Use the \"define\" command to define a command."), &cmdlist);
0450cc4c 2604 add_cmd ("support", class_support, _("Support facilities."), &cmdlist);
49a82d50 2605 add_cmd ("status", class_info, _("Status inquiries."), &cmdlist);
0450cc4c 2606 add_cmd ("files", class_files, _("Specifying and examining files."),
1a966eab 2607 &cmdlist);
0450cc4c 2608 add_cmd ("breakpoints", class_breakpoint,
1a966eab 2609 _("Making program stop at certain points."), &cmdlist);
0450cc4c
TT
2610 add_cmd ("data", class_vars, _("Examining data."), &cmdlist);
2611 add_cmd ("stack", class_stack, _("\
1a966eab 2612Examining the stack.\n\
d318976c
FN
2613The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
2614counting from zero for the innermost (currently executing) frame.\n\n\
2615At any time gdb identifies one frame as the \"selected\" frame.\n\
2616Variable lookups are done with respect to the selected frame.\n\
2617When the program being debugged stops, gdb selects the innermost frame.\n\
1a966eab 2618The commands below can be used to select other frames by number or address."),
d318976c 2619 &cmdlist);
e98d2e6d
PW
2620#ifdef TUI
2621 add_cmd ("text-user-interface", class_tui,
2622 _("TUI is the GDB text based interface.\n\
2623In TUI mode, GDB can display several text windows showing\n\
2624the source file, the processor registers, the program disassembly, ..."), &cmdlist);
2625#endif
0450cc4c 2626 add_cmd ("running", class_run, _("Running the program."), &cmdlist);
d318976c 2627
ebcd3b23 2628 /* Define general commands. */
d318976c 2629
d729566a 2630 add_com ("pwd", class_files, pwd_command, _("\
590042fc
PW
2631Print working directory.\n\
2632This is used for your program as well."));
4f8d22e3 2633
1a966eab 2634 c = add_cmd ("cd", class_files, cd_command, _("\
d092c5a2
SDJ
2635Set working directory to DIR for debugger.\n\
2636The debugger's current working directory specifies where scripts and other\n\
2637files that can be loaded by GDB are located.\n\
2638In order to change the inferior's current working directory, the recommended\n\
2639way is to use the \"set cwd\" command."), &cmdlist);
5ba2abeb 2640 set_cmd_completer (c, filename_completer);
d318976c 2641
1bedd215
AC
2642 add_com ("echo", class_support, echo_command, _("\
2643Print a constant string. Give string as argument.\n\
d318976c
FN
2644C escape sequences may be used in the argument.\n\
2645No newline is added at the end of the argument;\n\
2646use \"\\n\" if you want a newline to be printed.\n\
2647Since leading and trailing whitespace are ignored in command arguments,\n\
2648if you want to print some you must use \"\\\" before leading whitespace\n\
1bedd215 2649to be printed or after trailing whitespace."));
d318976c 2650
973817a3
JB
2651 add_setshow_enum_cmd ("script-extension", class_support,
2652 script_ext_enums, &script_ext_mode, _("\
2653Set mode for script filename extension recognition."), _("\
2654Show mode for script filename extension recognition."), _("\
2655off == no filename extension recognition (all sourced files are GDB scripts)\n\
2656soft == evaluate script according to filename extension, fallback to GDB script"
2657 "\n\
2658strict == evaluate script according to filename extension, error if not supported"
2659 ),
2660 NULL,
2661 show_script_ext_mode,
2662 &setlist, &showlist);
2663
3947f654
SM
2664 cmd_list_element *quit_cmd
2665 = add_com ("quit", class_support, quit_command, _("\
bdb52a22 2666Exit gdb.\n\
fa8f0a0f 2667Usage: quit [EXPR] or exit [EXPR]\n\
bdb52a22
TT
2668The optional expression EXPR, if present, is evaluated and the result\n\
2669used as GDB's exit code. The default is zero."));
3947f654
SM
2670 cmd_list_element *help_cmd
2671 = add_com ("help", class_support, help_command,
1bedd215 2672 _("Print list of commands."));
3947f654
SM
2673 set_cmd_completer (help_cmd, command_completer);
2674 add_com_alias ("q", quit_cmd, class_support, 1);
fa8f0a0f 2675 add_com_alias ("exit", quit_cmd, class_support, 1);
3947f654 2676 add_com_alias ("h", help_cmd, class_support, 1);
d318976c 2677
5bf193a2
AC
2678 add_setshow_boolean_cmd ("verbose", class_support, &info_verbose, _("\
2679Set verbosity."), _("\
2680Show verbosity."), NULL,
2681 set_verbose,
920d2a44 2682 show_info_verbose,
5bf193a2 2683 &setlist, &showlist);
d318976c 2684
f54bdb6d
SM
2685 add_setshow_prefix_cmd
2686 ("history", class_support,
2687 _("Generic command for setting command history parameters."),
2688 _("Generic command for showing command history parameters."),
2689 &sethistlist, &showhistlist, &setlist, &showlist);
d318976c 2690
5bf193a2
AC
2691 add_setshow_boolean_cmd ("expansion", no_class, &history_expansion_p, _("\
2692Set history expansion on command input."), _("\
2693Show history expansion on command input."), _("\
2694Without an argument, history expansion is enabled."),
2695 NULL,
920d2a44 2696 show_history_expansion_p,
5bf193a2 2697 &sethistlist, &showhistlist);
d318976c 2698
3947f654
SM
2699 cmd_list_element *info_cmd
2700 = add_prefix_cmd ("info", class_info, info_command, _("\
1bedd215 2701Generic command for showing things about the program being debugged."),
3947f654
SM
2702 &infolist, 0, &cmdlist);
2703 add_com_alias ("i", info_cmd, class_info, 1);
2704 add_com_alias ("inf", info_cmd, class_info, 1);
d318976c
FN
2705
2706 add_com ("complete", class_obscure, complete_command,
1bedd215 2707 _("List the completions for the rest of the line as a command."));
d318976c 2708
a7b9ceb8 2709 c = add_show_prefix_cmd ("show", class_info, _("\
700b53b1 2710Generic command for showing things about the debugger."),
2f822da5 2711 &showlist, 0, &cmdlist);
d318976c 2712 /* Another way to get at the same thing. */
a7b9ceb8 2713 add_alias_cmd ("set", c, class_info, 0, &infolist);
d318976c 2714
3947f654
SM
2715 cmd_list_element *with_cmd
2716 = add_com ("with", class_vars, with_command, _("\
fdbc9870
PA
2717Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.\n\
2718Usage: with SETTING [VALUE] [-- COMMAND]\n\
2719Usage: w SETTING [VALUE] [-- COMMAND]\n\
2720With no COMMAND, repeats the last executed command.\n\
2721\n\
2722SETTING is any setting you can change with the \"set\" subcommands.\n\
2723E.g.:\n\
2724 with language pascal -- print obj\n\
2725 with print elements unlimited -- print obj\n\
2726\n\
2727You can change multiple settings using nested with, and use\n\
2728abbreviations for commands and/or values. E.g.:\n\
2729 w la p -- w p el u -- p obj"));
3947f654
SM
2730 set_cmd_completer_handle_brkchars (with_cmd, with_command_completer);
2731 add_com_alias ("w", with_cmd, class_vars, 1);
fdbc9870 2732
9ad9b77d
PW
2733 add_internal_function ("_gdb_setting_str", _("\
2734$_gdb_setting_str - returns the value of a GDB setting as a string.\n\
2735Usage: $_gdb_setting_str (setting)\n\
2736\n\
2737auto-boolean values are \"off\", \"on\", \"auto\".\n\
2738boolean values are \"off\", \"on\".\n\
2739Some integer settings accept an unlimited value, returned\n\
2740as \"unlimited\"."),
2741 gdb_setting_str_internal_fn, NULL);
2742
2743 add_internal_function ("_gdb_setting", _("\
2744$_gdb_setting - returns the value of a GDB setting.\n\
2745Usage: $_gdb_setting (setting)\n\
2746auto-boolean values are \"off\", \"on\", \"auto\".\n\
2747boolean values are \"off\", \"on\".\n\
2748Some integer settings accept an unlimited value, returned\n\
2749as 0 or -1 depending on the setting."),
2750 gdb_setting_internal_fn, NULL);
2751
2752 add_internal_function ("_gdb_maint_setting_str", _("\
2753$_gdb_maint_setting_str - returns the value of a GDB maintenance setting as a string.\n\
2754Usage: $_gdb_maint_setting_str (setting)\n\
2755\n\
2756auto-boolean values are \"off\", \"on\", \"auto\".\n\
2757boolean values are \"off\", \"on\".\n\
2758Some integer settings accept an unlimited value, returned\n\
2759as \"unlimited\"."),
2760 gdb_maint_setting_str_internal_fn, NULL);
2761
2762 add_internal_function ("_gdb_maint_setting", _("\
2763$_gdb_maint_setting - returns the value of a GDB maintenance setting.\n\
2764Usage: $_gdb_maint_setting (setting)\n\
2765auto-boolean values are \"off\", \"on\", \"auto\".\n\
2766boolean values are \"off\", \"on\".\n\
2767Some integer settings accept an unlimited value, returned\n\
2768as 0 or -1 depending on the setting."),
2769 gdb_maint_setting_internal_fn, NULL);
2770
91265a7d
PA
2771 add_internal_function ("_shell", _("\
2772$_shell - execute a shell command and return the result.\n\
2773\n\
2774 Usage: $_shell (COMMAND)\n\
2775\n\
2776 Arguments:\n\
2777\n\
2778 COMMAND: The command to execute. Must be a string.\n\
2779\n\
2780 Returns:\n\
2781 The command's exit code: zero on success, non-zero otherwise."),
2782 shell_internal_fn, NULL);
2783
db5f229b 2784 add_cmd ("commands", no_set_class, show_commands, _("\
1a966eab 2785Show the history of commands you typed.\n\
d318976c 2786You can supply a command number to start with, or a `+' to start after\n\
1a966eab 2787the previous command number shown."),
d318976c
FN
2788 &showlist);
2789
db5f229b 2790 add_cmd ("version", no_set_class, show_version,
1a966eab 2791 _("Show what version of GDB this is."), &showlist);
d318976c 2792
6eaaf48b
EZ
2793 add_cmd ("configuration", no_set_class, show_configuration,
2794 _("Show how GDB was configured at build time."), &showlist);
2795
f54bdb6d
SM
2796 add_setshow_prefix_cmd ("debug", no_class,
2797 _("Generic command for setting gdb debugging flags."),
2798 _("Generic command for showing gdb debugging flags."),
2799 &setdebuglist, &showdebuglist,
2800 &setlist, &showlist);
d318976c 2801
3947f654
SM
2802 cmd_list_element *shell_cmd
2803 = add_com ("shell", class_support, shell_command, _("\
1bedd215
AC
2804Execute the rest of the line as a shell command.\n\
2805With no arguments, run an inferior shell."));
3947f654 2806 set_cmd_completer (shell_cmd, filename_completer);
d318976c 2807
3947f654 2808 add_com_alias ("!", shell_cmd, class_support, 0);
d563b953 2809
1bedd215
AC
2810 c = add_com ("edit", class_files, edit_command, _("\
2811Edit specified file or function.\n\
0378c332 2812With no argument, edits file containing most recent line listed.\n\
0378c332
FN
2813Editing targets can be specified in these ways:\n\
2814 FILE:LINENUM, to edit at that line in that file,\n\
2815 FUNCTION, to edit at the beginning of that function,\n\
2816 FILE:FUNCTION, to distinguish among like-named static functions.\n\
2817 *ADDRESS, to edit at the line containing that address.\n\
1bedd215 2818Uses EDITOR environment variable contents as editor (or ex as default)."));
0378c332
FN
2819
2820 c->completer = location_completer;
2821
3947f654
SM
2822 cmd_list_element *pipe_cmd
2823 = add_com ("pipe", class_support, pipe_command, _("\
947d3946
PW
2824Send the output of a gdb command to a shell command.\n\
2825Usage: | [COMMAND] | SHELL_COMMAND\n\
2826Usage: | -d DELIM COMMAND DELIM SHELL_COMMAND\n\
2827Usage: pipe [COMMAND] | SHELL_COMMAND\n\
2828Usage: pipe -d DELIM COMMAND DELIM SHELL_COMMAND\n\
2829\n\
2830Executes COMMAND and sends its output to SHELL_COMMAND.\n\
2831\n\
2832The -d option indicates to use the string DELIM to separate COMMAND\n\
2833from SHELL_COMMAND, in alternative to |. This is useful in\n\
2834case COMMAND contains a | character.\n\
2835\n\
2836With no COMMAND, repeat the last executed command\n\
2837and send its output to SHELL_COMMAND."));
3947f654
SM
2838 set_cmd_completer_handle_brkchars (pipe_cmd, pipe_command_completer);
2839 add_com_alias ("|", pipe_cmd, class_support, 0);
947d3946 2840
3947f654
SM
2841 cmd_list_element *list_cmd
2842 = add_com ("list", class_files, list_command, _("\
1bedd215 2843List specified function or line.\n\
0378c332 2844With no argument, lists ten more lines after or around previous listing.\n\
a4e5901b 2845\"list +\" lists the ten lines following a previous ten-line listing.\n\
0378c332 2846\"list -\" lists the ten lines before a previous ten-line listing.\n\
12f567bc 2847\"list .\" lists ten lines around the point of execution in the current frame.\n\
0378c332
FN
2848One argument specifies a line, and ten lines are listed around that line.\n\
2849Two arguments with comma between specify starting and ending lines to list.\n\
0378c332
FN
2850Lines can be specified in these ways:\n\
2851 LINENUM, to list around that line in current file,\n\
2852 FILE:LINENUM, to list around that line in that file,\n\
2853 FUNCTION, to list around beginning of that function,\n\
2854 FILE:FUNCTION, to distinguish among like-named static functions.\n\
2855 *ADDRESS, to list around the line containing that address.\n\
4fdd372d
AB
2856With two args, if one is empty, it stands for ten lines away from\n\
2857the other arg.\n\
2858\n\
2859By default, when a single location is given, display ten lines.\n\
2860This can be changed using \"set listsize\", and the current value\n\
2861can be shown using \"show listsize\"."));
0378c332 2862
3947f654 2863 add_com_alias ("l", list_cmd, class_files, 1);
0378c332 2864
1bedd215
AC
2865 c = add_com ("disassemble", class_vars, disassemble_command, _("\
2866Disassemble a specified section of memory.\n\
16f3242c 2867Usage: disassemble[/m|/r|/s] START [, END]\n\
83c31e7d 2868Default is the function surrounding the pc of the selected frame.\n\
6ff0ba5f 2869\n\
16f3242c
TT
2870With a /s modifier, source lines are included (if available).\n\
2871In this mode, the output is displayed in PC address order, and\n\
2872file names and contents for all relevant source files are displayed.\n\
2873\n\
d14508fe 2874With a /m modifier, source lines are included (if available).\n\
6ff0ba5f
DE
2875This view is \"source centric\": the output is in source line order,\n\
2876regardless of any optimization that is present. Only the main source file\n\
2877is displayed, not those of, e.g., any inlined functions.\n\
2878This modifier hasn't proved useful in practice and is deprecated\n\
2879in favor of /s.\n\
2880\n\
e6158f16 2881With a /r modifier, raw instructions in hex are included.\n\
6ff0ba5f 2882\n\
83c31e7d 2883With a single argument, the function surrounding that address is dumped.\n\
53a71c06 2884Two arguments (separated by a comma) are taken as a range of memory to dump,\n\
7e1e0340
DE
2885 in the form of \"start,end\", or \"start,+length\".\n\
2886\n\
2887Note that the address is interpreted as an expression, not as a location\n\
2888like in the \"break\" command.\n\
2889So, for example, if you want to disassemble function bar in file foo.c\n\
2890you must type \"disassemble 'foo.c'::bar\" and not \"disassemble foo.c:bar\"."));
b20885b0 2891 set_cmd_completer_handle_brkchars (c, disassemble_command_completer);
0378c332 2892
1bedd215
AC
2893 c = add_com ("make", class_support, make_command, _("\
2894Run the ``make'' program using the rest of the line as arguments."));
5ba2abeb 2895 set_cmd_completer (c, filename_completer);
cc81bc2d 2896 c = add_cmd ("user", no_class, show_user, _("\
ed3ef339 2897Show definitions of non-python/scheme user defined commands.\n\
d318976c 2898Argument is the name of the user defined command.\n\
1a966eab 2899With no argument, show definitions of all user defined commands."), &showlist);
cc81bc2d 2900 set_cmd_completer (c, show_user_completer);
66d8c862 2901 add_com ("apropos", class_support, apropos_command, _("\
590042fc 2902Search for commands matching a REGEXP.\n\
66d8c862
PW
2903Usage: apropos [-v] REGEXP\n\
2904Flag -v indicates to produce a verbose output, showing full documentation\n\
2905of the matching commands."));
20f01a46 2906
883b9c6c 2907 add_setshow_uinteger_cmd ("max-user-call-depth", no_class,
c0d88b1b 2908 &max_user_call_depth, _("\
ed3ef339
DE
2909Set the max call depth for non-python/scheme user-defined commands."), _("\
2910Show the max call depth for non-python/scheme user-defined commands."), NULL,
883b9c6c
YQ
2911 NULL,
2912 show_max_user_call_depth,
2913 &setlist, &showlist);
16026cd7
AS
2914
2915 add_setshow_boolean_cmd ("trace-commands", no_class, &trace_commands, _("\
2916Set tracing of GDB CLI commands."), _("\
2917Show state of GDB CLI command tracing."), _("\
2918When 'on', each command is displayed as it is executed."),
2919 NULL,
2920 NULL,
2921 &setlist, &showlist);
5a56e9c5 2922
cf00cd6f
PW
2923 const auto alias_opts = make_alias_options_def_group (nullptr);
2924
2925 static std::string alias_help
2926 = gdb::option::build_help (_("\
5a56e9c5 2927Define a new command that is an alias of an existing command.\n\
cf00cd6f 2928Usage: alias [-a] [--] ALIAS = COMMAND [DEFAULT-ARGS...]\n\
5a56e9c5
DE
2929ALIAS is the name of the alias command to create.\n\
2930COMMAND is the command being aliased to.\n\
cf00cd6f
PW
2931\n\
2932Options:\n\
2933%OPTIONS%\n\
2934\n\
2935GDB will automatically prepend the provided DEFAULT-ARGS to the list\n\
2936of arguments explicitly provided when using ALIAS.\n\
2937Use \"help aliases\" to list all user defined aliases and their default args.\n\
5a56e9c5
DE
2938\n\
2939Examples:\n\
2940Make \"spe\" an alias of \"set print elements\":\n\
e0c45ded 2941 alias spe = set print elements\n\
5a56e9c5 2942Make \"elms\" an alias of \"elements\" in the \"set print\" command:\n\
e0c45ded 2943 alias -a set print elms = set print elements\n\
cf00cd6f
PW
2944Make \"btf\" an alias of \"backtrace -full -past-entry -past-main\" :\n\
2945 alias btf = backtrace -full -past-entry -past-main\n\
2946Make \"wLapPeu\" an alias of 2 nested \"with\":\n\
2947 alias wLapPeu = with language pascal -- with print elements unlimited --"),
2948 alias_opts);
2949
2950 c = add_com ("alias", class_support, alias_command,
2951 alias_help.c_str ());
2952
2953 set_cmd_completer_handle_brkchars (c, alias_command_completer);
43e4916f 2954
61fb7376
TBA
2955 add_setshow_boolean_cmd ("suppress-cli-notifications", no_class,
2956 &user_wants_cli_suppress_notification,
2957 _("\
2958Set whether printing notifications on CLI is suppressed."), _("\
2959Show whether printing notifications on CLI is suppressed."), _("\
2960When on, printing notifications (such as inferior/thread switch)\n\
2961on CLI is suppressed."),
2962 set_suppress_cli_notifications,
2963 show_suppress_cli_notifications,
2964 &setlist,
2965 &showlist);
2966
b777eb6d 2967 const char *source_help_text = xstrprintf (_("\
43e4916f
TT
2968Read commands from a file named FILE.\n\
2969\n\
2970Usage: source [-s] [-v] FILE\n\
2971-s: search for the script in the source search path,\n\
2972 even if FILE contains directories.\n\
2973-v: each command in FILE is echoed as it is executed.\n\
2974\n\
2975Note that the file \"%s\" is read automatically in this way\n\
8579fd13 2976when GDB is started."), GDBINIT).release ();
43e4916f
TT
2977 c = add_cmd ("source", class_support, source_command,
2978 source_help_text, &cmdlist);
2979 set_cmd_completer (c, filename_completer);
2980}