All About Performance

and other stuff by Taras Glek

Python GDB - Logging File IO

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. :
1
2
3
4
5
6
7
8
9
break open  

source -p /path/to/your/script.py  

command 1  

taras  

end
  1. Have the script walk the backtrace to figure out the filename and the last Mozilla function. Log the info, issue gdb continue command.
  2. Print out a report and profit:
1
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.

Comments