?
Solved

List variables and their values

Posted on 2004-04-28
30
Medium Priority
?
204 Views
Last Modified: 2010-04-15
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
Comment
Question by:smegghead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 12
30 Comments
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 1600 total points
ID: 10940382
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
 
LVL 10

Author Comment

by:smegghead
ID: 10940479
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10940536
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!

 
LVL 10

Author Comment

by:smegghead
ID: 10940615
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10940720
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
 
LVL 10

Author Comment

by:smegghead
ID: 10940915
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10940925
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10940938
didnt see other post ... look at the code included with the article  (the variable window)
0
 
LVL 2

Accepted Solution

by:
jrberg earned 400 total points
ID: 10981982
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
 
LVL 10

Author Comment

by:smegghead
ID: 10982351
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10982581
thats where you get into the kinda stuff I sent.
0
 
LVL 10

Author Comment

by:smegghead
ID: 10982595
Have you got any code examples ?? or could you email them ?? I'd really appreciate it.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11002180
yeah I can send you code examples .. you just want to see the local variables within a method ?
0
 
LVL 10

Author Comment

by:smegghead
ID: 11003648
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11003762
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
 
LVL 10

Author Comment

by:smegghead
ID: 11003785
ok, sounds good.
0
 
LVL 10

Author Comment

by:smegghead
ID: 11044809
Hi,

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

Thanks.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11044823
doh sorry was busy with work and forgot ... ill try to get something out morrow.
0
 
LVL 10

Author Comment

by:smegghead
ID: 11050563
thanks.
0
 
LVL 10

Author Comment

by:smegghead
ID: 11129165
sorry Gregory, I know you're busy.. I'd still be interested to see you code if possible.

Thanks
0
 
LVL 10

Author Comment

by:smegghead
ID: 11198352
...please !!
0
 
LVL 10

Author Comment

by:smegghead
ID: 11440603
have I been forgotton ??? :-(
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11440890
omg im sorry ...  ill have internet hopefully friday after my move ...


I am also writing an article on this ...
0
 
LVL 10

Author Comment

by:smegghead
ID: 11440898
excellent, keep me posted. Who is the article for ??
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11441007
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
 
LVL 10

Author Comment

by:smegghead
ID: 11619521
Hello !!! :-)
0
 
LVL 10

Author Comment

by:smegghead
ID: 11724876
Just some code to point me in the right direction.. I don't need a fully working solution.

Chrs
smg.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11728927
drop me an email I am forgetful :(
0
 
LVL 10

Author Comment

by:smegghead
ID: 12066693
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question