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

.net 检查内存转储中所有实例上的字段,.net,debugging,windbg,sos,.net,Debugging,Windbg,Sos,我有一个内存转储,用于解决客户端问题。这是一个.NET(C#)应用程序。我的应用程序的问题是创建了太多特定类的实例。这个类有6300个实例,而实际上应该有20个。我希望遍历所有这些实例,并调用每个实例的name字段。在WinDbg/SOS中有没有简单的方法可以做到这一点 我知道我能用!dumpheap-键入{typename}以查找该类的所有实例,但我不确定如何将它们全部展开并查看我感兴趣的字段。您可以使用Windbg中的.foreach命令执行此操作 下面是一个简单的例子 using Syst

我有一个内存转储,用于解决客户端问题。这是一个.NET(C#)应用程序。我的应用程序的问题是创建了太多特定类的实例。这个类有6300个实例,而实际上应该有20个。我希望遍历所有这些实例,并调用每个实例的name字段。在WinDbg/SOS中有没有简单的方法可以做到这一点


我知道我能用!dumpheap-键入{typename}以查找该类的所有实例,但我不确定如何将它们全部展开并查看我感兴趣的字段。

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

下面是一个简单的例子

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;
        }
    }
}
下面是使用foreach构造转储所有对象的代码
.foreach($obj{!dumpheap-mt 00293858-short}){!do$obj}
。$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
现在我们有了这个,下一步是在程序的每个实例中获得字段“test”,下面是代码

 .foreach ($obj {!dumpheap -mt 00293858 -short}) {!do poi(${$obj}+0x4)} 
我正在foreach循环中使用
poi
命令。从上面的结果我们可以看出
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 <<
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
Foo位于第8个偏移量,下面是上述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 <<
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
编辑:-这里还有Tess关于转储会话内容的评论


这太完美了!!谢谢你的回答。现在来谈谈修复我的应用程序的真正问题:)是的,在我问了这个问题之后,我找到了苔丝的帖子。