1 /**************************************************************************
3 * Copyright 2013 VMware, Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
27 #include <limits.h> // for CHAR_MAX
32 #include "os_string.hpp"
34 #include "trace_parser.hpp"
35 #include "trace_writer.hpp"
38 static const char *synopsis = "Stream editing of a trace.";
45 << "usage: apitrace sed [OPTIONS] TRACE_FILE...\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"
57 shortOptions = "ho:e:";
59 const static struct option
61 {"help", no_argument, 0, 'h'},
62 {"output", required_argument, 0, 'o'},
66 using namespace trace;
70 * A visitor that replaces symbol constants.
72 class Replacer : public Visitor
75 std::string searchName;
76 std::string replaceName;
79 Replacer(const std::string & _searchName, const std::string & _replaceName) :
80 searchName(_searchName),
81 replaceName(_replaceName)
91 void visit(Bool *node) {
94 void visit(SInt *node) {
97 void visit(UInt *node) {
100 void visit(Float *node) {
103 void visit(Double *node) {
106 void visit(String *node) {
109 void visit(Enum *node) {
110 const EnumValue *it = node->lookup();
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;
124 void visit(Bitmask *bitmask) {
127 void visit(Struct *s) {
128 for (unsigned i = 0; i < s->members.size(); ++i) {
129 Value *memberValue = s->members[i];
134 void visit(Array *array) {
135 for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
140 void visit(Blob *blob) {
143 void visit(Pointer *p) {
146 void visit(Repr *r) {
147 _visit(r->humanValue);
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);
164 typedef std::list<Replacer> Replacements;
168 sed_trace(Replacements &replacements, const char *inFileName, std::string &outFileName)
172 if (!p.open(inFileName)) {
173 std::cerr << "error: failed to open " << inFileName << "\n";
177 /* Prepare outFileName file and writer for outFileName. */
178 if (outFileName.empty()) {
179 os::String base(inFileName);
180 base.trimExtension();
182 outFileName = std::string(base.str()) + std::string("-sed.trace");
185 trace::Writer writer;
186 if (!writer.open(outFileName.c_str())) {
187 std::cerr << "error: failed to create " << outFileName << "\n";
192 while ((call = p.parse_call())) {
194 for (Replacements::iterator it = replacements.begin(); it != replacements.end(); ++it) {
198 writer.writeCall(call);
203 std::cerr << "Edited trace is available as " << outFileName << "\n";
210 * Parse a string in the format "s/SEARCH/REPLACE/".
213 parseSubstOpt(Replacements &replacements, const char *opt)
223 // Parse the search pattern
224 const char *search_begin = opt;
225 while (*opt != '/') {
231 const char *search_end = opt++;
233 // Parse the replace pattern
234 const char *replace_begin = opt;
235 while (*opt != '/') {
241 const char *replace_end = opt++;
247 std::string search(search_begin, search_end);
248 std::string replace(replace_begin, replace_end);
250 replacements.push_back(Replacer(search, replace));
257 command(int argc, char *argv[])
259 Replacements replacements;
260 std::string outFileName;
263 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
269 outFileName = optarg;
272 if (!parseSubstOpt(replacements, optarg)) {
273 std::cerr << "error: invalid replacement patter `" << optarg << "`\n";
277 std::cerr << "error: unexpected option `" << opt << "`\n";
283 if (optind >= argc) {
284 std::cerr << "error: apitrace sed requires a trace file as an argument.\n";
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];
299 return sed_trace(replacements, argv[optind], outFileName);
303 const Command sed_command = {