Trace points are the new debugging aid in Visual Studio. Break points are the general feature in almost all debuggers but not trace points. It’s much popular in GDB community even it’s a bit late, Microsoft introduced trace points with Visual Studio 2005
Why we would use Trace points?
Sometimes during debugging it’s not possible to interrupt the program execution. The correctness of the program may affected by the delay introduced by the debugger. In such cases we can use trace points. As the name resembles break points breaks the execution of the program while trace points doesn’t (or optional). Trace points provide additional options to know the debugging information on Trace point location.
Take the following example. The function Display is being called from two different threads. To know from where the call been made, you can get the help of trace points. This scenario is common in a multi thread program
[sourcecode language='cpp']
#include
void DisplayFxn( int i )
{
printf( “\n%d”,i);
}
void ProcessValue(int i, const int nThreadID )
{
DisplayFxn(i);
}
DWORD WINAPI Thread1(LPVOID)
{
for(int i = 0; i< 100; i++ )
{
ProcessValue( i,GetCurrentThreadId());
Sleep(600);
}
return 0;
}
DWORD WINAPI Thread2(LPVOID)
{
for(int i = 0; i< 100; i++ )
{
ProcessValue( i,GetCurrentThreadId() );
Sleep(300);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE h[2];
h[0] = CreateThread( 0,0, Thread1,0,0,0);
h[1] = CreateThread( 0,0, Thread2,0,0,0);
WaitForMultipleObjects(2,h,TRUE,INFINITE);
return 0;
}
[/sourcecode]
First of all you’ve to create a trace point. You can create trace points from the scratch or convert an existing break point to trace point.
Now it’s time to specify the configuration of your trace point.
As you’re seeing in the configuration box, you can simply print the values of your variable by just giving the variable name inside curly braces.
You can also apply the appropriate predefined keywords to get enough information on the execution context. The basic list of the keywords are shown in the configuration box itself. Beside this, you can also use $TICK get current CPU tick and $FILEPOSE to insert the current file position. You can see a sample construct above. It may give following result where it contains all the specified data in output
[sourcecode language='cpp']
IN: 20Fxn: DisplayFxn(int),Thread: 0x140C Thread2 CPU Tick:0x4EC5FE Call Stack: TracePointSample.exe!DisplayFxn()
TracePointSample.exe!ProcessValue()
TracePointSample.exe!Thread2()
kernel32.dll!76ea36d6()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!77a5883c()
ntdll.dll!77a5880f()
IN: 11Fxn: DisplayFxn(int),Thread: 0xF74 Thread1 CPU Tick:0x4EC6C9 Call Stack: TracePointSample.exe!DisplayFxn()
TracePointSample.exe!ProcessValue()
TracePointSample.exe!Thread1()
kernel32.dll!76ea36d6()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!77a5883c()
ntdll.dll!77a5880f()
[/sourcecode]
Ok it’s time for your own try. Remember once more, this can be used effectively in time-out debugging situation(server client where server has a put some time out but delay from client because of debugging), debugging paint operations etc…. Check the documentation as well for more information