Debugging. NET applications using WinDbg [go]

Source: Internet
Author: User

1. Resolve the line. NET applications include the following issues:

    • Collapse
    • High CPU
    • Program exception
    • Program Hang Dead

2. Install WinDbg:

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

3. Configure WinDbg:

Run the windbg-> menu->file->symbol File path-> Set the _NT_SYMBOL_PATH variable as follows:
In the box that pops up, enter "C:\MyCodesSymbols; Srv*c:\mylocalsymbols*http://msdl.microsoft.com/download/symbols "(Following this setting, WinDbg will first be from the local folder \ c \ Find Symbol in Mycodessymbols, if not found, automatically download symbols from MS's symbol server). Another approach is to http://www.microsoft.com/whdc/devtools/debugging/symbolpkg.mspx from this symbol , Download the complete symbol installation package required by the appropriate operating system and install it,

4. Use ADPlus in WinDbg to get the dump file.

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

You can find Adplus.exe in the WinDbg installation directory, drag him to the command line, and then use the command

ADPLUS.EXE-HANG-PN test.exe-o c:\dumps//Fetch the current dump file

ADPLUS.EXE-CRASH-PN test.exe-o C:\dumps//monitoring application, when crash, get dump file

Command-PN: Application name,-P: Application pid,-odump file Output path

5. Load the debugger with WinDbg load dump file

Run the windbg-> menu->file->open Cresh dump to open the dump file and load the. NET debugger

. Loadby SOS Mscorwks. Net version 3.5 and below

. Loadby SOS CLR. Net 4.0

If the server's. NET version does not match native requires server version of the Mscordacwks.dll file

and set. Sympath = Mscordacwks_x86_x86_2.0.50727.3607.dl

6. Basic commands for WinDbg

Help SOS Instructions

!threads Show All Threads

!dumpheap displaying information about the managed heap

!clrstack Show Call Stack

!dumpobj Display the contents of an object

!dumparray Display Array

!SYNCBLK Display synchronization Blocks

!runaway Show Thread CPU time

!gcroot Tracking Object Memory Reference

!pe Printing Exceptions

7. Use of WinDbg

When I execute this code in a form:

[CSharp]View Plaincopyprint?
  1. Public Form1 () {
  2. InitializeComponent ();
  3. AppDomain.CurrentDomain.UnhandledException + = new Unhandledexceptioneventhandler (currentdomain_  UnhandledException);
  4. }
  5. private void Unhandledexceptionproc (object obj) {
  6. try {
  7. throw New Exception ("1st chance");
  8. } catch (Exception) {
  9. MessageBox.Show ("after 1st");
  10. }
  11. int d = 0;
  12. int n = 1/d;
  13. }

and active dump file


Enter-PE after opening the dump file with WinDbg: You can see where the problem is.

exceptions are so important that the operating system provides the appropriate debugging capabilities, and you can use the debugger to view exceptions. After an exception occurs, the operating system checks to see if the current user-state program has a debugger loaded before calling the user-state program's exception-handling function. If there is, then the operating system will first send the exception information to the debugger, so that the debugger can observe the exception of the first opportunity, so also known as the chance exception, the debugger after processing, the operating system to let the user state program to process. if the user-state program handles this exception, there is no debugger. Otherwise, before unhandled exception crashes, the operating system gives the debugger the opportunity to see the exception for the second time, so it is also called Second chance exception.

"Windows user-state program efficient Troubleshooting"

Analyze the following code: You can see that dummyobject consumes a lot of memory and even causes memory overflow

[CSharp]View Plaincopyprint?
  1. Private void Memeryleakproc (object obj)
  2. {
  3. While (true) {
  4. For (int i = 0; i < * 1024x768; i++) {
  5. Dummyobject o = new Dummyobject ();
  6. List. ADD (o);
  7. }
  8. Thread.Sleep (1000);
  9. }
  10. }

windbg command:!dumpheap–stat statistics stack Memory


Common causes of thread hang

-The thread pool or worker thread is concentrated in a time-consuming job, or locked by another thread

Core issue, find the thread that is stuck

!threads

~*e!clrstack

!synblk

[CSharp]View Plaincopyprint?
  1. Lock (SyncRoot) {
  2. int TP;
  3. int io;
  4. //threadpool.getmaxthreads (out tp, out IO);
  5. For (int i = 0; i <; i++) {
  6. Thread hangthread = new Thread (HANGPROC);
  7. Hangthread.start ();
  8. }
  9. MessageBox.Show ("Press to release lock");
  10. }
[CSharp]View Plaincopyprint?
    1. private void Hangproc (object obj)
    2. {
    3. Lock (syncRoot) {
    4. n = 0;
    5. }
    6. }

High CPU

-If there is no promotion with the business volume, the thread is processed over a long period of time

Core issue, find the thread that consumes the CPU

!runaway

~*e!clrstack

What happens when a thread deadlock occurs:

Two locks A, B,

A thread has got lock A, request lock B,

Another thread has got lock B, request lock A

Core issue: Find Locked threads

!threads

!syncblk

~*e!clrstack

• Two instructions to solve most of the problems!dumpheap–stat ~*e!clrstack http://blog.csdn.net/kntao/article/details/7086616

Debugging. NET applications using WinDbg [go]

Related Article

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.