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