Sunday, March 22, 2009

my weekend of debugging

I'm starting to learn C++. I've read 2 or 3 basic-level C++ books (really basic ones) and then decided to write some simple app. People who are more experienced with C++ suggest me to read books like EffC++ or smth like that and I will do it a bit later (as soon as I buy them) but I don't see how it should stop my experiments.

About the app: it's a really basic one, a bit more than 2000 lines on code and about 10-15 classes, but it's the biggest thing I've ever done in C++. The app has a simple configuration file and uses libconfig to parse it. There's a wrapper class for libconfig I wrote to make it more simple. Last week I decided to convert it to singleton pattern and that I've started to see weird behavior: when I was reading the same config property more then once, it started to get corrupted (I was dumping config file after each read to stdout for debug purposes). I decided that I've somehow triggered some hidden libconfig problem so I wrote a simple standalone app to reproduce the problem. To my surprise, my standalone app was working like a charm.

Then I've decided to try to debug this issue with gdb. The only command I've used in gdb before is 'bt' and really it was enough for me to catch problems in my or other people' C programs. So I learned how to set breakpoints and add watches, learned how to run apps in step-through mode and maybe other stuff. It turned out to be quite convenient and simple to use gdb so I'll use it in feature instead of using 'printf-technique' =) But, alas, gdb didn't help. I've stepped through the program, stepped through libconfig but it didn't help, I didn't noticed what's the reason of segfault.

I've remembered that there's an app called valgrind and people say it's extremely useful. As I'm running FreeBSD, I looked for it in the ports collection. But, alas, valgrind port runs only on FreeBSD <8 (and I run -CURRENT) and only on i386 (I have amd64 here). So I had to ask a person using linux to help and he provided me an output quickly. I've read "getting started" guide on valgrind site and figured out what's wrong with my app in 5 minutes (it was a malloc problem, but in totally unrelated class o_O).

So I've spent about 2 days and then fixed the bug in 5 minutes.

The lessons learned are:
1. gdb is a nice and simple tool easy to use and it's better than "printf-technique", so it worth to read some basic guide on gdb
2. valgrid seem to be a powerful tool and it looks like if some non-obvious problem occurs it makes sense to try it's memcheck tool first and then go to gdb or other stuff. It' sad, however, that it doesn't work on FreeBSD/amd64 -CURRENT. I'm not sure how to workaround this issue since I don't want to change os on my desktop. OTOH, I have an i386 laptop, so I'll probably install FreeBSD/i386 7.0 there or maybe some Linux.

No comments:

Post a Comment