本文介绍了gdb:如何列出打开的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

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

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.

环境:Debian-Lenny with gdb v6.8

Environment: Debian-Lenny with gdb v6.8

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

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

推荐答案

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

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

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

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

file hello
break main
run
next
lsof

输出:

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

这篇关于gdb:如何列出打开的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 08:30