Subject: [PATCH] [FEAT VS2038] pvattest: Improve logging
From: Steffen Eiden <seiden@linux.ibm.com>

Summary:     pvattest: Create, perform, and verify attestation measurements
Description: pvattest is a tool to attest an IBM Secure Execution guest.
             
             In a trusted environment, one can create a request using
             `pvattest create`. To get a measurement of an untrusted
             IBM Secure Execution guest call 'pvattest perform'.
             Again in a trusted environment, call 'pvattest verify'
             to verify that the measurement is the expected one.
             
             The tool runs on s390 and x86.
             It has the same requirements like libpv and therefore
             requires openssl v1.1.1+, glib2.56+, and libcurl.
             Additionally, to measure, the linux kernel must provide
             the Ultravisor userspace interface `uvdevice` at /dev/uv
             and must be executed  on an IBM Secure Execution guest on
             hardware with Ultravisor attestation support,
             like IBM z16 or later.
Upstream-ID: 972d4fe0a5503ab2191cef4644da406d01239769
Problem-ID:  VS2038

Upstream-Description:

             pvattest: Improve logging

             Change the name of `hexdump´ to `pvattest_log_bytes´. Additionally, allow
             that function to dump the bytes as a single hexadecimal number.

             Change the name of `printf_hexdump´ to `pvattest_hexdump´.

             Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
             Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
             Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>


Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
 pvattest/src/exchange_format.c |    4 ++--
 pvattest/src/log.c             |   29 +++++++++++++++++++----------
 pvattest/src/log.h             |   30 +++++++++++++++---------------
 pvattest/src/pvattest.c        |    2 +-
 4 files changed, 37 insertions(+), 28 deletions(-)

--- a/pvattest/src/exchange_format.c
+++ b/pvattest/src/exchange_format.c
@@ -455,8 +455,8 @@ static void print_entry(const char *name
 	fprintf(stream, _("%s (%#lx bytes)"), name, g_bytes_get_size(data));
 	if (print_data) {
 		fprintf(stream, ":\n");
-		printf_hexdump(g_bytes_get_data(data, NULL), g_bytes_get_size(data), 16, "      ",
-			       stream);
+		pvattest_hexdump(g_bytes_get_data(data, NULL), g_bytes_get_size(data), 16, "      ",
+				 stream);
 	}
 	fprintf(stream, "\n");
 }
--- a/pvattest/src/log.c
+++ b/pvattest/src/log.c
@@ -131,26 +131,35 @@ void pvattest_log_plain_logger(const cha
 	_log_logger(level, message, log_level, FALSE, "");
 }
 
-void hexdump(const void *data, size_t size, size_t len, const char *prefix, GLogLevelFlags log_lvl)
+void pvattest_log_bytes(const void *data, size_t size, size_t width, const char *prefix,
+			gboolean beautify, GLogLevelFlags log_lvl)
 {
 	const uint8_t *data_b = data;
 
 	pv_wrapped_g_assert(data);
 
-	g_log(PVATTEST_HEXDUMP_LOG_DOMAIN, log_lvl, "%s0x0000  ", prefix);
+	if (beautify)
+		g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "%s0x0000  ", prefix);
+	else
+		g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "%s", prefix);
 	for (size_t i = 0; i < size; i++) {
-		g_log(PVATTEST_HEXDUMP_LOG_DOMAIN, log_lvl, "%02x", data_b[i]);
-		if (i % 2 == 1)
-			g_log(PVATTEST_HEXDUMP_LOG_DOMAIN, log_lvl, " ");
+		g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "%02x", data_b[i]);
+		if (i % 2 == 1 && beautify)
+			g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, " ");
 		if (i == size - 1)
 			break;
-		if (i % len == len - 1)
-			g_log(PVATTEST_HEXDUMP_LOG_DOMAIN, log_lvl, "\n%s0x%04lx  ", prefix, i + 1);
+		if (i % width == width - 1) {
+			if (beautify)
+				g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "\n%s0x%04lx  ", prefix,
+				      i + 1);
+			else
+				g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "\n%s", prefix);
+		}
 	}
-	g_log(PVATTEST_HEXDUMP_LOG_DOMAIN, log_lvl, "\n");
+	g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "\n");
 }
 
-void printf_hexdump(const void *data, size_t size, size_t len, const char *prefix, FILE *stream)
+void pvattest_hexdump(const void *data, size_t size, size_t width, const char *prefix, FILE *stream)
 {
 	const uint8_t *data_b = data;
 
@@ -164,7 +173,7 @@ void printf_hexdump(const void *data, si
 			fprintf(stream, " ");
 		if (i == size - 1)
 			break;
-		if (i % len == len - 1)
+		if (i % width == width - 1)
 			fprintf(stream, "\n%s0x%04lx  ", prefix, i + 1);
 	}
 	fprintf(stream, "\n");
--- a/pvattest/src/log.h
+++ b/pvattest/src/log.h
@@ -23,7 +23,7 @@
 #define PVATTEST_LOG_LVL_DEFAULT PVATTEST_LOG_LVL_WARNING
 #define PVATTEST_LOG_LVL_MAX PVATTEST_LOG_LVL_DEBUG
 
-#define PVATTEST_HEXDUMP_LOG_DOMAIN "pvattest_hdump"
+#define PVATTEST_BYTES_LOG_DOMAIN "pvattest_bytes"
 
 void pvattest_log_increase_log_lvl(int *log_lvl);
 void pvattest_log_error(const char *format, ...);
@@ -45,22 +45,22 @@ void pvattest_log_default_logger(const c
  */
 void pvattest_log_plain_logger(const char *log_domain, GLogLevelFlags level, const char *message,
 			       void *user_data);
-#define dhexdump(v, s)                                                            \
-	{                                                                         \
-		pvattest_log_debug("%s (%li byte):", #v, s);                      \
-		hexdump(v, s, 16L, "    ", PVATTEST_LOG_LVL_DEBUG);               \
-		g_log(PVATTEST_HEXDUMP_LOG_DOMAIN, PVATTEST_LOG_LVL_DEBUG, "\n"); \
-	}
-#define gbhexdump(v)                                                                 \
+#define dhexdump(v, s)                                                               \
 	{                                                                            \
-		pvattest_log_debug("%s:(%li byte):", #v, g_bytes_get_size(v));       \
-		hexdump(g_bytes_get_data(v, NULL), g_bytes_get_size(v), 16L, "    ", \
-			PVATTEST_LOG_LVL_DEBUG);                                     \
-		g_log(PVATTEST_HEXDUMP_LOG_DOMAIN, PVATTEST_LOG_LVL_DEBUG, "\n");    \
+		pvattest_log_debug("%s (%li byte):", #v, s);                         \
+		pvattest_log_bytes(v, s, 16L, "    ", TRUE, PVATTEST_LOG_LVL_DEBUG); \
+		g_log(PVATTEST_BYTES_LOG_DOMAIN, PVATTEST_LOG_LVL_DEBUG, "\n");      \
+	}
+#define gbhexdump(v)                                                                            \
+	{                                                                                       \
+		pvattest_log_debug("%s:(%li byte):", #v, g_bytes_get_size(v));                  \
+		pvattest_log_bytes(g_bytes_get_data(v, NULL), g_bytes_get_size(v), 16L, "    ", \
+				   TRUE, PVATTEST_LOG_LVL_DEBUG);                               \
+		g_log(PVATTEST_BYTES_LOG_DOMAIN, PVATTEST_LOG_LVL_DEBUG, "\n");                 \
 	}
-void hexdump(const void *data, size_t size, size_t len, const char *prefix, GLogLevelFlags log_lvl)
-	PV_NONNULL(1);
-void printf_hexdump(const void *data, size_t size, size_t len, const char *prefix, FILE *stream)
+void pvattest_log_bytes(const void *data, size_t size, size_t width, const char *prefix,
+			gboolean beautify, GLogLevelFlags log_lvl) PV_NONNULL(1);
+void pvattest_hexdump(const void *data, size_t size, size_t width, const char *prefix, FILE *stream)
 	PV_NONNULL(1, 5);
 void pvattest_log_GError(const char *info, GError *error) PV_NONNULL(1);
 
--- a/pvattest/src/pvattest.c
+++ b/pvattest/src/pvattest.c
@@ -355,7 +355,7 @@ int main(int argc, char *argv[])
 	 * message)to filter messages based on the log level specified by the
 	 * user.
 	 */
-	g_log_set_handler(PVATTEST_HEXDUMP_LOG_DOMAIN,
+	g_log_set_handler(PVATTEST_BYTES_LOG_DOMAIN,
 			  G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
 			  &pvattest_log_plain_logger, &appl_log_lvl);
 
