]> git.cworth.org Git - apitrace/blobdiff - common/trace_writer_local.cpp
Use AddVectoredExceptionHandler
[apitrace] / common / trace_writer_local.cpp
index d03e1aefc141e26bdb00c7bf56a1487ecf010678..77c8bd67de3785db908e02a7dbf574f2385f0167 100644 (file)
@@ -31,6 +31,8 @@
 #include <string.h>
 
 #include "os.hpp"
+#include "os_thread.hpp"
+#include "os_string.hpp"
 #include "trace_file.hpp"
 #include "trace_writer.hpp"
 #include "trace_format.hpp"
@@ -73,32 +75,33 @@ LocalWriter::~LocalWriter()
 
 void
 LocalWriter::open(void) {
+    os::String szFileName;
 
-    static unsigned dwCounter = 0;
-
-    const char *szExtension = "trace";
-    char szFileName[PATH_MAX];
     const char *lpFileName;
 
     lpFileName = getenv("TRACE_FILE");
-    if (lpFileName) {
-        strncpy(szFileName, lpFileName, PATH_MAX);
-    }
-    else {
-        char szProcessName[PATH_MAX];
-        char szCurrentDir[PATH_MAX];
-        os::getProcessName(szProcessName, PATH_MAX);
-        os::getCurrentDir(szCurrentDir, PATH_MAX);
+    if (!lpFileName) {
+        static unsigned dwCounter = 0;
+
+        os::String process = os::getProcessName();
+#ifdef _WIN32
+        process.trimExtension();
+#endif
+        process.trimDirectory();
+
+        os::String prefix = os::getCurrentDir();
+        prefix.join(process);
 
         for (;;) {
             FILE *file;
 
             if (dwCounter)
-                snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
+                szFileName = os::String::format("%s.%u.trace", prefix.str(), dwCounter);
             else
-                snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
+                szFileName = os::String::format("%s.trace", prefix.str());
 
-            file = fopen(szFileName, "rb");
+            lpFileName = szFileName;
+            file = fopen(lpFileName, "rb");
             if (file == NULL)
                 break;
 
@@ -108,9 +111,12 @@ LocalWriter::open(void) {
         }
     }
 
-    os::log("apitrace: tracing to %s\n", szFileName);
+    os::log("apitrace: tracing to %s\n", lpFileName);
 
-    Writer::open(szFileName);
+    if (!Writer::open(lpFileName)) {
+        os::log("apitrace: error: failed to open %s\n", lpFileName);
+        os::abort();
+    }
 
 #if 0
     // For debugging the exception handler
@@ -118,6 +124,9 @@ LocalWriter::open(void) {
 #endif
 }
 
+static unsigned next_thread_id = 0;
+static os::thread_specific_ptr<unsigned> thread_id_specific_ptr;
+
 unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
     os::acquireMutex();
     ++acquired;
@@ -126,7 +135,18 @@ unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
         open();
     }
 
-    return Writer::beginEnter(sig);
+    unsigned *thread_id_ptr = thread_id_specific_ptr.get();
+    unsigned thread_id;
+    if (thread_id_ptr) {
+        thread_id = *thread_id_ptr;
+    } else {
+        thread_id = next_thread_id++;
+        thread_id_ptr = new unsigned;
+        *thread_id_ptr = thread_id;
+        thread_id_specific_ptr.reset(thread_id_ptr);
+    }
+
+    return Writer::beginEnter(sig, thread_id);
 }
 
 void LocalWriter::endEnter(void) {