Using MDB to figure out mysterious program behavior
Wednesday Jun 23, 2004
In working with Tcl and SWIG, I was trying to write a typemap to convert between a Tcl list and a C global char *foo[1024]. I kept having problems when I would try to append to the list foo, i.e. I'd try
set foo bar
lappend foo bletch
set foo
and instead of getting the list bar bletch as I expected, I'd see something like bletch bletch. Really strange.
I decided to use Solaris' mdb debugger to figure out what was going on by setting watchpoints. First I set a watchpoint on the first part of foo, since I knew I'd be changing that, i.e. I ran
foo:w
This was changing as I expected. Then I examined foo and set a watchpoint at the address at the first word in foo. At this point, I discovered this address being used in malloc() and free(). Ah ha, something was freeing this.
What I narrowed this down to was the fact that I was using the underlying Tcl_Obj string representation as my char *'s, but Tcl didn't know that and was freeing them. I realized I needed to allocate my own memory for copies of the strings and copy them over from the Tcl objects to my memory. Once I did that, things worked. I had scratched my head for a while, but mdb made it really easy to figure out what was going on.










