blob: f559d27cdb2a6f3a9f0bb38cecb13de2030265a6 [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
Jose Fonsecaac6776f2019-09-04 14:48:20 +010027#include <set>
Carl Worth33b92632012-08-14 14:11:19 -070028#include <sstream>
Carl Worth4c5f6fa2011-11-14 14:50:07 -080029#include <string.h>
José Fonsecab682b672012-02-15 07:13:31 +000030#include <limits.h> // for CHAR_MAX
31#include <getopt.h>
Carl Worth4c5f6fa2011-11-14 14:50:07 -080032
33#include "cli.hpp"
34
35#include "os_string.hpp"
36
José Fonsecad3c00132012-01-27 22:43:53 +000037#include "trace_callset.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080038#include "trace_parser.hpp"
José Fonseca630471a2012-01-27 22:06:51 +000039#include "trace_writer.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080040
41static const char *synopsis = "Create a new trace by trimming an existing trace.";
42
43static void
44usage(void)
45{
46 std::cout
José Fonsecab682b672012-02-15 07:13:31 +000047 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
José Fonsecad3c00132012-01-27 22:43:53 +000048 << synopsis << "\n"
49 "\n"
Carl Worth86464432012-08-11 11:46:54 -070050 " -h, --help Show detailed help for trim options and exit\n"
51 " --calls=CALLSET Include specified calls in the trimmed output.\n"
Carl Worth46abfd12012-08-14 22:32:29 -070052 " --frames=FRAMESET Include specified frames in the trimmed output.\n"
Jose Fonsecaac6776f2019-09-04 14:48:20 +010053 " --thread=THREAD_ID Only retain calls from specified thread (can be passed multiple times.)\n"
Carl Worth86464432012-08-11 11:46:54 -070054 " -o, --output=TRACE_FILE Output trace file\n"
55 ;
56}
57
José Fonsecab682b672012-02-15 07:13:31 +000058enum {
Imre Deak6f07a842012-05-08 15:20:43 +030059 CALLS_OPT = CHAR_MAX + 1,
Carl Worth46abfd12012-08-14 22:32:29 -070060 FRAMES_OPT,
Jose Fonsecad9809aa2015-08-06 12:19:25 +010061 THREAD_OPT
José Fonsecab682b672012-02-15 07:13:31 +000062};
63
64const static char *
Carl Worth54300eb2013-01-28 23:37:17 +110065shortOptions = "aho:x";
José Fonsecab682b672012-02-15 07:13:31 +000066
67const static struct option
68longOptions[] = {
69 {"help", no_argument, 0, 'h'},
70 {"calls", required_argument, 0, CALLS_OPT},
Carl Worth46abfd12012-08-14 22:32:29 -070071 {"frames", required_argument, 0, FRAMES_OPT},
Imre Deak6f07a842012-05-08 15:20:43 +030072 {"thread", required_argument, 0, THREAD_OPT},
73 {"output", required_argument, 0, 'o'},
José Fonsecab682b672012-02-15 07:13:31 +000074 {0, 0, 0, 0}
75};
76
Carl Worth163b22c2012-08-10 10:15:30 -070077struct stringCompare {
78 bool operator() (const char *a, const char *b) const {
79 return strcmp(a, b) < 0;
80 }
81};
82
Carl Worth163b22c2012-08-10 10:15:30 -070083struct trim_options {
84 /* Calls to be included in trace. */
85 trace::CallSet calls;
86
Carl Worth46abfd12012-08-14 22:32:29 -070087 /* Frames to be included in trace. */
88 trace::CallSet frames;
89
Carl Worth163b22c2012-08-10 10:15:30 -070090 /* Output filename */
91 std::string output;
92
Jose Fonsecaac6776f2019-09-04 14:48:20 +010093 /* Emit only calls from this thread (empty == all threads) */
94 std::set<unsigned> threadIds;
Carl Worth163b22c2012-08-10 10:15:30 -070095};
96
97static int
98trim_trace(const char *filename, struct trim_options *options)
99{
Carl Worth163b22c2012-08-10 10:15:30 -0700100 trace::Parser p;
Carl Worth46abfd12012-08-14 22:32:29 -0700101 unsigned frame;
Carl Worth163b22c2012-08-10 10:15:30 -0700102
103 if (!p.open(filename)) {
104 std::cerr << "error: failed to open " << filename << "\n";
105 return 1;
106 }
107
Jose Fonsecad9809aa2015-08-06 12:19:25 +0100108 /* Prepare output file and writer for output. */
109 if (options->output.empty()) {
110 os::String base(filename);
111 base.trimExtension();
Carl Worth163b22c2012-08-10 10:15:30 -0700112
Jose Fonsecad9809aa2015-08-06 12:19:25 +0100113 options->output = std::string(base.str()) + std::string("-trim.trace");
114 }
115
116 trace::Writer writer;
Jose Fonseca7301ab12017-08-03 18:25:53 +0100117 if (!writer.open(options->output.c_str(), p.getVersion(), p.getProperties())) {
Jose Fonsecad9809aa2015-08-06 12:19:25 +0100118 std::cerr << "error: failed to create " << options->output << "\n";
119 return 1;
120 }
121
122
Carl Worth46abfd12012-08-14 22:32:29 -0700123 frame = 0;
Carl Worth163b22c2012-08-10 10:15:30 -0700124 trace::Call *call;
125 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700126
Carl Worth18198bc2013-03-11 15:49:17 -0700127 /* There's no use doing any work past the last call and frame
Carl Worth46abfd12012-08-14 22:32:29 -0700128 * requested by the user. */
Carl Worth18198bc2013-03-11 15:49:17 -0700129 if ((options->calls.empty() || call->no > options->calls.getLast()) &&
130 (options->frames.empty() || frame > options->frames.getLast())) {
131
Carl Worth42249012012-08-14 10:26:11 -0700132 delete call;
Carl Worth5b827e12012-08-12 20:41:50 -0700133 break;
Carl Worth42249012012-08-14 10:26:11 -0700134 }
Carl Worth5b827e12012-08-12 20:41:50 -0700135
Carl Worth163b22c2012-08-10 10:15:30 -0700136 /* If requested, ignore all calls not belonging to the specified thread. */
Jose Fonsecaac6776f2019-09-04 14:48:20 +0100137 if (!options->threadIds.empty() &&
138 options->threadIds.find(call->thread_id) == options->threadIds.end()) {
Carl Worth46abfd12012-08-14 22:32:29 -0700139 goto NEXT;
Carl Worth42249012012-08-14 10:26:11 -0700140 }
Carl Worth163b22c2012-08-10 10:15:30 -0700141
Carl Worthcc6e51c2012-08-13 14:35:43 -0700142 /* If this call is included in the user-specified call set,
143 * then require it (and all dependencies) in the trimmed
144 * output. */
Carl Worth46abfd12012-08-14 22:32:29 -0700145 if (options->calls.contains(*call) ||
146 options->frames.contains(frame, call->flags)) {
147
Jose Fonsecad9809aa2015-08-06 12:19:25 +0100148 writer.writeCall(call);
Carl Worth163b22c2012-08-10 10:15:30 -0700149 }
Carl Worth42249012012-08-14 10:26:11 -0700150
Carl Worth46abfd12012-08-14 22:32:29 -0700151 NEXT:
Carl Worth46abfd12012-08-14 22:32:29 -0700152 if (call->flags & trace::CALL_FLAG_END_FRAME) {
153 frame++;
154 }
155
Carl Worth163b22c2012-08-10 10:15:30 -0700156 delete call;
157 }
158
Carl Worth1a800a12012-08-17 14:23:13 -0700159 std::cerr << "Trimmed trace is available as " << options->output << "\n";
Carl Worth163b22c2012-08-10 10:15:30 -0700160
161 return 0;
162}
163
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800164static int
165command(int argc, char *argv[])
166{
Carl Worth163b22c2012-08-10 10:15:30 -0700167 struct trim_options options;
168
Carl Worth46abfd12012-08-14 22:32:29 -0700169 options.calls = trace::CallSet(trace::FREQUENCY_NONE);
170 options.frames = trace::CallSet(trace::FREQUENCY_NONE);
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800171
José Fonsecab682b672012-02-15 07:13:31 +0000172 int opt;
173 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
174 switch (opt) {
175 case 'h':
Jose Fonsecad9809aa2015-08-06 12:19:25 +0100176 usage();
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800177 return 0;
José Fonsecab682b672012-02-15 07:13:31 +0000178 case CALLS_OPT:
Lawrence L Love5ab982d2013-12-05 11:31:16 -0800179 options.calls.merge(optarg);
Carl Worth163b22c2012-08-10 10:15:30 -0700180 break;
Carl Worth46abfd12012-08-14 22:32:29 -0700181 case FRAMES_OPT:
Lawrence L Love5ab982d2013-12-05 11:31:16 -0800182 options.frames.merge(optarg);
Carl Worth46abfd12012-08-14 22:32:29 -0700183 break;
Imre Deak6f07a842012-05-08 15:20:43 +0300184 case THREAD_OPT:
Jose Fonsecaac6776f2019-09-04 14:48:20 +0100185 options.threadIds.insert(atoi(optarg));
Imre Deak6f07a842012-05-08 15:20:43 +0300186 break;
José Fonsecab682b672012-02-15 07:13:31 +0000187 case 'o':
Carl Worth163b22c2012-08-10 10:15:30 -0700188 options.output = optarg;
José Fonsecab682b672012-02-15 07:13:31 +0000189 break;
190 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700191 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800192 usage();
193 return 1;
194 }
195 }
196
Carl Worth46abfd12012-08-14 22:32:29 -0700197 /* If neither of --calls nor --frames was set, default to the
198 * entire set of calls. */
199 if (options.calls.empty() && options.frames.empty()) {
200 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
201 }
202
José Fonsecab682b672012-02-15 07:13:31 +0000203 if (optind >= argc) {
204 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800205 usage();
206 return 1;
207 }
208
Carl Worthf630d9d2012-09-04 16:48:00 -0700209 if (argc > optind + 1) {
210 std::cerr << "error: extraneous arguments:";
211 for (int i = optind + 1; i < argc; i++) {
212 std::cerr << " " << argv[i];
213 }
214 std::cerr << "\n";
215 usage();
216 return 1;
217 }
218
Carl Worth163b22c2012-08-10 10:15:30 -0700219 return trim_trace(argv[optind], &options);
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800220}
221
222const Command trim_command = {
223 "trim",
224 synopsis,
Jose Fonsecad9809aa2015-08-06 12:19:25 +0100225 usage,
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800226 command
227};