Skip to content

Commit d5d5eff

Browse files
committed
Improved DEBUG_TRACE, printStack()
printStack() doesn't seem to work yet.
1 parent e2e51c2 commit d5d5eff

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

PythonScript/project/PythonSettings.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<ImportGroup Label="PropertySheets" />
44
<PropertyGroup Label="UserMacros">
55
<BoostBase>E:\libs\boost_1_55_0</BoostBase>
6-
<PythonBase>E:\work\Python-2.7</PythonBase>
6+
<PythonBase>E:\work\PythonGit\cpython</PythonBase>
77
<PythonLibPath>$(PythonBase)\PCbuild</PythonLibPath>
88
<BoostPythonLibPath>$(BoostBase)\stage\lib</BoostPythonLibPath>
99
<HtmlHelpBase>C:\Program Files (x86)\HTML Help Workshop</HtmlHelpBase>

PythonScript/src/DebugTrace.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
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

1859
void debugTraceStr(const std::wstringstream& item)
1960
{
2061
static volatile long *lineCount = allocateLineCount();
62+
FILETIME timestamp;
63+
GetSystemTimeAsFileTime(&timestamp);
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+
}

PythonScript/src/DebugTrace.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ void debugTrace(T msg)
1919
debugTraceStr(debug);
2020
}
2121

22+
void printStack();
2223

2324
void debugTraceStr(const std::wstringstream& item);
25+
void debugTraceVars(const char *format, ...);
2426

2527

2628
#define DEBUG_TRACE(msg) debugTrace(msg)
2729

30+
#define DEBUG_TRACE_S(args) debugTraceVars args
2831
#endif // DEBUGTRACE_20140215_H

PythonScript/src/stdafx.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
#define _WIN32_IE 0x0600 // Change this to the appropriate value to target other versions of IE.
2424
#endif
2525

26+
// The stacktrace function only works on > NTDDI_WINXP, so for debug we limit it
27+
#ifdef _DEBUG
28+
#define NTDDI_VERSION NTDDI_WINXPSP2
29+
#endif
30+
2631
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
2732
#define NOMINMAX
2833
// Windows Header Files:

0 commit comments

Comments
 (0)