]> git.cworth.org Git - apitrace/commitdiff
trace_write -> trace_writer
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Tue, 24 May 2011 07:42:59 +0000 (08:42 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Tue, 24 May 2011 07:42:59 +0000 (08:42 +0100)
15 files changed:
CMakeLists.txt
cgltrace.py
d3d10_1.py
d3d10misc.py
d3d8.py
d3d9.py
d3dshader.cpp
ddraw.py
glxtrace.py
gui/saverthread.cpp
trace_write.cpp [deleted file]
trace_write.hpp [deleted file]
trace_writer.cpp [new file with mode: 0644]
trace_writer.hpp [new file with mode: 0644]
wgltrace.py

index 2f0a83a46ff2ac836ce77c1218271114cfc9a21f..7b370757938c0382855ef7652b6bea481b537095 100755 (executable)
@@ -161,7 +161,7 @@ else (WIN32)
     set (glws glws_glx.cpp)
 endif (WIN32)
 
-add_library (trace trace_model.cpp trace_parser.cpp trace_write.cpp ${os})
+add_library (trace trace_model.cpp trace_parser.cpp trace_writer.cpp ${os})
 
 add_executable (tracedump tracedump.cpp)
 target_link_libraries (tracedump trace)
@@ -180,7 +180,7 @@ if (WIN32)
             COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d8.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d8.cpp
             DEPENDS d3d8.py trace.py d3d8types.py d3d8caps.py winapi.py stdapi.py
         )
-        add_library (d3d8 SHARED d3d8.def d3d8.cpp d3dshader.cpp trace_write.cpp os_win32.cpp)
+        add_library (d3d8 SHARED d3d8.def d3d8.cpp d3dshader.cpp trace_writer.cpp os_win32.cpp)
         set_target_properties (d3d8
             PROPERTIES PREFIX ""
             RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
@@ -197,7 +197,7 @@ if (WIN32)
             COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d9.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d9.cpp
             DEPENDS d3d9.py trace.py d3d9types.py d3d9caps.py winapi.py stdapi.py
         )
-        add_library (d3d9 SHARED d3d9.def d3d9.cpp d3dshader.cpp trace_write.cpp os_win32.cpp)
+        add_library (d3d9 SHARED d3d9.def d3d9.cpp d3dshader.cpp trace_writer.cpp os_win32.cpp)
         set_target_properties (d3d9
             PROPERTIES PREFIX ""
             RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/wrappers
@@ -214,7 +214,7 @@ if (WIN32)
     #        COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d10misc.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d10.cpp
     #        DEPENDS d3d10misc.py winapi.py stdapi.py
     #    )
-    #    add_library (d3d10 SHARED d3d10.def d3d10.cpp trace_write.cpp os_win32.cpp)
+    #    add_library (d3d10 SHARED d3d10.def d3d10.cpp trace_writer.cpp os_win32.cpp)
     #    set_target_properties (d3d10 PROPERTIES PREFIX "")
     #    install (TARGETS d3d10 RUNTIME DESTINATION wrappers)
     #endif (DirectX_D3D10_INCLUDE_DIR)
@@ -225,7 +225,7 @@ if (WIN32)
         COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/wgltrace.py > ${CMAKE_CURRENT_BINARY_DIR}/wgltrace.cpp
         DEPENDS wgltrace.py gltrace.py trace.py wglapi.py wglenum.py glapi.py glparams.py gltypes.py winapi.py stdapi.py
     )
-    add_library (wgltrace SHARED opengl32.def wgltrace.cpp trace_write.cpp os_win32.cpp ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp)
+    add_library (wgltrace SHARED opengl32.def wgltrace.cpp trace_writer.cpp os_win32.cpp ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp)
     set_target_properties (wgltrace PROPERTIES
         PREFIX ""
         OUTPUT_NAME opengl32
@@ -247,7 +247,7 @@ elseif (APPLE)
         DEPENDS cgltrace.py gltrace.py trace.py glxapi.py glapi.py glparams.py gltypes.py stdapi.py
     )
 
-    add_library (cgltrace SHARED cgltrace.cpp trace_write.cpp os_posix.cpp ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp)
+    add_library (cgltrace SHARED cgltrace.cpp trace_writer.cpp os_posix.cpp ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp)
 
     set_target_properties (cgltrace PROPERTIES
         # libGL.dylib
@@ -277,7 +277,7 @@ else ()
         DEPENDS glxtrace.py gltrace.py trace.py glxapi.py glapi.py glparams.py gltypes.py stdapi.py
     )
 
-    add_library (glxtrace SHARED glxtrace.cpp trace_write.cpp os_posix.cpp ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp)
+    add_library (glxtrace SHARED glxtrace.cpp trace_writer.cpp os_posix.cpp ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp)
 
     set_target_properties (glxtrace PROPERTIES
         # avoid the default "lib" prefix
index 33765f9b8d3b1ebfae3e81656c994e6434cc3cac..f850435ad92902b3f8685863c1fc7da79f6ab6f6 100644 (file)
@@ -50,7 +50,7 @@ if __name__ == '__main__':
     print '#endif'
     print '#include <dlfcn.h>'
     print
-    print '#include "trace_write.hpp"'
+    print '#include "trace_writer.hpp"'
     print
     print '// To validate our prototypes'
     print '#define GL_GLEXT_PROTOTYPES'
index c71e880bd1687ef72851e2405e1f4535daae0221..ebf28969ae422b44cb69ac8e37809d036049b4c6 100644 (file)
@@ -72,6 +72,6 @@ if __name__ == '__main__':
     print
     print '#include <d3d10_1.h>'
     print
-    print '#include "trace_write.hpp"'
+    print '#include "trace_writer.hpp"'
     print
     wrap()
index 00bd0e9f28a1353fa92c7851c1c0da32a7bd4a93..4553f190a820a6061cc9f22f89be5e9426b68c44 100644 (file)
@@ -64,6 +64,6 @@ if __name__ == '__main__':
     print
     print '#include <d3d10.h>'
     print
-    print '#include "trace_write.hpp"'
+    print '#include "trace_writer.hpp"'
     print
     wrap()
diff --git a/d3d8.py b/d3d8.py
index 07ddc475ce0832ec9fe76221e3df222a29a3712e..970b9c37cda84dedd8de6fa8d34078a094531ec6 100644 (file)
--- a/d3d8.py
+++ b/d3d8.py
@@ -296,7 +296,7 @@ if __name__ == '__main__':
     print '#include <d3d8.h>'
     print '#include "d3dshader.hpp"'
     print
-    print '#include "trace_write.hpp"'
+    print '#include "trace_writer.hpp"'
     print '#include "os.hpp"'
     print
     tracer = D3D8Tracer('d3d8.dll')
diff --git a/d3d9.py b/d3d9.py
index e2d5ba646b9eb86bfcf9191e057046d37e2f7bcd..81401627c43385031aad6b3735f07bf7e77f7e48 100644 (file)
--- a/d3d9.py
+++ b/d3d9.py
@@ -465,7 +465,7 @@ if __name__ == '__main__':
     print '#include "d3d9imports.hpp"'
     print '#include "d3dshader.hpp"'
     print
-    print '#include "trace_write.hpp"'
+    print '#include "trace_writer.hpp"'
     print '#include "os.hpp"'
     print
     tracer = D3D9Tracer('d3d9.dll')
index 799b35f9b07d6881677043d5648096e5f32fab7e..97f5d424299c042b7cf43aaf6ba63ef0cddf5342 100644 (file)
@@ -27,7 +27,7 @@
 
 #include <stdio.h>
 
-#include "trace_write.hpp"
+#include "trace_writer.hpp"
 #include "d3d9imports.hpp"
 #include "d3dshader.hpp"
 
index 0841a8a13c914152262d0ddbb01c295b646b38db..cc585ec85b34a353cd4062c0a411aae7d7e0056f 100644 (file)
--- a/ddraw.py
+++ b/ddraw.py
@@ -1619,6 +1619,6 @@ if __name__ == '__main__':
     print '#include <tchar.h>'
     print '#include <ddraw.h>'
     print
-    print '#include "trace_write.hpp"'
+    print '#include "trace_writer.hpp"'
     print
     wrap()
index b3c1b4f77e931488eee0b1ad9b446ea4e2de4bd4..dfc24df0dcf57c30cc78e9ce603ce40c356f8b8f 100644 (file)
@@ -58,7 +58,7 @@ if __name__ == '__main__':
     print '#endif'
     print '#include <dlfcn.h>'
     print
-    print '#include "trace_write.hpp"'
+    print '#include "trace_writer.hpp"'
     print
     print '// To validate our prototypes'
     print '#define GL_GLEXT_PROTOTYPES'
index b16a7708f561b6b062d47675766eebaf219f7c77..fe0f9a6c37ec78e7eaf9700948ed564886481214 100644 (file)
@@ -1,6 +1,6 @@
 #include "saverthread.h"
 
-#include "trace_write.hpp"
+#include "trace_writer.hpp"
 
 #include <QFile>
 #include <QHash>
diff --git a/trace_write.cpp b/trace_write.cpp
deleted file mode 100644 (file)
index 4dfba8a..0000000
+++ /dev/null
@@ -1,382 +0,0 @@
-/**************************************************************************
- *
- * Copyright 2007-2009 VMware, Inc.
- * All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- **************************************************************************/
-
-
-#include <assert.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <vector>
-
-#include <zlib.h>
-
-#include "os.hpp"
-#include "trace_write.hpp"
-#include "trace_format.hpp"
-
-
-namespace Trace {
-
-
-static gzFile g_gzFile = NULL;
-static void _Close(void) {
-    if (g_gzFile != NULL) {
-        gzclose(g_gzFile);
-        g_gzFile = NULL;
-    }
-}
-
-static void _Open(const char *szExtension) {
-    _Close();
-
-    static unsigned dwCounter = 0;
-
-    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);
-
-    g_gzFile = gzopen(szFileName, "wb");
-}
-
-static inline void Write(const void *sBuffer, size_t dwBytesToWrite) {
-    if (g_gzFile == NULL)
-        return;
-
-    gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
-}
-
-static inline void 
-WriteByte(char c) {
-    Write(&c, 1);
-}
-
-void inline 
-WriteUInt(unsigned long long value) {
-    char buf[2 * sizeof value];
-    unsigned len;
-
-    len = 0;
-    do {
-        assert(len < sizeof buf);
-        buf[len] = 0x80 | (value & 0x7f);
-        value >>= 7;
-        ++len;
-    } while (value);
-
-    assert(len);
-    buf[len - 1] &= 0x7f;
-
-    Write(buf, len);
-}
-
-static inline void 
-WriteFloat(float value) {
-    assert(sizeof value == 4);
-    Write((const char *)&value, sizeof value);
-}
-
-static inline void 
-WriteDouble(double value) {
-    assert(sizeof value == 8);
-    Write((const char *)&value, sizeof value);
-}
-
-static inline void 
-WriteString(const char *str) {
-    size_t len = strlen(str);
-    WriteUInt(len);
-    Write(str, len);
-}
-
-void Open(void) {
-    if (!g_gzFile) {
-        _Open("trace");
-        WriteUInt(TRACE_VERSION);
-    }
-}
-
-static unsigned call_no = 0;
-
-inline bool lookup(std::vector<bool> &map, size_t index) {
-    if (index >= map.size()) {
-        map.resize(index + 1);
-        return false;
-    } else {
-        return map[index];
-    }
-}
-
-static std::vector<bool> functions;
-static std::vector<bool> structs;
-static std::vector<bool> enums;
-static std::vector<bool> bitmasks;
-
-
-void Close(void) {
-    _Close();
-    call_no = 0;
-    functions = std::vector<bool>();
-    structs = std::vector<bool>();
-    enums = std::vector<bool>();
-    bitmasks = std::vector<bool>();
-}
-
-unsigned BeginEnter(const FunctionSig &function) {
-    OS::AcquireMutex();
-    Open();
-    WriteByte(Trace::EVENT_ENTER);
-    WriteUInt(function.id);
-    if (!lookup(functions, function.id)) {
-        WriteString(function.name);
-        WriteUInt(function.num_args);
-        for (unsigned i = 0; i < function.num_args; ++i) {
-            WriteString(function.args[i]);
-        }
-        functions[function.id] = true;
-    }
-    return call_no++;
-}
-
-void EndEnter(void) {
-    WriteByte(Trace::CALL_END);
-    gzflush(g_gzFile, Z_SYNC_FLUSH);
-    OS::ReleaseMutex();
-}
-
-void BeginLeave(unsigned call) {
-    OS::AcquireMutex();
-    WriteByte(Trace::EVENT_LEAVE);
-    WriteUInt(call);
-}
-
-void EndLeave(void) {
-    WriteByte(Trace::CALL_END);
-    gzflush(g_gzFile, Z_SYNC_FLUSH);
-    OS::ReleaseMutex();
-}
-
-void BeginArg(unsigned index) {
-    WriteByte(Trace::CALL_ARG);
-    WriteUInt(index);
-}
-
-void BeginReturn(void) {
-    WriteByte(Trace::CALL_RET);
-}
-
-void BeginArray(size_t length) {
-    WriteByte(Trace::TYPE_ARRAY);
-    WriteUInt(length);
-}
-
-void BeginStruct(const StructSig *sig) {
-    WriteByte(Trace::TYPE_STRUCT);
-    WriteUInt(sig->id);
-    if (!lookup(structs, sig->id)) {
-        WriteString(sig->name);
-        WriteUInt(sig->num_members);
-        for (unsigned i = 0; i < sig->num_members; ++i) {
-            WriteString(sig->members[i]);
-        }
-        structs[sig->id] = true;
-    }
-}
-
-void LiteralBool(bool value) {
-    WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
-}
-
-void LiteralSInt(signed long long value) {
-    if (value < 0) {
-        WriteByte(Trace::TYPE_SINT);
-        WriteUInt(-value);
-    } else {
-        WriteByte(Trace::TYPE_UINT);
-        WriteUInt(value);
-    }
-}
-
-void LiteralUInt(unsigned long long value) {
-    WriteByte(Trace::TYPE_UINT);
-    WriteUInt(value);
-}
-
-void LiteralFloat(float value) {
-    WriteByte(Trace::TYPE_FLOAT);
-    WriteFloat(value);
-}
-
-void LiteralDouble(double value) {
-    WriteByte(Trace::TYPE_DOUBLE);
-    WriteDouble(value);
-}
-
-void LiteralString(const char *str) {
-    if (!str) {
-        LiteralNull();
-        return;
-    }
-    WriteByte(Trace::TYPE_STRING);
-    WriteString(str);
-}
-
-void LiteralString(const char *str, size_t len) {
-    if (!str) {
-        LiteralNull();
-        return;
-    }
-    WriteByte(Trace::TYPE_STRING);
-    WriteUInt(len);
-    Write(str, len);
-}
-
-void LiteralWString(const wchar_t *str) {
-    if (!str) {
-        LiteralNull();
-        return;
-    }
-    WriteByte(Trace::TYPE_STRING);
-    WriteString("<wide-string>");
-}
-
-void LiteralBlob(const void *data, size_t size) {
-    if (!data) {
-        LiteralNull();
-        return;
-    }
-    WriteByte(Trace::TYPE_BLOB);
-    WriteUInt(size);
-    if (size) {
-        Write(data, size);
-    }
-}
-
-void LiteralEnum(const EnumSig *sig) {
-    WriteByte(Trace::TYPE_ENUM);
-    WriteUInt(sig->id);
-    if (!lookup(enums, sig->id)) {
-        WriteString(sig->name);
-        LiteralSInt(sig->value);
-        enums[sig->id] = true;
-    }
-}
-
-void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value) {
-    WriteByte(Trace::TYPE_BITMASK);
-    WriteUInt(bitmask.id);
-    if (!lookup(bitmasks, bitmask.id)) {
-        WriteUInt(bitmask.count);
-        for (unsigned i = 0; i < bitmask.count; ++i) {
-            if (i != 0 && bitmask.values[i].value == 0) {
-                OS::DebugMessage("apitrace: bitmask %s is zero but is not first flag\n", bitmask.values[i].name);
-            }
-            WriteString(bitmask.values[i].name);
-            WriteUInt(bitmask.values[i].value);
-        }
-        bitmasks[bitmask.id] = true;
-    }
-    WriteUInt(value);
-}
-
-void LiteralNull(void) {
-    WriteByte(Trace::TYPE_NULL);
-}
-
-void LiteralOpaque(const void *addr) {
-    if (!addr) {
-        LiteralNull();
-        return;
-    }
-    WriteByte(Trace::TYPE_OPAQUE);
-    WriteUInt((size_t)addr);
-}
-
-void Abort(void) {
-    Close();
-    OS::Abort();
-}
-
-} /* namespace Trace */
-
-
-#ifdef _WIN32
-
-#if 0
-BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
-    switch(fdwReason) {
-    case DLL_PROCESS_ATTACH:
-    case DLL_THREAD_ATTACH:
-        return TRUE;
-    case DLL_THREAD_DETACH:
-        return TRUE;
-    case DLL_PROCESS_DETACH:
-        Trace::Close();
-        return TRUE;
-    }
-    (void)hinstDLL;
-    (void)lpvReserved;
-    return TRUE;
-}
-#endif
-
-#else
-
-static void _uninit(void) __attribute__((destructor));
-static void _uninit(void) {
-    Trace::Close();
-}
-
-#endif
diff --git a/trace_write.hpp b/trace_write.hpp
deleted file mode 100644 (file)
index 8797e56..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-/**************************************************************************
- *
- * Copyright 2007-2010 VMware, Inc.
- * All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- **************************************************************************/
-
-/*
- * Trace writing functions.
- */
-
-#ifndef _TRACE_WRITE_HPP_
-#define _TRACE_WRITE_HPP_
-
-#include <stddef.h>
-
-namespace Trace {
-
-    typedef unsigned Id;
-
-    struct FunctionSig {
-        Id id;
-        const char *name;
-        unsigned num_args;
-        const char **args;
-    };
-
-    struct StructSig {
-        Id id;
-        const char *name;
-        unsigned num_members;
-        const char **members;
-    };
-
-    struct EnumSig {
-        Id id;
-        const char *name;
-        signed long long value;
-    };
-
-    struct BitmaskVal {
-        const char *name;
-        unsigned long long value;
-    };
-
-    struct BitmaskSig {
-        Id id;
-        unsigned count;
-        const BitmaskVal *values;
-    };
-
-    void Open(void);
-    void Close(void);
-
-    unsigned BeginEnter(const FunctionSig &function);
-    void EndEnter(void);
-
-    void BeginLeave(unsigned call);
-    void EndLeave(void);
-
-    void BeginArg(unsigned index);
-    inline void EndArg(void) {}
-
-    void BeginReturn(void);
-    inline void EndReturn(void) {}
-
-    void BeginArray(size_t length);
-    inline void EndArray(void) {}
-
-    inline void BeginElement(void) {}
-    inline void EndElement(void) {}
-
-    void BeginStruct(const StructSig *sig);
-    inline void EndStruct(void) {}
-
-    void LiteralBool(bool value);
-    void LiteralSInt(signed long long value);
-    void LiteralUInt(unsigned long long value);
-    void LiteralFloat(float value);
-    void LiteralDouble(double value);
-    void LiteralString(const char *str);
-    void LiteralString(const char *str, size_t size);
-    void LiteralWString(const wchar_t *str);
-    void LiteralBlob(const void *data, size_t size);
-    void LiteralEnum(const EnumSig *sig);
-    void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value);
-    void LiteralNull(void);
-    void LiteralOpaque(const void *ptr);
-
-    void Abort(void);
-}
-
-#endif /* _TRACE_WRITE_HPP_ */
diff --git a/trace_writer.cpp b/trace_writer.cpp
new file mode 100644 (file)
index 0000000..a97cef2
--- /dev/null
@@ -0,0 +1,382 @@
+/**************************************************************************
+ *
+ * Copyright 2007-2009 VMware, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#include <assert.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <vector>
+
+#include <zlib.h>
+
+#include "os.hpp"
+#include "trace_writer.hpp"
+#include "trace_format.hpp"
+
+
+namespace Trace {
+
+
+static gzFile g_gzFile = NULL;
+static void _Close(void) {
+    if (g_gzFile != NULL) {
+        gzclose(g_gzFile);
+        g_gzFile = NULL;
+    }
+}
+
+static void _Open(const char *szExtension) {
+    _Close();
+
+    static unsigned dwCounter = 0;
+
+    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);
+
+    g_gzFile = gzopen(szFileName, "wb");
+}
+
+static inline void Write(const void *sBuffer, size_t dwBytesToWrite) {
+    if (g_gzFile == NULL)
+        return;
+
+    gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
+}
+
+static inline void 
+WriteByte(char c) {
+    Write(&c, 1);
+}
+
+void inline 
+WriteUInt(unsigned long long value) {
+    char buf[2 * sizeof value];
+    unsigned len;
+
+    len = 0;
+    do {
+        assert(len < sizeof buf);
+        buf[len] = 0x80 | (value & 0x7f);
+        value >>= 7;
+        ++len;
+    } while (value);
+
+    assert(len);
+    buf[len - 1] &= 0x7f;
+
+    Write(buf, len);
+}
+
+static inline void 
+WriteFloat(float value) {
+    assert(sizeof value == 4);
+    Write((const char *)&value, sizeof value);
+}
+
+static inline void 
+WriteDouble(double value) {
+    assert(sizeof value == 8);
+    Write((const char *)&value, sizeof value);
+}
+
+static inline void 
+WriteString(const char *str) {
+    size_t len = strlen(str);
+    WriteUInt(len);
+    Write(str, len);
+}
+
+void Open(void) {
+    if (!g_gzFile) {
+        _Open("trace");
+        WriteUInt(TRACE_VERSION);
+    }
+}
+
+static unsigned call_no = 0;
+
+inline bool lookup(std::vector<bool> &map, size_t index) {
+    if (index >= map.size()) {
+        map.resize(index + 1);
+        return false;
+    } else {
+        return map[index];
+    }
+}
+
+static std::vector<bool> functions;
+static std::vector<bool> structs;
+static std::vector<bool> enums;
+static std::vector<bool> bitmasks;
+
+
+void Close(void) {
+    _Close();
+    call_no = 0;
+    functions = std::vector<bool>();
+    structs = std::vector<bool>();
+    enums = std::vector<bool>();
+    bitmasks = std::vector<bool>();
+}
+
+unsigned BeginEnter(const FunctionSig &function) {
+    OS::AcquireMutex();
+    Open();
+    WriteByte(Trace::EVENT_ENTER);
+    WriteUInt(function.id);
+    if (!lookup(functions, function.id)) {
+        WriteString(function.name);
+        WriteUInt(function.num_args);
+        for (unsigned i = 0; i < function.num_args; ++i) {
+            WriteString(function.args[i]);
+        }
+        functions[function.id] = true;
+    }
+    return call_no++;
+}
+
+void EndEnter(void) {
+    WriteByte(Trace::CALL_END);
+    gzflush(g_gzFile, Z_SYNC_FLUSH);
+    OS::ReleaseMutex();
+}
+
+void BeginLeave(unsigned call) {
+    OS::AcquireMutex();
+    WriteByte(Trace::EVENT_LEAVE);
+    WriteUInt(call);
+}
+
+void EndLeave(void) {
+    WriteByte(Trace::CALL_END);
+    gzflush(g_gzFile, Z_SYNC_FLUSH);
+    OS::ReleaseMutex();
+}
+
+void BeginArg(unsigned index) {
+    WriteByte(Trace::CALL_ARG);
+    WriteUInt(index);
+}
+
+void BeginReturn(void) {
+    WriteByte(Trace::CALL_RET);
+}
+
+void BeginArray(size_t length) {
+    WriteByte(Trace::TYPE_ARRAY);
+    WriteUInt(length);
+}
+
+void BeginStruct(const StructSig *sig) {
+    WriteByte(Trace::TYPE_STRUCT);
+    WriteUInt(sig->id);
+    if (!lookup(structs, sig->id)) {
+        WriteString(sig->name);
+        WriteUInt(sig->num_members);
+        for (unsigned i = 0; i < sig->num_members; ++i) {
+            WriteString(sig->members[i]);
+        }
+        structs[sig->id] = true;
+    }
+}
+
+void LiteralBool(bool value) {
+    WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
+}
+
+void LiteralSInt(signed long long value) {
+    if (value < 0) {
+        WriteByte(Trace::TYPE_SINT);
+        WriteUInt(-value);
+    } else {
+        WriteByte(Trace::TYPE_UINT);
+        WriteUInt(value);
+    }
+}
+
+void LiteralUInt(unsigned long long value) {
+    WriteByte(Trace::TYPE_UINT);
+    WriteUInt(value);
+}
+
+void LiteralFloat(float value) {
+    WriteByte(Trace::TYPE_FLOAT);
+    WriteFloat(value);
+}
+
+void LiteralDouble(double value) {
+    WriteByte(Trace::TYPE_DOUBLE);
+    WriteDouble(value);
+}
+
+void LiteralString(const char *str) {
+    if (!str) {
+        LiteralNull();
+        return;
+    }
+    WriteByte(Trace::TYPE_STRING);
+    WriteString(str);
+}
+
+void LiteralString(const char *str, size_t len) {
+    if (!str) {
+        LiteralNull();
+        return;
+    }
+    WriteByte(Trace::TYPE_STRING);
+    WriteUInt(len);
+    Write(str, len);
+}
+
+void LiteralWString(const wchar_t *str) {
+    if (!str) {
+        LiteralNull();
+        return;
+    }
+    WriteByte(Trace::TYPE_STRING);
+    WriteString("<wide-string>");
+}
+
+void LiteralBlob(const void *data, size_t size) {
+    if (!data) {
+        LiteralNull();
+        return;
+    }
+    WriteByte(Trace::TYPE_BLOB);
+    WriteUInt(size);
+    if (size) {
+        Write(data, size);
+    }
+}
+
+void LiteralEnum(const EnumSig *sig) {
+    WriteByte(Trace::TYPE_ENUM);
+    WriteUInt(sig->id);
+    if (!lookup(enums, sig->id)) {
+        WriteString(sig->name);
+        LiteralSInt(sig->value);
+        enums[sig->id] = true;
+    }
+}
+
+void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value) {
+    WriteByte(Trace::TYPE_BITMASK);
+    WriteUInt(bitmask.id);
+    if (!lookup(bitmasks, bitmask.id)) {
+        WriteUInt(bitmask.count);
+        for (unsigned i = 0; i < bitmask.count; ++i) {
+            if (i != 0 && bitmask.values[i].value == 0) {
+                OS::DebugMessage("apitrace: bitmask %s is zero but is not first flag\n", bitmask.values[i].name);
+            }
+            WriteString(bitmask.values[i].name);
+            WriteUInt(bitmask.values[i].value);
+        }
+        bitmasks[bitmask.id] = true;
+    }
+    WriteUInt(value);
+}
+
+void LiteralNull(void) {
+    WriteByte(Trace::TYPE_NULL);
+}
+
+void LiteralOpaque(const void *addr) {
+    if (!addr) {
+        LiteralNull();
+        return;
+    }
+    WriteByte(Trace::TYPE_OPAQUE);
+    WriteUInt((size_t)addr);
+}
+
+void Abort(void) {
+    Close();
+    OS::Abort();
+}
+
+} /* namespace Trace */
+
+
+#ifdef _WIN32
+
+#if 0
+BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
+    switch(fdwReason) {
+    case DLL_PROCESS_ATTACH:
+    case DLL_THREAD_ATTACH:
+        return TRUE;
+    case DLL_THREAD_DETACH:
+        return TRUE;
+    case DLL_PROCESS_DETACH:
+        Trace::Close();
+        return TRUE;
+    }
+    (void)hinstDLL;
+    (void)lpvReserved;
+    return TRUE;
+}
+#endif
+
+#else
+
+static void _uninit(void) __attribute__((destructor));
+static void _uninit(void) {
+    Trace::Close();
+}
+
+#endif
diff --git a/trace_writer.hpp b/trace_writer.hpp
new file mode 100644 (file)
index 0000000..d41acde
--- /dev/null
@@ -0,0 +1,111 @@
+/**************************************************************************
+ *
+ * Copyright 2007-2010 VMware, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+/*
+ * Trace writing functions.
+ */
+
+#ifndef _TRACE_WRITER_HPP_
+#define _TRACE_WRITER_HPP_
+
+#include <stddef.h>
+
+namespace Trace {
+
+    typedef unsigned Id;
+
+    struct FunctionSig {
+        Id id;
+        const char *name;
+        unsigned num_args;
+        const char **args;
+    };
+
+    struct StructSig {
+        Id id;
+        const char *name;
+        unsigned num_members;
+        const char **members;
+    };
+
+    struct EnumSig {
+        Id id;
+        const char *name;
+        signed long long value;
+    };
+
+    struct BitmaskVal {
+        const char *name;
+        unsigned long long value;
+    };
+
+    struct BitmaskSig {
+        Id id;
+        unsigned count;
+        const BitmaskVal *values;
+    };
+
+    void Open(void);
+    void Close(void);
+
+    unsigned BeginEnter(const FunctionSig &function);
+    void EndEnter(void);
+
+    void BeginLeave(unsigned call);
+    void EndLeave(void);
+
+    void BeginArg(unsigned index);
+    inline void EndArg(void) {}
+
+    void BeginReturn(void);
+    inline void EndReturn(void) {}
+
+    void BeginArray(size_t length);
+    inline void EndArray(void) {}
+
+    inline void BeginElement(void) {}
+    inline void EndElement(void) {}
+
+    void BeginStruct(const StructSig *sig);
+    inline void EndStruct(void) {}
+
+    void LiteralBool(bool value);
+    void LiteralSInt(signed long long value);
+    void LiteralUInt(unsigned long long value);
+    void LiteralFloat(float value);
+    void LiteralDouble(double value);
+    void LiteralString(const char *str);
+    void LiteralString(const char *str, size_t size);
+    void LiteralWString(const wchar_t *str);
+    void LiteralBlob(const void *data, size_t size);
+    void LiteralEnum(const EnumSig *sig);
+    void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value);
+    void LiteralNull(void);
+    void LiteralOpaque(const void *ptr);
+
+    void Abort(void);
+}
+
+#endif /* _TRACE_WRITER_HPP_ */
index 028580d2f6d7a1e4ed57cc783a11b19e6673ef2b..ea4474d95fe2e3cc16575c1bbfb122b8f90f85f8 100644 (file)
@@ -66,7 +66,7 @@ if __name__ == '__main__':
     print '#include <string.h>'
     print '#include <windows.h>'
     print
-    print '#include "trace_write.hpp"'
+    print '#include "trace_writer.hpp"'
     print '#include "os.hpp"'
     print
     print '''