]> git.cworth.org Git - apitrace/commitdiff
Add "apitrace trim" command.
authorCarl Worth <cworth@cworth.org>
Mon, 14 Nov 2011 22:50:07 +0000 (14:50 -0800)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Fri, 27 Jan 2012 21:56:39 +0000 (21:56 +0000)
This adds the functionality to read in a trace and create a new
"trimmed" trace as the output. There's not yet any functionality for
selecting pieces to trim out, so the implementation is currently just
a trace copier, (which is also the name of the new supporting class).

CMakeLists.txt
cli/CMakeLists.txt
cli/cli.hpp
cli/cli_main.cpp
cli/cli_trim.cpp [new file with mode: 0644]
common/trace_copier.cpp [new file with mode: 0644]
common/trace_copier.hpp [new file with mode: 0644]

index 22f7677570f9a8c0be9ca6d70ff500392a77fd0b..538ca8e80f08fc8ab837c9d29cd569c8a3f9f7b4 100755 (executable)
@@ -278,6 +278,7 @@ add_library (common STATIC
     common/trace_file_write.cpp
     common/trace_file_zlib.cpp
     common/trace_file_snappy.cpp
+    common/trace_copier.cpp
     common/trace_model.cpp
     common/trace_parser.cpp
     common/trace_parser_flags.cpp
index 17d4ffdce255992be54f219a5774e3cc3894d58d..b5c6b6c3a001ef452faa90dc7708d7f273f06f71 100644 (file)
@@ -8,6 +8,7 @@ add_executable (apitrace
     cli_pickle.cpp
     cli_repack.cpp
     cli_trace.cpp
+    cli_trim.cpp
 )
 
 install (TARGETS apitrace RUNTIME DESTINATION bin)
index fa72fc7809be573ea7535ad2f10efb4a1a0685b8..6c080e86de65683883d520661ecc728ac8a1ae2a 100644 (file)
@@ -47,5 +47,6 @@ extern const Command dump_command;
 extern const Command pickle_command;
 extern const Command repack_command;
 extern const Command trace_command;
+extern const Command trim_command;
 
 #endif /* _APITRACE_CLI_HPP_ */
index 9080a4cb68a5f6b3dcdf07b8ca89a347334f2bcf..a776107fd12a13c6ae2a01cff510245b096af115 100644 (file)
@@ -73,6 +73,7 @@ static const Command * commands[] = {
     &pickle_command,
     &repack_command,
     &trace_command,
+    &trim_command,
     &help_command
 };
 
diff --git a/cli/cli_trim.cpp b/cli/cli_trim.cpp
new file mode 100644 (file)
index 0000000..b35e144
--- /dev/null
@@ -0,0 +1,106 @@
+/**************************************************************************
+ *
+ * Copyright 2010 VMware, Inc.
+ * Copyright 2011 Intel corporation
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+#include <string.h>
+
+#include "cli.hpp"
+
+#include "os_string.hpp"
+
+#include "trace_parser.hpp"
+#include "trace_copier.hpp"
+
+static const char *synopsis = "Create a new trace by trimming an existing trace.";
+
+static void
+usage(void)
+{
+    std::cout
+        << "usage: apitrace trim <trace-file>\n"
+        << synopsis << "\n";
+}
+
+static int
+command(int argc, char *argv[])
+{
+    int i;
+
+    for (i = 0; i < argc; ++i) {
+        const char *arg = argv[i];
+
+        if (arg[0] != '-') {
+            break;
+        }
+
+        if (!strcmp(arg, "--")) {
+            break;
+        } else if (!strcmp(arg, "--help")) {
+            usage();
+            return 0;
+        } else {
+            std::cerr << "error: unknown option " << arg << "\n";
+            usage();
+            return 1;
+        }
+    }
+
+    if (i >= argc) {
+        std::cerr << "Error: apitrace trim requires a trace file as an argument.\n";
+        usage();
+        return 1;
+    }
+
+    trace::Parser p;
+
+    if (!p.open(argv[i])) {
+        std::cerr << "error: failed to open " << argv[i] << "\n";
+        return 1;
+    }
+
+    os::String base(argv[i]);
+    base.trimExtension();
+
+    std::string output = std::string(base.str()) + std::string("-trim.trace");
+
+    trace::Copier copier(output.c_str());
+
+    trace::Call *call;
+    while ((call = p.parse_call())) {
+        copier.visit(call);
+        delete call;
+    }
+
+    std::cout << "Trimmed trace is available as " << output << "\n";
+
+    return 0;
+}
+
+const Command trim_command = {
+    "trim",
+    synopsis,
+    usage,
+    command
+};
diff --git a/common/trace_copier.cpp b/common/trace_copier.cpp
new file mode 100644 (file)
index 0000000..89aa97f
--- /dev/null
@@ -0,0 +1,121 @@
+/*********************************************************************
+ *
+ * Copyright 2010 VMware, Inc.
+ * Copyright 2011 Intel Corporation
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ *********************************************************************/
+
+#include "trace_copier.hpp"
+
+namespace trace {
+
+Copier::Copier(const char *filename) : writer() {
+    writer.open(filename);
+}
+
+Copier::~Copier() {
+    writer.close();
+}
+
+void Copier::visit(Null *) {
+    writer.writeNull();
+}
+
+void Copier::visit(Bool *node) {
+    writer.writeBool(node->value);
+}
+
+void Copier::visit(SInt *node) {
+    writer.writeSInt(node->value);
+}
+
+void Copier::visit(UInt *node) {
+    writer.writeUInt(node->value);
+}
+
+void Copier::visit(Float *node) {
+    writer.writeFloat(node->value);
+}
+
+void Copier::visit(Double *node) {
+    writer.writeDouble(node->value);
+}
+
+void Copier::visit(String *node) {
+    writer.writeString(node->value);
+}
+
+void Copier::visit(Enum *node) {
+    writer.writeEnum(node->sig);
+}
+
+void Copier::visit(Bitmask *bitmask) {
+    writer.writeBitmask(bitmask->sig, bitmask->value);
+}
+
+void Copier::visit(Struct *s) {
+    writer.beginStruct(s->sig);
+    for (unsigned i = 0; i < s->members.size(); ++i) {
+        _visit(s->members[i]);
+    }
+}
+
+void Copier::visit(Array *array) {
+    if (array->values.size()) {
+        writer.beginArray(array->values.size());
+        for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
+            _visit(*it);
+        }
+        writer.endArray();
+    } else {
+        writer.writeNull();
+    }
+}
+
+void Copier::visit(Blob *blob) {
+    writer.writeBlob(blob->buf, blob->size);
+}
+
+void Copier::visit(Pointer *p) {
+    writer.writeOpaque((void *)p->value);
+}
+
+void Copier::visit(Call *call) {
+    unsigned __call = writer.beginEnter(call->sig);
+    for (unsigned i = 0; i < call->args.size(); ++i) {
+        writer.beginArg(i);
+        _visit(call->args[i]);
+        writer.endArg();
+    }
+    writer.endEnter();
+    writer.beginLeave(__call);
+    if (call->ret) {
+        writer.beginReturn();
+        _visit(call->ret);
+        writer.endReturn();
+    }
+    writer.endLeave();
+}
+
+}
diff --git a/common/trace_copier.hpp b/common/trace_copier.hpp
new file mode 100644 (file)
index 0000000..8df50f8
--- /dev/null
@@ -0,0 +1,62 @@
+/*********************************************************************
+ *
+ * Copyright 2011 Intel Corporation
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ *********************************************************************/
+
+#ifndef _TRACE_COPIER_HPP_
+#define _TRACE_COPIER_HPP_
+
+#include "trace_writer.hpp"
+
+
+namespace trace {
+
+class Copier: public Visitor
+{
+protected:
+    trace::Writer writer;
+
+public:
+    Copier(const char *);
+    ~Copier();
+    virtual void visit(Null *);
+    virtual void visit(Bool *);
+    virtual void visit(SInt *);
+    virtual void visit(UInt *);
+    virtual void visit(Float *);
+    virtual void visit(Double *);
+    virtual void visit(String *);
+    virtual void visit(Enum *);
+    virtual void visit(Bitmask *);
+    virtual void visit(Struct *);
+    virtual void visit(Array *);
+    virtual void visit(Blob *);
+    virtual void visit(Pointer *);
+    virtual void visit(Call *call);
+};
+
+}
+
+#endif /* _TRACE_COPIER_HPP_ */