]> git.cworth.org Git - apitrace/commitdiff
Try to be robust against fork.
authorJosé Fonseca <jfonseca@vmware.com>
Thu, 9 May 2013 17:32:37 +0000 (18:32 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Thu, 9 May 2013 17:32:37 +0000 (18:32 +0100)
common/os_process.hpp
common/trace_writer_local.cpp
common/trace_writer_local.hpp

index ba53408ccb8faa662df5d6f3ed021ce5e7d890f9..e1b1df17179e8457239e9b021d593b30d23eaac5 100644 (file)
@@ -33,6 +33,9 @@
 
 #ifdef _WIN32
 #include <windows.h>
+#else
+#include <sys/types.h>
+#include <unistd.h>
 #endif
 
 #include "os.hpp"
 namespace os {
 
 
-inline void
+typedef
+#ifdef _WIN32
+   DWORD
+#else
+   pid_t
+#endif
+ProcessId;
+
+
+static inline ProcessId
+getCurrentProcessId(void) {
+#ifdef _WIN32
+    return GetCurrentProcessId();
+#else
+    return getpid();
+#endif
+}
+
+
+static inline void
 setEnvironment(const char *name, const char *value) {
 #ifdef _WIN32
     SetEnvironmentVariableA(name, value);
@@ -51,7 +73,7 @@ setEnvironment(const char *name, const char *value) {
 }
 
 
-inline void
+static inline void
 unsetEnvironment(const char *name) {
 #ifdef _WIN32
     SetEnvironmentVariableA(name, NULL);
index 757e9c0fc328ce99dc94d9c7e33beaba1e49d158..a998193055b7fd94068f2478688f49964a4af059 100644 (file)
@@ -122,6 +122,8 @@ LocalWriter::open(void) {
         os::abort();
     }
 
+    pid = os::getCurrentProcessId();
+
 #if 0
     // For debugging the exception handler
     *((int *)0) = 0;
@@ -139,6 +141,17 @@ unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
 
     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
@@ -173,6 +186,11 @@ 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
index cc5dda0115eb425e462d12203bbb2c2bc4e65cd1..6e4abc44d60fad024e72fe347398c4badcaf32c1 100644 (file)
@@ -34,6 +34,7 @@
 #include <stdint.h>
 
 #include "os_thread.hpp"
+#include "os_process.hpp"
 #include "trace_writer.hpp"
 
 
@@ -70,6 +71,11 @@ namespace trace {
         os::recursive_mutex mutex;
         int acquired;
 
+        /**
+         * ID of the processed that opened the trace file.
+         */
+        os::ProcessId pid;
+
     public:
         /**
          * Should never called directly -- use localWriter singleton below