diff mbox

[RCF,2/2] tcg: add debug helpers tcg_debug_dump_i(32|64)

Message ID 1394632200-15633-3-git-send-email-alex.bennee@linaro.org
State Superseded
Headers show

Commit Message

Alex Bennée March 12, 2014, 1:50 p.m. UTC
From: Alex Bennée <alex.bennee@linaro.org>

The helpers are useful for debugging if you want to inspect interim
values of tcg temp variables while executing TCG generated code. This is
an alternative to tracing with logs or inspecting with GDB.

The functions do take format strings but to prevent massive memory loss
it will default to a constant string after a short number of uses.

 create mode 100644 tcg/tcg-helpers.c
 create mode 100644 tcg/tcg-helpers.h
diff mbox

Patch

diff --git a/Makefile.target b/Makefile.target
index ba12340..a87b7d7 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -73,7 +73,7 @@  all: $(PROGS) stap
 #########################################################
 # cpu emulator library
 obj-y = exec.o translate-all.o cpu-exec.o
-obj-y += tcg/tcg.o tcg/optimize.o
+obj-y += tcg/tcg.o tcg/optimize.o tcg/tcg-helpers.o
 obj-$(CONFIG_TCG_INTERPRETER) += tci.o
 obj-$(CONFIG_TCG_INTERPRETER) += disas/tci.o
 obj-y += fpu/softfloat.o
diff --git a/target-arm/helper.h b/target-arm/helper.h
index 8923f8a..7600c21 100644
--- a/target-arm/helper.h
+++ b/target-arm/helper.h
@@ -507,4 +507,6 @@  DEF_HELPER_FLAGS_3(crc32c, TCG_CALL_NO_RWG_SE, i32, i32, i32, i32)
 #include "helper-a64.h"
 #endif
 
+#include "tcg/tcg-helpers.h"
+
 #include "exec/def-helper.h"
diff --git a/tcg/tcg-helpers.c b/tcg/tcg-helpers.c
new file mode 100644
index 0000000..83bfee6
--- /dev/null
+++ b/tcg/tcg-helpers.c
@@ -0,0 +1,32 @@ 
+/*
+ * TCG Common Helper Functions
+ *
+ * Copyright (c) 2014
+ * Written by Alex Bennée <alex.bennee@linaro.org>
+ *
+ * This code is licensed under the GNU GPL v2.
+ *
+ * This file contains common non-architecture specific helper
+ * functions that can be used from TCG generated code. Currently these
+ * are mainly helpful for debugging.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "cpu.h"
+#include "exec/exec-all.h"
+#include "helper.h"
+
+/* TCG Value Dumpers */
+uint32_t HELPER(dump_u32)(uint32_t val, void *string)
+{
+    fprintf(stderr,"%s %x\n", (char *) string, val);
+    return val;
+}
+
+uint64_t HELPER(dump_u64)(uint64_t val, void *string)
+{
+    fprintf(stderr,"%s %lx\n", (char *) string, val);
+    return val;
+}
diff --git a/tcg/tcg-helpers.h b/tcg/tcg-helpers.h
new file mode 100644
index 0000000..510d8b5
--- /dev/null
+++ b/tcg/tcg-helpers.h
@@ -0,0 +1,57 @@ 
+/*
+ * TCG Common Helpers
+ *
+ * Copyright (c) 2014
+ * Written by Alex Bennée <alex.bennee@linaro.org>
+ *
+ * This code is licensed under the GNU GPL v2.
+ *
+ * WARNING: intended to be included in $ARCH/helper.h
+ */
+
+DEF_HELPER_2(dump_u32, i32, i32, ptr)
+DEF_HELPER_2(dump_u64, i64, i64, ptr)
+
+
+/*
+ * Formatting macros
+ *
+ * To be useful we would like to format a string with a message to be
+ * associated with this dump. As these strings have to live the
+ * lifetime of the generated code we are essentially leaking memory so
+ * the more times a given dump call is generated the more memory is
+ * consumed (not so for each time the TCG code itself is called)
+ */
+
+/* This limits the number of malloc'ed strings *per call site* */
+#define TCG_DEBUG_DUMP_MAX_STRINGS 10
+
+/* String macro magic */
+#define STRINGIFY_DETAIL(x) #x
+#define STRINGIFY(x) STRINGIFY_DETAIL(x)
+
+#define tcg_debug_dump_i32(tcg32, fmt, ...)                               \
+do {                                                                      \
+    static int tcg_debug_alloc_strings = 0;                               \
+    gchar *debug_string = (gchar *) __FILE__ ":" STRINGIFY(__LINE__) ":"; \
+    TCGv_ptr tcg_string;                                                  \
+    if (tcg_debug_alloc_strings++ < TCG_DEBUG_DUMP_MAX_STRINGS) {         \
+        debug_string = g_strdup_printf(fmt ":", ## __VA_ARGS__);          \
+    }                                                                     \
+    tcg_string = tcg_const_ptr(debug_string);                             \
+    gen_helper_dump_u32(tcg32, tcg32, tcg_string);                        \
+    tcg_temp_free_ptr(tcg_string);                                        \
+} while (0);
+
+#define tcg_debug_dump_i64(tcg64, fmt, ...)                               \
+do {                                                                      \
+    static int tcg_debug_alloc_strings = 0;                               \
+    gchar *debug_string = (gchar *) __FILE__ ":" STRINGIFY(__LINE__) ":"; \
+    TCGv_ptr tcg_string;                                                  \
+    if (tcg_debug_alloc_strings++ < TCG_DEBUG_DUMP_MAX_STRINGS) {         \
+        debug_string = g_strdup_printf(fmt ":", ## __VA_ARGS__);          \
+    }                                                                     \
+    tcg_string = tcg_const_ptr(debug_string);                             \
+    gen_helper_dump_u64(tcg64, tcg64, tcg_string);                        \
+    tcg_temp_free_ptr(tcg_string);                                        \
+} while (0);