Solved

List variables and their values

Posted on 2004-04-28
30
201 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 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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
 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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