0

I am trying to create a debugging tool, that would attach to a process and then view the contents of stack and heap.

Till now I am using CLRmd to attach to a process and then get the list of the type of variables inside stack and heap but still not able to get the values of the elements.

Is there any way through which I would be able to get the values? How come visual studio debugger is able to do that?

Language is not the constraint here.

Paradox
  • 327
  • 3
  • 19

1 Answers1

0

I created the following program with the ClrMd NuGet package (version 0.8.31.1) to display the contents of an object, i.e. field names and values:

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();
        }
    }
}
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222