/********************************************************************* * * 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. * *********************************************************************/ /* * Top-level application for accessing most all apitrace * functionality. */ #include #include #include #include #include #include #include #include "config.h" #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0])) typedef int (*command_function_t) (int argc, char *argv[], int first_command_arg); typedef struct command { const char *name; command_function_t function; const char *arguments; const char *summary; const char *documentation; } command_t; static int apitrace_trace_command(int argc, char *argv[], int first_command_arg); static int apitrace_help_command(int argc, char *argv[], int first_command_arg); static command_t commands[] = { { "trace", apitrace_trace_command, " [ ...]", "Trace execution of , writing to .trace", "\tThe given program will be executed with the given arguments.\n" "\tDuring execution, all OpenGL calls will be captured to a trace\n" "\tfile named .trace. That trace file can then be used\n" "\twith other apitrace utilities for replay or analysis." }, { "help", apitrace_help_command, "[]", "Print detailed help for the given command.", "\tExcept in this case, where this is all the help you will get." } }; static void usage(void) { command_t *command; int i, max_width = 0; std::cout << "Usage: apitrace [ ...]\n\n" "The available commands are as follows:\n\n"; std::cout << std::setiosflags(std::ios::left); for (i = 0; i < ARRAY_SIZE(commands); i++) { command = &commands[i]; if (strlen(command->name) > max_width) max_width = strlen(command->name); } for (i = 0; i < ARRAY_SIZE(commands); i++) { command = &commands[i]; std::cout << " " << std::setw(max_width+2) << command->name << " " << command->summary << "\n"; } std::cout << "\n" "Use \"apitrace help \" for more details on each command.\n"; } static int apitrace_help_command(int argc, char *argv[], int first_arg_command) { command_t *command; int i; if(argc == 0) { usage(); return 0; } for (i = 0; i < ARRAY_SIZE(commands); i++) { command = &commands[i]; if (strcmp(argv[0], command->name) == 0) { std::cout << "Help for \"apitrace " << argv[0] << "\":\n\n"; std::cout << command->name; if (command->arguments) std::cout << " " << command->arguments << "\n\n\t" << command->summary; else std::cout << "\t" << command->summary; std::cout << "\n\n" << command->documentation << "\n\n"; return 0; } } std::cerr << "Error: Unknown command: " << argv[0] << " (see \"apitrace help\").\n"; return 1; } /* Returns whether path exists as a regular file. */ static int _executable_exists(const char *path) { struct stat st; int err; err = stat(path, &st); if (err) return 0; if (! S_ISREG(st.st_mode)) return 0; return 1; } /* Given an relative 'path', return a newly allocated string giving * the absolute version of path. * * Will return 'path' itself if it is already absolute, or if it is * NULL or an empty string. */ static char * _make_absolute(void *ctx, char *path) { char *cwd, *absolute; if (path == NULL || *path == '\0' || *path == '/') return path; cwd = get_current_dir_name(); absolute = talloc_asprintf(ctx, "%s/%s", cwd, path); free (cwd); return absolute; } /* Given argv0 determine the directory from which the program was * run. If argv0 contains a '/' the directory is simply read from the * string itself. Otherwise, this function searches for the executable * by examining each directory within the PATH environment variable. * * Returns an empty string if argv0 is NULL or an empty string, or if * a path cannot be resolved. * * Note: This function may return a relative directory. Call * _make_absolute on the result if you need an absolute directory. */ char * _find_argv0_directory(void *ctx, const char *argv0) { const char *last_slash, *colon, *path; if (argv0 == NULL || argv0[0] == '\0') return talloc_strdup(ctx, ""); last_slash = argv0 + strlen(argv0); while (last_slash > argv0 && *last_slash != '/') last_slash--; /* Contains a '/'. Copy argv0 to just before final '/'. */ if (last_slash > argv0) return talloc_strndup(ctx, argv0, last_slash - argv0); /* Contains no '/'. Search PATH. */ path = getenv("PATH"); while (1) { char *dir; if (*path == '\0') break; colon = strchr(path, ':'); if (colon == NULL) colon = path + strlen(path); dir = talloc_strndup(ctx, path, colon - path); if (dir && *dir) { char *complete = talloc_asprintf(ctx, "%s/%s", dir, argv0); if (_executable_exists(complete)) { talloc_free(complete); return dir; } } talloc_free(dir); path = colon; while (*path == ':') path++; } return talloc_strdup(ctx, ""); } /* Find 'executable' by looking first in the apitrace private bin * directory (for example /usr/lib/apitrace), and then in the same * directory from which the current process was likely executed as * determined by 'argv0' and the PATH environment variable. */ static char * _find_private_bin(void *ctx, const char *executable, const char *argv0) { char *path, *dir; /* First look in our private bin directory. */ path = talloc_asprintf(ctx, "%s/%s", APITRACE_PRIVATE_BIN_DIR, executable); if (_executable_exists(path)) return path; talloc_free(path); /* Otherwise, look in the directory from which this executable was run. */ dir = _find_argv0_directory(ctx, argv0); path = talloc_asprintf(ctx, "%s/%s",dir, executable); talloc_free(dir); if (_executable_exists(path)) return path; std::cerr << "Error: Cannot find executable " << executable << ".\n"; exit (1); } static int apitrace_trace_command(int argc, char *argv[], int first_arg_command) { char *path; void *ctx = talloc_new(NULL); path = _find_private_bin(ctx, "glxtrace.so", argv[0]); path = _make_absolute (ctx, path); setenv("LD_PRELOAD", path, 1); talloc_free(ctx); execvp(argv[first_arg_command], argv + first_arg_command); std::cerr << "Error: Failed to execvp " << argv[first_arg_command] << "\n"; return 1; } int main(int argc, char **argv) { const char *command_name = "trace"; command_t *command; int i, first_command_arg; if (argc == 1) { usage(); return 1; } for (i = 1; i < argc; ++i) { const char *arg = argv[i]; if (arg[0] != '-') { break; } if (strcmp(arg, "--help") == 0) { return apitrace_help_command (0, NULL, 0); } else { std::cerr << "Error: unknown option " << arg << "\n"; usage(); return 1; } } first_command_arg = i; if (first_command_arg < argc) { command_name = argv[first_command_arg]; first_command_arg++; } for (i = 0; i < ARRAY_SIZE(commands); i++) { command = &commands[i]; if (strcmp(command_name, command->name) == 0) return (command->function) (argc, argv, first_command_arg); } std::cerr << "Error: unknown command " << command_name << " (see \"apitrace help\").\n"; return 1; }