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 **************************************************************************/
32 #include "cli_pager.hpp"
34 #include "trace_parser.hpp"
35 #include "trace_model.hpp"
36 #include "trace_callset.hpp"
39 using namespace trace;
42 class PickleVisitor : public trace::Visitor
48 PickleVisitor(PickleWriter &_writer) :
52 void visit(Null *node) {
56 void visit(Bool *node) {
57 writer.writeBool(node->value);
60 void visit(SInt *node) {
61 writer.writeInt(node->value);
64 void visit(UInt *node) {
65 writer.writeInt(node->value);
68 void visit(Float *node) {
69 writer.writeFloat(node->value);
72 void visit(Double *node) {
73 writer.writeFloat(node->value);
76 void visit(String *node) {
77 writer.writeString(node->value);
80 void visit(Enum *node) {
81 // TODO: keep symbolic name
82 writer.writeInt(node->value);
85 void visit(Bitmask *node) {
86 // TODO: keep symbolic name
87 writer.writeInt(node->value);
90 void visit(Struct *node) {
92 for (unsigned i = 0; i < node->sig->num_members; ++i) {
93 writer.beginItem(node->sig->member_names[i]);
94 _visit(node->members[i]);
100 void visit(Array *node) {
102 for (std::vector<Value *>::iterator it = node->values.begin(); it != node->values.end(); ++it) {
108 void visit(Blob *node) {
109 writer.writeString((const char *)node->buf, node->size);
112 void visit(Pointer *node) {
113 writer.writeInt(node->value);
116 void visit(Call *call) {
119 writer.writeInt(call->no);
121 writer.writeString(call->name());
124 for (unsigned i = 0; i < call->args.size(); ++i) {
126 _visit(call->args[i]);
144 static trace::CallSet calls(trace::FREQUENCY_ALL);
146 static const char *synopsis = "Pickle given trace(s) to standard output.";
152 << "usage: apitrace pickle [OPTIONS] <trace-file>...\n"
155 " --calls <CALLSET> Only pickle specified calls\n"
160 command(int argc, char *argv[])
164 for (i = 0; i < argc;) {
165 const char *arg = argv[i];
173 if (!strcmp(arg, "--")) {
175 } else if (!strcmp(arg, "--help")) {
178 } else if (!strcmp(arg, "--calls")) {
179 calls = trace::CallSet(argv[i++]);
181 std::cerr << "error: unknown option " << arg << "\n";
188 for (; i < argc; ++i) {
189 trace::Parser parser;
191 if (!parser.open(argv[i])) {
192 std::cerr << "error: failed to open " << argv[i] << "\n";
197 while ((call = parser.parse_call())) {
198 if (calls.contains(*call)) {
199 PickleWriter writer(std::cout);
200 PickleVisitor visitor(writer);
211 const Command pickle_command = {