简体   繁体   English

为什么我的调试器有时会发生故障并做一些不符合我的代码的事情?

[英]Why does my debugger sometimes freak out and do things like not line up with my code?

When I'm using my debugger (in my particular case, it was QT Creator together with GDB that inspired this) on my C++ code, sometimes even after calling make clean followed by make the debugger seems to freak out. 当我在我的C ++代码中使用我的调试器(在我的特定情况下,它是QT Creator以及GDB启发了这一点)时,有时甚至在调用make clean然后make调试器看起来吓坏了。

Sometimes it will seem to be lined up with another piece of code's line numbers, and will jump around. 有时它似乎与另一段代码的行号对齐,并会跳转。 Sometimes this is is off by one line, sometimes this is totally off and it'll jump around erratically. 有时这是一条线,有时这完全是关闭的,它会不规律地跳转。

Other times, it'll freak out by stepping into things I didn't ask it to step into, like while stepping over a function call, it might step into the string initialization routine that is part of it. 其他时候,它会因为踩到我没有要求它进入的事情而变得惊慌失措,就像踩过函数调用一样,它可能会进入作为其一部分的字符串初始化例程。

When I get seg faults, sometimes it's able to tell me where it happened perfectly, and other times it's not even able to display question marks for which functions called the code and from where, and all I see is assembly, even while running the exact same code repeatedly. 当我遇到seg故障时,有时它能够告诉我它在哪里完美发生,有时它甚至不能显示哪些函数称为代码的问号,从哪里开始,我所看到的就是汇编,即使在运行时也是如此重复相同的代码。

I can't seem to figure out a pattern to what causes these failures, and sometimes my debugger is perfectly well behaved. 我似乎无法找出导致这些失败的模式,有时我的调试器表现得非常好。

What are the theoretical reasons behind these debugger freak outs, and what are the concrete steps I can take to prevent them? 这些调试器背后的理论原因是什么,以及我可以采取哪些具体步骤来防止它们?

There's 3 very common reasons 有三个非常常见的原因

  • You're debugging optimized code. 您正在调试优化代码。 This rarely works - optimized code can be reordered/inlined/precomputed/etc. 这很少有效 - 优化的代码可以重新排序/内联/预先计算/等。 to the point there's no chance whatsoever to map it back to the source code. 到目前为止,没有任何机会将其映射回源代码。

  • You're not debugging, for whatever reason, the binary matching the current source code. 无论出于何种原因,您都没有调试与当前源代码匹配的二进制文件。

  • You've invoked undefined behavior somewhere - if whatever stuff your code did, it has messed around with the scaffolding the debugger needs to keep its sanity. 你已经在某处调用了未定义的行为 - 如果你的代码做了什么,它已经搞乱了调试器需要保持其理智的脚手架。 This is what usually happens when you get a segfault and you can't get a sane stack trace, you've overwritten/messed with the information(eg stack pointers) the debugger needs to do its job. 当你得到一个段错误并且你无法获得理智的堆栈跟踪时,通常会发生这种情况,你已经覆盖/搞乱了调试器需要完成其工作的信息(例如堆栈指针)。

And probably hundreds more - of the stuff I personally encounter is: debugging multithreaded code; 可能还有数百个 - 我个人遇到的问题是:调试多线程代码; depending on gcc/gdb versions and various other things - there's been quite a handful debugger bugs. 取决于gcc / gdb版本和各种其他东西 - 调试器错误很少。

One possible reason is that debuggers are as buggy as any other program! 一个可能的原因是调试器和其他程序一样有问题!

But the most common reason for a debugger not showing the right source location is that the compiler optimized the code in some way, so there is no simple correspondence between the source code and the executable code. 但是调试器没有显示正确的源位置的最常见原因是编译器以某种方式优化了代码,因此源代码和可执行代码之间没有简单的对应关系。 A common optimization that confuses debuggers is inlining, and C++ is very prone to it. 混淆调试器的常见优化是内联,而C ++非常容易使用它。

For example, your string initialization routine was probably inlined into the function call, so as far as the debugger was concerned, there was just one function that happened to start with some string initialization code. 例如,您的字符串初始化例程可能已内联到函数调用中,因此就调试器而言,只有一个函数恰好以某些字符串初始化代码开头。

If you're tracking down an algorithm bug (as opposed to a coding bug that produces undefined behavior, or a concurrency bug), turning the optimization level down will help you track the bug, because the debugger will have a simpler view of the code. 如果您正在追踪算法错误(与产生未定义行为的编码错误或并发错误相反),将优化级别降低将帮助您跟踪错误,因为调试器将具有更简单的代码视图。

I have the same question like yours, and I cannot solve it yet. 我和你一样有同样的问题,但我还是无法解决。 But I have came out one problem solution which is to install a virtual machine and install Unix system in it. 但我找到了一个问题解决方案,即安装虚拟机并在其中安装Unix系统。 And debug it in Linux system. 并在Linux系统中调试它。 Perhaps it will work. 也许它会起作用。

I have found out the reason, you should rebuild the project every time you changed your code, or the Qt will just run the old version of the code. 我发现原因,你应该在每次更改代码时重建项目,或者Qt只运行旧版本的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM