调试优化的 C/C++ 的有效方法有哪些?程序?
很多时候我使用优化的代码(有时甚至涉及矢量化循环),其中包含错误等。如何调试这样的代码?我正在寻找任何类型的工具或技术。我使用以下(可能已过时)工具,因此我希望升级。
我使用以下命令:
- 由于使用 ddd,您看不到代码,因此我使用 gdb+ dissambler 命令并查看生成的代码;我无法真正使用它单步执行程序。
- ndisasm
谢谢
Many times I work with optimized code (sometimes even involving vectorized loops), which contain bugs and such. How would one debug such code? I'm looking for any kind of tools or techniques. I use the following (possibly outdated) tools, so I'm looking to upgrade.
I use the following:
- Since with ddd, you cannot see the code, I use gdb+ dissambler command and see the produced code; I can't really step through the program using this.
- ndisasm
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
调试优化的程序总是比较困难,但总有办法。一些额外的提示:
It is always harder to debug optimised programs, but there are always ways. Some additional tips:
当调试发布版本时,您可以放入 __asm nops;作为断点的占位符(int 3)。这很好,因为您可以保证断点位置,而不会扰乱编译器优化或编写 printf/cout 语句。
When debugging release builds you can put in __asm nops; as a placeholder for breakpoints (int 3). This is nice as you can guarantee breakpoint locations without messing up compiler optimizations or writing printf/cout statements.
当然,调试非优化版本总是更容易。如果做不到这一点,反汇编代码可能会有所帮助。我使用过的其他技术包括通过强制打印或记录中间结果来部分取消优化代码,或者将关键变量更改为“易失性”,这样我至少可以查看该中的值调试器。
It's always easier to debug a non-optimized version, of course. Failing that, disassembly of the code can be helpful. Other techinques I've used include partially de-optimizing the code by forcing intermediate results to be printed or logged, or changing a critical variable to "volatile" so I can at least look at that value in the debugger.
很可能您所说的优化代码被打乱以减少周期(这使得调试变得困难),但实际上并不是非常优化。 这是我的意思的一个例子。
我会转关闭编译器优化,自己调试和调整它,然后如果代码中的热点实际上位于编译器看到的代码中(而不是在外部库中),则重新打开编译器优化。 (我将热点定义为经常可以找到 PC 的代码的一部分。这会自动排除包含函数调用的循环,因为它们会窃取 PC。)
Chances are what you call optimized code is scrambled to shave cycles (which makes debugging hard) but is not really very optimized. Here is an example of what I mean.
I would turn off the compiler optimization, debug and tune it yourself, and then turn compiler optimization back on if the code has hotspots that are actually in code the compiler sees (not in outside libraries). (I define a hotspot as a part of code where the PC is often found. That automatically exempts loops containing function calls because they steal away the PC.)