Question

I am learning TDD and using CppUTest in eclipse. Is there any way to debug my code getting a nagging segmentation fault.

Thanks

Was it helpful?

Solution

I don't know anything special in CppUTest or Eclipse to help you, but some generic segfault debugging ideas seem appropriate here:

  • Add flushing print statements (e.g. printf(...) + fflush(stdout) or fprintf(stderr, ...)) to your code and see what gets printed. 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

  • 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. This may catch problems that are root causes of your segfaults but aren't occuring at the exact place where the segfault is generated (e.g. double frees, out of bound array access clobbering pointers used later, etc).

OTHER TIPS

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 -vto your executable's arguments in the Debug dialog. 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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top