]> git.cworth.org Git - apitrace/blobdiff - cli/cli_trace.cpp
cli: Update description of windows trace apis.
[apitrace] / cli / cli_trace.cpp
index df78d55fc575c1919746af24d4e6bb5ccd07d9c6..40fad95bdf5c4b9a3a025d83ae0a32c7b45f0980 100644 (file)
  *
  *********************************************************************/
 
-#include <iostream>
-
-#include "cli.hpp"
 
-#include "os_path.hpp"
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+#include <getopt.h>
 
+#include <iostream>
 
-static int verbose = 1;
-
-static const char *synopsis = "Generate a new trace by executing the given program.";
+#include "os_string.hpp"
+#include "os_process.hpp"
 
-static void
-usage(void)
-{
-    std::cout << "usage: apitrace trace <program> [<args> ...]\n"
-        << synopsis << "\n"
-        "\n"
-        "    The given program will be executed with the given arguments.\n"
-        "    During execution, all OpenGL calls will be captured to a trace\n"
-        "    file named <program>.trace. That trace file can then be used\n"
-        "    with other apitrace utilities for replay or analysis.\n";
-}
-
-/* We only support "apitrace trace" on POSIX-like systems (not WIN32) */
-#ifndef _WIN32
+#include "cli.hpp"
+#include "cli_resources.hpp"
 
 
-#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
-find_wrapper(const char *filename)
-{
-    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();
 
-    complete = process_dir;
-    complete.join("wrappers");
-    complete.join(filename);
-#else
-    complete = APITRACE_BINARY_DIR "/wrappers";
-    complete.join(filename);
-#endif
+static inline bool
+copyWrapper(const os::String & wrapperPath,
+            const char *programPath,
+            bool verbose)
+{
+    os::String wrapperFilename(wrapperPath);
+    wrapperFilename.trimDirectory();
 
-    if (complete.exists())
-        return complete;
+    os::String tmpWrapper(programPath);
+    tmpWrapper.trimFilename();
+    tmpWrapper.join(wrapperFilename);
 
-    /* Second, look in the directory for installed wrappers. */
-    complete = APITRACE_WRAPPER_INSTALL_DIR;
-    complete.join(filename);
+    if (verbose) {
+        std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
+    }
 
-    if (complete.exists())
-        return complete;
+    if (tmpWrapper.exists()) {
+        std::cerr << "error: not overwriting " << tmpWrapper << "\n";
+        return false;
+    }
 
-    std::cerr << "error: cannot find " << filename << " (looked in " <<
-        APITRACE_WRAPPER_INSTALL_DIR << ")\n";
-    exit(1);
+    if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
+        std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
+        return false;
+    }
 
-    return "";
+    return true;
 }
 
+
 static int
-do_trace_posix(int argc, char *argv[])
+traceProgram(trace::API api,
+             char * const *argv,
+             const char *output,
+             bool verbose)
 {
-    os::Path binary = find_wrapper(CLI_TRACE_WRAPPER);
+    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_DXGI:
+        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. */
-#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 */
+    os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
+#endif /* TRACE_VARIABLE */
 
-    setenv(CLI_TRACE_VARIABLE, binary.str(), 1);
+    if (output) {
+        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 **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]);
+
+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
 
-    std::cerr << "Error: Failed to execute " << argv[0] << "\n";
+    if (output) {
+        os::unsetEnvironment("TRACE_FILE");
+    }
+    
+    return status;
 
-    return 1;
 }
 
-#endif
 
-static int
-command(int argc, char *argv[])
-{
+static const char *synopsis = "Generate a new trace by executing the given program.";
 
+static void
+usage(void)
+{
+    std::cout << "usage: apitrace trace [OPTIONS] PROGRAM [ARGS ...]\n"
+        << synopsis << "\n"
+        "\n"
+        "    The given program will be executed with the given arguments.\n"
+        "    During execution, all OpenGL calls will be captured to a trace\n"
+        "    file. That trace file can then be used\n"
+        "    with other apitrace utilities for replay or analysis.\n"
+        "\n"
+        "    -v, --verbose       verbose output\n"
+        "    -a, --api=API       specify API to trace ("
 #ifdef _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";
-    return 1;
-
+                                                      "gl, d3d7, d3d8, d3d9, or dxgi (for d3d10 and higher) "
 #else
+                                                      "gl or egl"
+#endif
+                                                      ");\n"
+        "                        default is `gl`\n"
+        "    -o, --output=TRACE  specify output trace file;\n"
+        "                        default is `PROGRAM.trace`\n";
+}
 
-    int i;
-
-    for (i = 0; i < argc; ++i) {
-        const char *arg = argv[i];
+const static char *
+shortOptions = "+hva:o:";
 
-        if (arg[0] != '-') {
-            break;
-        }
+const static struct option
+longOptions[] = {
+    {"help", no_argument, 0, 'h'},
+    {"verbose", no_argument, 0, 'v'},
+    {"api", required_argument, 0, 'a'},
+    {"output", required_argument, 0, 'o'},
+    {0, 0, 0, 0}
+};
 
-        if (!strcmp(arg, "--")) {
-            i++;
-            break;
-        } else if (!strcmp(arg, "--help")) {
+static int
+command(int argc, char *argv[])
+{
+    bool verbose = false;
+    trace::API api = trace::API_GL;
+    const char *output = NULL;
+
+    int opt;
+    while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
+        switch (opt) {
+        case 'h':
             usage();
             return 0;
-        } else {
-            std::cerr << "error: unknown option " << arg << "\n";
+        case 'v':
+            verbose = true;
+            break;
+        case 'a':
+            if (strcmp(optarg, "gl") == 0) {
+                api = trace::API_GL;
+            } else if (strcmp(optarg, "egl") == 0) {
+                api = trace::API_EGL;
+            } else if (strcmp(optarg, "d3d7") == 0) {
+                api = trace::API_D3D7;
+            } else if (strcmp(optarg, "d3d8") == 0) {
+                api = trace::API_D3D8;
+            } else if (strcmp(optarg, "d3d9") == 0) {
+                api = trace::API_D3D9;
+            } else if (strcmp(optarg, "dxgi") == 0 ||
+                       strcmp(optarg, "d3d10") == 0 ||
+                       strcmp(optarg, "d3d10_1") == 0 ||
+                       strcmp(optarg, "d3d11") == 0 ||
+                       strcmp(optarg, "d3d11_1") == 0) {
+                api = trace::API_DXGI;
+            } else {
+                std::cerr << "error: unknown API `" << optarg << "`\n";
+                usage();
+                return 1;
+            }
+            break;
+        case 'o':
+            output = optarg;
+            break;
+        default:
+            std::cerr << "error: unexpected option `" << opt << "`\n";
             usage();
             return 1;
         }
     }
 
-    if (i == argc) {
-        std::cerr << "Error: Need a command name to execute (see 'apitrace trace --help')\n";
+    if (optind == argc) {
+        std::cerr << "error: no command specified\n";
+        usage();
         return 1;
     }
 
-    return do_trace_posix(argc - i, argv + i);
-
-#endif
+    assert(argv[argc] == 0);
+    return traceProgram(api, argv + optind, output, verbose);
 }
 
 const Command trace_command = {