]> git.cworth.org Git - apitrace/blobdiff - common/trace_tools_trace.cpp
Add and use os::mutex.
[apitrace] / common / trace_tools_trace.cpp
index 9415edfaa0b8df11950bcef83935a95a99300626..5c88a25626c91ad77a9ba30ab5532c5dc0e80f65 100644 (file)
  *********************************************************************/
 
 
+#include <stdlib.h>
+
 #include <iostream>
 
-#include "os_path.hpp"
+#include "os_string.hpp"
+#include "os_process.hpp"
 #include "trace_tools.hpp"
 
 
 namespace trace {
 
 
-#ifdef __APPLE__
-#define CLI_TRACE_VARIABLE "DYLD_LIBRARY_PATH"
-#define CLI_TRACE_WRAPPER  "OpenGL"
+#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 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
 
 
-static os::Path
-findWrapper(const char *filename, bool verbose)
+static os::String
+findWrapper(const char *wrapperFilename)
 {
-    os::Path complete;
-
-    /* First look in the same directory from which this process is
-     * running, (to support developers running a compiled program that
-     * has not been installed. */
-#if 1
-    os::Path process_dir = os::getProcessName();
-
-    process_dir.trimFilename();
+    os::String wrapperPath;
+
+    os::String processDir = os::getProcessName();
+    processDir.trimFilename();
+
+    // Try relative build directory
+    // XXX: Just make build and install directory layout match
+    wrapperPath = processDir;
+    wrapperPath.join("wrappers");
+    wrapperPath.join(wrapperFilename);
+    if (wrapperPath.exists()) {
+        return wrapperPath;
+    }
 
-    complete = process_dir;
-    complete.join("wrappers");
-    complete.join(filename);
+    // Try relative install directory
+    wrapperPath = processDir;
+#if defined(_WIN32)
+    wrapperPath.join("..\\lib\\wrappers");
+#elif defined(__APPLE__)
+    wrapperPath.join("../lib/wrappers");
 #else
-    complete = APITRACE_BINARY_DIR "/wrappers";
-    complete.join(filename);
+    wrapperPath.join("../lib/apitrace/wrappers");
 #endif
+    wrapperPath.join(wrapperFilename);
+    if (wrapperPath.exists()) {
+        return wrapperPath;
+    }
 
-    if (complete.exists())
-        return complete;
-
-    /* Second, look in the directory for installed wrappers. */
-    complete = APITRACE_WRAPPER_INSTALL_DIR;
-    complete.join(filename);
-
-    if (complete.exists())
-        return complete;
-
-    std::cerr << "error: cannot find " << filename << " (looked in " <<
-        APITRACE_WRAPPER_INSTALL_DIR << ")\n";
-    exit(1);
+#ifndef _WIN32
+    // Try absolute install directory
+    wrapperPath = APITRACE_WRAPPERS_INSTALL_DIR;
+    wrapperPath.join(wrapperFilename);
+    if (wrapperPath.exists()) {
+        return wrapperPath;
+    }
+#endif
 
     return "";
 }
 
+
 int
-traceProgram(char * const *argv,
+traceProgram(API api,
+             char * const *argv,
              const char *output,
              bool verbose)
 {
+    const char *wrapperFilename;
+
+    /*
+     * TODO: simplify code
+     */
+
+    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:
+        wrapperFilename = "d3d10.dll";
+        break;
+    case API_D3D10_1:
+        wrapperFilename = "d3d10_1.dll";
+        break;
+    case API_D3D11:
+        wrapperFilename = "d3d11.dll";
+        break;
+#endif
+    default:
+        std::cerr << "error: unsupported API\n";
+        return 1;
+    }
 
-    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";
-    return 1;
+    os::String wrapperPath = findWrapper(wrapperFilename);
 
-#else
-    os::Path binary = findWrapper(CLI_TRACE_WRAPPER, verbose);
+    if (!wrapperPath.length()) {
+        std::cerr << "error: failed to find " << wrapperFilename << "\n";
+        return 1;
+    }
+
+#if defined(_WIN32)
+    /* On Windows copy the wrapper to the program directory.
+     */
+    os::String tmpWrapper(argv[0]);
+    tmpWrapper.trimFilename();
+    tmpWrapper.join(wrapperFilename);
 
+    if (verbose) {
+        std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
+    }
+
+    if (tmpWrapper.exists()) {
+        std::cerr << "error: not overwriting " << tmpWrapper << "\n";
+        return 1;
+    }
+
+    if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
+        std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
+        return 1;
+    }
+#endif /* _WIN32 */
+
+#if defined(__APPLE__)
     /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
      * directory, not the file. */
-#ifdef __APPLE__
-    binary.trimFilename();
+    wrapperPath.trimFilename();
 #endif
 
+#if defined(TRACE_VARIABLE)
     if (verbose) {
-        std::cerr << CLI_TRACE_VARIABLE << "=" << binary.str() << "\n";
+        std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
     }
-
     /* FIXME: Don't modify the current environment */
-    setenv(CLI_TRACE_VARIABLE, binary.str(), 1);
+    os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
+#endif /* TRACE_VARIABLE */
 
     if (output) {
-        setenv("TRACE_FILE", output, 1);
+        os::setEnvironment("TRACE_FILE", output);
     }
 
     if (verbose) {
@@ -126,17 +197,21 @@ traceProgram(char * const *argv,
         std::cerr << "\n";
     }
 
-    execvp(argv[0], argv);
-    
-    unsetenv(CLI_TRACE_VARIABLE);
-    if (output) {
-        unsetenv("TRACE_FILE");
-    }
+    int status = os::execute(argv);
 
-    std::cerr << "error: Failed to execute " << argv[0] << "\n";
+#if defined(TRACE_VARIABLE)
+    os::unsetEnvironment(TRACE_VARIABLE);
+#endif
+#if defined(_WIN32)
+    os::removeFile(tmpWrapper);
 #endif
 
-    return 1;
+    if (output) {
+        os::unsetEnvironment("TRACE_FILE");
+    }
+    
+    return status;
+
 }