書接上回,我們開始看另外幾個命令。
大家如果注意看My Code,能發現裡面既有value type,也有reference type。那麼,我怎麼查看,當前線程狀態下,我的那些變數的資訊呢?對於Managed 程式碼而言,最核心的命令就是!do(dump object的縮寫),這個!do後面是要加上你要看的變數的地址的。那麼,我怎麼知道我的變數的地址呢?
先來看另外一個命令,!dso(Dump Stack Objects的縮寫),這個命令可以把當前棧上所有的變數都搞出來。我們敲一下這個命令啊:歎號+dso,看看結果:
0:000> !dso
OS Thread Id: 0x1248 (0)
ESP/REG Object Name
002df0c4 0157651c Microsoft.Win32.SafeHandles.SafeFileHandle
002df0d4 0157651c Microsoft.Win32.SafeHandles.SafeFileHandle
002df108 015765b8 System.Byte[]
002df10c 01576530 System.IO.__ConsoleStream
002df130 01576560 System.IO.StreamReader
002df134 01576560 System.IO.StreamReader
002df138 01573750 SOSBasics.MatrixWorld
002df148 01576560 System.IO.StreamReader
002df14c 015768d4 System.IO.TextReader+SyncTextReader
002df150 01573750 SOSBasics.MatrixWorld
002df160 015768d4 System.IO.TextReader+SyncTextReader
002df164 01573750 SOSBasics.MatrixWorld
002df16c 01573750 SOSBasics.MatrixWorld
002df178 01573628 System.Object[] (System.String[])
002df234 01573628 System.Object[] (System.String[])
002df3e0 01573628 System.Object[] (System.String[])
002df408 01573628 System.Object[] (System.String[])
首先注意的一點,!dso是和線程相關的。你可以試一下,切換到1號或者2號線程,然後執行!dso,看看有什麼結果?(在我這個例子面,你看不到任何結果的)
上面的列表分為三個部分,最左面一列是啥,不管它。中間一列,就是對象的地址,右面,是對象的類型。ok,我們看一下上面紅色標記的那行,偶代碼裡面的Mtraix,它的object id是01573750,找到這個地址後,我們用!do命令來扒它的皮!執行!do 01573750,看結果
0:000> !do 01573750
Name: SOSBasics.MatrixWorld
MethodTable: 004132ac
EEClass: 0041142c
Size: 48(0x30) bytes
(c:\Users\charju\Documents\Visual Studio 2008\Projects\BasicWindbg\SOSBasics\SOSBasics\bin\Debug\SOSBasics.exe)
Fields:
MT Field Offset Type VT Attr Value Name
79102290 4000001 20 System.Int32 0 instance 6 generation
7910c348 4000002 4 System.Double 0 instance 123456789.000000 gold
790fd8c4 4000003 c System.String 0 instance 01573780 name
7910c878 4000004 24 System.DateTime 1 instance 01573774 age
79101fe4 4000005 10 ...ections.Hashtable 0 instance 015739fc systemKey
7912d8f8 4000006 14 System.Object[] 0 instance 01573ac4 leaders
790fd0f0 4000007 18 System.Object 0 instance 01573930 previousOne
00413364 4000008 1c SOSBasics.Zion 0 instance 01573af0 zion
首先映入眼帘的,是Name: SOSBasics.MatrixWorld,這表明這個對象的class name,剩下的幾行,我們暫且不用管它。看下面的這個table。該table一共有7個列,分別是MethodTable, field, offset, type, VirtulTable, Attribute, value和name。每行的內容,就是我們當前正在扒皮的object 01573750的內容。
命令!do最牛的地方是,所有的託管資源都可以這麼看。so,我們先看看第一行的generation。哦,它已經有值了,就是6,看一下My Code,6表明當前的NEO是第六代就是主,看第二行,是gold,值是123456789,而且類型是double。那麼看第三行,是name,它的value是什麼呢?是01573780 !
這是蝦米東東?這也是一個managed object,so,我們繼續用do來看它。
0:000> !do 01573780
Name: System.String
MethodTable: 790fd8c4
EEClass: 790fd824
Size: 38(0x26) bytes
(C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: The Matrix
Fields:
MT Field Offset Type VT Attr Value Name
79102290 4000096 4 System.Int32 0 instance 11 m_arrayLength
79102290 4000097 8 System.Int32 0 instance 10 m_stringLength
790ff328 4000098 c System.Char 0 instance 54 m_firstChar
790fd8c4 4000099 10 System.String 0 shared static Empty
>> Domain:Value 004b54a8:790d884c <<
7912dd40 400009a 14 System.Char[] 0 shared static WhitespaceChars
>> Domain:Value 004b54a8:01521470 <<
注意紅色部分!表明它是一個字串,值是:The Matrix。你仔細看這個table,發現一個普通的字串,裡面也有東西,有什麼arraylength,stringlength等。這個我們先不管,回到上一個table,繼續看。
name下面是age,地址是01573774,繼續!do 01573774 ,哦,為什麼出錯了呢?
0:000> !do 01573774
<Note: this object has an invalid CLASS field>
Invalid object
為蝦米是一個無效的對象呢?我們可以用!help dumpobj來看命令的協助
The column VT contains the value 1 if the field is a valuetype structure, and
0 if the field contains a pointer to another object. For valuetypes, you can
take the MethodTable pointer in the MT column, and the Value and pass them to
the command !DumpVC.
再看一下上面那個table摘錄下來的這個dataetime的東西
7910c878 4000004 24 System.DateTime 1 instance 01573774 age
ok,偶明白了。這個DateTime的VT列是1,so,我們需要換一個命令來看:!dumpvc,它需要兩個參數,一個是對應的methodtable,一個是當前的value。而methodtable就是最左面那個值,value就是最右面倒數第二個的值。so,重新用!dumpvc 7910c878 01573774 來看結果:
0:000> !dumpvc 7910c878 01573774
Name: System.DateTime
MethodTable 7910c878
EEClass: 7910c7d8
Size: 16(0x10) bytes
(C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
Fields:
MT Field Offset Type VT Attr Value Name
7910c19c 40000f4 0 System.UInt64 0 instance 662065056000000000 dateData
7912d7c0 40000f0 30 System.Int32[] 0 shared static DaysToMonth365
>> Domain:Value 004b54a8:0157397c <<
7912d7c0 40000f1 34 System.Int32[] 0 shared static DaysToMonth366
>> Domain:Value 004b54a8:015739bc <<
7910c878 40000f2 28 System.DateTime 1 shared static MinValue
>> Domain:Value 004b54a8:0157395c <<
7910c878 40000f3 2c System.DateTime 1 shared static MaxValue
>> Domain:Value 004b54a8:0157396c <<
好長一坨,第一行看那個System.UInt64就是我們的時間,誰有興趣就算一下。應該是一個ticket(我猜的)
我們繼續看015739fc systemKey這行,同樣,!do 015739fc,也是一大坨!
0:000> !do 015739fc
Name: System.Collections.Hashtable
MethodTable: 79101fe4
EEClass: 79101f74
Size: 56(0x38) bytes
(C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
Fields:
MT Field Offset Type VT Attr Value Name
7912d9bc 400092b 4 ...ashtable+bucket[] 0 instance 01573a34 buckets
79102290 400092c 1c System.Int32 0 instance 3 count
79102290 400092d 20 System.Int32 0 instance 1 occupancy
79102290 400092e 24 System.Int32 0 instance 7 loadsize
7910790c 400092f 28 System.Single 0 instance 0.720000 loadFactor
79102290 4000930 2c System.Int32 0 instance 3 version
7910be50 4000931 30 System.Boolean 0 instance 0 isWriterInProgress
79107ef8 4000932 8 ...tions.ICollection 0 instance 01575fcc keys
79107ef8 4000933 c ...tions.ICollection 0 instance 00000000 values
79116ef8 4000934 10 ...IEqualityComparer 0 instance 00000000 _keycomparer
790fd0f0 4000935 14 System.Object 0 instance 00000000 _syncRoot
79111df0 4000936 18 ...SerializationInfo 0 instance 00000000 m_siInfo
有幾個東西比較有意思。
第二行的Count,是3,表明我們的hashtable裡面有3個東西。倒數第五行和倒數第四行,分別是keys和values,我們繼續看一下吧!先看keys,!do 01575fcc
0:000> !do 01575fcc
Name: System.Collections.Hashtable+KeyCollection
MethodTable: 7911cd80
EEClass: 791fb9e8
Size: 12(0xc) bytes
(C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
Fields:
MT Field Offset Type VT Attr Value Name
79101fe4 400093a 4 ...ections.Hashtable 0 instance 015739fc _hashtable
奇怪了!為什麼do出來只有一行?我明明有三行資料啊!而且這個value=015739fc,怎麼這麼眼熟呢?哈!這不就是上面的hashtable嘛!怎麼繞來繞去又回來了呢?
ok,我們開始看下一個命令:dd。
重新看hashtable的第一行:7912d9bc 400092b 4 ...ashtable+bucket[] 0 instance 01573a34 buckets,這裡有一個buckets。地址是01573a34,那麼用dd來看一下:
0:000> dd 01573a34
01573a34 7912d9bc 0000000b 00000000 00000000
01573a44 00000000 00000000 00000000 00000000
01573a54 00000000 00000000 00000000 015737a8
01573a64 015737c8 e047abd0 00000000 00000000
01573a74 00000000 00000000 00000000 00000000
01573a84 00000000 00000000 00000000 00000000
01573a94 00000000 00000000 00000000 00000000
01573aa4 00000000 015737e8 0157380c 62e0f252
0:000> dd
01573ab4 01573844 01573860 742f0848 00000000
01573ac4 7912d8f8 00000007 790fd8c4 01573880
01573ad4 01573898 015738b0 015738c8 015738e0
01573ae4 015738f8 01573910 00000000 00413364
01573af4 015738f8 00000000 791013a0 00000000
01573b04 00000000 00000000 00000000 00000000
01573b14 00000000 790fd0f0 00000000 40010000
01573b24 79101444 0000000b 00000004 00000100
注意上面紅色部分,第一個0000000b,表明這個hashtable有11個對象,為啥這麼多?我不知道。我們看其他的
0:000> !do -nofields 015737a8
Name: System.String
MethodTable: 790fd8c4
EEClass: 790fd824
Size: 30(0x1e) bytes
(C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: Oracle
0:000> !do -nofields 015737c8
Name: System.String
MethodTable: 790fd8c4
EEClass: 790fd824
Size: 32(0x20) bytes
(C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: 會變臉的老女人
看到了?我們的key是oracle,值是:會變臉的老女人。這裡的do多了一個參數,叫做-nofields,對於string這些類型,能輸出的少點,看起來就舒服一點。類似的,我們能看到剩餘的2對key/value。
Hashtable暫時告一段落,我們繼續回到第一個table裡面,看Matrixworld裡面其他的對象。
7912d8f8 4000006 14 System.Object[] 0 instance 01573ac4 leaders
leaders,這是一個System.Object[]數群組類型,我們do一下:!do 01573ac4
0:000> !do 01573ac4
Name: System.Object[]
MethodTable: 7912d8f8
EEClass: 7912de6c
Size: 44(0x2c) bytes
Array: Rank 1, Number of elements 7, Type CLASS
Element Type: System.String
Fields:
None
顯然,不是我們想要的結果,所了一堆廢話而已:一維數組,7個元素,都是string類型的。那麼每個元素的內容是什麼呢?對於數組,我們可以用!da命令來看(歎號+da)。!da 01573ac4結果如下:
0:000> !da 01573ac4
Name: System.String[]
MethodTable: 7912d8f8
EEClass: 7912de6c
Size: 44(0x2c) bytes
Array: Rank 1, Number of elements 7, Type CLASS
Element Methodtable: 790fd8c4
[0] 01573880
[1] 01573898
[2] 015738b0
[3] 015738c8
[4] 015738e0
[5] 015738f8
[6] 01573910
嗯,very cool!一共7個都出來了,我們隨便do一個,比如說最後一個:!do -nofields 01573910
0:000> !do -nofields 01573910
Name: System.String
MethodTable: 790fd8c4
EEClass: 790fd824
Size: 32(0x20) bytes
(C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: 那個印度小女孩
哈,第七代救世主,那個印度小女孩。同理,其他的也能出來。
慢著,如果數組裡面有百八十個,這麼看,豈不是要累死嗎?我們!help dumparray,看一下協助,哦,有一個參數可以用,叫做-details。那麼我們可以這麼下da命令:!da -details 01573ac4 ,具體的聯絡,留給大家,不在這裡佔用版面了。
好!加油,感謝大家能看到這裡,這麼多copy/paste的結果,實在不好意思,嘿嘿。
現在繼續看00413364 4000008 1c SOSBasics.Zion 0 instance 01573af0 zion,哦,這是我們定義的第二個class:Zion,它也是人,so,do命令照樣好使。!do 01573af0
0:000> !do 01573af0
Name: SOSBasics.Zion
MethodTable: 00413364
EEClass: 004118ec
Size: 12(0xc) bytes
(c:\Users\charju\Documents\Visual Studio 2008\Projects\BasicWindbg\SOSBasics\SOSBasics\bin\Debug\SOSBasics.exe)
Fields:
MT Field Offset Type VT Attr Value Name
790fd8c4 4000009 4 System.String 0 instance 015738f8 one
很強大!只有一個field,就是我們的one,對於它,繼續do:!do -nofields 015738f8
0:000> !do -nofields 015738f8
Name: System.String
MethodTable: 790fd8c4
EEClass: 790fd824
Size: 24(0x18) bytes
(C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: NEO
cool!!!找到NEO了!(上面輸出的最後一行)
我們的do之旅也看就要完成了,堅持一下,看最後的一個,倒數第二個的資訊:790fd0f0 4000007 18 System.Object 0 instance 01573930 previousOne
0:000> !do 01573930
Name: System.String
MethodTable: 790fd8c4
EEClass: 790fd824
Size: 42(0x2a) bytes
(C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: NEO的前身,不知道是誰
Fields:
MT Field Offset Type VT Attr Value Name
79102290 4000096 4 System.Int32 0 instance 13 m_arrayLength
79102290 4000097 8 System.Int32 0 instance 12 m_stringLength
790ff328 4000098 c System.Char 0 instance 4e m_firstChar
790fd8c4 4000099 10 System.String 0 shared static Empty
>> Domain:Value 004b54a8:790d884c <<
7912dd40 400009a 14 System.Char[] 0 shared static WhitespaceChars
>> Domain:Value 004b54a8:01521470 <<
小菜一碟,這也是一個字串,是neo的前身。
(暈死,第一部分還沒有結束,再開一個文章,我們繼續看heap上的東西)