11#include " stdafx.h"
22#include " DebugTrace.h"
3+ #include < malloc.h>
4+ #include < stdio.h>
5+ #include < stdarg.h>
6+ #include < string.h>
7+ #include < Windows.h>
8+ #include < DbgHelp.h>
39
410// stati volatile long s_debugLineCount;
511
@@ -14,13 +20,66 @@ volatile long *allocateLineCount()
1420 return lineCount;
1521}
1622
23+ void printStack ()
24+ {
25+ unsigned int i;
26+ void * stack[ 100 ];
27+ unsigned short frames;
28+ SYMBOL_INFO * symbol;
29+ HANDLE process;
30+ char buffer[500 ];
31+ int bufferRemaining = 500 ;
32+ int usedBuffer;
33+ char *output = buffer;
34+
35+ process = GetCurrentProcess ();
36+
37+ SymInitialize ( process, NULL , TRUE );
38+
39+ frames = CaptureStackBackTrace ( 1 , 15 , stack, NULL );
40+ symbol = ( SYMBOL_INFO * )calloc ( sizeof ( SYMBOL_INFO ) + 256 * sizeof ( char ), 1 );
41+ symbol->MaxNameLen = 255 ;
42+ symbol->SizeOfStruct = sizeof ( SYMBOL_INFO );
43+
44+
45+ for ( i = 0 ; i < frames; i++ )
46+ {
47+ SymFromAddr ( process, ( DWORD64 )( stack[ i ] ), 0 , symbol );
48+
49+ usedBuffer = _snprintf_s (output, bufferRemaining, bufferRemaining, " %i: %s - 0x%0X\n " , frames - i - 1 , symbol->Name , symbol->Address );
50+ bufferRemaining -= usedBuffer;
51+ output += usedBuffer;
52+ }
53+
54+ debugTraceVars (" STACK %s" , buffer);
55+
56+ free ( symbol );
57+ }
1758
1859void debugTraceStr (const std::wstringstream& item)
1960{
2061 static volatile long *lineCount = allocateLineCount ();
62+ FILETIME timestamp;
63+ GetSystemTimeAsFileTime (×tamp);
2164
65+
2266 long currentLine = InterlockedIncrement (lineCount);
2367 std::wstringstream debug;
24- debug << L" PY>" << std::setfill (L' 0' ) << std::setw (5 ) << ::GetCurrentThreadId () << L" " << std::setw (5 ) << std::setfill (L' ' ) << currentLine << L" " << item.str ();
68+ debug << L" PY>" << std::setfill (L' 0' ) << std::setw (5 ) << ::GetCurrentThreadId ()
69+ << L" " << std::setw (5 ) << std::setfill (L' ' ) << currentLine
70+ << L" " << std::setw (8 ) << std::setfill (L' ' ) << timestamp.dwLowDateTime
71+ << L" " << item.str ();
72+
2573 OutputDebugString (debug.str ().c_str ());
2674}
75+
76+ void debugTraceVars (const char *format, ...)
77+ {
78+ va_list args;
79+ va_start ( args, format );
80+ char buffer[500 ];
81+ vsprintf_s<500 >(buffer, format, args);
82+ std::wstringstream result;
83+ result << buffer;
84+ debugTraceStr (result);
85+ }
0 commit comments