From 8f6421179ad29cbdaa9d5c5b39e9c001996c0f07 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Mon, 5 Sep 2011 16:31:45 +0100 Subject: [PATCH 1/5] trace: allow trace events with string arguments String arguments are useful for producing human-readable traces without post-processing (e.g. stderr backend). Although the simple backend cannot handles strings all others can. Strings should be allowed and the simple backend can be extended to support them. Signed-off-by: Stefan Hajnoczi --- docs/tracing.txt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/docs/tracing.txt b/docs/tracing.txt index d0171aabda..2c33a62b98 100644 --- a/docs/tracing.txt +++ b/docs/tracing.txt @@ -70,11 +70,6 @@ Trace events should use types as follows: cannot include all user-defined struct declarations and it is therefore necessary to use void * for pointers to structs. - Pointers (including char *) cannot be dereferenced easily (or at all) in - some trace backends. If pointers are used, ensure they are meaningful by - themselves and do not assume the data they point to will be traced. Do - not pass in string arguments. - * For everything else, use primitive scalar types (char, int, long) with the appropriate signedness. @@ -182,6 +177,9 @@ 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. +The "simple" backend currently does not capture string arguments, it simply +records the char* pointer value instead of the string that is pointed to. + ==== Monitor commands ==== * info trace From 598a3f35c5ceab0eb7a3cf82f8aadfc2003a31d8 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Sat, 3 Sep 2011 16:24:47 +0100 Subject: [PATCH 2/5] MAINTAINERS: add tracing subsystem Signed-off-by: Stefan Hajnoczi --- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 72b2099d3e..7c5ea873a8 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -459,6 +459,12 @@ S: Maintained F: slirp/ T: git://git.kiszka.org/qemu.git queues/slirp +Tracing +M: Stefan Hajnoczi +S: Maintained +F: trace/ +T: git://repo.or.cz/qemu/stefanha.git tracing + Usermode Emulation ------------------ BSD user From 85aff1586fe4a0dfc6eda2dfd6e79e78063f4fbb Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Mon, 5 Sep 2011 08:30:17 +0100 Subject: [PATCH 3/5] trace: portable simple trace backend using glib Convert the simple trace backend to glib so that it works under Windows. We cannot use pthread directly but glib provides portable abstractions. Also use glib atomics instead of newish gcc builtins which may not be supported on Windows toolchains. Signed-off-by: Stefan Hajnoczi --- trace/simple.c | 76 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/trace/simple.c b/trace/simple.c index a6093682dd..885764a031 100644 --- a/trace/simple.c +++ b/trace/simple.c @@ -12,8 +12,10 @@ #include #include #include +#ifndef _WIN32 #include #include +#endif #include "qemu-timer.h" #include "trace.h" #include "trace/control.h" @@ -54,9 +56,9 @@ enum { * Trace records are written out by a dedicated thread. The thread waits for * records to become available, writes them out, and then waits again. */ -static pthread_mutex_t trace_lock = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t trace_available_cond = PTHREAD_COND_INITIALIZER; -static pthread_cond_t trace_empty_cond = PTHREAD_COND_INITIALIZER; +static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT; +static GCond *trace_available_cond; +static GCond *trace_empty_cond; static bool trace_available; static bool trace_writeout_enabled; @@ -93,29 +95,30 @@ static bool get_trace_record(unsigned int idx, TraceRecord *record) */ static void flush_trace_file(bool wait) { - pthread_mutex_lock(&trace_lock); + g_static_mutex_lock(&trace_lock); trace_available = true; - pthread_cond_signal(&trace_available_cond); + g_cond_signal(trace_available_cond); if (wait) { - pthread_cond_wait(&trace_empty_cond, &trace_lock); + g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock)); } - pthread_mutex_unlock(&trace_lock); + g_static_mutex_unlock(&trace_lock); } static void wait_for_trace_records_available(void) { - pthread_mutex_lock(&trace_lock); + g_static_mutex_lock(&trace_lock); while (!(trace_available && trace_writeout_enabled)) { - pthread_cond_signal(&trace_empty_cond); - pthread_cond_wait(&trace_available_cond, &trace_lock); + g_cond_signal(trace_empty_cond); + g_cond_wait(trace_available_cond, + g_static_mutex_get_mutex(&trace_lock)); } trace_available = false; - pthread_mutex_unlock(&trace_lock); + g_static_mutex_unlock(&trace_lock); } -static void *writeout_thread(void *opaque) +static gpointer writeout_thread(gpointer opaque) { TraceRecord record; unsigned int writeout_idx = 0; @@ -159,7 +162,7 @@ static void trace(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, timestamp = get_clock(); - idx = __sync_fetch_and_add(&trace_idx, 1) % TRACE_BUF_LEN; + idx = g_atomic_int_exchange_and_add((gint *)&trace_idx, 1) % TRACE_BUF_LEN; trace_buf[idx] = (TraceRecord){ .event = event, .timestamp_ns = timestamp, @@ -331,28 +334,47 @@ bool trace_event_set_state(const char *name, bool state) return false; } -bool trace_backend_init(const char *events, const char *file) +/* Helper function to create a thread with signals blocked. Use glib's + * portable threads since QEMU abstractions cannot be used due to reentrancy in + * the tracer. Also note the signal masking on POSIX hosts so that the thread + * does not steal signals when the rest of the program wants them blocked. + */ +static GThread *trace_thread_create(GThreadFunc fn) { - pthread_t thread; - pthread_attr_t attr; + GThread *thread; +#ifndef _WIN32 sigset_t set, oldset; - int ret; - - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); sigfillset(&set); pthread_sigmask(SIG_SETMASK, &set, &oldset); - ret = pthread_create(&thread, &attr, writeout_thread, NULL); +#endif + thread = g_thread_create(writeout_thread, NULL, FALSE, NULL); +#ifndef _WIN32 pthread_sigmask(SIG_SETMASK, &oldset, NULL); +#endif - if (ret != 0) { - fprintf(stderr, "warning: unable to initialize simple trace backend\n"); - } else { - atexit(st_flush_trace_buffer); - trace_backend_init_events(events); - st_set_trace_file(file); + return thread; +} + +bool trace_backend_init(const char *events, const char *file) +{ + GThread *thread; + + if (!g_thread_supported()) { + g_thread_init(NULL); } + trace_available_cond = g_cond_new(); + trace_empty_cond = g_cond_new(); + + thread = trace_thread_create(writeout_thread); + if (!thread) { + fprintf(stderr, "warning: unable to initialize simple trace backend\n"); + return false; + } + + atexit(st_flush_trace_buffer); + trace_backend_init_events(events); + st_set_trace_file(file); return true; } From 6c2a40742602c3cbe6a3905229dd539d7c311550 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Mon, 5 Sep 2011 18:31:21 +0100 Subject: [PATCH 4/5] trace: use binary file open mode in simpletrace For Windows portability the simple trace backend must use the 'b' file open mode. This prevents the stdio library from mangling 0x0a/0x0d newline characters. Signed-off-by: Stefan Hajnoczi --- trace/simple.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trace/simple.c b/trace/simple.c index 885764a031..b639dda806 100644 --- a/trace/simple.c +++ b/trace/simple.c @@ -234,7 +234,7 @@ void st_set_trace_file_enabled(bool enable) .x1 = HEADER_VERSION, }; - trace_fp = fopen(trace_file_name, "w"); + trace_fp = fopen(trace_file_name, "wb"); if (!trace_fp) { return; } From 4b710a3cd410eb4eb39fbad2e38efe82c502250e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs=20Vilanova?= Date: Tue, 20 Sep 2011 21:03:48 +0200 Subject: [PATCH 5/5] trace: Update docs to use example events that exist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The events 'qemu_malloc' and 'qemu_free' used in the examples no longer exist, so use 'qemu_vmalloc' and 'qemu_vfree' instead. Signed-off-by: LluĂ­s Vilanova Signed-off-by: Stefan Hajnoczi --- docs/tracing.txt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/tracing.txt b/docs/tracing.txt index 2c33a62b98..95ca16c05d 100644 --- a/docs/tracing.txt +++ b/docs/tracing.txt @@ -31,8 +31,8 @@ There is a set of static trace events declared in the "trace-events" source file. Each trace event declaration names the event, its arguments, and the format string which can be used for pretty-printing: - qemu_malloc(size_t size, void *ptr) "size %zu ptr %p" - qemu_free(void *ptr) "ptr %p" + qemu_vmalloc(size_t size, void *ptr) "size %zu ptr %p" + qemu_vfree(void *ptr) "ptr %p" The "trace-events" file is processed by the "tracetool" script during build to generate code for the trace events. Trace events are invoked directly from @@ -40,14 +40,16 @@ source code like this: #include "trace.h" /* needed for trace event prototype */ - void *qemu_malloc(size_t size) + void *qemu_vmalloc(size_t size) { void *ptr; - if (!size && !allow_zero_malloc()) { - abort(); + size_t align = QEMU_VMALLOC_ALIGN; + + if (size < align) { + align = getpagesize(); } - ptr = oom_check(malloc(size ? size : 1)); - trace_qemu_malloc(size, ptr); /* <-- trace event */ + ptr = qemu_memalign(align, size); + trace_qemu_vmalloc(size, ptr); return ptr; }