【问题标题】:inspect field on all instances in memory dump检查内存转储中所有实例的字段
【发布时间】:2010-10-13 18:00:22
【问题描述】:

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

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

【问题讨论】:

    标签: .net debugging windbg sos


    【解决方案1】:

    您可以在 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;
            }
        }
    }
    

    !dumpheap 有一个简短的选项,它只返回对象地址。在我正在为Program 调试MT 的实例中是00293858

    !dumpheap -mt 00293858 -short
    

    这是使用 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
    

    现在我们有了这个,下一步是在每个程序实例中获取“测试”字段,这是执行此操作的代码

     .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 <<
    

    这里用于获取Program 类中的每个Foo 实例

    .foreach ($obj {!dumpheap -mt 00293858  -short}) {!do poi(${$obj}+0x8)}
    

    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
    

    编辑:- 这里还有一个来自 Tess 的 post 转储会话内容

    HTH

    【讨论】:

    • 这太完美了!!感谢您的出色回答。现在解决修复我的应用程序的真正问题:)
    • 是的,我在问完问题后找到了 Tess 的帖子。
    猜你喜欢
    • 2011-09-30
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    相关资源
    最近更新 更多