blob: 8fdd93c3153f2ace0c21870a78da5a3c6daed012 [file] [log] [blame]
Carl Worth4c5f6fa2011-11-14 14:50:07 -08001/**************************************************************************
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
José Fonsecad3c00132012-01-27 22:43:53 +000033#include "trace_callset.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080034#include "trace_parser.hpp"
José Fonseca630471a2012-01-27 22:06:51 +000035#include "trace_writer.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080036
37static const char *synopsis = "Create a new trace by trimming an existing trace.";
38
39static void
40usage(void)
41{
42 std::cout
José Fonsecad3c00132012-01-27 22:43:53 +000043 << "usage: apitrace trim [OPTIONS] <trace-file>...\n"
44 << synopsis << "\n"
45 "\n"
46 " --calls <CALLSET> Only trim specified calls\n"
47 " -o --output <TRACEFILE> Output trace file\n"
48 "\n"
49 ;
Carl Worth4c5f6fa2011-11-14 14:50:07 -080050}
51
52static int
53command(int argc, char *argv[])
54{
José Fonsecad3c00132012-01-27 22:43:53 +000055 std::string output;
56 trace::CallSet calls(trace::FREQUENCY_ALL);
Carl Worth4c5f6fa2011-11-14 14:50:07 -080057 int i;
58
José Fonsecad3c00132012-01-27 22:43:53 +000059 for (i = 0; i < argc;) {
Carl Worth4c5f6fa2011-11-14 14:50:07 -080060 const char *arg = argv[i];
61
62 if (arg[0] != '-') {
63 break;
64 }
65
José Fonsecad3c00132012-01-27 22:43:53 +000066 ++i;
67
Carl Worth4c5f6fa2011-11-14 14:50:07 -080068 if (!strcmp(arg, "--")) {
69 break;
70 } else if (!strcmp(arg, "--help")) {
71 usage();
72 return 0;
José Fonsecad3c00132012-01-27 22:43:53 +000073 } else if (!strcmp(arg, "--calls")) {
74 calls = trace::CallSet(argv[i++]);
75 } else if (!strcmp(arg, "-o") ||
76 !strcmp(arg, "--output")) {
77 output = argv[i++];
Carl Worth4c5f6fa2011-11-14 14:50:07 -080078 } else {
79 std::cerr << "error: unknown option " << arg << "\n";
80 usage();
81 return 1;
82 }
83 }
84
85 if (i >= argc) {
86 std::cerr << "Error: apitrace trim requires a trace file as an argument.\n";
87 usage();
88 return 1;
89 }
90
José Fonsecad3c00132012-01-27 22:43:53 +000091 for ( ; i < argc; ++i) {
92 trace::Parser p;
93 if (!p.open(argv[i])) {
94 std::cerr << "error: failed to open " << argv[i] << "\n";
95 return 1;
96 }
Carl Worth4c5f6fa2011-11-14 14:50:07 -080097
José Fonsecad3c00132012-01-27 22:43:53 +000098 if (output.empty()) {
99 os::String base(argv[i]);
100 base.trimExtension();
101
102 output = std::string(base.str()) + std::string("-trim.trace");
103 }
104
105 trace::Writer writer;
106 if (!writer.open(output.c_str())) {
107 std::cerr << "error: failed to create " << argv[i] << "\n";
108 return 1;
109 }
110
111 trace::Call *call;
112 while ((call = p.parse_call())) {
113 if (calls.contains(*call)) {
114 writer.writeCall(call);
115 }
116 delete call;
117 }
118
119 std::cout << "Trimmed trace is available as " << output << "\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800120 }
121
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800122 return 0;
123}
124
125const Command trim_command = {
126 "trim",
127 synopsis,
128 usage,
129 command
130};