Python GDB Rocks!

I wanted a non-painful way to figure out what’s causing bonus file IO. I’ve noticed that gtk likes to open files, but I didn’t have the exact details. So I grabbed python gdb, and with some tips on syscalls from gdb old-timers managed to produce a report to assign blame for open()ing files to relevant Mozilla functions.

Other than the gdb-hating syscalls issue, achieving this was simple

  1. Compile python-enabled gdb(Next set of distribution releases should have it..I hope)
  2. Define a new gdb command in a python file. I called mine “taras” for lack of a better name.
  3. Set a breakpoint, attach your command to it. :
    break open
    source -p /path/to/your/script.py
    command 1
    taras
    end
  4. Have the script walk the backtrace to figure out the filename and the last Mozilla function. Log the info, issue gdb continue command.
  5. Print out a report and profit:
    python report()

Here is my script. The only nasty part here is that I had to read the filename out of a register (i’m on amd64, on 32 it’d be $esi instead of $rdi) because gdb doesn’t deal well with system calls.

I’ve never throught it would be this fun to use gdb. I always thought debuggers should be scriptable, thanks to Tom Tromey (lots of gdb tutorials on Tom’s blog) and any others who finally made this a reality.

One Response to “Python GDB – Logging File IO”

  1. Benjamin Smedberg Says:

    You ran this on an unpackaged build, right? I’m pretty sure all those XPT files were merged in packaged builds in bug 469873.

Leave a Reply