]> git.cworth.org Git - apitrace/commitdiff
trace: Check process id also when the destructor is invoked.
authorJosé Fonseca <jfonseca@vmware.com>
Fri, 10 May 2013 11:27:26 +0000 (12:27 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Fri, 10 May 2013 11:27:26 +0000 (12:27 +0100)
common/trace_writer_local.cpp
common/trace_writer_local.hpp

index a998193055b7fd94068f2478688f49964a4af059..4d946f5c0a53a50aa5b9ba91d323ffa4d76101a6 100644 (file)
@@ -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;
     }
index 6e4abc44d60fad024e72fe347398c4badcaf32c1..815e3c0fe2ca55171ea6537826380f81d1bf899d 100644 (file)
@@ -76,6 +76,8 @@ namespace trace {
          */
         os::ProcessId pid;
 
+        void checkProcessId();
+
     public:
         /**
          * Should never called directly -- use localWriter singleton below