Posts Tagged ‘python’

Ok, so this time I won’t talk about Python scripting support in GDB. I’m getting tired of it myself. 🙂 I’ll just comment that it’s amazing the number of problems people in IRC report with GDB that can be solved with the Python support that we’re adding to it. Sometimes they need stuff which is only on the branch, but sometimes even the few bits already in CVS HEAD are enough!

Er, I actually do have one more thing to say about the subject: GDB had one project accepted in the Google Summer of Code 2009. Oguz Kayral is the student working on it, and I am his mentor. He will add support for subscribing to inferior events (e.g., signals, process and thread stops, thread creation) from Python. One use case for which this is useful was given by an IRC user at the #gdb channel:

<LimCore> how to run gdb from command line, so that it will run ./foo.bin with arguments: foo bar baz and it will run it instantly without waiting for ‘r’; And if program segfaults then it will do ‘bt’ without waiting for the command. (and if program terminates normally then it will also just quit)

LimCore will be able to write a simple and short Python script using the events API to solve his problem.

Now, moving on to other items: my team has been asked to improve GDB support for the hardware debug facilities in embedded PowerPC processors (for more info about these facilities, see Chapter 10 of Book III-E of the Power ISA v2.06). I announced this work to the GDB mailing list back in early March, and got useful insight from Joel Brobecker.

Today I posted an update on where we are with this work. We have the following ready for both native GDB and gdbserver on Linux:

  • one additional hardware watchpoint (two in total),
  • four hardware breakpoints,
  • one ranged hardware watchpoint.

And we still have the following features ahead of us:

  • support for the two DVC (Data Value Compare) registers, which enable hardware-accelerated conditions for hardware watchpoints,
  • two ranged hardware breakpoints.

Last and least, I was thinking of posting monthly GDB updates on what happened in GDB in the previous month as I did back in February, but I got busy and didn’t get around to it. I still entertain the idea though, so if you think it’s worth it, I’d be glad to know.


Read Full Post »

It’s been a while since I last talked about Python scripting support in GDB. Mostly because I’ve been focusing on getting stuff from the branch merged into CVS HEAD, so that GDB 7.0 can have some useful Python bindings.

The latest two patches committed upstream are for creating convenience functions in Python, and fo manipulating a program’s stack frames as Python objects. So, what can you do with those? I’ll borrow an example from Tom Tromey here: suppose you want to set a breakpoint which triggers only when the code is called by one specific function. You can create a convenience function like this:

import gdb
import re

class CallerIs (gdb.Function):
    """Return True if the calling function's name is equal to a string.
This function takes one or two arguments.
The first argument is the name of a function; if the calling function's
name is equal to this argument, this function returns True.
The optional second argument tells this function how many stack frames
to traverse to find the calling function.  The default is 1."""

    def __init__ (self):
        super (CallerIs, self).__init__ ("caller_is")

    def invoke (self, name, nframes = 1):
        frame = gdb.selected_frame ()
        while nframes > 0:
            frame = frame.older ()
            nframes = nframes - 1
        return frame.name () == name.string ()

CallerIs ()

And then create a conditional breakpoint using it, as in:

(gdb) break foo.c:42 if $caller_is ("some_function")

Or, to check the name of the grand-grand-caller of the code:

(gdb) break foo.c:42 if $caller_is ("some_function", 3)

Cool, huh? Now, why bother writing that big doc comment? GDB will use it as online help for the function you wrote:

(gdb) help function caller_is
Return True if the calling function's name is equal to a string.
This function takes one or two arguments.
The first argument is the name of a function; if the calling function's
name is equal to this argument, this function returns True.
The optional second argument tells this function how many stack frames
to traverse to find the calling function.  The default is 1.

One other patch which was merged in HEAD enables creating new GDB commands in Python, so you can write new commands to do some frame tricks too!

If you’d like to know more about the Python scripting work in GDB, I suggest you read the series of blog posts from Tom Tromey on this subject. It’ll give you a pretty good idea of what we have in the Python branch, and the direction we’re heading. Just keep in mind that some method names and syntax changed since he wrote that. Refer to the GDB manual in the Python branch (“make gdb.pdf” in gdb/doc/) or the example scripts (also in the Python branch, in gdb/python/lib/gdb/) for up-to-date details.

Read Full Post »

One annoying aspect of scripting GDB (using it’s own scripting language), is that if you test or set a variable which is not in scope, the script will generate an error and GDB will give up evaluating it. It would be useful to have a way to programatically check if the variable is in scope before trying to use it. There was even at least one try (from the brave Rob Quill) at implementing such a thing posted to the gdb-patches mailing list, but unfortunately as of now it still has some issues and haven’t been committed (I believe this patch is the latest version).

Anyway, this is the kind of thing we want to make easy to do in Python, so I decided to try to implement a solution using what we have in the Python branch. Tom Tromey already created the notion of convenience functions, which are functions that are implemented by GDB  that you can call in places where it accepts expressions, and he made it possible to implement such functions in Python. It follows that one can write a in_scope convenience function in Python which checks if a given variable name is valid in the current scope of the debugged program.

There were some missing pieces which I had to implement to get the in_scope function working (most notably, a method to get a string from a variable in the debugged program), and in the past few days I committed to the branch the patches which add those pieces. So now you can have the following script:

class InScope (gdb.Function):
    """Check if all the given variables or macros are in scope.
       Receives as argument a list of names separated by

    def __init__ (self):
        super (InScope, self).__init__ ("in_scope")

    def invoke (self, var):
        vars = set (var.string().split())
        found = set ()
        pc = gdb.get_selected_frame ().get_pc ()
        block = gdb.get_block_for_pc (pc)
        while block:
            for sym in block:
                if (sym.is_argument ()
                      or sym.is_constant ()
                      or sym.is_function ()
                      or sym.is_variable ()):
                    sym_name = sym.get_print_name ()
                    if sym_name in vars:
                        found.add (sym_name)
            block = block.get_superblock ()

        return vars == found

InScope ()

And use it like this:

Breakpoint 1, main (argc=1, argv=0x7fffffffe208) at /tmp/funcs.c:16
16        int i = 41;
(gdb) if $in_scope("i")
 >print "yay"
 >print "nay"
$1 = "yay"

If you save the script to ~/.gdb-in-scope.py, you can put the following in your ~/.gdbinit to load it automatically:

source ~/.gdb-in-scope.py

Now my goal is to cut out and submit upstream a set of patches to make the above work on regular GDB. The only problem is that I have the boring task of writing testcases and documentation for the code before I can submit it… 🙂

For now, If you want to use this script you can build a GDB from the branch, but be aware that the Python API as provided in the branch is subject to change.

Read Full Post »

It’s been a great while since I last posted about Python scripting in GDB, mostly because I’ve been busy coding the feature and getting it ready for upstream.

First of all, I’d like to take the opportunity to encourage people interested in using this feature to experiment with what we have implemented so far. The reason is that if you still can’t do what you want with the current code in the Python branch, we’d love to hear what you miss and implement it. We are working on what is useful for ourselves, and trying to decide what other people would find useful. But it’s not possible to imagine everything that people want to use this for, or even most things. Please refer to this wiki page to learn what currently works, what we plan to implement, and how to grab the code from the Python branch.

Feel free to write to the GDB mailing list or show up in the #gdb IRC channel at Freenode to discuss this work and/or bring your use case to our attention, so that we can support it. I hope that with enough input from prospective users we can ship something that’s immediately useful for most people, and avoid having to jump through hoops later and have to shoehorn something that we forgot to cater for initially, risking breaking scripts out there or ending up with an inconsistent API.

Anyway, back to business: I have just committed the second patch in the Python series! It exports GDB’s value subsystem to Python scripts. Basically, GDB values are objects which represent data in the inferior (GDB jargon for the program being debugged), holding its address in the inferior’s addressspace, its type and so on. See the “Python API” section in the GDB manual if you want to learn more about it (yes, we are even writing documentation for the feature!).

I committed the first patch back in August, but I didn’t mention it here because it didn’t do anything the user would find useful, really. It was just groundwork for the rest (autoconf and Makefile.in changes, a ‘python’ command in GDB which basically does nothing useful, initial documentation…). Still, it was about 1500 lines long (not counting the patch’s context)! This shows how much work it is to integrate Python support in GDB. I almost regret having joined this effort. 🙂

The second patch also doesn’t allow the user to do anything useful yet, unfortunately. But it is noteworthy because it is a base upon which a lot of other Python support code depend upon. Also, it’s the first committed patch which actually exposes something from GDB to Python. It took a while to get this code ready for two reasons: one was that there was a long discussion regarding how the syntax of acessing struct/union/class elements. The other was that implementing the Value class involved playing with little-documented aspects of Python’s C interface, and it took me time to discover how to do what I needed.

Now my next step is to choose the next patch from the Python series to submit upstream, and get it ready for posting (i.e., fix FIXMEs, add testcases and documentation). This brings me to another thing I’d like to mention. Back in April when I first prepared the Python patch series, I naively thought that after cutting them out, it was just a matter of posting them, iterate through a few review/rework steps and they’d be committed. Simple enough. But here we are in mid-October and just two from nine patches went in (now it’s more like 15 patches in total)! What happened?

The problem is that we’ve been working in the branch in an experimental and exploratory way, just hacking together enough to get something useful done. This was necessary because we didn’t know exactly what we would want to expose from GDB to Python, and how we wanted to do that. As we progressed and discussed the results, things started to become clear. The problem is that now we have a lot to clean up, voids to fill, and above all documentation and testcases to write. This takes time.

At least, that was the problem with the first two patches. I noticed Tom Tromey started to write more documentation and tie more loose ends than in the beginning (me? I’ve just been working on the first two patches until they were ready. Didn’t write sexy new stuff since then…), so there’s hope that the next patches will be easier to work with. We still lack a lot of tests for the testsuite, though…

Read Full Post »

As I mentioned before, I’ve been working on and off on adding Python scripting support to GDB, with Tom Tromey and Vladimir Prus. We did the work in a git repository, separate from the GDB main repo (which still uses CVS, by the way). Now came the time to get the work we did there, separate it in patches and post them for review on the gdb-patches mailing list.

This is the tale of my patch-producing efforts. I’m sorry, it is most likely boring for everyone but me. Still, I wanted to write it down so you are free to stop reading the post here. 🙂

Anyway, back to where I was: I (foolishly, perhaps?) volunteered to create the patches. One reason for me to do that is that I actually enjoy working with and learning about source code management and related issues, and this was a good opportunity for me to improve my git-fu (since I thought git could help me do the job in a sane way, which fortunately proved to be the case).

I say foolishly because I thought it wouldn’t take too much time to cut out the patches, since I knew what I would have to do… The task of course took longer than I expected, in part because of unforeseen autotools woes, but of course also because I underestimated the effort (I am an optimist).

Here is the method I used:

First of all, I wanted to update the code to the latest CVS version of GDB, because the work in the git repo was done based on a CVS version from February… Nothing to see here, actually. I just created a new branch with the latest CVS update, and rebased the commits on top of that. In hindsight, I should have merged the new CVS update into the python branch, which would have made me deal with less conflicts. I used rebase because I thought I would cherry pick the commits later, so I wanted each of them refreshed.

Then the real fun began. I started using interactive mode of git rebase to squash related commits together (to form the patches), and reorder them. I quickly realised that with this approach I would have a bit of difficulty with commits which touched different areas of the code and crossed the borders I had in my mind for the patches I wanted to generate. I would have to first split those into smaller, more behaving commits and only then squash them with other similar changes. That seemed to be more work than really necessary.

Also, the older commits did things in ways and places which were later changed, and it looked like I would have some trouble reconciling older and newer code to fit in one patch (maybe not though, maybe that would be taken care of more or less naturally). Also, I would need to take some time to familiarise myself with the commits in the branch, because I only authored some of them. The “shuffle commits around” approach wasn’t looking very promising.

I then turned to a different strategy: I generated a big patch containing all of the code in the branch, and applied it (using the plain old patch command) on top of a clean branch which contained only the CVS HEAD version I was using as a base. All I needed to do now was to selectively add to the index the changes that I wanted to include in a patch and then commit those changes together. And repeat the process for the next patch and so on.

It proved to be a good approach, especially because of the interactive mode of git-add. This mode asks you about each change inside a modified file, letting you add that change to the index or skip it, leaving it in the working directory for a future commit. git add -i streamlined the “change picking” process quite a lot, and made the patch-cutting almost mechanical. (By the way, this feature is also available in Mercurial, with the Record extension. I even believe (not sure though) that the Mercurial extension predates git add -i) (I don’t know if Bazaar has it, would be nice to know).

In this phase of the process, git rebase -i was useful. Sometimes I came accross a change in the working directory which would fit better in a patch which I had already committed. It was simply a matter of committing that change and then shuflling it back and squashing with the proper patch.

At each commit I pushed the changes to a pristine repo which I used to build GDB and guarantee that each patch included all the changes it needed.

Voilà, at the end of the process I had a git branch where each commit corresponded to one patch which I wanted to send to the mailing list.

Read Full Post »

I’ve been working on Python bindings for exposing GDB’s frame_info, the internal structure it uses to keep track of the frame stack in the debuggee (or inferior, in GDB parlance). I got just enough working to be able to implement an equivalent of GDB’s backtrace command entirely in Python. The difference is that my version of the command prints older frames first and newer last, which feels more natural to me. 🙂

Here’s the output I get:

(gdb) rbt
#2 0x080483bb in main at ../../src/examples/funcs.c:15
#1 0x08048391 in f1 at ../../src/examples/funcs.c:10
#0 f2 at ../../src/examples/funcs.c:5

A little bit more information, and the definition of the Python command which does the above can be found in my post to the mailing list. The code is on the git repo for the Python work.

Read Full Post »