From patchwork Sat Apr 20 21:21:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Waroquiers X-Patchwork-Id: 32349 Received: (qmail 45275 invoked by alias); 20 Apr 2019 21:22:10 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 45214 invoked by uid 89); 20 Apr 2019 21:22:10 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-19.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy=sends, executed, grep, UD:length X-HELO: mailsec111.isp.belgacom.be Received: from mailsec111.isp.belgacom.be (HELO mailsec111.isp.belgacom.be) (195.238.20.107) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 20 Apr 2019 21:22:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1555795328; x=1587331328; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EkbTv/TaD2AJZFm6mSSU8UGHSMAU5zG5BZYdkY6i99s=; b=xfb63mqB8XlSNeUfUC6tEGncBeKS4W7L4dyRN+WD2oYvnj8+TgdYVrY2 oj/f5chG/Zh8tHBwH7fM2MuSwC6iPw==; Received: from unknown (HELO md.home) ([109.131.123.45]) by relay.skynet.be with ESMTP/TLS/DHE-RSA-AES128-GCM-SHA256; 20 Apr 2019 23:21:59 +0200 From: Philippe Waroquiers To: gdb-patches@sourceware.org Cc: Philippe Waroquiers Subject: [RFA 2/4] Implement | (pipe) command. Date: Sat, 20 Apr 2019 23:21:51 +0200 Message-Id: <20190420212153.30934-3-philippe.waroquiers@skynet.be> In-Reply-To: <20190420212153.30934-1-philippe.waroquiers@skynet.be> References: <20190420212153.30934-1-philippe.waroquiers@skynet.be> MIME-Version: 1.0 X-IsSubscribed: yes 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: pipe [COMMAND] | SHELL_COMMAND Usage: | [COMMAND] | SHELL_COMMAND Executes COMMAND and sends its output to SHELL_COMMAND. 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. gdb/ChangeLog 2019-04-20 Philippe Waroquiers * cli/cli-cmds.c (pipe_command): New function. (_initialize_cli_cmds): Call add_com for pipe_command. Define | as an alias for pipe. cli/cli-decode.c (find_command_name_length): Recognize | as a single character command. --- gdb/cli/cli-cmds.c | 91 ++++++++++++++++++++++++++++++++++++++++++++ gdb/cli/cli-decode.c | 4 +- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c index 5f3b973f06..6964f9e1ab 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -24,6 +24,8 @@ #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 "gdbthread.h" #include "gdb_regex.h" /* Used by apropos_command. */ #include "gdb_vfork.h" #include "linespec.h" @@ -854,6 +856,86 @@ 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) +{ + FILE *to; + const char *shell_command = arg; + + if (arg == NULL) + error (_("Missing \"[COMMAND] | SHELL_COMMAND\"")); + + while (*shell_command && *shell_command != '|') + shell_command++; + + if (*shell_command != '|') + error (_("Missing | SHELL_COMMAND in \"| [COMMAND] | SHELL_COMMAND\"")); + + arg = skip_spaces (arg); + std::string gdb_cmd (arg, shell_command - arg); + + if (gdb_cmd.length () == 0) + { + repeat_previous (); + gdb_cmd = skip_spaces (saved_command_line); + if (gdb_cmd.length () == 0) + error (_("No previous command to relaunch")); + } + + shell_command++; /* skip the |. */ + shell_command = skip_spaces (shell_command); + if (*shell_command == 0) + error (_("Missing SHELL_COMMAND in \"| [COMMAND] | SHELL_COMMAND\"")); + + to = popen (shell_command, "w"); + if (to == NULL) + error (_("Error launching \"%s\""), shell_command); + + try + { + std::string gdb_cmd_result; + { + /* In case GDB_CMD switches of inferior/thread/frame, the below + restores the inferior/thread/frame. */ + scoped_restore_current_thread restore; + + gdb_cmd_result = execute_command_to_string (gdb_cmd.c_str (), + from_tty); + + if (fwrite (gdb_cmd_result.c_str (), 1, gdb_cmd_result.length (), to) + != gdb_cmd_result.length ()) + { + error (_("error writing \"%s\" result to \"%s\", errno %s"), + gdb_cmd.c_str (), shell_command, safe_strerror (errno)); + } + } + } + catch (const gdb_exception_error &ex) + { + pclose (to); + throw; + } + + int shell_command_status = pclose (to); + if (shell_command_status < 0) + error (_("shell command \"%s\" errno %s"), shell_command, + safe_strerror (errno)); + if (shell_command_status > 0) + { + if (WIFEXITED (shell_command_status)) + warning (_("shell command \"%s\" exit status %d"), shell_command, + WEXITSTATUS (shell_command_status)); + else if (WIFSIGNALED (shell_command_status)) + warning (_("shell command \"%s\" exit with signal %d"), shell_command, + WTERMSIG (shell_command_status)); + else + warning (_("shell command \"%s\" status %d"), shell_command, + shell_command_status); + } +} + static void list_command (const char *arg, int from_tty) { @@ -1845,6 +1927,15 @@ 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: pipe [COMMAND] | SHELL_COMMAND\n\ +Usage: | [COMMAND] | SHELL_COMMAND\n\ +Executes COMMAND and sends its output to SHELL_COMMAND.\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 50430953c7..46051090cd 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 == '_'