blob: aa60660e389102bbff7f93ebaab84daefa8dd2f5 [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;
165 for (const BitmaskFlag *it = sig->flags; it != sig->flags + sig->num_flags; ++it) {
166 if (it->value && (bitmask->value & it->value) == it->value &&
167 searchString.compare(it->name) == 0) {
168 bitmask->value &= ~it->value;
169 bitmask->value |= std::stoull(replaceString);
170 break;
171 }
172 }
José Fonseca084fe922013-07-09 16:35:14 +0100173 }
174
Jose Fonseca6ca93402016-03-05 14:44:48 +0000175 void visit(Struct *s) override {
176 for (auto memberValue : s->members) {
José Fonseca084fe922013-07-09 16:35:14 +0100177 _visit(memberValue);
178 }
179 }
180
Jose Fonseca6ca93402016-03-05 14:44:48 +0000181 void visit(Array *array) override {
182 for (auto & value : array->values) {
183 _visit(value);
José Fonseca084fe922013-07-09 16:35:14 +0100184 }
185 }
186
Jose Fonseca6ca93402016-03-05 14:44:48 +0000187 void visit(Blob *blob) override {
José Fonseca084fe922013-07-09 16:35:14 +0100188 }
189
Jose Fonseca6ca93402016-03-05 14:44:48 +0000190 void visit(Pointer *p) override {
Jose Fonseca62b09f52017-09-05 19:05:53 +0100191 if (isPointer &&
192 p->value == searchPointer) {
193 p->value = replacePointer;
194 }
José Fonseca084fe922013-07-09 16:35:14 +0100195 }
196
Jose Fonseca6ca93402016-03-05 14:44:48 +0000197 void visit(Repr *r) override {
José Fonseca084fe922013-07-09 16:35:14 +0100198 _visit(r->humanValue);
199 }
200
201 void visit(Call *call) {
Jose Fonseca6ca93402016-03-05 14:44:48 +0000202 for (auto & arg : call->args) {
203 if (arg.value) {
204 _visit(arg.value);
José Fonseca084fe922013-07-09 16:35:14 +0100205 }
206 }
207
208 if (call->ret) {
209 _visit(call->ret);
210 }
211 }
212};
213
214
215typedef std::list<Replacer> Replacements;
216
217
218static int
Jose Fonsecade8165d2017-08-03 18:54:28 +0100219sed_trace(Replacements &replacements,
220 const trace::Properties &extraProperties,
221 const char *inFileName,
Georg Lutz85792702018-04-13 20:39:53 +0200222 std::string &outFileName,
223 const trace::CallSet &calls)
José Fonseca084fe922013-07-09 16:35:14 +0100224{
225 trace::Parser p;
226
227 if (!p.open(inFileName)) {
228 std::cerr << "error: failed to open " << inFileName << "\n";
229 return 1;
230 }
231
232 /* Prepare outFileName file and writer for outFileName. */
233 if (outFileName.empty()) {
234 os::String base(inFileName);
235 base.trimExtension();
236
237 outFileName = std::string(base.str()) + std::string("-sed.trace");
238 }
239
Jose Fonsecade8165d2017-08-03 18:54:28 +0100240 trace::Properties properties(p.getProperties());
241 for (auto & kv : extraProperties) {
242 properties[kv.first] = kv.second;
243 }
244
José Fonseca084fe922013-07-09 16:35:14 +0100245 trace::Writer writer;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100246 if (!writer.open(outFileName.c_str(), p.getVersion(), properties)) {
José Fonseca084fe922013-07-09 16:35:14 +0100247 std::cerr << "error: failed to create " << outFileName << "\n";
248 return 1;
249 }
250
251 trace::Call *call;
252 while ((call = p.parse_call())) {
Georg Lutz85792702018-04-13 20:39:53 +0200253 if (calls.empty() || calls.contains(*call)) {
254 for (auto & replacement : replacements) {
255 replacement.visit(call);
256 }
José Fonseca084fe922013-07-09 16:35:14 +0100257 }
258
259 writer.writeCall(call);
260
261 delete call;
262 }
263
José Fonseca8cf63072013-07-25 20:18:58 +0100264 std::cerr << "Edited trace is available as " << outFileName << "\n";
José Fonseca084fe922013-07-09 16:35:14 +0100265
266 return 0;
267}
268
269
270/**
271 * Parse a string in the format "s/SEARCH/REPLACE/".
272 */
273static bool
274parseSubstOpt(Replacements &replacements, const char *opt)
275{
Arthur Huillet5ee86812015-06-08 16:04:45 +0200276 char separator;
277
José Fonseca084fe922013-07-09 16:35:14 +0100278 if (*opt++ != 's') {
279 return false;
280 }
281
Arthur Huillet5ee86812015-06-08 16:04:45 +0200282 separator = *opt++;
José Fonseca084fe922013-07-09 16:35:14 +0100283
284 // Parse the search pattern
285 const char *search_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200286 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100287 if (*opt == 0) {
288 return false;
289 }
290 ++opt;
291 }
292 const char *search_end = opt++;
293
294 // Parse the replace pattern
295 const char *replace_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 *replace_end = opt++;
303
304 if (*opt != 0) {
305 return false;
306 }
307
308 std::string search(search_begin, search_end);
309 std::string replace(replace_begin, replace_end);
310
Arthur Huillet5ee86812015-06-08 16:04:45 +0200311 // If search or replace strings are taken from a file, read the file
312 std::string file_subst = "@file(";
313
314 for (int i = 0; i < 2; i++) {
315 std::string *str = i ? &search : &replace;
316
317 if (!str->compare(0, file_subst.length(), file_subst)) {
318 if ((*str)[str->length()-1] != ')') {
319 return false;
320 }
321
322 std::string fname = str->substr(file_subst.length());
323 fname[fname.length()-1] = 0;
Jose Fonseca8b0807f2015-06-24 11:29:01 +0100324 FILE *f = fopen(fname.c_str(), "rt");
Arthur Huillet5ee86812015-06-08 16:04:45 +0200325 if (!f) {
326 std::cerr << "error: cannot open file " << fname << "\n";
327 return false;
328 }
329 char buf[1024];
330 (*str) = "";
331 while (!feof(f)) {
332 if (fgets(buf, 1024, f)) {
333 str->append(buf);
334 }
335 }
336 fclose(f);
337 }
338 }
339
340
José Fonseca084fe922013-07-09 16:35:14 +0100341 replacements.push_back(Replacer(search, replace));
342
343 return true;
344}
345
346
347static int
348command(int argc, char *argv[])
349{
350 Replacements replacements;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100351 trace::Properties extraProperties;
José Fonseca084fe922013-07-09 16:35:14 +0100352 std::string outFileName;
Georg Lutz85792702018-04-13 20:39:53 +0200353 trace::CallSet calls;
José Fonseca084fe922013-07-09 16:35:14 +0100354
355 int opt;
356 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
357 switch (opt) {
358 case 'h':
359 usage();
360 return 0;
361 case 'o':
362 outFileName = optarg;
363 break;
364 case 'e':
365 if (!parseSubstOpt(replacements, optarg)) {
Arthur Huillet5ee86812015-06-08 16:04:45 +0200366 std::cerr << "error: invalid replacement pattern `" << optarg << "`\n";
Jose Fonseca4c922f82017-08-03 18:56:38 +0100367 return 1;
José Fonseca084fe922013-07-09 16:35:14 +0100368 }
Jose Fonseca4c922f82017-08-03 18:56:38 +0100369 break;
Jose Fonsecade8165d2017-08-03 18:54:28 +0100370 case PROPERTY_OPT:
371 {
372 const char *sep = strchr(optarg, '=');
373 if (sep == nullptr) {
374 std::cerr << "error: bad property `" << optarg << "`\n";
375 return 1;
376 }
377 std::string name(static_cast<const char *>(optarg), sep);
378 std::string value(sep + 1);
379 extraProperties[name] = value;
380 break;
381 }
Georg Lutz85792702018-04-13 20:39:53 +0200382 case CALLS_OPT:
383 calls.merge(optarg);
384 break;
José Fonseca084fe922013-07-09 16:35:14 +0100385 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700386 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonseca084fe922013-07-09 16:35:14 +0100387 usage();
388 return 1;
389 }
390 }
391
392 if (optind >= argc) {
393 std::cerr << "error: apitrace sed requires a trace file as an argument.\n";
394 usage();
395 return 1;
396 }
397
398 if (argc > optind + 1) {
399 std::cerr << "error: extraneous arguments:";
400 for (int i = optind + 1; i < argc; i++) {
401 std::cerr << " " << argv[i];
402 }
403 std::cerr << "\n";
404 usage();
405 return 1;
406 }
407
Georg Lutz85792702018-04-13 20:39:53 +0200408 return sed_trace(replacements, extraProperties, argv[optind], outFileName, calls);
José Fonseca084fe922013-07-09 16:35:14 +0100409}
410
411
412const Command sed_command = {
413 "sed",
414 synopsis,
415 usage,
416 command
417};