public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: add a new build_id_equal function
@ 2024-05-08 15:27 Andrew Burgess
  2024-05-08 16:57 ` Andrew Burgess
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Burgess @ 2024-05-08 15:27 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

Add two versions of a new function build_id_equal which can be used to
compare build-ids, then make use of these functions in GDB.  It seems
better to have a specific function for the task of comparing build-ids
rather than having a length check followed by a memcmp call.

There should be no user visible changes after this commit.
---
 gdb/build-id.c |  3 +--
 gdb/build-id.h | 26 ++++++++++++++++++++++++++
 gdb/exec.c     |  6 ++----
 3 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/gdb/build-id.c b/gdb/build-id.c
index d1ebd8d37d7..e08a29ee1d3 100644
--- a/gdb/build-id.c
+++ b/gdb/build-id.c
@@ -62,8 +62,7 @@ build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
   if (found == NULL)
     warning (_("File \"%s\" has no build-id, file skipped"),
 	     bfd_get_filename (abfd));
-  else if (found->size != check_len
-	   || memcmp (found->data, check, found->size) != 0)
+  else if (!build_id_equal (found, check_len, check))
     warning (_("File \"%s\" has a different build-id, file skipped"),
 	     bfd_get_filename (abfd));
   else
diff --git a/gdb/build-id.h b/gdb/build-id.h
index 15bd8a964dc..c5f20f8782e 100644
--- a/gdb/build-id.h
+++ b/gdb/build-id.h
@@ -70,4 +70,30 @@ build_id_to_string (const bfd_build_id *build_id)
   return bin2hex (build_id->data, build_id->size);
 }
 
+/* Compare the content of two build-ids.  One build-id (A) is passed as a
+   build-id pointer, while the second is passed using BUILD_ID_LEN and
+   BUILD_ID_DATA.  Return true if the build-ids match, otherwise false.  */
+
+static inline bool
+build_id_equal (const bfd_build_id *a, const bfd_size_type build_id_len,
+		const bfd_byte *build_id_data)
+{
+  gdb_assert (a != nullptr);
+  gdb_assert (build_id_data != nullptr);
+
+  return (a->size == build_id_len
+	  && memcmp (a->data, build_id_data, a->size) == 0);
+}
+
+/* Like the above, but take two build-id pointers A and B.  */
+
+static inline bool
+build_id_equal (const bfd_build_id *a, const bfd_build_id *b)
+{
+  gdb_assert (a != nullptr);
+  gdb_assert (b != nullptr);
+
+  return build_id_equal (a, b->size, b->data);
+}
+
 #endif /* BUILD_ID_H */
diff --git a/gdb/exec.c b/gdb/exec.c
index 3d392b198a1..88915260d60 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -253,10 +253,8 @@ validate_exec_file (int from_tty)
 
 	  if (target_exec_file_build_id != nullptr)
 	    {
-	      if (exec_file_build_id->size == target_exec_file_build_id->size
-		  && memcmp (exec_file_build_id->data,
-			     target_exec_file_build_id->data,
-			     exec_file_build_id->size) == 0)
+	      if (build_id_equal (exec_file_build_id,
+				  target_exec_file_build_id))
 		{
 		  /* Match.  */
 		  return;

base-commit: 868883583e7520ff1bd99fcb224d2b33a990edff
-- 
2.25.4


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gdb: add a new build_id_equal function
  2024-05-08 15:27 [PATCH] gdb: add a new build_id_equal function Andrew Burgess
@ 2024-05-08 16:57 ` Andrew Burgess
  2024-05-08 17:07   ` Tom Tromey
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Burgess @ 2024-05-08 16:57 UTC (permalink / raw)
  To: gdb-patches

Andrew Burgess <aburgess@redhat.com> writes:

> Add two versions of a new function build_id_equal which can be used to
> compare build-ids, then make use of these functions in GDB.  It seems
> better to have a specific function for the task of comparing build-ids
> rather than having a length check followed by a memcmp call.
>
> There should be no user visible changes after this commit.

The initial version of this patch conflicts with the filename styling
patch that I just pushed to master.

Here's an updated version of the build-id patch, no real changes, just
resolved the merge conflict.

Thanks,
Andrew

---

commit 52a37959dfc8633d47e5aa18458d3bbf3dd53fb9
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Mon May 6 19:54:27 2024 +0100

    gdb: add a new build_id_equal function
    
    Add two versions of a new function build_id_equal which can be used to
    compare build-ids, then make use of these functions in GDB.  It seems
    better to have a specific function for the task of comparing build-ids
    rather than having a length check followed by a memcmp call.
    
    There should be no user visible changes after this commit.

diff --git a/gdb/build-id.c b/gdb/build-id.c
index 659801b0865..41667d5e5cf 100644
--- a/gdb/build-id.c
+++ b/gdb/build-id.c
@@ -63,8 +63,7 @@ build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
     warning (_("File \"%ps\" has no build-id, file skipped"),
 	     styled_string (file_name_style.style (),
 			    bfd_get_filename (abfd)));
-  else if (found->size != check_len
-	   || memcmp (found->data, check, found->size) != 0)
+  else if (!build_id_equal (found, check_len, check))
     warning (_("File \"%ps\" has a different build-id, file skipped"),
 	     styled_string (file_name_style.style (),
 			    bfd_get_filename (abfd)));
diff --git a/gdb/build-id.h b/gdb/build-id.h
index 15bd8a964dc..c5f20f8782e 100644
--- a/gdb/build-id.h
+++ b/gdb/build-id.h
@@ -70,4 +70,30 @@ build_id_to_string (const bfd_build_id *build_id)
   return bin2hex (build_id->data, build_id->size);
 }
 
+/* Compare the content of two build-ids.  One build-id (A) is passed as a
+   build-id pointer, while the second is passed using BUILD_ID_LEN and
+   BUILD_ID_DATA.  Return true if the build-ids match, otherwise false.  */
+
+static inline bool
+build_id_equal (const bfd_build_id *a, const bfd_size_type build_id_len,
+		const bfd_byte *build_id_data)
+{
+  gdb_assert (a != nullptr);
+  gdb_assert (build_id_data != nullptr);
+
+  return (a->size == build_id_len
+	  && memcmp (a->data, build_id_data, a->size) == 0);
+}
+
+/* Like the above, but take two build-id pointers A and B.  */
+
+static inline bool
+build_id_equal (const bfd_build_id *a, const bfd_build_id *b)
+{
+  gdb_assert (a != nullptr);
+  gdb_assert (b != nullptr);
+
+  return build_id_equal (a, b->size, b->data);
+}
+
 #endif /* BUILD_ID_H */
diff --git a/gdb/exec.c b/gdb/exec.c
index 3d392b198a1..88915260d60 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -253,10 +253,8 @@ validate_exec_file (int from_tty)
 
 	  if (target_exec_file_build_id != nullptr)
 	    {
-	      if (exec_file_build_id->size == target_exec_file_build_id->size
-		  && memcmp (exec_file_build_id->data,
-			     target_exec_file_build_id->data,
-			     exec_file_build_id->size) == 0)
+	      if (build_id_equal (exec_file_build_id,
+				  target_exec_file_build_id))
 		{
 		  /* Match.  */
 		  return;


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gdb: add a new build_id_equal function
  2024-05-08 16:57 ` Andrew Burgess
@ 2024-05-08 17:07   ` Tom Tromey
  2024-05-09  9:11     ` Andrew Burgess
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Tromey @ 2024-05-08 17:07 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:

Andrew> The initial version of this patch conflicts with the filename styling
Andrew> patch that I just pushed to master.

Happens all the time.

Andrew> Here's an updated version of the build-id patch, no real changes, just
Andrew> resolved the merge conflict.

Looks good.
Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gdb: add a new build_id_equal function
  2024-05-08 17:07   ` Tom Tromey
@ 2024-05-09  9:11     ` Andrew Burgess
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Burgess @ 2024-05-09  9:11 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

Tom Tromey <tom@tromey.com> writes:

>>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
>
> Andrew> The initial version of this patch conflicts with the filename styling
> Andrew> patch that I just pushed to master.
>
> Happens all the time.
>
> Andrew> Here's an updated version of the build-id patch, no real changes, just
> Andrew> resolved the merge conflict.
>
> Looks good.
> Approved-By: Tom Tromey <tom@tromey.com>

Pushed.

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-05-09  9:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-08 15:27 [PATCH] gdb: add a new build_id_equal function Andrew Burgess
2024-05-08 16:57 ` Andrew Burgess
2024-05-08 17:07   ` Tom Tromey
2024-05-09  9:11     ` Andrew Burgess

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).