0

I am working with a C++ codebase that does not use exceptions, and the convention is that every function returns false on failure, such that a significant portion of the code looks like this:

bool compute_something(int& result) {
    bool ok = step1();
    ok = ok && step2();
    ok = ok && step3();
    ...
    ok = ok && stepN(result);
    return ok;
}

When debugging an error, I would like to add a breakpoint whenever any variable named "ok" in the program becomes false, in order to find the exact moment the error occured.

I found this question, which is somewhat similar, but does not solve this problem (and it is gdb specific).

Can this be achieved in visual studio? Or any other environment for that matter, the answer might help someone else.

  • 1
    Hard to do. Names are just a convenience for the humans. The computer doesn't use them; everything is just an address or an offset from an address.The extra information from a debug build could help track down all of the `ok`s, but the general case doesn't work. How would pointers and references to `ok` be managed? What would happen with recursion? That could be a lot of `ok`s. – user4581301 May 29 '20 at 15:01
  • I am thinking of this the same as I would about a usual conditional breakpoint. Inside any scope, at any line, you can add a conditional breakpoint if `ok == false` (where ok is a local variable). Although I see what you mean, that by passing a local ok by reference to a function, you would be able to modify it indirectly. Indeed, the scenario I actually care about is exactly the one of usual breakpoints, when only a **local** variable gets modified. – Cosmin Pascaru May 29 '20 at 15:06

1 Answers1

2

Unfortunately, I am pretty sure you can't. There could be no breakpoints on variables by random names. You can place memory access breakpoint on a specific memory address, or you can use a variable name which is in scope in current sessions (which just puts the breakpoint on the address of said variable).

The reason for this is the fact that memory access breakpoints are done in hardware, and hardware has no notion of any names at all.

SergeyA
  • 61,605
  • 5
  • 78
  • 137