blob: cf9ffdf5786f0d46f313e016c8e19ac58316c387 [file] [log] [blame]
Carl Worthda96cbf2012-08-20 09:45:27 -07001/*********************************************************************
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
Carl Worthda96cbf2012-08-20 09:45:27 -070034#include "os_string.hpp"
35#include "os_process.hpp"
36
José Fonsecadf8c8192012-11-21 01:18:49 +000037#include "trace_parser.hpp"
Carl Worthda96cbf2012-08-20 09:45:27 -070038#include "trace_resource.hpp"
39
José Fonsecad7a44872012-11-21 08:58:42 +000040#include "cli.hpp"
41#include "cli_retrace.hpp"
42
Carl Worthda96cbf2012-08-20 09:45:27 -070043static const char *synopsis = "Replay a trace.";
44
45static void
46usage(void)
47{
José Fonsecad7a44872012-11-21 08:58:42 +000048 std::cout << "usage: apitrace retrace [OPTIONS] TRACE_FILE\n"
Carl Worthda96cbf2012-08-20 09:45:27 -070049 << synopsis << "\n"
50 "\n"
51 " -h, --help Show this help message and exit\n"
52 " -w, --wait Wait for user termination after the last frame\n"
53 "\n";
54}
55
56const static char *
57shortOptions = "hw";
58
59const static struct option
60longOptions[] = {
61 {"help", no_argument, 0, 'h'},
62 {"wait", required_argument, 0, 'w'},
63 {0, 0, 0, 0}
64};
65
José Fonsecadf8c8192012-11-21 01:18:49 +000066static trace::API
67guessApi(const char *filename)
68{
69 trace::Parser p;
70 if (!p.open(filename)) {
71 return trace::API_UNKNOWN;
72 }
73 trace::Call *call;
74 while ((call = p.parse_call())) {
75 delete call;
76
77 if (p.api != trace::API_UNKNOWN) {
78 return p.api;
79 }
80 }
81
82 return trace::API_UNKNOWN;
83}
84
José Fonsecad7a44872012-11-21 08:58:42 +000085int
86executeRetrace(const std::vector<const char *> & opts,
87 const char *traceName,
88 trace::API api) {
José Fonsecadf8c8192012-11-21 01:18:49 +000089 const char *retraceName;
90 switch (api) {
91 case trace::API_GL:
92 retraceName = "glretrace";
93 break;
94 case trace::API_EGL:
95 retraceName = "eglretrace";
96 break;
97 case trace::API_DX:
98 case trace::API_D3D7:
99 case trace::API_D3D8:
100 case trace::API_D3D9:
101 case trace::API_D3D10:
102 case trace::API_D3D10_1:
103 case trace::API_D3D11:
104 // Can be used with WINE
105 retraceName = "d3dretrace.exe";
106 break;
107 default:
108 std::cerr << "warning: could not guess trace's API\n";
109 retraceName = "glretrace";
110 break;
111 }
Carl Worthda96cbf2012-08-20 09:45:27 -0700112
José Fonsecad7a44872012-11-21 08:58:42 +0000113 std::vector<const char *> command;
José Fonsecadf8c8192012-11-21 01:18:49 +0000114 os::String retracePath = trace::findProgram(retraceName);
José Fonsecad7a44872012-11-21 08:58:42 +0000115 if (retracePath.length()) {
José Fonsecadf8c8192012-11-21 01:18:49 +0000116 command.push_back(retracePath);
117 } else {
118 command.push_back(retraceName);
119 }
Carl Worthda96cbf2012-08-20 09:45:27 -0700120
José Fonsecad7a44872012-11-21 08:58:42 +0000121 command.insert(command.end(), opts.begin(), opts.end());
Carl Worthda96cbf2012-08-20 09:45:27 -0700122
José Fonsecadf8c8192012-11-21 01:18:49 +0000123 command.push_back(traceName);
Carl Worthda96cbf2012-08-20 09:45:27 -0700124 command.push_back(NULL);
125
126 return os::execute((char * const *)&command[0]);
127}
128
José Fonsecad7a44872012-11-21 08:58:42 +0000129int
130executeRetrace(const std::vector<const char *> & opts,
131 const char *traceName) {
132 trace::API api = guessApi(traceName);
133 return executeRetrace(opts, traceName, api);
134}
135
136static int
137command(int argc, char *argv[])
138{
139 std::vector<const char *> opts;
140
141 const char *traceName;
142
143 int opt;
144 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
145 switch (opt) {
146 case 'h':
147 usage();
148 return 0;
149 case 'w':
150 opts.push_back("--wait");
151 break;
152 default:
153 std::cerr << "error: unexpected option `" << opt << "`\n";
154 usage();
155 return 1;
156 }
157 }
158
159 if (optind >= argc) {
160 std::cerr << "error: apitrace retrace requires a trace file as an argument.\n";
161 usage();
162 return 1;
163 }
164
165 if (optind < argc - 1) {
166 std::cerr << "error: apitrace retrace can accept only a single trace file argument.\n";
167 usage();
168 return 1;
169 }
170
171 traceName = argv[optind];
172
173 return executeRetrace(opts, traceName);
174}
175
José Fonseca3320d022012-11-18 15:45:27 +0000176const Command retrace_command = {
177 "retrace",
Carl Worthda96cbf2012-08-20 09:45:27 -0700178 synopsis,
179 usage,
180 command
181};