blob: 2a5e241899448b2be7f9a5c3ca5a0925a91ff3e0 [file] [log] [blame]
José Fonseca084fe922013-07-09 16:35:14 +01001/**************************************************************************
2 *
3 * Copyright 2013 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25
26
27#include <limits.h> // for CHAR_MAX
28#include <getopt.h>
29
Jose Fonsecae1dd9152016-03-23 11:01:39 +000030#include <string>
31
José Fonseca084fe922013-07-09 16:35:14 +010032#include "cli.hpp"
33
34#include "os_string.hpp"
35
36#include "trace_parser.hpp"
37#include "trace_writer.hpp"
38
39
40static const char *synopsis = "Stream editing of a trace.";
41
42
43static void
44usage(void)
45{
46 std::cout
47 << "usage: apitrace sed [OPTIONS] TRACE_FILE...\n"
48 << synopsis << "\n"
49 "\n"
50 " -h, --help Show detailed help for sed options and exit\n"
Arthur Huillet5ee86812015-06-08 16:04:45 +020051 " -e s/SEARCH/REPLACE/ Search and replace a symbol. Use @file(<path>)\n"
52 " to read SEARCH or REPLACE from a file.\n"
53 " Any character (not just /) can be used as \n"
54 " separator.\n"
55 " XXX: Only works for enums and strings.\n"
José Fonseca084fe922013-07-09 16:35:14 +010056 " -o, --output=TRACE_FILE Output trace file\n"
Jose Fonsecade8165d2017-08-03 18:54:28 +010057 " --property=NAME=VALUE Set a property\n"
José Fonseca084fe922013-07-09 16:35:14 +010058 ;
59}
60
61
Jose Fonsecade8165d2017-08-03 18:54:28 +010062enum {
63 PROPERTY_OPT = CHAR_MAX + 1,
64};
65
José Fonseca084fe922013-07-09 16:35:14 +010066const static char *
67shortOptions = "ho:e:";
68
69const static struct option
70longOptions[] = {
71 {"help", no_argument, 0, 'h'},
72 {"output", required_argument, 0, 'o'},
Jose Fonsecade8165d2017-08-03 18:54:28 +010073 {"property", required_argument, 0, PROPERTY_OPT},
José Fonseca084fe922013-07-09 16:35:14 +010074 {0, 0, 0, 0}
75};
76
77using namespace trace;
78
79
80/**
81 * A visitor that replaces symbol constants.
82 */
83class Replacer : public Visitor
84{
85protected:
Jose Fonseca62b09f52017-09-05 19:05:53 +010086 std::string searchString;
87 std::string replaceString;
88
89 bool isPointer = false;
90 unsigned long long searchPointer = 0;
91 unsigned long long replacePointer = 0;
José Fonseca084fe922013-07-09 16:35:14 +010092
93public:
Jose Fonseca62b09f52017-09-05 19:05:53 +010094 Replacer(const std::string & _searchString, const std::string & _replaceString) :
95 searchString(_searchString),
96 replaceString(_replaceString)
José Fonseca084fe922013-07-09 16:35:14 +010097 {
Jose Fonseca62b09f52017-09-05 19:05:53 +010098 if (searchString.length() > 2 &&
99 searchString[0] == '0' &&
100 searchString[1] == 'x') {
101 isPointer = true;
102 searchPointer = std::stoull(searchString, 0, 16);
103 assert(searchPointer);
104 replacePointer = std::stoull(replaceString, 0, 16);
105 assert(replacePointer);
106 }
José Fonseca084fe922013-07-09 16:35:14 +0100107 }
108
109 ~Replacer() {
110 }
111
Jose Fonseca6ca93402016-03-05 14:44:48 +0000112 void visit(Null *) override {
José Fonseca084fe922013-07-09 16:35:14 +0100113 }
114
Jose Fonseca6ca93402016-03-05 14:44:48 +0000115 void visit(Bool *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100116 }
117
Jose Fonseca6ca93402016-03-05 14:44:48 +0000118 void visit(SInt *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100119 }
120
Jose Fonseca6ca93402016-03-05 14:44:48 +0000121 void visit(UInt *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100122 }
123
Jose Fonseca6ca93402016-03-05 14:44:48 +0000124 void visit(Float *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100125 }
126
Jose Fonseca6ca93402016-03-05 14:44:48 +0000127 void visit(Double *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100128 }
129
Jose Fonseca6ca93402016-03-05 14:44:48 +0000130 void visit(String *node) override {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100131 if (!searchString.compare(node->value)) {
132 size_t len = replaceString.length() + 1;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200133 delete [] node->value;
Jose Fonseca5642f652015-06-24 11:33:26 +0100134 char *str = new char [len];
Jose Fonseca62b09f52017-09-05 19:05:53 +0100135 memcpy(str, replaceString.c_str(), len);
Arthur Huillet5ee86812015-06-08 16:04:45 +0200136 node->value = str;
137 }
José Fonseca084fe922013-07-09 16:35:14 +0100138 }
139
Jose Fonseca6ca93402016-03-05 14:44:48 +0000140 void visit(WString *node) override {
José Fonsecad5cda7c2014-09-25 15:19:09 +0100141 }
142
Jose Fonseca6ca93402016-03-05 14:44:48 +0000143 void visit(Enum *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100144 const EnumValue *it = node->lookup();
145 if (it) {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100146 if (searchString.compare(it->name) == 0) {
José Fonseca084fe922013-07-09 16:35:14 +0100147 const EnumSig *sig = node->sig;
148 for (unsigned i = 0; i < sig->num_values; ++i) {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100149 if (replaceString.compare(sig->values[i].name) == 0) {
José Fonseca084fe922013-07-09 16:35:14 +0100150 node->value = sig->values[i].value;
151 break;
152 }
153 }
154 }
155 }
156 }
157
Jose Fonseca6ca93402016-03-05 14:44:48 +0000158 void visit(Bitmask *bitmask) override {
José Fonseca084fe922013-07-09 16:35:14 +0100159 }
160
Jose Fonseca6ca93402016-03-05 14:44:48 +0000161 void visit(Struct *s) override {
162 for (auto memberValue : s->members) {
José Fonseca084fe922013-07-09 16:35:14 +0100163 _visit(memberValue);
164 }
165 }
166
Jose Fonseca6ca93402016-03-05 14:44:48 +0000167 void visit(Array *array) override {
168 for (auto & value : array->values) {
169 _visit(value);
José Fonseca084fe922013-07-09 16:35:14 +0100170 }
171 }
172
Jose Fonseca6ca93402016-03-05 14:44:48 +0000173 void visit(Blob *blob) override {
José Fonseca084fe922013-07-09 16:35:14 +0100174 }
175
Jose Fonseca6ca93402016-03-05 14:44:48 +0000176 void visit(Pointer *p) override {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100177 if (isPointer &&
178 p->value == searchPointer) {
179 p->value = replacePointer;
180 }
José Fonseca084fe922013-07-09 16:35:14 +0100181 }
182
Jose Fonseca6ca93402016-03-05 14:44:48 +0000183 void visit(Repr *r) override {
José Fonseca084fe922013-07-09 16:35:14 +0100184 _visit(r->humanValue);
185 }
186
187 void visit(Call *call) {
Jose Fonseca6ca93402016-03-05 14:44:48 +0000188 for (auto & arg : call->args) {
189 if (arg.value) {
190 _visit(arg.value);
José Fonseca084fe922013-07-09 16:35:14 +0100191 }
192 }
193
194 if (call->ret) {
195 _visit(call->ret);
196 }
197 }
198};
199
200
201typedef std::list<Replacer> Replacements;
202
203
204static int
Jose Fonsecade8165d2017-08-03 18:54:28 +0100205sed_trace(Replacements &replacements,
206 const trace::Properties &extraProperties,
207 const char *inFileName,
208 std::string &outFileName)
José Fonseca084fe922013-07-09 16:35:14 +0100209{
210 trace::Parser p;
211
212 if (!p.open(inFileName)) {
213 std::cerr << "error: failed to open " << inFileName << "\n";
214 return 1;
215 }
216
217 /* Prepare outFileName file and writer for outFileName. */
218 if (outFileName.empty()) {
219 os::String base(inFileName);
220 base.trimExtension();
221
222 outFileName = std::string(base.str()) + std::string("-sed.trace");
223 }
224
Jose Fonsecade8165d2017-08-03 18:54:28 +0100225 trace::Properties properties(p.getProperties());
226 for (auto & kv : extraProperties) {
227 properties[kv.first] = kv.second;
228 }
229
José Fonseca084fe922013-07-09 16:35:14 +0100230 trace::Writer writer;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100231 if (!writer.open(outFileName.c_str(), p.getVersion(), properties)) {
José Fonseca084fe922013-07-09 16:35:14 +0100232 std::cerr << "error: failed to create " << outFileName << "\n";
233 return 1;
234 }
235
236 trace::Call *call;
237 while ((call = p.parse_call())) {
238
Jose Fonseca6ca93402016-03-05 14:44:48 +0000239 for (auto & replacement : replacements) {
240 replacement.visit(call);
José Fonseca084fe922013-07-09 16:35:14 +0100241 }
242
243 writer.writeCall(call);
244
245 delete call;
246 }
247
José Fonseca8cf63072013-07-25 20:18:58 +0100248 std::cerr << "Edited trace is available as " << outFileName << "\n";
José Fonseca084fe922013-07-09 16:35:14 +0100249
250 return 0;
251}
252
253
254/**
255 * Parse a string in the format "s/SEARCH/REPLACE/".
256 */
257static bool
258parseSubstOpt(Replacements &replacements, const char *opt)
259{
Arthur Huillet5ee86812015-06-08 16:04:45 +0200260 char separator;
261
José Fonseca084fe922013-07-09 16:35:14 +0100262 if (*opt++ != 's') {
263 return false;
264 }
265
Arthur Huillet5ee86812015-06-08 16:04:45 +0200266 separator = *opt++;
José Fonseca084fe922013-07-09 16:35:14 +0100267
268 // Parse the search pattern
269 const char *search_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200270 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100271 if (*opt == 0) {
272 return false;
273 }
274 ++opt;
275 }
276 const char *search_end = opt++;
277
278 // Parse the replace pattern
279 const char *replace_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200280 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100281 if (*opt == 0) {
282 return false;
283 }
284 ++opt;
285 }
286 const char *replace_end = opt++;
287
288 if (*opt != 0) {
289 return false;
290 }
291
292 std::string search(search_begin, search_end);
293 std::string replace(replace_begin, replace_end);
294
Arthur Huillet5ee86812015-06-08 16:04:45 +0200295 // If search or replace strings are taken from a file, read the file
296 std::string file_subst = "@file(";
297
298 for (int i = 0; i < 2; i++) {
299 std::string *str = i ? &search : &replace;
300
301 if (!str->compare(0, file_subst.length(), file_subst)) {
302 if ((*str)[str->length()-1] != ')') {
303 return false;
304 }
305
306 std::string fname = str->substr(file_subst.length());
307 fname[fname.length()-1] = 0;
Jose Fonseca8b0807f2015-06-24 11:29:01 +0100308 FILE *f = fopen(fname.c_str(), "rt");
Arthur Huillet5ee86812015-06-08 16:04:45 +0200309 if (!f) {
310 std::cerr << "error: cannot open file " << fname << "\n";
311 return false;
312 }
313 char buf[1024];
314 (*str) = "";
315 while (!feof(f)) {
316 if (fgets(buf, 1024, f)) {
317 str->append(buf);
318 }
319 }
320 fclose(f);
321 }
322 }
323
324
José Fonseca084fe922013-07-09 16:35:14 +0100325 replacements.push_back(Replacer(search, replace));
326
327 return true;
328}
329
330
331static int
332command(int argc, char *argv[])
333{
334 Replacements replacements;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100335 trace::Properties extraProperties;
José Fonseca084fe922013-07-09 16:35:14 +0100336 std::string outFileName;
337
338 int opt;
339 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
340 switch (opt) {
341 case 'h':
342 usage();
343 return 0;
344 case 'o':
345 outFileName = optarg;
346 break;
347 case 'e':
348 if (!parseSubstOpt(replacements, optarg)) {
Arthur Huillet5ee86812015-06-08 16:04:45 +0200349 std::cerr << "error: invalid replacement pattern `" << optarg << "`\n";
Jose Fonseca4c922f82017-08-03 18:56:38 +0100350 return 1;
José Fonseca084fe922013-07-09 16:35:14 +0100351 }
Jose Fonseca4c922f82017-08-03 18:56:38 +0100352 break;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100353 case PROPERTY_OPT:
354 {
355 const char *sep = strchr(optarg, '=');
356 if (sep == nullptr) {
357 std::cerr << "error: bad property `" << optarg << "`\n";
358 return 1;
359 }
360 std::string name(static_cast<const char *>(optarg), sep);
361 std::string value(sep + 1);
362 extraProperties[name] = value;
363 break;
364 }
José Fonseca084fe922013-07-09 16:35:14 +0100365 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700366 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonseca084fe922013-07-09 16:35:14 +0100367 usage();
368 return 1;
369 }
370 }
371
372 if (optind >= argc) {
373 std::cerr << "error: apitrace sed requires a trace file as an argument.\n";
374 usage();
375 return 1;
376 }
377
378 if (argc > optind + 1) {
379 std::cerr << "error: extraneous arguments:";
380 for (int i = optind + 1; i < argc; i++) {
381 std::cerr << " " << argv[i];
382 }
383 std::cerr << "\n";
384 usage();
385 return 1;
386 }
387
Jose Fonsecade8165d2017-08-03 18:54:28 +0100388 return sed_trace(replacements, extraProperties, argv[optind], outFileName);
José Fonseca084fe922013-07-09 16:35:14 +0100389}
390
391
392const Command sed_command = {
393 "sed",
394 synopsis,
395 usage,
396 command
397};