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 **************************************************************************/
36 #include "trace_writer.hpp"
37 #include "trace_format.hpp"
56 if (g_gzFile != NULL) {
63 Writer::open(const char *filename) {
66 g_gzFile = gzopen(filename, "wb");
77 _writeUInt(TRACE_VERSION);
85 static unsigned dwCounter = 0;
87 const char *szExtension = "trace";
88 char szFileName[PATH_MAX];
89 const char *lpFileName;
91 lpFileName = getenv("TRACE_FILE");
93 strncpy(szFileName, lpFileName, PATH_MAX);
96 char szProcessName[PATH_MAX];
97 char szCurrentDir[PATH_MAX];
98 OS::GetProcessName(szProcessName, PATH_MAX);
99 OS::GetCurrentDir(szCurrentDir, PATH_MAX);
105 snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
107 snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
109 file = fopen(szFileName, "rb");
119 OS::DebugMessage("apitrace: tracing to %s\n", szFileName);
125 Writer::_write(const void *sBuffer, size_t dwBytesToWrite) {
126 if (g_gzFile == NULL)
129 gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
133 Writer::_writeByte(char c) {
138 Writer::_writeUInt(unsigned long long value) {
139 char buf[2 * sizeof value];
144 assert(len < sizeof buf);
145 buf[len] = 0x80 | (value & 0x7f);
151 buf[len - 1] &= 0x7f;
157 Writer::_writeFloat(float value) {
158 assert(sizeof value == 4);
159 _write((const char *)&value, sizeof value);
163 Writer::_writeDouble(double value) {
164 assert(sizeof value == 8);
165 _write((const char *)&value, sizeof value);
169 Writer::_writeString(const char *str) {
170 size_t len = strlen(str);
175 inline bool lookup(std::vector<bool> &map, size_t index) {
176 if (index >= map.size()) {
177 map.resize(index + 1);
184 unsigned Writer::beginEnter(const FunctionSig *sig) {
191 _writeByte(Trace::EVENT_ENTER);
193 if (!lookup(functions, sig->id)) {
194 _writeString(sig->name);
195 _writeUInt(sig->num_args);
196 for (unsigned i = 0; i < sig->num_args; ++i) {
197 _writeString(sig->arg_names[i]);
199 functions[sig->id] = true;
205 void Writer::endEnter(void) {
206 _writeByte(Trace::CALL_END);
207 gzflush(g_gzFile, Z_SYNC_FLUSH);
211 void Writer::beginLeave(unsigned call) {
213 _writeByte(Trace::EVENT_LEAVE);
217 void Writer::endLeave(void) {
218 _writeByte(Trace::CALL_END);
219 gzflush(g_gzFile, Z_SYNC_FLUSH);
223 void Writer::beginArg(unsigned index) {
224 _writeByte(Trace::CALL_ARG);
228 void Writer::beginReturn(void) {
229 _writeByte(Trace::CALL_RET);
232 void Writer::beginArray(size_t length) {
233 _writeByte(Trace::TYPE_ARRAY);
237 void Writer::beginStruct(const StructSig *sig) {
238 _writeByte(Trace::TYPE_STRUCT);
240 if (!lookup(structs, sig->id)) {
241 _writeString(sig->name);
242 _writeUInt(sig->num_members);
243 for (unsigned i = 0; i < sig->num_members; ++i) {
244 _writeString(sig->member_names[i]);
246 structs[sig->id] = true;
250 void Writer::writeBool(bool value) {
251 _writeByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
254 void Writer::writeSInt(signed long long value) {
256 _writeByte(Trace::TYPE_SINT);
259 _writeByte(Trace::TYPE_UINT);
264 void Writer::writeUInt(unsigned long long value) {
265 _writeByte(Trace::TYPE_UINT);
269 void Writer::writeFloat(float value) {
270 _writeByte(Trace::TYPE_FLOAT);
274 void Writer::writeDouble(double value) {
275 _writeByte(Trace::TYPE_DOUBLE);
279 void Writer::writeString(const char *str) {
284 _writeByte(Trace::TYPE_STRING);
288 void Writer::writeString(const char *str, size_t len) {
293 _writeByte(Trace::TYPE_STRING);
298 void Writer::writeWString(const wchar_t *str) {
303 _writeByte(Trace::TYPE_STRING);
304 _writeString("<wide-string>");
307 void Writer::writeBlob(const void *data, size_t size) {
312 _writeByte(Trace::TYPE_BLOB);
319 void Writer::writeEnum(const EnumSig *sig) {
320 _writeByte(Trace::TYPE_ENUM);
322 if (!lookup(enums, sig->id)) {
323 _writeString(sig->name);
324 Writer::writeSInt(sig->value);
325 enums[sig->id] = true;
329 void Writer::writeBitmask(const BitmaskSig *sig, unsigned long long value) {
330 _writeByte(Trace::TYPE_BITMASK);
332 if (!lookup(bitmasks, sig->id)) {
333 _writeUInt(sig->num_flags);
334 for (unsigned i = 0; i < sig->num_flags; ++i) {
335 if (i != 0 && sig->flags[i].value == 0) {
336 OS::DebugMessage("apitrace: sig %s is zero but is not first flag\n", sig->flags[i].name);
338 _writeString(sig->flags[i].name);
339 _writeUInt(sig->flags[i].value);
341 bitmasks[sig->id] = true;
346 void Writer::writeNull(void) {
347 _writeByte(Trace::TYPE_NULL);
350 void Writer::writeOpaque(const void *addr) {
355 _writeByte(Trace::TYPE_OPAQUE);
356 _writeUInt((size_t)addr);
359 } /* namespace Trace */