From: José Fonseca Date: Thu, 3 Nov 2011 13:59:54 +0000 (+0000) Subject: Split common functionality out of cli. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=efb9e170ed59dc5fc205bf890e9954fdf0a0cafc;p=apitrace Split common functionality out of cli. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index c7f3a44..87aac12 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -247,6 +247,7 @@ add_library (common STATIC common/trace_writer_local.cpp common/trace_writer_model.cpp common/trace_loader.cpp + common/trace_tools_trace.cpp common/image.cpp common/image_bmp.cpp common/image_pnm.cpp diff --git a/cli/cli_trace.cpp b/cli/cli_trace.cpp index df78d55..0d1f740 100644 --- a/cli/cli_trace.cpp +++ b/cli/cli_trace.cpp @@ -25,14 +25,16 @@ * *********************************************************************/ + +#include +#include + #include #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."; @@ -45,124 +47,39 @@ usage(void) " 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 .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); -#else - complete = APITRACE_BINARY_DIR "/wrappers"; - complete.join(filename); -#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 ""; -} - -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; + " with other apitrace utilities for replay or analysis.\n" + "\n" + " -v, --verbose verbose output\n" + " -o, --output TRACE specify output trace file\n"; } -#endif - 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 - + bool verbose = false; + const char *output = NULL; int i; - for (i = 0; i < argc; ++i) { + for (i = 0; i < argc; ) { const char *arg = argv[i]; if (arg[0] != '-') { break; } - if (!strcmp(arg, "--")) { - i++; + ++i; + + if (strcmp(arg, "--") == 0) { break; - } else if (!strcmp(arg, "--help")) { + } else if (strcmp(arg, "--help") == 0) { usage(); return 0; + } else if (strcmp(arg, "-v") == 0 || + strcmp(arg, "--verbose") == 0) { + verbose = true; + } else if (strcmp(arg, "-o") == 0 || + strcmp(arg, "--output") == 0) { + output = argv[i++]; } else { std::cerr << "error: unknown option " << arg << "\n"; usage(); @@ -171,13 +88,13 @@ command(int argc, char *argv[]) } if (i == argc) { - std::cerr << "Error: Need a command name to execute (see 'apitrace trace --help')\n"; + 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(argv + i, output, verbose); } const Command trace_command = { diff --git a/common/trace_tools.hpp b/common/trace_tools.hpp new file mode 100644 index 0000000..31831ad --- /dev/null +++ b/common/trace_tools.hpp @@ -0,0 +1,45 @@ +/************************************************************************** + * + * Copyright 2011 VMware, Inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + **************************************************************************/ + +#ifndef _TRACE_TOOLS_HPP_ +#define _TRACE_TOOLS_HPP_ + + +#include + + +namespace trace { + + +int +traceProgram(char * const *argv, + const char *output = NULL, + bool verbose = false); + + + +} /* namespace trace */ + +#endif /* _TRACE_TOOLS_HPP_ */ diff --git a/common/trace_tools_trace.cpp b/common/trace_tools_trace.cpp new file mode 100644 index 0000000..9415edf --- /dev/null +++ b/common/trace_tools_trace.cpp @@ -0,0 +1,143 @@ +/********************************************************************* + * + * Copyright 2011 Intel Corporation + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *********************************************************************/ + + +#include + +#include "os_path.hpp" +#include "trace_tools.hpp" + + + +namespace trace { + + +#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 +findWrapper(const char *filename, bool verbose) +{ + 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 + + 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 ""; +} + +int +traceProgram(char * const *argv, + const char *output, + bool verbose) +{ +#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 + os::Path binary = findWrapper(CLI_TRACE_WRAPPER, verbose); + + /* 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"; + } + + /* FIXME: Don't modify the current environment */ + setenv(CLI_TRACE_VARIABLE, binary.str(), 1); + + if (output) { + setenv("TRACE_FILE", output, 1); + } + + if (verbose) { + const char *sep = ""; + for (char * const * arg = argv; *arg; ++arg) { + std::cerr << *arg << sep; + sep = " "; + } + std::cerr << "\n"; + } + + execvp(argv[0], argv); + + unsetenv(CLI_TRACE_VARIABLE); + if (output) { + unsetenv("TRACE_FILE"); + } + + std::cerr << "error: Failed to execute " << argv[0] << "\n"; +#endif + + return 1; +} + + +} /* namespace trace */