如何跟踪 gcc 中的变量更改
是否可以跟踪变量何时、何地以及如何变化?我在 Linux 上使用 gcc 进行 C 编码
Is it possible to track when where and how does a variable change? I am coding in C using gcc on Linux
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在变量更改时运行的程序中放入一些额外的代码,那么不行——标准 C 没有提供这样做的方法。您必须找到程序中可以尝试更改变量值的所有位置,并在每个位置中放置一些日志记录代码。您可以通过重命名变量然后修复编译中断的点来使其更加可靠,但这只有在您可以使用该变量重新编译所有“客户端”代码时才有效:如果该变量位于包含许多其他人的应用程序的库中使用它,这可能不切实际。
使用 get/set 函数进行访问...
有时最好不要编写直接使用变量的代码,而是提供获取和设置值的函数:然后您可以进行一些额外的检查或者登录这些功能(如果有一天它变得有用)。但是,您不想在任何地方都这样做,否则您的代码将变得冗长且运行速度更慢。
轮询检查变量的更改...
如果偶尔检查一下就足够了,那么您可以编写一些由计时器/警报信号触发的代码,或者在另一个线程中运行的代码,定期检查变量以查看它是否已更改,但这不会帮助您找出它是如何更改的,并且如果值更改但又更改回来,那么您可能会完全错过这些更改。
使用 C++
如果您在 C++ 下编译程序(如果可能的话,对于小型程序甚至可能不需要对代码进行任何修改),那么您也许可以更改相关数据项的类型并编写一个重载的
operator=()
函数,当变量被赋值时将被调用,以及一个operator
旧类型()
转换函数,以便使用变量的地方不必修改:然后更改例如
调试器
正如Drakosha所说,如果这是一个调试问题,那么您可以在调试器下运行程序,直到解决问题。
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 aoperator
old-type()
conversion function so the places where the variable is used won't have to be modified:Then change e.g.
Debuggers
As Drakosha says, if this is a debugging issue then you can run your program under the debugger until you resolve the issue.
gdb中有内存断点。我认为这正是您正在寻找的:
我可以吗在 GDB 中的“内存访问”上设置断点?
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?