检查内存转储中所有实例的字段

Bry*_*yan 6 .net debugging windbg sos

我有一个内存转储,我用它来解决客户端问题.这是一个.NET(C#)应用程序.我的应用程序的问题是正在创建特定类的太多实例.当应该有类似20的东西时,这个类有6300个实例.我想循环遍历所有这些实例并调用每个实例的名称字段.在WinDbg/SOS中有没有简单的方法呢?

我知道我可以使用!dumpheap -type {typename}来查找该类的所有实例,但我不确定如何将它们全部扩展并查看我感兴趣的字段.

Nav*_*een 16

您可以使用.foreachWindbg中的命令执行此操作.

这是一个简单的例子

using System;
using System.Collections.Generic;
using System.Linq;
namespace Test
{
    class Program
    {
        static List<Program> list = new List<Program>();
        int i;
        string test;
        Foo f;
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                list.Add(new Program() { i = i, test = "Test" + i.ToString(), f = new Foo(i) });
            }
            Console.Read();
        }
    }
    class Foo
    {
        int j;
        public Foo(int i)
        {
            j = i;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

!dumpheap有一个短的选项,这将只返回对象的地址.在我调试MT的实例中Program是00293858

!dumpheap -mt 00293858 -short
Run Code Online (Sandbox Code Playgroud)

这是.foreach ($obj {!dumpheap -mt 00293858 -short}) {!do $obj}使用foreach构造转储所有对象的代码.$ obj将被赋予对象的地址.这是foreach循环的示例输出

Name:        Test.Program
MethodTable: 00293858
EEClass:     00291440
Size:        20(0x14) bytes
File:        c:\users\nsrinivasan\documents\visual studio 2010\Projects\Test\Test\bin\Debug\Test.exe
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
5c2e2978  4000002        c         System.Int32  1 instance        3 i
5c2df9ac  4000003        4        System.String  0 instance 0217c144 test
00293bfc  4000004        8             Test.Foo  0 instance 0217c15c f
002938b4  4000001        4 ...t.Program, Test]]  0   static 0217b97c list
Name:        Test.Program
MethodTable: 00293858
EEClass:     00291440
Size:        20(0x14) bytes
File:        c:\users\nsrinivasan\documents\visual studio 2010\Projects\Test\Test\bin\Debug\Test.exe
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
5c2e2978  4000002        c         System.Int32  1 instance        4 i
5c2df9ac  4000003        4        System.String  0 instance 0217c18c test
00293bfc  4000004        8             Test.Foo  0 instance 0217c1a4 f
002938b4  4000001        4 ...t.Program, Test]]  0   static 0217b97c list
Run Code Online (Sandbox Code Playgroud)

现在我们有了这个,下一步是在每个程序实例中获得字段"test",这是执行该操作的代码

 .foreach ($obj {!dumpheap -mt 00293858 -short}) {!do poi(${$obj}+0x4)} 
Run Code Online (Sandbox Code Playgroud)

poi在foreach循环中使用命令.从上面的结果我们可以看出test变量在4偏移量中,这就是使用的原因.poi(${$obj}+0x4) 这里是上面foreach的示例输出

0:004> .foreach ($obj {!dumpheap -mt 00293858       -short}) {!do poi(${$obj}+0x4)}
Name:        System.String
MethodTable: 5c2df9ac
EEClass:     5c018bb0
Size:        24(0x18) bytes
File:        C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String:      Test0
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
5c2e2978  40000ed        4         System.Int32  1 instance        5 m_stringLength
5c2e1dc8  40000ee        8          System.Char  1 instance       54 m_firstChar
5c2df9ac  40000ef        8        System.String  0   shared   static Empty
    >> Domain:Value  002f76c0:02171228 <<
Name:        System.String
MethodTable: 5c2df9ac
EEClass:     5c018bb0
Size:        24(0x18) bytes
File:        C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String:      Test1
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
5c2e2978  40000ed        4         System.Int32  1 instance        5 m_stringLength
5c2e1dc8  40000ee        8          System.Char  1 instance       54 m_firstChar
5c2df9ac  40000ef        8        System.String  0   shared   static Empty
    >> Domain:Value  002f76c0:02171228 <<
Run Code Online (Sandbox Code Playgroud)

这里是为了让每个Foo实例都在Program课堂上

.foreach ($obj {!dumpheap -mt 00293858  -short}) {!do poi(${$obj}+0x8)}
Run Code Online (Sandbox Code Playgroud)

Foo在第8个偏移量中,这里是上述foreach的输出样本

Name:        Test.Foo
MethodTable: 00293bfc
EEClass:     0029194c
Size:        12(0xc) bytes
File:        c:\users\nsrinivasan\documents\visual studio 2010\Projects\Test\Test\bin\Debug\Test.exe
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
5c2e2978  4000005        4         System.Int32  1 instance        0 j
Name:        Test.Foo
MethodTable: 00293bfc
EEClass:     0029194c
Size:        12(0xc) bytes
File:        c:\users\nsrinivasan\documents\visual studio 2010\Projects\Test\Test\bin\Debug\Test.exe
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
5c2e2978  4000005        4         System.Int32  1 instance        1 j
Run Code Online (Sandbox Code Playgroud)

编辑: - 这里也是Tess关于转储会话内容的帖子

HTH