From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8PYcJAmrDmAyfgAAWB0awg (envelope-from ) for ; Mon, 25 Jan 2021 06:27:05 -0500 Received: by simark.ca (Postfix, from userid 112) id 4CA141E940; Mon, 25 Jan 2021 06:27:04 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 5CF801E940 for ; Mon, 25 Jan 2021 06:27:04 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E74F2394CC3F; Mon, 25 Jan 2021 11:27:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E74F2394CC3F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1611574023; bh=NgE3kSrmurTy8H2EUlFC1hfR+Y962Jcl8DJIVeDcOsA=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=F13mE+rlOAT6y+71yKJ77TDWaK/MOG91zev6kD0ANNXDUE+pnnKHfzqtbk5AgwbKT ic4QATuKaSTltlyU1SEJQjjGBgoqE6bl2HCOhBfgoXtYsFFeFRs1gwGD/I8qRy8k+4 89mjj9XPv4p760t3eOd9L5Z69kgtqjmV0nl1ukhc= Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id BC0F2394848E for ; Mon, 25 Jan 2021 11:27:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BC0F2394848E Received: by mail-wr1-x42d.google.com with SMTP id 7so11960980wrz.0 for ; Mon, 25 Jan 2021 03:27:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NgE3kSrmurTy8H2EUlFC1hfR+Y962Jcl8DJIVeDcOsA=; b=SdqotUdnwV4RtIwZLaAS4mfxPGK/K0v6dFTJIENG5kZ3z2AVdgZKYWJUg0KL4Uf+ki NfUvhFCkd43oYkstTvkvzBZF/90FPmHn69SNllu3/YxFqt1iMhd2trpBBBbl7QQV/49s +rrL6MqAhp7EZbEnOewsirO1LKXK5pH4m//NuFOZ68B9CdyumDB0HkmUjlFrt3R/S2pe QdTrp/pyHTL/Dh6tSq/ROq8t0l50aH/UX+x3YE3fKAeALWeGMN4ADuMrE2G++1BFfcH2 bJCCNxP1kNVV116peiedCWCA3HA7JtVsA0gbbZYuNZHA/fMogIbsJqoR582uAdTYL4re I2eQ== X-Gm-Message-State: AOAM531HGbhxQi42L/27ofUMeyT2ixEbH9i6pVWy8CGpiFqKXozPiJrV w+3Kccp6MQhda/7qd5e1C6A+fXWbP2CYKCp67mQ2x9IKkXYUja1pwElMZ5FXMGbVtr2ijmVIPZW 4HGvvUZGosRsDuloCF3QUwgSqXzT/yvclW3gNomnNyu0U5jTSkbmsXW8DtNDdlX6KgOPZV33/Gx qo X-Google-Smtp-Source: ABdhPJxZASJ5zDcvMvQdDEOkkMXgjffZE4ys2wpA9wEEMvlZcjp0kpOOD37ro+UVs1KdNgloOf7j7g== X-Received: by 2002:a5d:6311:: with SMTP id i17mr377206wru.195.1611574019420; Mon, 25 Jan 2021 03:26:59 -0800 (PST) Received: from focaccia.undoers.io (cpc159317-cmbg20-2-0-cust151.5-4.cable.virginm.net. [81.111.29.152]) by smtp.gmail.com with ESMTPSA id q2sm20648220wma.6.2021.01.25.03.26.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Jan 2021 03:26:58 -0800 (PST) To: gdb-patches@sourceware.org Subject: [PATCH v2 2/5] gdb: prevent prefix commands from being hooks Date: Mon, 25 Jan 2021 11:26:46 +0000 Message-Id: <20210125112649.56362-3-mbarisione@undo.io> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210125112649.56362-1-mbarisione@undo.io> References: <20210108100706.96190-1-mbarisione@undo.io> <20210125112649.56362-1-mbarisione@undo.io> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , From: Marco Barisione via Gdb-patches Reply-To: Marco Barisione Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" Currently it's possible for hooks to be prefix commands: define-prefix hook-run define hook-run echo Will run!\n end define hook-run subcommand echo Subcommand\n end Then: (gdb) run Will run! [...] (gdb) hook-run subcommand Subcommand This doesn't seem very useful or worse, it can be confusing. Moreover, this creates some obscure corner cases in a later patch adding command renaming. Because of this, this patch prevents hooks to become prefixes and prefixes to become hooks. An alternative approach would have been to prevent prefixes starting with "hook-" or "hookpost-". The end result would have been similar but not identical as GDB allows commands starting with "hook-" or "hookpost-" which are not hooks (for instance, "define hook-this-doesnt-exist"). To be more consistent with this use case, prefixes for commands which are named like hooks but are not hooks are allowed. gdb/ChangeLog: * cli/cli-script.c (do_define_command): Prevent prefix commands from becoming hooks. (define_prefix_command): Prevent hooks from becoming prefix commands. gdb/testsuite/ChangeLog: * gdb.base/define-prefix.exp: Test that prefix commands cannot become hooks and vice versa. --- gdb/cli/cli-script.c | 25 ++++++++++++++++++------ gdb/testsuite/gdb.base/define-prefix.exp | 24 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c index 0544f3efb1b..e197852bc71 100644 --- a/gdb/cli/cli-script.c +++ b/gdb/cli/cli-script.c @@ -1455,6 +1455,11 @@ do_define_command (const char *comname, int from_tty, if (!query (_("Proceed? "))) error (_("Not confirmed.")); } + + if (c != nullptr && c->prefixlist != nullptr) + error (_("Command \"%s\" cannot become a hook as it's already a " + "prefix command."), + comfull); } comname = xstrdup (comname); @@ -1594,13 +1599,21 @@ define_prefix_command (const char *comname, int from_tty) c = lookup_cmd_exact (comname, *list); - if (c != nullptr && c->theclass != class_user) - error (_("Command \"%s\" is built-in."), comfull); - - if (c != nullptr && c->prefixlist != nullptr) + if (c != nullptr) { - /* c is already a user defined prefix command. */ - return; + if (c->theclass != class_user) + error (_("Command \"%s\" is built-in."), comfull); + + if (c->prefixlist != nullptr) + { + /* C is already a user defined prefix command. */ + return; + } + + if (c->hookee_pre != nullptr || c->hookee_post != nullptr) + error (_("Command \"%s\" cannot be a prefix command as it's " + "already a hook."), + comfull); } /* If the command does not exist at all, create it. */ diff --git a/gdb/testsuite/gdb.base/define-prefix.exp b/gdb/testsuite/gdb.base/define-prefix.exp index 71369caaed6..a8ce5765ec7 100644 --- a/gdb/testsuite/gdb.base/define-prefix.exp +++ b/gdb/testsuite/gdb.base/define-prefix.exp @@ -162,3 +162,27 @@ gdb_test "define-prefix something-not-existing something-else" \ gdb_test "define-prefix abc-prefix something-not-existing something-else" \ "Undefined abc-prefix command: \"something-not-existing\".*" +#################### +# Check error behaviour when interacting with hooks. + +# Define a command and hooks into it. +gdb_define_cmd "hookee1" {} +gdb_define_cmd "hook-hookee1" {} +gdb_define_cmd "hookpost-hookee1" {} + +# Check that making the hooks into prefix commands is not allowed. +gdb_test_exact "define-prefix hook-hookee1" \ + "Command \"hook-hookee1\" cannot be a prefix command as it's already a hook." +gdb_test_exact "define-prefix hookpost-hookee1" \ + "Command \"hookpost-hookee1\" cannot be a prefix command as it's already a hook." + +# Define a command and prefixes which look like hooks into it (but they +# are not). +gdb_define_cmd "hookee2" {} +gdb_test_no_output "define-prefix hook-hookee2" {} +gdb_test_no_output "define-prefix hookpost-hookee2" {} + +gdb_test_exact "define hook-hookee2" \ + "Command \"hook-hookee2\" cannot become a hook as it's already a prefix command." +gdb_test_exact "define hookpost-hookee2" \ + "Command \"hookpost-hookee2\" cannot become a hook as it's already a prefix command." -- 2.28.0