如何跟踪 gcc 中的变量更改

发布于 2024-10-18 03:57:21 字数 51 浏览 1 评论 0原文

是否可以跟踪变量何时、何地以及如何变化?我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2024-10-25 03:57:21

如果您想在变量更改时运行的程序中放入一些额外的代码,那么不行——标准 C 没有提供这样做的方法。您必须找到程序中可以尝试更改变量值的所有位置,并在每个位置中放置一些日志记录代码。您可以通过重命名变量然后修复编译中断的点来使其更加可靠,但这只有在您可以使用该变量重新编译所有“客户端”代码时才有效:如果该变量位于包含许多其他人的应用程序的库中使用它,这可能不切实际。


使用 get/set 函数进行访问...

有时最好不要编写直接使用变量的代码,而是提供获取和设置值的函数:然后您可以进行一些额外的检查或者登录这些功能(如果有一天它变得有用)。但是,您不想在任何地方都这样做,否则您的代码将变得冗长且运行速度更慢。


轮询检查变量的更改...

如果偶尔检查一下就足够了,那么您可以编写一些由计时器/警报信号触发的代码,或者在另一个线程中运行的代码,定期检查变量以查看它是否已更改,但这不会帮助您找出它是如何更改的,并且如果值更改但又更改回来,那么您可能会完全错过这些更改。


使用 C++

如果您在 C++ 下编译程序(如果可能的话,对于小型程序甚至可能不需要对代码进行任何修改),那么您也许可以更改相关数据项的类型并编写一个重载的 operator=() 函数,当变量被赋值时将被调用,以及一个 operator 旧类型 () 转换函数,以便使用变量的地方不必修改:

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_;
};

然后更改例如

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

调试器

正如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 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.

又怨 2024-10-25 03:57:21

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文