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 | |
| 109 | void visit(Enum *node) { |
| 110 | const EnumValue *it = node->lookup(); |
| 111 | if (it) { |
| 112 | if (searchName.compare(it->name) == 0) { |
| 113 | const EnumSig *sig = node->sig; |
| 114 | for (unsigned i = 0; i < sig->num_values; ++i) { |
| 115 | if (replaceName.compare(sig->values[i].name) == 0) { |
| 116 | node->value = sig->values[i].value; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void visit(Bitmask *bitmask) { |
| 125 | } |
| 126 | |
| 127 | void visit(Struct *s) { |
| 128 | for (unsigned i = 0; i < s->members.size(); ++i) { |
| 129 | Value *memberValue = s->members[i]; |
| 130 | _visit(memberValue); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void visit(Array *array) { |
| 135 | for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) { |
| 136 | _visit(*it); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void visit(Blob *blob) { |
| 141 | } |
| 142 | |
| 143 | void visit(Pointer *p) { |
| 144 | } |
| 145 | |
| 146 | void visit(Repr *r) { |
| 147 | _visit(r->humanValue); |
| 148 | } |
| 149 | |
| 150 | void visit(Call *call) { |
| 151 | for (unsigned i = 0; i < call->args.size(); ++i) { |
| 152 | if (call->args[i].value) { |
| 153 | _visit(call->args[i].value); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (call->ret) { |
| 158 | _visit(call->ret); |
| 159 | } |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | |
| 164 | typedef std::list<Replacer> Replacements; |
| 165 | |
| 166 | |
| 167 | static int |
| 168 | sed_trace(Replacements &replacements, const char *inFileName, std::string &outFileName) |
| 169 | { |
| 170 | trace::Parser p; |
| 171 | |
| 172 | if (!p.open(inFileName)) { |
| 173 | std::cerr << "error: failed to open " << inFileName << "\n"; |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | /* Prepare outFileName file and writer for outFileName. */ |
| 178 | if (outFileName.empty()) { |
| 179 | os::String base(inFileName); |
| 180 | base.trimExtension(); |
| 181 | |
| 182 | outFileName = std::string(base.str()) + std::string("-sed.trace"); |
| 183 | } |
| 184 | |
| 185 | trace::Writer writer; |
| 186 | if (!writer.open(outFileName.c_str())) { |
| 187 | std::cerr << "error: failed to create " << outFileName << "\n"; |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | trace::Call *call; |
| 192 | while ((call = p.parse_call())) { |
| 193 | |
| 194 | for (Replacements::iterator it = replacements.begin(); it != replacements.end(); ++it) { |
| 195 | it->visit(call); |
| 196 | } |
| 197 | |
| 198 | writer.writeCall(call); |
| 199 | |
| 200 | delete call; |
| 201 | } |
| 202 | |
José Fonseca | 8cf6307 | 2013-07-25 20:18:58 +0100 | [diff] [blame^] | 203 | std::cerr << "Edited trace is available as " << outFileName << "\n"; |
José Fonseca | 084fe92 | 2013-07-09 16:35:14 +0100 | [diff] [blame] | 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * Parse a string in the format "s/SEARCH/REPLACE/". |
| 211 | */ |
| 212 | static bool |
| 213 | parseSubstOpt(Replacements &replacements, const char *opt) |
| 214 | { |
| 215 | if (*opt++ != 's') { |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | if (*opt++ != '/') { |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | // Parse the search pattern |
| 224 | const char *search_begin = opt; |
| 225 | while (*opt != '/') { |
| 226 | if (*opt == 0) { |
| 227 | return false; |
| 228 | } |
| 229 | ++opt; |
| 230 | } |
| 231 | const char *search_end = opt++; |
| 232 | |
| 233 | // Parse the replace pattern |
| 234 | const char *replace_begin = opt; |
| 235 | while (*opt != '/') { |
| 236 | if (*opt == 0) { |
| 237 | return false; |
| 238 | } |
| 239 | ++opt; |
| 240 | } |
| 241 | const char *replace_end = opt++; |
| 242 | |
| 243 | if (*opt != 0) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | std::string search(search_begin, search_end); |
| 248 | std::string replace(replace_begin, replace_end); |
| 249 | |
| 250 | replacements.push_back(Replacer(search, replace)); |
| 251 | |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | static int |
| 257 | command(int argc, char *argv[]) |
| 258 | { |
| 259 | Replacements replacements; |
| 260 | std::string outFileName; |
| 261 | |
| 262 | int opt; |
| 263 | while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { |
| 264 | switch (opt) { |
| 265 | case 'h': |
| 266 | usage(); |
| 267 | return 0; |
| 268 | case 'o': |
| 269 | outFileName = optarg; |
| 270 | break; |
| 271 | case 'e': |
| 272 | if (!parseSubstOpt(replacements, optarg)) { |
| 273 | std::cerr << "error: invalid replacement patter `" << optarg << "`\n"; |
| 274 | } |
| 275 | break; |
| 276 | default: |
| 277 | std::cerr << "error: unexpected option `" << opt << "`\n"; |
| 278 | usage(); |
| 279 | return 1; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if (optind >= argc) { |
| 284 | std::cerr << "error: apitrace sed requires a trace file as an argument.\n"; |
| 285 | usage(); |
| 286 | return 1; |
| 287 | } |
| 288 | |
| 289 | if (argc > optind + 1) { |
| 290 | std::cerr << "error: extraneous arguments:"; |
| 291 | for (int i = optind + 1; i < argc; i++) { |
| 292 | std::cerr << " " << argv[i]; |
| 293 | } |
| 294 | std::cerr << "\n"; |
| 295 | usage(); |
| 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | return sed_trace(replacements, argv[optind], outFileName); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | const Command sed_command = { |
| 304 | "sed", |
| 305 | synopsis, |
| 306 | usage, |
| 307 | command |
| 308 | }; |