Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native threads in a .Net application

Tags:

c#

debugging

When I issued ~* e !clrstack command in WinDbg to identify the clr call stack of threads in a console application, it listed 5 threads. 3 of them are managed threads (Main Thread, Thread that I crated, garbage Collection thread (I expect)). 2 are native threads. I did not create any native threads.

What do these native threads do? Where could I get more information?

The output of the ~* e !clrstack command is listed below

0:004> ~* e !clrstack
OS Thread Id: 0x1ab8 (0)
ESP       EIP     
0012f3c0 7c90e514 [HelperMethodFrame: 0012f3c0] System.Threading.Thread.SleepInternal(Int32)
0012f414 79299275 System.Threading.Thread.Sleep(Int32)
0012f418 00c602bf testlock.LockTest.Test()
0012f458 00c60131 testlock.Program.Main(System.String[])
0012f69c 79e71b4c [GCFrame: 0012f69c] 
OS Thread Id: 0x1008 (1)
Unable to walk the managed stack. The current thread is likely not a 
managed thread. You can run !threads to get a list of managed threads in
the process
OS Thread Id: 0x209c (2)
Failed to start stack walk: 80004005
OS Thread Id: 0x1490 (3)
ESP       EIP     
00d6f74c 7c90e514 [GCFrame: 00d6f74c] 
00d6f81c 7c90e514 [HelperMethodFrame_1OBJ: 00d6f81c] System.Threading.Monitor.Enter(System.Object)
00d6f874 00c602b3 testlock.LockTest.Test()
00d6f8b4 00c6022c testlock.Program+<>c__DisplayClass1.<Main>b__0()
00d6f8c0 792d6d66 System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
00d6f8cc 792e01ef System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
00d6f8e4 792d6ce4 System.Threading.ThreadHelper.ThreadStart()
00d6fb0c 79e71b4c [GCFrame: 00d6fb0c] 
OS Thread Id: 0x1cb8 (4)
Unable to walk the managed stack. The current thread is likely not a 
managed thread. You can run !threads to get a list of managed threads in
the process
like image 901
Maanu Avatar asked Aug 23 '10 11:08

Maanu


1 Answers

When Windows runs your application, it sets up several unmanaged threads. This is completely normal.

You can get a stack trace from unmanaged threads from various commands like:

.kb100
!dumpstack

Personally, I like the output of !dumpstack, which gives you the combined managed and unmanaged stack.

If you want to look at only the managed threads, try

!threads

Tess Ferrandez describes some of the normal theads you will see while debugging in her blog:

http://blogs.msdn.com/b/tess/archive/2005/12/20/things-to-ignore-when-debugging-an-asp-net-hang.aspx

like image 93
Paul Williams Avatar answered Oct 06 '22 10:10

Paul Williams