#include <stdlib.h>
#include <string.h>
-#include <zlib.h>
-
#include "os.hpp"
#include "trace_writer.hpp"
+#include "trace_snappyfile.hpp"
#include "trace_format.hpp"
Writer::Writer() :
- g_gzFile(NULL),
call_no(0)
{
+ m_file = new Trace::SnappyFile;
close();
-};
+}
-Writer::~Writer() {
+Writer::~Writer()
+{
close();
-};
+ delete m_file;
+ m_file = NULL;
+}
void
Writer::close(void) {
- if (g_gzFile != NULL) {
- gzclose(g_gzFile);
- g_gzFile = NULL;
- }
+ m_file->close();
}
bool
Writer::open(const char *filename) {
close();
- g_gzFile = gzopen(filename, "wb");
- if (!g_gzFile) {
+ if (!m_file->open(filename, File::Write)) {
return false;
}
return true;
}
-void
-Writer::open(void) {
-
- static unsigned dwCounter = 0;
-
- const char *szExtension = "trace";
- char szFileName[PATH_MAX];
- const char *lpFileName;
-
- lpFileName = getenv("TRACE_FILE");
- if (lpFileName) {
- strncpy(szFileName, lpFileName, PATH_MAX);
- }
- else {
- char szProcessName[PATH_MAX];
- char szCurrentDir[PATH_MAX];
- OS::GetProcessName(szProcessName, PATH_MAX);
- OS::GetCurrentDir(szCurrentDir, PATH_MAX);
-
- for (;;) {
- FILE *file;
-
- if (dwCounter)
- snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension);
- else
- snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension);
-
- file = fopen(szFileName, "rb");
- if (file == NULL)
- break;
-
- fclose(file);
-
- ++dwCounter;
- }
- }
-
- OS::DebugMessage("apitrace: tracing to %s\n", szFileName);
-
- open(szFileName);
-}
-
void inline
Writer::_write(const void *sBuffer, size_t dwBytesToWrite) {
- if (g_gzFile == NULL)
- return;
-
- gzwrite(g_gzFile, sBuffer, dwBytesToWrite);
+ m_file->write(sBuffer, dwBytesToWrite);
}
void inline
}
}
-unsigned Writer::beginEnter(const FunctionSig &function) {
- OS::AcquireMutex();
-
- if (!g_gzFile) {
- open();
- }
-
+unsigned Writer::beginEnter(const FunctionSig *sig) {
_writeByte(Trace::EVENT_ENTER);
- _writeUInt(function.id);
- if (!lookup(functions, function.id)) {
- _writeString(function.name);
- _writeUInt(function.num_args);
- for (unsigned i = 0; i < function.num_args; ++i) {
- _writeString(function.arg_names[i]);
+ _writeUInt(sig->id);
+ if (!lookup(functions, sig->id)) {
+ _writeString(sig->name);
+ _writeUInt(sig->num_args);
+ for (unsigned i = 0; i < sig->num_args; ++i) {
+ _writeString(sig->arg_names[i]);
}
- functions[function.id] = true;
+ functions[sig->id] = true;
}
return call_no++;
void Writer::endEnter(void) {
_writeByte(Trace::CALL_END);
- gzflush(g_gzFile, Z_SYNC_FLUSH);
- OS::ReleaseMutex();
}
void Writer::beginLeave(unsigned call) {
- OS::AcquireMutex();
_writeByte(Trace::EVENT_LEAVE);
_writeUInt(call);
}
void Writer::endLeave(void) {
_writeByte(Trace::CALL_END);
- gzflush(g_gzFile, Z_SYNC_FLUSH);
- OS::ReleaseMutex();
}
void Writer::beginArg(unsigned index) {
}
}
-void Writer::writeBitmask(const BitmaskSig &bitmask, unsigned long long value) {
+void Writer::writeBitmask(const BitmaskSig *sig, unsigned long long value) {
_writeByte(Trace::TYPE_BITMASK);
- _writeUInt(bitmask.id);
- if (!lookup(bitmasks, bitmask.id)) {
- _writeUInt(bitmask.num_flags);
- for (unsigned i = 0; i < bitmask.num_flags; ++i) {
- if (i != 0 && bitmask.flags[i].value == 0) {
- OS::DebugMessage("apitrace: bitmask %s is zero but is not first flag\n", bitmask.flags[i].name);
+ _writeUInt(sig->id);
+ if (!lookup(bitmasks, sig->id)) {
+ _writeUInt(sig->num_flags);
+ for (unsigned i = 0; i < sig->num_flags; ++i) {
+ if (i != 0 && sig->flags[i].value == 0) {
+ OS::DebugMessage("apitrace: warning: sig %s is zero but is not first flag\n", sig->flags[i].name);
}
- _writeString(bitmask.flags[i].name);
- _writeUInt(bitmask.flags[i].value);
+ _writeString(sig->flags[i].name);
+ _writeUInt(sig->flags[i].value);
}
- bitmasks[bitmask.id] = true;
+ bitmasks[sig->id] = true;
}
_writeUInt(value);
}
_writeUInt((size_t)addr);
}
+
} /* namespace Trace */