Solved

List variables and their values

Posted on 2004-04-28
30
189 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
  • 16
  • 12
30 Comments
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 400 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
 
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 100 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now