]> git.cworth.org Git - apitrace/blob - trace_write.cpp
More efficient call 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> bitmasks;
175
176
177 unsigned BeginEnter(const FunctionSig &function) {
178    OS::AcquireMutex();
179    Open();
180    WriteByte(Trace::EVENT_ENTER);
181    WriteUInt(function.id);
182    if (!functions[function.id]) {
183       WriteString(function.name);
184       WriteUInt(function.num_args);
185       for (unsigned i = 0; i < function.num_args; ++i) {
186          WriteString(function.args[i]);
187       }
188       functions[function.id] = true;
189    }
190    return call_no++;
191 }
192
193 void EndEnter(void) {
194    WriteByte(Trace::CALL_END);
195    gzflush(g_gzFile, Z_SYNC_FLUSH);
196    OS::ReleaseMutex();
197 }
198
199 void BeginLeave(unsigned call) {
200    OS::AcquireMutex();
201    WriteByte(Trace::EVENT_LEAVE);
202    WriteUInt(call);
203 }
204
205 void EndLeave(void) {
206    WriteByte(Trace::CALL_END);
207    gzflush(g_gzFile, Z_SYNC_FLUSH);
208    OS::ReleaseMutex();
209 }
210
211 void BeginArg(unsigned index) {
212    WriteByte(Trace::CALL_ARG);
213    WriteUInt(index);
214 }
215
216 void BeginReturn(void) {
217    WriteByte(Trace::CALL_RET);
218 }
219
220 void BeginArray(size_t length) {
221    WriteByte(Trace::TYPE_ARRAY);
222    WriteUInt(length);
223 }
224
225 void BeginStruct(size_t length) {
226    WriteByte(Trace::TYPE_STRUCT);
227    WriteUInt(length);
228 }
229
230 void BeginMember(const char *name) {
231    WriteName(name);
232 }
233
234 void BeginBitmask(void) {
235    WriteByte(Trace::TYPE_BITMASK);
236 }
237
238 void EndBitmask(void) {
239    WriteByte(Trace::TYPE_NULL);
240 }
241
242 void LiteralBool(bool value) {
243    WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
244 }
245
246 void LiteralSInt(signed long long value) {
247    if (value < 0) {
248       WriteByte(Trace::TYPE_SINT);
249       WriteUInt(-value);
250    } else {
251       WriteByte(Trace::TYPE_UINT);
252       WriteUInt(value);
253    }
254 }
255
256 void LiteralUInt(unsigned long long value) {
257    WriteByte(Trace::TYPE_UINT);
258    WriteUInt(value);
259 }
260
261 void LiteralFloat(float value) {
262    WriteByte(Trace::TYPE_FLOAT);
263    WriteFloat(value);
264 }
265
266 void LiteralFloat(double value) {
267    WriteByte(Trace::TYPE_DOUBLE);
268    WriteDouble(value);
269 }
270
271 void LiteralString(const char *str) {
272    if (!str) {
273       LiteralNull();
274       return;
275    }
276    WriteByte(Trace::TYPE_STRING);
277    WriteString(str);
278 }
279
280 void LiteralString(const char *str, size_t len) {
281    if (!str) {
282       LiteralNull();
283       return;
284    }
285    WriteByte(Trace::TYPE_STRING);
286    WriteUInt(len);
287    Write(str, len);
288 }
289
290 void LiteralWString(const wchar_t *str) {
291    if (!str) {
292       LiteralNull();
293       return;
294    }
295    WriteByte(Trace::TYPE_STRING);
296    WriteString("<wide-string>");
297 }
298    
299 void LiteralBlob(const void *data, size_t size) {
300    if (!data) {
301       LiteralNull();
302       return;
303    }
304    WriteByte(Trace::TYPE_BLOB);
305    WriteUInt(size);
306    if (size) {
307       Write(data, size);
308    }
309 }
310
311 void LiteralNamedConstant(const char *name, long long value) {
312    WriteByte(Trace::TYPE_CONST);
313    WriteName(name);
314    LiteralSInt(value);
315 }
316
317 void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value) {
318    WriteByte(Trace::TYPE_BITMASK);
319    WriteUInt(bitmask.id);
320    if (!bitmasks[bitmask.id]) {
321       WriteUInt(bitmask.count);
322       for (unsigned i = 0; i < bitmask.count; ++i) {
323          WriteString(bitmask.values[i].name);
324          WriteUInt(bitmask.values[i].value);
325       }
326       bitmasks[bitmask.id] = true;
327    }
328    WriteUInt(value);
329 }
330
331 void LiteralNull(void) {
332    WriteByte(Trace::TYPE_NULL);
333 }
334
335 void LiteralOpaque(const void *addr) {
336    if (!addr) {
337       LiteralNull();
338       return;
339    }
340    WriteByte(Trace::TYPE_OPAQUE);
341    WriteUInt((size_t)addr);
342 }
343
344 void Abort(void) {
345     Close();
346     OS::Abort();
347 }
348
349 } /* namespace Trace */