]> git.cworth.org Git - apitrace/blobdiff - cli/cli_trace.cpp
cli: Merge trace_resource.cpp and trace_tools_trace.cpp in.
[apitrace] / cli / cli_trace.cpp
index 80775090c7b1be79cbce6dc9a8e9e6f6b8c521d2..1d19ed5c5ba11e9662d3253192d2ca5e26869122 100644 (file)
 
 #include <assert.h>
 #include <string.h>
+#include <stdlib.h>
 #include <getopt.h>
 
 #include <iostream>
 
+#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<const char *> 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 = {