Usman Ahmad Khan
Usman Ahmad Khan

Reputation: 9

How to watch a variable?

I have a code base of almost 16000 lines of C code in which I am performing different operations (on a Raspberry Pi). After each operation, I am updating the value of dc. dc is initially 0 and if through some error my controller loses its connection with my laptop it becomes 1.

I need to call a function whenever it goes 1. I heard of a function in JavaScript called Object.prototype.watch() and unwatch(). Basically what it does is watches a variable and whenever its value changes, it calls a function. I need to implement a similar function or statement or anything that calls a function when my dc value changes.

I cannot use if-else after each update of dc because it is not a good way of coding and there are going to be a lot of if-else if I use it.

Upvotes: 0

Views: 1921

Answers (4)

user1687327
user1687327

Reputation: 176

The most straightforward solution seems to be to stop changing the variable directly and write a getter and setter function, that will call some callback.

Upvotes: 0

phuclv
phuclv

Reputation: 41753

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

Upvotes: 0

klutt
klutt

Reputation: 31296

The sad answer is: No, there's no reliable way to watch a variable in C this way.

Depending on how the code works there are some workarounds.

One way is to hide (yes I know, it's hard to hide stuff completely in C) the variable. Define functions:

int noConnection() { return _noConnection; }
void lostConnection() { _noConnection = 1; myFunction(); }

Another way is to code some monitor that checks the variable at regular intervals. A drawback is if you really need this function to be run every time it changes, because it will not catch the event that a variable is changed and changed back between checks.

Upvotes: 1

0___________
0___________

Reputation: 67476

Nothing like this exists. Interpreted or managed languages have completely different rules. There is no other way than if.

You can wrap it into some kind of assertion or function but there is no other way than if in that wrapper

Upvotes: 1

Related Questions