. NET program debugging skills (1): some methods to quickly locate exceptions,. net debugging
As a programmer, solving bugs is a common task in our work. It can be said that the ability to solve problems is an important embodiment of a person's ability to work. This reflects the technical level, depth, and experience of a programmer.
In the process of solving the BUG, it is very important to locate the problem. There is a saying: "finding a problem is half of solving the problem.
This article describes how to quickly locate exceptions (specifically. NET program exceptions. Including locating exceptions on the local machine, locating. net program exceptions in the customer environment, and locating SilverLight exceptions in the customer environment.
I. Identify local exceptions
It is easy to locate exceptions on our local machine. Suppose we all use Visual Studio, you only need to check the Common Langeuage Runtime Exception (CLR Exception) in the debug-> Exception menu. For example:
Five types of exceptions can be found in the preceding figure, such as c ++ and win32 exceptions. For. NET programs, we only focus on CLR exceptions.
Next, debug the program. When there is an exception in the program code, VS will automatically locate the exception.
We can see detailed information about the exception and the stack information of the program in the call Stack window. In the stack information, we can see the error in which class and function, such:
There are two ways to debug the program. One is to start the program directly with VS, and the other is to attach the program to the process.
When you attach a program to a process, do not select the wrong program type. I am using. net4.0, so the program type is hosted (Version 4.0) code.
In addition, the shortcut key for attaching to a process is Ctrl + Alt + P.
But if the exception is not thrown in your code, how can we locate it?
You can open the options and settings under the debugging menu and deselect "My code only. Then the exception in other people's code can be thrown.
2. positioning in the customer Environment. NetProgram exception:
Our programs will eventually run in the customer's environment, and there will be no development tools such as VS in the customer environment. What should we do?
We can use a clever command line tool, mdbg.exe, which is installed with VS and can only Debug. net hosted code.
Mdbg.exeit has many functions. In this document, we only talk about the abnormal positioning function. Later, we will explain how to use mdbg.exe for single-step debugging.
For details about the Mdbg.exe command, enter h (help) or? For more information, see the following link,
Http://msdn.microsoft.com/zh-cn/ms229861 (vs.80). aspx #
When using clr.exe, mdbg.exe also has multiple versions.
C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v7.0A \ Bin \ Mdbg.exe for debugging
CLR2.0 (corresponding to net2.0, 3.0, 3.5) program.
C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v7.0A \ Bin \ NETFX 4.0 Tools \ Mdbg.exe is used to debug the CLR4.0 Program (for. net4.0 ).
In addition, mdbg.exe has a dependent DLL called MdbgCore. dll. We can directly copy the corresponding mdbg.exe and MdbgCore. dll to the customer's machine for use.
If you do not have VS installed, you can copy an installation package that is encapsulated by me.
It is installed in C: \ Program Files \ U8Debug by default.
The usage of Mdbg.exe is very simple. Double-click it and enter (Attach,And press enter to list all processes that can be attached.
PID indicates the process ID,
Enter a 11940 attachment to the leleapplication2.ex sample program.
At this time, the program will be interrupted and we can make some settings at this time. The program is automatically interrupted when an exception occurs.
Enter the ca (catch) command to check which events are interrupted by the current debugger.
The Exception corresponds to the Igonre all exception, that is, all exceptions are ignored and will not be interrupted when an Exception occurs.
Enter the ca ex command to interrupt the debugger when an exception occurs.
Enter the ca command again and you will see that the Exception value has changed to Stop on all exception. In this case, any exceptions will be interrupted.
Enter the command g to continue the program execution.
When an exception occurs, the debugger is automatically interrupted, for example:
Run the command w (where, where is the program running) to view the exception stack information.
Iii. Identify SilverLight exceptions in the customer Environment
If the customer's silverlight application server is deployed on the Internet, we can directly access the silverlight site using a browser, and then attach VS to the browser process for debugging.
The debugging method is the same as debugging a common. net program.
However, if your Silverlight server is deployed on the Intranet, you can only debug it on your machine.
In addition, mdbg.exe cannot help you, and it cannot debug the Silverlight program.
We also have another famous debugging tool Windbg. The installation package size is less than 20 MB, which is easy to install on the client. You can download the installation package on the Internet. Windbg is divided into 32-bit and 64-bit versions,
To debug a 32-bit program, use a 32-bit windbg, and vice versa, use a 64-bit windbg. For more information, remember this.
Compared with mdbg.exe, Windbg is much more powerful, but it is more complicated to use.
In fact, Windbg can debug many types of programs, such as common. NET programs and c ++. However, this article only describes how to use windbg to locate exceptions.
First, open the silverlight application in IE browser, and then open the windbg attachment to the IE process for debugging. You can use Attach to a process in the File menu or use the F6 shortcut key.
After the attachment process, the program will be interrupted to the debugger. At this time, we need to make two settings.
One is to break the program into the debugger when an exception occurs. The setting method is to open the Event Filter under the debug menu and set the CLR exception to enable.
In addition, you need to load the debugger extension. Anyone who has used windbg knows that the. net program needs to load a debugger extension sos. dll. This sos. dll also has a Silverlight version.
The installation directory is C: \ Program Files (x86) \ Microsoft silverlight \ 5.1.20.11.0 \ sos. dll.
In the command window, input. load C: \ Program Files (x86) \ Microsoft Silverlight \ 5.1.20.11.0 \ sos. dll to load it.
Then enter the command g to reply to the program execution. When an exception occurs, it is interrupted to the debugger.
If an exception occurs, we can use it! Pe (print exception) to view the exception information. For example:
If you want to view the stack information, enter the command! Clrstack
The above method can basically locate all. NET exceptions. This article ends.