From: José Fonseca Date: Sat, 7 Jul 2012 11:54:09 +0000 (+0100) Subject: Relocate tracing (dis/en)enablement. X-Git-Url: https://git.cworth.org/git?p=apitrace;a=commitdiff_plain;h=537c507874cdde0b507d306ac058767f506da8e2 Relocate tracing (dis/en)enablement. Out of common -- as it is tracing specific --- diff --git a/common/os.hpp b/common/os.hpp index 7f62451..cc72a0e 100644 --- a/common/os.hpp +++ b/common/os.hpp @@ -70,15 +70,6 @@ void log(const char *format, ...) #endif #endif -#ifdef ANDROID -bool apitrace_enabled(void); -#else -static inline bool apitrace_enabled(void) -{ - return true; -} -#endif - void abort(void); void setExceptionCallback(void (*callback)(void)); diff --git a/common/os_posix.cpp b/common/os_posix.cpp index e4de967..7d39d8a 100644 --- a/common/os_posix.cpp +++ b/common/os_posix.cpp @@ -46,10 +46,6 @@ #ifdef ANDROID #include -#include -#include -#include -#include #endif #ifndef PATH_MAX @@ -101,68 +97,6 @@ getProcessName(void) return path; } -#ifdef ANDROID -static String -getZygoteProcessName(void) -{ - String path; - size_t size = PATH_MAX; - char *buf = path.buf(size); - ssize_t len; - - int fd = open("/proc/self/cmdline", O_RDONLY); - - assert(fd >= 0); - len = read(fd, buf, size - 1); - close(fd); - path.truncate(len); - - return path; -} - -static bool isZygoteProcess(void) -{ - os::String proc_name; - - proc_name = getProcessName(); - proc_name.trimDirectory(); - - return strcmp(proc_name, "app_process") == 0; -} - -bool apitrace_enabled(void) -{ - static pid_t cached_pid; - static bool enabled; - pid_t pid; - - pid = getpid(); - if (cached_pid == pid) - return enabled; - cached_pid = pid; - - if (!isZygoteProcess()) { - os::log("apitrace[%d]: enabled for standalone %s", pid, - (const char *)getProcessName()); - enabled = true; - return true; - } - - char target_proc_name[PROP_VALUE_MAX] = ""; - os::String proc_name; - - proc_name = getZygoteProcessName(); - proc_name.trimDirectory(); - - __system_property_get("debug.apitrace.procname", target_proc_name); - enabled = !strcmp(target_proc_name, proc_name); - os::log("apitrace[%d]: %s for %s", - pid, enabled ? "enabled" : "disabled", (const char *)proc_name); - - return enabled; -} -#endif - String getCurrentDir(void) { diff --git a/wrappers/.gitignore b/wrappers/.gitignore index 8ba6fb8..2f81720 100644 --- a/wrappers/.gitignore +++ b/wrappers/.gitignore @@ -9,5 +9,4 @@ dlltrace.cpp egltrace.cpp gltrace.cpp glxtrace.cpp -trace.cpp wgltrace.cpp diff --git a/wrappers/CMakeLists.txt b/wrappers/CMakeLists.txt index a333504..259c7fb 100644 --- a/wrappers/CMakeLists.txt +++ b/wrappers/CMakeLists.txt @@ -9,6 +9,14 @@ include_directories ( ${CMAKE_SOURCE_DIR}/dispatch ) +add_library (common_trace STATIC + trace.cpp +) + +set_target_properties (common_trace PROPERTIES + # Ensure it can be statically linked in shared libraries + COMPILE_FLAGS "${CMAKE_SHARED_LIBRARY_CXX_FLAGS}" +) if (WIN32) if (MINGW) @@ -36,6 +44,7 @@ if (WIN32) ) add_library (ddrawtrace MODULE ddraw.def ddrawtrace.cpp) target_link_libraries (ddrawtrace + common_trace common ${ZLIB_LIBRARIES} ${SNAPPY_LIBRARIES} @@ -66,6 +75,7 @@ if (WIN32) ) add_library (d3d8trace MODULE d3d8.def d3d8trace.cpp d3d9shader.cpp) target_link_libraries (d3d8trace + common_trace common ${ZLIB_LIBRARIES} ${SNAPPY_LIBRARIES} @@ -96,6 +106,7 @@ if (WIN32) ) add_library (d3d9trace MODULE d3d9.def d3d9trace.cpp d3d9shader.cpp) target_link_libraries (d3d9trace + common_trace common ${ZLIB_LIBRARIES} ${SNAPPY_LIBRARIES} @@ -132,6 +143,7 @@ if (WIN32) ) add_library (d3d10trace MODULE d3d10.def d3d10trace.cpp d3d10shader.cpp) target_link_libraries (d3d10trace + common_trace common ${ZLIB_LIBRARIES} ${SNAPPY_LIBRARIES} @@ -166,6 +178,7 @@ if (WIN32) ) add_library (d3d10_1trace MODULE d3d10_1.def d3d10_1trace.cpp) target_link_libraries (d3d10_1trace + common_trace common ${ZLIB_LIBRARIES} ${SNAPPY_LIBRARIES} @@ -205,6 +218,7 @@ if (WIN32) ) add_library (d3d11trace MODULE d3d11.def d3d11trace.cpp) target_link_libraries (d3d11trace + common_trace common ${ZLIB_LIBRARIES} ${SNAPPY_LIBRARIES} @@ -240,6 +254,7 @@ if (WIN32) add_dependencies (wgltrace glproc) target_link_libraries (wgltrace glproc_gl + common_trace common ${ZLIB_LIBRARIES} ${SNAPPY_LIBRARIES} @@ -282,6 +297,7 @@ elseif (APPLE) target_link_libraries (cgltrace glproc_gl + common_trace common ${ZLIB_LIBRARIES} ${SNAPPY_LIBRARIES} @@ -323,6 +339,7 @@ elseif (X11_FOUND) target_link_libraries (glxtrace glproc_gl + common_trace common ${ZLIB_LIBRARIES} ${SNAPPY_LIBRARIES} @@ -369,6 +386,7 @@ if (ENABLE_EGL AND NOT WIN32 AND NOT APPLE) target_link_libraries (egltrace glproc_egl + common_trace common ${ZLIB_LIBRARIES} ${SNAPPY_LIBRARIES} diff --git a/wrappers/trace.cpp b/wrappers/trace.cpp new file mode 100644 index 0000000..eb68934 --- /dev/null +++ b/wrappers/trace.cpp @@ -0,0 +1,115 @@ +/************************************************************************** + * + * Copyright 2010-2011 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 + +#ifdef ANDROID +#include +#include +#include +#include +#endif + +#include "os.hpp" +#include "os_string.hpp" +#include "trace.hpp" + + +namespace trace { + + +#ifdef ANDROID + +static bool +isZygoteProcess(void) +{ + os::String proc_name; + + proc_name = os::getProcessName(); + proc_name.trimDirectory(); + + return strcmp(proc_name, "app_process") == 0; +} + +static os::String +getZygoteProcessName(void) +{ + os::String path; + size_t size = PATH_MAX; + char *buf = path.buf(size); + ssize_t len; + + int fd = open("/proc/self/cmdline", O_RDONLY); + + assert(fd >= 0); + len = read(fd, buf, size - 1); + close(fd); + path.truncate(len); + + return path; +} + +bool +isTracingEnabled(void) +{ + static pid_t cached_pid; + static bool enabled; + pid_t pid; + + pid = getpid(); + if (cached_pid == pid) + return enabled; + cached_pid = pid; + + if (!isZygoteProcess()) { + os::log("apitrace[%d]: enabled for standalone %s", + pid, (const char *)os::getProcessName()); + enabled = true; + return true; + } + + char target_proc_name[PROP_VALUE_MAX] = ""; + os::String proc_name; + + proc_name = getZygoteProcessName(); + proc_name.trimDirectory(); + + __system_property_get("debug.apitrace.procname", target_proc_name); + enabled = !strcmp(target_proc_name, proc_name); + os::log("apitrace[%d]: %s for %s", + pid, enabled ? "enabled" : "disabled", (const char *)proc_name); + + return enabled; +} + +#endif /* ANDROID */ + + +} /* namespace trace */ + diff --git a/wrappers/trace.hpp b/wrappers/trace.hpp new file mode 100644 index 0000000..997617e --- /dev/null +++ b/wrappers/trace.hpp @@ -0,0 +1,56 @@ +/************************************************************************** + * + * Copyright 2011 Jose Fonseca + * 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. + * + **************************************************************************/ + + +/* + * Comon definitions for all tracers. + */ + + +#ifndef _TRACE_HPP_ +#define _TRACE_HPP_ + + +namespace trace { + + +#ifdef ANDROID + +bool isTracingEnabled(void); + +#else /* !ANDROID */ + +static inline bool +isTracingEnabled(void) { + return true; +} + +#endif /* !ANDROID */ + + +} /* namespace trace */ + + +#endif /* _TRACE_HPP_ */ diff --git a/wrappers/trace.py b/wrappers/trace.py index aab99d7..958c072 100644 --- a/wrappers/trace.py +++ b/wrappers/trace.py @@ -396,6 +396,9 @@ class Tracer: print '#else' print '# include // alloca' print '#endif' + print + print '#include "trace.hpp"' + print def footer(self, api): pass @@ -422,14 +425,16 @@ class Tracer: print function.prototype() + ' {' if function.type is not stdapi.Void: print ' %s _result;' % function.type - print ' if (!os::apitrace_enabled()) {' + + # No-op if tracing is disabled + print ' if (!trace::isTracingEnabled()) {' Tracer.invokeFunction(self, function) if function.type is not stdapi.Void: print ' return _result;' else: print ' return;' print ' }' - print + self.traceFunctionImplBody(function) if function.type is not stdapi.Void: print ' return _result;'