From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 6/9] gdb: add asserts to gdbarch_register_name
Date: Thu, 1 Sep 2022 22:31:14 +0100 [thread overview]
Message-ID: <a688d3942d63b4f5a21f38bbd44852b82f214ce1.1662067442.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1662067442.git.aburgess@redhat.com>
This commit adds asserts to gdbarch_register_name that validate the
parameters, and the return value.
The interesting thing here is that gdbarch_register_name is generated
by gdbarch.py, and so, to add these asserts, I need to update the
generation script.
I've added two new arguments for Functions and Methods (as declared in
gdbarch-components.py), these arguments are 'param_checks' and
'result_checks'. Each of these new arguments can be used to list some
expressions that are then used within gdb_assert calls in the
generated code.
The asserts that validate the API as described in the comment I added
to gdbarch_register_name a few commits back; the register number
passed in needs to be a valid cooked register number, and the result
being returned should not be nullptr.
---
gdb/gdbarch-components.py | 16 ++++++++++++++++
gdb/gdbarch.c | 6 +++++-
gdb/gdbarch.py | 19 ++++++++++++++++++-
3 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/gdb/gdbarch-components.py b/gdb/gdbarch-components.py
index fcef090c2eb..b8dc1dd36f5 100644
--- a/gdb/gdbarch-components.py
+++ b/gdb/gdbarch-components.py
@@ -99,6 +99,20 @@
# argument, and NAME is the name. Note that while the names could be
# auto-generated, this approach lets the "comment" field refer to
# arguments in a nicer way. It is also just nicer for users.
+#
+# * "param_checks" - optional, a list of strings. Each string is an
+# expression that is placed within a gdb_assert before the call is
+# made to the Function/Method implementation. Each expression is
+# something that should be true, and it is expected that the
+# expression will make use of the parameters named in 'params' (though
+# this is not required).
+#
+# * "result_checks" - optional, a list of strings. Each string is an
+# expression that is placed within a gdb_assert after the call to the
+# Function/Method implementation. Within each expression the variable
+# 'result' can be used to reference the result of the function/method
+# implementation. The 'result_checks' can only be used if the 'type'
+# of this Function/Method is not 'void'.
Info(
type="const struct bfd_arch_info *",
@@ -568,6 +582,8 @@ should never return nullptr.
type="const char *",
name="register_name",
params=[("int", "regnr")],
+ param_checks=["regnr >= 0", "regnr < gdbarch_num_cooked_regs (gdbarch)"],
+ result_checks=["result != nullptr"],
predefault="0",
invalid=True,
)
diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c
index 0edae7f6f0a..81e26362954 100644
--- a/gdb/gdbarch.c
+++ b/gdb/gdbarch.c
@@ -2229,9 +2229,13 @@ gdbarch_register_name (struct gdbarch *gdbarch, int regnr)
{
gdb_assert (gdbarch != NULL);
gdb_assert (gdbarch->register_name != NULL);
+ gdb_assert (regnr >= 0);
+ gdb_assert (regnr < gdbarch_num_cooked_regs (gdbarch));
if (gdbarch_debug >= 2)
gdb_printf (gdb_stdlog, "gdbarch_register_name called\n");
- return gdbarch->register_name (gdbarch, regnr);
+ auto result = gdbarch->register_name (gdbarch, regnr);
+ gdb_assert (result != nullptr);
+ return result;
}
void
diff --git a/gdb/gdbarch.py b/gdb/gdbarch.py
index 696b3028e6f..699ed4f69d2 100755
--- a/gdb/gdbarch.py
+++ b/gdb/gdbarch.py
@@ -112,6 +112,8 @@ class Function(_Component):
postdefault=None,
invalid=None,
printer=None,
+ param_checks=None,
+ result_checks=None,
):
super().__init__(
comment=comment,
@@ -124,6 +126,11 @@ class Function(_Component):
printer=printer,
params=params,
)
+ self.param_checks = param_checks
+ self.result_checks = result_checks
+
+ if self.type == "void" and self.result_checks:
+ raise Exception("can't have result checks with a void return type")
def ftype(self):
"Return the name of the function typedef to use."
@@ -444,6 +451,9 @@ with open("gdbarch.c", "w") as f:
f" /* Do not check predicate: {c.get_predicate()}, allow call. */",
file=f,
)
+ if c.param_checks:
+ for rule in c.param_checks:
+ print(f" gdb_assert ({rule});", file=f)
print(" if (gdbarch_debug >= 2)", file=f)
print(
f""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""",
@@ -451,8 +461,15 @@ with open("gdbarch.c", "w") as f:
)
print(" ", file=f, end="")
if c.type != "void":
- print("return ", file=f, end="")
+ if c.result_checks:
+ print("auto result = ", file=f, end="")
+ else:
+ print("return ", file=f, end="")
print(f"gdbarch->{c.name} ({c.actuals()});", file=f)
+ if c.type != "void" and c.result_checks:
+ for rule in c.result_checks:
+ print(f" gdb_assert ({rule});", file=f)
+ print(" return result;", file=f)
print("}", file=f)
print(file=f)
print("void", file=f)
--
2.25.4
next prev parent reply other threads:[~2022-09-01 21:31 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-01 21:31 [PATCH 0/9] Lots of changes to gdbarch_register_name (many architectures) Andrew Burgess
2022-09-01 21:31 ` [PATCH 1/9] gdb/testsuite: rewrite capture_command_output proc Andrew Burgess
2022-09-01 21:31 ` [PATCH 2/9] gdb/riscv: fix failure in gdb.base/completion.exp Andrew Burgess
2022-09-01 21:31 ` [PATCH 3/9] gdb/gdbarch: add a comment to gdbarch_register_name Andrew Burgess
2022-09-01 21:31 ` [PATCH 4/9] gdb: add a gdbarch_register_name self test, and fix some architectures Andrew Burgess
2022-09-01 21:31 ` [PATCH 5/9] gdb: check for duplicate register names in selftest Andrew Burgess
2022-09-01 21:31 ` Andrew Burgess [this message]
2022-09-21 18:04 ` [PATCH 6/9] gdb: add asserts to gdbarch_register_name Tom Tromey
2022-09-01 21:31 ` [PATCH 7/9] gdb/csky: remove nullptr return from csky_pseudo_register_name Andrew Burgess
2022-09-01 21:31 ` [PATCH 8/9] gdb: final cleanup of various gdbarch_register_name methods Andrew Burgess
2022-09-01 21:31 ` [PATCH 9/9] gdb: update now gdbarch_register_name doesn't return nullptr Andrew Burgess
2022-09-21 18:07 ` [PATCH 0/9] Lots of changes to gdbarch_register_name (many architectures) Tom Tromey
2022-10-02 16:28 ` Andrew Burgess
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=a688d3942d63b4f5a21f38bbd44852b82f214ce1.1662067442.git.aburgess@redhat.com \
--to=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).