From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C6CC73858D20 for ; Fri, 3 Feb 2023 17:22:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C6CC73858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675444952; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=XrfmwEfwkp+6GJKc3xvS8Dp73kTZ/kE7aSdQL3SBYuE=; b=FR0MnMRhFuj/aiHhU2aaAmLCrISHezsn55eda6yyH/Ty16+v1T+YvGnRVtphnOoaoqFoYH IlhvSUKCdArX/+PoCevITvsRfOATOhqsKmJ2i10FNzkFrINwBDvSVFFRGGr56lZeKGt1gs GShsjUbBaoTTw+m2auB0UQLA5nzu2KY= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-277-9XzJ2MikOFaxgOXAhrzfAw-1; Fri, 03 Feb 2023 12:22:31 -0500 X-MC-Unique: 9XzJ2MikOFaxgOXAhrzfAw-1 Received: by mail-qv1-f72.google.com with SMTP id w2-20020a0cc702000000b0055c8ef137ddso3098766qvi.0 for ; Fri, 03 Feb 2023 09:22:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XrfmwEfwkp+6GJKc3xvS8Dp73kTZ/kE7aSdQL3SBYuE=; b=0N5AMdNH3MhYnvhUncGDVSR9TqcfYIu0dzZKh7gXmADHt6LB2TfWK8/3QXlvkb2eLJ TyFgw1bQ4684Z/3oi6vmmrk5wZ/l9hRON1b6JDQ8yVf5I8k8wtqmkVEeXqyUzBbdhVe7 1XIhk/+Jqao9TLqDorzA/5nQRw/WLa6MIKFhAYGGIaG7gzcv8IAJL9e5zIdW6C9hWXP6 Y39g7ejWRXNREzaFV3SZaOfjSAn11PCRFiG1ryobDiuWrwMhzVWKKMylqpWRkuC9LEeW 0PXT+CSKkN+wXh8VPAH6YpiUccn0p0Cw+MHqoZB4D76Mripa2VMdodblDuN9zuGuKb4U YlmQ== X-Gm-Message-State: AO0yUKW/ZR2IFAfiNMzZJbWie6xj4ZJRbjiIFYdC9TRc9ZNCgAFtKZBw K7IjZEFBAJhDaBB5u5puH/NnmWwgYJw3/5V7rNNtr5faV2IJxQO9zn4IHFJyXji96WTwNM9a/Jp oU822fxvYnqHmX9msR+JFvA== X-Received: by 2002:a05:622a:210:b0:3b6:3f4a:8154 with SMTP id b16-20020a05622a021000b003b63f4a8154mr19331502qtx.65.1675444950483; Fri, 03 Feb 2023 09:22:30 -0800 (PST) X-Google-Smtp-Source: AK7set94JiWT0jwMIIhq2CTecLVJxsKtAQpzuaRJJ6mUtLc/ypjGmINaD5Rq/71EKgO5shne8DTfNg== X-Received: by 2002:a05:622a:210:b0:3b6:3f4a:8154 with SMTP id b16-20020a05622a021000b003b63f4a8154mr19331468qtx.65.1675444950199; Fri, 03 Feb 2023 09:22:30 -0800 (PST) Received: from localhost (95.72.115.87.dyn.plus.net. [87.115.72.95]) by smtp.gmail.com with ESMTPSA id b2-20020ac86bc2000000b003b7e8c04d2esm1870182qtt.64.2023.02.03.09.22.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Feb 2023 09:22:29 -0800 (PST) From: Andrew Burgess To: Keith Seitz , gdb-patches@sourceware.org Cc: Wendy.Peikes@netapp.com Subject: Re: [RFC PATCH 2/2] Allow and evaluate expressions in command arguments In-Reply-To: <20230125193825.3665649-3-keiths@redhat.com> References: <20230125193825.3665649-1-keiths@redhat.com> <20230125193825.3665649-3-keiths@redhat.com> Date: Fri, 03 Feb 2023 17:22:28 +0000 Message-ID: <87edr6r7e3.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.7 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 List-Id: Keith Seitz via Gdb-patches writes: > 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. I really like the goal of this series, and think something like this would be awesome to add. I do worry about the extra $(..) wrapper though, that feels a little clunky, if the common case turns out to be just doing simple things like environment variables, I wonder if we could make things like: (gdb) file $_env("HOME")/$_env("BLAH") just work? We could still retain $(...) for full expression evaluation, because I guess, in theory this would allow things like: (gdb) some_gdb_command $(some_inferior_function()) thought I suspect things like this will be far less common ... maybe? Thanks, Andrew > --- > 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); > -- > 2.38.1