前幾天公號裏有一位朋友留言說,你windbg玩的溜,能幫我分析下被 ThreadStatic 修飾的變量到底存放在哪裏嗎?能不能幫我挖出來😂😂😂,其實這個問題問的挺深的,玩高級語言的朋友相信不多有接觸到這個的,雖然不少朋友都知道這個特性怎麼用,固然我也沒特別研究這個,既然要回答這個問題,我得研究研究回答之!爲了更好的普適性,先從簡單的提及!python
相信不少朋友在代碼中都使用過 static 變量,它的好處多多,好比說我常常會用 static 去作一個進程級緩存,從而提升程序的性能,固然你也能夠做爲一個很是好的一級緩存,以下代碼:git
public class Test { public static Dictionary<int, string> cachedDict = new Dictionary<int, string>(); }
剛纔我也說到了,這是一個進程級的緩存,多個線程都看得見,因此在多線程的環境下,你須要特別注意同步的問題。要麼使用鎖,要麼使用 ConcurrentDictionary,我以爲這也是一個思惟定式,不少時候思惟老是在現有基礎上去修補,去亡羊補牢,而沒有跳出這個思惟從根基上去處理,說這麼可能是什麼意思呢?我舉一個例子:github
在市面上常見的鏈式跟蹤框架中,好比說: Zikpin,SkyWalking,會使用一些集合去存儲跟蹤當前線程的一些鏈路信息,好比說 A -> B -> C -> D -> B -> A
,常規的思惟就像上面說的同樣,定義一個全局 cachedDict,而後使用各類同步機制,其實你也能夠下降 cachedDict 的訪問做用域,將 全局訪問 改爲 Thread級訪問,這難道不是更好的解決思路嗎?數組
要想作到 Thread級做用域,實現起來很是簡單,在 cachedDict 上打一個 ThreadStatic
特性便可,修改代碼以下:緩存
public class Test { [ThreadStatic] public static Dictionary<int, string> cachedDict = new Dictionary<int, string>(); }
接下來能夠開多個線程給 cachedDict 灌數據,看看 dict 是否是 Thread級做用域,實現代碼以下:cookie
class Program { static void Main(string[] args) { var task1 = Task.Run(() => { if (Test.cachedDict == null) Test.cachedDict = new Dictionary<int, string>(); Test.cachedDict.Add(1, "mary"); Test.cachedDict.Add(2, "john"); Console.WriteLine($"thread={Thread.CurrentThread.ManagedThreadId} 的 dict 有記錄: {Test.cachedDict.Count}"); }); var task2 = Task.Run(() => { if (Test.cachedDict == null) Test.cachedDict = new Dictionary<int, string>(); Test.cachedDict.Add(3, "python"); Test.cachedDict.Add(4, "jaskson"); Test.cachedDict.Add(5, "elen"); Console.WriteLine($"thread={Thread.CurrentThread.ManagedThreadId} 的 dict 有記錄: {Test.cachedDict.Count}"); }); Console.ReadLine(); } } public class Test { [ThreadStatic] public static Dictionary<int, string> cachedDict = new Dictionary<int, string>(); }
從結果來看,確實是一個 Thread 級,並且還避免了線程間同步開銷,哈哈😄,這麼神奇的東西,難怪有讀者想看看底層究竟是怎麼實現的。數據結構
每個線程都有一份屬於本身專屬的私有數據,這些數據就放在 Thread 的 TEB 中,若是你想看的話,能夠在 windbg 中打印出來。多線程
0:000> !teb TEB at 0000001e1cdd3000 ExceptionList: 0000000000000000 StackBase: 0000001e1cf80000 StackLimit: 0000001e1cf6e000 SubSystemTib: 0000000000000000 FiberData: 0000000000001e00 ArbitraryUserPointer: 0000000000000000 Self: 0000001e1cdd3000 EnvironmentPointer: 0000000000000000 ClientId: 0000000000005980 . 0000000000005aa8 RpcHandle: 0000000000000000 Tls Storage: 000001b599d06db0 PEB Address: 0000001e1cdd2000 LastErrorValue: 0 LastStatusValue: c0000139 Count Owned Locks: 0 HardErrorMode: 0
從 teb 的結構中能夠看出,既有 線程本地存儲(TLS),也有異常相關信息的存儲 (ExceptionList) 等等相關信息。框架
進程會在啓動後給 TLS 分配總共 1088 個槽位,每一個線程都會分配一個專屬的 tlsindex 索引,而且擁有一組 slots 槽位,能夠用 windbg 去驗證一下。函數
0:000> !tls Usage: tls <slot> [teb] slot: -1 to dump all allocated slots {0-0n1088} to dump specific slot teb: <empty> for current thread 0 for all threads in this process <teb address> (not threadid) to dump for specific thread. 0:000> !tls -1 TLS slots on thread: 5980.5aa8 0x0000 : 0000000000000000 0x0001 : 0000000000000000 0x0002 : 0000000000000000 0x0003 : 0000000000000000 0x0004 : 0000000000000000 ... 0x0019 : 0000000000000000 0x0040 : 0000000000000000 0:000> !t Lock DBG ID OSID ThreadOBJ State GC Mode GC Alloc Context Domain Count Apt Exception 0 1 5aa8 000001B599CEED90 2a020 Preemptive 000001B59B9042F8:000001B59B905358 000001b599cdb130 1 MTA 5 2 90c 000001B599CF4930 2b220 Preemptive 0000000000000000:0000000000000000 000001b599cdb130 0 MTA (Finalizer) 7 3 74 000001B59B7272A0 102a220 Preemptive 0000000000000000:0000000000000000 000001b599cdb130 0 MTA (Threadpool Worker) 9 4 2058 000001B59B7BAFF0 1029220 Preemptive 0000000000000000:0000000000000000 000001b599cdb130 0 MTA (Threadpool Worker)
從上面的 {0-0n1088} to dump specific slot
中能夠看出,進程中總會有 1088 個槽位,並且當前主線程 5aa8 擁有 27 個 slot 槽位。
好了,基本概念介紹完了,接下來準備分析一下彙編代碼了。
爲了更好的用 windbg 去挖,我就定義一個簡單的 ThreadStatic int 變量,代碼以下:
class Program { [ThreadStatic] public static int i = 0; static void Main(string[] args) { i = 10; // 12 line var num = i; Console.ReadLine(); } }
接下來用 !U 反彙編一下 Main 函數的代碼,着重看一下第 12 行代碼的 i = 10;
。
0:000> !U /d 00007ffbe0ae0ffb E:\net5\ConsoleApp5\ConsoleApp5\Program.cs @ 12: 00007ffb`e0ae0fd6 48b9b0fbb7e0fb7f0000 mov rcx,7FFBE0B7FBB0h 00007ffb`e0ae0fe0 ba01000000 mov edx,1 00007ffb`e0ae0fe5 e89657a95f call coreclr!JIT_GetSharedNonGCThreadStaticBase (00007ffc`40576780) 00007ffb`e0ae0fea c7401c0a000000 mov dword ptr [rax+1Ch],0Ah
從彙編指令上來看,最後的 10 賦給了 rax+1Ch
的低32位,那 rax 的地址從哪裏來的呢?能夠看出核心邏輯在 JIT_GetSharedNonGCThreadStaticBase 方法內,接下來就得研究一下這個方法都幹嗎了。
接下來在第 12 處設置一個斷點 !bpmd Program.cs:12
處,方法的簡化彙編代碼以下:
coreclr!JIT_GetSharedNonGCThreadStaticBase: 00007ffc`2c38679a 448b0dd7894300 mov r9d, dword ptr [coreclr!_tls_index (00007ffc`2c7bf178)] 00007ffc`2c3867a1 654c8b042558000000 mov r8, qword ptr gs:[58h] 00007ffc`2c3867aa b908000000 mov ecx, 8 00007ffc`2c3867af 4f8b04c8 mov r8, qword ptr [r8+r9*8] 00007ffc`2c3867b3 4e8b0401 mov r8, qword ptr [rcx+r8] 00007ffc`2c3867b7 493b8060040000 cmp rax, qword ptr [r8+460h] 00007ffc`2c3867be 732b jae coreclr!JIT_GetSharedNonGCThreadStaticBase+0x6b (00007ffc`2c3867eb) 00007ffc`2c3867c0 4d8b8058040000 mov r8, qword ptr [r8+458h] 00007ffc`2c3867c7 498b04c0 mov rax, qword ptr [r8+rax*8] 00007ffc`2c3867cb 4885c0 test rax, rax 00007ffc`2c3867ce 741b je coreclr!JIT_GetSharedNonGCThreadStaticBase+0x6b (00007ffc`2c3867eb) 00007ffc`2c3867d0 8bca mov ecx, edx 00007ffc`2c3867d2 f644011801 test byte ptr [rcx+rax+18h], 1 00007ffc`2c3867d7 7412 je coreclr!JIT_GetSharedNonGCThreadStaticBase+0x6b (00007ffc`2c3867eb) 00007ffc`2c3867d9 488b4c2420 mov rcx, qword ptr [rsp+20h] 00007ffc`2c3867de 4833cc xor rcx, rsp 00007ffc`2c3867e1 e89a170600 call coreclr!__security_check_cookie (00007ffc`2c3e7f80) 00007ffc`2c3867e6 4883c438 add rsp, 38h 00007ffc`2c3867ea c3 ret
接下來我仔細分析下這裏的 mov 操做。
這個很簡單,獲取該線程專屬的 tls_index 索引
這裏的 gs:[58h]
是什麼意思呢? 應該有朋友知道,gs寄存器 是專門用於存放當前線程的 teb 地址,後面的 58 表示在 teb 地址上的偏移量,那問題來了,這個地址到底指向誰了呢? 其實你能夠把 teb 的數據結構給打印出來就明白了。
0:000> dt teb coreclr!TEB +0x000 NtTib : _NT_TIB +0x038 EnvironmentPointer : Ptr64 Void +0x040 ClientId : _CLIENT_ID +0x050 ActiveRpcHandle : Ptr64 Void +0x058 ThreadLocalStoragePointer : Ptr64 Void +0x060 ProcessEnvironmentBlock : Ptr64 _PEB ...
上面這句 +0x058 ThreadLocalStoragePointer : Ptr64 Void
能夠看出,其實就是指向 ThreadLocalStoragePointer 。
有了前兩步的基礎,這句彙編就很簡單了,它作了一個索引操做: ThreadLocalStoragePointer[tls_index]
,對不對,從而獲取屬於該線程的 tls 內容,這個 ThreadStatic 的變量就會存放在這個數組的某一個內存塊中。
後續還有一些計算偏移的邏輯運算都基於這個 ThreadLocalStoragePointer[tls_index]
之上,方法調用繞來繞去,彙編無法看哈 😂😂😂
總的來講,能夠肯定 ThreadStatic 變量 確實是存放在 TEB 的 ThreadLocalStoragePointer 數組中,這幾天 NET5 的 CoreCLR 沒有編譯成功,你們若是感興趣,能夠 調試 CoreCLR + 彙編
作更深刻的挖掘!
更多高質量乾貨:參見個人 GitHub: dotnetfly