1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
4 * Copyright 2011 Intel corporation
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 **************************************************************************/
29 #include <limits.h> // for CHAR_MAX
34 #include "os_string.hpp"
36 #include "trace_callset.hpp"
37 #include "trace_parser.hpp"
38 #include "trace_writer.hpp"
40 static const char *synopsis = "Create a new trace by trimming an existing trace.";
46 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
49 " -h, --help show this help message and exit\n"
50 " --calls=CALLSET only retain specified calls\n"
51 " --thread=THREAD_ID only retain calls from specified thread\n"
52 " -o, --output=TRACE_FILE output trace file\n"
58 CALLS_OPT = CHAR_MAX + 1,
65 const static struct option
67 {"help", no_argument, 0, 'h'},
68 {"calls", required_argument, 0, CALLS_OPT},
69 {"thread", required_argument, 0, THREAD_OPT},
70 {"output", required_argument, 0, 'o'},
75 command(int argc, char *argv[])
78 trace::CallSet calls(trace::FREQUENCY_ALL);
83 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
89 calls = trace::CallSet(optarg);
92 thread = atoi(optarg);
98 std::cerr << "error: unexpected option `" << opt << "`\n";
104 if (optind >= argc) {
105 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
110 for (i = optind; i < argc; ++i) {
112 if (!p.open(argv[i])) {
116 if (output.empty()) {
117 os::String base(argv[i]);
118 base.trimExtension();
120 output = std::string(base.str()) + std::string("-trim.trace");
123 trace::Writer writer;
124 if (!writer.open(output.c_str())) {
125 std::cerr << "error: failed to create " << argv[i] << "\n";
130 while ((call = p.parse_call())) {
131 if (calls.contains(*call) &&
132 (thread == -1 || call->thread_id == thread)) {
133 writer.writeCall(call);
138 std::cout << "Trimmed trace is available as " << output << "\n";
144 const Command trim_command = {