]> git.cworth.org Git - apitrace/blob - log.cpp
Major move to visitor paradigm.
[apitrace] / log.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 <zlib.h>
34
35 #include "os.hpp"
36 #include "log.hpp"
37 #include "trace_format.hpp"
38
39
40 namespace Log {
41
42
43 static gzFile g_gzFile = NULL;
44 static void _Close(void) {
45    if(g_gzFile != NULL) {
46       gzclose(g_gzFile);
47       g_gzFile = NULL;
48    }
49 }
50
51 static int reentrancy = 0;
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    {
90        char szMessage[PATH_MAX];
91        snprintf(szMessage, PATH_MAX, "Tracing to %s\n", szFileName);
92        OS::DebugMessage(szMessage);
93    }
94
95    g_gzFile = gzopen(szFileName, "wb");
96 }
97
98 static inline void Write(const void *sBuffer, size_t dwBytesToWrite) {
99    if (g_gzFile == NULL)
100       return;
101    
102    if (reentrancy > 1)
103       return;
104
105    gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
106 }
107
108 static inline void 
109 WriteByte(char c) {
110    Write(&c, 1);
111 }
112
113 void inline 
114 WriteUInt(unsigned long long value) {
115    char buf[2 * sizeof value];
116    unsigned len;
117
118    len = 0;
119    do {
120       assert(len < sizeof buf);
121       buf[len] = 0x80 | (value & 0x7f);
122       value >>= 7;
123       ++len;
124    } while (value);
125
126    assert(len);
127    buf[len - 1] &= 0x7f;
128
129    Write(buf, len);
130 }
131
132 static inline void 
133 WriteFloat(float value) {
134    assert(sizeof value == 4);
135    Write((const char *)&value, sizeof value);
136 }
137
138 static inline void 
139 WriteDouble(double value) {
140    assert(sizeof value == 8);
141    Write((const char *)&value, sizeof value);
142 }
143
144 static inline void 
145 WriteString(const char *str) {
146    size_t len = strlen(str);
147    WriteUInt(len);
148    Write(str, len);
149 }
150
151 void Open(void) {
152     if (!g_gzFile) {
153         _Open("trace");
154         WriteUInt(TRACE_VERSION);
155     }
156 }
157
158 void Close(void) {
159    _Close();
160 }
161
162 void BeginCall(const char *function) {
163    OS::AcquireMutex();
164    Open();
165    ++reentrancy;
166    WriteString(function);
167 }
168
169 void EndCall(void) {
170    WriteByte(Trace::CALL_END);
171    --reentrancy;
172    gzflush(g_gzFile, Z_SYNC_FLUSH);
173    OS::ReleaseMutex();
174 }
175
176 void BeginArg(const char *name) {
177    WriteByte(Trace::CALL_ARG);
178    WriteString(name);
179 }
180
181 void EndArg(void) { }
182
183 void BeginReturn(void) {
184    WriteByte(Trace::CALL_RET);
185 }
186
187 void EndReturn(void) { }
188
189 void BeginArray(size_t length) {
190    WriteByte(Trace::TYPE_ARRAY);
191    WriteUInt(length);
192 }
193
194 void EndArray(void) { }
195
196 void BeginElement(void) { }
197
198 void EndElement(void) { }
199
200 void BeginStruct(const char *name) {
201    WriteByte(Trace::TYPE_STRUCT);
202 }
203
204 void EndStruct(void) {
205    WriteString("");
206 }
207
208 void BeginMember(const char *name) {
209    WriteString(name);
210 }
211
212 void EndMember(void) { }
213
214 void BeginBitmask(void) {
215    WriteByte(Trace::TYPE_BITMASK);
216 }
217
218 void EndBitmask(void) {
219    WriteByte(Trace::TYPE_NULL);
220 }
221
222 void BeginPointer(const void *addr)
223 {
224    WriteByte(Trace::TYPE_POINTER);
225    WriteUInt((size_t)addr);
226 }
227
228 void EndPointer(void) { }
229
230 void LiteralBool(bool value) {
231    WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
232 }
233
234 void LiteralSInt(signed long long value) {
235    if (value < 0) {
236       WriteByte(Trace::TYPE_SINT);
237       WriteUInt(-value);
238    } else {
239       WriteByte(Trace::TYPE_UINT);
240       WriteUInt(value);
241    }
242 }
243
244 void LiteralUInt(unsigned long long value) {
245    WriteByte(Trace::TYPE_UINT);
246    WriteUInt(value);
247 }
248
249 void LiteralFloat(float value) {
250    WriteByte(Trace::TYPE_FLOAT);
251    WriteFloat(value);
252 }
253
254 void LiteralFloat(double value) {
255    WriteByte(Trace::TYPE_DOUBLE);
256    WriteDouble(value);
257 }
258
259 void LiteralString(const char *str) {
260    if (!str) {
261       LiteralNull();
262       return;
263    }
264    WriteByte(Trace::TYPE_STRING);
265    WriteString(str);
266 }
267
268 void LiteralWString(const wchar_t *str) {
269    if (!str) {
270       LiteralNull();
271       return;
272    }
273    WriteByte(Trace::TYPE_STRING);
274    WriteString("<wide-string>");
275 }
276    
277 void LiteralBlob(const void *data, size_t size) {
278    if (!data) {
279       LiteralNull();
280       return;
281    }
282    WriteByte(Trace::TYPE_BLOB);
283    WriteUInt(size);
284    if (size) {
285       Write(data, size);
286    }
287 }
288
289 void LiteralNamedConstant(const char *name, long long value) {
290    WriteByte(Trace::TYPE_CONST);
291    WriteString(name);
292    LiteralSInt(value);
293 }
294
295 void LiteralNull(void) {
296    WriteByte(Trace::TYPE_NULL);
297 }
298
299 void LiteralOpaque(const void *addr) {
300    if (!addr) {
301       LiteralNull();
302       return;
303    }
304    WriteByte(Trace::TYPE_OPAQUE);
305    WriteUInt((size_t)addr);
306 }
307
308 void Abort(void) {
309     Close();
310     OS::Abort();
311 }
312
313 static void _uninit(void) __attribute__((destructor));
314 static void _uninit(void) {
315    Close();
316 }
317
318 } /* namespace Log */