Skip to contentSkip to conversation

trace-gen: support for trace points in code-generation routines

RFC PATCH New Lluís 1 participant 5 messages Nov 4, 2010 Kbuild

lore
Share & tools
lore

Lluís

author
#0 lore raw

v2 cover trace-gen: support for trace points in code-generation routines

XXX: Must be applied on top of the 'trace-instrument' patch series

Adds a new optional keyword ("gen") to the syntax in "trace-events". When specified, this event property will generate an extra set of functions that provide the ability to inject trace points into TCG-generated code. Note that the regular backend-dependant functions will also be generated.

The extra files generated, and their functions are:

- trace-helper.h
Declares a 'trace_proxy_##name' TCG helper for each affected trace event.

- trace-helper.c
Defines a 'trace_proxy_##name' TCG helper for each affected trace event, which simply calls 'trace_##name'.
Note that this scheme neatly blends with the "instrument" property.

- trace-gen.h
Defines a 'trace_gen_##name##_backend' function for each affected trace event, which handles all the (de)allocation of TCG temporaries and calls 'gen_helper_trace_proxy_##name'.
If the "instrument" property is not present, 'trace_gen_##name' is defined as an alias.
This file is instended to be included instead of "trace.h" for generating TCG calls to trace points.

In order to fully exploit the information available during TCG code generation, trace events with the "gen" property can also use TCG data types (e.g., TCG_ptr), which are gracefully handled by 'tracetool' when generating the "trace.h" and "trace.c" files.

v2:

Rebase onto latest QEMU version (d33ea50a958b2e050d2b28e5f17e3b55e91c6d74).

Handle trace events with an explicit "void" to indicate no arguments.

TODO:

* Check handling of TCGv_tl and target_long/target_ulong
* Should not emit code in 'trace_gen_##name' if event has property "disable" (relates to previous' series comment on "disable" vs "simple").
* Check per-cpu event tracing state before calling 'gen_helper_trace_proxy_##name' (upcoming series)

---

Lluís Vilanova (4):
trace-gen: gracefully handle TCG types in "trace-events"
trace-gen: auto-generate TCG helper routines for tracing
trace-gen: [all] include "trace-helper.h" on all "helper.h" files
trace-gen: auto-generate wrappers to call TCG trace helpers

 .gitignore                 |    2 
 Makefile                   |   21 ++
 Makefile.target            |    5 -
 configure                  |    5 +
 simpletrace.py             |    2 
 target-alpha/helper.h      |    2 
 target-arm/helper.h        |    2 
 target-cris/helper.h       |    2 
 target-i386/helper.h       |    2 
 target-m68k/helper.h       |    2 
 target-microblaze/helper.h |    2 
 target-mips/helper.h       |    2 
 target-ppc/helper.h        |    2 
 target-sh4/helper.h        |    2 
 target-sparc/helper.h      |    2 
 trace-events               |    8 +
 tracetool                  |  378 +++++++++++++++++++++++++++++++++++++++++++-
 17 files changed, 430 insertions(+), 11 deletions(-)
author
#1 lore raw

PATCH v2 1/4 trace-gen: gracefully handle TCG types in "trace-events"

Some trace events with the "gen" property will need to use TCG arguments.

As such, modify 'tracetool' to gracefully handle these types and let the backend code transliterate these types into their native counterpart.

---
 tracetool |  137 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 132 insertions(+), 5 deletions(-)
tracetool +132 -5
index f9098da..6f48215 100755
@@ -38,13 +38,74 @@ get_api_name()
     echo "$event"
 }
 
+# Convenience function to pair elements of two lists (of comma-separated values) of equal size
+# $1: list
+# $2: list
+# $3: format (defaults to ", $1 $2")
+zip_lists()
+{
+    [ -n "$1" -a -n "$2" ] || return
+
+    local format
+    format=$3
+    [ -n "$format" ] || format=", %s %s"
+
+    local i elem accum
+    i=1
+    accum=""
+    for elem in $1; do
+        if [ "$elem" = "${elem%,}" ]; then
+            accum="$accum $elem"
+        else
+            accum="$accum ${elem%,}"
+            eval __elem_$i=\"$accum\"
+            i=$(($i + 1))
+            accum=""
+        fi
+    done
+    eval __elem_$i=\"$accum\"
+
+    local tmp res
+    accum=""
+    res=""
+    i=1
+    for elem in $2; do
+        if [ "$elem" = "${elem%,}" ]; then
+            accum="$accum $elem"
+        else
+            accum="$accum ${elem%,}"
+            eval tmp=\$__elem_$i
+            tmp=$(printf "$format" "$tmp" "$accum")
+            res="$res$tmp"
+            i=$(($i + 1))
+            accum=""
+        fi
+    done
+    eval tmp=\$__elem_$i
+    tmp=$(printf "$format" "$tmp" "$elem")
+    res="$res$tmp"
+
+    echo $res
+}
+
 # Get the argument list of a trace event, including types and names
+# If given, the second argument is a function name to map on each element of the
+# comma-separated list
 get_args()
 {
     local args
     args=${1#*\(}
     args=${args%\)*}
-    echo "$args"
+
+    if [ -z "$2" -o "$args" = "void" ]; then
+        echo "$args"
+    else
+        local argtypes argnames res
+        argtypes=$(get_argtypes "$1" $2)
+        argnames=$(get_argnames "$1")
+        res=$(zip_lists "$argtypes" "$argnames")
+        echo "${res#, }"
+    fi
 }
 
 # Get the argument name list of a trace event
@@ -72,6 +133,60 @@ get_argnames()
     fi
 }
 
+# Get the argument type list of a trace event
+# If given, the second argument is a function name to map on each element of the
+# comma-separated list
+get_argtypes()
+{
+    if [ -z "$2" ]; then
+        local res elem accum
+        res=""
+        accum=""
+        for elem in $(get_args "$1"); do
+            if [ "${elem}" = "${elem%,}" ]; then
+                accum="$accum $elem"
+            else
+                [ "$elem" = "${elem#\*}" ] || accum="$accum *"
+                res="$res, $accum"
+                accum=""
+            fi
+        done
+        accum=${accum% *}
+        [ "$elem" = "${elem#\*}" ] || accum="$accum *"
+        res="$res, $accum"
+        echo "${res#, }"
+    else
+        local res elem accum
+        res=""
+        accum=""
+        for elem in $(get_argtypes "$1"); do
+            if [ "${elem}" = "${elem%,}" ]; then
+                accum="$accum $elem"
+            else
+                accum="$accum ${elem%,}"
+
+                # trim spaces
+                accum=${accum## }
+                accum=${accum%% }
+                # transliterate
+                [ -z "$2" ] || accum=$($2 "$accum")
+
+                res="$res, $accum"
+                accum=""
+            fi
+        done
+
+        # trim spaces
+        accum=${accum## }
+        accum=${accum%% }
+        # transliterate
+        [ -z "$2" ] || accum=$($2 "$accum")
+
+        res="$res, $accum"
+        echo "${res#, }"
+    fi
+}
+
 # Get the number of arguments to a trace event
 get_argc()
 {
@@ -107,6 +222,18 @@ get_fmt()
     echo "$fmt"
 }
 
+# Transliterate an argument in trace-events (raw) into its native counterpart
+# (e.g., TCGv_i32 -> uint32_t)
+native_type()
+{
+    case "$1" in
+        "TCGv_i32") echo "uint32_t" ;;
+        "TCGv_i64") echo "uint64_t" ;;
+        "TCGv_ptr") echo "void *"   ;;
+        *)          echo "$1"       ;;
+    esac
+}
+
 ################################################################################
 ### Backend code
 
@@ -120,7 +247,7 @@ line_h_nop()
 {
     local func args
     func=$(get_func_name "$1")
-    args=$(get_args "$1")
+    args=$(get_args "$1" native_type)
 
     # Define an empty function for the trace event
     cat <<EOF
@@ -173,7 +300,7 @@ line_h_simple()
     # XXX: why 'simple' backend does not expand into 'nop' when disabled?
     local func args argc trace_args
     func=$(get_func_name "$1")
-    args=$(get_args "$1")
+    args=$(get_args "$1" native_type)
     argc=$(get_argc "$1")
     
     trace_args="$simple_event_num"
@@ -267,7 +394,7 @@ line_h_ust()
     local name func args argnames
     name=$(get_event_name "$1")
     func=$(get_func_name "$1")
-    args=$(get_args "$1")
+    args=$(get_args "$1" native_type)
     argnames=$(get_argnames "$1")
 
     cat <<EOF
@@ -302,7 +429,7 @@ line_c_ust()
 
     local name args argnames fmt
     name=$(get_event_name "$1")
-    args=$(get_args "$1")
+    args=$(get_args "$1" native_type)
     argnames=$(get_argnames "$1")
     fmt=$(get_fmt "$1")
 
author
#2 lore raw

PATCH v2 2/4 trace-gen: auto-generate TCG helper routines for tracing

In reply to Lluís

Auto-generates file "trace-helper.h" to provide TCG helpers and "trace-helper.c" to proxy these helpers onto trace event routines.

Only trace events with the 'gen' property are affected.

---
 .gitignore      |    2 +
 Makefile        |   15 +++++++
 Makefile.target |    5 +-
 simpletrace.py  |    2 -
 trace-events    |    5 ++
 tracetool       |  112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 135 insertions(+), 6 deletions(-)
.gitignore +2 -0
index e4a351d..758f457 100644
@@ -4,6 +4,8 @@ config-host.*
 config-target.*
 trace.h
 trace.c
+trace-helper.h
+trace-helper.c
 *-timestamp
 *-softmmu
 *-darwin-user
Makefile +14 -1
index 97cb3b4..4effdcc 100644
@@ -1,6 +1,6 @@
 # Makefile for QEMU.
 
-GENERATED_HEADERS = config-host.h trace.h qemu-options.def
+GENERATED_HEADERS = config-host.h trace.h trace-helper.h trace-helper.c qemu-options.def
 
 ifneq ($(wildcard config-host.mak),)
 # Put the all: rule here so that config-host.mak can contain dependencies.
@@ -120,6 +120,18 @@ trace.c-timestamp: $(SRC_PATH)/trace-events config-host.mak
 
 trace.o: trace.c $(GENERATED_HEADERS)
 
+trace-helper.h: trace-helper.h-timestamp
+trace-helper.h-timestamp: $(SRC_PATH)/trace-events config-host.mak
+	$(call quiet-command,sh $(SRC_PATH)/tracetool $(TRACETOOL_EXTRA) --helper --$(TRACE_BACKEND) -h < $< > $@,"  GEN   trace-helper.h")
+	@cmp -s $@ trace-helper.h || cp $@ trace-helper.h
+
+trace-helper.c: trace-helper.c-timestamp
+trace-helper.c-timestamp: $(SRC_PATH)/trace-events config-host.mak
+	$(call quiet-command,sh $(SRC_PATH)/tracetool $(TRACETOOL_EXTRA) --helper --$(TRACE_BACKEND) -c < $< > $@,"  GEN   trace-helper.c")
+	@cmp -s $@ trace-helper.c || cp $@ trace-helper.c
+
+trace-helper.o: trace-helper.c $(GENERATED_HEADERS)
+
 simpletrace.o: simpletrace.c $(GENERATED_HEADERS)
 
 version.o: $(SRC_PATH)/version.rc config-host.mak
@@ -157,6 +169,7 @@ clean:
 	rm -f slirp/*.o slirp/*.d audio/*.o audio/*.d block/*.o block/*.d net/*.o net/*.d fsdev/*.o fsdev/*.d ui/*.o ui/*.d
 	rm -f qemu-img-cmds.h
 	rm -f trace.c trace.h trace.c-timestamp trace.h-timestamp
+	rm -f trace-helper.c trace-helper.h trace-helper.h-timestamp trace-helper.c-timestamp
 	$(MAKE) -C tests clean
 	for d in $(ALL_SUBDIRS) libhw32 libhw64 libuser libdis libdis-user; do \
 	if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
Makefile.target +2 -3
index 7a92e02..0c8b0ac 100644
@@ -378,9 +378,8 @@ endif
 
 $(QEMU_PROG)-prepare: $(GENERATED_HEADERS) $(LIBBACKDOOR_LIB) $(LIBINSTRUMENT_LIB) $(QEMU_PROG)
 
-$(QEMU_PROG): $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y) $(LIBBACKDOOR_LIB) $(LIBINSTRUMENT_LIB)
-	$(call LINK,$(obj-y) $(obj-$(TARGET_BASE_ARCH)-y)) $(LIBBACKDOOR_LIB) $(LIBINSTRUMENT_LIB)
-
+$(QEMU_PROG): trace-helper.o $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y) $(LIBBACKDOOR_LIB) $(LIBINSTRUMENT_LIB)
+	$(call LINK,trace-helper.o $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y)) $(LIBBACKDOOR_LIB) $(LIBINSTRUMENT_LIB)
 
 gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/feature_to_c.sh
 	$(call quiet-command,rm -f $@ && $(SHELL) $(SRC_PATH)/feature_to_c.sh $@ $(TARGET_XML_FILES),"  GEN   $(TARGET_DIR)$@")
simpletrace.py +1 -1
index 6d8100e..1cfc3de 100755
@@ -19,7 +19,7 @@ header_version  = 0
 
 trace_fmt = '=QQQQQQQQ'
 trace_len = struct.calcsize(trace_fmt)
-event_re  = re.compile(r'(disable\s+|instrument\s+)*([a-zA-Z0-9_]+)\(([^)]*)\).*')
+event_re  = re.compile(r'(disable\s+|instrument\s+|gen\s+)*([a-zA-Z0-9_]+)\(([^)]*)\).*')
 
 def err(msg):
     sys.stderr.write(msg + '\n')
trace-events +5 -0
index fefac91..52e4128 100644
@@ -38,6 +38,11 @@
 #   is identified by the '--with-instrument' configure option).
 #   The original backend-specific function is still available under the name
 #   'trace_##name##_backend'.
+#
+# - gen
+#   Provide trace events suitable for using during TCG code generation.
+#   Generates TCG function helpers reachable through 'helper_trace_gen_##name'
+#   which proxy their calls onto 'trace_##name'.
 
 # qemu-malloc.c
 disable qemu_malloc(size_t size, void *ptr) "size %zu ptr %p"
tracetool +111 -1

136 lines not on this page · raw message

author
#3 lore raw

PATCH v2 3/4 trace-gen: [all] include "trace-helper.h" on all "helper.h" files

In reply to Lluís
---
 target-alpha/helper.h      |    2 ++
 target-arm/helper.h        |    2 ++
 target-cris/helper.h       |    2 ++
 target-i386/helper.h       |    2 ++
 target-m68k/helper.h       |    2 ++
 target-microblaze/helper.h |    2 ++
 target-mips/helper.h       |    2 ++
 target-ppc/helper.h        |    2 ++
 target-sh4/helper.h        |    2 ++
 target-sparc/helper.h      |    2 ++
 10 files changed, 20 insertions(+), 0 deletions(-)
target-alpha/helper.h +2 -0
index ccf6a2a..030def3 100644
@@ -123,4 +123,6 @@ DEF_HELPER_2(stl_c_raw, i64, i64, i64)
 DEF_HELPER_2(stq_c_raw, i64, i64, i64)
 #endif
 
+#include "trace-helper.h"
+
 #include "def-helper.h"
target-arm/helper.h +2 -0
index 0d1bc47..d3ba5d9 100644
@@ -447,4 +447,6 @@ DEF_HELPER_3(iwmmxt_muladdswl, i64, i64, i32, i32)
 
 DEF_HELPER_2(set_teecr, void, env, i32)
 
+#include "trace-helper.h"
+
 #include "def-helper.h"
target-cris/helper.h +2 -0
index 093063a..620a199 100644
@@ -23,4 +23,6 @@ DEF_HELPER_FLAGS_2(evaluate_flags_move_2, TCG_CALL_PURE, i32, i32, i32)
 DEF_HELPER_0(evaluate_flags, void)
 DEF_HELPER_0(top_evaluate_flags, void)
 
+#include "trace-helper.h"
+
 #include "def-helper.h"
target-i386/helper.h +2 -0
index 979d94e..94d7bd1 100644
@@ -217,6 +217,8 @@ DEF_HELPER_2(rclq, tl, tl, tl)
 DEF_HELPER_2(rcrq, tl, tl, tl)
 #endif
 
+#include "trace-helper.h"
+
 #if defined(CONFIG_BACKDOOR)
 #include "backdoor/helper.h"
 #endif
target-m68k/helper.h +2 -0
index cb8a0c7..29e206b 100644
@@ -51,4 +51,6 @@ DEF_HELPER_3(set_mac_extu, void, env, i32, i32)
 DEF_HELPER_2(flush_flags, void, env, i32)
 DEF_HELPER_1(raise_exception, void, i32)
 
+#include "trace-helper.h"
+
 #include "def-helper.h"
target-microblaze/helper.h +2 -0
index 11ad1b6..3606614 100644
@@ -34,4 +34,6 @@ DEF_HELPER_2(mmu_write, void, i32, i32)
 
 DEF_HELPER_4(memalign, void, i32, i32, i32, i32)
 
+#include "trace-helper.h"
+
 #include "def-helper.h"
target-mips/helper.h +2 -0
index cb13fb2..4855b67 100644
@@ -287,4 +287,6 @@ DEF_HELPER_0(rdhwr_ccres, tl)
 DEF_HELPER_1(pmon, void, int)
 DEF_HELPER_0(wait, void)
 
+#include "trace-helper.h"
+
 #include "def-helper.h"
target-ppc/helper.h +2 -0
index 2bf9283..c76e733 100644
@@ -401,4 +401,6 @@ DEF_HELPER_2(store_601_batl, void, i32, tl)
 DEF_HELPER_2(store_601_batu, void, i32, tl)
 #endif
 
+#include "trace-helper.h"
+
 #include "def-helper.h"
target-sh4/helper.h +2 -0
index 4b2fcdd..daec7cd 100644
@@ -50,4 +50,6 @@ DEF_HELPER_1(fsqrt_DT, i64, i64)
 DEF_HELPER_1(ftrc_FT, i32, i32)
 DEF_HELPER_1(ftrc_DT, i32, i64)
 
+#include "trace-helper.h"
+
 #include "def-helper.h"
target-sparc/helper.h +2 -0
index 6f103e7..f2014b8 100644
@@ -160,4 +160,6 @@ VIS_CMPHELPER(cmpne);
 DEF_HELPER_0(compute_psr, void);
 DEF_HELPER_0(compute_C_icc, i32);
 
+#include "trace-helper.h"
+
 #include "def-helper.h"
author
#4 lore raw

PATCH v2 4/4 trace-gen: auto-generate wrappers to call TCG trace helpers

In reply to Lluís

Auto-generates file "trace-gen.h" with instrumentable wrappers to generate calls to TCG trace helpers.

Such wrappers are named 'trace_gen_##name', also reachable as 'trace_gen_##name##_backend' when instrumented.

Events with the "gen" property are also able to use TCG types on the trace event declaration, which are gracefully handled when generating trace functions.

Only trace events with the 'gen' property are affected.

---
 Makefile     |    8 +++-
 configure    |    5 ++
 trace-events |    3 +
 tracetool    |  131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 145 insertions(+), 2 deletions(-)
Makefile +7 -1
index 4effdcc..6737b75 100644
@@ -1,6 +1,6 @@
 # Makefile for QEMU.
 
-GENERATED_HEADERS = config-host.h trace.h trace-helper.h trace-helper.c qemu-options.def
+GENERATED_HEADERS = config-host.h trace.h trace-helper.h trace-helper.c trace-gen.h qemu-options.def
 
 ifneq ($(wildcard config-host.mak),)
 # Put the all: rule here so that config-host.mak can contain dependencies.
@@ -132,6 +132,11 @@ trace-helper.c-timestamp: $(SRC_PATH)/trace-events config-host.mak
 
 trace-helper.o: trace-helper.c $(GENERATED_HEADERS)
 
+trace-gen.h: trace-gen.h-timestamp
+trace-gen.h-timestamp: $(SRC_PATH)/trace-events config-host.mak
+	$(call quiet-command,sh $(SRC_PATH)/tracetool $(TRACETOOL_EXTRA) --gen --$(TRACE_BACKEND) -h < $< > $@,"  GEN   trace-gen.h")
+	@cmp -s $@ trace-gen.h || cp $@ trace-gen.h
+
 simpletrace.o: simpletrace.c $(GENERATED_HEADERS)
 
 version.o: $(SRC_PATH)/version.rc config-host.mak
@@ -170,6 +175,7 @@ clean:
 	rm -f qemu-img-cmds.h
 	rm -f trace.c trace.h trace.c-timestamp trace.h-timestamp
 	rm -f trace-helper.c trace-helper.h trace-helper.h-timestamp trace-helper.c-timestamp
+	rm -f trace-gen.h-timestamp trace-gen.c-timestamp
 	$(MAKE) -C tests clean
 	for d in $(ALL_SUBDIRS) libhw32 libhw64 libuser libdis libdis-user; do \
 	if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
configure +5 -0
index 40f94bf..622a9d4 100755
@@ -766,6 +766,11 @@ for opt do
       echo "Error: directory '$instrument' does not contain a \"trace-instrument.h\" file"
       echo
       exit 1
+  elif test ! -f "$instrument/trace-gen-instrument.h"; then
+      echo
+      echo "Error: directory '$instrument' does not contain a \"trace-gen-instrument.h\" file"
+      echo
+      exit 1
   else
       instrument=`readlink -f $instrument`
   fi
trace-events +3 -0
index 52e4128..334e19d 100644
@@ -43,6 +43,9 @@
 #   Provide trace events suitable for using during TCG code generation.
 #   Generates TCG function helpers reachable through 'helper_trace_gen_##name'
 #   which proxy their calls onto 'trace_##name'.
+#   As an extra aid, functions named 'trace_gen_##name' are generated into
+#   "trace-gen.h", which can be reched through 'trace_gen_##name##_backend' when
+#   the event is instrumented.
 
 # qemu-malloc.c
 disable qemu_malloc(size_t size, void *ptr) "size %zu ptr %p"
tracetool +130 -1

143 lines not on this page · raw message