summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Maximets <i.maximets@ovn.org>2022-07-29 16:30:28 +0200
committerIlya Maximets <i.maximets@ovn.org>2022-08-04 14:19:30 +0200
commit2b53191e15d106ecfbaabd6f9d48fca27991a316 (patch)
treec2ada7f2e7a923bc00f81a56b4c1b32d06e79d9f
parenta78680634e4e26b02d73736510480dafce70f862 (diff)
downloadopenvswitch-2b53191e15d106ecfbaabd6f9d48fca27991a316.tar.gz
dynamic-string: Add function for a sparse hex dump.
New function to dump large and sparsely populated data structures like struct flow. Reviewed-by: Roi Dayan <roid@nvidia.com> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
-rw-r--r--include/openvswitch/dynamic-string.h2
-rw-r--r--lib/dynamic-string.c36
2 files changed, 30 insertions, 8 deletions
diff --git a/include/openvswitch/dynamic-string.h b/include/openvswitch/dynamic-string.h
index ee1821710..1c262b049 100644
--- a/include/openvswitch/dynamic-string.h
+++ b/include/openvswitch/dynamic-string.h
@@ -61,6 +61,8 @@ void ds_put_printable(struct ds *, const char *, size_t);
void ds_put_hex(struct ds *ds, const void *buf, size_t size);
void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
uintptr_t ofs, bool ascii);
+void ds_put_sparse_hex_dump(struct ds *ds, const void *buf_, size_t size,
+ uintptr_t ofs, bool ascii);
int ds_get_line(struct ds *, FILE *);
int ds_get_preprocessed_line(struct ds *, FILE *, int *line_number);
int ds_get_test_line(struct ds *, FILE *);
diff --git a/lib/dynamic-string.c b/lib/dynamic-string.c
index 3b4520f87..8e9555a63 100644
--- a/lib/dynamic-string.c
+++ b/lib/dynamic-string.c
@@ -389,13 +389,9 @@ ds_put_hex(struct ds *ds, const void *buf_, size_t size)
}
}
-/* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
- * line. Numeric offsets are also included, starting at 'ofs' for the first
- * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
- * are also rendered alongside. */
-void
-ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
- uintptr_t ofs, bool ascii)
+static void
+ds_put_hex_dump__(struct ds *ds, const void *buf_, size_t size,
+ uintptr_t ofs, bool ascii, bool skip_zero_lines)
{
const uint8_t *buf = buf_;
const size_t per_line = 16; /* Maximum bytes per line. */
@@ -411,6 +407,10 @@ ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
end = start + size;
n = end - start;
+ if (skip_zero_lines && is_all_zeros(&buf[start], n)) {
+ goto next;
+ }
+
/* Print line. */
ds_put_format(ds, "%08"PRIxMAX" ",
(uintmax_t) ROUND_DOWN(ofs, per_line));
@@ -438,13 +438,33 @@ ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
ds_chomp(ds, ' ');
}
ds_put_format(ds, "\n");
-
+next:
ofs += n;
buf += n;
size -= n;
}
}
+/* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
+ * line. Numeric offsets are also included, starting at 'ofs' for the first
+ * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
+ * are also rendered alongside. */
+void
+ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
+ uintptr_t ofs, bool ascii)
+{
+ ds_put_hex_dump__(ds, buf_, size, ofs, ascii, false);
+}
+
+/* Same as 'ds_put_hex_dump', but doesn't print lines that only contains
+ * zero bytes. */
+void
+ds_put_sparse_hex_dump(struct ds *ds, const void *buf_, size_t size,
+ uintptr_t ofs, bool ascii)
+{
+ ds_put_hex_dump__(ds, buf_, size, ofs, ascii, true);
+}
+
int
ds_last(const struct ds *ds)
{