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" |
| 49 | " -e s/SEARCH/REPLACE/ Search and replace a symbol.\n" |
| 50 | " XXX: Only works for enums.\n" |
| 51 | " -o, --output=TRACE_FILE Output trace file\n" |
| 52 | ; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | const static char * |
| 57 | shortOptions = "ho:e:"; |
| 58 | |
| 59 | const static struct option |
| 60 | longOptions[] = { |
| 61 | {"help", no_argument, 0, 'h'}, |
| 62 | {"output", required_argument, 0, 'o'}, |
| 63 | {0, 0, 0, 0} |
| 64 | }; |
| 65 | |
| 66 | using namespace trace; |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * A visitor that replaces symbol constants. |
| 71 | */ |
| 72 | class Replacer : public Visitor |
| 73 | { |
| 74 | protected: |
| 75 | std::string searchName; |
| 76 | std::string replaceName; |
| 77 | |
| 78 | public: |
| 79 | Replacer(const std::string & _searchName, const std::string & _replaceName) : |
| 80 | searchName(_searchName), |
| 81 | replaceName(_replaceName) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | ~Replacer() { |
| 86 | } |
| 87 | |
| 88 | void visit(Null *) { |
| 89 | } |
| 90 | |
| 91 | void visit(Bool *node) { |
| 92 | } |
| 93 | |
| 94 | void visit(SInt *node) { |
| 95 | } |
| 96 | |
| 97 | void visit(UInt *node) { |
| 98 | } |
| 99 | |
| 100 | void visit(Float *node) { |
| 101 | } |
| 102 | |
| 103 | void visit(Double *node) { |
| 104 | } |
| 105 | |
| 106 | void visit(String *node) { |
| 107 | } |
| 108 | |
José Fonseca | d5cda7c | 2014-09-25 15:19:09 +0100 | [diff] [blame^] | 109 | void visit(WString *node) { |
| 110 | } |
| 111 | |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 112 | void visit(Enum *node) { |
| 113 | const EnumValue *it = node->lookup(); |
| 114 | if (it) { |
| 115 | if (searchName.compare(it->name) == 0) { |
| 116 | const EnumSig *sig = node->sig; |
| 117 | for (unsigned i = 0; i < sig->num_values; ++i) { |
| 118 | if (replaceName.compare(sig->values[i].name) == 0) { |
| 119 | node->value = sig->values[i].value; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void visit(Bitmask *bitmask) { |
| 128 | } |
| 129 | |
| 130 | void visit(Struct *s) { |
| 131 | for (unsigned i = 0; i < s->members.size(); ++i) { |
| 132 | Value *memberValue = s->members[i]; |
| 133 | _visit(memberValue); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void visit(Array *array) { |
| 138 | for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) { |
| 139 | _visit(*it); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void visit(Blob *blob) { |
| 144 | } |
| 145 | |
| 146 | void visit(Pointer *p) { |
| 147 | } |
| 148 | |
| 149 | void visit(Repr *r) { |
| 150 | _visit(r->humanValue); |
| 151 | } |
| 152 | |
| 153 | void visit(Call *call) { |
| 154 | for (unsigned i = 0; i < call->args.size(); ++i) { |
| 155 | if (call->args[i].value) { |
| 156 | _visit(call->args[i].value); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (call->ret) { |
| 161 | _visit(call->ret); |
| 162 | } |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | |
| 167 | typedef std::list<Replacer> Replacements; |
| 168 | |
| 169 | |
| 170 | static int |
| 171 | sed_trace(Replacements &replacements, const char *inFileName, std::string &outFileName) |
| 172 | { |
| 173 | trace::Parser p; |
| 174 | |
| 175 | if (!p.open(inFileName)) { |
| 176 | std::cerr << "error: failed to open " << inFileName << "\n"; |
| 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | /* Prepare outFileName file and writer for outFileName. */ |
| 181 | if (outFileName.empty()) { |
| 182 | os::String base(inFileName); |
| 183 | base.trimExtension(); |
| 184 | |
| 185 | outFileName = std::string(base.str()) + std::string("-sed.trace"); |
| 186 | } |
| 187 | |
| 188 | trace::Writer writer; |
| 189 | if (!writer.open(outFileName.c_str())) { |
| 190 | std::cerr << "error: failed to create " << outFileName << "\n"; |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | trace::Call *call; |
| 195 | while ((call = p.parse_call())) { |
| 196 | |
| 197 | for (Replacements::iterator it = replacements.begin(); it != replacements.end(); ++it) { |
| 198 | it->visit(call); |
| 199 | } |
| 200 | |
| 201 | writer.writeCall(call); |
| 202 | |
| 203 | delete call; |
| 204 | } |
| 205 | |
José Fonseca | 8cf6307 | 2013-07-25 20:18:58 +0100 | [diff] [blame] | 206 | std::cerr << "Edited trace is available as " << outFileName << "\n"; |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /** |
| 213 | * Parse a string in the format "s/SEARCH/REPLACE/". |
| 214 | */ |
| 215 | static bool |
| 216 | parseSubstOpt(Replacements &replacements, const char *opt) |
| 217 | { |
| 218 | if (*opt++ != 's') { |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | if (*opt++ != '/') { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | // Parse the search pattern |
| 227 | const char *search_begin = opt; |
| 228 | while (*opt != '/') { |
| 229 | if (*opt == 0) { |
| 230 | return false; |
| 231 | } |
| 232 | ++opt; |
| 233 | } |
| 234 | const char *search_end = opt++; |
| 235 | |
| 236 | // Parse the replace pattern |
| 237 | const char *replace_begin = opt; |
| 238 | while (*opt != '/') { |
| 239 | if (*opt == 0) { |
| 240 | return false; |
| 241 | } |
| 242 | ++opt; |
| 243 | } |
| 244 | const char *replace_end = opt++; |
| 245 | |
| 246 | if (*opt != 0) { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | std::string search(search_begin, search_end); |
| 251 | std::string replace(replace_begin, replace_end); |
| 252 | |
| 253 | replacements.push_back(Replacer(search, replace)); |
| 254 | |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | static int |
| 260 | command(int argc, char *argv[]) |
| 261 | { |
| 262 | Replacements replacements; |
| 263 | std::string outFileName; |
| 264 | |
| 265 | int opt; |
| 266 | while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { |
| 267 | switch (opt) { |
| 268 | case 'h': |
| 269 | usage(); |
| 270 | return 0; |
| 271 | case 'o': |
| 272 | outFileName = optarg; |
| 273 | break; |
| 274 | case 'e': |
| 275 | if (!parseSubstOpt(replacements, optarg)) { |
| 276 | std::cerr << "error: invalid replacement patter `" << optarg << "`\n"; |
| 277 | } |
| 278 | break; |
| 279 | default: |
José Fonseca | 4ce88b8 | 2013-10-11 17:24:47 -0700 | [diff] [blame] | 280 | std::cerr << "error: unexpected option `" << (char)opt << "`\n"; |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 281 | usage(); |
| 282 | return 1; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | if (optind >= argc) { |
| 287 | std::cerr << "error: apitrace sed requires a trace file as an argument.\n"; |
| 288 | usage(); |
| 289 | return 1; |
| 290 | } |
| 291 | |
| 292 | if (argc > optind + 1) { |
| 293 | std::cerr << "error: extraneous arguments:"; |
| 294 | for (int i = optind + 1; i < argc; i++) { |
| 295 | std::cerr << " " << argv[i]; |
| 296 | } |
| 297 | std::cerr << "\n"; |
| 298 | usage(); |
| 299 | return 1; |
| 300 | } |
| 301 | |
| 302 | return sed_trace(replacements, argv[optind], outFileName); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | const Command sed_command = { |
| 307 | "sed", |
| 308 | synopsis, |
| 309 | usage, |
| 310 | command |
| 311 | }; |