1 /**************************************************************************
3 * Copyright 2007-2009 VMware, Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
37 #include "trace_format.hpp"
43 static gzFile g_gzFile = NULL;
44 static void _Close(void) {
45 if(g_gzFile != NULL) {
51 static int reentrancy = 0;
53 static void _Open(const char *szExtension) {
56 static unsigned dwCounter = 0;
58 char szFileName[PATH_MAX];
59 const char *lpFileName;
61 lpFileName = getenv("TRACE_PATH");
63 strncpy(szFileName, lpFileName, PATH_MAX);
66 char szProcessName[PATH_MAX];
67 char szCurrentDir[PATH_MAX];
68 OS::GetProcessName(szProcessName, PATH_MAX);
69 OS::GetCurrentDir(szCurrentDir, PATH_MAX);
75 snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
77 snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
79 file = fopen(szFileName, "rb");
90 char szMessage[PATH_MAX];
91 snprintf(szMessage, PATH_MAX, "Tracing to %s\n", szFileName);
92 OS::DebugMessage(szMessage);
95 g_gzFile = gzopen(szFileName, "wb");
98 static inline void Write(const void *sBuffer, size_t dwBytesToWrite) {
105 gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
114 WriteUInt(unsigned long long value) {
115 char buf[2 * sizeof value];
120 assert(len < sizeof buf);
121 buf[len] = 0x80 | (value & 0x7f);
127 buf[len - 1] &= 0x7f;
133 WriteFloat(float value) {
134 assert(sizeof value == 4);
135 Write((const char *)&value, sizeof value);
139 WriteDouble(double value) {
140 assert(sizeof value == 8);
141 Write((const char *)&value, sizeof value);
145 WriteString(const char *str) {
146 size_t len = strlen(str);
154 WriteUInt(TRACE_VERSION);
162 void BeginCall(const char *function) {
166 WriteString(function);
170 WriteByte(Trace::CALL_END);
172 gzflush(g_gzFile, Z_SYNC_FLUSH);
176 void BeginArg(const char *name) {
177 WriteByte(Trace::CALL_ARG);
181 void EndArg(void) { }
183 void BeginReturn(void) {
184 WriteByte(Trace::CALL_RET);
187 void EndReturn(void) { }
189 void BeginArray(size_t length) {
190 WriteByte(Trace::TYPE_ARRAY);
194 void EndArray(void) { }
196 void BeginElement(void) { }
198 void EndElement(void) { }
200 void BeginStruct(const char *name) {
201 WriteByte(Trace::TYPE_STRUCT);
205 void EndStruct(void) {
209 void BeginMember(const char *name) {
213 void EndMember(void) { }
215 void BeginBitmask(void) {
216 WriteByte(Trace::TYPE_BITMASK);
219 void EndBitmask(void) {
220 WriteByte(Trace::TYPE_NULL);
223 void BeginPointer(const void *addr)
225 WriteByte(Trace::TYPE_POINTER);
226 WriteUInt((size_t)addr);
229 void EndPointer(void) { }
231 void LiteralBool(bool value) {
232 WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
235 void LiteralSInt(signed long long value) {
237 WriteByte(Trace::TYPE_SINT);
240 WriteByte(Trace::TYPE_UINT);
245 void LiteralUInt(unsigned long long value) {
246 WriteByte(Trace::TYPE_UINT);
250 void LiteralFloat(float value) {
251 WriteByte(Trace::TYPE_FLOAT);
255 void LiteralFloat(double value) {
256 WriteByte(Trace::TYPE_DOUBLE);
260 void LiteralString(const char *str) {
265 WriteByte(Trace::TYPE_STRING);
269 void LiteralString(const char *str, size_t len) {
274 WriteByte(Trace::TYPE_STRING);
279 void LiteralWString(const wchar_t *str) {
284 WriteByte(Trace::TYPE_STRING);
285 WriteString("<wide-string>");
288 void LiteralBlob(const void *data, size_t size) {
293 WriteByte(Trace::TYPE_BLOB);
300 void LiteralNamedConstant(const char *name, long long value) {
301 WriteByte(Trace::TYPE_CONST);
306 void LiteralNull(void) {
307 WriteByte(Trace::TYPE_NULL);
310 void LiteralOpaque(const void *addr) {
315 WriteByte(Trace::TYPE_OPAQUE);
316 WriteUInt((size_t)addr);
324 } /* namespace Log */