首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >查看内存中变量的内容和值

查看内存中变量的内容和值
EN

Stack Overflow用户
提问于 2017-08-17 13:03:24
回答 1查看 138关注 0票数 0

我正在尝试创建一个调试工具,它将附加到一个进程,然后查看堆栈和堆的内容。

到目前为止,我使用CLRmd附加到一个进程,然后获得堆栈和堆中变量类型的列表,但仍然无法获得元素的值。

有没有什么方法可以让我得到这些值呢?为什么visual studio调试器能够做到这一点?

语言不是这里的约束。

EN

回答 1

Stack Overflow用户

发布于 2017-10-02 19:11:05

我使用ClrMd NuGet包(0.8.31.1版)创建了以下程序来显示对象的内容,即字段名称和值:

代码语言:javascript
运行
复制
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.Diagnostics.Runtime;

namespace ClrMdTest
{
    class Program
    {
        static void Main(string[] args)
        {    
            var live = DataTarget.AttachToProcess(
                Process.GetProcessesByName("clrmdexampletarget")[0].Id,
                1000, AttachFlag.Passive);
            var liveClrVersion = live.ClrVersions[0];
            var liveRuntime = liveClrVersion.CreateRuntime();
            var addresses = liveRuntime.Heap.EnumerateObjectAddresses();

            // The where clause does some consistency check for live debugging
            // when the GC might cause the heap to be in an inconsistent state.
            var singleObjects = from obj in addresses
                let type = liveRuntime.Heap.GetObjectType(obj)
                where
                    type != null && !type.IsFree && !string.IsNullOrEmpty(type.Name) &&
                    type.Name.StartsWith("SomeInterestingNamespace")
                select new { Address = obj, Type = type};

            foreach (var singleObject in singleObjects)
            {
                foreach (var field in singleObject.Type.Fields)
                {
                    Console.WriteLine(field.Name + " =");
                    Console.WriteLine("   " + field.GetValue(singleObject.Address));
                }
            }

            Console.ReadLine();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45726928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档