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 **************************************************************************/
39 #include "trace_format.hpp"
45 static gzFile g_gzFile = NULL;
46 static void _Close(void) {
47 if(g_gzFile != NULL) {
53 static int reentrancy = 0;
55 static void _Open(const char *szExtension) {
58 static unsigned dwCounter = 0;
60 char szFileName[PATH_MAX];
61 const char *lpFileName;
63 lpFileName = getenv("TRACE_PATH");
65 strncpy(szFileName, lpFileName, PATH_MAX);
68 char szProcessName[PATH_MAX];
69 char szCurrentDir[PATH_MAX];
70 OS::GetProcessName(szProcessName, PATH_MAX);
71 OS::GetCurrentDir(szCurrentDir, PATH_MAX);
77 snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
79 snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
81 file = fopen(szFileName, "rb");
92 char szMessage[PATH_MAX];
93 snprintf(szMessage, PATH_MAX, "Tracing to %s\n", szFileName);
94 OS::DebugMessage(szMessage);
97 g_gzFile = gzopen(szFileName, "wb");
100 static inline void Write(const void *sBuffer, size_t dwBytesToWrite) {
101 if (g_gzFile == NULL)
107 gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
116 WriteUInt(unsigned long long value) {
117 char buf[2 * sizeof value];
122 assert(len < sizeof buf);
123 buf[len] = 0x80 | (value & 0x7f);
129 buf[len - 1] &= 0x7f;
135 WriteFloat(float value) {
136 assert(sizeof value == 4);
137 Write((const char *)&value, sizeof value);
141 WriteDouble(double value) {
142 assert(sizeof value == 8);
143 Write((const char *)&value, sizeof value);
147 WriteString(const char *str) {
148 size_t len = strlen(str);
153 typedef std::map<const char *, size_t> namemap;
154 static namemap names;
157 WriteName(const char *name) {
158 namemap::iterator it = names.find(name);
159 if (it == names.end()) {
160 size_t name_id = names.size();
163 names[name] = name_id;
165 WriteUInt(it->second);
172 WriteUInt(TRACE_VERSION);
180 void BeginCall(const char *function) {
188 WriteByte(Trace::CALL_END);
190 gzflush(g_gzFile, Z_SYNC_FLUSH);
194 void BeginArg(unsigned index, const char *name) {
195 WriteByte(Trace::CALL_ARG);
200 void BeginReturn(void) {
201 WriteByte(Trace::CALL_RET);
204 void BeginArray(size_t length) {
205 WriteByte(Trace::TYPE_ARRAY);
209 void BeginStruct(size_t length) {
210 WriteByte(Trace::TYPE_STRUCT);
214 void BeginMember(const char *name) {
218 void BeginBitmask(void) {
219 WriteByte(Trace::TYPE_BITMASK);
222 void EndBitmask(void) {
223 WriteByte(Trace::TYPE_NULL);
226 void LiteralBool(bool value) {
227 WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
230 void LiteralSInt(signed long long value) {
232 WriteByte(Trace::TYPE_SINT);
235 WriteByte(Trace::TYPE_UINT);
240 void LiteralUInt(unsigned long long value) {
241 WriteByte(Trace::TYPE_UINT);
245 void LiteralFloat(float value) {
246 WriteByte(Trace::TYPE_FLOAT);
250 void LiteralFloat(double value) {
251 WriteByte(Trace::TYPE_DOUBLE);
255 void LiteralString(const char *str) {
260 WriteByte(Trace::TYPE_STRING);
264 void LiteralString(const char *str, size_t len) {
269 WriteByte(Trace::TYPE_STRING);
274 void LiteralWString(const wchar_t *str) {
279 WriteByte(Trace::TYPE_STRING);
280 WriteString("<wide-string>");
283 void LiteralBlob(const void *data, size_t size) {
288 WriteByte(Trace::TYPE_BLOB);
295 void LiteralNamedConstant(const char *name, long long value) {
296 WriteByte(Trace::TYPE_CONST);
301 void LiteralNull(void) {
302 WriteByte(Trace::TYPE_NULL);
305 void LiteralOpaque(const void *addr) {
310 WriteByte(Trace::TYPE_OPAQUE);
311 WriteUInt((size_t)addr);
319 } /* namespace Log */