G++和__attribute__((优化))不改变调试器行为
G++ and __attribute__((optimize)) not changing debugger behavior
我正在尝试使用__attribute__
,以便使用与代码其余部分不同的标志来编译函数。例如:
#include <iostream>
#include <vector>
void MyNormalFunction();
void MyDebugabbleFunction() __attribute__((optimize(0)));
void MyNormalFunction()
{
std::cout << "Test" << std::endl;
std::vector<int> a;
for(unsigned int i = 0; i < 10; ++i)
{
a.push_back(i);
}
}
void MyDebugabbleFunction()
{
std::cout << "Test" << std::endl;
std::vector<int> a;
for(unsigned int i = 0; i < 10; ++i)
{
a.push_back(i);
}
}
int main()
{
MyNormalFunction();
MyDebugabbleFunction();
return 0;
}
我正在使用-g-O2进行构建,但我希望能够理智地调试MyDebugabbleFunction()
——所以我在其声明中使用了__attribute__((optimize(0)))
。然而,当使用调试器逐步执行这两个函数时,我真的看不出有什么区别。我希望在MyNormalFunction
中尝试逐步执行优化代码时会出现"看似不稳定"的行为,但在MyDebuggableFunction
中会出现标准的"-g"-仅调试器行为。
是我把__attribute__
做错了吗?或者我在这两个函数中使用了糟糕的演示代码(即没有得到"大量优化"的代码)?或者我误解了调试器中的区别?
我使用的是gcc 4.6。
根据GManNickG的建议进行编辑
我用这个代码代替,并建立了-O2-g:
#include <iostream>
#include <vector>
int MyNormalFunction();
int MyDebugabbleFunction() __attribute__((optimize(0)));
int MyNormalFunction()
{
int val = 0; // breakpoint here - debugger does NOT stop here
val = 1;
val = 2;
return val;
} // debugger stops here instead
int MyDebugabbleFunction()
{
int val = 0; // breakpoint here - debugger stops here and steps through the next 3 lines as if it were built with only -g
val = 1;
val = 2;
return val;
}
int main()
{
int a = MyNormalFunction();
std::cout << a << std::endl;
int b = MyDebugabbleFunction();
std::cout << b << std::endl;
return 0;
}
尝试这样的测试:
int MyNormalFunction()
{
int val = 0;
val = 1;
val = 2;
// should optimize to return 2
return val;
}
int MyDebuggableFunction() __attribute__((optimize(0)));
{
int val = 0;
val = 1;
val = 2;
// could optimize to return 2, but attribute blocks that
return val;
}
int main()
{
// we need to actually output the return values,
// or main itself could be optimized to nothing
std::cout << MyNormalFunction() << std::endl;
std::cout << MyDebuggableFunction() << std::endl;
}
这会让它更容易理解。
请注意,当逐步通过时,您应该从main
开始,因为它很可能会减少为:
int main()
{
std::cout << 2 << std::endl;
std::cout << MyDebuggableFunction() << std::endl;
}
如果你愿意的话,看看拆解会让这项任务变得容易得多。
修复代码中的错误后,它编译:
g++ -S x.c
_Z16MyNormalFunctionv:
.LFB1255:
.cfi_startproc
movl $2, %eax
ret
_Z20MyDebuggableFunctionv:
.LFB1256:
.cfi_startproc
movl $0, -4(%rsp)
movl $1, -4(%rsp)
movl $2, -4(%rsp)
movl -4(%rsp), %eax
ret
正如您所看到的,优化属性运行良好。
相关文章:
- 正在VS调试器中监视映射条目
- 为什么我的 VS 代码调试器在我的C++代码周围弹跳?
- 带有 GDB 调试器的 VS 代码内存视图
- 为什么调试器引发"read access violation. this was nullptr"异常?
- VS2017调试器:没有地址,可能是由于编译器优化
- 当前不会命中断点。没有调试器目标代码类型的可执行代码与此文件关联
- 使用 Windows 10 和 MINGW 编译器在 VS Code 上使用 gdb 调试器进行调试时出错
- 如何在可视化代码中生成用于 c++ 开发的调试器?
- 为什么我的程序无法在GDB在线编译器/调试器或Visual Studio C++ 2019中运行
- Popen使Qt调试器崩溃
- 附加调试器并以编程方式获取变量地址 Visual Studio
- 在集成终端上运行vscode lldb调试器时,如何获取程序的输出?
- 宏使枚举调试器感知?
- Eclipse-C++-调试器立即终止
- 标识gdb调试器中符号的源文件名
- Visual Studio图形调试器引发读取访问冲突异常
- 如何修复使用VScode调试器gcc调试时的"找不到文件"错误
- 如果您在C++上有 Makefile 项目,如何在 VScode 中"fix"调试器?
- 为什么调试器总是跳过此程序中的一行?
- G++和__attribute__((优化))不改变调试器行为