blob: 04222622c31e259d61dbaba9f628f969b63bd868 [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
33#include "trace_parser.hpp"
José Fonseca630471a2012-01-27 22:06:51 +000034#include "trace_writer.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080035
36static const char *synopsis = "Create a new trace by trimming an existing trace.";
37
38static void
39usage(void)
40{
41 std::cout
42 << "usage: apitrace trim <trace-file>\n"
43 << synopsis << "\n";
44}
45
46static int
47command(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
José Fonseca630471a2012-01-27 22:06:51 +000088 trace::Writer writer;
89 writer.open(output.c_str());
Carl Worth4c5f6fa2011-11-14 14:50:07 -080090
91 trace::Call *call;
92 while ((call = p.parse_call())) {
José Fonseca630471a2012-01-27 22:06:51 +000093 writer.writeCall(call);
Carl Worth4c5f6fa2011-11-14 14:50:07 -080094 delete call;
95 }
96
97 std::cout << "Trimmed trace is available as " << output << "\n";
98
99 return 0;
100}
101
102const Command trim_command = {
103 "trim",
104 synopsis,
105 usage,
106 command
107};