diff mbox

[Tracing] Specify trace file name

Message ID 20100709163019.2dc123bc@zephyr
State New
Headers show

Commit Message

Prerna Saxena July 9, 2010, 11 a.m. UTC
[PATCH] Allow users to specify a file for trace-outputs at configuration.
Also, allow trace files to be annotated by <pid> so each qemu instance has 
unique traces.

The trace file name can be passed as a config option:
--trace-file=/path/to/file
(Default : /tmp/trace )
At runtime, the pid of the qemu process is appended to the filename so 
that mutiple qemu instances do not have overlapping logs.

Eg : /tmp/trace-1234 for qemu launched with pid 1234.

I have yet to test this on windows. getpid() is used at many places
in code(including vnc.c), so I'm hoping this would be okay too.

Signed-off-by: Prerna Saxena <prerna@linux.vnet.ibm.com>
---
 configure     |   20 ++++++++++++++++++++
 simpletrace.c |   13 ++++++++++++-
 tracetool     |    1 +
 vl.c          |    8 ++++++++
 4 files changed, 41 insertions(+), 1 deletions(-)
diff mbox

Patch

diff --git a/configure b/configure
index 02bf602..18cb6ab 100755
--- a/configure
+++ b/configure
@@ -313,6 +313,7 @@  check_utests="no"
 user_pie="no"
 zero_malloc=""
 trace_backend="nop"
+trace_file=""
 
 # OS specific
 if check_define __linux__ ; then
@@ -517,6 +518,8 @@  for opt do
   ;;
   --trace-backend=*) trace_backend="$optarg"
   ;;
+  --trace-file=*) trace_file="$optarg"
+  ;;
   --enable-gprof) gprof="yes"
   ;;
   --static)
@@ -876,6 +879,9 @@  echo "  --disable-docs           disable documentation build"
 echo "  --disable-vhost-net      disable vhost-net acceleration support"
 echo "  --enable-vhost-net       enable vhost-net acceleration support"
 echo "  --trace-backend=B        Trace backend nop simple ust"
+echo "  --trace-file=NAME        Full PATH,NAME of file to store traces"
+echo "                           Default:/tmp/trace-<pid>"
+echo "                           Default:trace-<pid> on Windows"
 echo ""
 echo "NOTE: The object files are built at the place where configure is launched"
 exit 1
@@ -2132,6 +2138,7 @@  echo "fdatasync         $fdatasync"
 echo "uuid support      $uuid"
 echo "vhost-net support $vhost_net"
 echo "Trace backend     $trace_backend"
+echo "Trace Output File $trace_file-<pid>"
 
 if test $sdl_too_old = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -2387,6 +2394,19 @@  fi
 if test "$trace_backend" = "ust"; then
   LIBS="-lust $LIBS"
 fi
+# Set the appropriate trace file.
+if test "$trace_backend" = "simple"; then
+  if test "$trace_file" = ""; then
+    if test "$mingw32" = "yes" ; then
+      trace_file="\"trace-%u\""
+    else
+      trace_file="\"/tmp/trace-%u\""
+    fi
+  else
+    trace_file="\"$trace_file-%u\""
+  fi
+fi
+echo "CONFIG_TRACE_FILE=$trace_file" >> $config_host_mak
 echo "TOOLS=$tools" >> $config_host_mak
 echo "ROMS=$roms" >> $config_host_mak
 echo "MAKE=$make" >> $config_host_mak
diff --git a/simpletrace.c b/simpletrace.c
index 57c41fc..4f3228f 100644
--- a/simpletrace.c
+++ b/simpletrace.c
@@ -20,6 +20,16 @@  static TraceRecord trace_buf[TRACE_BUF_LEN];
 static unsigned int trace_idx;
 static FILE *trace_fp;
 
+char* trace_file_name;
+
+/**
+ * Initialize trace file name.
+ */
+int init_trace_file(void)
+{
+   return asprintf(&trace_file_name, CONFIG_TRACE_FILE, getpid());
+}
+
 static void trace(TraceEventID event, unsigned long x1,
                   unsigned long x2, unsigned long x3,
                   unsigned long x4, unsigned long x5) {
@@ -40,7 +50,7 @@  static void trace(TraceEventID event, unsigned long x1,
         trace_idx = 0;
 
         if (!trace_fp) {
-            trace_fp = fopen("/tmp/trace.log", "w");
+            trace_fp = fopen(trace_file_name, "w");
         }
         if (trace_fp) {
             size_t result = fwrite(trace_buf, sizeof trace_buf, 1, trace_fp);
@@ -78,6 +88,7 @@  void do_info_trace(Monitor *mon)
                           trace_buf[i].event, trace_buf[i].x1, trace_buf[i].x2,
                             trace_buf[i].x3, trace_buf[i].x4, trace_buf[i].x5);
     }
+    monitor_printf(mon, "Trace output logged at %s", trace_file_name);
 }
 
 void do_info_all_trace_events(Monitor *mon)
diff --git a/tracetool b/tracetool
index c77280d..05ece45 100755
--- a/tracetool
+++ b/tracetool
@@ -125,6 +125,7 @@  typedef struct {
     bool state;
 } TraceEvent;
 
+int init_trace_file(void);
 void trace1(TraceEventID event, unsigned long x1);
 void trace2(TraceEventID event, unsigned long x1, unsigned long x2);
 void trace3(TraceEventID event, unsigned long x1, unsigned long x2, unsigned long x3);
diff --git a/vl.c b/vl.c
index 920717a..adc28ef 100644
--- a/vl.c
+++ b/vl.c
@@ -95,6 +95,10 @@  extern int madvise(caddr_t, size_t, int);
 #include <windows.h>
 #endif
 
+#ifdef CONFIG_SIMPLE_TRACE
+#include "trace.h"
+#endif
+
 #ifdef CONFIG_SDL
 #if defined(__APPLE__) || defined(main)
 #include <SDL.h>
@@ -2758,6 +2762,10 @@  int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
+    /* Init tracing, if configured */
+#ifdef CONFIG_SIMPLE_TRACE
+    init_trace_file();
+#endif
     /* init the bluetooth world */
     if (foreach_device_config(DEV_BT, bt_parse))
         exit(1);