blob: de851f3382b883d06de20e35b11b95ac0a2414eb [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 {
José Fonseca084fe922013-07-09 16:35:14 +0100164 }
165
Jose Fonseca6ca93402016-03-05 14:44:48 +0000166 void visit(Struct *s) override {
167 for (auto memberValue : s->members) {
José Fonseca084fe922013-07-09 16:35:14 +0100168 _visit(memberValue);
169 }
170 }
171
Jose Fonseca6ca93402016-03-05 14:44:48 +0000172 void visit(Array *array) override {
173 for (auto & value : array->values) {
174 _visit(value);
José Fonseca084fe922013-07-09 16:35:14 +0100175 }
176 }
177
Jose Fonseca6ca93402016-03-05 14:44:48 +0000178 void visit(Blob *blob) override {
José Fonseca084fe922013-07-09 16:35:14 +0100179 }
180
Jose Fonseca6ca93402016-03-05 14:44:48 +0000181 void visit(Pointer *p) override {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100182 if (isPointer &&
183 p->value == searchPointer) {
184 p->value = replacePointer;
185 }
José Fonseca084fe922013-07-09 16:35:14 +0100186 }
187
Jose Fonseca6ca93402016-03-05 14:44:48 +0000188 void visit(Repr *r) override {
José Fonseca084fe922013-07-09 16:35:14 +0100189 _visit(r->humanValue);
190 }
191
192 void visit(Call *call) {
Jose Fonseca6ca93402016-03-05 14:44:48 +0000193 for (auto & arg : call->args) {
194 if (arg.value) {
195 _visit(arg.value);
José Fonseca084fe922013-07-09 16:35:14 +0100196 }
197 }
198
199 if (call->ret) {
200 _visit(call->ret);
201 }
202 }
203};
204
205
206typedef std::list<Replacer> Replacements;
207
208
209static int
Jose Fonsecade8165d2017-08-03 18:54:28 +0100210sed_trace(Replacements &replacements,
211 const trace::Properties &extraProperties,
212 const char *inFileName,
Georg Lutz85792702018-04-13 20:39:53 +0200213 std::string &outFileName,
214 const trace::CallSet &calls)
José Fonseca084fe922013-07-09 16:35:14 +0100215{
216 trace::Parser p;
217
218 if (!p.open(inFileName)) {
219 std::cerr << "error: failed to open " << inFileName << "\n";
220 return 1;
221 }
222
223 /* Prepare outFileName file and writer for outFileName. */
224 if (outFileName.empty()) {
225 os::String base(inFileName);
226 base.trimExtension();
227
228 outFileName = std::string(base.str()) + std::string("-sed.trace");
229 }
230
Jose Fonsecade8165d2017-08-03 18:54:28 +0100231 trace::Properties properties(p.getProperties());
232 for (auto & kv : extraProperties) {
233 properties[kv.first] = kv.second;
234 }
235
José Fonseca084fe922013-07-09 16:35:14 +0100236 trace::Writer writer;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100237 if (!writer.open(outFileName.c_str(), p.getVersion(), properties)) {
José Fonseca084fe922013-07-09 16:35:14 +0100238 std::cerr << "error: failed to create " << outFileName << "\n";
239 return 1;
240 }
241
242 trace::Call *call;
243 while ((call = p.parse_call())) {
Georg Lutz85792702018-04-13 20:39:53 +0200244 if (calls.empty() || calls.contains(*call)) {
245 for (auto & replacement : replacements) {
246 replacement.visit(call);
247 }
José Fonseca084fe922013-07-09 16:35:14 +0100248 }
249
250 writer.writeCall(call);
251
252 delete call;
253 }
254
José Fonseca8cf63072013-07-25 20:18:58 +0100255 std::cerr << "Edited trace is available as " << outFileName << "\n";
José Fonseca084fe922013-07-09 16:35:14 +0100256
257 return 0;
258}
259
260
261/**
262 * Parse a string in the format "s/SEARCH/REPLACE/".
263 */
264static bool
265parseSubstOpt(Replacements &replacements, const char *opt)
266{
Arthur Huillet5ee86812015-06-08 16:04:45 +0200267 char separator;
268
José Fonseca084fe922013-07-09 16:35:14 +0100269 if (*opt++ != 's') {
270 return false;
271 }
272
Arthur Huillet5ee86812015-06-08 16:04:45 +0200273 separator = *opt++;
José Fonseca084fe922013-07-09 16:35:14 +0100274
275 // Parse the search pattern
276 const char *search_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200277 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100278 if (*opt == 0) {
279 return false;
280 }
281 ++opt;
282 }
283 const char *search_end = opt++;
284
285 // Parse the replace pattern
286 const char *replace_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200287 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100288 if (*opt == 0) {
289 return false;
290 }
291 ++opt;
292 }
293 const char *replace_end = opt++;
294
295 if (*opt != 0) {
296 return false;
297 }
298
299 std::string search(search_begin, search_end);
300 std::string replace(replace_begin, replace_end);
301
Arthur Huillet5ee86812015-06-08 16:04:45 +0200302 // If search or replace strings are taken from a file, read the file
303 std::string file_subst = "@file(";
304
305 for (int i = 0; i < 2; i++) {
306 std::string *str = i ? &search : &replace;
307
308 if (!str->compare(0, file_subst.length(), file_subst)) {
309 if ((*str)[str->length()-1] != ')') {
310 return false;
311 }
312
313 std::string fname = str->substr(file_subst.length());
314 fname[fname.length()-1] = 0;
Jose Fonseca8b0807f2015-06-24 11:29:01 +0100315 FILE *f = fopen(fname.c_str(), "rt");
Arthur Huillet5ee86812015-06-08 16:04:45 +0200316 if (!f) {
317 std::cerr << "error: cannot open file " << fname << "\n";
318 return false;
319 }
320 char buf[1024];
321 (*str) = "";
322 while (!feof(f)) {
323 if (fgets(buf, 1024, f)) {
324 str->append(buf);
325 }
326 }
327 fclose(f);
328 }
329 }
330
331
José Fonseca084fe922013-07-09 16:35:14 +0100332 replacements.push_back(Replacer(search, replace));
333
334 return true;
335}
336
337
338static int
339command(int argc, char *argv[])
340{
341 Replacements replacements;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100342 trace::Properties extraProperties;
José Fonseca084fe922013-07-09 16:35:14 +0100343 std::string outFileName;
Georg Lutz85792702018-04-13 20:39:53 +0200344 trace::CallSet calls;
José Fonseca084fe922013-07-09 16:35:14 +0100345
346 int opt;
347 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
348 switch (opt) {
349 case 'h':
350 usage();
351 return 0;
352 case 'o':
353 outFileName = optarg;
354 break;
355 case 'e':
356 if (!parseSubstOpt(replacements, optarg)) {
Arthur Huillet5ee86812015-06-08 16:04:45 +0200357 std::cerr << "error: invalid replacement pattern `" << optarg << "`\n";
Jose Fonseca4c922f82017-08-03 18:56:38 +0100358 return 1;
José Fonseca084fe922013-07-09 16:35:14 +0100359 }
Jose Fonseca4c922f82017-08-03 18:56:38 +0100360 break;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100361 case PROPERTY_OPT:
362 {
363 const char *sep = strchr(optarg, '=');
364 if (sep == nullptr) {
365 std::cerr << "error: bad property `" << optarg << "`\n";
366 return 1;
367 }
368 std::string name(static_cast<const char *>(optarg), sep);
369 std::string value(sep + 1);
370 extraProperties[name] = value;
371 break;
372 }
Georg Lutz85792702018-04-13 20:39:53 +0200373 case CALLS_OPT:
374 calls.merge(optarg);
375 break;
José Fonseca084fe922013-07-09 16:35:14 +0100376 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700377 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonseca084fe922013-07-09 16:35:14 +0100378 usage();
379 return 1;
380 }
381 }
382
383 if (optind >= argc) {
384 std::cerr << "error: apitrace sed requires a trace file as an argument.\n";
385 usage();
386 return 1;
387 }
388
389 if (argc > optind + 1) {
390 std::cerr << "error: extraneous arguments:";
391 for (int i = optind + 1; i < argc; i++) {
392 std::cerr << " " << argv[i];
393 }
394 std::cerr << "\n";
395 usage();
396 return 1;
397 }
398
Georg Lutz85792702018-04-13 20:39:53 +0200399 return sed_trace(replacements, extraProperties, argv[optind], outFileName, calls);
José Fonseca084fe922013-07-09 16:35:14 +0100400}
401
402
403const Command sed_command = {
404 "sed",
405 synopsis,
406 usage,
407 command
408};