summaryrefslogtreecommitdiff
path: root/gdbsupport
diff options
context:
space:
mode:
authorAndrew Burgess <aburgess@redhat.com>2023-03-22 17:22:51 +0000
committerAndrew Burgess <aburgess@redhat.com>2023-03-29 08:57:10 +0100
commita6e5abae4e9e845e6e69e07f755c0805558dcd63 (patch)
treebbae67ddbbf65a8717b94be9f97f9bdcf4bc5074 /gdbsupport
parent6d84a385ed96ba457bdec3dd983643dd7afa3665 (diff)
downloadbinutils-gdb-a6e5abae4e9e845e6e69e07f755c0805558dcd63.tar.gz
gdb: move displaced_step_dump_bytes into gdbsupport (and rename)
It was pointed out during review of another patch that the function displaced_step_dump_bytes really isn't specific to displaced stepping, and should really get a more generic name and move into gdbsupport/. This commit does just that. The function is renamed to bytes_to_string and is moved into gdbsupport/common-utils.{cc,h}. The function implementation doesn't really change. Much... ... I have updated the function to take an array view, which makes it slightly easier to call in a couple of places where we already have a gdb::bytes_vector. I've then added an inline wrapper to convert a raw pointer and length into an array view, which is used in places where we don't easily have a gdb::bytes_vector (or similar). Updated all users of displaced_step_dump_bytes. There should be no user visible changes after this commit. Finally, I ended up having to add an include of gdb_assert.h into array-view.h. When I include array-view.h into common-utils.h I ran into build problems because array-view.h calls gdb_assert. Approved-By: Simon Marchi <simon.marchi@efficios.com>
Diffstat (limited to 'gdbsupport')
-rw-r--r--gdbsupport/array-view.h1
-rw-r--r--gdbsupport/common-utils.cc18
-rw-r--r--gdbsupport/common-utils.h16
3 files changed, 35 insertions, 0 deletions
diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
index 3d8248b08b7..d07c8bc53fc 100644
--- a/gdbsupport/array-view.h
+++ b/gdbsupport/array-view.h
@@ -21,6 +21,7 @@
#include "traits.h"
#include <algorithm>
#include <type_traits>
+#include "gdbsupport/gdb_assert.h"
/* An array_view is an abstraction that provides a non-owning view
over a sequence of contiguous objects.
diff --git a/gdbsupport/common-utils.cc b/gdbsupport/common-utils.cc
index e382fb28d8f..4a96f2c0e11 100644
--- a/gdbsupport/common-utils.cc
+++ b/gdbsupport/common-utils.cc
@@ -445,3 +445,21 @@ hex2bin (const char *hex)
return bin;
}
+
+/* See gdbsupport/common-utils.h. */
+
+std::string
+bytes_to_string (gdb::array_view<const gdb_byte> bytes)
+{
+ std::string ret;
+
+ for (size_t i = 0; i < bytes.size (); i++)
+ {
+ if (i == 0)
+ ret += string_printf ("%02x", bytes[i]);
+ else
+ ret += string_printf (" %02x", bytes[i]);
+ }
+
+ return ret;
+}
diff --git a/gdbsupport/common-utils.h b/gdbsupport/common-utils.h
index 97dcb9fa8ce..4ceb44d88b8 100644
--- a/gdbsupport/common-utils.h
+++ b/gdbsupport/common-utils.h
@@ -24,6 +24,7 @@
#include <vector>
#include "gdbsupport/byte-vector.h"
#include "gdbsupport/gdb_unique_ptr.h"
+#include "gdbsupport/array-view.h"
#include "poison.h"
#include "gdb_string_view.h"
@@ -194,6 +195,21 @@ extern int hex2bin (const char *hex, gdb_byte *bin, int count);
/* Like the above, but return a gdb::byte_vector. */
gdb::byte_vector hex2bin (const char *hex);
+/* Build a string containing the contents of BYTES. Each byte is
+ represented as a 2 character hex string, with spaces separating each
+ individual byte. */
+
+extern std::string bytes_to_string (gdb::array_view<const gdb_byte> bytes);
+
+/* See bytes_to_string above. This takes a BUFFER pointer and LENGTH
+ rather than an array view. */
+
+static inline std::string bytes_to_string (const gdb_byte *buffer,
+ size_t length)
+{
+ return bytes_to_string ({buffer, length});
+}
+
/* A fast hashing function. This can be used to hash data in a fast way
when the length is known. If no fast hashing library is available, falls
back to iterative_hash from libiberty. START_VALUE can be set to