]> git.cworth.org Git - apitrace/blobdiff - common/trace_tools_trace.cpp
image: Make PNG writing an Image method.
[apitrace] / common / trace_tools_trace.cpp
index 9415edfaa0b8df11950bcef83935a95a99300626..4c0082d7e51771e1d540c9ce53e5372ca27ef713 100644 (file)
  *********************************************************************/
 
 
+#include <stdlib.h>
+
 #include <iostream>
 
-#include "os_path.hpp"
+#include "os_string.hpp"
+#include "os_process.hpp"
 #include "trace_tools.hpp"
+#include "trace_resource.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 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. */
-#if 1
-    os::Path process_dir = os::getProcessName();
+    os::String tmpWrapper(programPath);
+    tmpWrapper.trimFilename();
+    tmpWrapper.join(wrapperFilename);
 
-    process_dir.trimFilename();
+    if (verbose) {
+        std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
+    }
 
-    complete = process_dir;
-    complete.join("wrappers");
-    complete.join(filename);
-#else
-    complete = APITRACE_BINARY_DIR "/wrappers";
-    complete.join(filename);
-#endif
+    if (tmpWrapper.exists()) {
+        std::cerr << "error: not overwriting " << tmpWrapper << "\n";
+        return false;
+    }
 
-    if (complete.exists())
-        return complete;
+    if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
+        std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
+        return false;
+    }
 
-    /* Second, look in the directory for installed wrappers. */
-    complete = APITRACE_WRAPPER_INSTALL_DIR;
-    complete.join(filename);
+    return true;
+}
 
-    if (complete.exists())
-        return complete;
 
-    std::cerr << "error: cannot find " << filename << " (looked in " <<
-        APITRACE_WRAPPER_INSTALL_DIR << ")\n";
-    exit(1);
+static const char *glWrappers[] = {
+    GL_TRACE_WRAPPER,
+    NULL
+};
 
-    return "";
-}
+#ifdef EGL_TRACE_WRAPPER
+static const char *eglWrappers[] = {
+    EGL_TRACE_WRAPPER,
+    NULL
+};
+#endif
+
+#ifdef _WIN32
+static const char *d3d7Wrappers[] = {
+    "ddraw.dll",
+    NULL
+};
+
+static const char *d3d8Wrappers[] = {
+    "d3d8.dll",
+    NULL
+};
+
+static const char *d3d9Wrappers[] = {
+    "d3d9.dll",
+    NULL
+};
+
+static const char *dxgiWrappers[] = {
+    "dxgitrace.dll",
+    //"dxgi.dll",
+    "d3d10.dll",
+    "d3d10_1.dll",
+    "d3d11.dll",
+    NULL
+};
+#endif
 
 int
-traceProgram(char * const *argv,
+traceProgram(API api,
+             char * const *argv,
              const char *output,
              bool verbose)
 {
+    const char **wrapperFilenames;
+    unsigned numWrappers;
+    int status = 1;
+
+    /*
+     * TODO: simplify code
+     */
+
+    switch (api) {
+    case API_GL:
+        wrapperFilenames = glWrappers;
+        break;
+#ifdef EGL_TRACE_WRAPPER
+    case API_EGL:
+        wrapperFilenames = eglWrappers;
+        break;
+#endif
 #ifdef _WIN32
+    case API_D3D7:
+        wrapperFilenames = d3d7Wrappers;
+        break;
+    case API_D3D8:
+        wrapperFilenames = d3d8Wrappers;
+        break;
+    case API_D3D9:
+        wrapperFilenames = d3d9Wrappers;
+        break;
+    case API_D3D10:
+    case API_D3D10_1:
+    case API_D3D11:
+        wrapperFilenames = dxgiWrappers;
+        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;
+    numWrappers = 0;
+    while (wrapperFilenames[numWrappers]) {
+        ++numWrappers;
+    }
 
-#else
-    os::Path binary = findWrapper(CLI_TRACE_WRAPPER, verbose);
+    unsigned i;
+    for (i = 0; i < numWrappers; ++i) {
+        const char *wrapperFilename = wrapperFilenames[i];
 
-    /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
-     * directory, not the file. */
-#ifdef __APPLE__
-    binary.trimFilename();
+        os::String wrapperPath = findWrapper(wrapperFilename);
+
+        if (!wrapperPath.length()) {
+            std::cerr << "error: failed to find " << wrapperFilename << "\n";
+            goto exit;
+        }
+
+#if defined(_WIN32)
+        /* On Windows copy the wrapper to the program directory.
+         */
+        if (!copyWrapper(wrapperPath, argv[0], verbose)) {
+            goto exit;
+        }
+#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 (verbose) {
-        std::cerr << CLI_TRACE_VARIABLE << "=" << binary.str() << "\n";
+#if defined(TRACE_VARIABLE)
+        assert(numWrappers == 1);
+        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 */
     }
 
-    /* FIXME: Don't modify the current environment */
-    setenv(CLI_TRACE_VARIABLE, binary.str(), 1);
-
     if (output) {
-        setenv("TRACE_FILE", output, 1);
+        os::setEnvironment("TRACE_FILE", output);
     }
 
     if (verbose) {
@@ -126,17 +217,28 @@ traceProgram(char * const *argv,
         std::cerr << "\n";
     }
 
-    execvp(argv[0], argv);
-    
-    unsetenv(CLI_TRACE_VARIABLE);
-    if (output) {
-        unsetenv("TRACE_FILE");
-    }
+    status = os::execute(argv);
 
-    std::cerr << "error: Failed to execute " << argv[0] << "\n";
+exit:
+#if defined(TRACE_VARIABLE)
+    os::unsetEnvironment(TRACE_VARIABLE);
+#endif
+#if defined(_WIN32)
+    for (unsigned j = 0; j < i; ++j) {
+        const char *wrapperFilename = wrapperFilenames[j];
+        os::String tmpWrapper(argv[0]);
+        tmpWrapper.trimFilename();
+        tmpWrapper.join(wrapperFilename);
+        os::removeFile(tmpWrapper);
+    }
 #endif
 
-    return 1;
+    if (output) {
+        os::unsetEnvironment("TRACE_FILE");
+    }
+    
+    return status;
+
 }