Skip to contentSkip to conversation

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

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

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Lluís

author
#0 lore raw

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

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.

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       |  336 +++++++++++++++++++++++++++++++++++--------------------
 6 files changed, 289 insertions(+), 135 deletions(-)
author
#1 lore raw

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

---
 Makefile  |    4 -
 tracetool |  315 +++++++++++++++++++++++++++++++++++++------------------------
 2 files changed, 194 insertions(+), 125 deletions(-)
Makefile +2 -2
index 252c817..fec086b 100644
@@ -108,12 +108,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 +192 -123

492 lines not on this page · raw message

author
#2 lore raw

PATCH 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).

---
 Makefile       |    4 ++--
 simpletrace.py |    2 +-
 trace-events   |   23 +++++++++++++++++------
 tracetool      |   23 ++++++++++++++++++++++-
 4 files changed, 42 insertions(+), 10 deletions(-)
Makefile +2 -2
index fec086b..1886317 100644
@@ -108,12 +108,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 c2cf168..0f3fab5 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_]+)\(([^)]*)\)\s+"([^"]*)"')
+event_re  = re.compile(r'\s*(disable\s+|instrument\s+)*([a-zA-Z0-9_]+)\(([^)]*)\)\s+"([^"]*)"')
 
 def err(msg):
     sys.stderr.write(msg + '\n')
trace-events +17 -6
index 4300178..3b91a1c 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 +22 -1
index 4bd264f..facb385 100755
@@ -349,12 +349,21 @@ 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()
 {
+    # XXX: should still provide instrumentation if event is disabled?
     local instrument
+    instrument=$(get_property "$1" "instrument")
+    if [ "$instrument" = "1" ]; then
+        had_instrument="1"
+        return
+    fi
 
     local api func
     api=$(get_api_name "$1")
@@ -404,12 +413,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
 
@@ -427,6 +442,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#-}" ;;
@@ -443,4 +459,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 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 9152723..90867e7 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 991f0b8..2476512 100755
@@ -332,6 +332,7 @@ trace_backend="nop"
 trace_file="trace"
 spice=""
 backdoor=""
+instrument=""
 
 # OS specific
 if check_define __linux__ ; then
@@ -752,6 +753,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
@@ -943,6 +960,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
@@ -2335,6 +2354,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"
@@ -2600,6 +2622,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