0

I have a large C# application, with multiple projects in Visual Studio 2013. I want to track the value of a particular variable, say varX, and find the exact statement (in one of the files) that is changing the value of varX.

Therefore, I want the debugger to step through the application, and stop exactly at the statement which changes the value of varX.

Can anyone tell me what debugging options/features can help me do the aforementioned?

Edit: By the way, the definition of the variable varX is not available in code. It gets it's definition from a DLL file.

SauravBhattacharya
  • 631
  • 3
  • 9
  • 24
  • 1
    I'm sure there is something in Rosalyn diagnostics that can do that. Also, if you have THAT many places that change the same local variable, you have a code maintainability problem. The quick and dirty solution is to find all usages of that instance and place a breakpoint at each usage. – David L Jan 27 '15 at 15:01
  • 1
    If its a property that is being changed cant you put a breakpoint in the setter? otherwise you can use conditional breakpoints – WraithNath Jan 27 '15 at 15:01
  • @WraithNath No, I can't set a breakpoint at the setter (there is no statement). Also, how can a conditional breakpoint help? Please explain. – SauravBhattacharya Jan 27 '15 at 15:05
  • You can put a normal breakpoint on everywhere the member variable is set (as david suggested), then right click it and you will be given an option for various conditions eg break if its a certain value. Normally with these kind of things it might be easier to change the member variable to private and add a property (depending on whether you think its being set from outside the current class) then log a warning message with environment.stacktrace to find out where its being called from. – WraithNath Jan 27 '15 at 15:12
  • You can use Windbg and use the Break On Access feature. E.g. ba w4 dddd will break everytime the memory address at dddd does change. Since it is a managed class you can use !DumpHeap -stat to find the MT of the class you are after and the use !DumpHeap -mt ddddd to dump all addresses of that type. Then you have the base address and the offset which you need to add up to use ba. Since the GC can move things around you could need a few tries but in principle it will work. – Alois Kraus Mar 01 '15 at 08:01

1 Answers1

0

With C++ (native) there is such a thing as a "memory changed" breakpoint, but not with C#.

And, I presume this does not answer your question: Can I set a breakpoint when variable is getting a specific value in .NET?

So, what I would do is that I would rename (NOT refactor) varX to _varX, and I would add the following:

int varX
{ 
    get{ return _varX; } 
    set{ _varX = value; }  //place breakpoint here!
}

Then, a normal execution breakpoint on the setter should do what you want to accomplish.

Community
  • 1
  • 1
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142