Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

List variables and their values

Hi,

After an exception is caught, I want to store the variables currently in-scope into a file.

I can obtain a stack trace, enabling me to discover where the error occurrs, but I want more information. i.e. All Variables.

Can I access the memory directly to get this information ?? when the CLR debugger is invoked, it has access to the variables.. but is there a tidy way to get these ??

Chrs
Smg

PS. I asked this a few weeks ago, but didn't get anywhere...
0
smegghead
Asked:
smegghead
  • 16
  • 12
2 Solutions
 
gregoryyoungCommented:
you can but its not going to be a 5 minute thing ...

here is a GREAT place to start.

http://msdn.microsoft.com/msdnmag/issues/02/11/CLRDebugging/default.aspx
0
 
smeggheadAuthor Commented:
Cool article, seems like it is possible, if only to get the values, rather than the names.. which is more help than nothing.

Has anybody actually done this ?
0
 
gregoryyoungCommented:
yes I have written a debugger that I integrated into my appication (also wrote a small compiler similar to LISP) you can get names if you have debugging symbols
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
smeggheadAuthor Commented:
Without the debugging symbols (I'm reluctant to release this to the end users - it's bad enough someone being able to reverse engineer apps) is it possible to list variables without their names.. like

v1="hello"
v2=10
v3=10/3/2003 12:30

Chrs
Smg.
0
 
gregoryyoungCommented:
no you could only say identify scope + location  ... but for reference types you could do more since they are self describing ....

i.e. I could realize that you have a reference to a foo object and then use reflections to figure out what foo is (get properties members etc) and list it

Greg
0
 
smeggheadAuthor Commented:
how do you get the scope/location in the first place ?? do you have to call api's or is it part of the framework ?
0
 
gregoryyoungCommented:
from the article ....

  Next, there's an issue that has been encountered over and over again in the article: how to map design-time constructs, such as variable names, to the runtime equivalent. Remember that the runtime knows nothing of high-level constructs like variable names. What it does know about are fields and offsets. Fields are class members, and offsets are local variable IL offsets from a base address just like they were with functions. The mapping between variable names and their IL representations is the same as it was for functions. Metadata is used alongside tokens to perform the design-time-to-runtime mapping.
  The next new concept is variable scopes. Although Active Script debugging supported scopes, it did not support the distinction of nested scopes within a local function scope. Such scopes may be represented as follows in a high-level language:
0
 
gregoryyoungCommented:
didnt see other post ... look at the code included with the article  (the variable window)
0
 
jrbergCommented:
I like to keep things simple,  When something goes wrong and I throw an exception I usually only care about the instance variables inside of my class.  To get these is simple.  The below code will print the names and values of every class global when the exception is thrown.  I hope this proves useful to you.


OUTPUT
Field Name : (MyClassVariable1) Field Value (wooop)
Field Name : (MyIntVariable1) Field Value (10)
Field Name : (AnotherVariable) Field Value (sdfsadfg)
Field Name : (MySuperDuperLong) Field Value (1231245)
Field Name : (MyDouble) Field Value (234234.3)


CODE
                class Test
      {
            string MyClassVariable1 = "wooop";
            int MyIntVariable1 = 10;
            string AnotherVariable = "sdfsadfg";
            long MySuperDuperLong=1231245;
            double MyDouble=234234.3;

            public void DoSomething()
            {
                  try
                  {
                        Console.WriteLine("asdfasdfsfd{0},{1}", "asdfasdf");
                  }
                  catch (Exception e)
                  {
                        Console.WriteLine(e.Message);
                        FieldInfo [] MyFields = this.GetType().GetFields( BindingFlags.Instance | BindingFlags.NonPublic );
                        foreach( FieldInfo MyFieldInfo in MyFields )
                        {
                              Console.WriteLine("Field Name : ({0}) Field Value ({1})", MyFieldInfo.Name, MyFieldInfo.GetValue(this) );
                        }
                  }
            }
      }
0
 
smeggheadAuthor Commented:
Hi,

This looks like it's heading in the right direction. However, most errors occur in functions which take paramers, so I'd be more interested in knowing these parameters. And if possible working my way down the stack to retrieve parameters/values at every level of the stack. Is this possible ??
0
 
gregoryyoungCommented:
thats where you get into the kinda stuff I sent.
0
 
smeggheadAuthor Commented:
Have you got any code examples ?? or could you email them ?? I'd really appreciate it.
0
 
gregoryyoungCommented:
yeah I can send you code examples .. you just want to see the local variables within a method ?
0
 
smeggheadAuthor Commented:
yeah, that would be an excellent starting point.

Basically, if an exception is thrown, it gets caught in a general catch-all try/catch block, can I still access the variables once the exception has been thrown ??

My email address is in my profile..

Thanks a lot.

Smg.
0
 
gregoryyoungCommented:
I have to look I believe you will have left scope at that point. Instead I believe you will need to call a function (which can then look by traversing up the stack)
0
 
smeggheadAuthor Commented:
ok, sounds good.
0
 
smeggheadAuthor Commented:
Hi,

Did you try to send the code ? I've not received anything yet.

Thanks.
0
 
gregoryyoungCommented:
doh sorry was busy with work and forgot ... ill try to get something out morrow.
0
 
smeggheadAuthor Commented:
thanks.
0
 
smeggheadAuthor Commented:
sorry Gregory, I know you're busy.. I'd still be interested to see you code if possible.

Thanks
0
 
smeggheadAuthor Commented:
...please !!
0
 
smeggheadAuthor Commented:
have I been forgotton ??? :-(
0
 
gregoryyoungCommented:
omg im sorry ...  ill have internet hopefully friday after my move ...


I am also writing an article on this ...
0
 
smeggheadAuthor Commented:
excellent, keep me posted. Who is the article for ??
0
 
gregoryyoungCommented:
i dunno yet ...

its about writing a managed debugger ... (I also write a version of turtle + an environment for it) then allow you to debug your turtle programs.
0
 
smeggheadAuthor Commented:
Hello !!! :-)
0
 
smeggheadAuthor Commented:
Just some code to point me in the right direction.. I don't need a fully working solution.

Chrs
smg.
0
 
gregoryyoungCommented:
drop me an email I am forgetful :(
0
 
smeggheadAuthor Commented:
I've awarded the points, but I'm still none the wiser as to how I get a list of variables within a function. If possible in the first place.

Smg.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 16
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now