trace-gen: support for trace points in code-generation routines
cover trace-gen: support for trace points in code-generation routines
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.
TODO:
* Handle TCGv_tl and target_long/target_ulong
---
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 - 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 | 366 +++++++++++++++++++++++++++++++++++++++++++- 15 files changed, 409 insertions(+), 13 deletions(-)
PATCH 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 | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 132 insertions(+), 8 deletions(-)
tracetool +132 -8
@@ -38,13 +38,71 @@ 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() +{ + 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" ]; then + local args + args=${1#*\(} + args=${args%\)*} + 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 +130,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 +219,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 +244,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 +297,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 +391,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 +426,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")
PATCH 2/4 trace-gen: auto-generate TCG helper routines for tracing
In reply to LluísAuto-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 +-- trace-events | 5 +++ tracetool | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 120 insertions(+), 5 deletions(-)
.gitignore +2 -0
@@ -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
@@ -1,6 +1,6 @@ # Makefile for QEMU. -GENERATED_HEADERS = config-host.h trace.h +GENERATED_HEADERS = config-host.h trace.h trace-helper.h trace-helper.c ifneq ($(wildcard config-host.mak),) # Put the all: rule here so that config-host.mak can contain dependencies.
@@ -118,6 +118,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
@@ -154,6 +166,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
@@ -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)$@")
trace-events +5 -0
@@ -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 +97 -1
122 lines not on this page · raw message
PATCH 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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"
PATCH 4/4 trace-gen: auto-generate wrappers to call TCG trace helpers
In reply to LluísAuto-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 +++- trace-events | 3 + tracetool | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 2 deletions(-)
Makefile +7 -1
@@ -1,6 +1,6 @@ # Makefile for QEMU. -GENERATED_HEADERS = config-host.h trace.h trace-helper.h trace-helper.c +GENERATED_HEADERS = config-host.h trace.h trace-helper.h trace-helper.c trace-gen.h ifneq ($(wildcard config-host.mak),) # Put the all: rule here so that config-host.mak can contain dependencies.
@@ -130,6 +130,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
@@ -167,6 +172,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; \
trace-events +3 -0
@@ -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 +129 -1
142 lines not on this page · raw message