Skip to contentSkip to conversation

trace-instrument: let the user wrap/override code generated from trace-events

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

lore
Share & tools
lore

Lluís

author
#0 lore raw

v2 cover trace-instrument: let the user wrap/override code generated from trace-events

XXX: 'tracetool' could be simplified by never generating real tracing code for
events with the "disable" property (not even with the 'simple' backend).
XXX: Must be applied on top of the 'backdoor' patch series

Adds a new optional keyword ("instrument") to the syntax in "trace-events". When specified, this event property lets the user provide her own implementation for that tracing event.

Still, in case the user only wants to wrap around the tracing event, tracetool's original implementation is accessible through function 'trace_##name##_backend', instead of the original 'trace_##name' (which now the user has to provide).

The user-provided tracing functions are expected to be in the static library "libinstrument.a", identified by the "--with-instrument" configuration parameter.

v2:

Rebase onto latest QEMU version (d33ea50a958b2e050d2b28e5f17e3b55e91c6d74).

Provide a '#define trace_##name##_enabled 1' in "trace.h" in case the tracing point arguments need some extra computations only when enabled.

TODO:

* Having the 'simple' backend generate code even when the "disable" property is present, complicates the flow in 'tracetool'.
Is this behaviour really needed?

---

Lluís Vilanova (3):
trace: rewrite 'tracetool' to facilitate future extensions
trace-instrument: let the user override events generated by 'tracetool'
trace-instrument: handle config-time activation

 Makefile        |    4 -
 Makefile.target |   29 ++++-
 configure       |   30 +++++
 simpletrace.py  |    2 
 trace-events    |   23 +++-
 tracetool       |  340 +++++++++++++++++++++++++++++++++++--------------------
 6 files changed, 293 insertions(+), 135 deletions(-)
author
#1 lore raw

PATCH v2 1/3 trace: rewrite 'tracetool' to facilitate future extensions

---
 Makefile  |    4 -
 tracetool |  313 +++++++++++++++++++++++++++++++++++++------------------------
 2 files changed, 192 insertions(+), 125 deletions(-)
Makefile +2 -2
index 02698e9..2ba52aa 100644
@@ -110,12 +110,12 @@ bt-host.o: QEMU_CFLAGS += $(BLUEZ_CFLAGS)
 
 trace.h: trace.h-timestamp
 trace.h-timestamp: $(SRC_PATH)/trace-events config-host.mak
-	$(call quiet-command,sh $(SRC_PATH)/tracetool --$(TRACE_BACKEND) -h < $< > $@,"  GEN   trace.h")
+	$(call quiet-command,sh $(SRC_PATH)/tracetool --regular --$(TRACE_BACKEND) -h < $< > $@,"  GEN   trace.h")
 	@cmp -s $@ trace.h || cp $@ trace.h
 
 trace.c: trace.c-timestamp
 trace.c-timestamp: $(SRC_PATH)/trace-events config-host.mak
-	$(call quiet-command,sh $(SRC_PATH)/tracetool --$(TRACE_BACKEND) -c < $< > $@,"  GEN   trace.c")
+	$(call quiet-command,sh $(SRC_PATH)/tracetool --regular --$(TRACE_BACKEND) -c < $< > $@,"  GEN   trace.c")
 	@cmp -s $@ trace.c || cp $@ trace.c
 
 trace.o: trace.c $(GENERATED_HEADERS)
tracetool +190 -123

490 lines not on this page · raw message

author
#2 lore raw

PATCH v2 2/3 trace-instrument: let the user override events generated by 'tracetool'

In reply to Lluís

Add a new event keyword ("instrument") that lets the user provide her own implementation of tracing events.

Still, tracetool's original implementation is accessible through function '_trace_##name' instead of 'trace_##name' (in case the user only wants to wrap around the event).

Additionally, a '#define trace_##name##_enabled 1' is provided in "trace.h" in case the tracing point arguments need some extra computations only when enabled.

---
 Makefile       |    4 ++--
 simpletrace.py |    2 +-
 trace-events   |   23 +++++++++++++++++------
 tracetool      |   33 ++++++++++++++++++++++++++++++---
 4 files changed, 50 insertions(+), 12 deletions(-)
Makefile +2 -2
index 2ba52aa..97cb3b4 100644
@@ -110,12 +110,12 @@ bt-host.o: QEMU_CFLAGS += $(BLUEZ_CFLAGS)
 
 trace.h: trace.h-timestamp
 trace.h-timestamp: $(SRC_PATH)/trace-events config-host.mak
-	$(call quiet-command,sh $(SRC_PATH)/tracetool --regular --$(TRACE_BACKEND) -h < $< > $@,"  GEN   trace.h")
+	$(call quiet-command,sh $(SRC_PATH)/tracetool $(TRACETOOL_EXTRA) --regular --$(TRACE_BACKEND) -h < $< > $@,"  GEN   trace.h")
 	@cmp -s $@ trace.h || cp $@ trace.h
 
 trace.c: trace.c-timestamp
 trace.c-timestamp: $(SRC_PATH)/trace-events config-host.mak
-	$(call quiet-command,sh $(SRC_PATH)/tracetool --regular --$(TRACE_BACKEND) -c < $< > $@,"  GEN   trace.c")
+	$(call quiet-command,sh $(SRC_PATH)/tracetool $(TRACETOOL_EXTRA) --regular --$(TRACE_BACKEND) -c < $< > $@,"  GEN   trace.c")
 	@cmp -s $@ trace.c || cp $@ trace.c
 
 trace.o: trace.c $(GENERATED_HEADERS)
simpletrace.py +1 -1
index 553a727..6d8100e 100755
@@ -19,7 +19,7 @@ header_version  = 0
 
 trace_fmt = '=QQQQQQQQ'
 trace_len = struct.calcsize(trace_fmt)
-event_re  = re.compile(r'(disable\s+)?([a-zA-Z0-9_]+)\(([^)]*)\).*')
+event_re  = re.compile(r'(disable\s+|instrument\s+)*([a-zA-Z0-9_]+)\(([^)]*)\).*')
 
 def err(msg):
     sys.stderr.write(msg + '\n')
trace-events +17 -6
index 947f8b0..fefac91 100644
@@ -12,21 +12,32 @@
 #
 # Format of a trace event:
 #
-# [disable] <name>(<type1> <arg1>[, <type2> <arg2>] ...) "<format-string>"
+# [<property> ...] <name>(<type1> <arg1>[, <type2> <arg2>] ...) "<format-string>"
 #
 # Example: qemu_malloc(size_t size) "size %zu"
 #
-# The "disable" keyword will build without the trace event.
-# In case of 'simple' trace backend, it will allow the trace event to be
-# compiled, but this would be turned off by default. It can be toggled on via
-# the monitor.
-#
 # The <name> must be a valid as a C function name.
 #
 # Types should be standard C types.  Use void * for pointers because the trace
 # system may not have the necessary headers included.
 #
 # The <format-string> should be a sprintf()-compatible format string.
+#
+# Properties:
+#
+# - disable
+#   Build QEMU without the trace event.
+#   In case of using the 'simple' trace backend, it will allow the trace event
+#   to be compiled, but this would be turned off by default. It can be toggled
+#   on via the monitor.
+#
+# - instrument
+#   Let the user provide code for the trace event.
+#   The "instrument" keyword will let the user provide her own 'trace_##name'
+#   implementation on "trace-instrument.h" and "libinstrument.a" (their location
+#   is identified by the '--with-instrument' configure option).
+#   The original backend-specific function is still available under the name
+#   'trace_##name##_backend'.
 
 # qemu-malloc.c
 disable qemu_malloc(size_t size, void *ptr) "size %zu ptr %p"
tracetool +30 -3
index c5ae6a7..f9098da 100755
@@ -349,15 +349,30 @@ traceto_h_regular()
 #include "qemu-common.h"
 EOF
     convert h $1 $2
+    if [ "$had_instrument" = "1" ]; then
+        echo '#include "trace-instrument.h"'
+    fi
     echo "#endif /* TRACE_H */"
 }
 
 line_h_regular()
 {
-    local api func
+    # XXX: should still provide instrumentation if event is disabled?
+    local api func instrument
     api=$(get_api_name "$1")
     func=$(get_func_name "$1")
-    echo "#define $api $func"
+    instrument=$(get_property "$1" "instrument")
+    if [ "$instrument" = "1" ]; then
+        had_instrument="1"
+    else
+        echo "#define $api $func"
+    fi
+
+    local disable
+    disable=$(get_property "$1" "disable")
+    if [ "$disable" = "0" ]; then
+        echo "#define ${api}_enabled 1"
+    fi
 }
 
 ### Regular -- C
@@ -402,12 +417,18 @@ frontend=nil
 backend=nil
 output=nil
 
+enable_instrument="0"
+had_instrument="0"
+
 usage()
 {
     cat >&2 <<EOF
-usage: $0 <frontend> <backend> <output>
+usage: $0 [<flag>] <frontend> <backend> <output>
 Generate tracing code for a file on stdin.
 
+Flags:
+  --instrument  Enable instrumentation
+
 Frontends:
   --regular     Regular frontend
 
@@ -425,6 +446,7 @@ EOF
 
 while [ $# -gt 0 ]; do
     case $1 in
+        "--instrument") enable_instrument="1" ;;
         "--regular") frontend="${1#--}" ;;
         "--nop"|"--simple"|"--ust") backend="${1#--}" ;;
         "-h"|"-c") output="${1#-}" ;;
@@ -441,4 +463,9 @@ fi
 
 traceto_${output}_$frontend $frontend $backend
 
+if [ "$had_instrument" = "1" -a "$enable_instrument" = "0" ]; then
+    echo "ERROR: You must configure QEMU using '--with-instrument' to use the 'instrument' property in \"trace-events\"" >/dev/stderr
+    exit 1
+fi
+
 exit 0
author
#3 lore raw

PATCH v2 3/3 trace-instrument: handle config-time activation

In reply to Lluís

Add a '--with-instrument' configuration option pointing to user-provided instrumentation callbacks.

Make is invoked on the user-provided directory, which must build a static library that might contain extra code needed by the user-provided instrumentation.

---
 Makefile.target |   29 ++++++++++++++++++++++++++---
 configure       |   30 ++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 3 deletions(-)
Makefile.target +26 -3
index da6ee03..7a92e02 100644
@@ -351,12 +351,35 @@ libbackdoor-clean:
 		VPATH=$(VPATH) SRC_PATH=$(SRC_PATH) V="$(V)" clean || true
 endif
 
+#########################################################
+# static instrumentation
+ifdef CONFIG_INSTRUMENT
+VPATH := $(VPATH):$(INSTRUMENT_PATH)
+
+LIBINSTRUMENT_LIB = libinstrument/libinstrument.a
+LIBINSTRUMENT_CLEAN = libinstrument-clean
+
+libinstrument/Makefile:
+	$(call quiet-command, mkdir -p libinstrument, "  CREAT $(TARGET_DIR)$@")
+	$(call quiet-command, rm -f libinstrument/Makefile)
+	$(call quiet-command, ln -s $(INSTRUMENT_PATH)/Makefile libinstrument/Makefile)
+
+libinstrument/libinstrument.a: libinstrument/Makefile force
+	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libinstrument	\
+		QEMU_CFLAGS="$(QEMU_CFLAGS) -I../target-$(TARGET_BASE_ARCH)"	\
+		TARGET_DIR=$(TARGET_DIR)libinstrument/ VPATH=$(VPATH)		\
+		SRC_PATH=$(SRC_PATH) V="$(V)" libinstrument.a)
+
+libinstrument-clean:
+	$(MAKE) $(SUBDIR_MAKEFLAGS) -C $(LIBINSTRUMENT_DIR)			\
+		VPATH=$(VPATH) SRC_PATH=$(SRC_PATH) V="$(V)" clean || true
+endif
 
 
-$(QEMU_PROG)-prepare: $(GENERATED_HEADERS) $(LIBBACKDOOR_LIB) $(QEMU_PROG)
+$(QEMU_PROG)-prepare: $(GENERATED_HEADERS) $(LIBBACKDOOR_LIB) $(LIBINSTRUMENT_LIB) $(QEMU_PROG)
 
-$(QEMU_PROG): $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y) $(LIBBACKDOOR_LIB)
-	$(call LINK,$(obj-y) $(obj-$(TARGET_BASE_ARCH)-y)) $(LIBBACKDOOR_LIB)
+$(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)
 
 
 gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/feature_to_c.sh
configure +30 -0
index e6fbf97..40f94bf 100755
@@ -333,6 +333,7 @@ trace_backend="nop"
 trace_file="trace"
 spice=""
 backdoor=""
+instrument=""
 
 # OS specific
 if check_define __linux__ ; then
@@ -753,6 +754,22 @@ for opt do
       backdoor=`readlink -f $backdoor`
   fi
   ;;
+  --with-instrument=*) instrument="$optarg"
+  if test ! -f "$instrument/Makefile"; then
+      echo
+      echo "Error: cannot make into '$instrument'"
+      echo "Please choose a directory where I can run 'make'"
+      echo
+      exit 1
+  elif test ! -f "$instrument/trace-instrument.h"; then
+      echo
+      echo "Error: directory '$instrument' does not contain a \"trace-instrument.h\" file"
+      echo
+      exit 1
+  else
+      instrument=`readlink -f $instrument`
+  fi
+  ;;
   *) echo "ERROR: unknown option $opt"; show_help="yes"
   ;;
   esac
@@ -946,6 +963,8 @@ echo "  --trace-file=NAME        Full PATH,NAME of file to store traces"
 echo "                           Default:trace-<pid>"
 echo "  --disable-spice          disable spice"
 echo "  --enable-spice           enable spice"
+echo "  --with-backdoor=PATH     enable backdoor communication and compile implementation in PATH"
+echo "  --with-instrument=PATH   enable static instrumentation and compile user code in PATH"
 echo ""
 echo "NOTE: The object files are built at the place where configure is launched"
 exit 1
@@ -2353,6 +2372,9 @@ echo "spice support     $spice"
 if test -n "$backdoor"; then
     echo "Backdoor comm.    $backdoor"
 fi
+if test -n "$instrument"; then
+    echo "Instrumentation   $instrument"
+fi
 
 if test $sdl_too_old = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -2622,6 +2644,14 @@ if test -n "$backdoor"; then
   rm -rf *-{bsd-usr,darwin-user,linux-user,softmmu}/libbackdoor/
 fi
 
+if test -n "$instrument"; then
+  echo "CONFIG_INSTRUMENT=y" >> $config_host_mak
+  echo "INSTRUMENT_PATH=$instrument" >> $config_host_mak
+  echo "TRACETOOL_EXTRA=--instrument" >> $config_host_mak
+  QEMU_CFLAGS="-I$instrument $QEMU_CFLAGS"
+  rm -rf *-{bsd-usr,darwin-user,linux-user,softmmu}/libinstrument/
+fi
+
 # XXX: suppress that
 if [ "$bsd" = "yes" ] ; then
   echo "CONFIG_BSD=y" >> $config_host_mak