简体   繁体   English

CPPUTest调试 - C ++

[英]CPPUTest debugging - C++

I am learning TDD and using CppUTest in eclipse. 我正在学习TDD并在eclipse中使用CppUTest。 Is there any way to debug my code getting a nagging segmentation fault. 有没有办法调试我的代码得到一个唠叨的分段错误。

Thanks 谢谢

I don't know anything special in CppUTest or Eclipse to help you, but some generic segfault debugging ideas seem appropriate here: 我不知道CppUTest或Eclipse有什么特别的东西可以帮助你,但是一些通用的段错误调试思路似乎合适:

  • Add flushing print statements (eg printf(...) + fflush(stdout) or fprintf(stderr, ...)) to your code and see what gets printed. 刷新打印语句(例如printf(...)+ fflush(stdout)或fprintf(stderr,...))添加到您的代码中,看看打印的内容。 Do this in a binary search fashion with just a few prints at a time until you narrow down exactly where it is crashing. 以二进制搜索方式执行此操作,一次只打印几次,直到您精确缩小崩溃位置。 This sounds old fashioned but is extremely effective. 这听起来很古老但非常有效。 Here is a guide I found googling that talks about this well-known technique: http://www.floccinaucinihilipilification.net/blog/2011/3/24/debugging-via-binary-search.html 这是一个我发现谷歌搜索的指南,谈论这个众所周知的技术: http//www.floccinaucinihilipilification.net/blog/2011/3/24/debugging-via-binary-search.html

  • Compile your code with debugging symbols and run it in a debugger. 使用调试符号编译代码并在调试器中运行它。 When you hit your segfault, ask for a backtrace and see if you can figure out what happened. 当你遇到段错误时,请求回溯,看看你是否能弄明白发生了什么。 When doing this it can be especially helpful to use a graphical debugger . 执行此操作时,使用图形调试器尤其有用。

  • Run your code with a debugging tool like a debug malloc library or something from the valgrind suite. 使用调试工具(如调试malloc库)valgrind套件中的某些内容运行代码。 This may catch problems that are root causes of your segfaults but aren't occuring at the exact place where the segfault is generated (eg double frees, out of bound array access clobbering pointers used later, etc). 这可能会捕获导致segfaults根本原因的问题,但不会发生在生成segfault的确切位置(例如,双重释放,以后使用的绑定数组访问blobbering指针等)。

It would be helpful if you could add some code to your question, to give us a better idea of what you are up against. 如果您可以在问题中添加一些代码,让我们更好地了解您的反对意见,将会很有帮助。 Not knowing any of the details, I would suggest the following: 不知道任何细节,我建议如下:

  1. Add -v to your executable's arguments in the Debug dialog. 在调试对话框中将-v添加到可执行文件的参数中。 This will print the names of your test cases as they are executed. 这将在执行时打印测试用例的名称。 The last name that prints is likely the test where the segmentation fault occurs. 打印的姓氏可能是发生分段错误的测试。
  2. Put a breakpoint in that test case, where you call your code under test 在该测试用例中放置一个断点,在此测试中调用您的代码
  3. Step into your code until the segfault occurs. 进入代码,直到发生段错误。
  4. Trace back the value that caused the segfault (most often, a dangling pointer) and find out, why it was NULL or uninitialized. 追溯导致段错误的值(最常见的是悬空指针)并找出为什么它是NULL或未初始化。

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

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