From 8e27804d29abfa3a7cd8630aaa3394677c8a3844 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Fri, 7 Dec 2012 07:00:46 +0000 Subject: [PATCH] cli: Merge trace_resource.cpp and trace_tools_trace.cpp in. It's only used by CLI and it will not change. --- CMakeLists.txt | 2 - cli/CMakeLists.txt | 1 + cli/cli_diff.cpp | 4 +- cli/cli_diff_images.cpp | 4 +- cli/cli_diff_state.cpp | 4 +- .../cli_resources.cpp | 7 +- .../cli_resources.hpp | 10 +- cli/cli_retrace.cpp | 4 +- cli/cli_trace.cpp | 172 +++++++++++++++++- {common => cli}/trace_tools.hpp | 0 {common => cli}/trace_tools_trace.cpp | 2 +- 11 files changed, 184 insertions(+), 26 deletions(-) rename common/trace_resource.cpp => cli/cli_resources.cpp (98%) rename common/trace_resource.hpp => cli/cli_resources.hpp (91%) rename {common => cli}/trace_tools.hpp (100%) rename {common => cli}/trace_tools_trace.cpp (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 961e73e..1c4094f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -320,8 +320,6 @@ add_library (common STATIC common/trace_writer_local.cpp common/trace_writer_model.cpp common/trace_loader.cpp - common/trace_resource.cpp - common/trace_tools_trace.cpp common/trace_profiler.cpp common/image.cpp common/image_bmp.cpp diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index ed99881..521886b 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable (apitrace cli_retrace.cpp cli_trace.cpp cli_trim.cpp + cli_resources.cpp ) target_link_libraries (apitrace diff --git a/cli/cli_diff.cpp b/cli/cli_diff.cpp index ffc0e30..76cdce1 100644 --- a/cli/cli_diff.cpp +++ b/cli/cli_diff.cpp @@ -31,14 +31,14 @@ #include "cli.hpp" #include "os_string.hpp" #include "os_process.hpp" -#include "trace_resource.hpp" +#include "cli_resources.hpp" static const char *synopsis = "Identify differences between two traces."; static os::String find_command(void) { - return trace::findScript("tracediff.py"); + return findScript("tracediff.py"); } static void diff --git a/cli/cli_diff_images.cpp b/cli/cli_diff_images.cpp index 3932fa2..ba8df3e 100644 --- a/cli/cli_diff_images.cpp +++ b/cli/cli_diff_images.cpp @@ -31,14 +31,14 @@ #include "cli.hpp" #include "os_string.hpp" #include "os_process.hpp" -#include "trace_resource.hpp" +#include "cli_resources.hpp" static const char *synopsis = "Identify differences between two image dumps."; static os::String find_command(void) { - return trace::findScript("snapdiff.py"); + return findScript("snapdiff.py"); } static void diff --git a/cli/cli_diff_state.cpp b/cli/cli_diff_state.cpp index 7f75484..341d244 100644 --- a/cli/cli_diff_state.cpp +++ b/cli/cli_diff_state.cpp @@ -33,7 +33,7 @@ #include "cli.hpp" #include "os_string.hpp" #include "os_process.hpp" -#include "trace_resource.hpp" +#include "cli_resources.hpp" static const char *synopsis = "Identify differences between two state dumps."; @@ -83,7 +83,7 @@ command(int argc, char *argv[]) file1 = argv[optind]; file2 = argv[optind + 1]; - os::String command = trace::findScript("jsondiff.py"); + os::String command = findScript("jsondiff.py"); char *args[5]; diff --git a/common/trace_resource.cpp b/cli/cli_resources.cpp similarity index 98% rename from common/trace_resource.cpp rename to cli/cli_resources.cpp index 04ac327..255a98b 100644 --- a/common/trace_resource.cpp +++ b/cli/cli_resources.cpp @@ -29,12 +29,10 @@ #include #include "os_string.hpp" -#include "trace_tools.hpp" +#include "cli_resources.hpp" -namespace trace { - os::String findProgram(const char*programFilename) { @@ -148,6 +146,3 @@ findScript(const char *scriptFilename) return ""; } - - -} /* namespace trace */ diff --git a/common/trace_resource.hpp b/cli/cli_resources.hpp similarity index 91% rename from common/trace_resource.hpp rename to cli/cli_resources.hpp index 28c5727..dde86dd 100644 --- a/common/trace_resource.hpp +++ b/cli/cli_resources.hpp @@ -23,8 +23,8 @@ * **************************************************************************/ -#ifndef _TRACE_RESOURCE_HPP_ -#define _TRACE_RESOURCE_HPP_ +#ifndef _CLI_RESOURCES_HPP_ +#define _CLI_RESOURCES_HPP_ #include @@ -33,9 +33,6 @@ #include "trace_api.hpp" -namespace trace { - - os::String findProgram(const char*programFilename); @@ -45,6 +42,5 @@ findScript(const char *name); os::String findWrapper(const char *wrapperFilename); -} /* namespace trace */ -#endif /* _TRACE_RESOURCE_HPP_ */ +#endif /* _CLI_RESOURCES_HPP_ */ diff --git a/cli/cli_retrace.cpp b/cli/cli_retrace.cpp index a2dbd09..062f51b 100644 --- a/cli/cli_retrace.cpp +++ b/cli/cli_retrace.cpp @@ -35,7 +35,7 @@ #include "os_process.hpp" #include "trace_parser.hpp" -#include "trace_resource.hpp" +#include "cli_resources.hpp" #include "cli.hpp" #include "cli_retrace.hpp" @@ -89,7 +89,7 @@ executeRetrace(const std::vector & opts, } std::vector command; - os::String retracePath = trace::findProgram(retraceName); + os::String retracePath = findProgram(retraceName); if (retracePath.length()) { command.push_back(retracePath); } else { diff --git a/cli/cli_trace.cpp b/cli/cli_trace.cpp index 8077509..1d19ed5 100644 --- a/cli/cli_trace.cpp +++ b/cli/cli_trace.cpp @@ -28,13 +28,181 @@ #include #include +#include #include #include +#include "os_string.hpp" +#include "os_process.hpp" + #include "cli.hpp" +#include "cli_resources.hpp" + + +#if defined(__APPLE__) +#define TRACE_VARIABLE "DYLD_LIBRARY_PATH" +#define GL_TRACE_WRAPPER "OpenGL" +#elif defined(_WIN32) +#define GL_TRACE_WRAPPER "opengl32.dll" +#else +#define TRACE_VARIABLE "LD_PRELOAD" +#define GL_TRACE_WRAPPER "glxtrace.so" +#define EGL_TRACE_WRAPPER "egltrace.so" +#endif + + +static inline bool +copyWrapper(const os::String & wrapperPath, + const char *programPath, + bool verbose) +{ + os::String wrapperFilename(wrapperPath); + wrapperFilename.trimDirectory(); + + os::String tmpWrapper(programPath); + tmpWrapper.trimFilename(); + tmpWrapper.join(wrapperFilename); + + if (verbose) { + std::cerr << wrapperPath << " -> " << tmpWrapper << "\n"; + } + + if (tmpWrapper.exists()) { + std::cerr << "error: not overwriting " << tmpWrapper << "\n"; + return false; + } + + if (!os::copyFile(wrapperPath, tmpWrapper, false)) { + std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n"; + return false; + } + + return true; +} + + +static int +traceProgram(trace::API api, + char * const *argv, + const char *output, + bool verbose) +{ + const char *wrapperFilename; + std::vector args; + int status = 1; + + /* + * TODO: simplify code + */ + + bool useInject = false; + switch (api) { + case trace::API_GL: + wrapperFilename = GL_TRACE_WRAPPER; + break; +#ifdef EGL_TRACE_WRAPPER + case trace::API_EGL: + wrapperFilename = EGL_TRACE_WRAPPER; + break; +#endif +#ifdef _WIN32 + case trace::API_D3D7: + wrapperFilename = "ddraw.dll"; + break; + case trace::API_D3D8: + wrapperFilename = "d3d8.dll"; + break; + case trace::API_D3D9: + wrapperFilename = "d3d9.dll"; + break; + case trace::API_D3D10: + case trace::API_D3D10_1: + case trace::API_D3D11: + wrapperFilename = "dxgitrace.dll"; + useInject = true; + break; +#endif + default: + std::cerr << "error: unsupported API\n"; + return 1; + } + + os::String wrapperPath = findWrapper(wrapperFilename); + if (!wrapperPath.length()) { + std::cerr << "error: failed to find " << wrapperFilename << "\n"; + goto exit; + } + +#if defined(_WIN32) + if (useInject) { + args.push_back("inject"); + args.push_back(wrapperPath); + } else { + /* On Windows copy the wrapper to the program directory. + */ + if (!copyWrapper(wrapperPath, argv[0], verbose)) { + goto exit; + } + } +#else /* !_WIN32 */ + (void)useInject; +#endif /* !_WIN32 */ + +#if defined(__APPLE__) + /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the + * directory, not the file. */ + wrapperPath.trimFilename(); +#endif + +#if defined(TRACE_VARIABLE) + if (verbose) { + std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n"; + } + /* FIXME: Don't modify the current environment */ + os::setEnvironment(TRACE_VARIABLE, wrapperPath.str()); +#endif /* TRACE_VARIABLE */ + + if (output) { + os::setEnvironment("TRACE_FILE", output); + } -#include "trace_tools.hpp" + for (char * const * arg = argv; *arg; ++arg) { + args.push_back(*arg); + } + args.push_back(NULL); + + if (verbose) { + const char *sep = ""; + for (unsigned i = 0; i < args.size(); ++i) { + std::cerr << sep << args[i]; + sep = " "; + } + std::cerr << "\n"; + } + + status = os::execute((char * const *)&args[0]); + +exit: +#if defined(TRACE_VARIABLE) + os::unsetEnvironment(TRACE_VARIABLE); +#endif +#if defined(_WIN32) + if (!useInject) { + os::String tmpWrapper(argv[0]); + tmpWrapper.trimFilename(); + tmpWrapper.join(wrapperFilename); + os::removeFile(tmpWrapper); + } +#endif + + if (output) { + os::unsetEnvironment("TRACE_FILE"); + } + + return status; + +} static const char *synopsis = "Generate a new trace by executing the given program."; @@ -131,7 +299,7 @@ command(int argc, char *argv[]) } assert(argv[argc] == 0); - return trace::traceProgram(api, argv + optind, output, verbose); + return traceProgram(api, argv + optind, output, verbose); } const Command trace_command = { diff --git a/common/trace_tools.hpp b/cli/trace_tools.hpp similarity index 100% rename from common/trace_tools.hpp rename to cli/trace_tools.hpp diff --git a/common/trace_tools_trace.cpp b/cli/trace_tools_trace.cpp similarity index 99% rename from common/trace_tools_trace.cpp rename to cli/trace_tools_trace.cpp index 77fc552..fa899f9 100644 --- a/common/trace_tools_trace.cpp +++ b/cli/trace_tools_trace.cpp @@ -33,7 +33,7 @@ #include "os_string.hpp" #include "os_process.hpp" #include "trace_tools.hpp" -#include "trace_resource.hpp" +#include "cli_resources.hpp" -- 2.43.0