January 01, 2005
Diagnostics traces
Here is a common and useful debugging technique:
Supposing C++ was extended to support this, the debugger might have a property page with function names and checkboxes. The checkboxes would be used to enable or disable the diagnostics trace for each function. They would obviously default to off.
How would C++ need to be extended? The compiler could generate code equivalent to this:
For some functions, for example those invoked from the game's innermost loops, the overhead of checking the value of g_trace_foo or equivalent would be too great, even in debug builds. So for these cases we would need a way of directing the compiler to statically disable tracing. Maybe something like this:
void foo(int a, double b)Sometimes we need a picture of what the program is doing that cannot be determined by using debugging techniques like breakpoints or stepping through code. For example, we might need to see how a function is used over the course of several frames. How can we improve this tracing code? For starters, there is a considerable amount of redundancy. This would be better:
{
printf("Enters %s/%d foo(%d, %f)", __FILE__, __LINE__, a, b);
// do stuff
printf("Exits %s/%d foo(%d, %f)", __FILE__, __LINE__, a, b);
}
void foo(int a, double b)Unfortunately, in C++, although we can get hold of the source filename and line number and for some compilers also the function name, there is no way to get the parameter values. This would be even better:
{
TRACE_ENTRY
// do stuff
TRACE_EXIT
}
void foo(int a, double b)That is, what if all functions automatically output a diagnostics trace without the programmer having to write any additional code? Obviously we would need to be able to turn it on and off for specific functions. Otherwise the game would slow to a crawl.
{
// do stuff
}
Supposing C++ was extended to support this, the debugger might have a property page with function names and checkboxes. The checkboxes would be used to enable or disable the diagnostics trace for each function. They would obviously default to off.
How would C++ need to be extended? The compiler could generate code equivalent to this:
void foo(int a, double b)Then all the debugger would need to do is change the value of the global vatiable g_trace_foo when the checkbox was clicked and the trace would change accordingly.
{
#ifdef DEBUG
if (g_trace_foo) printf("Enters %s/%d foo(%d, %f)",
__FILE__, __LINE__, a, b);
#endif // ifdef DEBUG
// do stuff
#ifdef DEBUG
if (g_trace_foo) printf("Exits %s/%d foo(%d, %f)",
__FILE__, __LINE__, a, b);
#endif // ifdef DEBUG
}
For some functions, for example those invoked from the game's innermost loops, the overhead of checking the value of g_trace_foo or equivalent would be too great, even in debug builds. So for these cases we would need a way of directing the compiler to statically disable tracing. Maybe something like this:
[DisableTrace]I don't expect this feature in C++ any time soon. However, I can easily add this feature to my C# to C++ translator. In fact I might just try it tomorrow... The joys of writing my own compiler :)
void updateParticle()
{
// ...
}