blob: e4d8f788f972d40b23b3803f02721500b7a2e879 [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)) {
49 return trace::API_UNKNOWN;
50 }
51 trace::Call *call;
52 while ((call = p.parse_call())) {
53 delete call;
54
55 if (p.api != trace::API_UNKNOWN) {
56 return p.api;
57 }
58 }
59
60 return trace::API_UNKNOWN;
61}
62
José Fonsecad7a44872012-11-21 08:58:42 +000063int
64executeRetrace(const std::vector<const char *> & opts,
65 const char *traceName,
66 trace::API api) {
José Fonsecadf8c8192012-11-21 01:18:49 +000067 const char *retraceName;
68 switch (api) {
69 case trace::API_GL:
70 retraceName = "glretrace";
71 break;
72 case trace::API_EGL:
73 retraceName = "eglretrace";
74 break;
75 case trace::API_DX:
76 case trace::API_D3D7:
77 case trace::API_D3D8:
78 case trace::API_D3D9:
José Fonsecae51e22f2012-12-07 07:48:10 +000079 case trace::API_DXGI:
80 // Use prefix so that it can be used with WINE
José Fonsecadf8c8192012-11-21 01:18:49 +000081 retraceName = "d3dretrace.exe";
82 break;
83 default:
84 std::cerr << "warning: could not guess trace's API\n";
85 retraceName = "glretrace";
86 break;
87 }
Carl Worthda96cbf2012-08-20 09:45:27 -070088
José Fonsecad7a44872012-11-21 08:58:42 +000089 std::vector<const char *> command;
José Fonseca8e278042012-12-07 07:00:46 +000090 os::String retracePath = findProgram(retraceName);
José Fonsecad7a44872012-11-21 08:58:42 +000091 if (retracePath.length()) {
José Fonsecadf8c8192012-11-21 01:18:49 +000092 command.push_back(retracePath);
93 } else {
94 command.push_back(retraceName);
95 }
Carl Worthda96cbf2012-08-20 09:45:27 -070096
José Fonsecad7a44872012-11-21 08:58:42 +000097 command.insert(command.end(), opts.begin(), opts.end());
Carl Worthda96cbf2012-08-20 09:45:27 -070098
José Fonsecaec827772012-11-21 09:13:32 +000099 if (traceName) {
100 command.push_back(traceName);
101 }
Carl Worthda96cbf2012-08-20 09:45:27 -0700102 command.push_back(NULL);
103
104 return os::execute((char * const *)&command[0]);
105}
106
José Fonsecad7a44872012-11-21 08:58:42 +0000107int
108executeRetrace(const std::vector<const char *> & opts,
109 const char *traceName) {
110 trace::API api = guessApi(traceName);
111 return executeRetrace(opts, traceName, api);
112}
113
José Fonsecaec827772012-11-21 09:13:32 +0000114
115static const char *synopsis = "Replay a trace.";
116
117static void
118usage(void)
119{
120 std::vector<const char *>opts;
121 opts.push_back("--help");
122 trace::API api = trace::API_GL;
123 executeRetrace(opts, NULL, api);
124}
125
José Fonsecad7a44872012-11-21 08:58:42 +0000126static int
127command(int argc, char *argv[])
128{
129 std::vector<const char *> opts;
José Fonsecaec827772012-11-21 09:13:32 +0000130 for (int i = 1; i < argc; ++i) {
131 opts.push_back(argv[i]);
132 }
José Fonsecad7a44872012-11-21 08:58:42 +0000133
José Fonsecaec827772012-11-21 09:13:32 +0000134 trace::API api = trace::API_GL;
135 if (argc >= 1) {
136 const char *lastArg = argv[argc -1];
137 if (lastArg[0] != '-') {
138 api = guessApi(lastArg);
José Fonsecad7a44872012-11-21 08:58:42 +0000139 }
140 }
141
José Fonsecaec827772012-11-21 09:13:32 +0000142 return executeRetrace(opts, NULL, api);
José Fonsecad7a44872012-11-21 08:58:42 +0000143}
144
José Fonseca3320d022012-11-18 15:45:27 +0000145const Command retrace_command = {
146 "retrace",
Carl Worthda96cbf2012-08-20 09:45:27 -0700147 synopsis,
148 usage,
149 command
150};