SOS Command: !Threads
Enable unmanaged debugging in your project properties, run your code to a break point. Press Ctrl + Alt + I.
Type following commands.
.load C:windowsMicrosoft.NETFrameworkv2.0.50727sos.dll
!Help !Threads
!Threads [-live] [-special]
-live: optional. Only print threads associated with a live thread.
-special: optional. With this switch, the command will display all the special
threads created by CLR. Those threads might not be managed threads
so they might not be shown in the first part of the command's
output. Example of special threads include: GC threads (in
concurrent GC and server GC), Debugger helper threads, Finalizer
threads, AppDomain Unload threads, and Threadpool timer threads.
!Threads lists all the mananaged threads in the process.
Each thread has many attributes, many of which can be ignored. The important ones are discussed below:
There are three ID columns:
- The debugger shorthand ID (In fiber mode, if the thread is a fiber which is switched out by the host, this column will be shown as “<<<<”)
- The CLR Thread ID
- The OS thread ID.
If PreEmptiveGC is enabled for a thread, then a garbage collection can occur while that thread is running. For example, if you break in while a managed thread is making a PInvoke call to a Win32 function, that thread will be in PreEmptive GC mode.
The Domain column indicates what AppDomain the thread is currently executing in. You can pass this value to !DumpDomain to find out more.
The APT column gives the COM apartment mode.
Exception will list the last thrown exception (if any) for the thread. More details can be obtained by passing the pointer value to !PrintException. If you get the notation “(nested exceptions)”, you can get details on those exceptions by switching to the thread in question, and running “!PrintException -nested”.
Sample output:
!threads
ThreadCount: 2
UnstartedThread: 0
BackgroundThread: 1
PendingThread: 0
DeadThread: 0
Hosted Runtime: no
PreEmptive GC Alloc Lock
ID OSID ThreadOBJ State GC Context Domain Count APT Exception
876 1 36c 001971b0 8a028 Enabled 00000000:00000000 0015d338 0 MTA
2268 2 8dc 001a38e8 b228 Enabled 00000000:00000000 0015d338 0 MTA (Finalizer)