X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Ftrace_writer_local.cpp;h=c5a5c4447b2b4213bea2603f6fd905f554c67bca;hb=52398312caaba3e1bc0613016d5bf3507d1e242a;hp=feb5c912dfd06b2a61f2a003f9f9759bc23255e0;hpb=4647f208f5a2b87391281e0f1202f66c23943bd9;p=apitrace diff --git a/common/trace_writer_local.cpp b/common/trace_writer_local.cpp index feb5c91..c5a5c44 100644 --- a/common/trace_writer_local.cpp +++ b/common/trace_writer_local.cpp @@ -36,6 +36,7 @@ #include "trace_file.hpp" #include "trace_writer_local.hpp" #include "trace_format.hpp" +#include "trace_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 @@ -122,31 +126,62 @@ LocalWriter::open(void) { os::abort(); } + pid = os::getCurrentProcessId(); + #if 0 // For debugging the exception handler *((int *)0) = 0; #endif } -static unsigned next_thread_num = 1; -static thread_specific unsigned thread_num = 0; +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) { +unsigned LocalWriter::beginEnter(const FunctionSig *sig, bool fake) { mutex.lock(); ++acquired; + checkProcessId(); if (!m_file->isOpened()) { open(); } - unsigned this_thread_num = thread_num; + // 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 = thread_num = next_thread_num++; + this_thread_num = next_thread_num++; + thread_num = reinterpret_cast(this_thread_num); } - assert(thread_num > 0); - unsigned thread_id = thread_num - 1; - 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 && backtrace_is_needed(sig->name)) { + std::vector backtrace = get_backtrace(); + beginBacktrace(backtrace.size()); + for (unsigned i = 0; i < backtrace.size(); ++i) { + writeStackFrame(&backtrace[i]); + } + endBacktrace(); + } + return call_no; } void LocalWriter::endEnter(void) { @@ -180,8 +215,12 @@ void LocalWriter::flush(void) { } 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(); + } } --acquired; }