diff mbox

[v2,09/11] trace-state: [simple] add "-trace events" argument to control initial state

Message ID 20110406183456.22854.99191.stgit@ginnungagap.bsc.es
State New
Headers show

Commit Message

=?utf-8?Q?Llu=C3=ADs?= April 6, 2011, 6:34 p.m. UTC
When using the "simple" tracing backend, all events are in disabled state by
default.

The "-trace events" argument can be used to provide a file with a list of trace
event names that will be enabled prior to starting execution. This saves the
user from manually toggling event states through the monitor interface, as well
as enables early tracing for the selected points, much like other
more-sophisticated backends like "ust" or "dtrace".

Signed-off-by: LluĂ­s Vilanova <vilanova@ac.upc.edu>
---
 docs/tracing.txt |    5 +++++
 qemu-config.c    |    3 +++
 qemu-options.hx  |   18 ++++++++++++++----
 vl.c             |   26 ++++++++++++++++++++++++++
 4 files changed, 48 insertions(+), 4 deletions(-)
diff mbox

Patch

diff --git a/docs/tracing.txt b/docs/tracing.txt
index 9138dca..26b221f 100644
--- a/docs/tracing.txt
+++ b/docs/tracing.txt
@@ -146,6 +146,11 @@  source tree.  It may not be as powerful as platform-specific or third-party
 trace backends but it is portable.  This is the recommended trace backend
 unless you have specific needs for more advanced backends.
 
+==== Enabling trace events from the command line ====
+
+The "-trace events=<file>" command line argument can be used to enable the
+events listed in <file> from the very beginning of the program.
+
 ==== Monitor commands ====
 
 * info trace
diff --git a/qemu-config.c b/qemu-config.c
index 8ba0804..7c7357f 100644
--- a/qemu-config.c
+++ b/qemu-config.c
@@ -306,6 +306,9 @@  static QemuOptsList qemu_trace_opts = {
         {
             .name = "file",
             .type = QEMU_OPT_STRING,
+        },{
+            .name = "events",
+            .type = QEMU_OPT_STRING,
         },
         { /* end if list */ }
     },
diff --git a/qemu-options.hx b/qemu-options.hx
index ef60730..ff8e75d 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -2358,13 +2358,23 @@  option will prevent QEMU from loading these configuration files at startup.
 ETEXI
 #ifdef CONFIG_SIMPLE_TRACE
 DEF("trace", HAS_ARG, QEMU_OPTION_trace,
-    "-trace\n"
-    "                Specify a trace file to log traces to\n",
+    "-trace [file=<file>][,events=<file>]\n"
+    "                specify tracing options\n",
     QEMU_ARCH_ALL)
 STEXI
-@item -trace
+@item -trace [file=@var{file}][,events=@var{file}]
 @findex -trace
-Specify a trace file to log output traces to.
+
+Specify tracing options.
+
+@table @option
+@item file=@var{file}
+Log output traces to @var{file}.
+@item events=@var{file}
+Immediately enable events listed in @var{file}.
+The file must contain one event name (as listed in the @var{trace-events} file)
+per line.
+@end table
 ETEXI
 #endif
 
diff --git a/vl.c b/vl.c
index 5a9ea51..35d440d 100644
--- a/vl.c
+++ b/vl.c
@@ -1968,6 +1968,7 @@  int main(int argc, char **argv, char **envp)
     int defconfig = 1;
 #if defined(CONFIG_SIMPLE_TRACE)
     const char *trace_file = NULL;
+    const char *trace_events_file = NULL;
 #endif
 
     atexit(qemu_run_exit_notifiers);
@@ -2766,6 +2767,7 @@  int main(int argc, char **argv, char **envp)
                 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
                 if (opts) {
                     trace_file = qemu_opt_get(opts, "file");
+                    trace_events_file = qemu_opt_get(opts, "events");
                 }
                 break;
 #endif
@@ -2818,6 +2820,30 @@  int main(int argc, char **argv, char **envp)
     if (!st_init(trace_file)) {
         fprintf(stderr, "warning: unable to initialize simple trace backend\n");
     }
+    if (trace_events_file) {
+        FILE *trace_events_fp = fopen(trace_events_file, "r");
+        if (!trace_events_fp) {
+            fprintf(stderr, "could not open trace events file '%s': %s\n",
+                    trace_events_file, strerror(errno));
+            exit(1);
+        }
+        char line_buf[1024];
+        while (fgets(line_buf, sizeof(line_buf), trace_events_fp)) {
+            size_t len = strlen(line_buf);
+            if (len > 1) {              /* skip empty lines */
+                line_buf[len - 1] = '\0';
+                if (!st_change_trace_event_state(line_buf, true)) {
+                    fprintf(stderr, "trace event '%s' does not exist\n", line_buf);
+                    exit(1);
+                }
+            }
+        }
+        if (fclose(trace_events_fp) != 0) {
+            fprintf(stderr, "error closing file '%s': %s\n",
+                    trace_events_file, strerror(errno));
+            exit(1);
+        }
+    }
 #endif
 
     /* If no data_dir is specified then try to find it relative to the