From: Marco Barisione via Gdb-patches <gdb-patches@sourceware.org>
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 [thread overview]
Message-ID: <20210514203824.97894-1-mbarisione@undo.io> (raw)
In-Reply-To: <1f3b725d-4b0a-d9ab-7553-24b52b3ff256@polymtl.ca>
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
next prev parent reply other threads:[~2021-05-14 20:38 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-08 10:07 [PATCH 0/4] Add support for command renaming Marco Barisione via Gdb-patches
2021-01-08 10:07 ` [PATCH 1/4] gdb: add lookup_cmd_exact to simplify a common pattern Marco Barisione via Gdb-patches
2021-01-10 0:06 ` Lancelot SIX via Gdb-patches
2021-01-17 10:47 ` Marco Barisione via Gdb-patches
2021-01-17 19:02 ` Lancelot SIX via Gdb-patches
2021-01-25 11:33 ` Luis Machado via Gdb-patches
2021-01-08 10:07 ` [PATCH 2/4] gdb: prevent prefix commands from being hooks Marco Barisione via Gdb-patches
2021-01-08 10:07 ` [PATCH 3/4] gdb: update the docs for add_cmd and do_add_cmd to match reality Marco Barisione via Gdb-patches
2021-01-08 10:07 ` [PATCH 4/4] gdb: Add support for renaming commands Marco Barisione via Gdb-patches
2021-01-08 10:30 ` Eli Zaretskii via Gdb-patches
2021-01-25 11:26 ` [PATCH v2 0/5] Add support for command renaming Marco Barisione via Gdb-patches
2021-01-25 11:26 ` [PATCH v2 1/5] gdb: add lookup_cmd_exact to simplify a common pattern Marco Barisione via Gdb-patches
2021-03-08 18:58 ` Simon Marchi
2021-05-07 14:47 ` Marco Barisione via Gdb-patches
2021-01-25 11:26 ` [PATCH v2 2/5] gdb: prevent prefix commands from being hooks Marco Barisione via Gdb-patches
2021-03-08 21:32 ` Simon Marchi via Gdb-patches
2021-03-09 9:42 ` Marco Barisione via Gdb-patches
2021-03-16 3:17 ` Simon Marchi via Gdb-patches
2021-05-07 14:59 ` Marco Barisione via Gdb-patches
2021-05-07 19:30 ` Simon Marchi via Gdb-patches
2021-05-07 20:11 ` Marco Barisione via Gdb-patches
2021-05-14 20:38 ` Marco Barisione via Gdb-patches [this message]
2021-01-25 11:26 ` [PATCH v2 3/5] gdb: update the docs for add_cmd and do_add_cmd to match reality Marco Barisione via Gdb-patches
2021-03-08 22:52 ` Simon Marchi via Gdb-patches
2021-03-08 23:10 ` Simon Marchi via Gdb-patches
2021-05-14 20:39 ` [PATCH v3 3/5] gdb: move declarations and docs for cli-decode.c to cli-decode.h Marco Barisione via Gdb-patches
2021-01-25 11:26 ` [PATCH v2 4/5] gdb: generate the prefix name for prefix commands on demand Marco Barisione via Gdb-patches
2021-03-08 23:25 ` Simon Marchi via Gdb-patches
2021-03-16 17:00 ` Simon Marchi via Gdb-patches
2021-05-12 11:10 ` Marco Barisione via Gdb-patches
2021-01-25 11:26 ` [PATCH v2 5/5] gdb: Add support for renaming commands Marco Barisione via Gdb-patches
2021-03-23 18:45 ` Simon Marchi via Gdb-patches
2021-05-14 20:41 ` [PATCH v3 5/5] gdb: add " Marco Barisione via Gdb-patches
2021-02-08 17:53 ` [PING] [PATCH v2 0/5] Add support for command renaming Marco Barisione via Gdb-patches
2021-02-15 8:27 ` [PING2] " Marco Barisione via Gdb-patches
2021-02-22 8:28 ` [PING 3] " Marco Barisione via Gdb-patches
2021-03-01 8:32 ` [PING 4] " Marco Barisione via Gdb-patches
2021-03-08 9:23 ` [PING 5] " Marco Barisione via Gdb-patches
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210514203824.97894-1-mbarisione@undo.io \
--to=gdb-patches@sourceware.org \
--cc=mbarisione@undo.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox