Windbg program debugging is. Net advanced development needs to master the necessary skills, analysis of memory leaks, analysis of high CPU, analysis of thread blocking, analysis of memory objects, analysis of thread stack, Live Dedugging. This field can be described as a combination of skills and scenario-based applications. It is meaningless to learn Windbg commands only without understanding the role of Troubleshooting.

To do a good job, you must first sharpen your tools. Let’s start with common commands and examples.

  1. Prepare a Dump file. 64-bit applications are recommended. For example: 64-bit IIS application w3WP process, 64-bit exe process can be. Task Manager – process – right click to create Dump file
  2. Download and install Windbg.Developer.microsoft.com/zh-cn/windo…Next, select Debugging Tools for Windows.





    Ctrl+D to open the Dump file.

SOS installation

dotnet tool install -g dotnet-sos
dotnet-sos install
Copy the code

Windbg basic usage

  1. Load the SOS debug extension DLL
.loadby sos clr
Copy the code
  1. The command to set and reload the debug symbol file will. Net some important PDB files downloaded to the specified path, loaded into the Windbg debugging environment, so that we can see the program which line error, running to which line
1 .symfix+ C:\symbols
2 .reload
Copy the code



3. Print the current debug symbol file search path

0:000> .sympath
Copy the code
  1. Look at the thread pool, analyze and confirm CPU usage, and which instructions can be used
0:000> !threadpoolCPU utilization: 2%Worker Thread: Total: 19 Running: 2 Idle: 17 MaxLimit: 32767 MinLimit: 4Work Request in Queue: 0--------------------------------------Number of Timers: 2--------------------------------------Completion Port Thread:Total: 4 Free: 4 MaxFree: 8 CurrentLimit: 4 MaxLimit: 1000 MinLimit: 4
Copy the code
  1. View the overall performance of the thread
! threadsCopy the code



6. Query the call stack for a specified thread, such as thread 34

 ~34s
Copy the code

! clrstackCopy the code



7. Check the CPU usage of the thread

! runawayCopy the code



The first column is the thread number, and the second column is the Total CPU usage time

  1. Dump stack objects to view information about all objects on the current thread stack
! dsoCopy the code



9. Query the Dump object information about a specified object in memory

!do
Copy the code
  1. Example Query information about a specified Array object in memory
! daCopy the code
  1. View the stack for the current thread and the variable information on each line of the stack
! clrstack -aCopy the code
  1. Windbg additional process debugging, enable CLR exception capture, view the exception, view the exception thread stack, disable CLR exception debugging, exit debugging
sxe clr g ! pe ! clrstack sxd clr qdCopy the code
  1. View the distribution of memory objects on the managed heap, three generations of information
! eeheap -gcCopy the code



14. View the Dll loaded on the managed heap

! eeheap -loaderCopy the code
  1. What is memory object generation promotion? Uncollected objects in garbage collection are also called survivors and are promoted to the next generation. The lifetime of the object can be analyzed through the generation promotion
  2. Query the total number and memory usage of each type of objects in the memory
! dumpheap -statCopy the code
  1. Query the number and size of large objects in memory
! dumpheap -stat -mt -min85000
Copy the code
  1. View instructions for the destructor queue of memory
! finalizequeueCopy the code
  1. Enter the instruction to view the gcroot of object 000000123557DFC0
! gcroot000000123557DFC0
Copy the code
  1. View instructions for thread blocking
! syncblkCopy the code
  1. Look at the Dump all the System.Net.Sockets.Socket object statistics in the instructions
! dumpheap -type System.Net.Sockets.Socket -statCopy the code

Summary of use of the Mex extension

Introduction to use:

Blogs.msdn.microsoft.com/luisdem/201…

Download address:

www.microsoft.com/en-us/downl…

After downloading, unzipping, there are two directories, X64 and X86, you can load them according to your own needs, currently we mainly use X64. Of course, you can copy this extension directly to the Windbg run directory.

Here, let’s show Windbg loading mex extension:

0:000> .load D:\Mex\x64\mex.dll
Mex External 3.0. 07172. Loaded!
Copy the code

Of course, we can continue loading SOS at the same time

0:000> .loadby sos clr
Copy the code

View the extensions that have been loaded:



If you are debugging Dump, it is recommended to set the path of the debug symbol:

srv*c:\symcache*http://msdl.microsoft.com/download/symbols; c:\symcache
Copy the code



Check out the various commands on Mex:

0:000> !mex.help
Mex currently has 255 extensions available.  Please specify a keyword to search.
Or browse by category:
Copy the code



You can click on it one by one.

Here I show you some common commands:

  1. ! Dae looks at all exceptions



2. View the status of all threads

! mex.mthreadsCopy the code



3.! Clrstack2 query thread stack,



4. Do2 View objects



5. View all Asp.Net requests

! mex.aspxpagesextCopy the code
  1. View exceptions for the current thread
! mex.pe2Copy the code
  1. Foreach
! foreachobject -x! "" do2 @#Obj"  System.Net.Socket
Copy the code