José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 1 | /************************************************************************** |
| 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 | |
| 38 | static const char *synopsis = "Stream editing of a trace."; |
| 39 | |
| 40 | |
| 41 | static void |
| 42 | usage(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 Huillet | 5ee8681 | 2015-06-08 16:04:45 +0200 | [diff] [blame] | 49 | " -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é Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 54 | " -o, --output=TRACE_FILE Output trace file\n" |
| 55 | ; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | const static char * |
| 60 | shortOptions = "ho:e:"; |
| 61 | |
| 62 | const static struct option |
| 63 | longOptions[] = { |
| 64 | {"help", no_argument, 0, 'h'}, |
| 65 | {"output", required_argument, 0, 'o'}, |
| 66 | {0, 0, 0, 0} |
| 67 | }; |
| 68 | |
| 69 | using namespace trace; |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * A visitor that replaces symbol constants. |
| 74 | */ |
| 75 | class Replacer : public Visitor |
| 76 | { |
| 77 | protected: |
| 78 | std::string searchName; |
| 79 | std::string replaceName; |
| 80 | |
| 81 | public: |
| 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 Huillet | 5ee8681 | 2015-06-08 16:04:45 +0200 | [diff] [blame] | 110 | if (!searchName.compare(node->value)) { |
| 111 | delete [] node->value; |
| 112 | char *str = new char [replaceName.length() + 1]; |
| 113 | strcpy(str, replaceName.c_str()); |
| 114 | node->value = str; |
| 115 | } |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 116 | } |
| 117 | |
José Fonseca | d5cda7c | 2014-09-25 15:19:09 +0100 | [diff] [blame] | 118 | void visit(WString *node) { |
| 119 | } |
| 120 | |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 121 | void visit(Enum *node) { |
| 122 | const EnumValue *it = node->lookup(); |
| 123 | if (it) { |
| 124 | if (searchName.compare(it->name) == 0) { |
| 125 | const EnumSig *sig = node->sig; |
| 126 | for (unsigned i = 0; i < sig->num_values; ++i) { |
| 127 | if (replaceName.compare(sig->values[i].name) == 0) { |
| 128 | node->value = sig->values[i].value; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void visit(Bitmask *bitmask) { |
| 137 | } |
| 138 | |
| 139 | void visit(Struct *s) { |
| 140 | for (unsigned i = 0; i < s->members.size(); ++i) { |
| 141 | Value *memberValue = s->members[i]; |
| 142 | _visit(memberValue); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void visit(Array *array) { |
| 147 | for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) { |
| 148 | _visit(*it); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void visit(Blob *blob) { |
| 153 | } |
| 154 | |
| 155 | void visit(Pointer *p) { |
| 156 | } |
| 157 | |
| 158 | void visit(Repr *r) { |
| 159 | _visit(r->humanValue); |
| 160 | } |
| 161 | |
| 162 | void visit(Call *call) { |
| 163 | for (unsigned i = 0; i < call->args.size(); ++i) { |
| 164 | if (call->args[i].value) { |
| 165 | _visit(call->args[i].value); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (call->ret) { |
| 170 | _visit(call->ret); |
| 171 | } |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | |
| 176 | typedef std::list<Replacer> Replacements; |
| 177 | |
| 178 | |
| 179 | static int |
| 180 | sed_trace(Replacements &replacements, const char *inFileName, std::string &outFileName) |
| 181 | { |
| 182 | trace::Parser p; |
| 183 | |
| 184 | if (!p.open(inFileName)) { |
| 185 | std::cerr << "error: failed to open " << inFileName << "\n"; |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | /* Prepare outFileName file and writer for outFileName. */ |
| 190 | if (outFileName.empty()) { |
| 191 | os::String base(inFileName); |
| 192 | base.trimExtension(); |
| 193 | |
| 194 | outFileName = std::string(base.str()) + std::string("-sed.trace"); |
| 195 | } |
| 196 | |
| 197 | trace::Writer writer; |
| 198 | if (!writer.open(outFileName.c_str())) { |
| 199 | std::cerr << "error: failed to create " << outFileName << "\n"; |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | trace::Call *call; |
| 204 | while ((call = p.parse_call())) { |
| 205 | |
| 206 | for (Replacements::iterator it = replacements.begin(); it != replacements.end(); ++it) { |
| 207 | it->visit(call); |
| 208 | } |
| 209 | |
| 210 | writer.writeCall(call); |
| 211 | |
| 212 | delete call; |
| 213 | } |
| 214 | |
José Fonseca | 8cf6307 | 2013-07-25 20:18:58 +0100 | [diff] [blame] | 215 | std::cerr << "Edited trace is available as " << outFileName << "\n"; |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /** |
| 222 | * Parse a string in the format "s/SEARCH/REPLACE/". |
| 223 | */ |
| 224 | static bool |
| 225 | parseSubstOpt(Replacements &replacements, const char *opt) |
| 226 | { |
Arthur Huillet | 5ee8681 | 2015-06-08 16:04:45 +0200 | [diff] [blame] | 227 | char separator; |
| 228 | |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 229 | if (*opt++ != 's') { |
| 230 | return false; |
| 231 | } |
| 232 | |
Arthur Huillet | 5ee8681 | 2015-06-08 16:04:45 +0200 | [diff] [blame] | 233 | separator = *opt++; |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 234 | |
| 235 | // Parse the search pattern |
| 236 | const char *search_begin = opt; |
Arthur Huillet | 5ee8681 | 2015-06-08 16:04:45 +0200 | [diff] [blame] | 237 | while (*opt != separator) { |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 238 | if (*opt == 0) { |
| 239 | return false; |
| 240 | } |
| 241 | ++opt; |
| 242 | } |
| 243 | const char *search_end = opt++; |
| 244 | |
| 245 | // Parse the replace pattern |
| 246 | const char *replace_begin = opt; |
Arthur Huillet | 5ee8681 | 2015-06-08 16:04:45 +0200 | [diff] [blame] | 247 | while (*opt != separator) { |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 248 | if (*opt == 0) { |
| 249 | return false; |
| 250 | } |
| 251 | ++opt; |
| 252 | } |
| 253 | const char *replace_end = opt++; |
| 254 | |
| 255 | if (*opt != 0) { |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | std::string search(search_begin, search_end); |
| 260 | std::string replace(replace_begin, replace_end); |
| 261 | |
Arthur Huillet | 5ee8681 | 2015-06-08 16:04:45 +0200 | [diff] [blame] | 262 | // If search or replace strings are taken from a file, read the file |
| 263 | std::string file_subst = "@file("; |
| 264 | |
| 265 | for (int i = 0; i < 2; i++) { |
| 266 | std::string *str = i ? &search : &replace; |
| 267 | |
| 268 | if (!str->compare(0, file_subst.length(), file_subst)) { |
| 269 | if ((*str)[str->length()-1] != ')') { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | std::string fname = str->substr(file_subst.length()); |
| 274 | fname[fname.length()-1] = 0; |
Jose Fonseca | 8b0807f | 2015-06-24 11:29:01 +0100 | [diff] [blame^] | 275 | FILE *f = fopen(fname.c_str(), "rt"); |
Arthur Huillet | 5ee8681 | 2015-06-08 16:04:45 +0200 | [diff] [blame] | 276 | if (!f) { |
| 277 | std::cerr << "error: cannot open file " << fname << "\n"; |
| 278 | return false; |
| 279 | } |
| 280 | char buf[1024]; |
| 281 | (*str) = ""; |
| 282 | while (!feof(f)) { |
| 283 | if (fgets(buf, 1024, f)) { |
| 284 | str->append(buf); |
| 285 | } |
| 286 | } |
| 287 | fclose(f); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 292 | replacements.push_back(Replacer(search, replace)); |
| 293 | |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | static int |
| 299 | command(int argc, char *argv[]) |
| 300 | { |
| 301 | Replacements replacements; |
| 302 | std::string outFileName; |
| 303 | |
| 304 | int opt; |
| 305 | while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { |
| 306 | switch (opt) { |
| 307 | case 'h': |
| 308 | usage(); |
| 309 | return 0; |
| 310 | case 'o': |
| 311 | outFileName = optarg; |
| 312 | break; |
| 313 | case 'e': |
| 314 | if (!parseSubstOpt(replacements, optarg)) { |
Arthur Huillet | 5ee8681 | 2015-06-08 16:04:45 +0200 | [diff] [blame] | 315 | std::cerr << "error: invalid replacement pattern `" << optarg << "`\n"; |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 316 | } |
| 317 | break; |
| 318 | default: |
José Fonseca | 4ce88b8 | 2013-10-11 17:24:47 -0700 | [diff] [blame] | 319 | std::cerr << "error: unexpected option `" << (char)opt << "`\n"; |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 320 | usage(); |
| 321 | return 1; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (optind >= argc) { |
| 326 | std::cerr << "error: apitrace sed requires a trace file as an argument.\n"; |
| 327 | usage(); |
| 328 | return 1; |
| 329 | } |
| 330 | |
| 331 | if (argc > optind + 1) { |
| 332 | std::cerr << "error: extraneous arguments:"; |
| 333 | for (int i = optind + 1; i < argc; i++) { |
| 334 | std::cerr << " " << argv[i]; |
| 335 | } |
| 336 | std::cerr << "\n"; |
| 337 | usage(); |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | return sed_trace(replacements, argv[optind], outFileName); |
| 342 | } |
| 343 | |
| 344 | |
| 345 | const Command sed_command = { |
| 346 | "sed", |
| 347 | synopsis, |
| 348 | usage, |
| 349 | command |
| 350 | }; |