第一篇 TTD 專題 :C# 那些短命線程都在干什么?( 二 )

第一篇 TTD 專題 :C# 那些短命線程都在干什么?
文章插圖
稍等片刻,你會得到如下三個文件 。

  1. ConsoleApp101.run錄制文件
  2. ConsoleApp101.idx錄制的索引文件
  3. ConsoleApp101.out日志文件
4. 分析思路
  1. 找到 tid=20 的 OSID 線程ID
因為此時的 tid=20 的 OSID 已經不存在了,所以用 !tt 在時間刻度上折半查找 OSID 存在的 position 。
0:007> !tt 94Setting position to 94% into the traceSetting position: 396DB:0(5ac8.20): Break instruction exception - code 80000003 (first/second chance not available)Time Travel Position: 396DB:0eax=00be602c ebx=00c7c2b0 ecx=00be6028 edx=0024e000 esi=00be6028 edi=00000000eip=77d8e925 esp=07acf1c8 ebp=07acf1c8 iopl=0nv up ei pl nz na po nccs=0023ss=002bds=002bes=002bfs=0053gs=002befl=00000202ntdll!RtlEnterCriticalSection+0x15:77d8e925 f00fba3000lock btr dword ptr [eax],0ds:002b:00be602c=ffffffff0:007> !tThreadCount:20UnstartedThread:0BackgroundThread: 19PendingThread:0DeadThread:0Hosted Runtime:noLock DBGIDOSID ThreadOBJState GC ModeGC Alloc ContextDomainCount Apt Exception...2420145c 0887AFB8302b220 Preemptive03C4C1A4:03C4DFE8 00c462f8 -00001 MTA (Threadpool Worker) 可以清楚的看到原來是 OSID =145cWindbgID=24 有了這個信息不代表此時它正在執行托管方法,所以我們還需要找到這個 145c 是何時出生的?
  1. 找到當前視頻中所有的 ThreadCreated 事件 。
可以在 Events 輸出信息中檢索 id=0x145c 的線程出生信息 。
0:024> dx -r2 @$curprocess.TTD.Events.Where(t => t.Type == "ThreadCreated").Select(t => t.Thread).Where(t=>t.Id==0x145c).Select(t=>t)@$curprocess.TTD.Events.Where(t => t.Type == "ThreadCreated").Select(t => t.Thread).Where(t=>t.Id==0x145c).Select(t=>t)[0x0]: UID: 27, TID: 0x145CUniqueId: 0x1bId: 0x145cLifetime: [38B21:0, 3BB45:0]ActiveTime: [38B6A:0, 3BB45:0]GatherMemoryUse[Gather inputs, outputs and memory used by a range of execution within a thread]從輸出中可以看到, Lifetime 表示這個線程的一生, ActiveTime 則是從線程的Start處開始的,畫個圖如下:
第一篇 TTD 專題 :C# 那些短命線程都在干什么?

文章插圖
接下來將進度條調到 !tt 38B21:0 處,那如何看代碼進入到托管方法中呢?這個就得各顯神通 , 我知道的有這么幾種 。
  1. 使用單步調試
先用 !tt 調整大致范圍,然后用 p,pc,pt,t,tc,tt 微調,比如我們這篇的 !tt 94 就能獲取到 tid=20 號線程的托管部分 。
0:024> !tt 94Setting position to 94% into the traceSetting position: 396DB:0(5ac8.20): Break instruction exception - code 80000003 (first/second chance not available)Time Travel Position: 396DB:0eax=00be602c ebx=00c7c2b0 ecx=00be6028 edx=0024e000 esi=00be6028 edi=00000000eip=77d8e925 esp=07acf1c8 ebp=07acf1c8 iopl=0nv up ei pl nz na po nccs=0023ss=002bds=002bes=002bfs=0053gs=002befl=00000202ntdll!RtlEnterCriticalSection+0x15:77d8e925 f00fba3000lock btr dword ptr [eax],0ds:002b:00be602c=ffffffff0:007> ~24seax=00000000 ebx=0b1bfab8 ecx=00000000 edx=00000000 esi=00000001 edi=0b1bfab8eip=77dc196c esp=0b1bfa78 ebp=0b1bfadc iopl=0nv up ei pl nz na pe nccs=0023ss=002bds=002bes=002bfs=0053gs=002befl=00000206ntdll!NtDelayExecution+0xc:77dc196c c20800ret80:024> !clrstackOS Thread Id: 0x145c (24)Child SPIP Call Site0B1BFB50 77dc196c [HelperMethodFrame: 0b1bfb50] System.Threading.Thread.SleepInternal(Int32)0B1BFBBC 07b906940B1BFBD0 03b99078 ConsoleApp1.Program.Test()0B1BFC04 03b98a03 ConsoleApp1.Program+c.b__0_0()0B1BFC10 07b9065d System.Threading.Tasks.Task.InnerInvoke() [/_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs @ 2387]0B1BFC1C 07b900cd System.Threading.Tasks.Task+c.<.cctor>b__272_0(System.Object) [/_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs @ 2375]0B1BFC24 07b90047 System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) [/_/src/libraries/System.Private.CoreLib/src/System/Threading/ExecutionContext.cs @ 268]0B1BFC54 07b907d2 System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread) [/_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs @ 2337]0B1BFCB8 03b9ff34 System.Threading.Tasks.Task.ExecuteEntryUnsafe(System.Threading.Thread) [/_/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs @ 2277]0B1BFCC8 070f7a36 System.Threading.ThreadPoolWorkQueue.Dispatch()0B1BFD24 070ff222 System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart() [/_/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WorkerThread.cs @ 63]0B1BFDB0 070e6545 System.Threading.Thread.StartCallback() [/_/src/coreclr/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs @ 105]0B1BFF04 0307b9cf [DebuggerU2MCatchHandlerFrame: 0b1bff04]

推薦閱讀