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