Skip to contentSkip to conversation

Specify trace file name

PATCH New Stefan Hajnoczi 1 participant 2 messages Jul 13, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Specify trace file name

From: Prerna Saxena <[email protected]>

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.

---
Prerna, I have modified this patch that you wrote and together with patch 2/2 it provides control over the trace file. The changes include:

* Set trace_file earlier in ./configure so $trace_file-<pid> displays correctly.
* Introduce open_trace_file() helper function to handle file naming.

 configure     |   13 +++++++++++++
 simpletrace.c |   16 ++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)
configure +13 -0
index 02bf602..fe1b027 100755
@@ -313,6 +313,7 @@ check_utests="no"
 user_pie="no"
 zero_malloc=""
 trace_backend="nop"
+trace_file="/tmp/trace"
 
 # OS specific
 if check_define __linux__ ; then
@@ -470,6 +471,7 @@ if test "$mingw32" = "yes" ; then
   bindir="\${prefix}"
   sysconfdir="\${prefix}"
   confsuffix=""
+  trace_file="trace"
 fi
 
 # find source path
@@ -517,6 +519,8 @@ for opt do
   ;;
   --trace-backend=*) trace_backend="$optarg"
   ;;
+  --trace-file=*) trace_file="$optarg"
+  ;;
   --enable-gprof) gprof="yes"
   ;;
   --static)
@@ -876,6 +880,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 +2139,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 +2395,11 @@ fi
 if test "$trace_backend" = "ust"; then
   LIBS="-lust $LIBS"
 fi
+# Set the appropriate trace file.
+if test "$trace_backend" = "simple"; then
+  trace_file="\"$trace_file-%u\""
+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
simpletrace.c +14 -2
index ed20d36..29fd6a9 100644
@@ -21,11 +21,23 @@ static TraceRecord trace_buf[TRACE_BUF_LEN];
 static unsigned int trace_idx;
 static FILE *trace_fp;
 
+static bool open_trace_file(void)
+{
+    char *filename;
+
+    if (asprintf(&filename, CONFIG_TRACE_FILE, getpid()) < 0) {
+        return false;
+    }
+
+    trace_fp = fopen(filename, "w");
+    free(filename);
+    return trace_fp != NULL;
+}
+
 static void flush_trace_buffer(void)
 {
     if (!trace_fp) {
-        trace_fp = fopen("/tmp/trace.log", "w");
-        if (trace_fp) {
+        if (open_trace_file()) {
             atexit(flush_trace_buffer);
         }
     }
Signature · 1 line
1.7.1
#1 lore raw

PATCH 2/2 Add trace-file command to open/close/flush trace file

This patch adds the trace-file command:

trace-file [on|off|flush]

Open, close, or flush the trace file. If no argument is given, the status of the trace file is displayed.

The trace file is turned on by default but is only written out when the trace buffer becomes full. The flush operation can be used to force write out at any time.

Turning off the trace file does not change the state of trace events; tracing will continue to the trace buffer. When the trace file is off, use "info trace" to display the contents of the trace buffer in memory.

---
 monitor.c       |   17 +++++++++++++++++
 qemu-monitor.hx |   14 ++++++++++++++
 simpletrace.c   |   45 +++++++++++++++++++++++++++++++++++++++++----
 tracetool       |    3 +++
 4 files changed, 75 insertions(+), 4 deletions(-)
monitor.c +17 -0
index 090e13c..1e35a6b 100644
@@ -540,6 +540,23 @@ static void do_change_trace_event_state(Monitor *mon, const QDict *qdict)
     bool new_state = qdict_get_bool(qdict, "option");
     change_trace_event_state(tp_name, new_state);
 }
+
+static void do_trace_file(Monitor *mon, const QDict *qdict)
+{
+    const char *op = qdict_get_try_str(qdict, "op");
+
+    if (!op) {
+        st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
+    } else if (!strcmp(op, "on")) {
+        st_set_trace_file_enabled(true);
+    } else if (!strcmp(op, "off")) {
+        st_set_trace_file_enabled(false);
+    } else if (!strcmp(op, "flush")) {
+        st_flush_trace_buffer();
+    } else {
+        monitor_printf(mon, "unexpected argument \"%s\"\n", op);
+    }
+}
 #endif
 
 static void user_monitor_complete(void *opaque, QObject *ret_data)
qemu-monitor.hx +14 -0
index c5ba3d2..25887bd 100644
@@ -273,6 +273,20 @@ STEXI
 @findex trace-event
 changes status of a trace event
 ETEXI
+
+    {
+        .name       = "trace-file",
+        .args_type  = "op:s?",
+        .params     = "op [on|off|flush]",
+        .help       = "open, close, or flush trace file",
+        .mhandler.cmd = do_trace_file,
+    },
+
+STEXI
+@item trace-file on|off|flush
+@findex trace-file
+Open, close, or flush the trace file.  If no argument is given, the status of the trace file is displayed.
+ETEXI
 #endif
 
     {
simpletrace.c +41 -4

76 lines not on this page · raw message

tracetool +3 -0
index 8d8f27c..999ef7e 100755
@@ -141,6 +141,9 @@ void trace4(TraceEventID event, unsigned long x1, unsigned long x2, unsigned lon
 void trace5(TraceEventID event, unsigned long x1, unsigned long x2, unsigned long x3, unsigned long x4, unsigned long x5);
 void st_print_trace(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...));
 void st_print_trace_events(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...));
+void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...));
+void st_flush_trace_buffer(void);
+void st_set_trace_file_enabled(bool enable);
 void change_trace_event_state(const char *tname, bool tstate);
 EOF
 
Signature · 1 line
1.7.1