If the variable is not referenced by any pointers then you already know the places where it is updated, so you can use visibleman's method. There won't be a lot of if-else
s like you anticipated. Just create a function like this
inline void update_dc(int new_dc) // or a macro if you want,
{ // but inline functions are more preferrable
#ifdef DEBUG_DC
if (new_dc == 1) // or if (new_dc != dc)
{
trap();
}
#endif
dc = new_dc;
}
and replace all assignments to dc with this function. You can do that easily with a regex in any text editors' find/replace command, or find/sed in the terminal
However in the general case when you don't know exactly at what point it can be updated, the easiest way is using a separate thread for watching it. You'll do some kind of polling by using a timer, or by checking the value and then sleep for some time to avoid wasting CPU usage. But you won't get the exact place where the change happens, unless you insert a value check after every instruction, which slows down your app many times.
You may also run a debugger like gdb and attach it to your own process to watch. It'll be a lot more flexible this way, but you'll need more memory. You can also try some debugging library if available
See Is is possible to set a gdb watchpoint programatically?
Many architectures do have hardware watch points, so debuggers will try to use them up before turning up to software watching (which is extremely slow). If you know about the architecture you can also set up the debug registers manually like that instead of running a full-fledged debugger. Of course most of the time you'll need to run in privileged mode to set those registers
On x86 there are 3 breakpoints stored in DR0-DR3 which will break on execution, data write, data read/write and IO read/write. I don't know the situation on ARM but it seems to have 6 hardware breakpoints. You can check those on ARM's documentation if you want to go that way.