From: José Fonseca Date: Fri, 10 May 2013 11:27:26 +0000 (+0100) Subject: trace: Check process id also when the destructor is invoked. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=a880472a469e2acdc004176166161a6772c37f1f;p=apitrace trace: Check process id also when the destructor is invoked. --- diff --git a/common/trace_writer_local.cpp b/common/trace_writer_local.cpp index a998193..4d946f5 100644 --- a/common/trace_writer_local.cpp +++ b/common/trace_writer_local.cpp @@ -71,6 +71,7 @@ LocalWriter::LocalWriter() : LocalWriter::~LocalWriter() { os::resetExceptionCallback(); + checkProcessId(); } void @@ -135,23 +136,27 @@ 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) { mutex.lock(); ++acquired; + checkProcessId(); if (!m_file->isOpened()) { open(); - } else { - if (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(); - } } // Although thread_num is a void *, we actually use it as a uintptr_t @@ -186,11 +191,6 @@ void LocalWriter::endLeave(void) { } void LocalWriter::flush(void) { - if (os::getCurrentProcessId() != pid) { - os::log("apitrace: ignoring exception in child process\n"); - return; - } - /* * Do nothing if the mutex is already acquired (e.g., if a segfault happen * while writing the file) as state could be inconsistent, therefore yield @@ -203,8 +203,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; } diff --git a/common/trace_writer_local.hpp b/common/trace_writer_local.hpp index 6e4abc4..815e3c0 100644 --- a/common/trace_writer_local.hpp +++ b/common/trace_writer_local.hpp @@ -76,6 +76,8 @@ namespace trace { */ os::ProcessId pid; + void checkProcessId(); + public: /** * Should never called directly -- use localWriter singleton below