tags:

views:

182

answers:

3

I am wondering if it might be possible to get a list of files/directories that the debugged application has opened but not closed from GDB itself ?

Currently I set a breakpoint and then I use an external program like lsof to check for opened files.

But this approach is really annoying.

Environment: Debian-Lenny with gdb v6.8

EDIT: I am asking because my application is leaking file handles in some situations

A: 

No, but you can run lsof and filter down to the debugged process.

bmargulies
+2  A: 

On Linux you can also just look in /proc/<pid>/fd. To do that from GDB (e.g. if you want to attach it to a breakpoint) is pretty simple. Or of course you can just use lsof, too.

(gdb) info proc
process 5262
cmdline = '/bin/ls'
cwd = '/afs/acm.uiuc.edu/user/njriley'
exe = '/bin/ls'
(gdb) shell ls -l /proc/5262/fd
total 0
lrwx------ 1 njriley users 64 Feb  9 12:45 0 -> /dev/pts/14
lrwx------ 1 njriley users 64 Feb  9 12:45 1 -> /dev/pts/14
lrwx------ 1 njriley users 64 Feb  9 12:45 2 -> /dev/pts/14
lr-x------ 1 njriley users 64 Feb  9 12:45 3 -> pipe:[62083274]
l-wx------ 1 njriley users 64 Feb  9 12:45 4 -> pipe:[62083274]
lr-x------ 1 njriley users 64 Feb  9 12:45 5 -> /bin/ls
(gdb) shell lsof -p 5262
COMMAND  PID    USER   FD   TYPE DEVICE    SIZE     NODE NAME
ls      5262 njriley  cwd    DIR   0,18   14336   262358 /afs/acm.uiuc.edu/user/njriley
ls      5262 njriley  rtd    DIR    8,5    4096        2 /
ls      5262 njriley  txt    REG    8,5   92312     8255 /bin/ls
ls      5262 njriley  mem    REG    8,5   14744   441594 /lib/libattr.so.1.1.0
ls      5262 njriley  mem    REG    8,5    9680   450321 /lib/i686/cmov/libdl-2.7.so
ls      5262 njriley  mem    REG    8,5  116414   450307 /lib/i686/cmov/libpthread-2.7.so
ls      5262 njriley  mem    REG    8,5 1413540   450331 /lib/i686/cmov/libc-2.7.so
ls      5262 njriley  mem    REG    8,5   24800   441511 /lib/libacl.so.1.1.0
ls      5262 njriley  mem    REG    8,5   95964   441580 /lib/libselinux.so.1
ls      5262 njriley  mem    REG    8,5   30624   450337 /lib/i686/cmov/librt-2.7.so
ls      5262 njriley  mem    REG    8,5  113248   441966 /lib/ld-2.7.so
ls      5262 njriley    0u   CHR 136,14               16 /dev/pts/14
ls      5262 njriley    1u   CHR 136,14               16 /dev/pts/14
ls      5262 njriley    2u   CHR 136,14               16 /dev/pts/14
ls      5262 njriley    3r  FIFO    0,6         62083274 pipe
ls      5262 njriley    4w  FIFO    0,6         62083274 pipe
ls      5262 njriley    5r   REG    8,5   92312     8255 /bin/ls
Nicholas Riley
is there a way to automate this?
You can attach a breakpoint command list (http://sourceware.org/gdb/current/onlinedocs/gdb/Break-Commands.html) to any breakpoint, thus running lsof on a breakpoint. If you need to track where fds were opened, you can also try valgrind's --track-fds option.
Nicholas Riley
A: 

due to the help of Nicholas I was able to fully automate the task by defining a macro.

.gdbinit:

define lsof
  shell rm -f pidfile
  set logging file pidfile
  set logging on
  info proc
  set logging off
  shell lsof -p `cat pidfile | perl -n -e 'print $1 if /process (.+)/'`
end

document lsof
  List open files
end

here is a session using the new macro (the program opens a file in the /tmp directory):

file hello    
break main
run
next
lsof

output:

...
hello   2683  voku    5r   REG    8,1    37357 11110 /home/voku/hello
hello   2683  voku    6w   REG    8,1        0  3358 /tmp/testfile.txt
...