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 **************************************************************************/
37 #include "cli_pager.hpp"
39 #include "trace_parser.hpp"
40 #include "trace_model.hpp"
41 #include "trace_callset.hpp"
44 using namespace trace;
47 class PickleVisitor : public trace::Visitor
53 PickleVisitor(PickleWriter &_writer) :
57 void visit(Null *node) {
61 void visit(Bool *node) {
62 writer.writeBool(node->value);
65 void visit(SInt *node) {
66 writer.writeInt(node->value);
69 void visit(UInt *node) {
70 writer.writeInt(node->value);
73 void visit(Float *node) {
74 writer.writeFloat(node->value);
77 void visit(Double *node) {
78 writer.writeFloat(node->value);
81 void visit(String *node) {
82 writer.writeString(node->value);
85 void visit(Enum *node) {
86 // TODO: keep symbolic name
87 writer.writeInt(node->value);
90 void visit(Bitmask *node) {
91 // TODO: keep symbolic name
92 writer.writeInt(node->value);
95 void visit(Struct *node) {
97 for (unsigned i = 0; i < node->sig->num_members; ++i) {
98 writer.beginItem(node->sig->member_names[i]);
99 _visit(node->members[i]);
105 void visit(Array *node) {
107 for (std::vector<Value *>::iterator it = node->values.begin(); it != node->values.end(); ++it) {
113 void visit(Blob *node) {
114 writer.writeString((const char *)node->buf, node->size);
117 void visit(Pointer *node) {
118 writer.writeInt(node->value);
121 void visit(Call *call) {
124 writer.writeInt(call->no);
126 writer.writeString(call->name());
129 for (unsigned i = 0; i < call->args.size(); ++i) {
131 _visit(call->args[i]);
149 static trace::CallSet calls(trace::FREQUENCY_ALL);
151 static const char *synopsis = "Pickle given trace(s) to standard output.";
157 << "usage: apitrace pickle [OPTIONS] <trace-file>...\n"
160 " --calls <CALLSET> Only pickle specified calls\n"
165 command(int argc, char *argv[])
169 for (i = 0; i < argc;) {
170 const char *arg = argv[i];
178 if (!strcmp(arg, "--")) {
180 } else if (!strcmp(arg, "--help")) {
183 } else if (!strcmp(arg, "--calls")) {
184 calls = trace::CallSet(argv[i++]);
186 std::cerr << "error: unknown option " << arg << "\n";
193 // Set stdout in binary mode
195 int mode = _setmode(_fileno(stdout), _O_BINARY);
197 std::cerr << "warning: failed to set stdout in binary mode\n";
201 for (; i < argc; ++i) {
202 trace::Parser parser;
204 if (!parser.open(argv[i])) {
205 std::cerr << "error: failed to open " << argv[i] << "\n";
210 while ((call = parser.parse_call())) {
211 if (calls.contains(*call)) {
212 PickleWriter writer(std::cout);
213 PickleVisitor visitor(writer);
225 _setmode(_fileno(stdout), mode);
232 const Command pickle_command = {