blob: db799b96ba012532ccab46fe633a22f1b7afaf32 [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"
José Fonseca8e278042012-12-07 07:00:46 +000038#include "cli_resources.hpp"
Carl Worthda96cbf2012-08-20 09:45:27 -070039
José Fonsecad7a44872012-11-21 08:58:42 +000040#include "cli.hpp"
41#include "cli_retrace.hpp"
42
Carl Worthda96cbf2012-08-20 09:45:27 -070043
José Fonsecadf8c8192012-11-21 01:18:49 +000044static trace::API
45guessApi(const char *filename)
46{
47 trace::Parser p;
48 if (!p.open(filename)) {
José Fonsecaa4bcf6a2012-12-07 09:26:01 +000049 exit(1);
José Fonsecadf8c8192012-11-21 01:18:49 +000050 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
José Fonsecad7a44872012-11-21 08:58:42 +000064int
65executeRetrace(const std::vector<const char *> & opts,
66 const char *traceName,
67 trace::API api) {
José Fonsecadf8c8192012-11-21 01:18:49 +000068 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:
José Fonsecae51e22f2012-12-07 07:48:10 +000080 case trace::API_DXGI:
81 // Use prefix so that it can be used with WINE
José Fonsecadf8c8192012-11-21 01:18:49 +000082 retraceName = "d3dretrace.exe";
83 break;
84 default:
85 std::cerr << "warning: could not guess trace's API\n";
86 retraceName = "glretrace";
87 break;
88 }
Carl Worthda96cbf2012-08-20 09:45:27 -070089
José Fonsecad7a44872012-11-21 08:58:42 +000090 std::vector<const char *> command;
José Fonseca8e278042012-12-07 07:00:46 +000091 os::String retracePath = findProgram(retraceName);
José Fonsecad7a44872012-11-21 08:58:42 +000092 if (retracePath.length()) {
José Fonsecadf8c8192012-11-21 01:18:49 +000093 command.push_back(retracePath);
94 } else {
95 command.push_back(retraceName);
96 }
Carl Worthda96cbf2012-08-20 09:45:27 -070097
José Fonsecad7a44872012-11-21 08:58:42 +000098 command.insert(command.end(), opts.begin(), opts.end());
Carl Worthda96cbf2012-08-20 09:45:27 -070099
José Fonsecaec827772012-11-21 09:13:32 +0000100 if (traceName) {
101 command.push_back(traceName);
102 }
Carl Worthda96cbf2012-08-20 09:45:27 -0700103 command.push_back(NULL);
104
105 return os::execute((char * const *)&command[0]);
106}
107
José Fonsecad7a44872012-11-21 08:58:42 +0000108int
109executeRetrace(const std::vector<const char *> & opts,
110 const char *traceName) {
111 trace::API api = guessApi(traceName);
112 return executeRetrace(opts, traceName, api);
113}
114
José Fonsecaec827772012-11-21 09:13:32 +0000115
116static const char *synopsis = "Replay a trace.";
117
118static void
119usage(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
José Fonsecad7a44872012-11-21 08:58:42 +0000127static int
128command(int argc, char *argv[])
129{
130 std::vector<const char *> opts;
José Fonsecaec827772012-11-21 09:13:32 +0000131 for (int i = 1; i < argc; ++i) {
132 opts.push_back(argv[i]);
133 }
José Fonsecad7a44872012-11-21 08:58:42 +0000134
José Fonsecaec827772012-11-21 09:13:32 +0000135 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);
José Fonsecad7a44872012-11-21 08:58:42 +0000140 }
141 }
142
José Fonsecaec827772012-11-21 09:13:32 +0000143 return executeRetrace(opts, NULL, api);
José Fonsecad7a44872012-11-21 08:58:42 +0000144}
145
José Fonseca3320d022012-11-18 15:45:27 +0000146const Command retrace_command = {
Carl Worthf5f7f7f2013-02-11 13:49:02 -0800147 "replay",
Carl Worthda96cbf2012-08-20 09:45:27 -0700148 synopsis,
149 usage,
150 command
151};