Wednesday, March 08, 2006

Automate your debugging using Expect

That's right; isn't it cool to see automate and debugging in the same sentence? :)

I think I read this, may be an year ago, in comp.lang.tcl. The poster was trying to track down a memory leak in a C application. He is a tcl/expect guy and wanted to run gdb under expect. His idea is really cool. Basically he wants to connect to gdb thru' expect. Then issue commands to set breakpoints at malloc and free. Every time gdb hits the break point, expect gets control. It issues a bt. Grabs the output and stores in, say a tcl array.

It may collect other data like caller, arguments(like size of allocation) and any other information that can be seen as relevant(say from info arg/frame). After a sufficiently long run, you have this complete history of who did alloc/free when/where. That can be analyzed to lead to the source of the leak. The beauty of the whole process is that, the original C source is never changed. No need to add a printf or any such thing.

I found this idea of automating a gdb debug session very powerful. I haven't yet had an opportunity to try it out; but I believe its power is awesome. That's a great way of throwing a machine's power at solving its own complexity.

There should be various other applications; the real power comes in that, you don't have to change anything in the C program. You stop it, at will with gdb and collect data to store it in your tcl data structures. It does involve a decent amount of initial work in getting the expect script work; but I think for a really tricky problem, this method should come in handy.

1 Comments:

At 10:18 AM, Blogger debedb said...

http://debedb.blogspot.com/2007/02/poor-mans-tracepoints-and-call-sequence.html

 

Post a Comment

<< Home