使用 gdb 调试 C++ STL/Boost 的最佳实践
用gdb调试,任何使用STL/boost的c++代码仍然是一场噩梦.任何在 STL 中使用过 gdb 的人都知道这一点.例如,在此处中查看一些调试会话的示例运行.
Debugging with gdb, any c++ code that uses STL/boost is still a nightmare. Anyone who has used gdb with STL knows this. For example, see sample runs of some debugging sessions in code here.
我试图通过收集提示来减轻痛苦.您能否评论我在下面收集的技巧(特别是您一直在使用的技巧以及您建议对它们进行的任何更改)-我列出的技巧是按技术性递减顺序排列的.
I am trying to reduce the pain by collecting tips. Can you please comment on the tips I have collected below (particularly which ones you have been using and any changes you would recommend on them) - I have listed the tips is decreasing order of technicality.
- 是否有人在使用 "Stanford GDB STL utils" 和 "UCF GDB utils"?是否有一些用于提升数据结构的实用程序?上面的实用程序似乎不能递归使用,例如在一个命令中以清晰的方式打印 boost::shared_ptr 的向量.
- 编写您的 .gdbinit 文件.例如,包括在 UCF GDB 实用程序底部列出的 C++ 相关美化器.
- 使用已检查/调试的 STL/Boost 库,例如 STLport.
- 使用日志记录(例如此处所述)
- Is anyone using "Stanford GDB STL utils" and "UCF GDB utils"? Is there some such utils for boost data structures? The utils above do not seem to be usable recursively, for example for printing vector of a boost::shared_ptr in a legible manner within one command.
- Write your .gdbinit file. Include, for example, C++ related beautifiers, listed at the bottom of UCF GDB utils.
- Use checked/debug STL/Boost library, such as STLport.
- Use logging (for example as described here)
更新:GDB 有一个新的 C++ 分支.
推荐答案
也许不是你要找的那种技巧",但我不得不说,我从 C++ 转向几年后的经验 &STL 到 C++ &提升 &STL 是我现在花在 GDB 上的时间比以前少了很多.我把这归结为以下几点:
Maybe not the sort of "tip" you were looking for, but I have to say that my experience after a few years of moving from C++ & STL to C++ & boost & STL is that I now spend a lot less time in GDB than I used to. I put this down to a number of things:
- 提升智能指针(特别是共享指针",以及需要性能时的指针容器).我不记得上次我必须写一个显式删除(删除是 C++ 恕我直言的转到").GDB 需要花费大量时间来追踪无效和泄漏的指针.
- boost 充满了经过验证的代码,用于您可能会在其他情况下将劣质版本组合在一起.例如
boost::bimap
非常适合 LRU 缓存逻辑的通用模式.还有一堆 GDB 时间. - 采用单元测试.
boost::test
的 AUTO 宏意味着设置测试用例绝对轻而易举(比 CppUnit 更容易).早在它被内置到您必须附加调试器的任何东西之前,它就会捕获很多东西. - 与此相关的是,像
boost::bind
这样的工具可以更轻松地进行测试设计.例如,算法可以更通用,并且与它们所操作的类型的联系更少;这使得将它们插入到测试垫片/代理/模拟对象等中变得更加容易(而且事实上,接触 boost 的模板特性会鼓励你敢于模板化"你以前从未考虑过的东西,产生类似的测试好处). boost::array
.C 数组"性能,在调试版本中进行范围检查.- boost 充满了很棒的代码,你会忍不住从中学习
- boost smart pointers (particularly "shared pointer", and the pointer containers when performance is needed). I can't remember the last time I had to write an explicit delete (delete is the "goto" of C++ IMHO). There goes a lot of GDB time tracking down invalid and leaking pointers.
- boost is full of proven code for things you'd probably hack together an inferior version of otherwise. e.g
boost::bimap
is great for the common pattern of LRU caching logic. There goes another heap of GDB time. - Adopting unittesting.
boost::test
's AUTO macros mean it's an absolute doddle to set up test cases (easier than CppUnit). This catches lots of stuff long before it gets built into anything you'd have to attach a debugger to. - Related to that, tools like
boost::bind
make it easier to design-for-test. e.g algorithms can be more generic and less tied up with the types they operate on; this makes plugging them into test shims/proxies/mock objects etc easier (that and the fact that exposure to boost's template-tasticness will encourage you to "dare to template" things you'd never have considered before, yielding similar testing benefits). boost::array
. "C array" performance, with range checking in debug builds.- boost is full of great code you can't help but learn from
相关文章