Dear all,
I have a big program. A variable change unexpectedly, so I want to find what function change this variable.
How can I track a variable in Linux environment?
Is there any method?
Any help are welcome.
Dear all,
I have a big program. A variable change unexpectedly, so I want to find what function change this variable.
How can I track a variable in Linux environment?
Is there any method?
Any help are welcome.
You can watch it in a debugger, but the bigger problem is that access to the variable isn't strictly controlled (ie. it's probably global). This generally makes tracking changes more difficult, as you've independently discovered.
My best code is written with the delete key.
I mean, I want to set a trigger, a function will run when a variable changes.
Can I do it?
Your best bet is to simply call a function with the new value...
But, when do I should call this function?
I want this function runs when the variable changes.
Can I do it?
In general, no. In a debugger, you can set something like a "watchpoint" (the name and method to set one depend on the debugger) so, when the variable changes, execution is suspended at the instruction that caused the change.
If you want to somehow modify your code so a function will be called if a variable changes then the possibilities are remote (for example, is creating a thread which monitors the value that you know is being changed unexpectedly). The problem is that changes the layout of your program itself in memory (as both the thread function and the method of creating a thread consume some resources). A side effect can therefore be that a different variable starts being changed.
There is a very good reason that people actively discourage hacking of code, and debugging. Yes, it is difficult to design code does not clobber variables it shouldn't. But it is actually harder, particularly in large programs, to find the culprit when some variable is unexpectedly being changed.
Have a look at the original post, Tater. Existing program with a variable being unexpectedly changed. Wanting to somehow call a function when that unexpected change occurs.
Hence Prelude's recommendation (and mine) to use a debugger.
Why not do it in the way the debugger itself does it?
...(AFAIK) ..by running the monitored code as a inferior( child?) process to the monitoring code.
>Why not do it in the way the debugger itself does it?
Why not just use a debugger instead of reinventing the wheel with no good reason?
My best code is written with the delete key.
Fun and educational, possibly.
While there is always the possibility of some breakthrough what leads to a better wheel, it is more likelihood of recreating the square wheel and then expending a lot of effort trying to get it to rotate.
That depends on what you call "better". The implementation of debuggers amounts to one program setting up an environment that allows it to control and monitor the internal execution of another program. You could use the same techniques as an alternative to callbacks, I suppose.
However, I have in mind images of sledgehammers and nuts.