microsoft / clrmd

Microsoft.Diagnostics.Runtime is a set of APIs for introspecting processes and dumps.
MIT License
1.05k stars 256 forks source link

How to get stack variables and their values? #1235

Closed P9avel closed 8 months ago

P9avel commented 8 months ago

Hi. i am wrote code

`internal class Program { static void Main(string[] args) {

    // I am want  to get this variables
    int i = 123;
    var o = new object();

    var pid = Process.GetCurrentProcess().Id;
    using (var dataTarget = DataTarget.AttachToProcess(pid, false))
    {
        ClrInfo currentRuntime = dataTarget.ClrVersions[0];
        ClrRuntime runtime = currentRuntime.CreateRuntime();

        foreach (ClrThread thread in runtime.Threads)
        {
            if (!thread.EnumerateStackTrace(true).Any())
            {
                Console.WriteLine(thread.ManagedThreadId);

               // here current stack
               // How to get 'i' and 'o' names and values?
            }
            Console.WriteLine("");
        }
    }
    Console.WriteLine("Hello, World!");
}`

How to get 'i' and 'o' names and values?

leculver commented 8 months ago

ClrMD doesn't offer any way to get locals or arguments. You would need to use a real debugging API like ICorDebug to get those.

ClrMD is not meant to be a debugger, it's a runtime and memory inspection API. Since the CLR Runtime itself doesn't "track" locals and arguments in a way that's directly represented in the runtime, we don't expose it as part of the ClrMD api.

I know that distinction can be confusing, but in a similar way we don't offer breakpoints and stepping in ClrMD either.