1

For reference, I have reviewed the following questions: C# reusable function to dump current value of local variables, Find size of object instance in bytes in c#

However this question is a somewhat different take on the issue. Per this answer, local variables are stored on the stack (at least for value types).

My goal: A reusable method that could be called in any other method, and automatically produce a memory dump of only the local variables of that method. I am not necessarily looking for something that immediately returns objects - a raw byte array from memory is fine.

I do not want a full process dump, if at all possible.

What I have been trying:

My initial thought was that I could try to get the object memory addresses. I understand that the addresses of managed objects can be obtained by:

TypedReference tr = __makeref(obj);
return **(IntPtr**)(&tr);

However, I cannot figure out how to actually get the addresses of the local variables without manually calling this for every single variable.

I then thought about using the stack and the method information to determine how many local variables I needed, and the type. Using the following code, for example, I can get the method information:

catch (Exception ex)
    var trace = new System.Diagnostics.StackTrace(ex);
    var frame = trace.GetFrame(0);
    var method = frame.GetMethod();
    var locals = method.GetMethodBody().LocalVariables;
}

However, I am lacking information on how to get the latest data from the stack. I understand that this may be very complicated, but I am interested in seeing what can be done in C# without having to resort to external dependencies or third-party tools.

With that in mind, using either safe or unsafe code (documented or undocumented), is there a way to pop the last n items from the stack or to get the most recently added memory addresses?

If not, is there a better approach to accomplishing this goal?

Note: I just discovered SOS.DLL. Will this help?

Community
  • 1
  • 1
vbnet3d
  • 1,151
  • 1
  • 19
  • 37
  • 2
    Are you trying to build a debugger? Know that while the IL code uses a stack, the JITter may use registers as well. – Lasse V. Karlsen Oct 20 '16 at 16:42
  • I suppose so, yes. I am open to whatever is the best method of achieving my end goal, which is automated local variable dumping on exceptions. Ideally, I would like a solution that works with Debug and Release builds. – vbnet3d Oct 20 '16 at 16:44
  • Use of the stack is an implementation detail and not guaranteed by the spec, so any solution will rely on specifics of the implementation (and as Lasse noted, the JITer can well move variables to registers even if the .NET implementation otherwise puts them on the stack). – Eric J. Oct 20 '16 at 16:44
  • You may want to look into using https://msdn.microsoft.com/en-us/library/windows/desktop/ms679309(v=vs.85).aspx – xxbbcc Oct 20 '16 at 16:44
  • @EricJ. If it helps, this will probably only be used on one framework version (4.5) and one platform (Windows 10). I am not sure how much of a difference that would make? – vbnet3d Oct 20 '16 at 16:47
  • @vbnet3d That assumption is rather risky to make. Even framework patches may introduce changes to runtime behavior. – xxbbcc Oct 20 '16 at 16:48
  • @xxbbcc In this situation, we control distribution very tightly, but I see what you mean. – vbnet3d Oct 20 '16 at 16:48
  • @LasseV.Karlsen Out of curiousity, could you provide an example of how to access the stack and/or a JIT register? – vbnet3d Oct 20 '16 at 19:38
  • I wouldn't know how to do that, I have no experience with programming a debugger. – Lasse V. Karlsen Oct 21 '16 at 09:44

0 Answers0