]> git.cworth.org Git - apitrace/blob - cli/cli_trim.cpp
b35e14429d48786f78c2f29242fe7ba9d8898257
[apitrace] / cli / cli_trim.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * Copyright 2011 Intel corporation
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies 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 included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 #include <string.h>
28
29 #include "cli.hpp"
30
31 #include "os_string.hpp"
32
33 #include "trace_parser.hpp"
34 #include "trace_copier.hpp"
35
36 static const char *synopsis = "Create a new trace by trimming an existing trace.";
37
38 static void
39 usage(void)
40 {
41     std::cout
42         << "usage: apitrace trim <trace-file>\n"
43         << synopsis << "\n";
44 }
45
46 static int
47 command(int argc, char *argv[])
48 {
49     int i;
50
51     for (i = 0; i < argc; ++i) {
52         const char *arg = argv[i];
53
54         if (arg[0] != '-') {
55             break;
56         }
57
58         if (!strcmp(arg, "--")) {
59             break;
60         } else if (!strcmp(arg, "--help")) {
61             usage();
62             return 0;
63         } else {
64             std::cerr << "error: unknown option " << arg << "\n";
65             usage();
66             return 1;
67         }
68     }
69
70     if (i >= argc) {
71         std::cerr << "Error: apitrace trim requires a trace file as an argument.\n";
72         usage();
73         return 1;
74     }
75
76     trace::Parser p;
77
78     if (!p.open(argv[i])) {
79         std::cerr << "error: failed to open " << argv[i] << "\n";
80         return 1;
81     }
82
83     os::String base(argv[i]);
84     base.trimExtension();
85
86     std::string output = std::string(base.str()) + std::string("-trim.trace");
87
88     trace::Copier copier(output.c_str());
89
90     trace::Call *call;
91     while ((call = p.parse_call())) {
92         copier.visit(call);
93         delete call;
94     }
95
96     std::cout << "Trimmed trace is available as " << output << "\n";
97
98     return 0;
99 }
100
101 const Command trim_command = {
102     "trim",
103     synopsis,
104     usage,
105     command
106 };