X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_local_writer.cpp;h=ea6c111eb8756ff6b0fa2386bebf5560d71d766a;hb=851d0b0452234ace66a511327bd8e6f9d68fe9e9;hp=062f0889ea3b44ec38d5da06cef4246a6f881d4a;hpb=52f0c2507638fb623088c9def5b2cc0bf652478e;p=apitrace diff --git a/trace_local_writer.cpp b/trace_local_writer.cpp index 062f088..ea6c111 100644 --- a/trace_local_writer.cpp +++ b/trace_local_writer.cpp @@ -30,9 +30,8 @@ #include #include -#include - #include "os.hpp" +#include "trace_file.hpp" #include "trace_writer.hpp" #include "trace_format.hpp" @@ -40,6 +39,22 @@ namespace Trace { +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 LocalWriter::open(void) { @@ -80,12 +95,20 @@ 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) { + if (!m_file->isOpened()) { open(); } @@ -94,21 +117,40 @@ 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 (m_file->isOpened()) { + m_file->flush(); + } + OS::ReleaseMutex(); + } +} + + +LocalWriter localWriter; + } /* namespace Trace */