Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 1 | /********************************************************************* |
| 2 | * |
| 3 | * Copyright 2011 Jose Fonseca |
| 4 | * Copyright 2012 Intel Corporation |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person |
| 8 | * obtaining a copy of this software and associated documentation |
| 9 | * files (the "Software"), to deal in the Software without |
| 10 | * restriction, including without limitation the rights to use, copy, |
| 11 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 12 | * of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be |
| 16 | * included in all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | * |
| 27 | *********************************************************************/ |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include <limits.h> // for CHAR_MAX |
| 31 | #include <getopt.h> |
| 32 | #include <iostream> |
| 33 | |
| 34 | #include "cli.hpp" |
| 35 | |
| 36 | #include "os_string.hpp" |
| 37 | #include "os_process.hpp" |
| 38 | |
José Fonseca | df8c819 | 2012-11-21 01:18:49 +0000 | [diff] [blame^] | 39 | #include "trace_parser.hpp" |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 40 | #include "trace_resource.hpp" |
| 41 | |
| 42 | static const char *synopsis = "Replay a trace."; |
| 43 | |
| 44 | static void |
| 45 | usage(void) |
| 46 | { |
José Fonseca | 3320d02 | 2012-11-18 15:45:27 +0000 | [diff] [blame] | 47 | std::cout << "usage apitrace retrace [OPTIONS] TRACE_FILE\n" |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 48 | << synopsis << "\n" |
| 49 | "\n" |
| 50 | " -h, --help Show this help message and exit\n" |
| 51 | " -w, --wait Wait for user termination after the last frame\n" |
| 52 | "\n"; |
| 53 | } |
| 54 | |
| 55 | const static char * |
| 56 | shortOptions = "hw"; |
| 57 | |
| 58 | const static struct option |
| 59 | longOptions[] = { |
| 60 | {"help", no_argument, 0, 'h'}, |
| 61 | {"wait", required_argument, 0, 'w'}, |
| 62 | {0, 0, 0, 0} |
| 63 | }; |
| 64 | |
José Fonseca | df8c819 | 2012-11-21 01:18:49 +0000 | [diff] [blame^] | 65 | static trace::API |
| 66 | guessApi(const char *filename) |
| 67 | { |
| 68 | trace::Parser p; |
| 69 | if (!p.open(filename)) { |
| 70 | return trace::API_UNKNOWN; |
| 71 | } |
| 72 | trace::Call *call; |
| 73 | while ((call = p.parse_call())) { |
| 74 | delete call; |
| 75 | |
| 76 | if (p.api != trace::API_UNKNOWN) { |
| 77 | return p.api; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return trace::API_UNKNOWN; |
| 82 | } |
| 83 | |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 84 | static int |
| 85 | command(int argc, char *argv[]) |
| 86 | { |
| 87 | bool wait = false; |
José Fonseca | df8c819 | 2012-11-21 01:18:49 +0000 | [diff] [blame^] | 88 | const char *traceName; |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 89 | |
| 90 | int opt; |
| 91 | while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { |
| 92 | switch (opt) { |
| 93 | case 'h': |
| 94 | usage(); |
| 95 | return 0; |
| 96 | case 'w': |
| 97 | wait = true; |
| 98 | break; |
| 99 | default: |
| 100 | std::cerr << "error: unexpected option `" << opt << "`\n"; |
| 101 | usage(); |
| 102 | return 1; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (optind >= argc) { |
José Fonseca | 3320d02 | 2012-11-18 15:45:27 +0000 | [diff] [blame] | 107 | std::cerr << "error: apitrace retrace requires a trace file as an argument.\n"; |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 108 | usage(); |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | if (optind < argc - 1) { |
José Fonseca | 3320d02 | 2012-11-18 15:45:27 +0000 | [diff] [blame] | 113 | std::cerr << "error: apitrace retrace can accept only a single trace file argument.\n"; |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 114 | usage(); |
| 115 | return 1; |
| 116 | } |
| 117 | |
José Fonseca | df8c819 | 2012-11-21 01:18:49 +0000 | [diff] [blame^] | 118 | traceName = argv[optind]; |
| 119 | |
| 120 | trace::API api = guessApi(traceName); |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 121 | |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 122 | std::vector<const char *> command; |
José Fonseca | df8c819 | 2012-11-21 01:18:49 +0000 | [diff] [blame^] | 123 | const char *retraceName; |
| 124 | switch (api) { |
| 125 | case trace::API_GL: |
| 126 | retraceName = "glretrace"; |
| 127 | break; |
| 128 | case trace::API_EGL: |
| 129 | retraceName = "eglretrace"; |
| 130 | break; |
| 131 | case trace::API_DX: |
| 132 | case trace::API_D3D7: |
| 133 | case trace::API_D3D8: |
| 134 | case trace::API_D3D9: |
| 135 | case trace::API_D3D10: |
| 136 | case trace::API_D3D10_1: |
| 137 | case trace::API_D3D11: |
| 138 | // Can be used with WINE |
| 139 | retraceName = "d3dretrace.exe"; |
| 140 | break; |
| 141 | default: |
| 142 | std::cerr << "warning: could not guess trace's API\n"; |
| 143 | retraceName = "glretrace"; |
| 144 | break; |
| 145 | } |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 146 | |
José Fonseca | df8c819 | 2012-11-21 01:18:49 +0000 | [diff] [blame^] | 147 | os::String retracePath = trace::findProgram(retraceName); |
| 148 | if (retracePath) { |
| 149 | command.push_back(retracePath); |
| 150 | } else { |
| 151 | command.push_back(retraceName); |
| 152 | } |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 153 | |
| 154 | if (wait) { |
| 155 | command.push_back("--wait"); |
| 156 | } |
| 157 | |
José Fonseca | df8c819 | 2012-11-21 01:18:49 +0000 | [diff] [blame^] | 158 | command.push_back(traceName); |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 159 | command.push_back(NULL); |
| 160 | |
| 161 | return os::execute((char * const *)&command[0]); |
| 162 | } |
| 163 | |
José Fonseca | 3320d02 | 2012-11-18 15:45:27 +0000 | [diff] [blame] | 164 | const Command retrace_command = { |
| 165 | "retrace", |
Carl Worth | da96cbf | 2012-08-20 09:45:27 -0700 | [diff] [blame] | 166 | synopsis, |
| 167 | usage, |
| 168 | command |
| 169 | }; |