blob: be4283cef77e3ac4d2ec7acccf1257a94f4fdbcc [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"
57 ;
58}
59
60
61const static char *
62shortOptions = "ho:e:";
63
64const static struct option
65longOptions[] = {
66 {"help", no_argument, 0, 'h'},
67 {"output", required_argument, 0, 'o'},
68 {0, 0, 0, 0}
69};
70
71using namespace trace;
72
73
74/**
75 * A visitor that replaces symbol constants.
76 */
77class Replacer : public Visitor
78{
79protected:
80 std::string searchName;
81 std::string replaceName;
82
83public:
84 Replacer(const std::string & _searchName, const std::string & _replaceName) :
85 searchName(_searchName),
86 replaceName(_replaceName)
87 {
88 }
89
90 ~Replacer() {
91 }
92
Jose Fonseca6ca93402016-03-05 14:44:48 +000093 void visit(Null *) override {
José Fonseca084fe922013-07-09 16:35:14 +010094 }
95
Jose Fonseca6ca93402016-03-05 14:44:48 +000096 void visit(Bool *node) override {
José Fonseca084fe922013-07-09 16:35:14 +010097 }
98
Jose Fonseca6ca93402016-03-05 14:44:48 +000099 void visit(SInt *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100100 }
101
Jose Fonseca6ca93402016-03-05 14:44:48 +0000102 void visit(UInt *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100103 }
104
Jose Fonseca6ca93402016-03-05 14:44:48 +0000105 void visit(Float *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100106 }
107
Jose Fonseca6ca93402016-03-05 14:44:48 +0000108 void visit(Double *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100109 }
110
Jose Fonseca6ca93402016-03-05 14:44:48 +0000111 void visit(String *node) override {
Arthur Huillet5ee86812015-06-08 16:04:45 +0200112 if (!searchName.compare(node->value)) {
Jose Fonseca5642f652015-06-24 11:33:26 +0100113 size_t len = replaceName.length() + 1;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200114 delete [] node->value;
Jose Fonseca5642f652015-06-24 11:33:26 +0100115 char *str = new char [len];
116 memcpy(str, replaceName.c_str(), len);
Arthur Huillet5ee86812015-06-08 16:04:45 +0200117 node->value = str;
118 }
José Fonseca084fe922013-07-09 16:35:14 +0100119 }
120
Jose Fonseca6ca93402016-03-05 14:44:48 +0000121 void visit(WString *node) override {
José Fonsecad5cda7c2014-09-25 15:19:09 +0100122 }
123
Jose Fonseca6ca93402016-03-05 14:44:48 +0000124 void visit(Enum *node) override {
José Fonseca084fe922013-07-09 16:35:14 +0100125 const EnumValue *it = node->lookup();
126 if (it) {
127 if (searchName.compare(it->name) == 0) {
128 const EnumSig *sig = node->sig;
129 for (unsigned i = 0; i < sig->num_values; ++i) {
130 if (replaceName.compare(sig->values[i].name) == 0) {
131 node->value = sig->values[i].value;
132 break;
133 }
134 }
135 }
136 }
137 }
138
Jose Fonseca6ca93402016-03-05 14:44:48 +0000139 void visit(Bitmask *bitmask) override {
José Fonseca084fe922013-07-09 16:35:14 +0100140 }
141
Jose Fonseca6ca93402016-03-05 14:44:48 +0000142 void visit(Struct *s) override {
143 for (auto memberValue : s->members) {
José Fonseca084fe922013-07-09 16:35:14 +0100144 _visit(memberValue);
145 }
146 }
147
Jose Fonseca6ca93402016-03-05 14:44:48 +0000148 void visit(Array *array) override {
149 for (auto & value : array->values) {
150 _visit(value);
José Fonseca084fe922013-07-09 16:35:14 +0100151 }
152 }
153
Jose Fonseca6ca93402016-03-05 14:44:48 +0000154 void visit(Blob *blob) override {
José Fonseca084fe922013-07-09 16:35:14 +0100155 }
156
Jose Fonseca6ca93402016-03-05 14:44:48 +0000157 void visit(Pointer *p) override {
José Fonseca084fe922013-07-09 16:35:14 +0100158 }
159
Jose Fonseca6ca93402016-03-05 14:44:48 +0000160 void visit(Repr *r) override {
José Fonseca084fe922013-07-09 16:35:14 +0100161 _visit(r->humanValue);
162 }
163
164 void visit(Call *call) {
Jose Fonseca6ca93402016-03-05 14:44:48 +0000165 for (auto & arg : call->args) {
166 if (arg.value) {
167 _visit(arg.value);
José Fonseca084fe922013-07-09 16:35:14 +0100168 }
169 }
170
171 if (call->ret) {
172 _visit(call->ret);
173 }
174 }
175};
176
177
178typedef std::list<Replacer> Replacements;
179
180
181static int
182sed_trace(Replacements &replacements, const char *inFileName, std::string &outFileName)
183{
184 trace::Parser p;
185
186 if (!p.open(inFileName)) {
187 std::cerr << "error: failed to open " << inFileName << "\n";
188 return 1;
189 }
190
191 /* Prepare outFileName file and writer for outFileName. */
192 if (outFileName.empty()) {
193 os::String base(inFileName);
194 base.trimExtension();
195
196 outFileName = std::string(base.str()) + std::string("-sed.trace");
197 }
198
199 trace::Writer writer;
Jose Fonseca7301ab12017-08-03 18:25:53 +0100200 if (!writer.open(outFileName.c_str(), p.getVersion(), p.getProperties())) {
José Fonseca084fe922013-07-09 16:35:14 +0100201 std::cerr << "error: failed to create " << outFileName << "\n";
202 return 1;
203 }
204
205 trace::Call *call;
206 while ((call = p.parse_call())) {
207
Jose Fonseca6ca93402016-03-05 14:44:48 +0000208 for (auto & replacement : replacements) {
209 replacement.visit(call);
José Fonseca084fe922013-07-09 16:35:14 +0100210 }
211
212 writer.writeCall(call);
213
214 delete call;
215 }
216
José Fonseca8cf63072013-07-25 20:18:58 +0100217 std::cerr << "Edited trace is available as " << outFileName << "\n";
José Fonseca084fe922013-07-09 16:35:14 +0100218
219 return 0;
220}
221
222
223/**
224 * Parse a string in the format "s/SEARCH/REPLACE/".
225 */
226static bool
227parseSubstOpt(Replacements &replacements, const char *opt)
228{
Arthur Huillet5ee86812015-06-08 16:04:45 +0200229 char separator;
230
José Fonseca084fe922013-07-09 16:35:14 +0100231 if (*opt++ != 's') {
232 return false;
233 }
234
Arthur Huillet5ee86812015-06-08 16:04:45 +0200235 separator = *opt++;
José Fonseca084fe922013-07-09 16:35:14 +0100236
237 // Parse the search pattern
238 const char *search_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200239 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100240 if (*opt == 0) {
241 return false;
242 }
243 ++opt;
244 }
245 const char *search_end = opt++;
246
247 // Parse the replace pattern
248 const char *replace_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200249 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100250 if (*opt == 0) {
251 return false;
252 }
253 ++opt;
254 }
255 const char *replace_end = opt++;
256
257 if (*opt != 0) {
258 return false;
259 }
260
261 std::string search(search_begin, search_end);
262 std::string replace(replace_begin, replace_end);
263
Arthur Huillet5ee86812015-06-08 16:04:45 +0200264 // If search or replace strings are taken from a file, read the file
265 std::string file_subst = "@file(";
266
267 for (int i = 0; i < 2; i++) {
268 std::string *str = i ? &search : &replace;
269
270 if (!str->compare(0, file_subst.length(), file_subst)) {
271 if ((*str)[str->length()-1] != ')') {
272 return false;
273 }
274
275 std::string fname = str->substr(file_subst.length());
276 fname[fname.length()-1] = 0;
Jose Fonseca8b0807f2015-06-24 11:29:01 +0100277 FILE *f = fopen(fname.c_str(), "rt");
Arthur Huillet5ee86812015-06-08 16:04:45 +0200278 if (!f) {
279 std::cerr << "error: cannot open file " << fname << "\n";
280 return false;
281 }
282 char buf[1024];
283 (*str) = "";
284 while (!feof(f)) {
285 if (fgets(buf, 1024, f)) {
286 str->append(buf);
287 }
288 }
289 fclose(f);
290 }
291 }
292
293
José Fonseca084fe922013-07-09 16:35:14 +0100294 replacements.push_back(Replacer(search, replace));
295
296 return true;
297}
298
299
300static int
301command(int argc, char *argv[])
302{
303 Replacements replacements;
304 std::string outFileName;
305
306 int opt;
307 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
308 switch (opt) {
309 case 'h':
310 usage();
311 return 0;
312 case 'o':
313 outFileName = optarg;
314 break;
315 case 'e':
316 if (!parseSubstOpt(replacements, optarg)) {
Arthur Huillet5ee86812015-06-08 16:04:45 +0200317 std::cerr << "error: invalid replacement pattern `" << optarg << "`\n";
José Fonseca084fe922013-07-09 16:35:14 +0100318 }
319 break;
320 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700321 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonseca084fe922013-07-09 16:35:14 +0100322 usage();
323 return 1;
324 }
325 }
326
327 if (optind >= argc) {
328 std::cerr << "error: apitrace sed requires a trace file as an argument.\n";
329 usage();
330 return 1;
331 }
332
333 if (argc > optind + 1) {
334 std::cerr << "error: extraneous arguments:";
335 for (int i = optind + 1; i < argc; i++) {
336 std::cerr << " " << argv[i];
337 }
338 std::cerr << "\n";
339 usage();
340 return 1;
341 }
342
343 return sed_trace(replacements, argv[optind], outFileName);
344}
345
346
347const Command sed_command = {
348 "sed",
349 synopsis,
350 usage,
351 command
352};