問題簡介
ASP.NET應用中, 每一訪問者都會單獨獲得一個Session。在Web應用程序中,當一個用戶訪問該應用時,Session類型的變量可以供這個用戶在該Web應用的所有頁面中共享數據;如果另一個用戶也同時訪問該Web應用,他也擁有自己的Session變量,但兩個用戶之間無法通過Session變量共享信息.很多時候在生產環境中調試需要從dump中分析session中的特定數據, 我們可以從HttpContext對象上找到與這個請求有關的用戶Session.以下是從dump中分析查找session中存儲對象的一般步驟。
查找方法
通過ILSPY反編譯HttpContext.Current.Session. 發現Session是一個HttSessionState類型的對象, 保存在HttpContext.Items中, Key值為AspSession. 要找到Session對象, 需要先從DUMP中找到HttpContext的Items對象, 並從這個Hashtable中找到AspSession.
1. 加載 sos.dll
.load C:\Windows\Microsoft.NET\Framework64\v2.0.50727\sos.dll
2. 定位到需要關注的線程, 找到當前的HttpContext
0:027> !dso OS Thread Id: 0x2d8c (27) RSP/REG Object Name …… 0000001a05fee8d0 00000019043a3328 System.Web.HttpContext ……
3. 首先需要找到items的地址. 在items中找到buckets的地址. 用命令 !da –details <address>將里面的元素列出來, 找到key為"AspSession", 它所保存的value就是Session對象.
0:027> !do 00000019043a3328 Name: System.Web.HttpContext MethodTable: 000007f9e9294ac0 EEClass: 000007f9e8ef2428 Size: 336(0x150) bytes (C:\Windows\assembly\GAC_64\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll) Fields: MT Field Offset Type VT Attr Value Name 000007f9e92975e8 4000fe6 8 ...IHttpAsyncHandler 0 instance 0000000000000000 _asyncAppHandler 000007f9e9297118 4000fe7 10 ...b.HttpApplication 0 instance 00000019043a66c8 _appInstance 000007f9e9297658 4000fe8 18 ....Web.IHttpHandler 0 instance 000000190441fcd0 _handler 000007f9e9297a98 4000fe9 20 ...m.Web.HttpRequest 0 instance 00000019043a3478 _request 000007f9e9297e18 4000fea 28 ....Web.HttpResponse 0 instance 00000019043a35c8 _response 000007f9e9298330 4000feb 30 ...HttpServerUtility 0 instance 00000018c43e2188 _server 000007fa22e711c0 4000fec 38 ...Collections.Stack 0 instance 0000000000000000 _traceContextStack 000007f9e929d698 4000fed 40 ....Web.TraceContext 0 instance 0000000000000000 _topTraceContext 000007fa22e7f578 4000fee 48 ...ections.Hashtable 0 instance 00000018c43e2450 _items …….. 0:027> !do 00000018c43e2450 Name: System.Collections.Hashtable MethodTable: 000007fa22e7f578 EEClass: 000007fa22a82390 Size: 88(0x58) bytes (C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll) Fields: MT Field Offset Type VT Attr Value Name 000007fa22e7f740 400098d 8 ...ashtable+bucket[] 0 instance 00000018c43e24a8 buckets 000007fa22e7ee90 400098e 38 System.Int32 1 instance 2 count 000007fa22e7ee90 400098f 3c System.Int32 1 instance 0 occupancy 000007fa22e7ee90 4000990 40 System.Int32 1 instance 7 loadsize 000007fa22e74318 4000991 44 System.Single 1 instance 0.720000 loadFactor 0:027> !da -details 00000018c43e24a8 Name: System.Collections.Hashtable+bucket[] MethodTable: 000007fa22e7f740 EEClass: 000007fa22b214c8 Size: 288(0x120) bytes Array: Rank 1, Number of elements 11, Type VALUETYPE Element Methodtable: 000007fa22e7f8a8 ………….. [5] 00000018c43e2530 Name: System.Collections.Hashtable+bucket MethodTable 000007fa22e7f8a8 EEClass: 000007fa22b21600 Size: 40(0x28) bytes (C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll) Fields: MT Field Offset Type VT Attr Value Name 000007fa22e77510 4000999 0 System.Object 0 instance 00000018c43e2db0 key 000007fa22e77510 400099a 8 System.Object 0 instance 00000018c43e2de0 val 000007fa22e7ee90 400099b 10 System.Int32 1 instance 817416418 hash_coll 0:027> !do 00000018c43e2db0 Name: System.String MethodTable: 000007fa22e77c20 EEClass: 000007fa22a7e550 Size: 46(0x2e) bytes (C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll) String: AspSession …… 0:027> !do 00000018c43e2de0 Name: System.Web.SessionState.HttpSessionState MethodTable: 000007f9e9298258 EEClass: 000007f9e8ef3e70 Size: 24(0x18) bytes (C:\Windows\assembly\GAC_64\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll) Fields: MT Field Offset Type VT Attr Value Name 000007f9e9238018 4001f5e 8 ...IHttpSessionState 0 instance 00000018c43e2d70 _container
4. 直接通過review代碼來找到Session內的數據保存在那里有點困難. HttpSessionState的 _container是一個接口類型. 在代碼中找到確切的類型會比較花時間.
5. 通過dump, 我們可以看到_container的確切類型是System.Web.SessionState.HttpSessionStateContainer. 所以也就檢查該類型的代碼, 發生數據是保持在 _sessionItems的對象上.
0:027> !do 00000018c43e2de0 Name: System.Web.SessionState.HttpSessionState MethodTable: 000007f9e9298258 EEClass: 000007f9e8ef3e70 Size: 24(0x18) bytes (C:\Windows\assembly\GAC_64\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll) Fields: MT Field Offset Type VT Attr Value Name 000007f9e9238018 4001f5e 8 ...IHttpSessionState 0 instance 00000018c43e2d70 _container 0:027> !do 00000018c43e2d70 Name: System.Web.SessionState.HttpSessionStateContainer MethodTable: 000007f9e92a0ea0 EEClass: 000007f9e8ef73b8 Size: 64(0x40) bytes (C:\Windows\assembly\GAC_64\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll) Fields: MT Field Offset Type VT Attr Value Name 000007fa22e77c20 4001f5f 8 System.String 0 instance 0000000000000000 _id 000007f9e9208b28 4001f60 10 ...ateItemCollection 0 instance 00000018c43e2630 _sessionItems 000007f9e92979c8 4001f61 18 ...ObjectsCollection 0 instance 00000018c43e2b80 _staticObjects 000007fa22e7ee90 4001f62 28 System.Int32 1 instance 20 _timeout 000007fa22e76df0 4001f63 34 System.Boolean 1 instance 1 _newSession 000007f9e92a1f00 4001f64 2c System.Int32 1 instance 1 _cookieMode 000007f9e92a1dd0 4001f65 30 System.Int32 1 instance 1 _mode 000007fa22e76df0 4001f66 35 System.Boolean 1 instance 0 _abandon 000007fa22e76df0 4001f67 36 System.Boolean 1 instance 0 _isReadonly 000007f9e92a0de8 4001f68 20 ...essionStateModule 0 instance 00000019043a7190 _stateModule
6. 按照同樣的步驟, _sessionItem的類型為System.Web.SessionState.SessionStateItemCollection. 數據保存在_entriesTable 中, 這里是Session保存數據的地方.
7. 按照代碼中找到的結果, 需要從DUMP中列出_entriesTable的元素. 這樣就可以檢查Session中所保存的數據.
實例代碼 :
0:027> !do 00000018c43e2630 Name: System.Web.SessionState.SessionStateItemCollection MethodTable: 000007f9e9208c70 EEClass: 000007f9e8eafd60 Size: 112(0x70) bytes (C:\Windows\assembly\GAC_64\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll) Fields: MT Field Offset Type VT Attr Value Name 000007fa22e76df0 400118f 44 System.Boolean 1 instance 0 _readOnly 000007fa22e7eb08 4001190 8 ...ections.ArrayList 0 instance 00000018c43e29e0 _entriesArray 000007fa22e7ff28 4001191 10 ...IEqualityComparer 0 instance 00000018c436e9e0 _keyComparer 000007fa22e7f578 4001192 18 ...ections.Hashtable 0 instance 00000018c43e2a08 _entriesTable 000007fa22173e60 4001193 20 ...e+NameObjectEntry 0 instance 0000000000000000 _nullKeyEntry 0:027> !do 00000018c43e2a08 Name: System.Collections.Hashtable MethodTable: 000007fa22e7f578 EEClass: 000007fa22a82390 Size: 88(0x58) bytes (C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll) Fields: MT Field Offset Type VT Attr Value Name 000007fa22e7f740 400098d 8 ...ashtable+bucket[] 0 instance 00000018c4423be8 buckets 000007fa22e7ee90 400098e 38 System.Int32 1 instance 10 count 000007fa22e7ee90 400098f 3c System.Int32 1 instance 4 occupancy 000007fa22e7ee90 4000990 40 System.Int32 1 instance 16 loadsize ….. 0:027> !da -details 00000018c4423be8 Name: System.Collections.Hashtable+bucket[] MethodTable: 000007fa22e7f740 EEClass: 000007fa22b214c8 Size: 576(0x240) bytes Array: Rank 1, Number of elements 23, Type VALUETYPE Element Methodtable: 000007fa22e7f8a8 …… [10] 00000018c4423ce8 Name: System.Collections.Hashtable+bucket MethodTable 000007fa22e7f8a8 EEClass: 000007fa22b21600 Size: 40(0x28) bytes (C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll) Fields: MT Field Offset Type VT Attr Value Name 000007fa22e77510 4000999 0 System.Object 0 instance 00000018c44235f0 key 000007fa22e77510 400099a 8 System.Object 0 instance 00000018c4423690 val 000007fa22e7ee90 400099b 10 System.Int32 1 instance 1884180917 hash_coll ……. 0:027> !do 00000018c4423690 Name: System.Collections.Specialized.NameObjectCollectionBase+NameObjectEntry MethodTable: 000007fa22173e60 EEClass: 000007fa21ee9158 Size: 32(0x20) bytes (C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll) Fields: MT Field Offset Type VT Attr Value Name 000007fa22e77c20 4001199 8 System.String 0 instance 00000018c44235f0 Key 000007fa22e77510 400119a 10 System.Object 0 instance 00000018c4423650 Value 0:027> !do 00000018c4423690 Name: System.Collections.Specialized.NameObjectCollectionBase+NameObjectEntry MethodTable: 000007fa22173e60 EEClass: 000007fa21ee9158 Size: 32(0x20) bytes (C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll) Fields: MT Field Offset Type VT Attr Value Name 000007fa22e77c20 4001199 8 System.String 0 instance 00000018c44235f0 Key 000007fa22e77510 400119a 10 System.Object 0 instance 00000018c4423650 Value 0:027> !do 00000018c44235f0 Name: System.String MethodTable: 000007fa22e77c20 EEClass: 000007fa22a7e550 Size: 40(0x28) bytes (C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll) String: Key : 2 -------------> 測試數據中的Key值 ……. 0:027> !do 00000018c4423690 Name: System.Collections.Specialized.NameObjectCollectionBase+NameObjectEntry MethodTable: 000007fa22173e60 EEClass: 000007fa21ee9158 Size: 32(0x20) bytes (C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll) Fields: MT Field Offset Type VT Attr Value Name 000007fa22e77c20 4001199 8 System.String 0 instance 00000018c44235f0 Key 000007fa22e77510 400119a 10 System.Object 0 instance 00000018c4423650 Value 0:027> !do 00000018c4423650 Name: System.String MethodTable: 000007fa22e77c20 EEClass: 000007fa22a7e550 Size: 62(0x3e) bytes (C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll) String: Here is value :20; -------------> 測試數據中的Value值 ……..
希望以上內容對您有所幫助
Sonic Guo