Use windbg to debug. Net Applications

Source: Internet
Author: User

1. Solve the following problems for online. NET applications:

  • Crash
  • High CPU
  • Program exception
  • Program hang dead

2. Install windbg:

Http://msdn.microsoft.com/en-us/windows/hardware/gg463009.aspx

3. Configure windbg:

Run windbg-> menu-> file-> symbol file path-> set the _ nt_symbol_path variable as follows:
In the pop-up box, enter "C: \ mycodessymbols; SRV * c: \ mylocalsymbols * configure (follow these settings to find the symbol from the local folder C: \ mycodessymbols first, if no symbols is found, the system automatically downloads the symbols from the MS symbol server ). Another way is to extract from this symbol,

4. Use adplus in windbg to obtain the dump file.

The dump file is the memory image of the process. You can save the execution status of the program to the dump file through the debugger.

In the windbginstallation directory, you can find adplus.exe, drag it to the command line, and then use the command

Adplus.exe-Hang-PN test.exe-o c: \ dumps // capture the current dump file

Adplus.exe-crash-PN test.exe-o c: \ dumps // listen to the application. When the crash is executed, obtain the dump file.

Command-PN: Application name,-P: Application PID,-odump file output path

5. Use windbg to load the dump file to load the debugger

Run windbg> menu> File> open cresh dump to open the dump file and load the. NET debugger.

. Loadby SOS mscorwks. Net 3.5 and below

. Loadby sos clr. Net 4.0

If the. Net version of the server does not match the local machine, the mscordacwks. dll file of the server version is required.

Set. sympath = mscordacwks_x86_x86_2.0.50727.3607.dl

6. Basic windbg commands

Help SOS Command help

! Threads display all threads

! Dumpheap displays the managed heap Information

! Clrstack display call stack

! Dumpobj displays the content of an object

! Dumparray display Array

! Syncblk Display Synchronization Block

! Runaway display thread CPU time

! Gcroot trace Object Memory Reference

! PE printing exception

7. Use of windbg

When I execute this code in form:

Public form1 () {<br/> initializecomponent (); <br/> appdomain. currentdomain. unhandledexception + = new unhandledexceptioneventhandler (currentdomain_unhandledexception); <br/>}< br/> private void unhandledexceptionproc (Object OBJ) {<br/> try {<br/> throw new exception ("1st chance"); <br/>}catch (exception) {<br/> MessageBox. show ("after 1st"); <br/>}< br/> int D = 0; <br/> int n = 1/d; <br/>}

And activity dump files


Open the dump file with windbg and enter-PE: You can see the problem.

Exceptions are so important that the operating system provides corresponding debugging functions and you can use the debugger to view exceptions. After an exception occurs, the operating system checks whether the current user State Program has a debugger loaded before calling the exception handling function of the user State program. If yes, the operating system will first send the exception information to the debugger so that the debugger has the first chance to observe the exception. This is also called the first chance exception. After the debugger finishes processing the exception, the operating system allows the user State program to process. If the user State program handles this exception, there will be no debugger. Otherwise, the operating system will give the debugger a second chance to observe the exception before the unhandled exception crashes, so it is also called the Second Chance exception.

Efficient troubleshooting of Windows User State Programs

Analyze the following code: it can be seen that dummyobject occupies a lot of memory, and even causes memory overflow.

Private void memeryleakproc (Object OBJ) <br/>{< br/> while (true) {<br/> for (INT I = 0; I <100*1024; I ++) {<br/> dummyobject o = new dummyobject (); <br/> list. add (o); <br/>}< br/> thread. sleep (1000); <br/>}< br/>}

Windbg command :! Dumpheap-stat statistics stack memory

Common Causes of thread hang

-The thread pool or worker threads are concentrated in a time-consuming job or locked by other threads.

Core issue: Find the thread in Hang

! Threads

~ * E! Clrstack

! Synblk

Lock (syncroot) {<br/> int TP; <br/> int IO; <br/> // threadpool. getmaxthreads (Out TP, out Io); <br/> for (INT I = 0; I <100; I ++) {<br/> thread hangthread = new thread (hangproc); <br/> hangthread. start (); <br/>}< br/> MessageBox. show ("press to release lock"); <br/>}Private void hangproc (Object OBJ) <br/>{< br/> lock (syncroot) {<br/> n = 0; <br/>}< br/>

High CPU

-If the business volume does not increase, some threads process the data for a long time.

Core issue: Find the CPU-consuming thread

! Runaway

~ * E! Clrstack

 

Thread deadlock:

Two locks a, B,

A thread has obtained the lock a and applied for the lock B,

Another thread has obtained the lock B and applied for the lock.

Core issue: Find the locked thread

! Threads

! Syncblk

~ * E! Clrstack

 

• Two commands can solve most of the problems •! Dumpheap-stat •~ * E! Clrstack

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.