I'm debugging a reference leak in a GObject-based application. GObject has a simple built-in mechanism to help with such matters: you can set the g_trap_object_ref
variable in gobject.c to the object that you care about, and then every ref or unref of that object will hit a breakpoint instruction (via G_BREAKPOINT()
).
So sure enough, the program does get stopped, with gdb reporting:
Program received signal SIGTRAP, Trace/breakpoint trap.
g_object_ref (_object=0x65f090) at gobject.c:2606
2606 old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
(gdb) _
which is a great start. Now, normally I'd script some commands to be run at a breakpoint I manually set using commands 3
(for breakpoint 3, say). But the equivalent for SIGTRAP
, namely handle SIGTRAP
, doesn't give me the option of doing anything particularly interesting. Is there a good way to do this?
(I'm aware that there are other ways to debug reference leaks, such as setting watchpoints on the object's ref_count
field, refdbg, scripting regular breakpoints on g_object_ref()
and g_object_unref()
. I'm about to go try of those now. I'm looking specifically for a way to script a response to SIGTRAP
. It might come in useful in other situations, too, and I'd be surprised if gdb doesn't support this.)