X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Ftrace_tools_trace.cpp;h=77fc5520ba90a3797bb5a1e3b6d75d50b9d91b21;hb=975f124938c523fc15933bcd4cdd7a25ce48ebc4;hp=f97776ed93961fbbbc444f551bb55a9d1f0b5912;hpb=09879ecc0785138789a535ceb43d788606a791e3;p=apitrace diff --git a/common/trace_tools_trace.cpp b/common/trace_tools_trace.cpp index f97776e..77fc552 100644 --- a/common/trace_tools_trace.cpp +++ b/common/trace_tools_trace.cpp @@ -30,8 +30,10 @@ #include -#include "os_path.hpp" +#include "os_string.hpp" +#include "os_process.hpp" #include "trace_tools.hpp" +#include "trace_resource.hpp" @@ -39,110 +41,167 @@ namespace trace { #if defined(__APPLE__) -#define CLI_TRACE_VARIABLE "DYLD_LIBRARY_PATH" -#define CLI_TRACE_WRAPPER "OpenGL" +#define TRACE_VARIABLE "DYLD_LIBRARY_PATH" +#define GL_TRACE_WRAPPER "OpenGL" #elif defined(_WIN32) -#define CLI_TRACE_VARIABLE "" -#define CLI_TRACE_WRAPPER "opengl32.dll" +#define GL_TRACE_WRAPPER "opengl32.dll" #else -#define CLI_TRACE_VARIABLE "LD_PRELOAD" -#define CLI_TRACE_WRAPPER "glxtrace.so" +#define TRACE_VARIABLE "LD_PRELOAD" +#define GL_TRACE_WRAPPER "glxtrace.so" +#define EGL_TRACE_WRAPPER "egltrace.so" #endif -os::Path -findFile(const char *relPath, - const char *absPath, - bool verbose) +static inline bool +copyWrapper(const os::String & wrapperPath, + const char *programPath, + bool verbose) { - os::Path complete; + os::String wrapperFilename(wrapperPath); + wrapperFilename.trimDirectory(); - /* First look in the same directory from which this process is - * running, (to support developers running a compiled program that - * has not been installed. */ - os::Path process_dir = os::getProcessName(); + os::String tmpWrapper(programPath); + tmpWrapper.trimFilename(); + tmpWrapper.join(wrapperFilename); - process_dir.trimFilename(); - - complete = process_dir; - complete.join(relPath); - - if (complete.exists()) - return complete; + if (verbose) { + std::cerr << wrapperPath << " -> " << tmpWrapper << "\n"; + } - /* Second, look in the directory for installed wrappers. */ - complete = absPath; - if (complete.exists()) - return complete; + if (tmpWrapper.exists()) { + std::cerr << "error: not overwriting " << tmpWrapper << "\n"; + return false; + } - if (verbose) { - std::cerr << "error: cannot find " << relPath << " or " << absPath << "\n"; + if (!os::copyFile(wrapperPath, tmpWrapper, false)) { + std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n"; + return false; } - return ""; + return true; } int -traceProgram(char * const *argv, +traceProgram(API api, + char * const *argv, const char *output, bool verbose) { - os::Path wrapper; - - wrapper = findFile("wrappers/" CLI_TRACE_WRAPPER, APITRACE_WRAPPER_INSTALL_DIR CLI_TRACE_WRAPPER, verbose); - - if (!wrapper.length()) { + const char *wrapperFilename; + std::vector args; + int status = 1; + + /* + * TODO: simplify code + */ + + bool useInject = false; + switch (api) { + case API_GL: + wrapperFilename = GL_TRACE_WRAPPER; + break; +#ifdef EGL_TRACE_WRAPPER + case API_EGL: + wrapperFilename = EGL_TRACE_WRAPPER; + break; +#endif +#ifdef _WIN32 + case API_D3D7: + wrapperFilename = "ddraw.dll"; + break; + case API_D3D8: + wrapperFilename = "d3d8.dll"; + break; + case API_D3D9: + wrapperFilename = "d3d9.dll"; + break; + case API_D3D10: + case API_D3D10_1: + case API_D3D11: + wrapperFilename = "dxgitrace.dll"; + useInject = true; + break; +#endif + default: + std::cerr << "error: unsupported API\n"; return 1; } -#if defined(_WIN32) - - std::cerr << - "The 'apitrace trace' command is not supported for this operating system.\n" - "Instead, you will need to copy opengl32.dll, d3d8.dll, or d3d9.dll from\n" - APITRACE_WRAPPER_INSTALL_DIR "\n" - "to the directory with the application to trace, then run the application.\n"; + os::String wrapperPath = findWrapper(wrapperFilename); + if (!wrapperPath.length()) { + std::cerr << "error: failed to find " << wrapperFilename << "\n"; + goto exit; + } -#else +#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) +#if defined(__APPLE__) /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the * directory, not the file. */ - wrapper.trimFilename(); + wrapperPath.trimFilename(); #endif +#if defined(TRACE_VARIABLE) if (verbose) { - std::cerr << CLI_TRACE_VARIABLE << "=" << wrapper.str() << "\n"; + std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n"; } - /* FIXME: Don't modify the current environment */ - setenv(CLI_TRACE_VARIABLE, wrapper.str(), 1); + os::setEnvironment(TRACE_VARIABLE, wrapperPath.str()); +#endif /* TRACE_VARIABLE */ if (output) { - setenv("TRACE_FILE", output, 1); + os::setEnvironment("TRACE_FILE", output); + } + + for (char * const * arg = argv; *arg; ++arg) { + args.push_back(*arg); } + args.push_back(NULL); if (verbose) { const char *sep = ""; - for (char * const * arg = argv; *arg; ++arg) { - std::cerr << *arg << sep; + for (unsigned i = 0; i < args.size(); ++i) { + std::cerr << sep << args[i]; sep = " "; } std::cerr << "\n"; } - execvp(argv[0], argv); + status = os::execute((char * const *)&args[0]); - unsetenv(CLI_TRACE_VARIABLE); - if (output) { - unsetenv("TRACE_FILE"); +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); } - - std::cerr << "error: Failed to execute " << argv[0] << "\n"; #endif - return 1; + if (output) { + os::unsetEnvironment("TRACE_FILE"); + } + + return status; + }