From patchwork Wed Jan 25 19:38:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keith Seitz X-Patchwork-Id: 63683 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 158A5385B535 for ; Wed, 25 Jan 2023 19:38:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 158A5385B535 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1674675536; bh=PHHdeo4n/CXZsYw5a7NROfBch4CqSAD7wpZgATmD74g=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=MyV1AzngYCZFeHzOD1GlSytMJKUea87N9mPsdSo4uSufdvfFbt9wM14NO1QxdsMw/ tKCd87XPLDA7BbfPwFIpM3CHDkGFcl85jeETnKQKWmubG5EIi+xP9xH2T27Pc0UiTO kn3iteo8m+0VKaFCq6qbBE/omPPRnlLLnEJHrvyg= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id AE06A3858C50 for ; Wed, 25 Jan 2023 19:38:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AE06A3858C50 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-564-zmaoraiOPoabwhwuhedIMg-1; Wed, 25 Jan 2023 14:38:27 -0500 X-MC-Unique: zmaoraiOPoabwhwuhedIMg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 22040858F0E; Wed, 25 Jan 2023 19:38:27 +0000 (UTC) Received: from guittard.uglyboxes.com (unknown [10.2.16.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id C1BE12166B26; Wed, 25 Jan 2023 19:38:26 +0000 (UTC) To: gdb-patches@sourceware.org Cc: Wendy.Peikes@netapp.com Subject: [RFC PATCH 1/2] Add $_env convenience function Date: Wed, 25 Jan 2023 11:38:24 -0800 Message-Id: <20230125193825.3665649-2-keiths@redhat.com> In-Reply-To: <20230125193825.3665649-1-keiths@redhat.com> References: <20230125193825.3665649-1-keiths@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, WEIRD_QUOTING autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Keith Seitz via Gdb-patches From: Keith Seitz Reply-To: Keith Seitz Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This patch adds a new Python convenience function which simply returns the value of the given environment variable or throws a KeyError. (gdb) p $_env("HOME") $1 = "/home/keiths" [This patch is here for the purposes of demonstarting an alternative design/implementation to a previously proposed patch. See https://sourceware.org/pipermail/gdb-patches/2022-October/192396.html .] --- gdb/data-directory/Makefile.in | 1 + gdb/python/lib/gdb/function/env.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 gdb/python/lib/gdb/function/env.py diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in index f1139291eed..3b0a05d61dc 100644 --- a/gdb/data-directory/Makefile.in +++ b/gdb/data-directory/Makefile.in @@ -107,6 +107,7 @@ PYTHON_FILE_LIST = \ gdb/function/as_string.py \ gdb/function/caller_is.py \ gdb/function/strfns.py \ + gdb/function/env.py \ gdb/printer/__init__.py \ gdb/printer/bound_registers.py diff --git a/gdb/python/lib/gdb/function/env.py b/gdb/python/lib/gdb/function/env.py new file mode 100644 index 00000000000..54a441cea54 --- /dev/null +++ b/gdb/python/lib/gdb/function/env.py @@ -0,0 +1,21 @@ +"""$_env""" + +import gdb +import os + +class _Env(gdb.Function): + """$_env - return the value of an environment variable. + + Usage: $_env("NAME") + + Returns: + Value of the environment variable named NAME or throws KeyError if NAME is + undefined in the environment.""" + + def __init__(self): + super(_Env, self).__init__("_env") + + def invoke(self, name): + return os.environ[name.string()] + +_Env() From patchwork Wed Jan 25 19:38:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keith Seitz X-Patchwork-Id: 63684 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 88C87385842E for ; Wed, 25 Jan 2023 19:38:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 88C87385842E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1674675539; bh=nmA76F2jD97wVOq68i+WmN2RADgfICWB+ZDgFJQnF2A=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=tyCHl9sVXQZImu/wnv5Viqu5JFJl/e3gf0HtxtDoh9qc2aA7goGjdbkzYNhcFWnA/ OBVTw6d8gOqmy68nfNmcV75eOxrwyhpy90UM/TkbQfAXU5JrirlCFLjo6zLFp8GG48 qZzvlGBaZZH2NsDfbpvF3CYWvOdHYS+aflCGh+O4= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id A9E6D3858D38 for ; Wed, 25 Jan 2023 19:38:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A9E6D3858D38 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-576-IcQw9X6JNqGWV2LQaFUvXw-1; Wed, 25 Jan 2023 14:38:27 -0500 X-MC-Unique: IcQw9X6JNqGWV2LQaFUvXw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9D1FB2806040; Wed, 25 Jan 2023 19:38:27 +0000 (UTC) Received: from guittard.uglyboxes.com (unknown [10.2.16.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id 483E62166B26; Wed, 25 Jan 2023 19:38:27 +0000 (UTC) To: gdb-patches@sourceware.org Cc: Wendy.Peikes@netapp.com Subject: [RFC PATCH 2/2] Allow and evaluate expressions in command arguments Date: Wed, 25 Jan 2023 11:38:25 -0800 Message-Id: <20230125193825.3665649-3-keiths@redhat.com> In-Reply-To: <20230125193825.3665649-1-keiths@redhat.com> References: <20230125193825.3665649-1-keiths@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Keith Seitz via Gdb-patches From: Keith Seitz Reply-To: Keith Seitz Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This patch adds support for arbitrary expressions to be passed to GDB commands, evaluated, and the results substituted into command arguments. This allows users to pass arbitrary expressions into commands which currently do not support any way of doing this. This is especially useful with, for example, convenience variables and functions. Example: $ export MYDIR="tmp" $ export MYFILE="simple" $ gdb -nx -q (gdb) file $($_env("HOME"))/$($_env("MYDIR"))/$($_env("MYFILE")) Reading symbols from /home/keiths/tmp/simple... (gdb) In order to mark the bounds of expressions to be evaluated, I've introduced the (arbitrary) marker "$()". Everything contained inside this marker will be passed to the expression parser, evaluated, printed as a string, and then substituted for the original expression. --- gdb/top.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/gdb/top.c b/gdb/top.c index 205eb360ba3..3f394b5b63c 100644 --- a/gdb/top.c +++ b/gdb/top.c @@ -57,6 +57,8 @@ #include "gdbsupport/pathstuff.h" #include "cli/cli-style.h" #include "pager.h" +#include "valprint.h" +#include "cp-support.h" /* for find_toplevel_char */ /* readline include files. */ #include "readline/readline.h" @@ -567,6 +569,60 @@ set_repeat_arguments (const char *args) repeat_arguments = args; } +/* Evaluate and expand any expressions in the command line list of arguments + given by ORIG_ARGS. All occurrences of "$(expr)" will be replaced with a + string representation of the evaluated EXPR. */ + +#define EVAL_START_STRING "$(" +#define EVAL_END_TOKEN ')' + +static std::string +expand_command_arg (const char *orig_arg) +{ + std::string new_arg = orig_arg; + std::string::size_type n = new_arg.find (EVAL_START_STRING); + + /* These needs adjustment to output "simple" strings. For example, + if a char value is used, we end up with "97 'a'" instead of simply "a". */ + struct value_print_options opts; + get_user_print_options (&opts); + + string_file stb; + + while (n != std::string::npos) + { + const char *args = new_arg.c_str (); + const char *c = find_toplevel_char (args + n + 2, EVAL_END_TOKEN); + + if (c != nullptr) + { + std::string expr = new_arg.substr (n + 2, c - args - n - 2); + struct value *val = parse_and_eval (expr.c_str ()); + + value_print (val, &stb, &opts); + + /* If value_print returns a quote-enclosed string, remove the + quote characters. */ + std::string r = stb.release (); + + if (*r.begin () == '\"' && *r.rbegin () == '\"') + { + r.erase (r.begin ()); + r.pop_back (); + } + + new_arg.replace (n, c - args - n + 1, r); + n = new_arg.find (EVAL_START_STRING); + } + } + +#if 0 + if (new_arg != orig_arg) + printf ("NEW_ARG = %s\n", new_arg.c_str ()); +#endif + return new_arg; +} + /* Execute the line P as a command, in the current user context. Pass FROM_TTY as second argument to the defining function. */ @@ -657,6 +713,15 @@ execute_command (const char *p, int from_tty) if (c->deprecated_warn_user) deprecated_cmd_warning (line, cmdlist); + /* Do any expression substitutions on ARG. */ + std::string expanded_arg; + + if (arg != nullptr) + { + expanded_arg = expand_command_arg (arg); + arg = expanded_arg.c_str (); + } + /* c->user_commands would be NULL in the case of a python command. */ if (c->theclass == class_user && c->user_commands) execute_user_command (c, arg);