From: José Fonseca Date: Fri, 26 Nov 2010 11:35:54 +0000 (+0000) Subject: log - > trace X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=f2efceaae999b2c5b9f2a6561915f9765e22dcc7;p=apitrace log - > trace --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fc59ee..04c7a44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,7 +102,7 @@ if (WIN32) # COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d8.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d8.cpp # DEPENDS d3d8.py d3d8types.py d3d8caps.py winapi.py stdapi.py # ) - # add_library (d3d8 SHARED d3d8.def d3d8.cpp log.cpp os_win32.cpp) + # add_library (d3d8 SHARED d3d8.def d3d8.cpp trace_write.cpp os_win32.cpp) # set_target_properties (d3d8 PROPERTIES PREFIX "") #endif (DirectX_D3D8_FOUND) @@ -114,7 +114,7 @@ if (WIN32) # COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d9.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d9.cpp # DEPENDS d3d9.py d3d9types.py d3d9caps.py d3dshader.py winapi.py stdapi.py # ) - # add_library (d3d9 SHARED d3d9.def d3d9.cpp log.cpp os_win32.cpp) + # add_library (d3d9 SHARED d3d9.def d3d9.cpp trace_write.cpp os_win32.cpp) # set_target_properties (d3d9 PROPERTIES PREFIX "") #endif (DirectX_D3DX9_FOUND) @@ -126,7 +126,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 log.cpp os_win32.cpp) + # add_library (d3d10 SHARED d3d10.def d3d10.cpp trace_write.cpp os_win32.cpp) # set_target_properties (d3d10 PROPERTIES PREFIX "") #endif (DirectX_D3D10_FOUND) @@ -136,7 +136,7 @@ if (WIN32) COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/wgltrace.py > ${CMAKE_CURRENT_BINARY_DIR}/opengl32.cpp DEPENDS wgltrace.py trace.py wglapi.py glapi.py glenum.py winapi.py stdapi.py ) - add_library (opengl SHARED opengl32.def opengl32.cpp log.cpp os_win32.cpp) + add_library (opengl SHARED opengl32.def opengl32.cpp trace_write.cpp os_win32.cpp) set_target_properties (opengl PROPERTIES PREFIX "" OUTPUT_NAME opengl32) @@ -153,7 +153,7 @@ else () DEPENDS glxtrace.py trace.py glxapi.py glapi.py glenum.py stdapi.py ) - add_library (glxtrace SHARED glxtrace.cpp log.cpp os_posix.cpp) + add_library (glxtrace SHARED glxtrace.cpp trace_write.cpp os_posix.cpp) set_target_properties (glxtrace PROPERTIES PREFIX "") target_link_libraries (glxtrace dl) endif () diff --git a/d3d10_1.py b/d3d10_1.py index 14420da..c71e880 100644 --- a/d3d10_1.py +++ b/d3d10_1.py @@ -72,6 +72,6 @@ if __name__ == '__main__': print print '#include ' print - print '#include "log.hpp"' + print '#include "trace_write.hpp"' print wrap() diff --git a/d3d10misc.py b/d3d10misc.py index 9396a56..00bd0e9 100644 --- a/d3d10misc.py +++ b/d3d10misc.py @@ -64,6 +64,6 @@ if __name__ == '__main__': print print '#include ' print - print '#include "log.hpp"' + print '#include "trace_write.hpp"' print wrap() diff --git a/d3d8.py b/d3d8.py index 7204554..fdf788f 100644 --- a/d3d8.py +++ b/d3d8.py @@ -283,7 +283,7 @@ if __name__ == '__main__': print '#include ' print '#include ' print - print '#include "log.hpp"' + print '#include "trace_write.hpp"' print wrap() diff --git a/d3d9.py b/d3d9.py index 4e1da90..572d328 100644 --- a/d3d9.py +++ b/d3d9.py @@ -411,7 +411,7 @@ if __name__ == '__main__': print print '#include ' print - print '#include "log.hpp"' + print '#include "trace_write.hpp"' print wrap() diff --git a/d3dshader.py b/d3dshader.py index bb37678..6093336 100644 --- a/d3dshader.py +++ b/d3dshader.py @@ -85,7 +85,7 @@ found: LPD3DXBUFFER pDisassembly = NULL; if (pfnD3DXDisassembleShader( (DWORD *)tokens, FALSE, NULL, &pDisassembly) == D3D_OK) - Log::LiteralString((char *)pDisassembly->GetBufferPointer()); + Trace::LiteralString((char *)pDisassembly->GetBufferPointer()); if(pDisassembly) pDisassembly->Release(); diff --git a/ddraw.py b/ddraw.py index bc1f8f2..0841a8a 100644 --- a/ddraw.py +++ b/ddraw.py @@ -1619,6 +1619,6 @@ if __name__ == '__main__': print '#include ' print '#include ' print - print '#include "log.hpp"' + print '#include "trace_write.hpp"' print wrap() diff --git a/glxtrace.py b/glxtrace.py index b8cd151..14aca2a 100644 --- a/glxtrace.py +++ b/glxtrace.py @@ -37,7 +37,7 @@ class GlxTracer(Tracer): print ' if (!pglXGetProcAddress) {' print ' pglXGetProcAddress = (PglXGetProcAddress)dlsym(RTLD_NEXT, "glXGetProcAddress");' print ' if (!pglXGetProcAddress)' - print ' Log::Abort();' + print ' Trace::Abort();' print ' }' return 'pglXGetProcAddress((const GLubyte *)"%s")' % (function.name,) @@ -65,7 +65,7 @@ if __name__ == '__main__': print '#include ' print '#include "glxext.h"' print - print '#include "log.hpp"' + print '#include "trace_write.hpp"' print '#include "glsize.hpp"' print print 'extern "C" {' diff --git a/log.cpp b/log.cpp deleted file mode 100644 index 719666f..0000000 --- a/log.cpp +++ /dev/null @@ -1,324 +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 -#include -#include -#include -#include - -#include - -#include - -#include "os.hpp" -#include "log.hpp" -#include "trace_format.hpp" - - -namespace Log { - - -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_PATH"); - 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); -} - -typedef std::map namemap; -static namemap names; - -static inline void -WriteName(const char *name) { - namemap::iterator it = names.find(name); - if (it == names.end()) { - size_t name_id = names.size(); - WriteUInt(name_id); - WriteString(name); - names[name] = name_id; - } else { - WriteUInt(it->second); - } -} - -void Open(void) { - if (!g_gzFile) { - _Open("trace"); - WriteUInt(TRACE_VERSION); - } -} - -void Close(void) { - _Close(); -} - -static unsigned call_no = 0; - -unsigned BeginEnter(const char *function) { - OS::AcquireMutex(); - Open(); - WriteByte(Trace::EVENT_ENTER); - WriteName(function); - 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, const char *name) { - WriteByte(Trace::CALL_ARG); - WriteUInt(index); - WriteName(name); -} - -void BeginReturn(void) { - WriteByte(Trace::CALL_RET); -} - -void BeginArray(size_t length) { - WriteByte(Trace::TYPE_ARRAY); - WriteUInt(length); -} - -void BeginStruct(size_t length) { - WriteByte(Trace::TYPE_STRUCT); - WriteUInt(length); -} - -void BeginMember(const char *name) { - WriteName(name); -} - -void BeginBitmask(void) { - WriteByte(Trace::TYPE_BITMASK); -} - -void EndBitmask(void) { - WriteByte(Trace::TYPE_NULL); -} - -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 LiteralFloat(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(""); -} - -void LiteralBlob(const void *data, size_t size) { - if (!data) { - LiteralNull(); - return; - } - WriteByte(Trace::TYPE_BLOB); - WriteUInt(size); - if (size) { - Write(data, size); - } -} - -void LiteralNamedConstant(const char *name, long long value) { - WriteByte(Trace::TYPE_CONST); - WriteName(name); - LiteralSInt(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 Log */ diff --git a/log.hpp b/log.hpp deleted file mode 100644 index edbf002..0000000 --- a/log.hpp +++ /dev/null @@ -1,77 +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. - * - **************************************************************************/ - -#ifndef _LOG_HPP_ -#define _LOG_HPP_ - -namespace Log { - - void Open(void); - void Close(void); - - unsigned BeginEnter(const char *function); - void EndEnter(void); - - void BeginLeave(unsigned call); - void EndLeave(void); - - void BeginArg(unsigned index, const char *name); - 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(size_t length); - inline void EndStruct(void) {} - - void BeginMember(const char *name); - inline void EndMember(void) {} - - void BeginBitmask(void); - void EndBitmask(void); - - void LiteralBool(bool value); - void LiteralSInt(signed long long value); - void LiteralUInt(unsigned long long value); - void LiteralFloat(float value); - void LiteralFloat(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 LiteralNamedConstant(const char *name, long long value); - void LiteralNull(void); - void LiteralOpaque(const void *ptr); - - void Abort(void); -} - -#endif /* _LOG_HPP_ */ diff --git a/os_posix.cpp b/os_posix.cpp index 590738f..f4dbaab 100644 --- a/os_posix.cpp +++ b/os_posix.cpp @@ -30,7 +30,7 @@ #include #include "os.hpp" -#include "log.hpp" +#include "trace_write.hpp" namespace OS { @@ -108,6 +108,6 @@ Abort(void) static void _uninit(void) __attribute__((destructor)); static void _uninit(void) { - Log::Close(); + Trace::Close(); } diff --git a/os_win32.cpp b/os_win32.cpp index 5f8dddf..f9fb56a 100644 --- a/os_win32.cpp +++ b/os_win32.cpp @@ -28,7 +28,7 @@ #include #include "os.hpp" -#include "log.hpp" +#include "trace_write.hpp" namespace OS { @@ -131,7 +131,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { case DLL_THREAD_DETACH: return TRUE; case DLL_PROCESS_DETACH: - Log::Close(); + Trace::Close(); return TRUE; } (void)hinstDLL; diff --git a/trace.py b/trace.py index 677faf3..388729a 100644 --- a/trace.py +++ b/trace.py @@ -51,12 +51,12 @@ class DumpDeclarator(stdapi.OnceVisitor): for type, name in struct.members: self.visit(type) print 'static void __traceStruct%s(const %s &value) {' % (struct.id, struct.expr) - print ' Log::BeginStruct(%u);' % len(struct.members) + print ' Trace::BeginStruct(%u);' % len(struct.members) for type, name in struct.members: - print ' Log::BeginMember("%s");' % (name,) + print ' Trace::BeginMember("%s");' % (name,) dump_instance(type, 'value.%s' % (name,)) - print ' Log::EndMember();' - print ' Log::EndStruct();' + print ' Trace::EndMember();' + print ' Trace::EndStruct();' print '}' print @@ -71,10 +71,10 @@ class DumpDeclarator(stdapi.OnceVisitor): print ' switch(value) {' for value in enum.values: print ' case %s:' % value - print ' Log::LiteralNamedConstant("%s", %s);' % (value, value) + print ' Trace::LiteralNamedConstant("%s", %s);' % (value, value) print ' break;' print ' default:' - print ' Log::LiteralSInt(value);' + print ' Trace::LiteralSInt(value);' print ' break;' print ' }' print '}' @@ -82,16 +82,16 @@ class DumpDeclarator(stdapi.OnceVisitor): def visit_bitmask(self, bitmask): print 'static void __traceBitmask%s(%s value) {' % (bitmask.id, bitmask.type) - print ' Log::BeginBitmask();' + print ' Trace::BeginBitmask();' for value in bitmask.values: print ' if((value & %s) == %s) {' % (value, value) - print ' Log::LiteralNamedConstant("%s", %s);' % (value, value) + print ' Trace::LiteralNamedConstant("%s", %s);' % (value, value) print ' value &= ~%s;' % value print ' }' print ' if(value) {' dump_instance(bitmask.type, "value"); print ' }' - print ' Log::EndBitmask();' + print ' Trace::EndBitmask();' print '}' print @@ -115,13 +115,13 @@ class DumpImplementer(stdapi.Visitor): '''Dump an instance.''' def visit_literal(self, literal, instance): - print ' Log::Literal%s(%s);' % (literal.format, instance) + print ' Trace::Literal%s(%s);' % (literal.format, instance) def visit_string(self, string, instance): if string.length is not None: - print ' Log::LiteralString((const char *)%s, %s);' % (instance, string.length) + print ' Trace::LiteralString((const char *)%s, %s);' % (instance, string.length) else: - print ' Log::LiteralString((const char *)%s);' % instance + print ' Trace::LiteralString((const char *)%s);' % instance def visit_const(self, const, instance): self.visit(const.type, instance) @@ -132,19 +132,19 @@ class DumpImplementer(stdapi.Visitor): def visit_array(self, array, instance): print ' if(%s) {' % instance index = '__i' + array.type.id - print ' Log::BeginArray(%s);' % (array.length,) + print ' Trace::BeginArray(%s);' % (array.length,) print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index) - print ' Log::BeginElement();' + print ' Trace::BeginElement();' self.visit(array.type, '(%s)[%s]' % (instance, index)) - print ' Log::EndElement();' + print ' Trace::EndElement();' print ' }' - print ' Log::EndArray();' + print ' Trace::EndArray();' print ' }' print ' else' - print ' Log::LiteralNull();' + print ' Trace::LiteralNull();' def visit_blob(self, blob, instance): - print ' Log::LiteralBlob(%s, %s);' % (instance, blob.size) + print ' Trace::LiteralBlob(%s, %s);' % (instance, blob.size) def visit_enum(self, enum, instance): print ' __traceEnum%s(%s);' % (enum.id, instance) @@ -154,14 +154,14 @@ class DumpImplementer(stdapi.Visitor): def visit_pointer(self, pointer, instance): print ' if(%s) {' % instance - print ' Log::BeginArray(1);' - print ' Log::BeginElement();' + print ' Trace::BeginArray(1);' + print ' Trace::BeginElement();' dump_instance(pointer.type, "*" + instance) - print ' Log::EndElement();' - print ' Log::EndArray();' + print ' Trace::EndElement();' + print ' Trace::EndArray();' print ' }' print ' else' - print ' Log::LiteralNull();' + print ' Trace::LiteralNull();' def visit_handle(self, handle, instance): self.visit(handle.type, instance) @@ -170,10 +170,10 @@ class DumpImplementer(stdapi.Visitor): self.visit(alias.type, instance) def visit_opaque(self, opaque, instance): - print ' Log::LiteralOpaque((const void *)%s);' % instance + print ' Trace::LiteralOpaque((const void *)%s);' % instance def visit_interface(self, interface, instance): - print ' Log::LiteralOpaque((const void *)%s);' % instance + print ' Trace::LiteralOpaque((const void *)%s);' % instance dump_instance = DumpImplementer().visit @@ -297,7 +297,7 @@ class Tracer: assert function.fail != '' print ' return %s;' % function.fail else: - print ' Log::Abort();' + print ' Trace::Abort();' def get_function_address(self, function): raise NotImplementedError @@ -320,21 +320,21 @@ class Tracer: print ' %s __result;' % function.type result = '__result = ' self._get_true_pointer(function) - print ' unsigned __call = Log::BeginEnter("%s");' % (function.name) + print ' unsigned __call = Trace::BeginEnter("%s");' % (function.name) for arg in function.args: if not arg.output: self.unwrap_arg(function, arg) self.dump_arg(function, arg) - print ' Log::EndEnter();' + print ' Trace::EndEnter();' print ' %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args])) - print ' Log::BeginLeave(__call);' + print ' Trace::BeginLeave(__call);' for arg in function.args: if arg.output: self.dump_arg(function, arg) self.wrap_arg(function, arg) if function.type is not stdapi.Void: self.dump_ret(function, "__result") - print ' Log::EndLeave();' + print ' Trace::EndLeave();' if function.type is not stdapi.Void: self.wrap_ret(function, "__result") print ' return __result;' @@ -342,9 +342,9 @@ class Tracer: print def dump_arg(self, function, arg): - print ' Log::BeginArg(%u, "%s");' % (arg.index, arg.name,) + print ' Trace::BeginArg(%u, "%s");' % (arg.index, arg.name,) dump_instance(arg.type, arg.name) - print ' Log::EndArg();' + print ' Trace::EndArg();' def wrap_arg(self, function, arg): wrap_instance(arg.type, arg.name) @@ -353,9 +353,9 @@ class Tracer: unwrap_instance(arg.type, arg.name) def dump_ret(self, function, instance): - print ' Log::BeginReturn();' + print ' Trace::BeginReturn();' dump_instance(function.type, instance) - print ' Log::EndReturn();' + print ' Trace::EndReturn();' def wrap_ret(self, function, instance): wrap_instance(function.type, instance) @@ -403,10 +403,10 @@ class Tracer: else: print ' %s __result;' % method.type result = '__result = ' - print ' Log::BeginCall("%s");' % (interface.name + '::' + method.name) - print ' Log::BeginArg(0, "this");' - print ' Log::LiteralOpaque((const void *)m_pInstance);' - print ' Log::EndArg();' + print ' Trace::BeginCall("%s");' % (interface.name + '::' + method.name) + print ' Trace::BeginArg(0, "this");' + print ' Trace::LiteralOpaque((const void *)m_pInstance);' + print ' Trace::EndArg();' for arg in method.args: if not arg.output: self.unwrap_arg(method, arg) @@ -417,11 +417,11 @@ class Tracer: self.dump_arg(method, arg) self.wrap_arg(method, arg) if method.type is not Void: - print ' Log::BeginReturn("%s");' % method.type + print ' Trace::BeginReturn("%s");' % method.type dump_instance(method.type, "__result") - print ' Log::EndReturn();' + print ' Trace::EndReturn();' wrap_instance(method.type, '__result') - print ' Log::EndCall();' + print ' Trace::EndCall();' if method.name == 'QueryInterface': print ' if (*ppvObj == m_pInstance)' print ' *ppvObj = this;' diff --git a/trace_write.cpp b/trace_write.cpp new file mode 100644 index 0000000..054303b --- /dev/null +++ b/trace_write.cpp @@ -0,0 +1,324 @@ +/************************************************************************** + * + * 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 +#include +#include +#include +#include + +#include + +#include + +#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_PATH"); + 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); +} + +typedef std::map namemap; +static namemap names; + +static inline void +WriteName(const char *name) { + namemap::iterator it = names.find(name); + if (it == names.end()) { + size_t name_id = names.size(); + WriteUInt(name_id); + WriteString(name); + names[name] = name_id; + } else { + WriteUInt(it->second); + } +} + +void Open(void) { + if (!g_gzFile) { + _Open("trace"); + WriteUInt(TRACE_VERSION); + } +} + +void Close(void) { + _Close(); +} + +static unsigned call_no = 0; + +unsigned BeginEnter(const char *function) { + OS::AcquireMutex(); + Open(); + WriteByte(Trace::EVENT_ENTER); + WriteName(function); + 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, const char *name) { + WriteByte(Trace::CALL_ARG); + WriteUInt(index); + WriteName(name); +} + +void BeginReturn(void) { + WriteByte(Trace::CALL_RET); +} + +void BeginArray(size_t length) { + WriteByte(Trace::TYPE_ARRAY); + WriteUInt(length); +} + +void BeginStruct(size_t length) { + WriteByte(Trace::TYPE_STRUCT); + WriteUInt(length); +} + +void BeginMember(const char *name) { + WriteName(name); +} + +void BeginBitmask(void) { + WriteByte(Trace::TYPE_BITMASK); +} + +void EndBitmask(void) { + WriteByte(Trace::TYPE_NULL); +} + +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 LiteralFloat(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(""); +} + +void LiteralBlob(const void *data, size_t size) { + if (!data) { + LiteralNull(); + return; + } + WriteByte(Trace::TYPE_BLOB); + WriteUInt(size); + if (size) { + Write(data, size); + } +} + +void LiteralNamedConstant(const char *name, long long value) { + WriteByte(Trace::TYPE_CONST); + WriteName(name); + LiteralSInt(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 */ diff --git a/trace_write.hpp b/trace_write.hpp new file mode 100644 index 0000000..7b494e4 --- /dev/null +++ b/trace_write.hpp @@ -0,0 +1,77 @@ +/************************************************************************** + * + * 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. + * + **************************************************************************/ + +#ifndef _TRACE_HPP_ +#define _TRACE_HPP_ + +namespace Trace { + + void Open(void); + void Close(void); + + unsigned BeginEnter(const char *function); + void EndEnter(void); + + void BeginLeave(unsigned call); + void EndLeave(void); + + void BeginArg(unsigned index, const char *name); + 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(size_t length); + inline void EndStruct(void) {} + + void BeginMember(const char *name); + inline void EndMember(void) {} + + void BeginBitmask(void); + void EndBitmask(void); + + void LiteralBool(bool value); + void LiteralSInt(signed long long value); + void LiteralUInt(unsigned long long value); + void LiteralFloat(float value); + void LiteralFloat(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 LiteralNamedConstant(const char *name, long long value); + void LiteralNull(void); + void LiteralOpaque(const void *ptr); + + void Abort(void); +} + +#endif /* _TRACE_HPP_ */ diff --git a/wgltrace.py b/wgltrace.py index b5f94f3..f75ae80 100644 --- a/wgltrace.py +++ b/wgltrace.py @@ -403,7 +403,7 @@ class WglTracer(Tracer): print ' if (!pwglGetProcAddress) {' print ' pwglGetProcAddress = (PwglGetProcAddress)__GetProcAddress("wglGetProcAddress");' print ' if (!pwglGetProcAddress)' - print ' Log::Abort();' + print ' Trace::Abort();' print ' }' return 'pwglGetProcAddress("%s")' % (function.name,) @@ -431,7 +431,7 @@ if __name__ == '__main__': print print '#include "glimports.hpp"' print - print '#include "log.hpp"' + print '#include "trace_write.hpp"' print '#include "os.hpp"' print '#include "glsize.hpp"' print diff --git a/winapi.py b/winapi.py index 30826e3..5345a0d 100644 --- a/winapi.py +++ b/winapi.py @@ -199,13 +199,13 @@ class Dll: print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))' print r' return FALSE;' print r' _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name - print r' Log::Open("%s");' % self.name + print r' Trace::Open("%s");' % self.name print r' case DLL_THREAD_ATTACH:' print r' return TRUE;' print r' case DLL_THREAD_DETACH:' print r' return TRUE;' print r' case DLL_PROCESS_DETACH:' - print r' Log::Close();' + print r' Trace::Close();' print r' if(g_hDll) {' print r' FreeLibrary(g_hDll);' print r' g_hDll = NULL;'