Archive for September, 2010

Tweets for week ending 2010-09-26

  • Openismus day 80: Accompanied Vada to 2nd day of pre-school. Small jhbuild dep fix. Started on gmemory C++ port. Dinner with #ds2011 team. #
  • Openismus day 79: Read half of http://ur1.ca/1rhny. Spent rest of day in #ds2011 kick-off meeting. Tried to fix some gtkmm build issues. #
  • Openismus day 78: Fixed another small bug in cluttermm_tutorial. Set up Merkmal project. Spent rest of time with jhbuild issues. #
  • Openismus day 77: Fixed another example in cluttermm tutorial. http://ur1.ca/1pgel Wrote a blog post. http://ur1.ca/1pge5 Ate PB&J sandwich. #

Debugging reference counting in GNOME C++ bindings with GDB

Overview

For the last couple days I’ve been tracking down some reference counting problems in cluttermm. In the course of this I’ve been working on figuring out how GDB can help me do this. Here is what I’ve come up with. If you have improvements please let me know.

Watching the reference count variable

The first small obstacle is that pretty much all objects are wrapped in a RefPtr, glibmm‘s reference-counting shared smartpointers. This is a good thing and gdb makes accessing the underlying C++ object pretty painless. The following command demonstrates this.

p alpha.pCppObject_

For this example I’m using the alpha variable of type Glib::RefPtr<Clutter::Alpha>. In this command we used the fact that gdb gives you access to any member of a class, whether public or private. You should have gotten output similar to the following.

$1 = (Clutter::Alpha *) 0x9cfe40

Now the interesting part. We want to detect any changes in the ref_count variable. To do this we need to watch the ref_count field. The following command does this.

watch alpha.pCppObject_->gobject_->ref_count

To make this a bit more convenient, I’ve used the gdb define command to create a shortcut. Here is an example session.

(gdb) define watchRefCnt
Type commands for definition of "watchRefCnt".
End with a line saying just "end".
>watch ($arg0).pCppObject_->gobject_->ref_count
>end

Now you just need to use the shortcut like this.

watchRefCnt alpha

Which should give you a confirmation similar to the following.

Hardware watchpoint 2: (alpha).pCppObject_->gobject_->ref_count

The obvious thing to do next is to place this command in a .gdbinit file so that it can be pulled in automatically.

Breaking on last unreference

One of things I was most interested in catching was when the g_object_unref function was called with the condition that the _object pointer was equal to a certian pointer and the ref_count variable == 1. This would indicate that the object is ready to be disposed. To do this we first need to get the address of the underlying GObject like this.

(gdb) p alpha.pCppObject_->gobject_
$3 = (GObject *) 0x9c91e0

You now have a convenience variable, $3, which you can use in the conditions. You then need to set a breakpoint on g_object_unref and set the conditions like this.

b g_object_unref
cond 4 _object==$3&&((GObject*)$3)->ref_count==1

This, of course, would also be nice as a custom command. I’ll leave that as an exercise for the reader. ;)

Future explorations

I forsee future blog entries along these lines so stay tuned. There are quite a few things that interest me in gdb right now.

The Archer project is quickly improving C++ support in gdb. I’ve been using gdb 7.2 for my debugging as I wanted to have a stable debugging platform with all the new C++ enhancements. They have a git repository for those who want the bleeding edge.

Python scripting has been available in gdb since 7.0. I plan on looking into this as a way of streamlining debugging tasks and honing my Python skills which are pretty rusty.

Please let me know if you see any areas for improvement.

Tweets for week ending 2010-09-19

  • Openismus day 76: murrayc showed me the proper way to fix cluttermm bug. Committed fix for that and a few other issues. http://ur1.ca/1o8sp #
  • Openismus day 75: Found the problem with cluttermm. Clutter::Alpha::get_timeline() was the culprit. Came up with a fix that worked. #
  • Openismus day 74: Went thru clutter code to see what tutorial program is doing. emacs+gdb+gtags (GNU Global) is nice for this. More gdb too. #
  • Openismus day 73: Took a bit of a detour to go deeper into gdb. Think I've pretty much gone though the entire manual. Back to cluttermm bug. #
  • Openismus day 72: Struggling with the bug in cluttermm. Maybe I should first understand what the clutter c code is doing. ;) #
  • Openismus day 71: Learned to use gdb to debug reference counting issues in GObject-based code. http://ur1.ca/1lrpi & comments were helpful. #

Tweets for week ending 2010-09-12

  • Openismus day 70: Looking into ref count issues in cluttermm. Grokking refs in *mm/glib. Doc patch committed in clutter. http://ur1.ca/1kpji #
  • Openismus day 69: Working on getting effects example in cluttermm_tutorial working. Found a few other examples are broken. Looking on it. #
  • Openismus day 68: Got cluttermm compiling & commited changes. http://ur1.ca/1jr3k Then fixed dumb std::vector issue. http://ur1.ca/1jr3x :-# #
  • Openismus day 67: Trudged through compile issues while removing *Handle containers in cluttermm. Started using emacs yasnippet module. #
  • Openismus day 66: Learnt enough Perl to read some of the *mm binding generation code. Workd on removing *Handle usage in cluttermm. #

Tweets for week ending 2010-09-05

  • Openismus day 65: With murrayc's help, found fix for cluttermm_tutorial segfault. Requires going deeper into *mm bindings. Will commit soon. #
  • Openismus day 64: tbf helped try to find cause of segfault in cluttermm_tutorial. Still couldn't fix it. :( Learnt some advanced gdb stuff. #
  • Openismus day 63: Think I fixed yesterday's bug. cluttermm-tutorial still segfaults a few lines later, though. Got GNOME git commit access! #
  • Openismus day 62: Looked through lots of *mm libraries to get an idea of how things are really done. Tracked down another bug in cluttermm. #
  • Openismus day 61: Finished up *mm wrapping tutorial, fixed small bug in cluttermm that broke cluttermm turorial. http://www.ur1.ca/1ecom #