blob: 206e24e687cc1b8cd60cae8e02f23079badfeb49 [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
José Fonsecab682b672012-02-15 07:13:31 +000027
Carl Worth4c5f6fa2011-11-14 14:50:07 -080028#include <string.h>
José Fonsecab682b672012-02-15 07:13:31 +000029#include <limits.h> // for CHAR_MAX
30#include <getopt.h>
Carl Worth4c5f6fa2011-11-14 14:50:07 -080031
32#include "cli.hpp"
33
34#include "os_string.hpp"
35
José Fonsecad3c00132012-01-27 22:43:53 +000036#include "trace_callset.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080037#include "trace_parser.hpp"
José Fonseca630471a2012-01-27 22:06:51 +000038#include "trace_writer.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080039
40static const char *synopsis = "Create a new trace by trimming an existing trace.";
41
42static void
43usage(void)
44{
45 std::cout
José Fonsecab682b672012-02-15 07:13:31 +000046 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
José Fonsecad3c00132012-01-27 22:43:53 +000047 << synopsis << "\n"
48 "\n"
José Fonseca2e93bcb2012-02-18 15:33:12 +000049 " -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"
José Fonsecad3c00132012-01-27 22:43:53 +000052 "\n"
53 ;
Carl Worth4c5f6fa2011-11-14 14:50:07 -080054}
55
José Fonsecab682b672012-02-15 07:13:31 +000056enum {
57 CALLS_OPT = CHAR_MAX + 1,
58};
59
60const static char *
Kenneth Graunke5245ded2012-04-05 10:51:26 -070061shortOptions = "ho:";
José Fonsecab682b672012-02-15 07:13:31 +000062
63const static struct option
64longOptions[] = {
65 {"help", no_argument, 0, 'h'},
66 {"calls", required_argument, 0, CALLS_OPT},
José Fonsecab682b672012-02-15 07:13:31 +000067 {"output", optional_argument, 0, 'o'},
68 {0, 0, 0, 0}
69};
70
Carl Worth4c5f6fa2011-11-14 14:50:07 -080071static int
72command(int argc, char *argv[])
73{
José Fonsecad3c00132012-01-27 22:43:53 +000074 std::string output;
75 trace::CallSet calls(trace::FREQUENCY_ALL);
Carl Worth4c5f6fa2011-11-14 14:50:07 -080076 int i;
77
José Fonsecab682b672012-02-15 07:13:31 +000078 int opt;
79 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
80 switch (opt) {
81 case 'h':
Carl Worth4c5f6fa2011-11-14 14:50:07 -080082 usage();
83 return 0;
José Fonsecab682b672012-02-15 07:13:31 +000084 case CALLS_OPT:
85 calls = trace::CallSet(optarg);
86 break;
87 case 'o':
88 output = optarg;
89 break;
90 default:
91 std::cerr << "error: unexpected option `" << opt << "`\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -080092 usage();
93 return 1;
94 }
95 }
96
José Fonsecab682b672012-02-15 07:13:31 +000097 if (optind >= argc) {
98 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -080099 usage();
100 return 1;
101 }
102
José Fonsecab682b672012-02-15 07:13:31 +0000103 for (i = optind; i < argc; ++i) {
José Fonsecad3c00132012-01-27 22:43:53 +0000104 trace::Parser p;
105 if (!p.open(argv[i])) {
106 std::cerr << "error: failed to open " << argv[i] << "\n";
107 return 1;
108 }
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800109
José Fonsecad3c00132012-01-27 22:43:53 +0000110 if (output.empty()) {
111 os::String base(argv[i]);
112 base.trimExtension();
113
114 output = std::string(base.str()) + std::string("-trim.trace");
115 }
116
117 trace::Writer writer;
118 if (!writer.open(output.c_str())) {
119 std::cerr << "error: failed to create " << argv[i] << "\n";
120 return 1;
121 }
122
123 trace::Call *call;
124 while ((call = p.parse_call())) {
125 if (calls.contains(*call)) {
126 writer.writeCall(call);
127 }
128 delete call;
129 }
130
131 std::cout << "Trimmed trace is available as " << output << "\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800132 }
133
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800134 return 0;
135}
136
137const Command trim_command = {
138 "trim",
139 synopsis,
140 usage,
141 command
142};