From: Philippe Waroquiers <philippe.waroquiers@skynet.be>
To: gdb-patches@sourceware.org
Cc: Philippe Waroquiers <philippe.waroquiers@skynet.be>
Subject: [RFAv4 3/5] Implement | (pipe) command.
Date: Thu, 30 May 2019 14:21:00 -0000 [thread overview]
Message-ID: <20190530142106.25487-4-philippe.waroquiers@skynet.be> (raw)
In-Reply-To: <20190530142106.25487-1-philippe.waroquiers@skynet.be>
The pipe command allows to run a GDB command, and pipe its output
to a shell command:
(gdb) help pipe
Send the output of a gdb command to a shell command.
Usage: | [COMMAND] | SHELL_COMMAND
Usage: | -d DELIM COMMAND DELIM SHELL_COMMAND
Usage: pipe [COMMAND] | SHELL_COMMAND
Usage: pipe -d DELIM COMMAND DELIM SHELL_COMMAND
Executes COMMAND and sends its output to SHELL_COMMAND.
The -d option indicates to use the string DELIM to separate COMMAND
from SHELL_COMMAND, in alternative to |. This is useful in
case COMMAND contains a | character.
With no COMMAND, repeat the last executed command
and send its output to SHELL_COMMAND.
(gdb)
For example:
(gdb) pipe print some_data_structure | grep -B3 -A3 something
The pipe character is defined as an alias for pipe command, so that
the above can be typed as:
(gdb) | print some_data_structure | grep -B3 -A3 something
If no GDB COMMAND is given, then the previous command is relaunched,
and its output is sent to the given SHELL_COMMAND.
This also defines convenience vars $_shell_exitcode and $_shell_exitsignal
to record the exit code and exit signal of the last shell command
launched by GDB e.g. by "shell", "pipe", ...
gdb/ChangeLog
2019-05-30 Philippe Waroquiers <philippe.waroquiers@skynet.be>
* cli/cli-cmds.c (pipe_command): New function.
(_initialize_cli_cmds): Call add_com for pipe_command.
Define | as an alias for pipe.
(exit_status_set_internal_vars): New function.
(shell_escape): Call exit_status_set_internal_vars.
cli/cli-decode.c (find_command_name_length): Recognize | as
a single character command.
---
gdb/cli/cli-cmds.c | 104 +++++++++++++++++++++++++++++++++++++++++++
gdb/cli/cli-decode.c | 4 +-
2 files changed, 106 insertions(+), 2 deletions(-)
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index daf409a558..bb2b04ddd8 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -24,6 +24,7 @@
#include "completer.h"
#include "target.h" /* For baud_rate, remote_debug and remote_timeout. */
#include "common/gdb_wait.h" /* For shell escape implementation. */
+#include "gdbcmd.h"
#include "gdb_regex.h" /* Used by apropos_command. */
#include "gdb_vfork.h"
#include "linespec.h"
@@ -41,6 +42,7 @@
#include "block.h"
#include "ui-out.h"
+#include "interps.h"
#include "top.h"
#include "cli/cli-decode.h"
@@ -668,6 +670,25 @@ echo_command (const char *text, int from_tty)
gdb_flush (gdb_stdout);
}
+/* Sets the last launched shell command convenience variables based on
+ EXIT_STATUS. */
+
+static void
+exit_status_set_internal_vars (int exit_status)
+{
+ struct internalvar *var_code = lookup_internalvar ("_shell_exitcode");
+ struct internalvar *var_signal = lookup_internalvar ("_shell_exitsignal");
+
+ clear_internalvar (var_code);
+ clear_internalvar (var_signal);
+ if (WIFEXITED (exit_status))
+ set_internalvar_integer (var_code, WEXITSTATUS (exit_status));
+ else if (WIFSIGNALED (exit_status))
+ set_internalvar_integer (var_signal, WTERMSIG (exit_status));
+ else
+ warning (_("unexpected shell command exit status %d\n"), exit_status);
+}
+
static void
shell_escape (const char *arg, int from_tty)
{
@@ -689,6 +710,7 @@ shell_escape (const char *arg, int from_tty)
/* Make sure to return to the directory GDB thinks it is, in case
the shell command we just ran changed it. */
chdir (current_directory);
+ exit_status_set_internal_vars (rc);
#endif
#else /* Can fork. */
int status, pid;
@@ -716,6 +738,7 @@ shell_escape (const char *arg, int from_tty)
waitpid (pid, &status, 0);
else
error (_("Fork failed"));
+ exit_status_set_internal_vars (status);
#endif /* Can fork. */
}
@@ -827,6 +850,70 @@ edit_command (const char *arg, int from_tty)
xfree (p);
}
+/* Implementation of the "pipe" command. */
+
+static void
+pipe_command (const char *arg, int from_tty)
+{
+ std::string delim ("|");
+
+ if (arg != nullptr && check_for_argument (&arg, "-d", 2))
+ {
+ delim = extract_arg (&arg);
+ if (delim.empty ())
+ error (_("Missing delimiter DELIM after -d"));
+ }
+
+ const char *command = arg;
+ if (command == nullptr)
+ error (_("Missing COMMAND"));
+
+ arg = strstr (arg, delim.c_str ());
+
+ if (arg == nullptr)
+ error (_("Missing delimiter before SHELL_COMMAND"));
+
+ std::string gdb_cmd (command, arg - command);
+
+ arg += delim.length (); /* Skip the delimiter. */
+
+ if (gdb_cmd.empty ())
+ {
+ repeat_previous ();
+ gdb_cmd = skip_spaces (get_saved_command_line ());
+ if (gdb_cmd.empty ())
+ error (_("No previous command to relaunch"));
+ }
+
+ const char *shell_command = skip_spaces (arg);
+ if (*shell_command == '\0')
+ error (_("Missing SHELL_COMMAND"));
+
+ FILE *to_shell_command = popen (shell_command, "w");
+
+ if (to_shell_command == nullptr)
+ error (_("Error launching \"%s\""), shell_command);
+
+ try
+ {
+ stdio_file pipe_file (to_shell_command);
+
+ execute_command_to_ui_file (&pipe_file, gdb_cmd.c_str (), from_tty);
+ }
+ catch (...)
+ {
+ pclose (to_shell_command);
+ throw;
+ }
+
+ int exit_status = pclose (to_shell_command);
+
+ if (exit_status < 0)
+ error (_("shell command \"%s\" failed: %s"), shell_command,
+ safe_strerror (errno));
+ exit_status_set_internal_vars (exit_status);
+}
+
static void
list_command (const char *arg, int from_tty)
{
@@ -1818,6 +1905,23 @@ Uses EDITOR environment variable contents as editor (or ex as default)."));
c->completer = location_completer;
+ c = add_com ("pipe", class_support, pipe_command, _("\
+Send the output of a gdb command to a shell command.\n\
+Usage: | [COMMAND] | SHELL_COMMAND\n\
+Usage: | -d DELIM COMMAND DELIM SHELL_COMMAND\n\
+Usage: pipe [COMMAND] | SHELL_COMMAND\n\
+Usage: pipe -d DELIM COMMAND DELIM SHELL_COMMAND\n\
+\n\
+Executes COMMAND and sends its output to SHELL_COMMAND.\n\
+\n\
+The -d option indicates to use the string DELIM to separate COMMAND\n\
+from SHELL_COMMAND, in alternative to |. This is useful in\n\
+case COMMAND contains a | character.\n\
+\n\
+With no COMMAND, repeat the last executed command\n\
+and send its output to SHELL_COMMAND."));
+ add_com_alias ("|", "pipe", class_support, 0);
+
add_com ("list", class_files, list_command, _("\
List specified function or line.\n\
With no argument, lists ten more lines after or around previous listing.\n\
diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index 72e2a97009..e3076f263c 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -1311,9 +1311,9 @@ find_command_name_length (const char *text)
Note that this is larger than the character set allowed when
creating user-defined commands. */
- /* Recognize '!' as a single character command so that, e.g., "!ls"
+ /* Recognize the single character commands so that, e.g., "!ls"
works as expected. */
- if (*p == '!')
+ if (*p == '!' || *p == '|')
return 1;
while (isalnum (*p) || *p == '-' || *p == '_'
--
2.20.1
next prev parent reply other threads:[~2019-05-30 14:21 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-30 14:21 [RFAv4 0/5] " Philippe Waroquiers
2019-05-30 14:21 ` Philippe Waroquiers [this message]
2019-05-30 14:21 ` [RFAv4 4/5] Test the " Philippe Waroquiers
2019-05-30 14:21 ` [RFAv4 1/5] Add previous_saved_command_line to allow a command to repeat a previous command Philippe Waroquiers
2019-05-30 14:21 ` [RFAv4 2/5] Add function execute_command_to_ui_file Philippe Waroquiers
2019-05-30 14:21 ` [RFAv4 5/5] NEWS and documentation for | (pipe) command Philippe Waroquiers
2019-05-30 15:02 ` Eli Zaretskii
2019-05-31 14:39 ` Pedro Alves
2019-05-31 14:42 ` [RFAv4 0/5] Implement " Pedro Alves
2019-05-31 15:29 ` Philippe Waroquiers
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=20190530142106.25487-4-philippe.waroquiers@skynet.be \
--to=philippe.waroquiers@skynet.be \
--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