From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id gQruL9jfnmAOJwAAWB0awg (envelope-from ) for ; Fri, 14 May 2021 16:38:48 -0400 Received: by simark.ca (Postfix, from userid 112) id B68B41F11C; Fri, 14 May 2021 16:38:48 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [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 DE43C1E783 for ; Fri, 14 May 2021 16:38:47 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 67AA83851C04; Fri, 14 May 2021 20:38:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 67AA83851C04 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1621024727; bh=a9VALX5iGlJ4PAtw8dFgmwcaCYxHpKgz1lANqLDzUhk=; 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=vfmCbf3shCunZf4ld48HNxgaPR1VJcibVlBKQA3VlL8lQlTJfxjVqx8uiIev+1rla r34jR0R6KNFBWMic0x8Y/+k9izGr9u/dGGNj4OHTd+l+H7gK5Nadk3ypfmLOHbfhg9 tRTtqQfOfzxyE0zsCrKnV3pX6LwxbMhWETcChaEc= 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 D7016385E454 for ; Fri, 14 May 2021 20:38:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D7016385E454 Received: by mail-wm1-x331.google.com with SMTP id f75-20020a1c1f4e0000b0290171001e7329so354038wmf.1 for ; Fri, 14 May 2021 13:38:44 -0700 (PDT) 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=a9VALX5iGlJ4PAtw8dFgmwcaCYxHpKgz1lANqLDzUhk=; b=BA/AjoJRnlb1ImS0XEqXCTmglJ5/gZwBR9C4djaATw6Zj5AtUII3AjhJq6vN00fOqC 0J6jbzEW4+t9mwz0bLIX3dK6cEcLl9X2fdMkORuaR7RnND05UrF6ueAyCd4fAycRGbRi 93PL6IvGDhjc5jvLPyW7krC6xpmwqNhzylHtu1q5/hELEtqoMyxmp9sZOtQIBV8C9nyZ szAvk8KVVoZLNWX64VKNj6v20gg2MT4Zg89Ovp9ygcCVPY8zZKxowsxFceKuFcJxu6yc BuAM0PHoDqb+W0PDh9ec/h7bdcZodXWNSk6Doozwl2sbwAQ06BGsQW+PKN1WxT/Fe2pe tN4g== X-Gm-Message-State: AOAM530dePBl7MRjPn0nl1+hiWxez2qTfxDQ402SVQFjHLNfm63SqKCy 2cxlvcXwjY3CUEmmLXIe11M+uNGYiJA8tNYJaiOT7j/74iUoPeA8LREugyVcM1keG5A3UVQ17i2 SoaGiW4T3in70ja+i4fpkURnEYBprQTXB35qRxFNLHhdKP42o4d7Cc9r5XhA6qjzEPKy0UI5kuQ == X-Google-Smtp-Source: ABdhPJwb4bL2RiXsm++yTWzbGU/x31jregGkhDqh9gz88LuKyLoz0kpExbYBeij/yQDWVJm8faaQTg== X-Received: by 2002:a1c:4d05:: with SMTP id o5mr10656229wmh.131.1621024723115; Fri, 14 May 2021 13:38:43 -0700 (PDT) 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 j13sm8601529wrd.81.2021.05.14.13.38.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 May 2021 13:38:42 -0700 (PDT) To: gdb-patches@sourceware.org Subject: [PATCH v3 2/5] gdb: prevent prefix commands from being hooks Date: Fri, 14 May 2021 21:38:24 +0100 Message-Id: <20210514203824.97894-1-mbarisione@undo.io> X-Mailer: git-send-email 2.28.0 In-Reply-To: <1f3b725d-4b0a-d9ab-7553-24b52b3ff256@polymtl.ca> References: <1f3b725d-4b0a-d9ab-7553-24b52b3ff256@polymtl.ca> 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 eb8853a5e64..5841f545882 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); @@ -1592,13 +1597,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