X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Ftrace_writer_local.cpp;h=feb5c912dfd06b2a61f2a003f9f9759bc23255e0;hb=c8695f74ce1ee6a93dee4b3f7da5a70a64706c82;hp=0730150e0f228d5fbb0fb6d8e9d6c611d1383002;hpb=447b3d5c7402b0a9e780a26dfed76376e723ac94;p=apitrace diff --git a/common/trace_writer_local.cpp b/common/trace_writer_local.cpp index 0730150..feb5c91 100644 --- a/common/trace_writer_local.cpp +++ b/common/trace_writer_local.cpp @@ -31,9 +31,10 @@ #include #include "os.hpp" +#include "os_thread.hpp" #include "os_string.hpp" #include "trace_file.hpp" -#include "trace_writer.hpp" +#include "trace_writer_local.hpp" #include "trace_format.hpp" @@ -88,7 +89,11 @@ LocalWriter::open(void) { #endif process.trimDirectory(); - os::String prefix = os::getCurrentDir(); +#ifdef ANDROID + os::String prefix = "/data"; +#else + os::String prefix = os::getCurrentDir(); +#endif prefix.join(process); for (;;) { @@ -123,25 +128,35 @@ LocalWriter::open(void) { #endif } +static unsigned next_thread_num = 1; +static thread_specific unsigned thread_num = 0; + unsigned LocalWriter::beginEnter(const FunctionSig *sig) { - os::acquireMutex(); + mutex.lock(); ++acquired; if (!m_file->isOpened()) { open(); } - return Writer::beginEnter(sig); + unsigned this_thread_num = thread_num; + if (!this_thread_num) { + this_thread_num = thread_num = next_thread_num++; + } + + assert(thread_num > 0); + unsigned thread_id = thread_num - 1; + return Writer::beginEnter(sig, thread_id); } void LocalWriter::endEnter(void) { Writer::endEnter(); --acquired; - os::releaseMutex(); + mutex.unlock(); } void LocalWriter::beginLeave(unsigned call) { - os::acquireMutex(); + mutex.lock(); ++acquired; Writer::beginLeave(call); } @@ -149,23 +164,28 @@ void LocalWriter::beginLeave(unsigned call) { void LocalWriter::endLeave(void) { Writer::endLeave(); --acquired; - os::releaseMutex(); + mutex.unlock(); } 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. + * while writing the file) as state could be inconsistent, therefore yield + * inconsistent trace files and/or repeated segfaults till infinity. */ - if (!acquired) { - os::acquireMutex(); + mutex.lock(); + if (acquired) { + os::log("apitrace: ignoring exception while tracing\n"); + } else { + ++acquired; if (m_file->isOpened()) { os::log("apitrace: flushing trace due to an exception\n"); m_file->flush(); } - os::releaseMutex(); + --acquired; } + mutex.unlock(); }