gdb:如何列出打开的文件

16 gdb

我想知道是否有可能获得已调试的应用程序已打开但未从GDB本身关闭的文件/目录列表?

目前我设置了断点,然后我使用外部程序lsof来检查打开的文件.

但这种方法真的很烦人.

环境:Debian-Lenny与gdb v6.8

编辑:我问,因为我的应用程序在某些情况下泄漏文件句柄

Nic*_*ley 21

在Linux上你也可以查看/proc/<pid>/fd.要从GDB中执行此操作(例如,如果要将其附加到断点),则非常简单.或者当然你也可以使用lsof.

(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
Run Code Online (Sandbox Code Playgroud)


exb*_*ker 10

如果lsof您的系统上不可用(我遇到过这样的问题),您可以使用gdb info os files. 它打印有关所有进程打开文件的信息。


小智 7

由于尼古拉斯的帮助,我能够通过定义宏来完全自动化任务.

.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
Run Code Online (Sandbox Code Playgroud)

这是一个使用新宏的会话(程序打开/ tmp目录中的文件):

file hello    
break main
run
next
lsof
Run Code Online (Sandbox Code Playgroud)

输出:

...
hello   2683  voku    5r   REG    8,1    37357 11110 /home/voku/hello
hello   2683  voku    6w   REG    8,1        0  3358 /tmp/testfile.txt
...
Run Code Online (Sandbox Code Playgroud)