Debugging a Tcl extension written in C using gdb

TR Debugging an extension written in pure Tcl is easy because Tcl has so nice introspection capabilities built in. But if you write an extension for Tcl using C, you can use gdb, the GNU debugger, to help you debugging this extension. First, you need to compile your extension with the '-g' flag to build debugging symbols. Then, you just call tclsh with gdb:

 > gdb tclsh

The Tcl script that has to be run to test/debug the extension can then be called:

 (gdb) run myTclScript.tcl

If you need breakpoints before the script is running, you can just define them in advance. This is even possible before the Tcl C extension has been loaded by the Tcl script:

 (gdb) break My_C_Procedure
 Function "My_C_Procedure" not defined.
 Make breakpoint pending on future shared library load? (y or [n]) y
 Breakpoint 1 (My_C_Procedure) pending.
 (gdb) run myTclScript.tcl

If you have some arguments like 'Tcl_Obj *const objv' you need to look into, you can do the following:

 (gdb) x/s  objv[2]
 0x1809c08:       "\001"
 (gdb) call (char*) Tcl_GetStringFromObj(objv[2])
 $6 = 0x315400 "foo"

See also: