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