I put together a gtk-module yesterday to record events from the main loop for post-analysis. I'm sure this has been done before. You can find it at http://git.dronelabs.com/gdkrecord/.
GTK_MODULES=gdkrecord ./myprog
By default the output file is pid-prgname.gdkrecord. You can override this with
GDKRECORD_FILENAME=myoutfile
Inside of the tools directory is gdkrecord-tool. It can dump the information in plain text, xml, json, and csv as well as generate graphs like the one below.
./gdkrecord-tool graph myprog.gdkrecord
Which will generate myprog.gdrecord.png. It only adds information on a few event types right now, but I'll continue adding information extractors as I need them.
-- Christian Hergert 2009-08-15