]> git.cworth.org Git - apitrace/blob - trace_write.cpp
More compact struct representation.
[apitrace] / trace_write.cpp
1 /**************************************************************************
2  *
3  * Copyright 2007-2009 VMware, Inc.
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 <assert.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <map>
34
35 #include <zlib.h>
36
37 #include "os.hpp"
38 #include "trace_write.hpp"
39 #include "trace_format.hpp"
40
41
42 namespace Trace {
43
44
45 static gzFile g_gzFile = NULL;
46 static void _Close(void) {
47    if(g_gzFile != NULL) {
48       gzclose(g_gzFile);
49       g_gzFile = NULL;
50    }
51 }
52
53 static void _Open(const char *szExtension) {
54    _Close();
55    
56    static unsigned dwCounter = 0;
57
58    char szFileName[PATH_MAX];
59    const char *lpFileName;
60
61    lpFileName = getenv("TRACE_PATH");
62    if (lpFileName) {
63        strncpy(szFileName, lpFileName, PATH_MAX);
64    }
65    else {
66        char szProcessName[PATH_MAX];
67        char szCurrentDir[PATH_MAX];
68        OS::GetProcessName(szProcessName, PATH_MAX);
69        OS::GetCurrentDir(szCurrentDir, PATH_MAX);
70
71        for(;;) {
72           FILE *file;
73           
74           if (dwCounter)
75              snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
76           else
77              snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
78           
79           file = fopen(szFileName, "rb");
80           if(file == NULL)
81              break;
82           
83           fclose(file);
84           
85           ++dwCounter;
86        }
87    }
88
89    OS::DebugMessage("apitrace: tracing to %s\n", szFileName);
90
91    g_gzFile = gzopen(szFileName, "wb");
92 }
93
94 static inline void Write(const void *sBuffer, size_t dwBytesToWrite) {
95    if (g_gzFile == NULL)
96       return;
97    
98    gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
99 }
100
101 static inline void 
102 WriteByte(char c) {
103    Write(&c, 1);
104 }
105
106 void inline 
107 WriteUInt(unsigned long long value) {
108    char buf[2 * sizeof value];
109    unsigned len;
110
111    len = 0;
112    do {
113       assert(len < sizeof buf);
114       buf[len] = 0x80 | (value & 0x7f);
115       value >>= 7;
116       ++len;
117    } while (value);
118
119    assert(len);
120    buf[len - 1] &= 0x7f;
121
122    Write(buf, len);
123 }
124
125 static inline void 
126 WriteFloat(float value) {
127    assert(sizeof value == 4);
128    Write((const char *)&value, sizeof value);
129 }
130
131 static inline void 
132 WriteDouble(double value) {
133    assert(sizeof value == 8);
134    Write((const char *)&value, sizeof value);
135 }
136
137 static inline void 
138 WriteString(const char *str) {
139    size_t len = strlen(str);
140    WriteUInt(len);
141    Write(str, len);
142 }
143
144 typedef std::map<const char *, size_t> namemap;
145 static namemap names;
146
147 static inline void 
148 WriteName(const char *name) {
149    namemap::iterator it = names.find(name);
150    if (it == names.end()) {
151        size_t name_id = names.size();
152        WriteUInt(name_id);
153        WriteString(name);
154        names[name] = name_id;
155    } else {
156        WriteUInt(it->second);
157    }
158 }
159
160 void Open(void) {
161     if (!g_gzFile) {
162         _Open("trace");
163         WriteUInt(TRACE_VERSION);
164     }
165 }
166
167 void Close(void) {
168    _Close();
169 }
170
171 static unsigned call_no = 0;
172
173 static std::map<Id, bool> functions;
174 static std::map<Id, bool> structs;
175 static std::map<Id, bool> enums;
176 static std::map<Id, bool> bitmasks;
177
178
179 unsigned BeginEnter(const FunctionSig &function) {
180    OS::AcquireMutex();
181    Open();
182    WriteByte(Trace::EVENT_ENTER);
183    WriteUInt(function.id);
184    if (!functions[function.id]) {
185       WriteString(function.name);
186       WriteUInt(function.num_args);
187       for (unsigned i = 0; i < function.num_args; ++i) {
188          WriteString(function.args[i]);
189       }
190       functions[function.id] = true;
191    }
192    return call_no++;
193 }
194
195 void EndEnter(void) {
196    WriteByte(Trace::CALL_END);
197    gzflush(g_gzFile, Z_SYNC_FLUSH);
198    OS::ReleaseMutex();
199 }
200
201 void BeginLeave(unsigned call) {
202    OS::AcquireMutex();
203    WriteByte(Trace::EVENT_LEAVE);
204    WriteUInt(call);
205 }
206
207 void EndLeave(void) {
208    WriteByte(Trace::CALL_END);
209    gzflush(g_gzFile, Z_SYNC_FLUSH);
210    OS::ReleaseMutex();
211 }
212
213 void BeginArg(unsigned index) {
214    WriteByte(Trace::CALL_ARG);
215    WriteUInt(index);
216 }
217
218 void BeginReturn(void) {
219    WriteByte(Trace::CALL_RET);
220 }
221
222 void BeginArray(size_t length) {
223    WriteByte(Trace::TYPE_ARRAY);
224    WriteUInt(length);
225 }
226
227 void BeginStruct(const StructSig *sig) {
228    WriteByte(Trace::TYPE_STRUCT);
229    WriteUInt(sig->id);
230    if (!structs[sig->id]) {
231       WriteString(sig->name);
232       WriteUInt(sig->num_members);
233       for (unsigned i = 0; i < sig->num_members; ++i) {
234          WriteString(sig->members[i]);
235       }
236       structs[sig->id] = true;
237    }
238 }
239
240 void LiteralBool(bool value) {
241    WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
242 }
243
244 void LiteralSInt(signed long long value) {
245    if (value < 0) {
246       WriteByte(Trace::TYPE_SINT);
247       WriteUInt(-value);
248    } else {
249       WriteByte(Trace::TYPE_UINT);
250       WriteUInt(value);
251    }
252 }
253
254 void LiteralUInt(unsigned long long value) {
255    WriteByte(Trace::TYPE_UINT);
256    WriteUInt(value);
257 }
258
259 void LiteralFloat(float value) {
260    WriteByte(Trace::TYPE_FLOAT);
261    WriteFloat(value);
262 }
263
264 void LiteralFloat(double value) {
265    WriteByte(Trace::TYPE_DOUBLE);
266    WriteDouble(value);
267 }
268
269 void LiteralString(const char *str) {
270    if (!str) {
271       LiteralNull();
272       return;
273    }
274    WriteByte(Trace::TYPE_STRING);
275    WriteString(str);
276 }
277
278 void LiteralString(const char *str, size_t len) {
279    if (!str) {
280       LiteralNull();
281       return;
282    }
283    WriteByte(Trace::TYPE_STRING);
284    WriteUInt(len);
285    Write(str, len);
286 }
287
288 void LiteralWString(const wchar_t *str) {
289    if (!str) {
290       LiteralNull();
291       return;
292    }
293    WriteByte(Trace::TYPE_STRING);
294    WriteString("<wide-string>");
295 }
296    
297 void LiteralBlob(const void *data, size_t size) {
298    if (!data) {
299       LiteralNull();
300       return;
301    }
302    WriteByte(Trace::TYPE_BLOB);
303    WriteUInt(size);
304    if (size) {
305       Write(data, size);
306    }
307 }
308
309 void LiteralEnum(const EnumSig *sig) {
310    WriteByte(Trace::TYPE_ENUM);
311    WriteUInt(sig->id);
312    if (!enums[sig->id]) {
313       WriteString(sig->name);
314       LiteralSInt(sig->value);
315       enums[sig->id] = true;
316    }
317 }
318
319 void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value) {
320    WriteByte(Trace::TYPE_BITMASK);
321    WriteUInt(bitmask.id);
322    if (!bitmasks[bitmask.id]) {
323       WriteUInt(bitmask.count);
324       for (unsigned i = 0; i < bitmask.count; ++i) {
325          WriteString(bitmask.values[i].name);
326          WriteUInt(bitmask.values[i].value);
327       }
328       bitmasks[bitmask.id] = true;
329    }
330    WriteUInt(value);
331 }
332
333 void LiteralNull(void) {
334    WriteByte(Trace::TYPE_NULL);
335 }
336
337 void LiteralOpaque(const void *addr) {
338    if (!addr) {
339       LiteralNull();
340       return;
341    }
342    WriteByte(Trace::TYPE_OPAQUE);
343    WriteUInt((size_t)addr);
344 }
345
346 void Abort(void) {
347     Close();
348     OS::Abort();
349 }
350
351 } /* namespace Trace */