]> git.cworth.org Git - apitrace/blobdiff - cli/cli_trace.cpp
cli: Auto-detect retrace for dump-images too.
[apitrace] / cli / cli_trace.cpp
index df78d55fc575c1919746af24d4e6bb5ccd07d9c6..80775090c7b1be79cbce6dc9a8e9e6f6b8c521d2 100644 (file)
  *
  *********************************************************************/
 
+
+#include <assert.h>
+#include <string.h>
+#include <getopt.h>
+
 #include <iostream>
 
 #include "cli.hpp"
 
-#include "os_path.hpp"
+#include "trace_tools.hpp"
 
 
-static int verbose = 1;
-
 static const char *synopsis = "Generate a new trace by executing the given program.";
 
 static void
 usage(void)
 {
-    std::cout << "usage: apitrace trace <program> [<args> ...]\n"
+    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 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
-
-
-#ifdef __APPLE__
-#define CLI_TRACE_VARIABLE "DYLD_LIBRARY_PATH"
-#define CLI_TRACE_WRAPPER  "OpenGL"
-#else
-#define CLI_TRACE_VARIABLE "LD_PRELOAD"
-#define CLI_TRACE_WRAPPER  "glxtrace.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);
+        "    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
+                                                      "gl, d3d7, d3d8, d3d9, or d3d10"
 #else
-    complete = APITRACE_BINARY_DIR "/wrappers";
-    complete.join(filename);
+                                                      "gl or egl"
 #endif
-
-    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);
-
-    return "";
+                                                      ");\n"
+        "                        default is `gl`\n"
+        "    -o, --output=TRACE  specify output trace file;\n"
+        "                        default is `PROGRAM.trace`\n";
 }
 
-static int
-do_trace_posix(int argc, char *argv[])
-{
-    os::Path binary = find_wrapper(CLI_TRACE_WRAPPER);
-
-    /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
-     * directory, not the file. */
-#ifdef __APPLE__
-    binary.trimFilename();
-#endif
-
-    if (verbose) {
-        std::cerr << CLI_TRACE_VARIABLE << "=" << binary.str() << "\n";
-    }
-
-    setenv(CLI_TRACE_VARIABLE, binary.str(), 1);
-
-    if (verbose) {
-        const char *sep = "";
-        for (char **arg = argv; *arg; ++arg) {
-            std::cerr << *arg << sep;
-            sep = " ";
-        }
-        std::cerr << "\n";
-    }
-
-    execvp(argv[0], argv);
-
-    std::cerr << "Error: Failed to execute " << argv[0] << "\n";
-
-    return 1;
-}
+const static char *
+shortOptions = "+hva:o:";
 
-#endif
+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}
+};
 
 static int
 command(int argc, char *argv[])
 {
-
-#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;
-
-#else
-
-    int i;
-
-    for (i = 0; i < argc; ++i) {
-        const char *arg = argv[i];
-
-        if (arg[0] != '-') {
-            break;
-        }
-
-        if (!strcmp(arg, "--")) {
-            i++;
-            break;
-        } else if (!strcmp(arg, "--help")) {
+    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, "d3d10") == 0) {
+                api = trace::API_D3D10;
+            } else if (strcmp(optarg, "d3d10_1") == 0) {
+                api = trace::API_D3D10_1;
+            } else if (strcmp(optarg, "d3d11") == 0) {
+                api = trace::API_D3D11;
+            } 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 trace::traceProgram(api, argv + optind, output, verbose);
 }
 
 const Command trace_command = {