From: José Fonseca Date: Sat, 20 Aug 2011 12:01:37 +0000 (+0100) Subject: Specialized Writer for tracing the current process. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=de6cfa776014ca36679a225f95b05b0b1e956cf2;p=apitrace Specialized Writer for tracing the current process. No need to burden the GUI and other tools with mutexes, and file sync/flushes. --- diff --git a/trace.py b/trace.py index d9d50ac..90cc426 100644 --- a/trace.py +++ b/trace.py @@ -309,7 +309,7 @@ class Tracer: self.footer(api) def header(self, api): - print 'Trace::Writer __writer;' + print 'Trace::LocalWriter __writer;' print def footer(self, api): diff --git a/trace_writer.cpp b/trace_writer.cpp index af8d62f..7fcd0e6 100644 --- a/trace_writer.cpp +++ b/trace_writer.cpp @@ -79,48 +79,6 @@ Writer::open(const char *filename) { 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) @@ -182,12 +140,6 @@ inline bool lookup(std::vector &map, size_t index) { } unsigned Writer::beginEnter(const FunctionSig *sig) { - OS::AcquireMutex(); - - if (!g_gzFile) { - open(); - } - _writeByte(Trace::EVENT_ENTER); _writeUInt(sig->id); if (!lookup(functions, sig->id)) { @@ -204,20 +156,15 @@ unsigned Writer::beginEnter(const FunctionSig *sig) { 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) { @@ -356,5 +303,76 @@ void Writer::writeOpaque(const void *addr) { _writeUInt((size_t)addr); } + +void +LocalWriter::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); + + Writer::open(szFileName); +} + +unsigned LocalWriter::beginEnter(const FunctionSig *sig) { + OS::AcquireMutex(); + + if (!g_gzFile) { + open(); + } + + return Writer::beginEnter(sig); +} + +void LocalWriter::endEnter(void) { + Writer::endEnter(); + gzflush(g_gzFile, Z_SYNC_FLUSH); + OS::ReleaseMutex(); +} + +void LocalWriter::beginLeave(unsigned call) { + OS::AcquireMutex(); + Writer::beginLeave(call); +} + +void LocalWriter::endLeave(void) { + Writer::endLeave(); + gzflush(g_gzFile, Z_SYNC_FLUSH); + OS::ReleaseMutex(); +} + + } /* namespace Trace */ diff --git a/trace_writer.hpp b/trace_writer.hpp index 4f9cd2b..20a1530 100644 --- a/trace_writer.hpp +++ b/trace_writer.hpp @@ -54,7 +54,6 @@ namespace Trace { Writer(); ~Writer(); - void open(void); bool open(const char *filename); void close(void); @@ -104,6 +103,27 @@ namespace Trace { void inline _writeString(const char *str); }; + + /** + * A specialized Writer class, mean to trace the current process. + * + * In particular: + * - it creates a trace file based on the current process name + * - uses mutexes to allow tracing from multiple threades + * - flushes the output to ensure the last call is traced in event of + * abnormal termination + */ + class LocalWriter : public Writer { + protected: + public: + void open(void); + + unsigned beginEnter(const FunctionSig *sig); + void endEnter(void); + + void beginLeave(unsigned call); + void endLeave(void); + }; } #endif /* _TRACE_WRITER_HPP_ */