]> git.cworth.org Git - apitrace/commitdiff
Merge branch 'master' into compression
authorZack Rusin <zack@kde.org>
Wed, 24 Aug 2011 03:02:10 +0000 (23:02 -0400)
committerZack Rusin <zack@kde.org>
Wed, 24 Aug 2011 03:02:10 +0000 (23:02 -0400)
Conflicts:
trace_writer.cpp

1  2 
trace_writer.cpp
trace_writer.hpp

diff --combined trace_writer.cpp
index 7511d3192420db31f65ee2e74dd5203db3ec201a,7fcd0e6762b24acd247d536e670a5d36b99ef8ac..057e3957be289a63b9598c3c4344ae75171cb71e
  #include <stdlib.h>
  #include <string.h>
  
 -#include <zlib.h>
 -
  #include "os.hpp"
  #include "trace_writer.hpp"
 +#include "trace_file.hpp"
  #include "trace_format.hpp"
  
  
@@@ -40,29 -41,30 +40,29 @@@ namespace Trace 
  
  
  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
@@@ -177,12 -140,6 +135,6 @@@ inline bool lookup(std::vector<bool> &m
  }
  
  unsigned Writer::beginEnter(const FunctionSig *sig) {
-     OS::AcquireMutex();
-     if (!m_file->isOpened()) {
-         open();
-     }
      _writeByte(Trace::EVENT_ENTER);
      _writeUInt(sig->id);
      if (!lookup(functions, sig->id)) {
  
  void Writer::endEnter(void) {
      _writeByte(Trace::CALL_END);
-     OS::ReleaseMutex();
  }
  
  void Writer::beginLeave(unsigned call) {
-     OS::AcquireMutex();
      _writeByte(Trace::EVENT_LEAVE);
      _writeUInt(call);
  }
  
  void Writer::endLeave(void) {
      _writeByte(Trace::CALL_END);
-     m_file->flush();
-     OS::ReleaseMutex();
  }
  
  void Writer::beginArg(unsigned index) {
@@@ -350,5 -303,76 +298,75 @@@ void Writer::writeOpaque(const void *ad
      _writeUInt((size_t)addr);
  }
  
 -    if (!g_gzFile) {
+ 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();
 -    gzflush(g_gzFile, Z_SYNC_FLUSH);
++    if (!m_file->isOpened()) {
+         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();
++    m_file->flush();
+     OS::ReleaseMutex();
+ }
  } /* namespace Trace */
  
diff --combined trace_writer.hpp
index eb81f233f604bef706d1149d867d13ef1748e2fc,20a153093f4fc0b5d0f95411b0833ab5482a1657..4d46f739f8f231fcdee4e39de053b21949193448
  
  
  namespace Trace {
 +    class File;
  
      class Writer {
      protected:
 -        void *g_gzFile;
 +        File *m_file;
          unsigned call_no;
  
          std::vector<bool> functions;
@@@ -55,7 -54,6 +55,6 @@@
          Writer();
          ~Writer();
  
-         void open(void);
          bool open(const char *filename);
          void close(void);
  
          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_ */