/* GDB CLI command scripting.
- Copyright (C) 1986-2018 Free Software Foundation, Inc.
+ Copyright (C) 1986-2019 Free Software Foundation, Inc.
This file is part of GDB.
#include "ui-out.h"
#include "top.h"
#include "breakpoint.h"
+#include "tracepoint.h"
#include "cli/cli-cmds.h"
#include "cli/cli-decode.h"
#include "cli/cli-script.h"
#include "interps.h"
#include "compile/compile.h"
#include "common/gdb_string_view.h"
+#include "python/python.h"
+#include "guile/guile.h"
#include <vector>
/* Prototypes for local functions. */
static enum command_control_type
-recurse_read_control_structure (char * (*read_next_line_func) (void),
- struct command_line *current_cmd,
- void (*validator)(char *, void *),
- void *closure);
+recurse_read_control_structure
+ (gdb::function_view<const char * ()> read_next_line_func,
+ struct command_line *current_cmd,
+ gdb::function_view<void (const char *)> validator);
+
+static void do_define_command (const char *comname, int from_tty,
+ const counted_command_line *commands);
static char *read_next_line (void);
/* This is to prevent certain commands being printed twice. */
static int suppress_next_print_command_trace = 0;
+/* Command element for the 'while' command. */
+static cmd_list_element *while_cmd_element = nullptr;
+
+/* Command element for the 'if' command. */
+static cmd_list_element *if_cmd_element = nullptr;
+
+/* Command element for the 'define' command. */
+static cmd_list_element *define_cmd_element = nullptr;
+
/* Structure for arguments to user defined functions. */
class user_args
case compile_control:
case python_control:
case guile_control:
+ case define_control:
return 1;
default:
return 0;
static struct command_line *
build_command_line (enum command_control_type type, const char *args)
{
- if ((args == NULL || *args == '\0')
- && (type == if_control || type == while_control))
- error (_("if/while commands require arguments."));
+ if (args == NULL || *args == '\0')
+ {
+ if (type == if_control)
+ error (_("if command requires an argument."));
+ else if (type == while_control)
+ error (_("while command requires an argument."));
+ else if (type == define_control)
+ error (_("define command requires an argument."));
+ }
gdb_assert (args != NULL);
return new struct command_line (type, xstrdup (args));
command_lines_deleter ());
/* Read in the body of this command. */
- if (recurse_read_control_structure (read_next_line, cmd.get (), 0, 0)
+ if (recurse_read_control_structure (read_next_line, cmd.get (), 0)
== invalid_control)
{
warning (_("Error reading in canned sequence of commands."));
}
}
+/* See cli-script.h. */
+
+void
+execute_control_commands (struct command_line *cmdlines, int from_tty)
+{
+ /* Set the instream to 0, indicating execution of a
+ user-defined function. */
+ scoped_restore restore_instream
+ = make_scoped_restore (¤t_ui->instream, nullptr);
+ scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
+ scoped_restore save_nesting
+ = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
+
+ while (cmdlines)
+ {
+ enum command_control_type ret = execute_control_command (cmdlines,
+ from_tty);
+ if (ret != simple_control && ret != break_control)
+ {
+ warning (_("Error executing canned sequence of commands."));
+ break;
+ }
+ cmdlines = cmdlines->next;
+ }
+}
+
+/* See cli-script.h. */
+
+std::string
+execute_control_commands_to_string (struct command_line *commands,
+ int from_tty)
+{
+ /* GDB_STDOUT should be better already restored during these
+ restoration callbacks. */
+ set_batch_flag_and_restore_page_info save_page_info;
+
+ string_file str_file;
+
+ {
+ current_uiout->redirect (&str_file);
+ ui_out_redirect_pop redirect_popper (current_uiout);
+
+ scoped_restore save_stdout
+ = make_scoped_restore (&gdb_stdout, &str_file);
+ scoped_restore save_stderr
+ = make_scoped_restore (&gdb_stderr, &str_file);
+ scoped_restore save_stdlog
+ = make_scoped_restore (&gdb_stdlog, &str_file);
+ scoped_restore save_stdtarg
+ = make_scoped_restore (&gdb_stdtarg, &str_file);
+ scoped_restore save_stdtargerr
+ = make_scoped_restore (&gdb_stdtargerr, &str_file);
+
+ execute_control_commands (commands, from_tty);
+ }
+
+ return std::move (str_file.string ());
+}
+
void
execute_user_command (struct cmd_list_element *c, const char *args)
{
- struct ui *ui = current_ui;
counted_command_line cmdlines_copy;
- enum command_control_type ret;
extern unsigned int max_user_call_depth;
/* Ensure that the user commands can't be deleted while they are
if (user_args_stack.size () > max_user_call_depth)
error (_("Max user call depth exceeded -- command aborted."));
- /* Set the instream to 0, indicating execution of a
- user-defined function. */
- scoped_restore restore_instream
- = make_scoped_restore (&ui->instream, nullptr);
-
- scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
-
- scoped_restore save_nesting
- = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
- while (cmdlines)
- {
- ret = execute_control_command (cmdlines);
- if (ret != simple_control && ret != break_control)
- {
- warning (_("Error executing canned sequence of commands."));
- break;
- }
- cmdlines = cmdlines->next;
- }
+ execute_control_commands (cmdlines, 0);
}
/* This function is called every time GDB prints a prompt. It ensures
via while_command or if_command. Inner levels of 'if' and 'while'
are dealt with directly. Therefore we can use these functions
to determine whether the command has been printed already or not. */
+ATTRIBUTE_PRINTF (1, 2)
void
-print_command_trace (const char *cmd)
+print_command_trace (const char *fmt, ...)
{
int i;
for (i=0; i < command_nest_depth; i++)
printf_filtered ("+");
- printf_filtered ("%s\n", cmd);
+ va_list args;
+
+ va_start (args, fmt);
+ vprintf_filtered (fmt, args);
+ va_end (args);
+ puts_filtered ("\n");
}
/* Helper for execute_control_command. */
static enum command_control_type
-execute_control_command_1 (struct command_line *cmd)
+execute_control_command_1 (struct command_line *cmd, int from_tty)
{
struct command_line *current;
struct value *val;
{
/* A simple command, execute it and return. */
std::string new_line = insert_user_defined_cmd_args (cmd->line);
- execute_command (new_line.c_str (), 0);
+ execute_command (new_line.c_str (), from_tty);
ret = cmd->control_type;
break;
}
case while_control:
{
- int len = strlen (cmd->line) + 7;
- char *buffer = (char *) alloca (len);
-
- xsnprintf (buffer, len, "while %s", cmd->line);
- print_command_trace (buffer);
+ print_command_trace ("while %s", cmd->line);
/* Parse the loop control expression for the while statement. */
std::string new_line = insert_user_defined_cmd_args (cmd->line);
{
scoped_restore save_nesting
= make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
- ret = execute_control_command_1 (current);
+ ret = execute_control_command_1 (current, from_tty);
/* If we got an error, or a "break" command, then stop
looping. */
case if_control:
{
- int len = strlen (cmd->line) + 4;
- char *buffer = (char *) alloca (len);
-
- xsnprintf (buffer, len, "if %s", cmd->line);
- print_command_trace (buffer);
+ print_command_trace ("if %s", cmd->line);
/* Parse the conditional for the if statement. */
std::string new_line = insert_user_defined_cmd_args (cmd->line);
{
scoped_restore save_nesting
= make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
- ret = execute_control_command_1 (current);
+ ret = execute_control_command_1 (current, from_tty);
/* If we got an error, get out. */
if (ret != simple_control)
ret = simple_control;
break;
+ case define_control:
+ print_command_trace ("define %s", cmd->line);
+ do_define_command (cmd->line, 0, &cmd->body_list_0);
+ ret = simple_control;
+ break;
+
case python_control:
case guile_control:
{
}
enum command_control_type
-execute_control_command (struct command_line *cmd)
+execute_control_command (struct command_line *cmd, int from_tty)
{
/* Make sure we use the console uiout. It's possible that we are executing
breakpoint commands while running the MI interpreter. */
interp *console = interp_lookup (current_ui, INTERP_CONSOLE);
scoped_restore save_uiout
- = make_scoped_restore (¤t_uiout, interp_ui_out (console));
+ = make_scoped_restore (¤t_uiout, console->interp_ui_out ());
- return execute_control_command_1 (cmd);
+ return execute_control_command_1 (cmd, from_tty);
}
/* Like execute_control_command, but first set
else
prompt_ptr = NULL;
- return command_line_input (prompt_ptr, from_tty, "commands");
-}
-
-/* Return true if CMD's name is NAME. */
-
-static bool
-command_name_equals (struct cmd_list_element *cmd, const char *name)
-{
- return (cmd != NULL
- && cmd != CMD_LIST_AMBIGUOUS
- && strcmp (cmd->name, name) == 0);
+ return command_line_input (prompt_ptr, "commands");
}
/* Given an input line P, skip the command and return a pointer to the
Otherwise, only "end" is recognized. */
static enum misc_command_type
-process_next_line (char *p, struct command_line **command, int parse_commands,
- void (*validator)(char *, void *), void *closure)
+process_next_line (const char *p, struct command_line **command,
+ int parse_commands,
+ gdb::function_view<void (const char *)> validator)
+
{
- char *p_end;
- char *p_start;
+ const char *p_end;
+ const char *p_start;
int not_handled = 0;
/* Not sure what to do here. */
/* Check for while, if, break, continue, etc and build a new
command line structure for them. */
- if (command_name_equals (cmd, "while-stepping"))
+ if (cmd == while_stepping_cmd_element)
{
/* Because validate_actionline and encode_action lookup
command's line as command, we need the line to
not. */
*command = build_command_line (while_stepping_control, p);
}
- else if (command_name_equals (cmd, "while"))
- {
- *command = build_command_line (while_control, line_first_arg (p));
- }
- else if (command_name_equals (cmd, "if"))
- {
- *command = build_command_line (if_control, line_first_arg (p));
- }
- else if (command_name_equals (cmd, "commands"))
- {
- *command = build_command_line (commands_control, line_first_arg (p));
- }
- else if (command_name_equals (cmd, "python") && !inline_cmd)
+ else if (cmd == while_cmd_element)
+ *command = build_command_line (while_control, line_first_arg (p));
+ else if (cmd == if_cmd_element)
+ *command = build_command_line (if_control, line_first_arg (p));
+ else if (cmd == commands_cmd_element)
+ *command = build_command_line (commands_control, line_first_arg (p));
+ else if (cmd == define_cmd_element)
+ *command = build_command_line (define_control, line_first_arg (p));
+ else if (cmd == python_cmd_element && !inline_cmd)
{
/* Note that we ignore the inline "python command" form
here. */
*command = build_command_line (python_control, "");
}
- else if (command_name_equals (cmd, "compile") && !inline_cmd)
+ else if (cmd == compile_cmd_element && !inline_cmd)
{
/* Note that we ignore the inline "compile command" form
here. */
*command = build_command_line (compile_control, "");
(*command)->control_u.compile.scope = COMPILE_I_INVALID_SCOPE;
}
- else if (command_name_equals (cmd, "guile") && !inline_cmd)
+ else if (cmd == guile_cmd_element && !inline_cmd)
{
/* Note that we ignore the inline "guile command" form here. */
*command = build_command_line (guile_control, "");
if (validator)
{
-
TRY
{
- validator ((*command)->line, closure);
+ validator ((*command)->line);
}
CATCH (ex, RETURN_MASK_ALL)
{
obtain lines of the command. */
static enum command_control_type
-recurse_read_control_structure (char * (*read_next_line_func) (void),
+recurse_read_control_structure (gdb::function_view<const char * ()> read_next_line_func,
struct command_line *current_cmd,
- void (*validator)(char *, void *),
- void *closure)
+ gdb::function_view<void (const char *)> validator)
{
enum misc_command_type val;
enum command_control_type ret;
- struct command_line **body_ptr, *child_tail, *next;
+ struct command_line *child_tail, *next;
counted_command_line *current_body = ¤t_cmd->body_list_0;
child_tail = NULL;
current_cmd->control_type != python_control
&& current_cmd->control_type != guile_control
&& current_cmd->control_type != compile_control,
- validator, closure);
+ validator);
/* Just skip blanks and comments. */
if (val == nop_command)
{
control_level++;
ret = recurse_read_control_structure (read_next_line_func, next,
- validator, closure);
+ validator);
control_level--;
if (ret != simple_control)
#define END_MESSAGE "End with a line saying just \"end\"."
counted_command_line
-read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
- void (*validator)(char *, void *), void *closure)
+read_command_lines (const char *prompt_arg, int from_tty, int parse_commands,
+ gdb::function_view<void (const char *)> validator)
{
if (from_tty && input_interactive_p (current_ui))
{
END_MESSAGE);
}
else
- {
- printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
- gdb_flush (gdb_stdout);
- }
+ printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
}
counted_command_line head (nullptr, command_lines_deleter ());
if (current_interp_named_p (INTERP_CONSOLE))
head = read_command_lines_1 (read_next_line, parse_commands,
- validator, closure);
+ validator);
else
{
scoped_restore_interp interp_restorer (INTERP_CONSOLE);
head = read_command_lines_1 (read_next_line, parse_commands,
- validator, closure);
+ validator);
}
if (from_tty && input_interactive_p (current_ui)
obtained using READ_NEXT_LINE_FUNC. */
counted_command_line
-read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
- void (*validator)(char *, void *), void *closure)
+read_command_lines_1 (gdb::function_view<const char * ()> read_next_line_func,
+ int parse_commands,
+ gdb::function_view<void (const char *)> validator)
{
struct command_line *tail, *next;
counted_command_line head (nullptr, command_lines_deleter ());
{
dont_repeat ();
val = process_next_line (read_next_line_func (), &next, parse_commands,
- validator, closure);
+ validator);
/* Ignore blank lines or comments. */
if (val == nop_command)
{
control_level++;
ret = recurse_read_control_structure (read_next_line_func, next,
- validator, closure);
+ validator);
control_level--;
if (ret == invalid_control)
{
}
+/* Define a user-defined command. If COMMANDS is NULL, then this is a
+ top-level call and the commands will be read using
+ read_command_lines. Otherwise, it is a "define" command in an
+ existing command and the commands are provided. In the
+ non-top-level case, various prompts and warnings are disabled. */
+
static void
-define_command (const char *comname, int from_tty)
+do_define_command (const char *comname, int from_tty,
+ const counted_command_line *commands)
{
-#define MAX_TMPBUF 128
enum cmd_hook_type
{
CMD_NO_HOOK = 0,
};
struct cmd_list_element *c, *newc, *hookc = 0, **list;
const char *tem, *comfull;
- char tmpbuf[MAX_TMPBUF];
int hook_type = CMD_NO_HOOK;
int hook_name_size = 0;
if (c && strcmp (comname, c->name) != 0)
c = 0;
- if (c)
+ if (c && commands == nullptr)
{
int q;
hookc = lookup_cmd (&tem, *list, "", -1, 0);
if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
hookc = 0;
- if (!hookc)
+ if (!hookc && commands == nullptr)
{
warning (_("Your new `%s' command does not "
"hook any existing command."),
comname = xstrdup (comname);
- xsnprintf (tmpbuf, sizeof (tmpbuf),
- "Type commands for definition of \"%s\".", comfull);
- counted_command_line cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
+ counted_command_line cmds;
+ if (commands == nullptr)
+ {
+ std::string prompt
+ = string_printf ("Type commands for definition of \"%s\".", comfull);
+ cmds = read_command_lines (prompt.c_str (), from_tty, 1, 0);
+ }
+ else
+ cmds = *commands;
newc = add_cmd (comname, class_user, user_defined_command,
(c && c->theclass == class_user)
}
}
+static void
+define_command (const char *comname, int from_tty)
+{
+ do_define_command (comname, from_tty, nullptr);
+}
+
static void
document_command (const char *comname, int from_tty)
{
struct cmd_list_element *c, **list;
const char *tem;
const char *comfull;
- char tmpbuf[128];
comfull = comname;
list = validate_comname (&comname);
if (c->theclass != class_user)
error (_("Command \"%s\" is built-in."), comfull);
- xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
- comfull);
- counted_command_line doclines = read_command_lines (tmpbuf, from_tty,
- 0, 0, 0);
+ std::string prompt = string_printf ("Type documentation for \"%s\".",
+ comfull);
+ counted_command_line doclines = read_command_lines (prompt.c_str (),
+ from_tty, 0, 0);
if (c->doc)
xfree ((char *) c->doc);
scoped_restore restore_line_number
= make_scoped_restore (&source_line_number, 0);
- scoped_restore resotre_file
- = make_scoped_restore (&source_file_name, file);
+ scoped_restore restore_file
+ = make_scoped_restore<std::string, const std::string &> (&source_file_name,
+ file);
scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
prepended. */
throw_error (e.error,
_("%s:%d: Error in sourced command file:\n%s"),
- source_file_name, source_line_number, e.message);
+ source_file_name.c_str (), source_line_number,
+ e.what ());
}
END_CATCH
}
Document a user-defined command.\n\
Give command name as argument. Give documentation on following lines.\n\
End with a line of just \"end\"."));
- add_com ("define", class_support, define_command, _("\
+ define_cmd_element = add_com ("define", class_support, define_command, _("\
Define a new command name. Command name is argument.\n\
Definition appears on following lines, one command per line.\n\
End with a line of just \"end\".\n\
Use the \"document\" command to give documentation for the new command.\n\
-Commands defined in this way may have up to ten arguments."));
+Commands defined in this way may accept an unlimited number of arguments\n\
+accessed via $arg0 .. $argN. $argc tells how many arguments have\n\
+been passed."));
- add_com ("while", class_support, while_command, _("\
+ while_cmd_element = add_com ("while", class_support, while_command, _("\
Execute nested commands WHILE the conditional expression is non zero.\n\
The conditional expression must follow the word `while' and must in turn be\n\
followed by a new line. The nested commands must be entered one per line,\n\
and should be terminated by the word `end'."));
- add_com ("if", class_support, if_command, _("\
+ if_cmd_element = add_com ("if", class_support, if_command, _("\
Execute nested commands once IF the conditional expression is non zero.\n\
The conditional expression must follow the word `if' and must in turn be\n\
followed by a new line. The nested commands must be entered one per line,\n\