Re: [Xen-devel] [PATCH 3/3] tests/x86emul: Improve the utility of verbose mode

2018-03-09 Thread Jan Beulich
>>> On 06.03.18 at 21:24,  wrote:
> --- a/tools/tests/x86_emulator/test_x86_emulator.c
> +++ b/tools/tests/x86_emulator/test_x86_emulator.c
> @@ -16,6 +16,53 @@
>  #include "xop.h"
>  
>  #define verbose false /* Switch to true for far more logging. */
> +#define fn_width (int)(sizeof("cmpxchg") - 1)

Strictly speaking this needs another pair of parentheses. But you
can avoid this by simply moving the cast into the existing ones.

> +static const char *seg_to_str(enum x86_segment seg)
> +{
> +switch ( seg )
> +{
> +#define CASE(x) case x86_seg_ ## x: return # x
> +CASE(es);

Here and also in the other helper the indentation of the CASE()es
is one level too deep.

With these addressed
Reviewed-by: Jan Beulich 

Jan


___
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

[Xen-devel] [PATCH 3/3] tests/x86emul: Improve the utility of verbose mode

2018-03-06 Thread Andrew Cooper
 * Align the function call names, which aligns all subsequent data on the
   line.
 * Convert x86_segment and X86EMUL_ constants to strings rather than printing
   raw numbers.
 * Move the printing to the end of the function, and either hexdump the result
   or print the failure code.

No change by default as verbose is off.

Signed-off-by: Andrew Cooper 
---
CC: Jan Beulich 
---
 tools/tests/x86_emulator/test_x86_emulator.c | 99 
 1 file changed, 87 insertions(+), 12 deletions(-)

diff --git a/tools/tests/x86_emulator/test_x86_emulator.c 
b/tools/tests/x86_emulator/test_x86_emulator.c
index a764d99..6e24637 100644
--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -16,6 +16,53 @@
 #include "xop.h"
 
 #define verbose false /* Switch to true for far more logging. */
+#define fn_width (int)(sizeof("cmpxchg") - 1)
+
+static const char *seg_to_str(enum x86_segment seg)
+{
+switch ( seg )
+{
+#define CASE(x) case x86_seg_ ## x: return # x
+CASE(es);
+CASE(cs);
+CASE(ss);
+CASE(ds);
+CASE(fs);
+CASE(gs);
+CASE(tr);
+CASE(ldtr);
+CASE(gdtr);
+CASE(idtr);
+CASE(none);
+#undef CASE
+default: return "??";
+}
+}
+
+static const char *x86emul_to_str(int rc)
+{
+switch ( rc )
+{
+#define CASE(x) case X86EMUL_ ## x: return # x
+CASE(OKAY);
+CASE(UNHANDLEABLE);
+CASE(EXCEPTION);
+CASE(RETRY);
+CASE(DONE);
+CASE(UNIMPLEMENTED);
+#undef CASE
+default: return "??";
+}
+}
+
+static void hexdump_newline(const void *ptr, size_t size)
+{
+const unsigned char *p = ptr;
+
+for ( ; size; --size, ++p )
+printf(" %02x", *p);
+printf("\n");
+}
 
 static void blowfish_set_regs(struct cpu_user_regs *regs)
 {
@@ -230,9 +277,6 @@ static int read(
 
 emul_save_fpu_state();
 
-if ( verbose )
-printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
-
 switch ( seg )
 {
 uint64_t value;
@@ -292,6 +336,17 @@ static int read(
 memcpy(p_data, (void *)offset, bytes);
 
  out:
+if ( verbose )
+{
+printf("** %*s(%s, %p,, %u,) =>",
+   fn_width, __func__, seg_to_str(seg), (void *)offset, bytes);
+
+if ( rc )
+printf(" fail %s\n", x86emul_to_str(rc));
+else
+hexdump_newline(p_data, bytes);
+}
+
 emul_restore_fpu_state();
 
 return rc;
@@ -306,11 +361,15 @@ static int fetch(
 {
 emul_save_fpu_state();
 
-if ( verbose )
-printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
-
 memcpy(p_data, (void *)offset, bytes);
 
+if ( verbose )
+{
+printf("** %*s(%s, %p,, %u,) =>",
+   fn_width, __func__, seg_to_str(seg), (void *)offset, bytes);
+hexdump_newline(p_data, bytes);
+}
+
 emul_restore_fpu_state();
 
 return X86EMUL_OKAY;
@@ -327,9 +386,6 @@ static int write(
 
 emul_save_fpu_state();
 
-if ( verbose )
-printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
-
 if ( !is_x86_user_segment(seg) )
 {
 rc = X86EMUL_UNHANDLEABLE;
@@ -339,6 +395,17 @@ static int write(
 memcpy((void *)offset, p_data, bytes);
 
  out:
+if ( verbose )
+{
+printf("** %*s(%s, %p,, %u,) =>",
+   fn_width, __func__, seg_to_str(seg), (void *)offset, bytes);
+
+if ( rc )
+printf(" fail %s\n", x86emul_to_str(rc));
+else
+hexdump_newline(p_data, bytes);
+}
+
 emul_restore_fpu_state();
 
 return rc;
@@ -356,9 +423,6 @@ static int cmpxchg(
 
 emul_save_fpu_state();
 
-if ( verbose )
-printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
-
 if ( !is_x86_user_segment(seg) )
 {
 rc = X86EMUL_UNHANDLEABLE;
@@ -368,6 +432,17 @@ static int cmpxchg(
 memcpy((void *)offset, new, bytes);
 
  out:
+if ( verbose )
+{
+printf("** %*s(%s, %p,, %u,) =>",
+   fn_width, __func__, seg_to_str(seg), (void *)offset, bytes);
+
+if ( rc )
+printf(" fail %s\n", x86emul_to_str(rc));
+else
+hexdump_newline(new, bytes);
+}
+
 emul_restore_fpu_state();
 
 return rc;
-- 
2.1.4


___
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel