From 734121778439134bf4843a1b22038fb9ece333c1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Wed, 24 Aug 2011 19:33:06 +0100 Subject: [PATCH] Use the exception handler from localWriter to only flush on a signal. --- trace_local_writer.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- trace_writer.hpp | 5 +++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/trace_local_writer.cpp b/trace_local_writer.cpp index 9dc746e..ae6a6b6 100644 --- a/trace_local_writer.cpp +++ b/trace_local_writer.cpp @@ -40,7 +40,20 @@ namespace Trace { -LocalWriter::LocalWriter() { +static void exceptionCallback(void) +{ + OS::DebugMessage("apitrace: flushing trace due to an exception\n"); + localWriter.flush(); +} + + +LocalWriter::LocalWriter() : + acquired(0) +{} + +LocalWriter::~LocalWriter() +{ + OS::ResetExceptionCallback(); } void @@ -83,10 +96,18 @@ LocalWriter::open(void) { OS::DebugMessage("apitrace: tracing to %s\n", szFileName); Writer::open(szFileName); + + OS::SetExceptionCallback(exceptionCallback); + +#if 0 + // For debugging the exception handler + *((int *)0) = 0; +#endif } unsigned LocalWriter::beginEnter(const FunctionSig *sig) { OS::AcquireMutex(); + ++acquired; if (!g_gzFile) { open(); @@ -97,21 +118,37 @@ unsigned LocalWriter::beginEnter(const FunctionSig *sig) { void LocalWriter::endEnter(void) { Writer::endEnter(); - gzflush(g_gzFile, Z_SYNC_FLUSH); + --acquired; OS::ReleaseMutex(); } void LocalWriter::beginLeave(unsigned call) { OS::AcquireMutex(); + ++acquired; Writer::beginLeave(call); } void LocalWriter::endLeave(void) { Writer::endLeave(); - gzflush(g_gzFile, Z_SYNC_FLUSH); + --acquired; OS::ReleaseMutex(); } +void LocalWriter::flush(void) { + /* + * Do nothing if the mutex is already acquired (e.g., if a segfault happen + * while writing the file) to prevent dead-lock. + */ + + if (!acquired) { + OS::AcquireMutex(); + if (g_gzFile) { + gzflush(g_gzFile, Z_SYNC_FLUSH); + } + OS::ReleaseMutex(); + } +} + LocalWriter localWriter; diff --git a/trace_writer.hpp b/trace_writer.hpp index d959714..94ca4fd 100644 --- a/trace_writer.hpp +++ b/trace_writer.hpp @@ -115,11 +115,14 @@ namespace Trace { */ class LocalWriter : public Writer { protected: + int acquired; + public: /** * Should never called directly -- use localWriter singleton below instead. */ LocalWriter(); + ~LocalWriter(); void open(void); @@ -128,6 +131,8 @@ namespace Trace { void beginLeave(unsigned call); void endLeave(void); + + void flush(void); }; /** -- 2.7.4