Friday, December 09, 2011

Python - the little language that can

This week I discovered that the gdb debugger has a built-in python interpreter.  You can issue python commands from the gdb command line using the "python" or "py" prefix.  In the simplest form

     py print "Hello World"

will print "Hello World" using python from within gdb.  But there is much more.

First it is possible to define commands which can be executed directly by gdb.  In my case I initially defined 2 commands "dv" and "pv" which allow defining a variable based on its address with "dv" and printing the variable using "pv".  Then I moved on to defining a command "wv" which will print a variable after every execution step.

Then I tried ddd.  It automatically included all my startup files and my gdb commands worked immediately in gdb.  Then I started tinkering with the memory dump facility for the ddd data window.  Fortunately it logged all its commands to gdb in the console window.  I saw commands like

    graph display `x/4lg 0x602010`

The  back quotes looked like my old-friend command substitution from shell programming.  So naturally I tried

    graph display `pv stack`

where "pv" is my newly minted command and "stack" is a variable I defined using "dv".  This worked!  A portion of the data window is shown below.


Next it occurred to me that I had read a little about an example command which was written in python for gdb which starts a GTK window.  When I first read about it, this sounded silly.  The command starts a thread and displays a window with a button on it.  Each click of the button sends a command into gdb which causes gdb to print "Hello World".  After the success with ddd, I realised that I can implement a GUI window to add to ddd which will make it far easier to manage my new collection of variables than by typing commands.  So now I have a new goal: improving ddd to better support assembly programming with an additional GUI windows.

The hunt is on!

No comments: