]> git.cworth.org Git - apitrace/blob - cli/cli_pickle.cpp
Set binary mode when outputing PNM from glretrace via stdout.
[apitrace] / cli / cli_pickle.cpp
1 /**************************************************************************
2  *
3  * Copyright 2012 Jose Fonseca
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <string.h>
28
29 #include "pickle.hpp"
30
31 #include "os_binary.hpp"
32
33 #include "cli.hpp"
34 #include "cli_pager.hpp"
35
36 #include "trace_parser.hpp"
37 #include "trace_model.hpp"
38 #include "trace_callset.hpp"
39
40
41 using namespace trace;
42
43
44 class PickleVisitor : public trace::Visitor
45 {
46 protected:
47     PickleWriter &writer;
48
49 public:
50     PickleVisitor(PickleWriter &_writer) :
51         writer(_writer) {
52     }
53
54     void visit(Null *node) {
55         writer.writeInt(0);
56     }
57
58     void visit(Bool *node) {
59         writer.writeBool(node->value);
60     }
61
62     void visit(SInt *node) {
63         writer.writeInt(node->value);
64     }
65
66     void visit(UInt *node) {
67         writer.writeInt(node->value);
68     }
69
70     void visit(Float *node) {
71         writer.writeFloat(node->value);
72     }
73
74     void visit(Double *node) {
75         writer.writeFloat(node->value);
76     }
77
78     void visit(String *node) {
79         writer.writeString(node->value);
80     }
81
82     void visit(Enum *node) {
83         // TODO: keep symbolic name
84         writer.writeInt(node->value);
85     }
86
87     void visit(Bitmask *node) {
88         // TODO: keep symbolic name
89         writer.writeInt(node->value);
90     }
91
92     void visit(Struct *node) {
93         writer.beginDict();
94         for (unsigned i = 0; i < node->sig->num_members; ++i) {
95             writer.beginItem(node->sig->member_names[i]);
96             _visit(node->members[i]);
97             writer.endItem();
98         }
99         writer.endDict();
100     }
101
102     void visit(Array *node) {
103         writer.beginList();
104         for (std::vector<Value *>::iterator it = node->values.begin(); it != node->values.end(); ++it) {
105             _visit(*it);
106         }
107         writer.endList();
108     }
109
110     void visit(Blob *node) {
111         writer.writeString((const char *)node->buf, node->size);
112     }
113
114     void visit(Pointer *node) {
115         writer.writeInt(node->value);
116     }
117
118     void visit(Call *call) {
119         writer.beginTuple();
120
121         writer.writeInt(call->no);
122
123         writer.writeString(call->name());
124
125         writer.beginList();
126         for (unsigned i = 0; i < call->args.size(); ++i) {
127             if (call->args[i].value) {
128                 _visit(call->args[i].value);
129             } else {
130                 writer.writeNone();
131             }
132         }
133         writer.endList();
134
135         if (call->ret) {
136             _visit(call->ret);
137         } else {
138             writer.writeNone();
139         }
140
141         writer.endTuple();
142     }
143 };
144
145
146 static trace::CallSet calls(trace::FREQUENCY_ALL);
147
148 static const char *synopsis = "Pickle given trace(s) to standard output.";
149
150 static void
151 usage(void)
152 {
153     std::cout
154         << "usage: apitrace pickle [OPTIONS] <trace-file>...\n"
155         << synopsis << "\n"
156         "\n"
157         "    --calls <CALLSET>   Only pickle specified calls\n"
158     ;
159 }
160
161 static int
162 command(int argc, char *argv[])
163 {
164     int i;
165
166     for (i = 1; i < argc;) {
167         const char *arg = argv[i];
168
169         if (arg[0] != '-') {
170             break;
171         }
172
173         ++i;
174
175         if (!strcmp(arg, "--")) {
176             break;
177         } else if (!strcmp(arg, "--help")) {
178             usage();
179             return 0;
180         } else if (!strcmp(arg, "--calls")) {
181             calls = trace::CallSet(argv[i++]);
182         } else {
183             std::cerr << "error: unknown option " << arg << "\n";
184             usage();
185             return 1;
186         }
187     }
188
189     os::setBinaryMode(stdout);
190
191     for (; i < argc; ++i) {
192         trace::Parser parser;
193
194         if (!parser.open(argv[i])) {
195             std::cerr << "error: failed to open " << argv[i] << "\n";
196             return 1;
197         }
198
199         trace::Call *call;
200         while ((call = parser.parse_call())) {
201             if (calls.contains(*call)) {
202                 PickleWriter writer(std::cout);
203                 PickleVisitor visitor(writer);
204                 
205                 visitor.visit(call);
206             }
207             delete call;
208         }
209     }
210
211     return 0;
212 }
213
214 const Command pickle_command = {
215     "pickle",
216     synopsis,
217     usage,
218     command
219 };