4

This is not about setting a single breakpoint, conditional or not.

I admit this is a bad situation but I have a large-ish, undocumented codebase in front of me. Somewhere in this code an int is, after pressing a button, set to a specific value (lets say 200).

I thought I had found the occurences where this could happen but at both those points the variables are set to the correct value (< 200).

Now, instead of spending another four hours traversing call stacks, is there a way of pausing/breaking the debugger, in the sense of how a breakpoint pauses the deugger, the moment anywhere in the program an int is set to a specific (200) value?

A different way of phrasing this would be: Can I set a conditional breakpoint on/for native types?

Example:

public class Program
{
    public static void Main()
    {
        int a = 0;
        int b = 0;
        int c = 0;

        while(a < 5)
        {
            a++;
            b++;
            c = a+b;
        }
    }
}

Lets say anytime this algorithm assigns the value '4' to any of the three ints, I would like for the debugger to pause/break.

Steffen Winkler
  • 2,805
  • 2
  • 35
  • 58
  • Could you change the int value to a property and simply breakpoint inside the setter? – Paul Michaels Sep 13 '18 at 08:57
  • @pm_2 sorry, I guess I'm still not clear enough. It is not about one `int` variable. I would like to break/pause the moment any `int` in the entire program is assigned a specific value – Steffen Winkler Sep 13 '18 at 08:59
  • 2
    This would be a tough nut to crack for any debugger in any language. You'd need to turn off any and all optimizations, treat all assignments as though they're `volatile` and issue checks after all such assignments... symbolic debugging would probably be faster than that. (The short answer is no.) – Jeroen Mostert Sep 13 '18 at 09:01
  • 2
    In Visual Studio, You can right click on the breakpoint and set a specific condition for any variable reachable. – Doğa Gençer Sep 13 '18 at 09:01
  • Not so much in the debugger, but if you haven't done, have a look at IL weavers: https://github.com/Fody/Fody. It might end up being more work that to trace through the code, though. – Paul Michaels Sep 13 '18 at 09:04
  • @DoğaGençer please see my edit. Your comment wouldn't work. Well, unless you set a conditional breakpoint for each of the assignments. Which is possible for a large-ish codebase if you have ReSharper or a similiar tool admittedly. – Steffen Winkler Sep 13 '18 at 09:05
  • I don't think you have the ability in the debugger to do this...you might be best running a performance profiller on it - it'll give you snapshots of the app as it runs at particular moments in time. Might be limited to Enterprise license but what you're looking for is beyond the debugger as is – DiskJunky Sep 13 '18 at 09:06
  • 1
    @DiskJunky I thought as much but figured better safe than sorry ;) – Steffen Winkler Sep 13 '18 at 09:07
  • I'm pretty sure you can tweak the profiler to take snapshots at particular events, like button clicks. It's worth tinkering with – DiskJunky Sep 13 '18 at 09:10
  • @SteffenWinkler I understood your situtation better now. I don't know if there's way to listen to the variable and interfere the debugger, sorry. – Doğa Gençer Sep 13 '18 at 09:10
  • 1
    This is the sort of low-level thing that practically requires hooking the IL generation to do it with any efficiency. It could be done from a profiler (analyze all instructions that assign values and plug in a check after the assignment), but writing one of those is not a project you undertake on a whim. You're probably best off isolating the code path that sets the value to the best of your ability, then single-stepping through that if you need to. Tedious, but I don't know of any automatable managed debuggers out of the box. WinDbg is automatable, but its managed features are too limited. – Jeroen Mostert Sep 13 '18 at 09:13
  • I'd imagine it shouldn't take 4 hours of call stacks, you know what button triggers it, and presumably you have some idea of which variables might be the culprits so just breakpoint on the button press and breakpoint on the setters of those culprits – Sayse Sep 13 '18 at 09:16
  • Possible duplicate of [How to set conditional breakpoints in Visual Studio?](https://stackoverflow.com/questions/6670415/how-to-set-conditional-breakpoints-in-visual-studio) – Peter B Sep 13 '18 at 09:16
  • @PeterB nope, conditional breakpoint wouldn't cover this. Unless you can set one on a primitive type itself. – Steffen Winkler Sep 13 '18 at 09:18
  • @Sayse slight hyperbole, I admit. – Steffen Winkler Sep 13 '18 at 09:19
  • @Steffen Winkler, Data breakpoint supports the native C++: https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2017, but not very sure that whether it is the way you want to get. – Jack Zhai Sep 24 '18 at 03:02
  • even if this were about C++, it would still fall short - since in my case I wouldn't know a specific memory address or variable. – Steffen Winkler Sep 24 '18 at 13:10

0 Answers0