简体   繁体   English

在gdb中<值优化>是一个问题吗?

[英]Is <value optimized out> in gdb a problem?

I have an application that only crashes in -O2 optimization (compiled with gcc 4.2.4). 我有一个只在-O2优化中崩溃的应用程序(用gcc 4.2.4编译)。 When I step through the code and get to the spot that crashes and try to inspect the value, I get a "value optimized out" in gdb. 当我单步执行代码并到达崩溃并尝试检查值的位置时,我在gdb中得到“值优化”。

I read on the internet that this means that the value is stored in the register. 我在网上看到这意味着该值存储在寄存器中。 I was wondering if my crash could be related to the fact that some information is placed in registers? 我想知道我的崩溃是否与某些信息放在寄存器中这一事实有关? Is there a way to print what is in the registers to see if it has been corrupted? 有没有办法打印寄存器中的内容以查看它是否已损坏? Is there a way to keep optimizations but not use registers? 有没有办法保持优化但不使用寄存器?

Thanks! 谢谢!

It's 99% likely to be a bug in your code and 1% likely to be a compiler code generation bug. 它有99%可能是您的代码中的错误,1%可能是编译器代码生成错误。 So spend a proportionate amount of time looking for latent bugs in your code but be aware that you just may have found a code generation bug (in which case you'll need to study the compiler generated code carefully to see what the problem is). 因此,花费相当多的时间在代码中查找潜在的错误,但要注意您可能已经发现了代码生成错误(在这种情况下,您需要仔细研究编译器生成的代码以查看问题所在)。

try info registers in gdb. 在gdb中尝试info registers

You can disable optimization with -O0, but there's something fishy and I suggest you to investigate further and eventually post the code. 您可以使用-O0禁用优化,但有一些可疑的东西,我建议您进一步调查并最终发布代码。

如果您可以检测程序流中的错误,您可以自己进行一些打印,如果它与内存泄漏和内存损坏有关,那么valgrind可能是比gdb更好的朋友。

This is not a problem, it is more of an issue with the aggressive optimizations in newer versions of gcc. 这不是问题,在更新版本的gcc中进行积极优化更是一个问题。

See: A Plan to Fix Local Variable Debug Information in GCC . 请参阅: 在GCC中修复本地变量调试信息的计划

The one thing to watch is pointer aliasing, where the compiler can make assumptions that you do not comply with, for example that a pointer you pass into a function does not point to a global variable also used by that function. 要注意的一件事是指针别名,编译器可以在其中进行不符合的假设,例如,传递给函数的指针不指向该函数也使用的全局变量。 But you can control this with compiler options etc. 但您可以使用编译器选项等控制它。

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

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