12

Following my previous question, in which I wanted to dump all the variables in the stack (from the current and all the previous frame) that can be seen here: Is there a way to examine the stack variables at runtime in C#?

I was suggested to intercept the calls manually or use AOP framework like PostSharp to simplify such a task. I looked at PostSharp and the interception arguments don't include the variables in the current stack frame. I wonder if there is a simple way to automatically get all the local variables in the current stack frame. I suppose I can perform code analysis and generate code that copies all those values into a collection, but maybe there is a built-in mechanism that does it.

Thanks in advance for any suggestions.

EDIT: I should have given more detail of why I want to do this. I want to be able to suspend execution in the middle of a method. If I had the contents of the stack I could resume execution later, or even serialize it and continue it in another machine (assuming it is relatively simple code, so no threads or I/O for example). It is ok to run a code analysis tool that would allow me to automatically generate extra code that saves this state. I think I will probably have to analyze the CIL to do this.

Community
  • 1
  • 1
cloudraven
  • 2,484
  • 1
  • 24
  • 49
  • 1
    Good luck with your goal. I'd recommend to read on details of garbage collection in .Net - it does suspsend execution of managed code and does manipulation with methods' data. (search for something like "insde .net garbage collection" to start) – Alexei Levenkov Mar 01 '11 at 00:23

3 Answers3

8
   System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace();
        System.Diagnostics.StackFrame frame = trace.GetFrame(0);
        MethodBase method = frame.GetMethod();
        MethodBody methodBody = method.GetMethodBody();
        if (methodBody != null)
        {
            foreach (var local in methodBody.LocalVariables)
            {
                Console.WriteLine(local);
            }
        }
        Console.ReadKey();
JDPeckham
  • 2,414
  • 2
  • 23
  • 26
  • 3
    This is sample how to see local variables defined in a method's IL. It will not help with runtime behavior (values/presence) nor information not present in IL - like names. – Alexei Levenkov Feb 27 '11 at 22:52
  • That almost works, but yes, I definitely need the names of the variable – cloudraven Feb 28 '11 at 06:16
  • To get the names, you would have to write code to look them up in the .pdb files if they are available... and it's not easy. – Owen Johnson Jul 01 '13 at 18:47
8

You should use debugging API and debug your program from another process. Writing your own managed debugger is not trivial, but at least supported way of achieving your stated goal.

To my knowledge there is nothing in managed .Net framwork that you can use to collect information about run-time state of local variables of a method.

Note that there are enough cases when values for local variables do not exist to make writing general code to handle them complex:

  • method can be inlined (local variables will be merged with calling methods)
  • local variables can be optimized out
  • local variable can get out of scope and be garbage collected befor end of method.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
3

Consider to Write minidumps using a custom PostSharp aspect (with IL transformation).

A shared debuging engine library, written in C#. is available on NuGet as Microsoft.Samples.Debugging.MdbgEngine.

The code is available on GitHub as part of the PADRE ( Pluggable Automatic Debugging and Reporting Engine)repository

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170