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 **************************************************************************/
38 #include "trace_write.hpp"
39 #include "trace_format.hpp"
45 static gzFile g_gzFile = NULL;
46 static void _Close(void) {
47 if(g_gzFile != NULL) {
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");
89 OS::DebugMessage("apitrace: tracing to %s\n", szFileName);
91 g_gzFile = gzopen(szFileName, "wb");
94 static inline void Write(const void *sBuffer, size_t dwBytesToWrite) {
98 gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
107 WriteUInt(unsigned long long value) {
108 char buf[2 * sizeof value];
113 assert(len < sizeof buf);
114 buf[len] = 0x80 | (value & 0x7f);
120 buf[len - 1] &= 0x7f;
126 WriteFloat(float value) {
127 assert(sizeof value == 4);
128 Write((const char *)&value, sizeof value);
132 WriteDouble(double value) {
133 assert(sizeof value == 8);
134 Write((const char *)&value, sizeof value);
138 WriteString(const char *str) {
139 size_t len = strlen(str);
147 WriteUInt(TRACE_VERSION);
155 static unsigned call_no = 0;
157 inline bool lookup(std::vector<bool> &map, size_t index) {
158 if (index >= map.size()) {
159 map.resize(index + 1);
166 static std::vector<bool> functions;
167 static std::vector<bool> structs;
168 static std::vector<bool> enums;
169 static std::vector<bool> bitmasks;
172 unsigned BeginEnter(const FunctionSig &function) {
175 WriteByte(Trace::EVENT_ENTER);
176 WriteUInt(function.id);
177 if (!lookup(functions, function.id)) {
178 WriteString(function.name);
179 WriteUInt(function.num_args);
180 for (unsigned i = 0; i < function.num_args; ++i) {
181 WriteString(function.args[i]);
183 functions[function.id] = true;
188 void EndEnter(void) {
189 WriteByte(Trace::CALL_END);
190 gzflush(g_gzFile, Z_SYNC_FLUSH);
194 void BeginLeave(unsigned call) {
196 WriteByte(Trace::EVENT_LEAVE);
200 void EndLeave(void) {
201 WriteByte(Trace::CALL_END);
202 gzflush(g_gzFile, Z_SYNC_FLUSH);
206 void BeginArg(unsigned index) {
207 WriteByte(Trace::CALL_ARG);
211 void BeginReturn(void) {
212 WriteByte(Trace::CALL_RET);
215 void BeginArray(size_t length) {
216 WriteByte(Trace::TYPE_ARRAY);
220 void BeginStruct(const StructSig *sig) {
221 WriteByte(Trace::TYPE_STRUCT);
223 if (!lookup(structs, sig->id)) {
224 WriteString(sig->name);
225 WriteUInt(sig->num_members);
226 for (unsigned i = 0; i < sig->num_members; ++i) {
227 WriteString(sig->members[i]);
229 structs[sig->id] = true;
233 void LiteralBool(bool value) {
234 WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
237 void LiteralSInt(signed long long value) {
239 WriteByte(Trace::TYPE_SINT);
242 WriteByte(Trace::TYPE_UINT);
247 void LiteralUInt(unsigned long long value) {
248 WriteByte(Trace::TYPE_UINT);
252 void LiteralFloat(float value) {
253 WriteByte(Trace::TYPE_FLOAT);
257 void LiteralFloat(double value) {
258 WriteByte(Trace::TYPE_DOUBLE);
262 void LiteralString(const char *str) {
267 WriteByte(Trace::TYPE_STRING);
271 void LiteralString(const char *str, size_t len) {
276 WriteByte(Trace::TYPE_STRING);
281 void LiteralWString(const wchar_t *str) {
286 WriteByte(Trace::TYPE_STRING);
287 WriteString("<wide-string>");
290 void LiteralBlob(const void *data, size_t size) {
295 WriteByte(Trace::TYPE_BLOB);
302 void LiteralEnum(const EnumSig *sig) {
303 WriteByte(Trace::TYPE_ENUM);
305 if (!lookup(enums, sig->id)) {
306 WriteString(sig->name);
307 LiteralSInt(sig->value);
308 enums[sig->id] = true;
312 void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value) {
313 WriteByte(Trace::TYPE_BITMASK);
314 WriteUInt(bitmask.id);
315 if (!lookup(bitmasks, bitmask.id)) {
316 WriteUInt(bitmask.count);
317 for (unsigned i = 0; i < bitmask.count; ++i) {
318 WriteString(bitmask.values[i].name);
319 WriteUInt(bitmask.values[i].value);
321 bitmasks[bitmask.id] = true;
326 void LiteralNull(void) {
327 WriteByte(Trace::TYPE_NULL);
330 void LiteralOpaque(const void *addr) {
335 WriteByte(Trace::TYPE_OPAQUE);
336 WriteUInt((size_t)addr);
344 } /* namespace Trace */
350 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
352 case DLL_PROCESS_ATTACH:
353 case DLL_THREAD_ATTACH:
355 case DLL_THREAD_DETACH:
357 case DLL_PROCESS_DETACH:
369 static void _uninit(void) __attribute__((destructor));
370 static void _uninit(void) {