From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA 8/8] Let gdb.execute handle multi-line commands
Date: Thu, 19 Apr 2018 19:16:00 -0000 [thread overview]
Message-ID: <20180419191539.661-9-tom@tromey.com> (raw)
In-Reply-To: <20180419191539.661-1-tom@tromey.com>
This changes the Python API so that gdb.execute can now handle
multi-line commands, like "commands" or "define".
2018-04-19 Tom Tromey <tom@tromey.com>
PR python/22730:
* NEWS: Mention gdb.execute change.
* gdbcmd.h (execute_control_command): Don't declare.
* python/python.c (execute_gdb_command): Use read_command_lines_1,
execute_control_commands, execute_control_commands_to_string.
* cli/cli-script.h (execute_control_commands)
(execute_control_commands_to_string): Declare.
(execute_control_command): Add from_tty parameter.
* cli/cli-script.c (execute_control_commands)
(execute_control_commands_to_string): New functions.
(execute_user_command): Use execute_control_commands.
(execute_control_command_1): Add "from_tty" parameter. Update.
(execute_control_command): Likewise.
testsuite/ChangeLog
2018-04-19 Tom Tromey <tom@tromey.com>
PR python/22730:
* gdb.python/python.exp: Test multi-line execute.
---
gdb/ChangeLog | 16 +++++++
gdb/NEWS | 2 +
gdb/cli/cli-script.c | 93 ++++++++++++++++++++++++++-----------
gdb/cli/cli-script.h | 15 +++++-
gdb/gdbcmd.h | 2 -
gdb/python/python.c | 19 +++++++-
gdb/testsuite/ChangeLog | 5 ++
gdb/testsuite/gdb.python/python.exp | 3 ++
8 files changed, 123 insertions(+), 32 deletions(-)
diff --git a/gdb/NEWS b/gdb/NEWS
index 14d5fbb7c0..a72ee21cf8 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -29,6 +29,8 @@ set|show record btrace cpu
** The commands attached to a breakpoint can be set by assigning to
the breakpoint's "commands" field.
+ ** gdb.execute can now execute multi-line gdb commands.
+
* New targets
RiscV ELF riscv*-*-elf
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index 453983b3bd..775fda4b96 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -369,12 +369,69 @@ execute_cmd_post_hook (struct cmd_list_element *c)
}
}
+/* 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
@@ -390,25 +447,7 @@ execute_user_command (struct cmd_list_element *c, const char *args)
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
@@ -460,7 +499,7 @@ print_command_trace (const char *fmt, ...)
/* 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;
@@ -478,7 +517,7 @@ execute_control_command_1 (struct command_line *cmd)
{
/* 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;
}
@@ -533,7 +572,7 @@ execute_control_command_1 (struct command_line *cmd)
{
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. */
@@ -588,7 +627,7 @@ execute_control_command_1 (struct command_line *cmd)
{
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)
@@ -639,7 +678,7 @@ execute_control_command_1 (struct command_line *cmd)
}
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. */
@@ -647,7 +686,7 @@ execute_control_command (struct command_line *cmd)
scoped_restore save_uiout
= make_scoped_restore (¤t_uiout, interp_ui_out (console));
- return execute_control_command_1 (cmd);
+ return execute_control_command_1 (cmd, from_tty);
}
/* Like execute_control_command, but first set
diff --git a/gdb/cli/cli-script.h b/gdb/cli/cli-script.h
index 3bebd0ed9d..736ebb3a7b 100644
--- a/gdb/cli/cli-script.h
+++ b/gdb/cli/cli-script.h
@@ -122,10 +122,23 @@ extern void show_user_1 (struct cmd_list_element *c,
const char *name,
struct ui_file *stream);
+/* Execute the commands in CMDLINES. */
+
+extern void execute_control_commands (struct command_line *cmdlines,
+ int from_tty);
+
+/* Run execute_control_commands for COMMANDS. Capture its output into
+ the returned string, do not display it to the screen. BATCH_FLAG
+ will be temporarily set to true. */
+
+extern std::string execute_control_commands_to_string
+ (struct command_line *commands, int from_tty);
+
/* Exported to gdb/breakpoint.c */
extern enum command_control_type
- execute_control_command (struct command_line *cmd);
+ execute_control_command (struct command_line *cmd,
+ int from_tty = 0);
extern enum command_control_type
execute_control_command_untraced (struct command_line *cmd);
diff --git a/gdb/gdbcmd.h b/gdb/gdbcmd.h
index 342c8b2c0e..b675ae8618 100644
--- a/gdb/gdbcmd.h
+++ b/gdb/gdbcmd.h
@@ -135,8 +135,6 @@ extern struct cmd_list_element *save_cmdlist;
extern void execute_command (const char *, int);
extern std::string execute_command_to_string (const char *p, int from_tty);
-enum command_control_type execute_control_command (struct command_line *);
-
extern void print_command_line (struct command_line *, unsigned int,
struct ui_file *);
extern void print_command_lines (struct ui_out *,
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 45b6516fc2..5438a148b5 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -581,6 +581,20 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
{
struct interp *interp;
+ std::string arg_copy = arg;
+ bool first = true;
+ char *save_ptr = nullptr;
+ gdb::function_view<const char * ()> reader
+ = [&] ()
+ {
+ const char *result = strtok_r (first ? &arg_copy[0] : nullptr,
+ "\n", &save_ptr);
+ first = false;
+ return result;
+ };
+
+ counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
+
scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
scoped_restore save_uiout = make_scoped_restore (¤t_uiout);
@@ -592,9 +606,10 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
scoped_restore preventer = prevent_dont_repeat ();
if (to_string)
- to_string_res = execute_command_to_string (arg, from_tty);
+ to_string_res = execute_control_commands_to_string (lines.get (),
+ from_tty);
else
- execute_command (arg, from_tty);
+ execute_control_commands (lines.get (), from_tty);
}
CATCH (except, RETURN_MASK_ALL)
{
diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp
index cee195f315..f6bf93add0 100644
--- a/gdb/testsuite/gdb.python/python.exp
+++ b/gdb/testsuite/gdb.python/python.exp
@@ -119,6 +119,9 @@ gdb_test_no_output \
"python x = gdb.execute('printf \"%d\", 23', to_string = True)"
gdb_test "python print (x)" "23"
+gdb_test "python gdb.execute('echo 2\\necho 3\\\\n\\n')" "23" \
+ "multi-line execute"
+
# Test post_event.
gdb_py_test_multiple "post event insertion" \
"python" "" \
--
2.13.6
next prev parent reply other threads:[~2018-04-19 19:16 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-19 19:16 [RFA 0/8] Various command-related improvements Tom Tromey
2018-04-19 19:16 ` [RFA 1/8] Allocate cmd_list_element with new Tom Tromey
2018-04-19 19:16 ` [RFA 6/8] Use function_view in cli-script.c Tom Tromey
2018-04-22 19:02 ` Pedro Alves
2018-04-24 23:38 ` Tom Tromey
2018-04-19 19:16 ` [RFA 5/8] Allow defining a user command inside a user command Tom Tromey
2018-04-24 16:43 ` Pedro Alves
2018-04-24 23:24 ` Tom Tromey
2018-04-19 19:16 ` [RFA 7/8] Allow breakpoint commands to be set from Python Tom Tromey
2018-04-19 19:31 ` Eli Zaretskii
2018-04-24 16:43 ` Pedro Alves
2018-04-19 19:16 ` [RFA 2/8] Use counted_command_line everywhere Tom Tromey
2018-04-24 16:43 ` Pedro Alves
2018-04-24 23:11 ` Tom Tromey
2018-04-24 23:18 ` Tom Tromey
2018-04-19 19:16 ` [RFA 4/8] Constify prompt argument to read_command_lines Tom Tromey
2018-04-24 16:43 ` Pedro Alves
2018-04-19 19:16 ` Tom Tromey [this message]
2018-04-19 19:32 ` [RFA 8/8] Let gdb.execute handle multi-line commands Eli Zaretskii
2018-04-24 16:44 ` Pedro Alves
2018-04-19 19:16 ` [RFA 3/8] Make print_command_trace varargs Tom Tromey
2018-04-24 16:43 ` Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180419191539.661-9-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).