Friday, September 6, 2013

Heisenbug, Post-mortem debugging, and a reminder.

Today I discovered post-mortem debugging. I knew programs could generate core dumps and that those dumps contained program execution info. I often wondered why I couldn't find those core dumps when my progs crashed with the "Segmentation fault (core dumped)" message.

Today I also had my first Heisenbug (1). My program segfaulted at runtime but not inside gdb.

This is why I set out to find how to tell bash to let my programs generate core dumps (2, 3)...
ulimit -c unlimited
./myprogram
... and how to use that in gdb
gdb ./myprogram corefile

And so I discovered that my program was crashing in the deepest layers of glibc, after a use of the new operator. After triple-checking my code, I started suspecting glibc! :) But suddenly I realized I was messing with memory allocated by another guy with malloc/calloc and that I was messing with those pointer using new/delete. So, as a reminder : never mix C memory routines with C++ memory operators (4) (or make sure you know where that pointer comes from).

After 6 years of C++ coding... it's never too late!

Of interest:

  1. Heisenbug Wikipedia : http://en.wikipedia.org/wiki/Unusual_software_bug#Heisenbug
  2. SO showing post-mortem debugging : http://stackoverflow.com/questions/4628521/segfault-only-when-not-using-debugger
  3. Article explaining ulimit : http://www.admin-linux.fr/?p=7608 (french)
  4. C vs C++ allocation : http://forums.codeguru.com/showthread.php?367779-malloc-vs-delete-new-vs-free


No comments:

Post a Comment