Debugging Techniques in C#
C# solves three problems I faced when designing the useful and scalable debugging system. These problems exist either in Java, C/C++, or both (my main programming languages).
- Not having very much meta-information (e.g. line number, method name, etc.)
- Having to add and remove debug statements whenever the debug focus shifts
- Having the debug statements compiled into the program affecting performance
Ok, discussing the solution for these problems in order we'll start with number one. Basically a few classes from the System.Reflection and System.Diagnostics namespaces solve this problem. Using the System.Diagnostics.StackFrame class the call stack can be explored and stack details such as what is on the stack level above the current one, what line is a function being called from, etc. And with the System.Reflection classes the names of functions, namespaces, variable types, etc., can be ascertained. Applying all this to the problem at hand here's some code that retrieves the file name, line number, and method name of the function that called it.
// create the stack frame for the function that // called this function StackFrame sf = new StackFrame( 1, true ); // save the method name string methodName = sf.GetMethod().ToString(); // save the file name string fileName = sf.GetFileName(); // save the line number int lineNumber = sf.GetFileLineNumber();
// create the namespaces hashtable namespaces = new Hashtable(); // get the assembly of this class Assembly a = Assembly.GetAssembly( new Debug().GetType() ); // now cycle through each type and gather up all the namespaces foreach( Type type in a.GetTypes() ) { // check if the namespace is already in our table if( ! namespaces.Contains( type.Namespace ) ) namespaces.Add( type.Namespace, true ); }
The above code will create a Hashtable object containing all the namespaces in the same assembly as the Debug class of which this code is a part. Of course, this is only half of the solution so here is the actual filtering code.
// only proceed if the namespace in question is being debugged if( (bool) namespaces[ method.DeclaringType.Namespace ] ) // this is where the debug statement display code would be
Ok, so far so good. With the problems number one and two knocked out that leaves us with the obstacle of removing those darn debug statements when the final release is to be made without actually having to manually comment out or delete each one. This is where those handy little things called attributes come in to play. Assuming you have knowledge of how attributes work in C# I'll cut to the chase and introduce the ConditionalAttribute which is part of the aforementioned System.Diagnostics namespace. So here's some showing how to use the ConditionalAttribute class.
[Conditional("DEBUG")]
public void Debug( string msg )
{
// this is where the debug statement
// display code would be
}
What this will do is when this program is compiled it will check if 'DEBUG' was #defined and if it was then the call to this function will be remain, however if 'DEBUG' is not #defined all calls to this function not will be compiled leaving us with no performance hit.
Now, some of you are probably saying that you could solve some of the problems above in C/C++ and Java. I will note that neither language solves all of them. For example in C/C++ you can #define and #ifdef much like you can use ConditionalAttribute class and C# defines. But getting useful meta-information in C/C++ is limited. And in Java getting meta-information is possible but as far as I know all the code is always compiled (i.e. there's no Conditional type functionality). Thankfully there is C# which does solve (gracefully I might add) all the problems I mentioned above and quite a few more.
To demonstrate these techniques I wrote a small windows application along with this article. The Debug class can be plugged into your own projects. So enjoy and happy coding.
About the author
Mike Borromeo is a college of engineering student at the University of Michigan (AA). He has experience in C/C++, Java, C#, PHP, VBScript, JavaScript, MSSQL, and HTML. He can be reached at MikeD227@hotmail.com. The ideas and code for this article were developed as part of the Gnutella client project Phosl (http://www.phosl.com).
Comments
Equivalent Java compile out debug code
Posted by jjakob on 04/05/2005 09:58pmJust to clarify in Java you can compile out the debugging by defining a static final. class MyClass public static final bool debug = false; public MyClass() { if(debug) System.out.println("MyClass::Constructor() starting"); //do some work if(debug) System.out.println("MyClass::Constructor() done"); } } If you compile and decompile the class file you will see the debug lines are missing. If you set debug to true then you will see the debug lines in the decompiled class file are included.
ReplyVery Helpful
Posted by Legacy on 01/21/2004 12:00amOriginally posted by: HefnySco
That you it is very helpful
Replydebug c++ dll called from c#??
Posted by Legacy on 01/08/2004 12:00amOriginally posted by: chhabi
how to debug c++ dll through c#
how to debug c++ dll in c#
Posted by wvered on 10/24/2004 05:03amyou have to copy the c++ ".dll" file to the directory where the ".exe" of the c# is located. the ".dll" should be created in debug mode. then in the c# project you have to set "enable unmanaged debugging" option to be "true" good luck ;)
ReplyNice Application
Posted by Legacy on 06/09/2003 12:00amOriginally posted by: sunyy
Hi,
Its a very nice app and suits my need. Thanks a lot!
Reply