Hi Tomek,

On Tue, 2009-09-01 at 10:06 +0200, Tomek Grabiec wrote:
> It disables buffering of strings passed to trace_printf().
> 
> Signed-off-by: Tomek Grabiec <tgrab...@gmail.com>

Does something like this work for your scenario?

                        Pekka

>From 9d6a82a9290f5f75a5018506b4febe31ad8df3e0 Mon Sep 17 00:00:00 2001
From: Pekka Enberg <penb...@cs.helsinki.fi>
Date: Tue, 1 Sep 2009 21:21:15 +0300
Subject: [PATCH] vm: Flush and disable buffering on SIGABRT

Signed-off-by: Pekka Enberg <penb...@cs.helsinki.fi>
---
 include/vm/trace.h |    1 +
 vm/signal.c        |   23 +++++++++++++++++------
 vm/trace.c         |   36 ++++++++++++++++++++++++++++++------
 3 files changed, 48 insertions(+), 12 deletions(-)

diff --git a/include/vm/trace.h b/include/vm/trace.h
index 79959ea..e95bbb9 100644
--- a/include/vm/trace.h
+++ b/include/vm/trace.h
@@ -3,5 +3,6 @@
 
 int trace_printf(const char *fmt, ...);
 void trace_flush(void);
+void trace_emergency_flush(void);
 
 #endif /* _VM_TRACE_H */
diff --git a/vm/signal.c b/vm/signal.c
index 094fec6..50e0576 100644
--- a/vm/signal.c
+++ b/vm/signal.c
@@ -35,6 +35,7 @@
 #include "vm/preload.h"
 #include "vm/signal.h"
 #include "vm/stack-trace.h"
+#include "vm/trace.h"
 
 #include "arch/signal.h"
 
@@ -80,7 +81,7 @@ static unsigned long gc_safepoint_bh(unsigned long addr)
        return addr;
 }
 
-static void sigfpe_handler(int sig, siginfo_t *si, void *ctx)
+static void handle_sigfpe(int sig, siginfo_t *si, void *ctx)
 {
        if (signal_from_native(ctx))
                goto exit;
@@ -96,7 +97,7 @@ static void sigfpe_handler(int sig, siginfo_t *si, void *ctx)
        print_backtrace_and_die(sig, si, ctx);
 }
 
-static void sigsegv_handler(int sig, siginfo_t *si, void *ctx)
+static void handle_sigsegv(int sig, siginfo_t *si, void *ctx)
 {
        if (signal_from_native(ctx))
                goto exit;
@@ -160,11 +161,18 @@ static void sigsegv_handler(int sig, siginfo_t *si, void 
*ctx)
        print_backtrace_and_die(sig, si, ctx);
 }
 
-static void signal_handler(int sig, siginfo_t *si, void *ctx)
+static void handle_signal(int sig, siginfo_t *si, void *ctx)
 {
        print_backtrace_and_die(sig, si, ctx);
 }
 
+static void handle_abort(int sig, siginfo_t *si, void *ctx)
+{
+       trace_emergency_flush();
+
+       print_backtrace_and_die(sig, si, ctx);
+}
+
 void setup_signal_handlers(void)
 {
        struct sigaction sa;
@@ -172,12 +180,15 @@ void setup_signal_handlers(void)
        sigemptyset(&sa.sa_mask);
        sa.sa_flags     = SA_RESTART | SA_SIGINFO;
 
-       sa.sa_sigaction = sigsegv_handler;
+       sa.sa_sigaction = handle_sigsegv;
        sigaction(SIGSEGV, &sa, NULL);
 
-       sa.sa_sigaction = sigfpe_handler;
+       sa.sa_sigaction = handle_sigfpe;
        sigaction(SIGFPE, &sa, NULL);
 
-       sa.sa_sigaction = signal_handler;
+       sa.sa_sigaction = handle_signal;
        sigaction(SIGUSR1, &sa, NULL);
+
+       sa.sa_sigaction = handle_abort;
+       sigaction(SIGABRT, &sa, NULL);
 }
diff --git a/vm/trace.c b/vm/trace.c
index 0192de6..0ebafbf 100644
--- a/vm/trace.c
+++ b/vm/trace.c
@@ -38,6 +38,8 @@ static pthread_mutex_t trace_mutex = 
PTHREAD_MUTEX_INITIALIZER;
 
 static __thread struct string *trace_buffer;
 
+static bool disable_tracing;
+
 static void setup_trace_buffer(void)
 {
        if (trace_buffer)
@@ -53,6 +55,13 @@ int trace_printf(const char *fmt, ...)
        va_list args;
        int err;
 
+       if (disable_tracing) {
+               va_start(args, fmt);
+               vfprintf(stderr, fmt, args);
+               va_end(args);
+               return 0;
+       }
+
        setup_trace_buffer();
 
        va_start(args, fmt);
@@ -62,14 +71,15 @@ int trace_printf(const char *fmt, ...)
        return err;
 }
 
-void trace_flush(void)
+static void do_trace_flush(void)
 {
        struct vm_thread *self;
        char *thread_name;
        char *line;
        char *next;
 
-       setup_trace_buffer();
+       if (!trace_buffer)
+               return;
 
        self = vm_thread_self();
        if (self)
@@ -77,8 +87,6 @@ void trace_flush(void)
        else
                thread_name = strdup("unknown");
 
-       pthread_mutex_lock(&trace_mutex);
-
        line = trace_buffer->value;
        next = index(line, '\n');
        while (next) {
@@ -94,8 +102,24 @@ void trace_flush(void)
        memmove(trace_buffer->value, line, strlen(line) + 1);
        trace_buffer->length = strlen(line);
 
-       pthread_mutex_unlock(&trace_mutex);
-
        if (self)
                free(thread_name);
 }
+
+void trace_flush(void)
+{
+       setup_trace_buffer();
+
+       pthread_mutex_lock(&trace_mutex);
+
+       do_trace_flush();
+
+       pthread_mutex_unlock(&trace_mutex);
+}
+
+void trace_emergency_flush(void)
+{
+       disable_tracing = true;
+
+       do_trace_flush();
+}
-- 
1.5.6.3




------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Jatovm-devel mailing list
Jatovm-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jatovm-devel

Reply via email to