简体   繁体   English

我如何查看一个值<optimized out> C++ 中的变量?</optimized>

[英]How do I view the value of an <optimized out> variable in C++?

I am using gdb to debug a C++ program.我正在使用 gdb 调试 C++ 程序。

I have this code:我有这段代码:

int x = floor(sqrt(3));

and I want to view the value of x.我想查看 x 的值。 However, gdb claims that x is "< optimized_out >".但是,gdb 声称 x 是“<optimized_out>”。 How do I view the value of x?如何查看x的值? Should I change my compiler flags?我应该更改我的编译器标志吗?

On high optimization levels, the compiler can eliminate intermediate values, as you have seen here.在高优化级别,编译器可以消除中间值,如您在此处所见。 There are a number of options:有多种选择:

  • You can reduce the optimization level to make it easier for the debugger to keep track of things.您可以降低优化级别,以便调试器更轻松地跟踪事物。 -O0 is certain to work (but will be quite a lot slower), -O1 might work okay as well. -O0肯定可以工作(但会慢很多),- -O1也可能工作正常。
  • You can add some explicit print statements to log the output value.您可以添加一些显式打印语句来记录输出值。
  • You can also usually force the compiler to retain this specific value by making it volatile (but remember to un-make it volatile when you're done!).您通常还可以通过将其设置为 volatile 来强制编译器保留此特定值(但请记住在完成后将其取消设置为 volatile!)。 Note, however, that since control flow is also subject to alteration in optimized code, even if you can see the value of the variable, it may not be entirely clear what point in the code you're at when you're looking at the variable in question.但是请注意,由于优化代码中的控制流也会发生变化,即使您可以看到变量的值,当您查看代码时,可能并不完全清楚您所在的代码点有问题的变量。

If you can't or don't want to disable optimization, then you can try declaring the variable as volatile .如果您不能或不想禁用优化,那么您可以尝试将该变量声明为volatile This is usually enough to make your compiler preserve the variable in the final code.这通常足以让您的编译器在最终代码中保留变量。

Alternatively, in recent GCC versions you can disable optimization for just a function, like this:或者,在最近的 GCC 版本中,您可以仅对一个函数禁用优化,如下所示:

void my_function() __attribute__((optimize(0)))
{
  int x = floor(sqrt(3));
}

When using reverse debugging, try to step back closer to the definition point of the variable使用反向调试时,尽量往后退一步,靠近变量的定义点

As shown at: What does <value optimized out> mean in gdb?如: gdb中<值优化输出>是什么意思? it is often the case that within functions:通常情况下,在函数内:

  • at the start of the function, the value of variables can be observed在函数的开始,可以观察到变量的值
  • towards the end of the function however, the variable is more and more likely to become <optimized out> , as it was stored only in a register due to optimizations, and not on memory on the stack.然而,在函数结束时,变量越来越有可能变成<optimized out> ,因为它由于优化而仅存储在寄存器中,而不是存储在堆栈中的内存中。 So when it is not needed anymore, the register is likely to be reused and overwritten by another variable, and then the debug metadata informs GDB of that.因此,当不再需要它时,该寄存器很可能会被另一个变量重用和覆盖,然后调试元数据会通知 GDB。

Therefore, if you are using some kind of reverse debugging such as Mozilla rr , which you will do all the time once you try it once, then one good bet is to try and step back closer to the point of definition/last usage of the variable with reverse-finish + reverse-next and see if you can observe it there.因此,如果您正在使用某种反向调试,例如 Mozilla rr ,一旦您尝试一次,您就会一直这样做,那么一个好的选择是尝试退回到定义点/最后一次使用变量与reverse-finish + reverse-next ,看看你是否可以在那里观察它。

This can be observed concretely with the example code shown at What does <value optimized out> mean in gdb?这可以通过在 gdb什么是<值优化输出> 中显示的示例代码来具体观察 and has saved me a few times, especially when running the unoptimized program makes it take too long to reach the point of interest (which is unsurprising given the terribly inefficient assembly generated by -O0 as seen on that answer).并且已经救了我几次,特别是当运行未优化的程序使得到达兴趣点需要很长时间时(考虑到-O0生成的非常低效的程序集,如该答案所示,这并不奇怪)。

Create your own 'global variable' and print the optimized out variable into this global variable.创建您自己的“全局变量”并将优化的输出变量打印到此全局变量中。 Make sure to remove these globals created by you after you are done with the debugging!完成调试后,请确保删除这些由您创建的全局变量!

With C++ in Visual Studio with the VisualGDB extension, I've seen class-scoped variables that are syntactically correct, but with the runtime variable inspection and hover text claiming the values are optimized out, even though they are actually not.在带有 VisualGDB 扩展的 Visual Studio 中使用 C++,我看到类范围的变量在语法上是正确的,但是通过运行时变量检查和 hover 文本声称值已被优化,即使它们实际上不是。

In order to view the value, prefixing the variable name with the class name in the quick watch or watch window resolved for me.为了查看值,在变量名前面加上快速监视中的 class 名称或观看 window 为我解析。

For example: the myvariable value that appears to be optimized out in myclass can be viewed with myclass::myvariable .例如:在myclass中似乎被优化掉的myvariable值可以用myclass::myvariable查看。

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

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