X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Ftrace_writer_local.cpp;h=529f83b2fbd88b8f5406860d6dece3aef0d8b758;hb=0f48706319d715bea08cad23fc233a46b9f9f224;hp=77c8bd67de3785db908e02a7dbf574f2385f0167;hpb=5926761f1682c26266a78f2674e58beff3bfae88;p=apitrace diff --git a/common/trace_writer_local.cpp b/common/trace_writer_local.cpp index 77c8bd6..529f83b 100644 --- a/common/trace_writer_local.cpp +++ b/common/trace_writer_local.cpp @@ -34,8 +34,9 @@ #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" +#include "os_backtrace.hpp" namespace trace { @@ -63,6 +64,8 @@ static void exceptionCallback(void) LocalWriter::LocalWriter() : acquired(0) { + os::log("apitrace: loaded\n"); + // Install the signal handlers as early as possible, to prevent // interfering with the application's signal handling. os::setExceptionCallback(exceptionCallback); @@ -71,6 +74,7 @@ LocalWriter::LocalWriter() : LocalWriter::~LocalWriter() { os::resetExceptionCallback(); + checkProcessId(); } void @@ -89,7 +93,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 (;;) { @@ -118,45 +126,72 @@ LocalWriter::open(void) { os::abort(); } + pid = os::getCurrentProcessId(); + #if 0 // For debugging the exception handler *((int *)0) = 0; #endif } -static unsigned next_thread_id = 0; -static os::thread_specific_ptr thread_id_specific_ptr; +static uintptr_t next_thread_num = 1; + +static OS_THREAD_SPECIFIC_PTR(void) +thread_num; + +void LocalWriter::checkProcessId(void) { + if (m_file->isOpened() && + os::getCurrentProcessId() != pid) { + // We are a forked child process that inherited the trace file, so + // create a new file. We can't call any method of the current + // file, as it may cause it to flush and corrupt the parent's + // trace, so we effectively leak the old file object. + m_file = File::createSnappy(); + // Don't want to open the same file again + os::unsetEnvironment("TRACE_FILE"); + open(); + } +} -unsigned LocalWriter::beginEnter(const FunctionSig *sig) { - os::acquireMutex(); +unsigned LocalWriter::beginEnter(const FunctionSig *sig, bool fake) { + mutex.lock(); ++acquired; + checkProcessId(); if (!m_file->isOpened()) { open(); } - unsigned *thread_id_ptr = thread_id_specific_ptr.get(); - unsigned thread_id; - if (thread_id_ptr) { - thread_id = *thread_id_ptr; - } else { - thread_id = next_thread_id++; - thread_id_ptr = new unsigned; - *thread_id_ptr = thread_id; - thread_id_specific_ptr.reset(thread_id_ptr); + // Although thread_num is a void *, we actually use it as a uintptr_t + uintptr_t this_thread_num = + reinterpret_cast(static_cast(thread_num)); + if (!this_thread_num) { + this_thread_num = next_thread_num++; + thread_num = reinterpret_cast(this_thread_num); } - return Writer::beginEnter(sig, thread_id); + assert(this_thread_num); + unsigned thread_id = this_thread_num - 1; + unsigned call_no = Writer::beginEnter(sig, thread_id); + if (!fake && os::backtrace_is_needed(sig->name)) { + std::vector backtrace = os::get_backtrace(); + beginBacktrace(backtrace.size()); + for (unsigned i = 0; i < backtrace.size(); ++i) { + writeStackFrame(&backtrace[i]); + } + endBacktrace(); + } + return call_no; } void LocalWriter::endEnter(void) { Writer::endEnter(); --acquired; - os::releaseMutex(); + mutex.unlock(); } void LocalWriter::beginLeave(unsigned call) { - os::acquireMutex(); + mutex.lock(); ++acquired; Writer::beginLeave(call); } @@ -164,23 +199,32 @@ 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(); + if (os::getCurrentProcessId() != pid) { + os::log("apitrace: ignoring exception in child process\n"); + } else { + os::log("apitrace: flushing trace due to an exception\n"); + m_file->flush(); + } } - os::releaseMutex(); + --acquired; } + mutex.unlock(); }