]> git.cworth.org Git - apitrace/blob - cli/cli_trace.cpp
80775090c7b1be79cbce6dc9a8e9e6f6b8c521d2
[apitrace] / cli / cli_trace.cpp
1 /*********************************************************************
2  *
3  * Copyright 2011 Intel Corporation
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use, copy,
10  * modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  *
26  *********************************************************************/
27
28
29 #include <assert.h>
30 #include <string.h>
31 #include <getopt.h>
32
33 #include <iostream>
34
35 #include "cli.hpp"
36
37 #include "trace_tools.hpp"
38
39
40 static const char *synopsis = "Generate a new trace by executing the given program.";
41
42 static void
43 usage(void)
44 {
45     std::cout << "usage: apitrace trace [OPTIONS] PROGRAM [ARGS ...]\n"
46         << synopsis << "\n"
47         "\n"
48         "    The given program will be executed with the given arguments.\n"
49         "    During execution, all OpenGL calls will be captured to a trace\n"
50         "    file. That trace file can then be used\n"
51         "    with other apitrace utilities for replay or analysis.\n"
52         "\n"
53         "    -v, --verbose       verbose output\n"
54         "    -a, --api=API       specify API to trace ("
55 #ifdef _WIN32
56                                                       "gl, d3d7, d3d8, d3d9, or d3d10"
57 #else
58                                                       "gl or egl"
59 #endif
60                                                       ");\n"
61         "                        default is `gl`\n"
62         "    -o, --output=TRACE  specify output trace file;\n"
63         "                        default is `PROGRAM.trace`\n";
64 }
65
66 const static char *
67 shortOptions = "+hva:o:";
68
69 const static struct option
70 longOptions[] = {
71     {"help", no_argument, 0, 'h'},
72     {"verbose", no_argument, 0, 'v'},
73     {"api", required_argument, 0, 'a'},
74     {"output", required_argument, 0, 'o'},
75     {0, 0, 0, 0}
76 };
77
78 static int
79 command(int argc, char *argv[])
80 {
81     bool verbose = false;
82     trace::API api = trace::API_GL;
83     const char *output = NULL;
84
85     int opt;
86     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
87         switch (opt) {
88         case 'h':
89             usage();
90             return 0;
91         case 'v':
92             verbose = true;
93             break;
94         case 'a':
95             if (strcmp(optarg, "gl") == 0) {
96                 api = trace::API_GL;
97             } else if (strcmp(optarg, "egl") == 0) {
98                 api = trace::API_EGL;
99             } else if (strcmp(optarg, "d3d7") == 0) {
100                 api = trace::API_D3D7;
101             } else if (strcmp(optarg, "d3d8") == 0) {
102                 api = trace::API_D3D8;
103             } else if (strcmp(optarg, "d3d9") == 0) {
104                 api = trace::API_D3D9;
105             } else if (strcmp(optarg, "d3d10") == 0) {
106                 api = trace::API_D3D10;
107             } else if (strcmp(optarg, "d3d10_1") == 0) {
108                 api = trace::API_D3D10_1;
109             } else if (strcmp(optarg, "d3d11") == 0) {
110                 api = trace::API_D3D11;
111             } else {
112                 std::cerr << "error: unknown API `" << optarg << "`\n";
113                 usage();
114                 return 1;
115             }
116             break;
117         case 'o':
118             output = optarg;
119             break;
120         default:
121             std::cerr << "error: unexpected option `" << opt << "`\n";
122             usage();
123             return 1;
124         }
125     }
126
127     if (optind == argc) {
128         std::cerr << "error: no command specified\n";
129         usage();
130         return 1;
131     }
132
133     assert(argv[argc] == 0);
134     return trace::traceProgram(api, argv + optind, output, verbose);
135 }
136
137 const Command trace_command = {
138     "trace",
139     synopsis,
140     usage,
141     command
142 };