检查现场对内存转储所有实例 [英] inspect field on all instances in memory dump

查看:199
本文介绍了检查现场对内存转储所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我用来解决客户问题,内存转储。这是一个.NET(C#)应用程序。在我的申请的问题是,正在创建特定类的情况太多。有此上课的时候​​应该有类似20.我想遍历所有这些实例,并调用每个这些实例的名称字段6300实例。有没有简单的方法来做到这一点在WinDbg中/ SOS?

I have a memory dump that I'm using to troubleshoot a client issue. This is a .NET (C#) application. The problem with my application is that too many instances of a particular class are being created. There are 6300 instances of this class when there should be something like 20. I want to loop through all of those instances and call the name field of each of those instances. Is there any easy way to do this in WinDbg/SOS?

我知道我可以使用!dumpheap型{}类型名称找到该类的所有实例,但我不知道我怎么能扩大它们并查看我感兴趣的领域。

I know I can use !dumpheap -type {typename} to find all the instances of that class, but I'm not sure how I can expand them all and view the field I am interested in.

推荐答案

之内的Windbg你可以用 .foreach 执行此命令。

You can do this with .foreach command within Windbg.

下面是一个简单的例子

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 具有只会返回对象地址的短选项。在实例中我调试MT为计划是00293858

The !dumpheap has a short option which would just return the object address. In the instance i am debugging MT for Program is 00293858

!dumpheap -mt 00293858 -short

下面是一个code转储中的所有对象 .foreach($ OBJ {!dumpheap -mt 00293858 -short}){!做$ OBJ} 使用foreach构造。的$物镜将获得分配与对象的地址。这里距离foreach循环输出样本

Here is a code to dump all the objects .foreach ($obj {!dumpheap -mt 00293858 -short}) {!do $obj} using the foreach construct. The $obj would get assigned with the address of the object. And here is the sample output from the foreach loop

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

现在,我们有这个,下一步是让程序的每个实例中的现场考这里是code要做到这一点

Now that we have this , the next step is to get the field "test" within each instance of program and here is the code to do that

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

我使用foreach循环中的 POI 命令。从上面的结果,我们可以做出来的测试变量是在4偏移,这对使用 POI($ {$ OBJ} +为0x4的原因) 在这里,从上面的foreach输出样本

I am using poi command within the foreach loop. From the above result we can make out the test variable is in the 4 offset and that's the reason for using poi(${$obj}+0x4) And here is the sample output from the above 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 <<

这里是为计划

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

本富是在8偏移,这里是样本上面的foreach输出

The Foo is in the 8th offset and here is sample the output for the above 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

编辑: - 而且,这里是一个<一个href="http://blogs.msdn.com/b/tess/archive/2007/09/18/debugging-script-dumping-out-asp-net-session-contents.aspx">post从苔丝倾销会话内容

- Also here is a post from Tess on dumping session contents

心连心

这篇关于检查现场对内存转储所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆