blob: b1e6b485ba991fd895216ce6862b3e727dceb2be [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
Carl Worth33b92632012-08-14 14:11:19 -070027#include <sstream>
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"
Carl Worth86464432012-08-11 11:46:54 -070049 " -h, --help Show detailed help for trim options and exit\n"
50 " --calls=CALLSET Include specified calls in the trimmed output.\n"
Carl Worth46abfd12012-08-14 22:32:29 -070051 " --frames=FRAMESET Include specified frames in the trimmed output.\n"
Carl Worth86464432012-08-11 11:46:54 -070052 " --thread=THREAD_ID Only retain calls from specified thread\n"
53 " -o, --output=TRACE_FILE Output trace file\n"
54 ;
55}
56
José Fonsecab682b672012-02-15 07:13:31 +000057enum {
Imre Deak6f07a842012-05-08 15:20:43 +030058 CALLS_OPT = CHAR_MAX + 1,
Carl Worth46abfd12012-08-14 22:32:29 -070059 FRAMES_OPT,
Jose Fonsecad9809aa2015-08-06 12:19:25 +010060 THREAD_OPT
José Fonsecab682b672012-02-15 07:13:31 +000061};
62
63const static char *
Carl Worth54300eb2013-01-28 23:37:17 +110064shortOptions = "aho:x";
José Fonsecab682b672012-02-15 07:13:31 +000065
66const static struct option
67longOptions[] = {
68 {"help", no_argument, 0, 'h'},
69 {"calls", required_argument, 0, CALLS_OPT},
Carl Worth46abfd12012-08-14 22:32:29 -070070 {"frames", required_argument, 0, FRAMES_OPT},
Imre Deak6f07a842012-05-08 15:20:43 +030071 {"thread", required_argument, 0, THREAD_OPT},
72 {"output", required_argument, 0, 'o'},
José Fonsecab682b672012-02-15 07:13:31 +000073 {0, 0, 0, 0}
74};
75
Carl Worth163b22c2012-08-10 10:15:30 -070076struct stringCompare {
77 bool operator() (const char *a, const char *b) const {
78 return strcmp(a, b) < 0;
79 }
80};
81
Carl Worth163b22c2012-08-10 10:15:30 -070082struct trim_options {
83 /* Calls to be included in trace. */
84 trace::CallSet calls;
85
Carl Worth46abfd12012-08-14 22:32:29 -070086 /* Frames to be included in trace. */
87 trace::CallSet frames;
88
Carl Worth163b22c2012-08-10 10:15:30 -070089 /* Output filename */
90 std::string output;
91
92 /* Emit only calls from this thread (-1 == all threads) */
93 int thread;
94};
95
96static int
97trim_trace(const char *filename, struct trim_options *options)
98{
Carl Worth163b22c2012-08-10 10:15:30 -070099 trace::Parser p;
Carl Worth46abfd12012-08-14 22:32:29 -0700100 unsigned frame;
Carl Worth163b22c2012-08-10 10:15:30 -0700101
102 if (!p.open(filename)) {
103 std::cerr << "error: failed to open " << filename << "\n";
104 return 1;
105 }
106
Jose Fonsecad9809aa2015-08-06 12:19:25 +0100107 /* Prepare output file and writer for output. */
108 if (options->output.empty()) {
109 os::String base(filename);
110 base.trimExtension();
Carl Worth163b22c2012-08-10 10:15:30 -0700111
Jose Fonsecad9809aa2015-08-06 12:19:25 +0100112 options->output = std::string(base.str()) + std::string("-trim.trace");
113 }
114
115 trace::Writer writer;
116 if (!writer.open(options->output.c_str())) {
117 std::cerr << "error: failed to create " << options->output << "\n";
118 return 1;
119 }
120
121
Carl Worth46abfd12012-08-14 22:32:29 -0700122 frame = 0;
Carl Worth163b22c2012-08-10 10:15:30 -0700123 trace::Call *call;
124 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700125
Carl Worth18198bc2013-03-11 15:49:17 -0700126 /* There's no use doing any work past the last call and frame
Carl Worth46abfd12012-08-14 22:32:29 -0700127 * requested by the user. */
Carl Worth18198bc2013-03-11 15:49:17 -0700128 if ((options->calls.empty() || call->no > options->calls.getLast()) &&
129 (options->frames.empty() || frame > options->frames.getLast())) {
130
Carl Worth42249012012-08-14 10:26:11 -0700131 delete call;
Carl Worth5b827e12012-08-12 20:41:50 -0700132 break;
Carl Worth42249012012-08-14 10:26:11 -0700133 }
Carl Worth5b827e12012-08-12 20:41:50 -0700134
Carl Worth163b22c2012-08-10 10:15:30 -0700135 /* If requested, ignore all calls not belonging to the specified thread. */
Carl Worth42249012012-08-14 10:26:11 -0700136 if (options->thread != -1 && call->thread_id != options->thread) {
Carl Worth46abfd12012-08-14 22:32:29 -0700137 goto NEXT;
Carl Worth42249012012-08-14 10:26:11 -0700138 }
Carl Worth163b22c2012-08-10 10:15:30 -0700139
Carl Worthcc6e51c2012-08-13 14:35:43 -0700140 /* If this call is included in the user-specified call set,
141 * then require it (and all dependencies) in the trimmed
142 * output. */
Carl Worth46abfd12012-08-14 22:32:29 -0700143 if (options->calls.contains(*call) ||
144 options->frames.contains(frame, call->flags)) {
145
Jose Fonsecad9809aa2015-08-06 12:19:25 +0100146 writer.writeCall(call);
Carl Worth163b22c2012-08-10 10:15:30 -0700147 }
Carl Worth42249012012-08-14 10:26:11 -0700148
Carl Worth46abfd12012-08-14 22:32:29 -0700149 NEXT:
Carl Worth46abfd12012-08-14 22:32:29 -0700150 if (call->flags & trace::CALL_FLAG_END_FRAME) {
151 frame++;
152 }
153
Carl Worth163b22c2012-08-10 10:15:30 -0700154 delete call;
155 }
156
Carl Worth1a800a12012-08-17 14:23:13 -0700157 std::cerr << "Trimmed trace is available as " << options->output << "\n";
Carl Worth163b22c2012-08-10 10:15:30 -0700158
159 return 0;
160}
161
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800162static int
163command(int argc, char *argv[])
164{
Carl Worth163b22c2012-08-10 10:15:30 -0700165 struct trim_options options;
166
Carl Worth46abfd12012-08-14 22:32:29 -0700167 options.calls = trace::CallSet(trace::FREQUENCY_NONE);
168 options.frames = trace::CallSet(trace::FREQUENCY_NONE);
Carl Worth163b22c2012-08-10 10:15:30 -0700169 options.output = "";
170 options.thread = -1;
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:
Carl Worth163b22c2012-08-10 10:15:30 -0700185 options.thread = 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};