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 *szName, const char *szExtension) {
56 static unsigned dwCounter = 0;
58 char szProcessName[PATH_MAX];
59 char szFileName[PATH_MAX];
61 OS::GetProcessName(szProcessName, PATH_MAX);
67 snprintf(szFileName, PATH_MAX, "%s.%s.%u.%s", szProcessName, szName, dwCounter, szExtension);
69 snprintf(szFileName, PATH_MAX, "%s.%s.%s", szProcessName, szName, szExtension);
71 file = fopen(szFileName, "rb");
80 fprintf(stderr, "Logging to %s\n", szFileName);
81 g_gzFile = gzopen(szFileName, "wb");
84 static inline void Write(const void *sBuffer, size_t dwBytesToWrite) {
91 gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
100 WriteUInt(unsigned long long value) {
101 char buf[2 * sizeof value];
106 assert(len < sizeof buf);
107 buf[len] = 0x80 | (value & 0x7f);
113 buf[len - 1] &= 0x7f;
119 WriteFloat(float value) {
120 assert(sizeof value == 4);
121 Write((const char *)&value, sizeof value);
125 WriteDouble(double value) {
126 assert(sizeof value == 8);
127 Write((const char *)&value, sizeof value);
131 WriteString(const char *str) {
132 size_t len = strlen(str);
137 void Open(const char *name) {
138 _Open(name, "trace");
139 WriteUInt(TRACE_VERSION);
146 void BeginCall(const char *function) {
149 WriteString(function);
153 WriteByte(Trace::CALL_END);
155 gzflush(g_gzFile, Z_SYNC_FLUSH);
159 void BeginArg(const char *type, const char *name) {
160 WriteByte(Trace::CALL_ARG);
164 void EndArg(void) { }
166 void BeginReturn(const char *type) {
167 WriteByte(Trace::CALL_RET);
170 void EndReturn(void) { }
172 void BeginArray(const char *type, size_t length) {
173 WriteByte(Trace::TYPE_ARRAY);
177 void EndArray(void) { }
179 void BeginElement(const char *type) { }
181 void EndElement(void) { }
183 void BeginStruct(const char *type) {
184 WriteByte(Trace::TYPE_STRUCT);
187 void EndStruct(void) {
191 void BeginMember(const char *type, const char *name) {
195 void EndMember(void) { }
197 void BeginBitmask(const char *type) {
198 WriteByte(Trace::TYPE_BITMASK);
201 void EndBitmask(void) {
202 WriteByte(Trace::TYPE_NULL);
205 void BeginPointer(const char *type, const void *addr)
207 WriteByte(Trace::TYPE_POINTER);
208 WriteUInt((size_t)addr);
211 void EndPointer(void) { }
213 void LiteralBool(bool value) {
214 WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
217 void LiteralSInt(signed long long value) {
219 WriteByte(Trace::TYPE_SINT);
222 WriteByte(Trace::TYPE_UINT);
227 void LiteralUInt(unsigned long long value) {
228 WriteByte(Trace::TYPE_UINT);
232 void LiteralFloat(float value) {
233 WriteByte(Trace::TYPE_FLOAT);
237 void LiteralFloat(double value) {
238 WriteByte(Trace::TYPE_DOUBLE);
242 void LiteralString(const char *str) {
247 WriteByte(Trace::TYPE_STRING);
251 void LiteralWString(const wchar_t *str) {
256 WriteByte(Trace::TYPE_STRING);
257 WriteString("<wide-string>");
260 void LiteralBlob(const void *data, size_t size) {
265 WriteByte(Trace::TYPE_BLOB);
272 void LiteralNamedConstant(const char *name, long long value) {
273 WriteByte(Trace::TYPE_CONST);
278 void LiteralNull(void) {
279 WriteByte(Trace::TYPE_NULL);
282 void LiteralOpaque(const void *addr) {
287 WriteByte(Trace::TYPE_OPAQUE);
288 WriteUInt((size_t)addr);
296 } /* namespace Log */