blob: 9bddaceaa1f1f0a47717d81f5db23bdb705af67d [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
Georg Lutz85792702018-04-13 20:39:53 +020036#include "trace_callset.hpp"
José Fonseca084fe922013-07-09 16:35:14 +010037#include "trace_parser.hpp"
38#include "trace_writer.hpp"
39
40
41static const char *synopsis = "Stream editing of a trace.";
42
43
44static void
45usage(void)
46{
47 std::cout
48 << "usage: apitrace sed [OPTIONS] TRACE_FILE...\n"
49 << synopsis << "\n"
50 "\n"
51 " -h, --help Show detailed help for sed options and exit\n"
Arthur Huillet5ee86812015-06-08 16:04:45 +020052 " -e s/SEARCH/REPLACE/ Search and replace a symbol. Use @file(<path>)\n"
53 " to read SEARCH or REPLACE from a file.\n"
54 " Any character (not just /) can be used as \n"
55 " separator.\n"
56 " XXX: Only works for enums and strings.\n"
José Fonseca084fe922013-07-09 16:35:14 +010057 " -o, --output=TRACE_FILE Output trace file\n"
Jose Fonsecade8165d2017-08-03 18:54:28 +010058 " --property=NAME=VALUE Set a property\n"
Georg Lutz85792702018-04-13 20:39:53 +020059 " --calls=CALLSET Apply search/replace only to specified calls.\n"
60 " All other calls remain untouched.\n"
José Fonseca084fe922013-07-09 16:35:14 +010061 ;
62}
63
64
Jose Fonsecade8165d2017-08-03 18:54:28 +010065enum {
66 PROPERTY_OPT = CHAR_MAX + 1,
Georg Lutz85792702018-04-13 20:39:53 +020067 CALLS_OPT
Jose Fonsecade8165d2017-08-03 18:54:28 +010068};
69
José Fonseca084fe922013-07-09 16:35:14 +010070const static char *
71shortOptions = "ho:e:";
72
73const static struct option
74longOptions[] = {
75 {"help", no_argument, 0, 'h'},
76 {"output", required_argument, 0, 'o'},
Jose Fonsecade8165d2017-08-03 18:54:28 +010077 {"property", required_argument, 0, PROPERTY_OPT},
Georg Lutz85792702018-04-13 20:39:53 +020078 {"calls", required_argument, 0, CALLS_OPT},
José Fonseca084fe922013-07-09 16:35:14 +010079 {0, 0, 0, 0}
80};
81
82using namespace trace;
83
84
85/**
86 * A visitor that replaces symbol constants.
87 */
88class Replacer : public Visitor
89{
90protected:
Jose Fonseca62b09f52017-09-05 19:05:53 +010091 std::string searchString;
92 std::string replaceString;
93
94 bool isPointer = false;
95 unsigned long long searchPointer = 0;
96 unsigned long long replacePointer = 0;
José Fonseca084fe922013-07-09 16:35:14 +010097
98public:
Jose Fonseca62b09f52017-09-05 19:05:53 +010099 Replacer(const std::string & _searchString, const std::string & _replaceString) :
100 searchString(_searchString),
101 replaceString(_replaceString)
José Fonseca084fe922013-07-09 16:35:14 +0100102 {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100103 if (searchString.length() > 2 &&
104 searchString[0] == '0' &&
105 searchString[1] == 'x') {
106 isPointer = true;
107 searchPointer = std::stoull(searchString, 0, 16);
108 assert(searchPointer);
109 replacePointer = std::stoull(replaceString, 0, 16);
110 assert(replacePointer);
111 }
José Fonseca084fe922013-07-09 16:35:14 +0100112 }
113
Jose Fonseca3457fe42018-09-11 11:18:26 +0100114 virtual ~Replacer() {
José Fonseca084fe922013-07-09 16:35:14 +0100115 }
116
Jose Fonseca6ca93402016-03-05 14:44:48 +0000117 void visit(Null *) override {
José Fonseca084fe922013-07-09 16:35:14 +0100118 }
119
Jose Fonseca6ca93402016-03-05 14:44:48 +0000120 void visit(Bool *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100121 }
122
Jose Fonseca6ca93402016-03-05 14:44:48 +0000123 void visit(SInt *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100124 }
125
Jose Fonseca6ca93402016-03-05 14:44:48 +0000126 void visit(UInt *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100127 }
128
Jose Fonseca6ca93402016-03-05 14:44:48 +0000129 void visit(Float *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100130 }
131
Jose Fonseca6ca93402016-03-05 14:44:48 +0000132 void visit(Double *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100133 }
134
Jose Fonseca6ca93402016-03-05 14:44:48 +0000135 void visit(String *node) override {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100136 if (!searchString.compare(node->value)) {
137 size_t len = replaceString.length() + 1;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200138 delete [] node->value;
Jose Fonseca5642f652015-06-24 11:33:26 +0100139 char *str = new char [len];
Jose Fonseca62b09f52017-09-05 19:05:53 +0100140 memcpy(str, replaceString.c_str(), len);
Arthur Huillet5ee86812015-06-08 16:04:45 +0200141 node->value = str;
142 }
José Fonseca084fe922013-07-09 16:35:14 +0100143 }
144
Jose Fonseca6ca93402016-03-05 14:44:48 +0000145 void visit(WString *node) override {
José Fonsecad5cda7c2014-09-25 15:19:09 +0100146 }
147
Jose Fonseca6ca93402016-03-05 14:44:48 +0000148 void visit(Enum *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100149 const EnumValue *it = node->lookup();
150 if (it) {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100151 if (searchString.compare(it->name) == 0) {
José Fonseca084fe922013-07-09 16:35:14 +0100152 const EnumSig *sig = node->sig;
153 for (unsigned i = 0; i < sig->num_values; ++i) {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100154 if (replaceString.compare(sig->values[i].name) == 0) {
José Fonseca084fe922013-07-09 16:35:14 +0100155 node->value = sig->values[i].value;
156 break;
157 }
158 }
159 }
160 }
161 }
162
Jose Fonseca6ca93402016-03-05 14:44:48 +0000163 void visit(Bitmask *bitmask) override {
Jose Fonseca14bb51d2019-01-03 12:04:18 +0000164 const BitmaskSig *sig = bitmask->sig;
Jose Fonsecaf411d552019-01-24 13:19:01 +0000165 for (const BitmaskFlag *searchIt = sig->flags; searchIt != sig->flags + sig->num_flags; ++searchIt) {
166 if (searchIt->value && (bitmask->value & searchIt->value) == searchIt->value &&
167 searchString.compare(searchIt->name) == 0) {
168 bitmask->value &= ~searchIt->value;
169 for (const BitmaskFlag *replaceIt = sig->flags; replaceIt != sig->flags + sig->num_flags; ++replaceIt) {
170 if (replaceString.compare(replaceIt->name) == 0) {
171 bitmask->value |= replaceIt->value;
172 return;
173 }
174 }
175 /*
176 * Didn't match any of the flags, so presume replacement string
177 * is an integer.
178 */
Jose Fonseca14bb51d2019-01-03 12:04:18 +0000179 bitmask->value |= std::stoull(replaceString);
Jose Fonsecaf411d552019-01-24 13:19:01 +0000180 return;
Jose Fonseca14bb51d2019-01-03 12:04:18 +0000181 }
182 }
José Fonseca084fe922013-07-09 16:35:14 +0100183 }
184
Jose Fonseca6ca93402016-03-05 14:44:48 +0000185 void visit(Struct *s) override {
186 for (auto memberValue : s->members) {
José Fonseca084fe922013-07-09 16:35:14 +0100187 _visit(memberValue);
188 }
189 }
190
Jose Fonseca6ca93402016-03-05 14:44:48 +0000191 void visit(Array *array) override {
192 for (auto & value : array->values) {
193 _visit(value);
José Fonseca084fe922013-07-09 16:35:14 +0100194 }
195 }
196
Jose Fonseca6ca93402016-03-05 14:44:48 +0000197 void visit(Blob *blob) override {
José Fonseca084fe922013-07-09 16:35:14 +0100198 }
199
Jose Fonseca6ca93402016-03-05 14:44:48 +0000200 void visit(Pointer *p) override {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100201 if (isPointer &&
202 p->value == searchPointer) {
203 p->value = replacePointer;
204 }
José Fonseca084fe922013-07-09 16:35:14 +0100205 }
206
Jose Fonseca6ca93402016-03-05 14:44:48 +0000207 void visit(Repr *r) override {
José Fonseca084fe922013-07-09 16:35:14 +0100208 _visit(r->humanValue);
209 }
210
211 void visit(Call *call) {
Jose Fonseca6ca93402016-03-05 14:44:48 +0000212 for (auto & arg : call->args) {
213 if (arg.value) {
214 _visit(arg.value);
José Fonseca084fe922013-07-09 16:35:14 +0100215 }
216 }
217
218 if (call->ret) {
219 _visit(call->ret);
220 }
221 }
222};
223
224
225typedef std::list<Replacer> Replacements;
226
227
228static int
Jose Fonsecade8165d2017-08-03 18:54:28 +0100229sed_trace(Replacements &replacements,
230 const trace::Properties &extraProperties,
231 const char *inFileName,
Georg Lutz85792702018-04-13 20:39:53 +0200232 std::string &outFileName,
233 const trace::CallSet &calls)
José Fonseca084fe922013-07-09 16:35:14 +0100234{
235 trace::Parser p;
236
237 if (!p.open(inFileName)) {
238 std::cerr << "error: failed to open " << inFileName << "\n";
239 return 1;
240 }
241
242 /* Prepare outFileName file and writer for outFileName. */
243 if (outFileName.empty()) {
244 os::String base(inFileName);
245 base.trimExtension();
246
247 outFileName = std::string(base.str()) + std::string("-sed.trace");
248 }
249
Jose Fonsecade8165d2017-08-03 18:54:28 +0100250 trace::Properties properties(p.getProperties());
251 for (auto & kv : extraProperties) {
252 properties[kv.first] = kv.second;
253 }
254
José Fonseca084fe922013-07-09 16:35:14 +0100255 trace::Writer writer;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100256 if (!writer.open(outFileName.c_str(), p.getVersion(), properties)) {
José Fonseca084fe922013-07-09 16:35:14 +0100257 std::cerr << "error: failed to create " << outFileName << "\n";
258 return 1;
259 }
260
261 trace::Call *call;
262 while ((call = p.parse_call())) {
Georg Lutz85792702018-04-13 20:39:53 +0200263 if (calls.empty() || calls.contains(*call)) {
264 for (auto & replacement : replacements) {
265 replacement.visit(call);
266 }
José Fonseca084fe922013-07-09 16:35:14 +0100267 }
268
269 writer.writeCall(call);
270
271 delete call;
272 }
273
José Fonseca8cf63072013-07-25 20:18:58 +0100274 std::cerr << "Edited trace is available as " << outFileName << "\n";
José Fonseca084fe922013-07-09 16:35:14 +0100275
276 return 0;
277}
278
279
280/**
281 * Parse a string in the format "s/SEARCH/REPLACE/".
282 */
283static bool
284parseSubstOpt(Replacements &replacements, const char *opt)
285{
Arthur Huillet5ee86812015-06-08 16:04:45 +0200286 char separator;
287
José Fonseca084fe922013-07-09 16:35:14 +0100288 if (*opt++ != 's') {
289 return false;
290 }
291
Arthur Huillet5ee86812015-06-08 16:04:45 +0200292 separator = *opt++;
José Fonseca084fe922013-07-09 16:35:14 +0100293
294 // Parse the search pattern
295 const char *search_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200296 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100297 if (*opt == 0) {
298 return false;
299 }
300 ++opt;
301 }
302 const char *search_end = opt++;
303
304 // Parse the replace pattern
305 const char *replace_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200306 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100307 if (*opt == 0) {
308 return false;
309 }
310 ++opt;
311 }
312 const char *replace_end = opt++;
313
314 if (*opt != 0) {
315 return false;
316 }
317
318 std::string search(search_begin, search_end);
319 std::string replace(replace_begin, replace_end);
320
Arthur Huillet5ee86812015-06-08 16:04:45 +0200321 // If search or replace strings are taken from a file, read the file
322 std::string file_subst = "@file(";
323
324 for (int i = 0; i < 2; i++) {
325 std::string *str = i ? &search : &replace;
326
327 if (!str->compare(0, file_subst.length(), file_subst)) {
328 if ((*str)[str->length()-1] != ')') {
329 return false;
330 }
331
332 std::string fname = str->substr(file_subst.length());
333 fname[fname.length()-1] = 0;
Jose Fonseca8b0807f2015-06-24 11:29:01 +0100334 FILE *f = fopen(fname.c_str(), "rt");
Arthur Huillet5ee86812015-06-08 16:04:45 +0200335 if (!f) {
336 std::cerr << "error: cannot open file " << fname << "\n";
337 return false;
338 }
339 char buf[1024];
340 (*str) = "";
341 while (!feof(f)) {
342 if (fgets(buf, 1024, f)) {
343 str->append(buf);
344 }
345 }
346 fclose(f);
347 }
348 }
349
350
José Fonseca084fe922013-07-09 16:35:14 +0100351 replacements.push_back(Replacer(search, replace));
352
353 return true;
354}
355
356
357static int
358command(int argc, char *argv[])
359{
360 Replacements replacements;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100361 trace::Properties extraProperties;
José Fonseca084fe922013-07-09 16:35:14 +0100362 std::string outFileName;
Georg Lutz85792702018-04-13 20:39:53 +0200363 trace::CallSet calls;
José Fonseca084fe922013-07-09 16:35:14 +0100364
365 int opt;
366 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
367 switch (opt) {
368 case 'h':
369 usage();
370 return 0;
371 case 'o':
372 outFileName = optarg;
373 break;
374 case 'e':
375 if (!parseSubstOpt(replacements, optarg)) {
Arthur Huillet5ee86812015-06-08 16:04:45 +0200376 std::cerr << "error: invalid replacement pattern `" << optarg << "`\n";
Jose Fonseca4c922f82017-08-03 18:56:38 +0100377 return 1;
José Fonseca084fe922013-07-09 16:35:14 +0100378 }
Jose Fonseca4c922f82017-08-03 18:56:38 +0100379 break;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100380 case PROPERTY_OPT:
381 {
382 const char *sep = strchr(optarg, '=');
383 if (sep == nullptr) {
384 std::cerr << "error: bad property `" << optarg << "`\n";
385 return 1;
386 }
387 std::string name(static_cast<const char *>(optarg), sep);
388 std::string value(sep + 1);
389 extraProperties[name] = value;
390 break;
391 }
Georg Lutz85792702018-04-13 20:39:53 +0200392 case CALLS_OPT:
393 calls.merge(optarg);
394 break;
José Fonseca084fe922013-07-09 16:35:14 +0100395 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700396 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonseca084fe922013-07-09 16:35:14 +0100397 usage();
398 return 1;
399 }
400 }
401
402 if (optind >= argc) {
403 std::cerr << "error: apitrace sed requires a trace file as an argument.\n";
404 usage();
405 return 1;
406 }
407
408 if (argc > optind + 1) {
409 std::cerr << "error: extraneous arguments:";
410 for (int i = optind + 1; i < argc; i++) {
411 std::cerr << " " << argv[i];
412 }
413 std::cerr << "\n";
414 usage();
415 return 1;
416 }
417
Georg Lutz85792702018-04-13 20:39:53 +0200418 return sed_trace(replacements, extraProperties, argv[optind], outFileName, calls);
José Fonseca084fe922013-07-09 16:35:14 +0100419}
420
421
422const Command sed_command = {
423 "sed",
424 synopsis,
425 usage,
426 command
427};