]> git.cworth.org Git - apitrace/blob - cli/cli_retrace.cpp
d22f719bd65d6a584c416db6010d558a6a55ac9a
[apitrace] / cli / cli_retrace.cpp
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 "os_string.hpp"
35 #include "os_process.hpp"
36
37 #include "trace_parser.hpp"
38 #include "cli_resources.hpp"
39
40 #include "cli.hpp"
41 #include "cli_retrace.hpp"
42
43
44 static trace::API
45 guessApi(const char *filename)
46 {
47     trace::Parser p;
48     if (!p.open(filename)) {
49         exit(1);
50         return trace::API_UNKNOWN;
51     }
52     trace::Call *call;
53     while ((call = p.parse_call())) {
54         delete call;
55
56         if (p.api != trace::API_UNKNOWN) {
57             return p.api;
58         }
59     }
60
61     return trace::API_UNKNOWN;
62 }
63
64 int
65 executeRetrace(const std::vector<const char *> & opts,
66                const char *traceName,
67                trace::API api) {
68     const char *retraceName;
69     switch (api) {
70     case trace::API_GL:
71         retraceName = "glretrace";
72         break;
73     case trace::API_EGL:
74         retraceName = "eglretrace";
75         break;
76     case trace::API_DX:
77     case trace::API_D3D7:
78     case trace::API_D3D8:
79     case trace::API_D3D9:
80     case trace::API_DXGI:
81         // Use prefix so that it can be used with WINE
82         retraceName = "d3dretrace.exe";
83         break;
84     default:
85         std::cerr << "warning: could not guess trace's API\n";
86         retraceName = "glretrace";
87         break;
88     }
89
90     std::vector<const char *> command;
91     os::String retracePath = findProgram(retraceName);
92     if (retracePath.length()) {
93         command.push_back(retracePath);
94     } else {
95         command.push_back(retraceName);
96     }
97
98     command.insert(command.end(), opts.begin(), opts.end());
99
100     if (traceName) {
101         command.push_back(traceName);
102     }
103     command.push_back(NULL);
104
105     return os::execute((char * const *)&command[0]);
106 }
107
108 int
109 executeRetrace(const std::vector<const char *> & opts,
110                const char *traceName) {
111     trace::API api = guessApi(traceName);
112     return executeRetrace(opts, traceName, api);
113 }
114
115
116 static const char *synopsis = "Replay a trace.";
117
118 static void
119 usage(void)
120 {
121     std::vector<const char *>opts;
122     opts.push_back("--help");
123     trace::API api = trace::API_GL;
124     executeRetrace(opts, NULL, api);
125 }
126
127 static int
128 command(int argc, char *argv[])
129 {
130     std::vector<const char *> opts;
131     for (int i = 1; i < argc; ++i) {
132         opts.push_back(argv[i]);
133     }
134
135     trace::API api = trace::API_GL;
136     if (argc >= 1) {
137         const char *lastArg = argv[argc -1];
138         if (lastArg[0] != '-') {
139             api = guessApi(lastArg);
140         }
141     }
142
143     return executeRetrace(opts, NULL, api);
144 }
145
146 const Command retrace_command = {
147     "retrace",
148     synopsis,
149     usage,
150     command
151 };