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 **************************************************************************/
28 #include <limits.h> // for CHAR_MAX
33 #include "os_binary.hpp"
36 #include "cli_pager.hpp"
38 #include "trace_parser.hpp"
39 #include "trace_model.hpp"
40 #include "trace_callset.hpp"
43 using namespace trace;
46 class PickleVisitor : public trace::Visitor
53 PickleVisitor(PickleWriter &_writer, bool _symbolic) :
58 void visit(Null *node) {
62 void visit(Bool *node) {
63 writer.writeBool(node->value);
66 void visit(SInt *node) {
67 writer.writeInt(node->value);
70 void visit(UInt *node) {
71 writer.writeInt(node->value);
74 void visit(Float *node) {
75 writer.writeFloat(node->value);
78 void visit(Double *node) {
79 writer.writeFloat(node->value);
82 void visit(String *node) {
83 writer.writeString(node->value);
86 void visit(Enum *node) {
88 const EnumValue *it = node->lookup();
90 writer.writeString(it->name);
94 writer.writeInt(node->value);
97 void visit(Bitmask *node) {
99 unsigned long long value = node->value;
100 const BitmaskSig *sig = node->sig;
102 for (const BitmaskFlag *it = sig->flags; it != sig->flags + sig->num_flags; ++it) {
103 if ((it->value && (value & it->value) == it->value) ||
104 (!it->value && value == 0)) {
105 writer.writeString(it->name);
113 writer.writeInt(value);
117 writer.writeInt(node->value);
121 void visit(Struct *node) {
124 for (unsigned i = 0; i < node->sig->num_members; ++i) {
125 writer.beginItem(node->sig->member_names[i]);
126 _visit(node->members[i]);
132 for (unsigned i = 0; i < node->sig->num_members; ++i) {
133 _visit(node->members[i]);
139 void visit(Array *node) {
141 for (std::vector<Value *>::iterator it = node->values.begin(); it != node->values.end(); ++it) {
147 void visit(Blob *node) {
148 writer.writeByteArray(node->buf, node->size);
151 void visit(Pointer *node) {
152 writer.writeInt(node->value);
155 void visit(Repr *r) {
157 _visit(r->humanValue);
159 _visit(r->machineValue);
163 void visit(Call *call) {
166 writer.writeInt(call->no);
168 writer.writeString(call->name());
171 for (unsigned i = 0; i < call->args.size(); ++i) {
172 if (call->args[i].value) {
173 _visit(call->args[i].value);
191 static trace::CallSet calls(trace::FREQUENCY_ALL);
193 static const char *synopsis = "Pickle given trace(s) to standard output.";
199 << "usage: apitrace pickle [OPTIONS] TRACE_FILE...\n"
202 " -h, --help show this help message and exit\n"
203 " -s, --symbolic dump symbolic names\n"
204 " --calls=CALLSET only dump specified calls\n"
209 CALLS_OPT = CHAR_MAX + 1,
215 const static struct option
217 {"help", no_argument, 0, 'h'},
218 {"symbolic", no_argument, 0, 's'},
219 {"calls", required_argument, 0, CALLS_OPT},
224 command(int argc, char *argv[])
229 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
238 calls = trace::CallSet(optarg);
241 std::cerr << "error: unexpected option `" << opt << "`\n";
247 os::setBinaryMode(stdout);
249 std::cout.sync_with_stdio(false);
251 PickleWriter writer(std::cout);
252 PickleVisitor visitor(writer, symbolic);
254 for (int i = optind; i < argc; ++i) {
255 trace::Parser parser;
257 if (!parser.open(argv[i])) {
262 while ((call = parser.parse_call())) {
263 if (calls.contains(*call)) {
275 const Command pickle_command = {