diff options
-rw-r--r-- | bmp.hpp | 150 | ||||
-rw-r--r-- | d3dshader.py | 8 | ||||
-rw-r--r-- | dump.cpp | 24 | ||||
-rw-r--r-- | formatter.hpp | 120 | ||||
-rw-r--r-- | os_posix.cpp | 18 | ||||
-rw-r--r-- | os_win32.cpp | 40 | ||||
-rw-r--r-- | trace_format.hpp | 42 | ||||
-rw-r--r-- | trace_model.cpp | 348 | ||||
-rw-r--r-- | trace_model.hpp | 224 | ||||
-rw-r--r-- | trace_parser.hpp | 664 | ||||
-rw-r--r-- | trace_write.cpp | 358 | ||||
-rw-r--r-- | trace_write.hpp | 6 | ||||
-rw-r--r-- | wglapi.py | 2 |
13 files changed, 1002 insertions, 1002 deletions
@@ -34,33 +34,33 @@ namespace BMP { #pragma pack(push,2) struct FileHeader { - uint16_t bfType; - uint32_t bfSize; - uint16_t bfReserved1; - uint16_t bfReserved2; - uint32_t bfOffBits; + uint16_t bfType; + uint32_t bfSize; + uint16_t bfReserved1; + uint16_t bfReserved2; + uint32_t bfOffBits; }; #pragma pack(pop) struct InfoHeader { - uint32_t biSize; - int32_t biWidth; - int32_t biHeight; - uint16_t biPlanes; - uint16_t biBitCount; - uint32_t biCompression; - uint32_t biSizeImage; - int32_t biXPelsPerMeter; - int32_t biYPelsPerMeter; - uint32_t biClrUsed; - uint32_t biClrImportant; + uint32_t biSize; + int32_t biWidth; + int32_t biHeight; + uint16_t biPlanes; + uint16_t biBitCount; + uint32_t biCompression; + uint32_t biSizeImage; + int32_t biXPelsPerMeter; + int32_t biYPelsPerMeter; + uint32_t biClrUsed; + uint32_t biClrImportant; }; struct Pixel { - uint8_t rgbBlue; - uint8_t rgbGreen; - uint8_t rgbRed; - uint8_t rgbAlpha; + uint8_t rgbBlue; + uint8_t rgbGreen; + uint8_t rgbRed; + uint8_t rgbAlpha; }; @@ -71,61 +71,61 @@ write(const char *filename, unsigned stride, bool flip = false) { - struct FileHeader bmfh; - struct InfoHeader bmih; - unsigned x, y; - - bmfh.bfType = 0x4d42; - bmfh.bfSize = 14 + 40 + height*width*4; - bmfh.bfReserved1 = 0; - bmfh.bfReserved2 = 0; - bmfh.bfOffBits = 14 + 40; - - bmih.biSize = 40; - bmih.biWidth = width; - bmih.biHeight = height; - bmih.biPlanes = 1; - bmih.biBitCount = 32; - bmih.biCompression = 0; - bmih.biSizeImage = height*width*4; - bmih.biXPelsPerMeter = 0; - bmih.biYPelsPerMeter = 0; - bmih.biClrUsed = 0; - bmih.biClrImportant = 0; - - std::ofstream stream(filename, std::ofstream::binary); - - stream.write((const char *)&bmfh, 14); - stream.write((const char *)&bmih, 40); - - if (flip) { - y = height; - while (y--) { - const unsigned char *ptr = rgba + y * stride; - for (x = 0; x < width; ++x) { - struct Pixel pixel; - pixel.rgbRed = ptr[x*4 + 0]; - pixel.rgbGreen = ptr[x*4 + 1]; - pixel.rgbBlue = ptr[x*4 + 2]; - pixel.rgbAlpha = ptr[x*4 + 3]; - stream.write((const char *)&pixel, 4); - } - } - } else { - for (y = 0; y < height; ++y) { - const unsigned char *ptr = rgba + y * stride; - for (x = 0; x < width; ++x) { - struct Pixel pixel; - pixel.rgbRed = ptr[x*4 + 0]; - pixel.rgbGreen = ptr[x*4 + 1]; - pixel.rgbBlue = ptr[x*4 + 2]; - pixel.rgbAlpha = ptr[x*4 + 3]; - stream.write((const char *)&pixel, 4); - } - } - } - - stream.close(); + struct FileHeader bmfh; + struct InfoHeader bmih; + unsigned x, y; + + bmfh.bfType = 0x4d42; + bmfh.bfSize = 14 + 40 + height*width*4; + bmfh.bfReserved1 = 0; + bmfh.bfReserved2 = 0; + bmfh.bfOffBits = 14 + 40; + + bmih.biSize = 40; + bmih.biWidth = width; + bmih.biHeight = height; + bmih.biPlanes = 1; + bmih.biBitCount = 32; + bmih.biCompression = 0; + bmih.biSizeImage = height*width*4; + bmih.biXPelsPerMeter = 0; + bmih.biYPelsPerMeter = 0; + bmih.biClrUsed = 0; + bmih.biClrImportant = 0; + + std::ofstream stream(filename, std::ofstream::binary); + + stream.write((const char *)&bmfh, 14); + stream.write((const char *)&bmih, 40); + + if (flip) { + y = height; + while (y--) { + const unsigned char *ptr = rgba + y * stride; + for (x = 0; x < width; ++x) { + struct Pixel pixel; + pixel.rgbRed = ptr[x*4 + 0]; + pixel.rgbGreen = ptr[x*4 + 1]; + pixel.rgbBlue = ptr[x*4 + 2]; + pixel.rgbAlpha = ptr[x*4 + 3]; + stream.write((const char *)&pixel, 4); + } + } + } else { + for (y = 0; y < height; ++y) { + const unsigned char *ptr = rgba + y * stride; + for (x = 0; x < width; ++x) { + struct Pixel pixel; + pixel.rgbRed = ptr[x*4 + 0]; + pixel.rgbGreen = ptr[x*4 + 1]; + pixel.rgbBlue = ptr[x*4 + 2]; + pixel.rgbAlpha = ptr[x*4 + 3]; + stream.write((const char *)&pixel, 4); + } + } + } + + stream.close(); } diff --git a/d3dshader.py b/d3dshader.py index 6093336c..f48997f2 100644 --- a/d3dshader.py +++ b/d3dshader.py @@ -43,10 +43,10 @@ class D3DShader(Type): print ''' typedef HRESULT (WINAPI *PD3DXDISASSEMBLESHADER)( - CONST DWORD *pShader, - BOOL EnableColorCode, - LPCSTR pComments, - LPD3DXBUFFER *ppDisassembly + CONST DWORD *pShader, + BOOL EnableColorCode, + LPCSTR pComments, + LPD3DXBUFFER *ppDisassembly ); ''' print 'void Dump%s(const DWORD *tokens)' % (self.id) @@ -29,16 +29,16 @@ int main(int argc, char **argv) { - for (int i = 1; i < argc; ++i) { - Trace::Parser p; - if (p.open(argv[i])) { - Trace::Call *call; - call = p.parse_call(); - while (call) { - std::cout << *call; - call = p.parse_call(); - } - } - } - return 0; + for (int i = 1; i < argc; ++i) { + Trace::Parser p; + if (p.open(argv[i])) { + Trace::Call *call; + call = p.parse_call(); + while (call) { + std::cout << *call; + call = p.parse_call(); + } + } + } + return 0; } diff --git a/formatter.hpp b/formatter.hpp index 2ae789d4..3c9db3ca 100644 --- a/formatter.hpp +++ b/formatter.hpp @@ -38,38 +38,38 @@ namespace Formatter { class Attribute { public: - virtual ~Attribute() {} + virtual ~Attribute() {} - virtual void apply(std::ostream &) const {} + virtual void apply(std::ostream &) const {} }; enum Color { - RED, - GREEN, - BLUE, + RED, + GREEN, + BLUE, }; class Formatter { public: - virtual ~Formatter() {} + virtual ~Formatter() {} - virtual Attribute *normal(void) const { return new Attribute; } - virtual Attribute *bold(void) const { return new Attribute; } - virtual Attribute *italic(void) const { return new Attribute; } - virtual Attribute *color(Color) const { return new Attribute; } + virtual Attribute *normal(void) const { return new Attribute; } + virtual Attribute *bold(void) const { return new Attribute; } + virtual Attribute *italic(void) const { return new Attribute; } + virtual Attribute *color(Color) const { return new Attribute; } }; - + class AnsiAttribute : public Attribute { protected: - const char *escape; + const char *escape; public: - AnsiAttribute(const char *_escape) : escape(_escape) {} - void apply(std::ostream& os) const { - os << "\33[" << escape; - } + AnsiAttribute(const char *_escape) : escape(_escape) {} + void apply(std::ostream& os) const { + os << "\33[" << escape; + } }; @@ -81,23 +81,23 @@ public: class AnsiFormatter : public Formatter { protected: public: - virtual Attribute *normal(void) const { return new AnsiAttribute("0m"); } - virtual Attribute *bold(void) const { return new AnsiAttribute("1m"); } - virtual Attribute *italic(void) const { return new AnsiAttribute("3m"); } - virtual Attribute *color(Color c) const { - static const char *color_escapes[] = { - "31m", /* red */ - "32m", /* green */ - "34m", /* blue */ - }; - return new AnsiAttribute(color_escapes[c]); - } + virtual Attribute *normal(void) const { return new AnsiAttribute("0m"); } + virtual Attribute *bold(void) const { return new AnsiAttribute("1m"); } + virtual Attribute *italic(void) const { return new AnsiAttribute("3m"); } + virtual Attribute *color(Color c) const { + static const char *color_escapes[] = { + "31m", /* red */ + "32m", /* green */ + "34m", /* blue */ + }; + return new AnsiAttribute(color_escapes[c]); + } }; inline std::ostream& operator<<(std::ostream& os, const Attribute *attr) { - attr->apply(os); - return os; + attr->apply(os); + return os; } @@ -107,25 +107,25 @@ inline std::ostream& operator<<(std::ostream& os, const Attribute *attr) { class WindowsAttribute : public Attribute { protected: - WORD wAttributes; + WORD wAttributes; public: - WindowsAttribute(WORD _wAttributes) : wAttributes(_wAttributes) {} - void apply(std::ostream& os) const { - DWORD nStdHandleOutput; - if (os == std::cout) { - nStdHandleOutput = STD_OUTPUT_HANDLE; - } else if (os == std::cerr) { - nStdHandleOutput = STD_ERROR_HANDLE; - } else { - return; - } - HANDLE hConsoleOutput = GetStdHandle(nStdHandleOutput); - if (hConsoleOutput == INVALID_HANDLE_VALUE) { - return; - } - - SetConsoleTextAttribute(hConsoleOutput, wAttributes); - } + WindowsAttribute(WORD _wAttributes) : wAttributes(_wAttributes) {} + void apply(std::ostream& os) const { + DWORD nStdHandleOutput; + if (os == std::cout) { + nStdHandleOutput = STD_OUTPUT_HANDLE; + } else if (os == std::cerr) { + nStdHandleOutput = STD_ERROR_HANDLE; + } else { + return; + } + HANDLE hConsoleOutput = GetStdHandle(nStdHandleOutput); + if (hConsoleOutput == INVALID_HANDLE_VALUE) { + return; + } + + SetConsoleTextAttribute(hConsoleOutput, wAttributes); + } }; @@ -135,17 +135,17 @@ public: class WindowsFormatter : public Formatter { protected: public: - virtual Attribute *normal(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); } - virtual Attribute *bold(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); } - virtual Attribute *italic(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); } - virtual Attribute *color(Color c) const { - static const WORD color_escapes[] = { - FOREGROUND_RED | FOREGROUND_INTENSITY, - FOREGROUND_GREEN | FOREGROUND_INTENSITY, - FOREGROUND_BLUE | FOREGROUND_INTENSITY, - }; - return new WindowsAttribute(color_escapes[c]); - } + virtual Attribute *normal(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); } + virtual Attribute *bold(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); } + virtual Attribute *italic(void) const { return new WindowsAttribute(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); } + virtual Attribute *color(Color c) const { + static const WORD color_escapes[] = { + FOREGROUND_RED | FOREGROUND_INTENSITY, + FOREGROUND_GREEN | FOREGROUND_INTENSITY, + FOREGROUND_BLUE | FOREGROUND_INTENSITY, + }; + return new WindowsAttribute(color_escapes[c]); + } }; #endif @@ -153,9 +153,9 @@ public: inline Formatter *defaultFormatter(void) { #ifdef WIN32 - return new WindowsFormatter; + return new WindowsFormatter; #else - return new AnsiFormatter; + return new AnsiFormatter; #endif } diff --git a/os_posix.cpp b/os_posix.cpp index 3f54da2c..4c16096d 100644 --- a/os_posix.cpp +++ b/os_posix.cpp @@ -60,7 +60,7 @@ GetProcessName(char *str, size_t size) ssize_t len; char szProcessPath[PATH_MAX + 1]; char *lpProcessName; - + // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1); if (len == -1) { @@ -91,18 +91,18 @@ GetCurrentDir(char *str, size_t size) void DebugMessage(const char *format, ...) { - va_list ap; - va_start(ap, format); - fflush(stdout); - vfprintf(stderr, format, ap); - va_end(ap); + va_list ap; + va_start(ap, format); + fflush(stdout); + vfprintf(stderr, format, ap); + va_end(ap); } long long GetTime(void) { - struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_usec + tv.tv_sec*1000000LL; + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_usec + tv.tv_sec*1000000LL; } void diff --git a/os_win32.cpp b/os_win32.cpp index 8077bd9a..57a7fbca 100644 --- a/os_win32.cpp +++ b/os_win32.cpp @@ -90,30 +90,30 @@ GetCurrentDir(char *str, size_t size) void DebugMessage(const char *format, ...) { - char buf[4096]; - - va_list ap; - va_start(ap, format); - fflush(stdout); - vsnprintf(buf, sizeof buf, format, ap); - va_end(ap); - - OutputDebugStringA(buf); - if (!IsDebuggerPresent()) { - fflush(stdout); - fputs(buf, stderr); - fflush(stderr); - } + char buf[4096]; + + va_list ap; + va_start(ap, format); + fflush(stdout); + vsnprintf(buf, sizeof buf, format, ap); + va_end(ap); + + OutputDebugStringA(buf); + if (!IsDebuggerPresent()) { + fflush(stdout); + fputs(buf, stderr); + fflush(stderr); + } } long long GetTime(void) { - static LARGE_INTEGER frequency; - LARGE_INTEGER counter; - if(!frequency.QuadPart) - QueryPerformanceFrequency(&frequency); - QueryPerformanceCounter(&counter); - return counter.QuadPart*1000000LL/frequency.QuadPart; + static LARGE_INTEGER frequency; + LARGE_INTEGER counter; + if(!frequency.QuadPart) + QueryPerformanceFrequency(&frequency); + QueryPerformanceCounter(&counter); + return counter.QuadPart*1000000LL/frequency.QuadPart; } void diff --git a/trace_format.hpp b/trace_format.hpp index 43efa0fc..688c65a6 100644 --- a/trace_format.hpp +++ b/trace_format.hpp @@ -31,33 +31,33 @@ namespace Trace { #define TRACE_VERSION 0 enum Event { - EVENT_ENTER = 0, - EVENT_LEAVE, - EVENT_MESSAGE + EVENT_ENTER = 0, + EVENT_LEAVE, + EVENT_MESSAGE }; enum CallDetail { - CALL_END = 0, - CALL_ARG, - CALL_RET, - CALL_THREAD, + CALL_END = 0, + CALL_ARG, + CALL_RET, + CALL_THREAD, }; enum Type { - TYPE_NULL = 0, - TYPE_FALSE, - TYPE_TRUE, - TYPE_SINT, - TYPE_UINT, - TYPE_FLOAT, - TYPE_DOUBLE, - TYPE_STRING, // Null terminated, human readible string - TYPE_BLOB, // Block of bytes - TYPE_ENUM, - TYPE_BITMASK, - TYPE_ARRAY, - TYPE_STRUCT, - TYPE_OPAQUE, + TYPE_NULL = 0, + TYPE_FALSE, + TYPE_TRUE, + TYPE_SINT, + TYPE_UINT, + TYPE_FLOAT, + TYPE_DOUBLE, + TYPE_STRING, // Null terminated, human readible string + TYPE_BLOB, // Block of bytes + TYPE_ENUM, + TYPE_BITMASK, + TYPE_ARRAY, + TYPE_STRUCT, + TYPE_OPAQUE, }; /* diff --git a/trace_model.cpp b/trace_model.cpp index d0bf31d5..2e20b243 100644 --- a/trace_model.cpp +++ b/trace_model.cpp @@ -46,186 +46,186 @@ void Blob::visit(Visitor &visitor) { visitor.visit(this); } class Dumper : public Visitor { protected: - std::ostream &os; - Formatter::Formatter *formatter; - Formatter::Attribute *normal; - Formatter::Attribute *bold; - Formatter::Attribute *italic; - Formatter::Attribute *red; - Formatter::Attribute *pointer; - Formatter::Attribute *literal; + std::ostream &os; + Formatter::Formatter *formatter; + Formatter::Attribute *normal; + Formatter::Attribute *bold; + Formatter::Attribute *italic; + Formatter::Attribute *red; + Formatter::Attribute *pointer; + Formatter::Attribute *literal; public: - Dumper(std::ostream &_os) : os(_os) { - formatter = Formatter::defaultFormatter(); - normal = formatter->normal(); - bold = formatter->bold(); - italic = formatter->italic(); - red = formatter->color(Formatter::RED); - pointer = formatter->color(Formatter::GREEN); - literal = formatter->color(Formatter::BLUE); - } - - ~Dumper() { - delete normal; - delete bold; - delete italic; - delete red; - delete pointer; - delete literal; - delete formatter; - } - - void visit(Null *) { - os << "NULL"; - } - - void visit(Bool *node) { - os << literal << (node->value ? "true" : "false") << normal; - } - - void visit(SInt *node) { - os << literal << node->value << normal; - } - - void visit(UInt *node) { - os << literal << node->value << normal; - } - - void visit(Float *node) { - os << literal << node->value << normal; - } - - void visit(String *node) { - os << literal << '"' << node->value << '"' << normal; - } - - void visit(Enum *node) { - os << literal << node->name << normal; - } - - void visit(Bitmask *bitmask) { - unsigned long long value = bitmask->value; - const Bitmask::Signature *sig = bitmask->sig; - bool first = true; - for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) { - assert(it->second); - if ((value & it->second) == it->second) { - if (!first) { - os << " | "; - } - os << literal << it->first << normal; - value &= ~it->second; - first = false; - } - } - if (value || first) { - if (!first) { - os << " | "; - } - os << literal << std::hex << value << std::dec << normal; - } - } - - void visit(Struct *s) { - const char *sep = ""; - os << "{"; - for (unsigned i = 0; i < s->members.size(); ++i) { - os << sep << italic << s->sig->member_names[i] << normal << " = "; - _visit(s->members[i]); - sep = ", "; - } - os << "}"; - } - - void visit(Array *array) { - if (array->values.size() == 1) { - os << "&"; - _visit(array->values[0]); - } - else { - const char *sep = ""; - os << "{"; - for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) { - os << sep; - _visit(*it); + Dumper(std::ostream &_os) : os(_os) { + formatter = Formatter::defaultFormatter(); + normal = formatter->normal(); + bold = formatter->bold(); + italic = formatter->italic(); + red = formatter->color(Formatter::RED); + pointer = formatter->color(Formatter::GREEN); + literal = formatter->color(Formatter::BLUE); + } + + ~Dumper() { + delete normal; + delete bold; + delete italic; + delete red; + delete pointer; + delete literal; + delete formatter; + } + + void visit(Null *) { + os << "NULL"; + } + + void visit(Bool *node) { + os << literal << (node->value ? "true" : "false") << normal; + } + + void visit(SInt *node) { + os << literal << node->value << normal; + } + + void visit(UInt *node) { + os << literal << node->value << normal; + } + + void visit(Float *node) { + os << literal << node->value << normal; + } + + void visit(String *node) { + os << literal << '"' << node->value << '"' << normal; + } + + void visit(Enum *node) { + os << literal << node->name << normal; + } + + void visit(Bitmask *bitmask) { + unsigned long long value = bitmask->value; + const Bitmask::Signature *sig = bitmask->sig; + bool first = true; + for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) { + assert(it->second); + if ((value & it->second) == it->second) { + if (!first) { + os << " | "; + } + os << literal << it->first << normal; + value &= ~it->second; + first = false; + } + } + if (value || first) { + if (!first) { + os << " | "; + } + os << literal << std::hex << value << std::dec << normal; + } + } + + void visit(Struct *s) { + const char *sep = ""; + os << "{"; + for (unsigned i = 0; i < s->members.size(); ++i) { + os << sep << italic << s->sig->member_names[i] << normal << " = "; + _visit(s->members[i]); + sep = ", "; + } + os << "}"; + } + + void visit(Array *array) { + if (array->values.size() == 1) { + os << "&"; + _visit(array->values[0]); + } + else { + const char *sep = ""; + os << "{"; + for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) { + os << sep; + _visit(*it); + sep = ", "; + } + os << "}"; + } + } + + void visit(Blob *blob) { + os << pointer << "blob(" << blob->size << ")" << normal; + } + + void visit(Call *call) { + const char *sep = ""; + os << bold << call->sig->name << normal << "("; + for (unsigned i = 0; i < call->args.size(); ++i) { + os << sep << italic << call->sig->arg_names[i] << normal << " = "; + _visit(call->args[i]); sep = ", "; - } - os << "}"; - } - } - - void visit(Blob *blob) { - os << pointer << "blob(" << blob->size << ")" << normal; - } - - void visit(Call *call) { - const char *sep = ""; - os << bold << call->sig->name << normal << "("; - for (unsigned i = 0; i < call->args.size(); ++i) { - os << sep << italic << call->sig->arg_names[i] << normal << " = "; - _visit(call->args[i]); - sep = ", "; - } - os << ")"; - if (call->ret) { - os << " = "; - _visit(call->ret); - } - os << "\n"; - } + } + os << ")"; + if (call->ret) { + os << " = "; + _visit(call->ret); + } + os << "\n"; + } }; std::ostream & operator <<(std::ostream &os, Value *value) { - Dumper d(os); - if (value) { - value->visit(d); - } - return os; + Dumper d(os); + if (value) { + value->visit(d); + } + return os; } static inline const Value *unwrap(const Value *node) { - const Enum *c = dynamic_cast<const Enum *>(node); - if (c) - return c->value; - return node; + const Enum *c = dynamic_cast<const Enum *>(node); + if (c) + return c->value; + return node; } Value::operator bool(void) const { - const Bool *b = dynamic_cast<const Bool *>(unwrap(this)); - if (b) - return b->value; - assert(0); - return false; + const Bool *b = dynamic_cast<const Bool *>(unwrap(this)); + if (b) + return b->value; + assert(0); + return false; } Value::operator signed long long(void) const { - const SInt *sint = dynamic_cast<const SInt *>(unwrap(this)); - if (sint) - return sint->value; - const UInt *uint = dynamic_cast<const UInt *>(unwrap(this)); - if (uint) - return uint->value; - assert(0); - return 0; + const SInt *sint = dynamic_cast<const SInt *>(unwrap(this)); + if (sint) + return sint->value; + const UInt *uint = dynamic_cast<const UInt *>(unwrap(this)); + if (uint) + return uint->value; + assert(0); + return 0; } Value::operator unsigned long long(void) const { - const UInt *uint = dynamic_cast<const UInt *>(unwrap(this)); - if (uint) - return uint->value; - assert(0); - return 0; + const UInt *uint = dynamic_cast<const UInt *>(unwrap(this)); + if (uint) + return uint->value; + assert(0); + return 0; } Value::operator double(void) const { - const Float *fl = dynamic_cast<const Float *>(unwrap(this)); - assert(fl); - return fl->value; + const Float *fl = dynamic_cast<const Float *>(unwrap(this)); + assert(fl); + return fl->value; } static Null null; @@ -241,31 +241,31 @@ const Value & Value::operator[](size_t index) const { } void * Value::blob(void) const { - const Blob *blob = dynamic_cast<const Blob *>(unwrap(this)); - if (blob) - return blob->buf; - const Null *null = dynamic_cast<const Null *>(unwrap(this)); - if (null) - return NULL; - assert(0); - return NULL; + const Blob *blob = dynamic_cast<const Blob *>(unwrap(this)); + if (blob) + return blob->buf; + const Null *null = dynamic_cast<const Null *>(unwrap(this)); + if (null) + return NULL; + assert(0); + return NULL; } const char * Value::string(void) const { - const String *string = dynamic_cast<const String *>(unwrap(this)); - if (string) - return string->value.c_str(); - const Null *null = dynamic_cast<const Null *>(unwrap(this)); - if (null) - return NULL; - assert(0); - return NULL; + const String *string = dynamic_cast<const String *>(unwrap(this)); + if (string) + return string->value.c_str(); + const Null *null = dynamic_cast<const Null *>(unwrap(this)); + if (null) + return NULL; + assert(0); + return NULL; } std::ostream & operator <<(std::ostream &os, Call &call) { - Dumper d(os); - d.visit(&call); - return os; + Dumper d(os); + d.visit(&call); + return os; } diff --git a/trace_model.hpp b/trace_model.hpp index 71581e88..e2c0519d 100644 --- a/trace_model.hpp +++ b/trace_model.hpp @@ -47,60 +47,60 @@ class UInt; class Value { public: - virtual void visit(Visitor &visitor) = 0; + virtual void visit(Visitor &visitor) = 0; - operator bool (void) const; - operator signed long long (void) const; - operator unsigned long long (void) const; - operator double (void) const; + operator bool (void) const; + operator signed long long (void) const; + operator unsigned long long (void) const; + operator double (void) const; - void *blob(void) const; - const char *string(void) const; + void *blob(void) const; + const char *string(void) const; - inline operator signed char (void) const { - return static_cast<signed long long>(*this); - } + inline operator signed char (void) const { + return static_cast<signed long long>(*this); + } - inline operator unsigned char (void) const { - return static_cast<signed long long>(*this); - } + inline operator unsigned char (void) const { + return static_cast<signed long long>(*this); + } - inline operator signed short (void) const { - return static_cast<signed long long>(*this); - } + inline operator signed short (void) const { + return static_cast<signed long long>(*this); + } - inline operator unsigned short (void) const { - return static_cast<unsigned long long>(*this); - } + inline operator unsigned short (void) const { + return static_cast<unsigned long long>(*this); + } - inline operator signed (void) const { - return static_cast<signed long long>(*this); - } + inline operator signed (void) const { + return static_cast<signed long long>(*this); + } - inline operator unsigned (void) const { - return static_cast<unsigned long long>(*this); - } + inline operator unsigned (void) const { + return static_cast<unsigned long long>(*this); + } - inline operator signed long (void) const { - return static_cast<signed long long>(*this); - } + inline operator signed long (void) const { + return static_cast<signed long long>(*this); + } - inline operator unsigned long (void) const { - return static_cast<unsigned long long>(*this); - } + inline operator unsigned long (void) const { + return static_cast<unsigned long long>(*this); + } - inline operator float (void) const { - return static_cast<double>(*this); - } + inline operator float (void) const { + return static_cast<double>(*this); + } - const Value & operator[](size_t index) const; + const Value & operator[](size_t index) const; }; class Null : public Value { public: - void visit(Visitor &visitor); + void visit(Visitor &visitor); }; @@ -109,152 +109,152 @@ public: class Bool : public Value { public: - Bool(bool _value) : value(_value) {} + Bool(bool _value) : value(_value) {} - void visit(Visitor &visitor); + void visit(Visitor &visitor); - bool value; + bool value; }; class SInt : public Value { public: - SInt(signed long long _value) : value(_value) {} + SInt(signed long long _value) : value(_value) {} - void visit(Visitor &visitor); + void visit(Visitor &visitor); - signed long long value; + signed long long value; }; class UInt : public Value { public: - UInt(unsigned long long _value) : value(_value) {} + UInt(unsigned long long _value) : value(_value) {} - void visit(Visitor &visitor); + void visit(Visitor &visitor); - unsigned long long value; + unsigned long long value; }; class Float : public Value { public: - Float(double _value) : value(_value) {} + Float(double _value) : value(_value) {} - void visit(Visitor &visitor); + void visit(Visitor &visitor); - double value; + double value; }; class String : public Value { public: - String(std::string _value) : value(_value) {} + String(std::string _value) : value(_value) {} - void visit(Visitor &visitor); + void visit(Visitor &visitor); - std::string value; + std::string value; }; class Enum : public Value { public: - Enum(std::string &_name, Value *_value) : name(_name), value(_value) {} + Enum(std::string &_name, Value *_value) : name(_name), value(_value) {} - void visit(Visitor &visitor); + void visit(Visitor &visitor); - std::string name; - Value *value; + std::string name; + Value *value; }; class Bitmask : public UInt { public: - typedef std::pair<std::string, unsigned long long> Pair; - typedef std::vector<Pair> Signature; + typedef std::pair<std::string, unsigned long long> Pair; + typedef std::vector<Pair> Signature; - Bitmask(const Signature *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {} + Bitmask(const Signature *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {} - void visit(Visitor &visitor); + void visit(Visitor &visitor); - const Signature *sig; + const Signature *sig; }; class Struct : public Value { public: - struct Signature { - std::string name; - std::vector<std::string> member_names; - }; + struct Signature { + std::string name; + std::vector<std::string> member_names; + }; - Struct(Signature *_sig) : sig(_sig), members(_sig->member_names.size()) { } + Struct(Signature *_sig) : sig(_sig), members(_sig->member_names.size()) { } - void visit(Visitor &visitor); + void visit(Visitor &visitor); - const Signature *sig; - std::vector<Value *> members; + const Signature *sig; + std::vector<Value *> members; }; class Array : public Value { public: - Array(size_t len) : values(len) {} + Array(size_t len) : values(len) {} - void visit(Visitor &visitor); + void visit(Visitor &visitor); - std::vector<Value *> values; + std::vector<Value *> values; }; class Blob : public Value { public: - Blob(size_t _size) { - size = _size; - buf = new char[_size]; - } + Blob(size_t _size) { + size = _size; + buf = new char[_size]; + } - ~Blob() { - delete [] buf; - } + ~Blob() { + delete [] buf; + } - void visit(Visitor &visitor); + void visit(Visitor &visitor); - size_t size; - char *buf; + size_t size; + char *buf; }; class Visitor { public: - virtual void visit(Null *) {assert(0);} - virtual void visit(Bool *) {assert(0);} - virtual void visit(SInt *) {assert(0);} - virtual void visit(UInt *) {assert(0);} - virtual void visit(Float *) {assert(0);} - virtual void visit(String *) {assert(0);} - virtual void visit(Enum *) {assert(0);} - virtual void visit(Bitmask *bitmask) {visit(static_cast<UInt *>(bitmask));} - virtual void visit(Struct *) {assert(0);} - virtual void visit(Array *) {assert(0);} - virtual void visit(Blob *) {assert(0);} + virtual void visit(Null *) {assert(0);} + virtual void visit(Bool *) {assert(0);} + virtual void visit(SInt *) {assert(0);} + virtual void visit(UInt *) {assert(0);} + virtual void visit(Float *) {assert(0);} + virtual void visit(String *) {assert(0);} + virtual void visit(Enum *) {assert(0);} + virtual void visit(Bitmask *bitmask) {visit(static_cast<UInt *>(bitmask));} + virtual void visit(Struct *) {assert(0);} + virtual void visit(Array *) {assert(0);} + virtual void visit(Blob *) {assert(0);} protected: - inline void _visit(Value *value) { - if (value) { - value->visit(*this); - } - } + inline void _visit(Value *value) { + if (value) { + value->visit(*this); + } + } }; @@ -269,25 +269,25 @@ double asFloat(const Value &node); class Call { public: - struct Signature { - std::string name; - std::vector<std::string> arg_names; - }; + struct Signature { + std::string name; + std::vector<std::string> arg_names; + }; - unsigned no; - const Signature *sig; - std::vector<Value *> args; - Value *ret; + unsigned no; + const Signature *sig; + std::vector<Value *> args; + Value *ret; - Call(Signature *_sig) : sig(_sig), args(_sig->arg_names.size()), ret(0) { } + Call(Signature *_sig) : sig(_sig), args(_sig->arg_names.size()), ret(0) { } - inline const std::string name(void) const { - return sig->name; - } + inline const std::string name(void) const { + return sig->name; + } - inline Value & arg(unsigned index) { - return *(args[index]); - } + inline Value & arg(unsigned index) { + return *(args[index]); + } }; diff --git a/trace_parser.hpp b/trace_parser.hpp index 6b519a07..04dfd866 100644 --- a/trace_parser.hpp +++ b/trace_parser.hpp @@ -49,355 +49,355 @@ namespace Trace { class Parser { protected: - gzFile file; + gzFile file; - typedef std::list<Call *> CallList; - CallList calls; + typedef std::list<Call *> CallList; + CallList calls; - typedef std::vector<Call::Signature *> FunctionMap; - FunctionMap functions; + typedef std::vector<Call::Signature *> FunctionMap; + FunctionMap functions; - typedef std::vector<Struct::Signature *> StructMap; - StructMap structs; + typedef std::vector<Struct::Signature *> StructMap; + StructMap structs; - typedef std::vector<Enum *> EnumMap; - EnumMap enums; + typedef std::vector<Enum *> EnumMap; + EnumMap enums; - typedef std::vector<Bitmask::Signature *> BitmaskMap; - BitmaskMap bitmasks; + typedef std::vector<Bitmask::Signature *> BitmaskMap; + BitmaskMap bitmasks; - unsigned next_call_no; + unsigned next_call_no; public: - Parser() { - file = NULL; - next_call_no = 0; - } - - ~Parser() { - close(); - } - - bool open(const char *filename) { - unsigned long long version; - - file = gzopen(filename, "rb"); - if (!file) { - return false; - } - - version = read_uint(); - if (version != TRACE_VERSION) { - std::cerr << "error: unsupported format version" << version << "\n"; - return false; - } - - return true; - } - - void close(void) { - if (file) { - gzclose(file); - file = NULL; - } - } - - Call *parse_call(void) { - do { - int c = read_byte(); - switch(c) { - case Trace::EVENT_ENTER: - parse_enter(); - break; - case Trace::EVENT_LEAVE: - return parse_leave(); - case Trace::EVENT_MESSAGE: - std::cerr << "message: " << read_string() << "\n"; - break; - default: - std::cerr << "error: unknown call detail " << c << "\n"; - assert(0); - /* fallthrough */ - case -1: + Parser() { + file = NULL; + next_call_no = 0; + } + + ~Parser() { + close(); + } + + bool open(const char *filename) { + unsigned long long version; + + file = gzopen(filename, "rb"); + if (!file) { + return false; + } + + version = read_uint(); + if (version != TRACE_VERSION) { + std::cerr << "error: unsupported format version" << version << "\n"; + return false; + } + + return true; + } + + void close(void) { + if (file) { + gzclose(file); + file = NULL; + } + } + + Call *parse_call(void) { + do { + int c = read_byte(); + switch(c) { + case Trace::EVENT_ENTER: + parse_enter(); + break; + case Trace::EVENT_LEAVE: + return parse_leave(); + case Trace::EVENT_MESSAGE: + std::cerr << "message: " << read_string() << "\n"; + break; + default: + std::cerr << "error: unknown call detail " << c << "\n"; + assert(0); + /* fallthrough */ + case -1: + return NULL; + } + } while(true); + } + + /** + * Helper function to lookup an ID in a vector, resizing the vector if it doesn't fit. + */ + template<class T> + T *lookup(std::vector<T *> &map, size_t index) { + if (index >= map.size()) { + map.resize(index + 1); + return NULL; + } else { + return map[index]; + } + } + + void parse_enter(void) { + size_t id = read_uint(); + + Call::Signature *sig = lookup(functions, id); + if (!sig) { + sig = new Call::Signature; + sig->name = read_string(); + unsigned size = read_uint(); + for (unsigned i = 0; i < size; ++i) { + sig->arg_names.push_back(read_string()); + } + functions[id] = sig; + } + assert(sig); + + Call *call = new Call(sig); + call->no = next_call_no++; + + parse_call_details(call); + + calls.push_back(call); + } + + Call *parse_leave(void) { + unsigned call_no = read_uint(); + + Call *call = NULL; + for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) { + if ((*it)->no == call_no) { + call = *it; + calls.erase(it); + break; + } + } + assert(call); + if (!call) { return NULL; - } - } while(true); - } - - /** - * Helper function to lookup an ID in a vector, resizing the vector if it doesn't fit. - */ - template<class T> - T *lookup(std::vector<T *> &map, size_t index) { - if (index >= map.size()) { - map.resize(index + 1); - return NULL; - } else { - return map[index]; - } - } - - void parse_enter(void) { - size_t id = read_uint(); - - Call::Signature *sig = lookup(functions, id); - if (!sig) { - sig = new Call::Signature; - sig->name = read_string(); - unsigned size = read_uint(); - for (unsigned i = 0; i < size; ++i) { - sig->arg_names.push_back(read_string()); - } - functions[id] = sig; - } - assert(sig); - - Call *call = new Call(sig); - call->no = next_call_no++; - - parse_call_details(call); - - calls.push_back(call); - } - - Call *parse_leave(void) { - unsigned call_no = read_uint(); - - Call *call = NULL; - for (CallList::iterator it = calls.begin(); it != calls.end(); ++it) { - if ((*it)->no == call_no) { - call = *it; - calls.erase(it); - break; - } - } - assert(call); - if (!call) { - return NULL; - } - parse_call_details(call); - return call; - } - - void parse_call_details(Call *call) { - do { - int c = read_byte(); - switch(c) { - case Trace::CALL_END: - return; - case Trace::CALL_ARG: - parse_arg(call); - break; - case Trace::CALL_RET: - call->ret = parse_value(); - break; - default: - std::cerr << "error: unknown call detail " << c << "\n"; + } + parse_call_details(call); + return call; + } + + void parse_call_details(Call *call) { + do { + int c = read_byte(); + switch(c) { + case Trace::CALL_END: + return; + case Trace::CALL_ARG: + parse_arg(call); + break; + case Trace::CALL_RET: + call->ret = parse_value(); + break; + default: + std::cerr << "error: unknown call detail " << c << "\n"; + assert(0); + /* fallthrough */ + case -1: + return; + } + } while(true); + } + + void parse_arg(Call *call) { + unsigned index = read_uint(); + Value *value = parse_value(); + if (index >= call->args.size()) { + call->args.resize(index + 1); + } + call->args[index] = value; + } + + Value *parse_value(void) { + int c; + c = read_byte(); + switch(c) { + case Trace::TYPE_NULL: + return new Null; + case Trace::TYPE_FALSE: + return new Bool(false); + case Trace::TYPE_TRUE: + return new Bool(true); + case Trace::TYPE_SINT: + return parse_sint(); + case Trace::TYPE_UINT: + return parse_uint(); + case Trace::TYPE_FLOAT: + return parse_float(); + case Trace::TYPE_DOUBLE: + return parse_double(); + case Trace::TYPE_STRING: + return parse_string(); + case Trace::TYPE_ENUM: + return parse_enum(); + case Trace::TYPE_BITMASK: + return parse_bitmask(); + case Trace::TYPE_ARRAY: + return parse_array(); + case Trace::TYPE_STRUCT: + return parse_struct(); + case Trace::TYPE_BLOB: + return parse_blob(); + case Trace::TYPE_OPAQUE: + return parse_opaque(); + default: + std::cerr << "error: unknown type " << c << "\n"; assert(0); - /* fallthrough */ - case -1: - return; - } - } while(true); - } - - void parse_arg(Call *call) { - unsigned index = read_uint(); - Value *value = parse_value(); - if (index >= call->args.size()) { - call->args.resize(index + 1); - } - call->args[index] = value; - } - - Value *parse_value(void) { - int c; - c = read_byte(); - switch(c) { - case Trace::TYPE_NULL: - return new Null; - case Trace::TYPE_FALSE: - return new Bool(false); - case Trace::TYPE_TRUE: - return new Bool(true); - case Trace::TYPE_SINT: - return parse_sint(); - case Trace::TYPE_UINT: - return parse_uint(); - case Trace::TYPE_FLOAT: - return parse_float(); - case Trace::TYPE_DOUBLE: - return parse_double(); - case Trace::TYPE_STRING: - return parse_string(); - case Trace::TYPE_ENUM: - return parse_enum(); - case Trace::TYPE_BITMASK: - return parse_bitmask(); - case Trace::TYPE_ARRAY: - return parse_array(); - case Trace::TYPE_STRUCT: - return parse_struct(); - case Trace::TYPE_BLOB: - return parse_blob(); - case Trace::TYPE_OPAQUE: - return parse_opaque(); - default: - std::cerr << "error: unknown type " << c << "\n"; - assert(0); - return NULL; - } - } - - Value *parse_sint() { - return new SInt(-(signed long long)read_uint()); - } - - Value *parse_uint() { - return new UInt(read_uint()); - } - - Value *parse_float() { - float value; - gzread(file, &value, sizeof value); - return new Float(value); - } - - Value *parse_double() { - double value; - gzread(file, &value, sizeof value); - return new Float(value); - } - - Value *parse_string() { - return new String(read_string()); - } - - Value *parse_enum() { - size_t id = read_uint(); - Enum *sig = lookup(enums, id); - if (!sig) { - std::string name = read_string(); - Value *value = parse_value(); - sig = new Enum(name, value); - enums[id] = sig; - } - assert(sig); - return sig; - } - - Value *parse_bitmask() { - size_t id = read_uint(); - Bitmask::Signature *sig = lookup(bitmasks, id); - if (!sig) { - size_t size = read_uint(); - sig = new Bitmask::Signature(size); - for (Bitmask::Signature::iterator it = sig->begin(); it != sig->end(); ++it) { - it->first = read_string(); - it->second = read_uint(); - assert(it->second); - } - bitmasks[id] = sig; - } - assert(sig); - - unsigned long long value = read_uint(); - - return new Bitmask(sig, value); - } - - Value *parse_array(void) { - size_t len = read_uint(); - Array *array = new Array(len); - for (size_t i = 0; i < len; ++i) { - array->values[i] = parse_value(); - } - return array; - } - - Value *parse_blob(void) { - size_t size = read_uint(); - Blob *blob = new Blob(size); - if (size) { - gzread(file, blob->buf, size); - } - return blob; - } - - Value *parse_struct() { - size_t id = read_uint(); - - Struct::Signature *sig = lookup(structs, id); - if (!sig) { - sig = new Struct::Signature; - sig->name = read_string(); - unsigned size = read_uint(); - for (unsigned i = 0; i < size; ++i) { - sig->member_names.push_back(read_string()); - } - structs[id] = sig; - } - assert(sig); - - Struct *value = new Struct(sig); - - for (size_t i = 0; i < sig->member_names.size(); ++i) { - value->members[i] = parse_value(); - } - - return value; - } - - Value *parse_opaque() { - unsigned long long addr; - addr = read_uint(); - /* XXX */ - return new UInt(addr); - } - - std::string read_string(void) { - size_t len = read_uint(); - if (!len) { - return std::string(); - } - char * buf = new char[len]; - gzread(file, buf, len); - std::string value(buf, len); - delete [] buf; + return NULL; + } + } + + Value *parse_sint() { + return new SInt(-(signed long long)read_uint()); + } + + Value *parse_uint() { + return new UInt(read_uint()); + } + + Value *parse_float() { + float value; + gzread(file, &value, sizeof value); + return new Float(value); + } + + Value *parse_double() { + double value; + gzread(file, &value, sizeof value); + return new Float(value); + } + + Value *parse_string() { + return new String(read_string()); + } + + Value *parse_enum() { + size_t id = read_uint(); + Enum *sig = lookup(enums, id); + if (!sig) { + std::string name = read_string(); + Value *value = parse_value(); + sig = new Enum(name, value); + enums[id] = sig; + } + assert(sig); + return sig; + } + + Value *parse_bitmask() { + size_t id = read_uint(); + Bitmask::Signature *sig = lookup(bitmasks, id); + if (!sig) { + size_t size = read_uint(); + sig = new Bitmask::Signature(size); + for (Bitmask::Signature::iterator it = sig->begin(); it != sig->end(); ++it) { + it->first = read_string(); + it->second = read_uint(); + assert(it->second); + } + bitmasks[id] = sig; + } + assert(sig); + + unsigned long long value = read_uint(); + + return new Bitmask(sig, value); + } + + Value *parse_array(void) { + size_t len = read_uint(); + Array *array = new Array(len); + for (size_t i = 0; i < len; ++i) { + array->values[i] = parse_value(); + } + return array; + } + + Value *parse_blob(void) { + size_t size = read_uint(); + Blob *blob = new Blob(size); + if (size) { + gzread(file, blob->buf, size); + } + return blob; + } + + Value *parse_struct() { + size_t id = read_uint(); + + Struct::Signature *sig = lookup(structs, id); + if (!sig) { + sig = new Struct::Signature; + sig->name = read_string(); + unsigned size = read_uint(); + for (unsigned i = 0; i < size; ++i) { + sig->member_names.push_back(read_string()); + } + structs[id] = sig; + } + assert(sig); + + Struct *value = new Struct(sig); + + for (size_t i = 0; i < sig->member_names.size(); ++i) { + value->members[i] = parse_value(); + } + + return value; + } + + Value *parse_opaque() { + unsigned long long addr; + addr = read_uint(); + /* XXX */ + return new UInt(addr); + } + + std::string read_string(void) { + size_t len = read_uint(); + if (!len) { + return std::string(); + } + char * buf = new char[len]; + gzread(file, buf, len); + std::string value(buf, len); + delete [] buf; #if TRACE_VERBOSE - std::cerr << "\tSTRING \"" << value << "\"\n"; + std::cerr << "\tSTRING \"" << value << "\"\n"; #endif - return value; - } - - unsigned long long read_uint(void) { - unsigned long long value = 0; - int c; - unsigned shift = 0; - do { - c = gzgetc(file); - if (c == -1) { - break; - } - value |= (unsigned long long)(c & 0x7f) << shift; - shift += 7; - } while(c & 0x80); + return value; + } + + unsigned long long read_uint(void) { + unsigned long long value = 0; + int c; + unsigned shift = 0; + do { + c = gzgetc(file); + if (c == -1) { + break; + } + value |= (unsigned long long)(c & 0x7f) << shift; + shift += 7; + } while(c & 0x80); #if TRACE_VERBOSE - std::cerr << "\tUINT " << value << "\n"; + std::cerr << "\tUINT " << value << "\n"; #endif - return value; - } + return value; + } - int read_byte(void) { - int c = gzgetc(file); + int read_byte(void) { + int c = gzgetc(file); #if TRACE_VERBOSE - if (c < 0) - std::cerr << "\tEOF" << "\n"; - else - std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n"; + if (c < 0) + std::cerr << "\tEOF" << "\n"; + else + std::cerr << "\tBYTE 0x" << std::hex << c << std::dec << "\n"; #endif - return c; - } + return c; + } }; diff --git a/trace_write.cpp b/trace_write.cpp index 854db104..15d1d1fd 100644 --- a/trace_write.cpp +++ b/trace_write.cpp @@ -44,101 +44,101 @@ namespace Trace { static gzFile g_gzFile = NULL; static void _Close(void) { - if(g_gzFile != NULL) { - gzclose(g_gzFile); - g_gzFile = NULL; - } + if(g_gzFile != NULL) { + gzclose(g_gzFile); + g_gzFile = NULL; + } } static void _Open(const char *szExtension) { - _Close(); - - static unsigned dwCounter = 0; - - char szFileName[PATH_MAX]; - const char *lpFileName; - - lpFileName = getenv("TRACE_PATH"); - 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); - - g_gzFile = gzopen(szFileName, "wb"); + _Close(); + + static unsigned dwCounter = 0; + + char szFileName[PATH_MAX]; + const char *lpFileName; + + lpFileName = getenv("TRACE_PATH"); + 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); + + g_gzFile = gzopen(szFileName, "wb"); } static inline void Write(const void *sBuffer, size_t dwBytesToWrite) { - if (g_gzFile == NULL) - return; - - gzwrite(g_gzFile, sBuffer, dwBytesToWrite); + if (g_gzFile == NULL) + return; + + gzwrite(g_gzFile, sBuffer, dwBytesToWrite); } static inline void WriteByte(char c) { - Write(&c, 1); + Write(&c, 1); } void inline WriteUInt(unsigned long long value) { - char buf[2 * sizeof value]; - unsigned len; + char buf[2 * sizeof value]; + unsigned len; - len = 0; - do { - assert(len < sizeof buf); - buf[len] = 0x80 | (value & 0x7f); - value >>= 7; - ++len; - } while (value); + len = 0; + do { + assert(len < sizeof buf); + buf[len] = 0x80 | (value & 0x7f); + value >>= 7; + ++len; + } while (value); - assert(len); - buf[len - 1] &= 0x7f; + assert(len); + buf[len - 1] &= 0x7f; - Write(buf, len); + Write(buf, len); } static inline void WriteFloat(float value) { - assert(sizeof value == 4); - Write((const char *)&value, sizeof value); + assert(sizeof value == 4); + Write((const char *)&value, sizeof value); } static inline void WriteDouble(double value) { - assert(sizeof value == 8); - Write((const char *)&value, sizeof value); + assert(sizeof value == 8); + Write((const char *)&value, sizeof value); } static inline void WriteString(const char *str) { - size_t len = strlen(str); - WriteUInt(len); - Write(str, len); + size_t len = strlen(str); + WriteUInt(len); + Write(str, len); } void Open(void) { @@ -149,18 +149,18 @@ void Open(void) { } void Close(void) { - _Close(); + _Close(); } static unsigned call_no = 0; inline bool lookup(std::vector<bool> &map, size_t index) { - if (index >= map.size()) { - map.resize(index + 1); - return false; - } else { - return map[index]; - } + if (index >= map.size()) { + map.resize(index + 1); + return false; + } else { + return map[index]; + } } static std::vector<bool> functions; @@ -170,170 +170,170 @@ static std::vector<bool> bitmasks; unsigned BeginEnter(const FunctionSig &function) { - OS::AcquireMutex(); - Open(); - 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.args[i]); - } - functions[function.id] = true; - } - return call_no++; + OS::AcquireMutex(); + Open(); + 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.args[i]); + } + functions[function.id] = true; + } + return call_no++; } void EndEnter(void) { - WriteByte(Trace::CALL_END); - gzflush(g_gzFile, Z_SYNC_FLUSH); - OS::ReleaseMutex(); + WriteByte(Trace::CALL_END); + gzflush(g_gzFile, Z_SYNC_FLUSH); + OS::ReleaseMutex(); } void BeginLeave(unsigned call) { - OS::AcquireMutex(); - WriteByte(Trace::EVENT_LEAVE); - WriteUInt(call); + OS::AcquireMutex(); + WriteByte(Trace::EVENT_LEAVE); + WriteUInt(call); } void EndLeave(void) { - WriteByte(Trace::CALL_END); - gzflush(g_gzFile, Z_SYNC_FLUSH); - OS::ReleaseMutex(); + WriteByte(Trace::CALL_END); + gzflush(g_gzFile, Z_SYNC_FLUSH); + OS::ReleaseMutex(); } void BeginArg(unsigned index) { - WriteByte(Trace::CALL_ARG); - WriteUInt(index); + WriteByte(Trace::CALL_ARG); + WriteUInt(index); } void BeginReturn(void) { - WriteByte(Trace::CALL_RET); + WriteByte(Trace::CALL_RET); } void BeginArray(size_t length) { - WriteByte(Trace::TYPE_ARRAY); - WriteUInt(length); + WriteByte(Trace::TYPE_ARRAY); + WriteUInt(length); } void BeginStruct(const StructSig *sig) { - WriteByte(Trace::TYPE_STRUCT); - WriteUInt(sig->id); - if (!lookup(structs, sig->id)) { - WriteString(sig->name); - WriteUInt(sig->num_members); - for (unsigned i = 0; i < sig->num_members; ++i) { - WriteString(sig->members[i]); - } - structs[sig->id] = true; - } + WriteByte(Trace::TYPE_STRUCT); + WriteUInt(sig->id); + if (!lookup(structs, sig->id)) { + WriteString(sig->name); + WriteUInt(sig->num_members); + for (unsigned i = 0; i < sig->num_members; ++i) { + WriteString(sig->members[i]); + } + structs[sig->id] = true; + } } void LiteralBool(bool value) { - WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE); + WriteByte(value ? Trace::TYPE_TRUE : Trace::TYPE_FALSE); } void LiteralSInt(signed long long value) { - if (value < 0) { - WriteByte(Trace::TYPE_SINT); - WriteUInt(-value); - } else { - WriteByte(Trace::TYPE_UINT); - WriteUInt(value); - } + if (value < 0) { + WriteByte(Trace::TYPE_SINT); + WriteUInt(-value); + } else { + WriteByte(Trace::TYPE_UINT); + WriteUInt(value); + } } void LiteralUInt(unsigned long long value) { - WriteByte(Trace::TYPE_UINT); - WriteUInt(value); + WriteByte(Trace::TYPE_UINT); + WriteUInt(value); } void LiteralFloat(float value) { - WriteByte(Trace::TYPE_FLOAT); - WriteFloat(value); + WriteByte(Trace::TYPE_FLOAT); + WriteFloat(value); } void LiteralFloat(double value) { - WriteByte(Trace::TYPE_DOUBLE); - WriteDouble(value); + WriteByte(Trace::TYPE_DOUBLE); + WriteDouble(value); } void LiteralString(const char *str) { - if (!str) { - LiteralNull(); - return; - } - WriteByte(Trace::TYPE_STRING); - WriteString(str); + if (!str) { + LiteralNull(); + return; + } + WriteByte(Trace::TYPE_STRING); + WriteString(str); } void LiteralString(const char *str, size_t len) { - if (!str) { - LiteralNull(); - return; - } - WriteByte(Trace::TYPE_STRING); - WriteUInt(len); - Write(str, len); + if (!str) { + LiteralNull(); + return; + } + WriteByte(Trace::TYPE_STRING); + WriteUInt(len); + Write(str, len); } void LiteralWString(const wchar_t *str) { - if (!str) { - LiteralNull(); - return; - } - WriteByte(Trace::TYPE_STRING); - WriteString("<wide-string>"); -} - + if (!str) { + LiteralNull(); + return; + } + WriteByte(Trace::TYPE_STRING); + WriteString("<wide-string>"); +} + void LiteralBlob(const void *data, size_t size) { - if (!data) { - LiteralNull(); - return; - } - WriteByte(Trace::TYPE_BLOB); - WriteUInt(size); - if (size) { - Write(data, size); - } + if (!data) { + LiteralNull(); + return; + } + WriteByte(Trace::TYPE_BLOB); + WriteUInt(size); + if (size) { + Write(data, size); + } } void LiteralEnum(const EnumSig *sig) { - WriteByte(Trace::TYPE_ENUM); - WriteUInt(sig->id); - if (!lookup(enums, sig->id)) { - WriteString(sig->name); - LiteralSInt(sig->value); - enums[sig->id] = true; - } + WriteByte(Trace::TYPE_ENUM); + WriteUInt(sig->id); + if (!lookup(enums, sig->id)) { + WriteString(sig->name); + LiteralSInt(sig->value); + enums[sig->id] = true; + } } void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value) { - WriteByte(Trace::TYPE_BITMASK); - WriteUInt(bitmask.id); - if (!lookup(bitmasks, bitmask.id)) { - WriteUInt(bitmask.count); - for (unsigned i = 0; i < bitmask.count; ++i) { - WriteString(bitmask.values[i].name); - WriteUInt(bitmask.values[i].value); - } - bitmasks[bitmask.id] = true; - } - WriteUInt(value); + WriteByte(Trace::TYPE_BITMASK); + WriteUInt(bitmask.id); + if (!lookup(bitmasks, bitmask.id)) { + WriteUInt(bitmask.count); + for (unsigned i = 0; i < bitmask.count; ++i) { + WriteString(bitmask.values[i].name); + WriteUInt(bitmask.values[i].value); + } + bitmasks[bitmask.id] = true; + } + WriteUInt(value); } void LiteralNull(void) { - WriteByte(Trace::TYPE_NULL); + WriteByte(Trace::TYPE_NULL); } void LiteralOpaque(const void *addr) { - if (!addr) { - LiteralNull(); - return; - } - WriteByte(Trace::TYPE_OPAQUE); - WriteUInt((size_t)addr); + if (!addr) { + LiteralNull(); + return; + } + WriteByte(Trace::TYPE_OPAQUE); + WriteUInt((size_t)addr); } void Abort(void) { diff --git a/trace_write.hpp b/trace_write.hpp index 66057df2..ca4addc5 100644 --- a/trace_write.hpp +++ b/trace_write.hpp @@ -63,13 +63,13 @@ namespace Trace { void Open(void); void Close(void); - + unsigned BeginEnter(const FunctionSig &function); void EndEnter(void); - + void BeginLeave(unsigned call); void EndLeave(void); - + void BeginArg(unsigned index); inline void EndArg(void) {} @@ -130,7 +130,7 @@ LAYERPLANEDESCRIPTOR = Struct("LAYERPLANEDESCRIPTOR", [ LPLAYERPLANEDESCRIPTOR = Pointer(LAYERPLANEDESCRIPTOR) WGLSWAP = Struct("WGLSWAP", [ - (HDC, "hdc"), + (HDC, "hdc"), (UINT, "uiFlags"), ]) |