]> git.cworth.org Git - apitrace/blobdiff - common/trace_tools_trace.cpp
inject: Use DLL injection for D3D10+ tracing.
[apitrace] / common / trace_tools_trace.cpp
index 5c88a25626c91ad77a9ba30ab5532c5dc0e80f65..77fc5520ba90a3797bb5a1e3b6d75d50b9d91b21 100644 (file)
@@ -33,6 +33,7 @@
 #include "os_string.hpp"
 #include "os_process.hpp"
 #include "trace_tools.hpp"
+#include "trace_resource.hpp"
 
 
 
@@ -51,47 +52,33 @@ namespace trace {
 #endif
 
 
-static os::String
-findWrapper(const char *wrapperFilename)
+static inline bool
+copyWrapper(const os::String & wrapperPath,
+            const char *programPath,
+            bool verbose)
 {
-    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;
+    os::String wrapperFilename(wrapperPath);
+    wrapperFilename.trimDirectory();
+
+    os::String tmpWrapper(programPath);
+    tmpWrapper.trimFilename();
+    tmpWrapper.join(wrapperFilename);
+
+    if (verbose) {
+        std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
     }
 
-    // Try relative install directory
-    wrapperPath = processDir;
-#if defined(_WIN32)
-    wrapperPath.join("..\\lib\\wrappers");
-#elif defined(__APPLE__)
-    wrapperPath.join("../lib/wrappers");
-#else
-    wrapperPath.join("../lib/apitrace/wrappers");
-#endif
-    wrapperPath.join(wrapperFilename);
-    if (wrapperPath.exists()) {
-        return wrapperPath;
+    if (tmpWrapper.exists()) {
+        std::cerr << "error: not overwriting " << tmpWrapper << "\n";
+        return false;
     }
 
-#ifndef _WIN32
-    // Try absolute install directory
-    wrapperPath = APITRACE_WRAPPERS_INSTALL_DIR;
-    wrapperPath.join(wrapperFilename);
-    if (wrapperPath.exists()) {
-        return wrapperPath;
+    if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
+        std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
+        return false;
     }
-#endif
 
-    return "";
+    return true;
 }
 
 
@@ -102,11 +89,14 @@ traceProgram(API api,
              bool verbose)
 {
     const char *wrapperFilename;
+    std::vector<const char *> args;
+    int status = 1;
 
     /*
      * TODO: simplify code
      */
 
+    bool useInject = false;
     switch (api) {
     case API_GL:
         wrapperFilename = GL_TRACE_WRAPPER;
@@ -127,13 +117,10 @@ traceProgram(API api,
         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";
+        wrapperFilename = "dxgitrace.dll";
+        useInject = true;
         break;
 #endif
     default:
@@ -142,33 +129,25 @@ traceProgram(API api,
     }
 
     os::String wrapperPath = findWrapper(wrapperFilename);
-
     if (!wrapperPath.length()) {
         std::cerr << "error: failed to find " << wrapperFilename << "\n";
-        return 1;
+        goto exit;
     }
 
 #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;
+    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;
+        }
     }
-#endif /* _WIN32 */
+#else  /* !_WIN32 */
+    (void)useInject;
+#endif /* !_WIN32 */
 
 #if defined(__APPLE__)
     /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
@@ -188,22 +167,33 @@ traceProgram(API api,
         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";
     }
 
-    int status = os::execute(argv);
+    status = os::execute((char * const *)&args[0]);
 
+exit:
 #if defined(TRACE_VARIABLE)
     os::unsetEnvironment(TRACE_VARIABLE);
 #endif
 #if defined(_WIN32)
-    os::removeFile(tmpWrapper);
+    if (!useInject) {
+        os::String tmpWrapper(argv[0]);
+        tmpWrapper.trimFilename();
+        tmpWrapper.join(wrapperFilename);
+        os::removeFile(tmpWrapper);
+    }
 #endif
 
     if (output) {