使用windbg查看DependencyObject的屬性

這裏以WPF做爲探測用的例子,簡單一些,看看Title的值是什麼樣子。(之因此寫這個,由於不是簡單的一個!do就能看到東西的,中間要繞兩下,這也涉及到了DependencyObject的實現機制及數據的存儲機制)express

 

首先用VS新建一個WPF工程,在XAML中咱們修改一下Title屬性和Background屬性,運行,獲得界面以下(我故意縮小了窗口寬度以及高度)lua

image

那麼,Title的完整字符串是什麼呢?窗體的背景色是什麼呢?spa

 

與常規的managed debugging同樣,首先attach到該進程上,而後加載sosdebug

image

0:011> .loadby sos clr3d

窗體的類型默認是MainWindow,因此咱們直接到heap裏面去找。orm

0:011> !dumpheap -type MainWindow對象

*** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll - blog

PDB symbol for clr.dll not loaded進程

Address MT Sizeci

0000008300043bc8 00007ffa11afb918 664

Statistics:

MT Count TotalSize Class Name

00007ffa11afb918 1 664 WindbgDependency.MainWindow

Total 1 objects

咱們看到,只有一個object,地址是0000008300043bc8

而後咱們來do一下看看

0:011> !do 0000008300043bc8

Name: WindbgDependency.MainWindow

MethodTable: 00007ffa11afb918

EEClass: 00007ffa11c41ec8

Size: 664(0x298) bytes

File: c:\users\charju\documents\visual studio 2013\Projects\WindbgDependency\WindbgDependency\bin\Debug\WindbgDependency.exe

Fields:

MT Field Offset Type VT Attr Value Name

00007ffa50764130 4000e1f 8 ...eading.Dispatcher 0 instance 000000830000c748 _dispatcher

00007ffa5076ac00 400137d 10 ...endencyObjectType 0 instance 00000083000446c8 _dType

00007ffa6f67e0f0 400137e 18 System.Object 0 instance 0000000000000000 _contextStorage

00007ffa5076d668 400137f 20 ...ctiveValueEntry[] 0 instance 00000083000ad3c0 _effectiveValues

………………………..(這裏忽略了其餘輸出)

00007ffa50764c80 4000330 1d0 ...spatcherOperation 0 instance 0000000000000000 _contentRenderedCallback

00007ffa6f679080 4000331 1d8 System.WeakReference 0 instance 0000000000000000 _currentPanningTarget

00007ffa50767440 40002e2 998 ...ependencyProperty 0 static 000000830001fdc0 TaskbarItemInfoProperty

00007ffa50767440 40002e3 9a0 ...ependencyProperty 0 static 00000083000203a0 AllowsTransparencyProperty

00007ffa50767440 40002e4 9a8 ...ependencyProperty 0 static 00000083000205a0 TitleProperty

00007ffa50767440 40002e5 9b0 ...ependencyProperty 0 static 00000083000207d8 IconProperty

00007ffa50767440 40002e6 9b8 ...ependencyProperty 0 static 00000083000209f0 SizeToContentProperty

00007ffa50767440 40002e7 9c0 ...ependencyProperty 0 static 0000008300020e48 TopProperty

有兩個東西是咱們感興趣的,第一個_effectiveValues是一個列表,包含了該object全部屬性的value。第二個TitleProperty是一個DependencyObject,包含了咱們窗體Title的信息。

首先檢查TitleProperty

0:011> !do 00000083000205a0

Name: System.Windows.DependencyProperty

MethodTable: 00007ffa50767440

EEClass: 00007ffa505e7040

Size: 88(0x58) bytes

File: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll

Fields:

MT Field Offset Type VT Attr Value Name

00007ffa6f67db10 4001398 8 System.String 0 instance 000000830001d9f8 _name

00007ffa6f67f6e0 4001399 10 System.Type 0 instance 00000083000021d0 _propertyType

00007ffa6f67f6e0 400139a 18 System.Type 0 instance 000000830001d0f8 _ownerType

00007ffa50765ef8 400139b 20 ....PropertyMetadata 0 instance 0000008300020540 _defaultMetadata

00007ffa50766048 400139c 28 ...dateValueCallback 0 instance 0000008300020500 _validateValueCallback

00007ffa5076d520 400139d 30 ...ndencyPropertyKey 0 instance 0000000000000000 _readOnlyKey

00007ffa509d1f98 400139e 40 System.Int32 1 instance 262153 _packedData

00007ffa5076cf78 400139f 48 ....InsertionSortMap 1 instance 00000083000205e8 _metadataMap

00007ffa50766100 40013a0 38 ...erceValueCallback 0 instance 0000000000000000 _designerCoerceValueCallback

00007ffa6f67e0f0 4001397 838 System.Object 0 static 0000008300008740 UnsetValue

00007ffa5076b9a0 40013a1 830 ...ty, WindowsBase]] 1 static 0000008310007ce8 RegisteredPropertyList

00007ffa6f681670 40013a2 840 ...ections.Hashtable 0 static 0000008300009f78 PropertyFromName

00007ffa6f6803f8 40013a3 5d4 System.Int32 1 static 402 GlobalIndexCount

00007ffa6f67e0f0 40013a4 848 System.Object 0 static 0000008300009fc8 Synchronized

00007ffa6f67f6e0 40013a5 850 System.Type 0 static 0000008300009fe0 NullableType

重點在_packedData上,該值的低16位是一個PropertyIndex,既然是index,那麼是哪一個列表數據的index呢?其實就對應着上面的_effectiveValues。咱們先查看index是什麼

0:011> ? 0n262153

Evaluate expression: 262153 = 00000000`00040009

ok,低16位就是0009,那麼咱們此時就能夠查看列表數據了

0:011> !da -details 00000083000ad3c0
Name:        System.Windows.EffectiveValueEntry[]
MethodTable: 00007ffa5076d668
EEClass:     00007ffa50612f30
Size:        440(0x1b8) bytes
Array:       Rank 1, Number of elements 26, Type VALUETYPE
Element Methodtable: 00007ffa507687d8
[0] 00000083000ad3d0
    Name:        System.Windows.EffectiveValueEntry
    MethodTable: 00007ffa507687d8
    EEClass:     00007ffa50611710
    Size:        32(0x20) bytes
    File:        C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll
    Fields:
                      MT    Field   Offset                 Type VT     Attr            Value Name
        00007ffa6f67e0f0  40013c9        0            System.Object      0     instance     000000830000edf0     _value
        00007ffa6f654038  40013ca        8             System.Int16      1     instance                    0     _propertyIndex
        00007ffa509d2058  40013cb        a             System.Int16      1     instance                   11     _source
[1] 00000083000ad3e0
    Name:        System.Windows.EffectiveValueEntry
    MethodTable: 00007ffa507687d8
    EEClass:     00007ffa50611710
    Size:        32(0x20) bytes
    File:        C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll
    Fields:
                      MT    Field   Offset                 Type VT     Attr            Value Name
        00007ffa6f67e0f0  40013c9        0            System.Object      0     instance     000000830001a9d8     _value
        00007ffa6f654038  40013ca        8             System.Int16      1     instance                    3     _propertyIndex
        00007ffa509d2058  40013cb        a             System.Int16      1     instance                   11     _source
[2] 00000083000ad3f0
    Name:        System.Windows.EffectiveValueEntry
    MethodTable: 00007ffa507687d8
    EEClass:     00007ffa50611710
    Size:        32(0x20) bytes
    File:        C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll
    Fields:
                      MT    Field   Offset                 Type VT     Attr            Value Name
        00007ffa6f67e0f0  40013c9        0            System.Object      0     instance     000000830009fe10     _value
        00007ffa6f654038  40013ca        8             System.Int16      1     instance                    5     _propertyIndex
        00007ffa509d2058  40013cb        a             System.Int16      1     instance                   11     _source
[3] 00000083000ad400
    Name:        System.Windows.EffectiveValueEntry
    MethodTable: 00007ffa507687d8
    EEClass:     00007ffa50611710
    Size:        32(0x20) bytes
    File:        C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll
    Fields:
                      MT    Field   Offset                 Type VT     Attr            Value Name
        00007ffa6f67e0f0  40013c9        0            System.Object      0     instance    
000000830004b9f8     _value
        00007ffa6f654038  40013ca        8             System.Int16      1     instance                   
9     _propertyIndex
        00007ffa509d2058  40013cb        a             System.Int16      1     instance                   11     _source
[4] 00000083000ad410
    Name:        System.Windows.EffectiveValueEntry
    MethodTable: 00007ffa507687d8
    EEClass:     00007ffa50611710
    Size:        32(0x20) bytes
    File:        C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll
    Fields:
                      MT    Field   Offset                 Type VT     Attr            Value Name
        00007ffa6f67e0f0  40013c9        0            System.Object      0     instance     00000083000bea58     _value
        00007ffa6f654038  40013ca        8             System.Int16      1     instance                   12     _propertyIndex
        00007ffa509d2058  40013cb        a             System.Int16      1     instance                   11     _source

這個列表裏面,第三個元素的_propertyIndex就是0009,它對應的_value是000000830004b9f8。到這裏就ok了,直接do一下看看

0:011> !do 000000830004b9f8    
Name:        System.String
MethodTable: 00007ffa6f67db10
EEClass:     00007ffa6efb6a18
Size:        60(0x3c) bytes
File:        C:\WINDOWS\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String:      魔法能量結合,巴啦啦能量,風雷電極

Title已經獲得了,那麼background是什麼顏色呢?

相似的,

0:019> ? 0n131290
Evaluate expression: 131290 = 00000000`000200da

後16位是0xda,再轉換一下,10進制對應的是218
0:019> ? da
Evaluate expression: 218 = 00000000`000000da

 

那麼,從_effectiveValue中能夠看到_propertyIndex=218的是16號元素

[16] 00000006e71a21c0
    Name:        System.Windows.EffectiveValueEntry
    MethodTable: 00007ffa507687d8
    EEClass:     00007ffa50611710
    Size:        32(0x20) bytes
    File:        C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll
    Fields:
                      MT    Field   Offset                 Type VT     Attr            Value Name
        00007ffa6f67e0f0  40013c9        0            System.Object      0     instance    
00000006e70e8be8     _value
        00007ffa6f654038  40013ca        8             System.Int16      1     instance                 218     _propertyIndex
        00007ffa509d2058  40013cb        a             System.Int16      1     instance                   11     _source


看這個對象,其實是一個純色

0:019> !do 00000006e70e8be8    
Name:        System.Windows.Media.SolidColorBrush

對應的顏色,也是一個DependencyObject

0:019> !do 00000006e70e8be8    
Name:        System.Windows.Media.SolidColorBrush
MethodTable: 00007ffa4fc8fc60
EEClass:     00007ffa4f948c58
Size:        80(0x50) bytes
File:        C:\WINDOWS\Microsoft.Net\assembly\GAC_64\PresentationCore\v4.0_4.0.0.0__31bf3856ad364e35\PresentationCore.dll
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
00007ffa50764130  4000e1f        8 ...eading.Dispatcher  0 instance 0000000000000000 _dispatcher
00007ffa5076ac00  400137d       10 ...endencyObjectType  0 instance 00000006e70d03b8 _dType
00007ffa6f67e0f0  400137e       18        System.Object  0 instance 0000000000000000 _contextStorage
00007ffa5076d668  400137f       20 ...ctiveValueEntry[]  0 instance 00000006e70e8c78 _effectiveValues
00007ffa6f651800  4001380       28        System.UInt32  1 instance       1080557569 _packedData
00007ffa50767440  400137b      7f0 ...ependencyProperty  0   static 00000006e70b6038 DirectDependencyProperty
00007ffa5076d580  400137c      7f8 ...ndler, mscorlib]]  0   static 00000006e70b6090 InheritanceContextChangedHandlersField
00007ffa6f67e0f0  4001381      800        System.Object  0   static 00000006e70b60b0 ExpressionInAlternativeStore
00007ffa507693b0  4001382      808 ...onStorageCallback  0   static 00000006e70b7640 _getExpressionCore
00007ffa5076d5f8  4001383      810 ...bject, mscorlib]]  0   static 00000006e70b60c8 DependentListMapField
00007ffa5076ac00  4001384      818 ...endencyObjectType  0   static 00000006e70af4b0 DType
00007ffa50767440  40013ed       30 ...ependencyProperty  0 instance 0000000000000000 _property
00007ffa50770928  40013ec        8 ...able+EventStorage  0 TLstatic  _eventStorage
    >> Thread:Value <<
00007ffa5076dbd0  4000d59      d60 ...rence, mscorlib]]  0   static 0000000000000000 StoredWeakReferenceField
00007ffa50767440  4001ffd     2a70 ...ependencyProperty  0   static 00000006e70d05c0 OpacityProperty
00007ffa50767440  4001ffe     2a78 ...ependencyProperty  0   static 00000006e70d0748 TransformProperty
00007ffa50767440  4001fff     2a80 ...ependencyProperty  0   static 00000006e70d08d0 RelativeTransformProperty
00007ffa4fc8b388  4002000     2a88 ...s.Media.Transform  0   static 00000006e70b5840 s_Transform
00007ffa4fc8b388  4002001     2a90 ...s.Media.Transform  0   static 00000006e70b5840 s_RelativeTransform
00007ffa4fc92da8  4002369       38 ...tiChannelResource  1 instance 00000006e70e8c20 _duceResource
00007ffa50767440  4002368     3108 ...ependencyProperty  0   static
00000006e70d0360 ColorProperty
00007ffa4fc8c878  400236a     3100 ...ndows.Media.Color  1   static 00000006f708c898 s_Color

經過上面同樣的步驟,咱們最終找到這個ColorProperty的對應value以下:

0:019> !do 00000006e70e8c38    
Name:        System.Windows.Media.Color
MethodTable: 00007ffa4fc8c878
EEClass:     00007ffa4f948200
Size:        64(0x40) bytes
File:        C:\WINDOWS\Microsoft.Net\assembly\GAC_64\PresentationCore\v4.0_4.0.0.0__31bf3856ad364e35\PresentationCore.dll
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
00007ffa4fc90570  400206d        8 ...edia.ColorContext  0 instance 0000000000000000 context
00007ffa50396648  400206e       20 ...a.Color+MILColorF  1 instance 00000006e70e8c58 scRgbColor
00007ffa4fca4bb8  400206f       30 ...ia.Color+MILColor  1 instance
00000006e70e8c68 sRgbColor
00007ffa6f641be0  4002070       10      System.Single[]  0 instance 0000000000000000 nativeColorValue
00007ffa6f653968  4002071       18       System.Boolean  1 instance                0 isFromScRgb

 

如上的sRgbColor就是顏色了,注意它的VT=1,便是個值類型。

0:019> dd 00000006e70e8c68 l 1
00000006`e70e8c68  00a5ffff

若是稍微熟悉一下GDI,就知道它對應的RGB是0xff, 0xa5, 0x00,什麼顏色呢?能夠到mspaint裏面簡單看一下

 image

額,知道了,就是橙色。

最後咱們看一下這個簡單窗口的全貌

image

 

(在女兒的教導下,小魔仙的各類咒語我幾乎都滾瓜爛熟,咱們還發明瞭其餘的,好比:巴啦啦能量,淨化!這是在晚上洗臉時念的;巴啦啦能量,凍結!這是對她晚安時說的)

相關文章
相關標籤/搜索