*
*********************************************************************/
-#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.";
-
-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";
-}
+#include "os_string.hpp"
+#include "os_process.hpp"
-/* We only support "apitrace trace" on POSIX-like systems (not WIN32) */
-#ifndef _WIN32
+#include "cli.hpp"
+#include "cli_resources.hpp"
-#include <sys/stat.h>
-#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 int
-file_exists(const char *path)
+
+static inline bool
+copyWrapper(const os::String & wrapperPath,
+ const char *programPath,
+ bool verbose)
{
- struct stat st;
- int err;
+ os::String wrapperFilename(wrapperPath);
+ wrapperFilename.trimDirectory();
+
+ os::String tmpWrapper(programPath);
+ tmpWrapper.trimFilename();
+ tmpWrapper.join(wrapperFilename);
+
+ if (verbose) {
+ std::cerr << wrapperPath << " -> " << tmpWrapper << "\n";
+ }
- err = stat(path, &st);
- if (err)
- return 0;
+ if (tmpWrapper.exists()) {
+ std::cerr << "error: not overwriting " << tmpWrapper << "\n";
+ return false;
+ }
- if (! S_ISREG(st.st_mode))
- return 0;
+ if (!os::copyFile(wrapperPath, tmpWrapper, false)) {
+ std::cerr << "error: failed to copy " << wrapperPath << " into " << tmpWrapper << "\n";
+ return false;
+ }
- return 1;
+ return true;
}
-static os::Path
-find_wrapper(const char *filename)
+
+static int
+traceProgram(trace::API api,
+ char * const *argv,
+ const char *output,
+ bool verbose)
{
- os::Path complete;
+ 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;
+ }
- /* 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 wrapperPath = findWrapper(wrapperFilename, verbose);
+ if (!wrapperPath.length()) {
+ std::cerr << "error: failed to find " << wrapperFilename << " wrapper\n";
+ goto exit;
+ }
- process_dir.trimFilename();
+#if defined(_WIN32)
+ useInject = true;
+ 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 */
- complete = process_dir;
- complete.join("wrappers");
- complete.join(filename);
-#else
- complete = APITRACE_BINARY_DIR "/wrappers";
- complete.join(filename);
+#if defined(__APPLE__)
+ /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
+ * directory, not the file. */
+ wrapperPath.trimFilename();
#endif
- if (file_exists(complete))
- return complete;
+ /*
+ * Spawn child process.
+ */
- /* Second, look in the directory for installed wrappers. */
- complete = APITRACE_WRAPPER_INSTALL_DIR;
- complete.join(filename);
+ {
+#if defined(TRACE_VARIABLE)
+ const char *oldEnvVarValue = getenv(TRACE_VARIABLE);
+ if (oldEnvVarValue) {
+ wrapperPath.append(OS_PATH_SEP);
+ wrapperPath.append(oldEnvVarValue);
+ }
- if (file_exists(complete))
- return complete;
+ /* FIXME: Don't modify our (ie parent) environment */
+ os::setEnvironment(TRACE_VARIABLE, wrapperPath.str());
- std::cerr << "error: cannot find " << filename << " (looked in " <<
- APITRACE_WRAPPER_INSTALL_DIR << ")\n";
- exit(1);
+ if (verbose) {
+ std::cerr << TRACE_VARIABLE << "=" << wrapperPath.str() << "\n";
+ }
+#endif /* TRACE_VARIABLE */
- return "";
-}
+ if (output) {
+ os::setEnvironment("TRACE_FILE", output);
+ }
-static int
-do_trace_posix(int argc, char *argv[])
-{
- os::Path binary = find_wrapper(CLI_TRACE_WRAPPER);
+ for (char * const * arg = argv; *arg; ++arg) {
+ args.push_back(*arg);
+ }
- /* On Mac OS X, using DYLD_LIBRARY_PATH, we actually set the
- * directory, not the file. */
-#ifdef __APPLE__
- binary.trimFilename();
-#endif
+ if (verbose) {
+ const char *sep = "";
+ for (unsigned i = 0; i < args.size(); ++i) {
+ std::cerr << sep << args[i];
+ sep = " ";
+ }
+ std::cerr << "\n";
+ }
- if (verbose) {
- std::cerr << CLI_TRACE_VARIABLE << "=" << binary.str() << "\n";
- }
+ args.push_back(NULL);
- setenv(CLI_TRACE_VARIABLE, binary.str(), 1);
+ status = os::execute((char * const *)&args[0]);
- if (verbose) {
- const char *sep = "";
- for (char **arg = argv; *arg; ++arg) {
- std::cerr << *arg << sep;
- sep = " ";
+#if defined(TRACE_VARIABLE)
+ if (oldEnvVarValue) {
+ os::setEnvironment(TRACE_VARIABLE, oldEnvVarValue);
+ } else {
+ os::unsetEnvironment(TRACE_VARIABLE);
}
- std::cerr << "\n";
+#endif
}
- execvp(argv[0], argv);
+exit:
+#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 = {