January 01, 2005

Diagnostics traces

Here is a common and useful debugging technique:
void foo(int a, double b)

{
printf("Enters %s/%d foo(%d, %f)", __FILE__, __LINE__, a, b);
// do stuff
printf("Exits %s/%d foo(%d, %f)", __FILE__, __LINE__, a, 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:
void foo(int a, double b)

{
TRACE_ENTRY
// do stuff
TRACE_EXIT
}
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:
void foo(int a, double b)

{
// do stuff
}
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.

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)

{
#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
}
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.

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]

void updateParticle()
{
// ...
}
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 :)


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?