1

Is it possible to track when where and how does a variable change? I am coding in C using gcc on Linux

Soham
  • 863
  • 2
  • 19
  • 35

2 Answers2

4

If you want to put some extra code inside your program that is run when a variable changes, then no - Standard C doesn't provide a way to do that. You have to find all the places in your program that could try to change the variable value and put some logging code in each and every one. You can make that more reliable by renaming the variable then fixing the points where compilation breaks, but that only works if you can recompile all the "client" code using the variable: if the variable is in a library with lots of other peoples' applications using it, that might not be practical.


Using get/set functions for access...

It's sometimes a good idea not to write code that directly uses a variable, but instead provide functions that get and set the value: then you can put some extra checks or logging inside those functions if it becomes useful some day. But, you don't want to do that everywhere or your code will be verbose and slower to run.


Polling checks for changes to the variable...

If it's good enough to check every now and then, then you can write some code that is triggered by a timer/alarm signal, or that runs in another thread, that checks on the variable periodically to see if it's changed, but that won't help you find out how it changed, and if the value changes but is changed back then you might miss those changes completely.


Using C++

If you compile your program under C++ (if possible, which for small programs may not even require any modifications to your code), then you can perhaps change the type of the data item in question and write an overloaded operator=() function that will be called when the variable is assigned to, and a operator old-type() conversion function so the places where the variable is used won't have to be modified:

template <typename T>
class Intercept
{
    Intercept(const T& t) : t_(t) { }
    T& operator=(const T& t) {
        std::cerr << "change " << t_ << " to " << t << '\n'; t_ = t; 
        return *this;
    }
    operator T&() { return t_; }
    operator const T&() const { return t_; }
    T t_;
};

Then change e.g.

int x;  // from
Intercept<int> x;  // to

Debuggers

As Drakosha says, if this is a debugging issue then you can run your program under the debugger until you resolve the issue.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • I have completely missed the point. The issue I am facing is, though a function is being triggered only once(confirmed via logging messages), yet "one line" in the code is (either replicated somewhere accidentally in the code or I dont know what) is getting accessed. I comment that line out, the behaviour is as expected. I don't, the behaviour is as expected when the function should trigger, but when it should not get triggered, only that assignment is getting executed. I know it sounds funny, almost impossible. But this is making me tear my hairs. – Soham Feb 18 '11 at 09:43
  • @Soham: Are you printing the value immediately before and after the line in question? You might add some extra print statements before and after the function call and in other spots to try to identify when it changes. Can the function be called from multiple threads or an asynchronous signal handler? Are there any pointers to the changed value that might be being used to modify it later? – Tony Delroy Feb 18 '11 at 10:02
  • Thanks for helping me out with some hints. There are no threads. Pointers, yes. :( Certain investigations led me to conclude, calling one function, which has nothing to do with that data is changing it :( (if it didnt happen with me, it would have made a great joke) – Soham Feb 18 '11 at 10:48
  • @Soham: you're welcome... glad you're homing in on it. Maybe a bad pointer in there, or a bad index in an array operation, buffer overrun etc.. Too much of programming is tracking down these little bugs! Cheers. – Tony Delroy Feb 18 '11 at 11:12
  • Tony, will it be possible for you to contact you other than SO, I would like to interact more closely.got a few questions,as well – Soham Feb 18 '11 at 11:46
  • Sure... in the meanwhile you might wish to take a look at [link](http://stackoverflow.com/questions/5043044/serious-memory-clash-variables-clashing-in-c). Seems to be the reason for my entire loss of the day and countless hairs – Soham Feb 18 '11 at 15:24
1

There are memory break points in gdb. I think that's exactly what you are looking for:

Can I set a breakpoint on 'memory access' in GDB?

Community
  • 1
  • 1
Drakosha
  • 11,925
  • 4
  • 39
  • 52
  • It does seem that this is what I want. But not able to achieve it. It doesnt really break when the writing is taking place. It just executes the object file and exits with a code of 72. My watch variable is a member of a structure which is accessed via a pointer. – Soham Feb 18 '11 at 09:21
  • Is there a more visual environment where I can step through and monitor variables at will, when I am running (Something like microsoft's visual studio) in Linux? – Soham Feb 18 '11 at 09:30