blob: 401b7a63596d80b9375da7a6853f1dc46b0f4814 [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
30#include "cli.hpp"
31
32#include "os_string.hpp"
33
34#include "trace_parser.hpp"
35#include "trace_writer.hpp"
36
37
38static const char *synopsis = "Stream editing of a trace.";
39
40
41static void
42usage(void)
43{
44 std::cout
45 << "usage: apitrace sed [OPTIONS] TRACE_FILE...\n"
46 << synopsis << "\n"
47 "\n"
48 " -h, --help Show detailed help for sed options and exit\n"
Arthur Huillet5ee86812015-06-08 16:04:45 +020049 " -e s/SEARCH/REPLACE/ Search and replace a symbol. Use @file(<path>)\n"
50 " to read SEARCH or REPLACE from a file.\n"
51 " Any character (not just /) can be used as \n"
52 " separator.\n"
53 " XXX: Only works for enums and strings.\n"
José Fonseca084fe922013-07-09 16:35:14 +010054 " -o, --output=TRACE_FILE Output trace file\n"
55 ;
56}
57
58
59const static char *
60shortOptions = "ho:e:";
61
62const static struct option
63longOptions[] = {
64 {"help", no_argument, 0, 'h'},
65 {"output", required_argument, 0, 'o'},
66 {0, 0, 0, 0}
67};
68
69using namespace trace;
70
71
72/**
73 * A visitor that replaces symbol constants.
74 */
75class Replacer : public Visitor
76{
77protected:
78 std::string searchName;
79 std::string replaceName;
80
81public:
82 Replacer(const std::string & _searchName, const std::string & _replaceName) :
83 searchName(_searchName),
84 replaceName(_replaceName)
85 {
86 }
87
88 ~Replacer() {
89 }
90
91 void visit(Null *) {
92 }
93
94 void visit(Bool *node) {
95 }
96
97 void visit(SInt *node) {
98 }
99
100 void visit(UInt *node) {
101 }
102
103 void visit(Float *node) {
104 }
105
106 void visit(Double *node) {
107 }
108
109 void visit(String *node) {
Arthur Huillet5ee86812015-06-08 16:04:45 +0200110 if (!searchName.compare(node->value)) {
Jose Fonseca5642f652015-06-24 11:33:26 +0100111 size_t len = replaceName.length() + 1;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200112 delete [] node->value;
Jose Fonseca5642f652015-06-24 11:33:26 +0100113 char *str = new char [len];
114 memcpy(str, replaceName.c_str(), len);
Arthur Huillet5ee86812015-06-08 16:04:45 +0200115 node->value = str;
116 }
José Fonseca084fe922013-07-09 16:35:14 +0100117 }
118
José Fonsecad5cda7c2014-09-25 15:19:09 +0100119 void visit(WString *node) {
120 }
121
José Fonseca084fe922013-07-09 16:35:14 +0100122 void visit(Enum *node) {
123 const EnumValue *it = node->lookup();
124 if (it) {
125 if (searchName.compare(it->name) == 0) {
126 const EnumSig *sig = node->sig;
127 for (unsigned i = 0; i < sig->num_values; ++i) {
128 if (replaceName.compare(sig->values[i].name) == 0) {
129 node->value = sig->values[i].value;
130 break;
131 }
132 }
133 }
134 }
135 }
136
137 void visit(Bitmask *bitmask) {
138 }
139
140 void visit(Struct *s) {
141 for (unsigned i = 0; i < s->members.size(); ++i) {
142 Value *memberValue = s->members[i];
143 _visit(memberValue);
144 }
145 }
146
147 void visit(Array *array) {
148 for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
149 _visit(*it);
150 }
151 }
152
153 void visit(Blob *blob) {
154 }
155
156 void visit(Pointer *p) {
157 }
158
159 void visit(Repr *r) {
160 _visit(r->humanValue);
161 }
162
163 void visit(Call *call) {
164 for (unsigned i = 0; i < call->args.size(); ++i) {
165 if (call->args[i].value) {
166 _visit(call->args[i].value);
167 }
168 }
169
170 if (call->ret) {
171 _visit(call->ret);
172 }
173 }
174};
175
176
177typedef std::list<Replacer> Replacements;
178
179
180static int
181sed_trace(Replacements &replacements, const char *inFileName, std::string &outFileName)
182{
183 trace::Parser p;
184
185 if (!p.open(inFileName)) {
186 std::cerr << "error: failed to open " << inFileName << "\n";
187 return 1;
188 }
189
190 /* Prepare outFileName file and writer for outFileName. */
191 if (outFileName.empty()) {
192 os::String base(inFileName);
193 base.trimExtension();
194
195 outFileName = std::string(base.str()) + std::string("-sed.trace");
196 }
197
198 trace::Writer writer;
199 if (!writer.open(outFileName.c_str())) {
200 std::cerr << "error: failed to create " << outFileName << "\n";
201 return 1;
202 }
203
204 trace::Call *call;
205 while ((call = p.parse_call())) {
206
207 for (Replacements::iterator it = replacements.begin(); it != replacements.end(); ++it) {
208 it->visit(call);
209 }
210
211 writer.writeCall(call);
212
213 delete call;
214 }
215
José Fonseca8cf63072013-07-25 20:18:58 +0100216 std::cerr << "Edited trace is available as " << outFileName << "\n";
José Fonseca084fe922013-07-09 16:35:14 +0100217
218 return 0;
219}
220
221
222/**
223 * Parse a string in the format "s/SEARCH/REPLACE/".
224 */
225static bool
226parseSubstOpt(Replacements &replacements, const char *opt)
227{
Arthur Huillet5ee86812015-06-08 16:04:45 +0200228 char separator;
229
José Fonseca084fe922013-07-09 16:35:14 +0100230 if (*opt++ != 's') {
231 return false;
232 }
233
Arthur Huillet5ee86812015-06-08 16:04:45 +0200234 separator = *opt++;
José Fonseca084fe922013-07-09 16:35:14 +0100235
236 // Parse the search pattern
237 const char *search_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200238 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100239 if (*opt == 0) {
240 return false;
241 }
242 ++opt;
243 }
244 const char *search_end = opt++;
245
246 // Parse the replace pattern
247 const char *replace_begin = opt;
Arthur Huillet5ee86812015-06-08 16:04:45 +0200248 while (*opt != separator) {
José Fonseca084fe922013-07-09 16:35:14 +0100249 if (*opt == 0) {
250 return false;
251 }
252 ++opt;
253 }
254 const char *replace_end = opt++;
255
256 if (*opt != 0) {
257 return false;
258 }
259
260 std::string search(search_begin, search_end);
261 std::string replace(replace_begin, replace_end);
262
Arthur Huillet5ee86812015-06-08 16:04:45 +0200263 // If search or replace strings are taken from a file, read the file
264 std::string file_subst = "@file(";
265
266 for (int i = 0; i < 2; i++) {
267 std::string *str = i ? &search : &replace;
268
269 if (!str->compare(0, file_subst.length(), file_subst)) {
270 if ((*str)[str->length()-1] != ')') {
271 return false;
272 }
273
274 std::string fname = str->substr(file_subst.length());
275 fname[fname.length()-1] = 0;
Jose Fonseca8b0807f2015-06-24 11:29:01 +0100276 FILE *f = fopen(fname.c_str(), "rt");
Arthur Huillet5ee86812015-06-08 16:04:45 +0200277 if (!f) {
278 std::cerr << "error: cannot open file " << fname << "\n";
279 return false;
280 }
281 char buf[1024];
282 (*str) = "";
283 while (!feof(f)) {
284 if (fgets(buf, 1024, f)) {
285 str->append(buf);
286 }
287 }
288 fclose(f);
289 }
290 }
291
292
José Fonseca084fe922013-07-09 16:35:14 +0100293 replacements.push_back(Replacer(search, replace));
294
295 return true;
296}
297
298
299static int
300command(int argc, char *argv[])
301{
302 Replacements replacements;
303 std::string outFileName;
304
305 int opt;
306 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
307 switch (opt) {
308 case 'h':
309 usage();
310 return 0;
311 case 'o':
312 outFileName = optarg;
313 break;
314 case 'e':
315 if (!parseSubstOpt(replacements, optarg)) {
Arthur Huillet5ee86812015-06-08 16:04:45 +0200316 std::cerr << "error: invalid replacement pattern `" << optarg << "`\n";
José Fonseca084fe922013-07-09 16:35:14 +0100317 }
318 break;
319 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700320 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonseca084fe922013-07-09 16:35:14 +0100321 usage();
322 return 1;
323 }
324 }
325
326 if (optind >= argc) {
327 std::cerr << "error: apitrace sed requires a trace file as an argument.\n";
328 usage();
329 return 1;
330 }
331
332 if (argc > optind + 1) {
333 std::cerr << "error: extraneous arguments:";
334 for (int i = optind + 1; i < argc; i++) {
335 std::cerr << " " << argv[i];
336 }
337 std::cerr << "\n";
338 usage();
339 return 1;
340 }
341
342 return sed_trace(replacements, argv[optind], outFileName);
343}
344
345
346const Command sed_command = {
347 "sed",
348 synopsis,
349 usage,
350 command
351};