]> git.cworth.org Git - apitrace/commitdiff
Specialized Writer for tracing the current process.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Sat, 20 Aug 2011 12:01:37 +0000 (13:01 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Sat, 20 Aug 2011 12:01:37 +0000 (13:01 +0100)
No need to burden the GUI and other tools with mutexes, and file sync/flushes.

trace.py
trace_writer.cpp
trace_writer.hpp

index d9d50acfcf096034443502f2c24740366b2c5f2e..90cc426cd7688d8a7014c3ca5e74b911734f5418 100644 (file)
--- a/trace.py
+++ b/trace.py
@@ -309,7 +309,7 @@ class Tracer:
         self.footer(api)
 
     def header(self, api):
-        print 'Trace::Writer __writer;'
+        print 'Trace::LocalWriter __writer;'
         print
 
     def footer(self, api):
index af8d62f7aee45602d4d8f18e8d7abfe9dda5f34a..7fcd0e6762b24acd247d536e670a5d36b99ef8ac 100644 (file)
@@ -79,48 +79,6 @@ Writer::open(const char *filename) {
     return true;
 }
 
-void
-Writer::open(void) {
-
-    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);
-
-        for (;;) {
-            FILE *file;
-
-            if (dwCounter)
-                snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
-            else
-                snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
-
-            file = fopen(szFileName, "rb");
-            if (file == NULL)
-                break;
-
-            fclose(file);
-
-            ++dwCounter;
-        }
-    }
-
-    OS::DebugMessage("apitrace: tracing to %s\n", szFileName);
-
-    open(szFileName);
-}
-
 void inline
 Writer::_write(const void *sBuffer, size_t dwBytesToWrite) {
     if (g_gzFile == NULL)
@@ -182,12 +140,6 @@ inline bool lookup(std::vector<bool> &map, size_t index) {
 }
 
 unsigned Writer::beginEnter(const FunctionSig *sig) {
-    OS::AcquireMutex();
-
-    if (!g_gzFile) {
-        open();
-    }
-
     _writeByte(Trace::EVENT_ENTER);
     _writeUInt(sig->id);
     if (!lookup(functions, sig->id)) {
@@ -204,20 +156,15 @@ unsigned Writer::beginEnter(const FunctionSig *sig) {
 
 void Writer::endEnter(void) {
     _writeByte(Trace::CALL_END);
-    gzflush(g_gzFile, Z_SYNC_FLUSH);
-    OS::ReleaseMutex();
 }
 
 void Writer::beginLeave(unsigned call) {
-    OS::AcquireMutex();
     _writeByte(Trace::EVENT_LEAVE);
     _writeUInt(call);
 }
 
 void Writer::endLeave(void) {
     _writeByte(Trace::CALL_END);
-    gzflush(g_gzFile, Z_SYNC_FLUSH);
-    OS::ReleaseMutex();
 }
 
 void Writer::beginArg(unsigned index) {
@@ -356,5 +303,76 @@ void Writer::writeOpaque(const void *addr) {
     _writeUInt((size_t)addr);
 }
 
+
+void
+LocalWriter::open(void) {
+
+    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);
+
+        for (;;) {
+            FILE *file;
+
+            if (dwCounter)
+                snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
+            else
+                snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
+
+            file = fopen(szFileName, "rb");
+            if (file == NULL)
+                break;
+
+            fclose(file);
+
+            ++dwCounter;
+        }
+    }
+
+    OS::DebugMessage("apitrace: tracing to %s\n", szFileName);
+
+    Writer::open(szFileName);
+}
+
+unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
+    OS::AcquireMutex();
+
+    if (!g_gzFile) {
+        open();
+    }
+
+    return Writer::beginEnter(sig);
+}
+
+void LocalWriter::endEnter(void) {
+    Writer::endEnter();
+    gzflush(g_gzFile, Z_SYNC_FLUSH);
+    OS::ReleaseMutex();
+}
+
+void LocalWriter::beginLeave(unsigned call) {
+    OS::AcquireMutex();
+    Writer::beginLeave(call);
+}
+
+void LocalWriter::endLeave(void) {
+    Writer::endLeave();
+    gzflush(g_gzFile, Z_SYNC_FLUSH);
+    OS::ReleaseMutex();
+}
+
+
 } /* namespace Trace */
 
index 4f9cd2b27a32e913bad5889fe32c750afa099a6f..20a153093f4fc0b5d0f95411b0833ab5482a1657 100644 (file)
@@ -54,7 +54,6 @@ namespace Trace {
         Writer();
         ~Writer();
 
-        void open(void);
         bool open(const char *filename);
         void close(void);
 
@@ -104,6 +103,27 @@ namespace Trace {
         void inline _writeString(const char *str);
 
     };
+
+    /**
+     * A specialized Writer class, mean to trace the current process.
+     *
+     * In particular:
+     * - it creates a trace file based on the current process name
+     * - uses mutexes to allow tracing from multiple threades
+     * - flushes the output to ensure the last call is traced in event of
+     *   abnormal termination
+     */
+    class LocalWriter : public Writer {
+    protected:
+    public:
+        void open(void);
+
+        unsigned beginEnter(const FunctionSig *sig);
+        void endEnter(void);
+
+        void beginLeave(unsigned call);
+        void endLeave(void);
+    };
 }
 
 #endif /* _TRACE_WRITER_HPP_ */