]> git.cworth.org Git - apitrace/blob - cli/cli_trim.cpp
Convert trim option parsing to getopt.
[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
28 #include <string.h>
29 #include <limits.h> // for CHAR_MAX
30 #include <getopt.h>
31
32 #include "cli.hpp"
33
34 #include "os_string.hpp"
35
36 #include "trace_callset.hpp"
37 #include "trace_parser.hpp"
38 #include "trace_writer.hpp"
39
40 static const char *synopsis = "Create a new trace by trimming an existing trace.";
41
42 static void
43 usage(void)
44 {
45     std::cout
46         << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
47         << synopsis << "\n"
48         "\n"
49         "    -h, --help             show this help message and exit\n"
50         "    --calls=CALLSET        only trim specified calls\n"
51         "    -o --output=TRACE_FILE output trace file\n"
52         "\n"
53     ;
54 }
55
56 enum {
57         CALLS_OPT = CHAR_MAX + 1,
58 };
59
60 const static char *
61 shortOptions = "ho";
62
63 const static struct option
64 longOptions[] = {
65     {"help", no_argument, 0, 'h'},
66     {"calls", required_argument, 0, CALLS_OPT},
67     {"calls", required_argument, 0, CALLS_OPT},
68     {"output", optional_argument, 0, 'o'},
69     {0, 0, 0, 0}
70 };
71
72 static int
73 command(int argc, char *argv[])
74 {
75     std::string output;
76     trace::CallSet calls(trace::FREQUENCY_ALL);
77     int i;
78
79     int opt;
80     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
81         switch (opt) {
82         case 'h':
83             usage();
84             return 0;
85         case CALLS_OPT:
86             calls = trace::CallSet(optarg);
87             break;
88         case 'o':
89             output = optarg;
90             break;
91         default:
92             std::cerr << "error: unexpected option `" << opt << "`\n";
93             usage();
94             return 1;
95         }
96     }
97
98     if (optind >= argc) {
99         std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
100         usage();
101         return 1;
102     }
103
104     for (i = optind; i < argc; ++i) {
105         trace::Parser p;
106         if (!p.open(argv[i])) {
107             std::cerr << "error: failed to open " << argv[i] << "\n";
108             return 1;
109         }
110
111         if (output.empty()) {
112             os::String base(argv[i]);
113             base.trimExtension();
114
115             output = std::string(base.str()) + std::string("-trim.trace");
116         }
117
118         trace::Writer writer;
119         if (!writer.open(output.c_str())) {
120             std::cerr << "error: failed to create " << argv[i] << "\n";
121             return 1;
122         }
123
124         trace::Call *call;
125         while ((call = p.parse_call())) {
126             if (calls.contains(*call)) {
127                 writer.writeCall(call);
128             }
129             delete call;
130         }
131
132         std::cout << "Trimmed trace is available as " << output << "\n";
133     }
134
135     return 0;
136 }
137
138 const Command trim_command = {
139     "trim",
140     synopsis,
141     usage,
142     command
143 };