From 1630be10e241e2741af716988da3e51dded481a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Mon, 15 Nov 2010 15:50:45 +0000 Subject: [PATCH] Start abstracting OS functionality. --- CMakeLists.txt | 4 +-- SConstruct | 6 +++++ glx.py | 2 +- log.cpp | 68 ++++++++---------------------------------------- os.hpp | 52 +++++++++++++++++++++++++++++++++++++ os_posix.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ os_win32.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 227 insertions(+), 60 deletions(-) create mode 100644 os.hpp create mode 100644 os_posix.cpp create mode 100644 os_win32.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index accf9c3..9e590fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,7 +73,7 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/opengl32.py > ${CMAKE_CURRENT_BINARY_DIR}/opengl32.cpp DEPENDS opengl32.py gl.py windows.py base.py ) - add_library (opengl32 SHARED opengl32.def opengl32.cpp log.cpp) + add_library (opengl32 SHARED opengl32.def opengl32.cpp log.cpp os_win32.cpp) set_target_properties (opengl32 PROPERTIES PREFIX "") else () @@ -84,7 +84,7 @@ else () COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glx.py > ${CMAKE_CURRENT_BINARY_DIR}/glx.cpp DEPENDS glx.py gl.py dl.py base.py ) - add_library (glxtrace SHARED glx.cpp log.cpp) + add_library (glxtrace SHARED glx.cpp log.cpp os_posix.cpp) set_target_properties (glxtrace PROPERTIES PREFIX "") target_link_libraries (glxtrace dl) endif () diff --git a/SConstruct b/SConstruct index 140d72f..9adf118 100644 --- a/SConstruct +++ b/SConstruct @@ -215,6 +215,7 @@ if has_d3d7 and False: 'ddraw.def', 'ddraw.cpp', 'log.cpp', + 'os_win32.cpp', ] ) @@ -233,6 +234,7 @@ if has_d3d8: 'd3d8.def', 'd3d8.cpp', 'log.cpp', + 'os_win32.cpp', ] ) @@ -251,6 +253,7 @@ if has_d3d9: 'd3d9.def', 'd3d9.cpp', 'log.cpp', + 'os_win32.cpp', ] ) @@ -269,6 +272,7 @@ if has_d3d10: 'd3d10.def', 'd3d10.cpp', 'log.cpp', + 'os_win32.cpp', ] ) @@ -287,6 +291,7 @@ if has_d3d10_1: 'd3d10_1.def', 'd3d10_1.cpp', 'log.cpp', + 'os_win32.cpp', ] ) @@ -304,6 +309,7 @@ opengl32 = env.SharedLibrary( 'opengl32.def', 'opengl32.cpp', 'log.cpp', + 'os_win32.cpp', ] ) diff --git a/glx.py b/glx.py index 4ed6226..79a4f46 100644 --- a/glx.py +++ b/glx.py @@ -26,7 +26,7 @@ from gl import * from dl import * -libgl = Dll("libGL.so") +libgl = Dll("GL") libgl.functions += [ DllFunction(Void, "glNewList", [(GLuint, "list"), (GLenum, "mode")]), DllFunction(Void, "glEndList", []), diff --git a/log.cpp b/log.cpp index 0461794..2493ec1 100644 --- a/log.cpp +++ b/log.cpp @@ -29,48 +29,20 @@ #include #include -#ifdef WIN32 -#include -#endif - #include +#include "os.hpp" #include "log.hpp" -#ifdef WIN32 -#ifndef PATH_MAX -#define PATH_MAX _MAX_PATH -#endif -#ifndef snprintf -#define snprintf _snprintf -#endif -#ifndef vsnprintf -#define vsnprintf _vsnprintf -#endif -#endif /* WIN32 */ - -#ifndef PATH_MAX -#define PATH_MAX 1024 -#endif - namespace Log { static gzFile g_gzFile = NULL; -static char g_szFileName[PATH_MAX]; - -#if WIN32 -static CRITICAL_SECTION CriticalSection; -#endif /* WIN32 */ - static void _Close(void) { if(g_gzFile != NULL) { gzclose(g_gzFile); g_gzFile = NULL; -#if WIN32 - DeleteCriticalSection(&CriticalSection); -#endif } } @@ -79,32 +51,20 @@ static void _Open(const char *szName, const char *szExtension) { static unsigned dwCounter = 0; - char szProcessPath[PATH_MAX]; - char *lpProcessName; - char *lpProcessExt; - -#ifdef WIN32 - GetModuleFileNameA(NULL, szProcessPath, sizeof(szProcessPath)/sizeof(szProcessPath[0])); + char szProcessName[PATH_MAX]; + char szFileName[PATH_MAX]; - lpProcessName = strrchr(szProcessPath, '\\'); - lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath; - lpProcessExt = strrchr(lpProcessName, '.'); - if(lpProcessExt) - *lpProcessExt = '\0'; -#else - // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe - lpProcessName = ""; -#endif + OS::GetProcessName(szProcessName, PATH_MAX); for(;;) { FILE *file; if(dwCounter) - snprintf(g_szFileName, PATH_MAX, "%s.%s.%u.%s.gz", lpProcessName, szName, dwCounter, szExtension); + snprintf(szFileName, PATH_MAX, "%s.%s.%u.%s.gz", szProcessName, szName, dwCounter, szExtension); else - snprintf(g_szFileName, PATH_MAX, "%s.%s.%s.gz", lpProcessName, szName, szExtension); + snprintf(szFileName, PATH_MAX, "%s.%s.%s.gz", szProcessName, szName, szExtension); - file = fopen(g_szFileName, "rb"); + file = fopen(szFileName, "rb"); if(file == NULL) break; @@ -113,10 +73,8 @@ static void _Open(const char *szName, const char *szExtension) { ++dwCounter; } - g_gzFile = gzopen(g_szFileName, "wb"); -#ifdef WIN32 - InitializeCriticalSection(&CriticalSection); -#endif + fprintf(stderr, "Logging to %s\n", szFileName); + g_gzFile = gzopen(szFileName, "wb"); } static inline void _ReOpen(void) { @@ -311,9 +269,7 @@ void TextF(const char *format, ...) { } void BeginCall(const char *function) { -#ifdef WIN32 - EnterCriticalSection(&CriticalSection); -#endif + OS::AcquireMutex(); Indent(1); BeginTag("call", "name", function); NewLine(); @@ -324,9 +280,7 @@ void EndCall(void) { EndTag("call"); NewLine(); gzflush(g_gzFile, Z_SYNC_FLUSH); -#ifdef WIN32 - LeaveCriticalSection(&CriticalSection); -#endif + OS::ReleaseMutex(); } void BeginArg(const char *type, const char *name) { diff --git a/os.hpp b/os.hpp new file mode 100644 index 0000000..8e1c295 --- /dev/null +++ b/os.hpp @@ -0,0 +1,52 @@ +/************************************************************************** + * + * Copyright 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 _OS_HPP_ +#define _OS_HPP_ + +#ifdef WIN32 +#ifndef snprintf +#define snprintf _snprintf +#endif +#ifndef vsnprintf +#define vsnprintf _vsnprintf +#endif +#endif /* WIN32 */ + +#ifndef PATH_MAX +#define PATH_MAX 1024 +#endif + +namespace OS { + +void AcquireMutex(void); + +void ReleaseMutex(void); + +bool GetProcessName(char *str, size_t size); + +} /* namespace OS */ + +#endif /* _OS_HPP_ */ diff --git a/os_posix.cpp b/os_posix.cpp new file mode 100644 index 0000000..5b27519 --- /dev/null +++ b/os_posix.cpp @@ -0,0 +1,74 @@ +/************************************************************************** + * + * Copyright 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. + * + **************************************************************************/ + +#include +#include +#include + +#include "os.hpp" + +namespace OS { + + +static pthread_mutex_t +mutex = PTHREAD_MUTEX_INITIALIZER; + + +void +AcquireMutex(void) +{ + pthread_mutex_lock(&mutex); +} + + +void +ReleaseMutex(void) +{ + pthread_mutex_unlock(&mutex); +} + + +bool +GetProcessName(char *str, size_t size) +{ + char szProcessPath[PATH_MAX + 1]; + char *lpProcessName; + + // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe + if (readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1) == -1) { + *str = 0; + return false; + } + + lpProcessName = strrchr(szProcessPath, '/'); + lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath; + + strncpy(str, lpProcessName, size); + + return true; +} + + +} /* namespace OS */ diff --git a/os_win32.cpp b/os_win32.cpp new file mode 100644 index 0000000..2e28ec8 --- /dev/null +++ b/os_win32.cpp @@ -0,0 +1,81 @@ +/************************************************************************** + * + * Copyright 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. + * + **************************************************************************/ + +#include +#include + +#include "os.hpp" + + +namespace OS { + + +/* + * Trick from http://locklessinc.com/articles/pthreads_on_windows/ + */ +static CRITICAL_SECTION +CriticalSection = { + (_CRITICAL_SECTION_DEBUG *)-1, -1, 0, 0, 0, 0 +}; + + +void +AcquireMutex(void) +{ + EnterCriticalSection(&CriticalSection); +} + + +void +ReleaseMutex(void) +{ + LeaveCriticalSection(&CriticalSection); +} + + +bool +GetProcessName(char *str, size_t size) +{ + char szProcessPath[PATH_MAX]; + char *lpProcessName; + char *lpProcessExt; + + GetModuleFileNameA(NULL, szProcessPath, sizeof(szProcessPath)/sizeof(szProcessPath[0])); + + lpProcessName = strrchr(szProcessPath, '\\'); + lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath; + + lpProcessExt = strrchr(lpProcessName, '.'); + if (lpProcessExt) { + *lpProcessExt = '\0'; + } + + strncpy(str, lpProcessName, size); + + return true; +} + + +} /* namespace OS */ -- 2.7.4