From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 0EPGB+ou+F+2JwAAWB0awg (envelope-from ) for ; Fri, 08 Jan 2021 05:07:38 -0500 Received: by simark.ca (Postfix, from userid 112) id 1A1DA1E99A; Fri, 8 Jan 2021 05:07:38 -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 BC5AB1E4F4; Fri, 8 Jan 2021 05:07:37 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 36AFD397301E; Fri, 8 Jan 2021 10:07:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 36AFD397301E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1610100457; 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=Cq2iI3UTvw2LL04IYNQ9EoRNPdTwhUADTkC/C3Kfhk2ZEwAKzaGvZycnr4iJYQE+N Yi/7nsjDh4TptotQBJnUe0krGQhiLB6O+kWXRgfIUOeWwTCqpVJddbM71SaMSN7jdd 8RwNxCpfmiPhZkjgSRh4AX7QKD7DqvxF5NFabnbc= Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id C8B9B3858025 for ; Fri, 8 Jan 2021 10:07:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C8B9B3858025 Received: by mail-wm1-x331.google.com with SMTP id n16so6420781wmc.0 for ; Fri, 08 Jan 2021 02:07:34 -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=iQwnbAaivC3u1bXBu18WM5pwU19n4Igk8qhYcSMX3mAnb+u0lQRsa6rV/dzq5WoZmr bdqS3xZqryfOjSQx8mNIdN6XluR1TS1QV1aDM1Rkskl+TPldlfM7CHReZJrJ9fP7Z7ae xh95ger62mLJ7S25A+KmSCVGAvopC1OuOusDVXfGfYogk4OyjIaWLFKqJo+cks8YHFzy zN0TB/LdOEpzfaLeEHfAVO4xTY4hXUK9Wg55aYj0psRG7xrq49e5xbERpox8ykBbB1MK +yklwKaSXJi+rqSIOs3lfyJOEoQRD0K0SmU5+iT0gssRA2CD/+VrQzD0kS1mD+uo31xb +PsA== X-Gm-Message-State: AOAM531gVSDWUwyAUatLVN+aaCTEj0fx6vuVpBeNMBLr1oW5YTDFIN43 +wBVUAMQJONPfvIxAfz90M6wcKMczDCCITqEvzRS11zfh2qkPTL/h517yFVY9J5a83BEmrl6xyF NTEFfoAxD0BhD2UrwTu0Bpcp/I4vMSXuRxn9JPnL/SlVSYmVb1QMtlW0J4YmXun2klg/kv2CRPV Ub X-Google-Smtp-Source: ABdhPJwobedCAn0NU52kSUM5f4EATLM+MAqB+2RJCbkv7ukEp61eAcbQ13z83AaAmiA5K/zTNocWwQ== X-Received: by 2002:a7b:c8da:: with SMTP id f26mr2381540wml.50.1610100453350; Fri, 08 Jan 2021 02:07:33 -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 s205sm11579143wmf.46.2021.01.08.02.07.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Jan 2021 02:07:32 -0800 (PST) To: gdb-patches@sourceware.org Subject: [PATCH 2/4] gdb: prevent prefix commands from being hooks Date: Fri, 8 Jan 2021 10:07:04 +0000 Message-Id: <20210108100706.96190-3-mbarisione@undo.io> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210108100706.96190-1-mbarisione@undo.io> References: <20210108100706.96190-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