]> git.cworth.org Git - apitrace/blobdiff - common/trace_writer_local.cpp
d3dretrace: Dump D3D10 depth stencil surfaces as float.
[apitrace] / common / trace_writer_local.cpp
index a998193055b7fd94068f2478688f49964a4af059..529f83b2fbd88b8f5406860d6dece3aef0d8b758 100644 (file)
@@ -36,6 +36,7 @@
 #include "trace_file.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
@@ -135,23 +139,27 @@ static uintptr_t next_thread_num = 1;
 static OS_THREAD_SPECIFIC_PTR(void)
 thread_num;
 
-unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
+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, bool fake) {
     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
@@ -164,7 +172,16 @@ unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
 
     assert(this_thread_num);
     unsigned thread_id = this_thread_num - 1;
-    return Writer::beginEnter(sig, thread_id);
+    unsigned call_no = Writer::beginEnter(sig, thread_id);
+    if (!fake && os::backtrace_is_needed(sig->name)) {
+        std::vector<RawStackFrame> 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) {
@@ -186,11 +203,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 +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;
     }