1 /**************************************************************************
3 * Copyright 2012 Jose Fonseca
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 **************************************************************************/
31 #include "os_binary.hpp"
34 #include "cli_pager.hpp"
36 #include "trace_parser.hpp"
37 #include "trace_model.hpp"
38 #include "trace_callset.hpp"
41 using namespace trace;
44 class PickleVisitor : public trace::Visitor
50 PickleVisitor(PickleWriter &_writer) :
54 void visit(Null *node) {
58 void visit(Bool *node) {
59 writer.writeBool(node->value);
62 void visit(SInt *node) {
63 writer.writeInt(node->value);
66 void visit(UInt *node) {
67 writer.writeInt(node->value);
70 void visit(Float *node) {
71 writer.writeFloat(node->value);
74 void visit(Double *node) {
75 writer.writeFloat(node->value);
78 void visit(String *node) {
79 writer.writeString(node->value);
82 void visit(Enum *node) {
83 // TODO: keep symbolic name
84 writer.writeInt(node->value);
87 void visit(Bitmask *node) {
88 // TODO: keep symbolic name
89 writer.writeInt(node->value);
92 void visit(Struct *node) {
94 for (unsigned i = 0; i < node->sig->num_members; ++i) {
95 writer.beginItem(node->sig->member_names[i]);
96 _visit(node->members[i]);
102 void visit(Array *node) {
104 for (std::vector<Value *>::iterator it = node->values.begin(); it != node->values.end(); ++it) {
110 void visit(Blob *node) {
111 writer.writeString((const char *)node->buf, node->size);
114 void visit(Pointer *node) {
115 writer.writeInt(node->value);
118 void visit(Call *call) {
121 writer.writeInt(call->no);
123 writer.writeString(call->name());
126 for (unsigned i = 0; i < call->args.size(); ++i) {
127 if (call->args[i].value) {
128 _visit(call->args[i].value);
146 static trace::CallSet calls(trace::FREQUENCY_ALL);
148 static const char *synopsis = "Pickle given trace(s) to standard output.";
154 << "usage: apitrace pickle [OPTIONS] <trace-file>...\n"
157 " --calls <CALLSET> Only pickle specified calls\n"
162 command(int argc, char *argv[])
166 for (i = 1; i < argc;) {
167 const char *arg = argv[i];
175 if (!strcmp(arg, "--")) {
177 } else if (!strcmp(arg, "--help")) {
180 } else if (!strcmp(arg, "--calls")) {
181 calls = trace::CallSet(argv[i++]);
183 std::cerr << "error: unknown option " << arg << "\n";
189 os::setBinaryMode(stdout);
191 for (; i < argc; ++i) {
192 trace::Parser parser;
194 if (!parser.open(argv[i])) {
195 std::cerr << "error: failed to open " << argv[i] << "\n";
200 while ((call = parser.parse_call())) {
201 if (calls.contains(*call)) {
202 PickleWriter writer(std::cout);
203 PickleVisitor visitor(writer);
214 const Command pickle_command = {