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 **************************************************************************/
34 #include "trace_writer.hpp"
35 #include "trace_file.hpp"
36 #include "trace_format.hpp"
45 m_file = new Trace::SnappyFile;
62 Writer::open(const char *filename) {
65 if (!m_file->open(filename, File::Write)) {
75 _writeUInt(TRACE_VERSION);
83 static unsigned dwCounter = 0;
85 const char *szExtension = "trace";
86 char szFileName[PATH_MAX];
87 const char *lpFileName;
89 lpFileName = getenv("TRACE_FILE");
91 strncpy(szFileName, lpFileName, PATH_MAX);
94 char szProcessName[PATH_MAX];
95 char szCurrentDir[PATH_MAX];
96 OS::GetProcessName(szProcessName, PATH_MAX);
97 OS::GetCurrentDir(szCurrentDir, PATH_MAX);
103 snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
105 snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
107 file = fopen(szFileName, "rb");
117 OS::DebugMessage("apitrace: tracing to %s\n", szFileName);
123 Writer::_write(const void *sBuffer, size_t dwBytesToWrite) {
124 m_file->write(sBuffer, dwBytesToWrite);
128 Writer::_writeByte(char c) {
133 Writer::_writeUInt(unsigned long long value) {
134 char buf[2 * sizeof value];
139 assert(len < sizeof buf);
140 buf[len] = 0x80 | (value & 0x7f);
146 buf[len - 1] &= 0x7f;
152 Writer::_writeFloat(float value) {
153 assert(sizeof value == 4);
154 _write((const char *)&value, sizeof value);
158 Writer::_writeDouble(double value) {
159 assert(sizeof value == 8);
160 _write((const char *)&value, sizeof value);
164 Writer::_writeString(const char *str) {
165 size_t len = strlen(str);
170 inline bool lookup(std::vector<bool> &map, size_t index) {
171 if (index >= map.size()) {
172 map.resize(index + 1);
179 unsigned Writer::beginEnter(const FunctionSig *sig) {
182 if (!m_file->isOpened()) {
186 _writeByte(Trace::EVENT_ENTER);
188 if (!lookup(functions, sig->id)) {
189 _writeString(sig->name);
190 _writeUInt(sig->num_args);
191 for (unsigned i = 0; i < sig->num_args; ++i) {
192 _writeString(sig->arg_names[i]);
194 functions[sig->id] = true;
200 void Writer::endEnter(void) {
201 _writeByte(Trace::CALL_END);
205 void Writer::beginLeave(unsigned call) {
207 _writeByte(Trace::EVENT_LEAVE);
211 void Writer::endLeave(void) {
212 _writeByte(Trace::CALL_END);
217 void Writer::beginArg(unsigned index) {
218 _writeByte(Trace::CALL_ARG);
222 void Writer::beginReturn(void) {
223 _writeByte(Trace::CALL_RET);
226 void Writer::beginArray(size_t length) {
227 _writeByte(Trace::TYPE_ARRAY);
231 void Writer::beginStruct(const StructSig *sig) {
232 _writeByte(Trace::TYPE_STRUCT);
234 if (!lookup(structs, sig->id)) {
235 _writeString(sig->name);
236 _writeUInt(sig->num_members);
237 for (unsigned i = 0; i < sig->num_members; ++i) {
238 _writeString(sig->member_names[i]);
240 structs[sig->id] = true;
244 void Writer::writeBool(bool value) {
245 _writeByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE);
248 void Writer::writeSInt(signed long long value) {
250 _writeByte(Trace::TYPE_SINT);
253 _writeByte(Trace::TYPE_UINT);
258 void Writer::writeUInt(unsigned long long value) {
259 _writeByte(Trace::TYPE_UINT);
263 void Writer::writeFloat(float value) {
264 _writeByte(Trace::TYPE_FLOAT);
268 void Writer::writeDouble(double value) {
269 _writeByte(Trace::TYPE_DOUBLE);
273 void Writer::writeString(const char *str) {
278 _writeByte(Trace::TYPE_STRING);
282 void Writer::writeString(const char *str, size_t len) {
287 _writeByte(Trace::TYPE_STRING);
292 void Writer::writeWString(const wchar_t *str) {
297 _writeByte(Trace::TYPE_STRING);
298 _writeString("<wide-string>");
301 void Writer::writeBlob(const void *data, size_t size) {
306 _writeByte(Trace::TYPE_BLOB);
313 void Writer::writeEnum(const EnumSig *sig) {
314 _writeByte(Trace::TYPE_ENUM);
316 if (!lookup(enums, sig->id)) {
317 _writeString(sig->name);
318 Writer::writeSInt(sig->value);
319 enums[sig->id] = true;
323 void Writer::writeBitmask(const BitmaskSig *sig, unsigned long long value) {
324 _writeByte(Trace::TYPE_BITMASK);
326 if (!lookup(bitmasks, sig->id)) {
327 _writeUInt(sig->num_flags);
328 for (unsigned i = 0; i < sig->num_flags; ++i) {
329 if (i != 0 && sig->flags[i].value == 0) {
330 OS::DebugMessage("apitrace: warning: sig %s is zero but is not first flag\n", sig->flags[i].name);
332 _writeString(sig->flags[i].name);
333 _writeUInt(sig->flags[i].value);
335 bitmasks[sig->id] = true;
340 void Writer::writeNull(void) {
341 _writeByte(Trace::TYPE_NULL);
344 void Writer::writeOpaque(const void *addr) {
349 _writeByte(Trace::TYPE_OPAQUE);
350 _writeUInt((size_t)addr);
353 } /* namespace Trace */