简体   繁体   English

获取 OS X 中打开的文件(描述符)列表

[英]Get list of open files (descriptors) in OS X

I would like to get a list of open files in a process on os x (10.9.1).我想获取 os x (10.9.1) 进程中打开文件的列表。 In Linux I was able to get this from /proc/PID/fd .在 Linux 中,我能够从/proc/PID/fd获得这个。 However I'm not sure how to get the same on OS X. I found that the procfs is not present on the OS X (by default. possible implementations present, but I do not want to go that way).但是我不确定如何在 OS X 上获得相同的结果。我发现 OS X 上不存在 procfs(默认情况下。可能的实现存在,但我不想那样做)。

So how do I get (natively) the list of open files in a process on OS X. One way is lsof .那么我如何(本机)获取 OS X 进程中打开文件的列表。一种方法是lsof is there any other support available?还有其他支持吗? please let me know where I can get more info on this.请让我知道在哪里可以获得更多信息。

Thanks.谢谢。

I had a hard time getting Activity Monitor to show open files for a process that was running as root (via sudo).我很难让活动监视器显示以 root 身份运行的进程的打开文件(通过 sudo)。 The original question mentions lsof , and it does the trick exactly.最初的问题提到了lsof ,它确实起到了作用。 If you know the process name or PID, it's super quick.如果您知道进程名称或 PID,它会非常快。

Find processes by name:按名称查找进程:

lsof -c processname

Find processes by PID:按 PID 查找进程:

lsof -p 123

(Prefix with sudo as needed, such as if you are not the owner of the process.) (根据需要使用sudo前缀,例如,如果您不是该进程的所有者。)

At least on OSX 10.10 (Yosemite, didn't check on Mavericks), you can get the list of open files by process via the default activity monitor application.至少在 OSX 10.10(Yosemite,没有检查 Mavericks)上,您可以通过默认的活动监视器应用程序按进程获取打开文件的列表。 Just double click on the relevant process on the list and select "Open Files and Ports" tab on the popup.只需双击列表中的相关进程,然后在弹出窗口中选择“打开文件和端口”选项卡。

Tip: cmd+f shortcut on that pane allows for searching and highlighting on the content.提示:该窗格上的 cmd+f 快捷方式允许搜索和突出显示内容。

This works for some things:这适用于某些事情:

sudo fs_usage | grep dev

for /dev/ files or similar.对于/dev/文件或类似文件。

The clean and simple approach to inspect the current process ( ie the equivalent of /proc/self/fd on Linux) is to use ls /dev/fd/ :检查当前进程(Linux 上的/proc/self/fd等价物)的简洁方法是使用ls /dev/fd/

eg例如

$ touch "file"
$ exec 3<>file

$ ls /dev/fd/    

0 1 2 3

列出 /Volumes/VolumeName 上的打开文件:

lsof | grep "/Volumes/VolumeName"

I use the What's Open application that is very handfull (with filters, disk selection, ...).我使用非常实用的 What's Open 应用程序(带有过滤器、磁盘选择等)。

You can find it there : http://whatsopen.en.softonic.com/mac .你可以在那里找到它: http ://whatsopen.en.softonic.com/mac。

Since you asked "Is there any other support [than lsof] available?", try this:既然你问“还有其他支持 [除了 lsof] 可用吗?”,试试这个:

Create a command line tool using the "proc_pidinfo" C API referenced in the selected answer to this question: How can I programmatically get the list of open file descriptors for a given PID on OS X?使用此问题的选定答案中引用的“proc_pidinfo”C API 创建命令行工具: 如何以编程方式获取 OS X 上给定 PID 的打开文件描述符列表?

You can use proc_pidinfo with the PROC_PIDLISTFDS option to enumerate the files used by a given process.您可以使用带有 PROC_PIDLISTFDS 选项的 proc_pidinfo 来枚举给定进程使用的文件。 You can then use proc_pidfdinfo on each file in turn with the PROC_PIDFDVNODEPATHINFO option to get its path.然后,您可以在每个文件上依次使用 proc_pidfdinfo 和 PROC_PIDFDVNODEPATHINFO 选项来获取其路径。

"You can get the list of open files by process via the default activity monitor application. Just double click on the relevant process on the list and select "Open Files and Ports" tab on the popup." “您可以通过默认的活动监视器应用程序按进程获取打开文件的列表。只需双击列表上的相关进程,然后在弹出窗口中选择“打开文件和端口”选项卡。” But "you had a hard time getting Activity Monitor to show open files for a process that was running as root (via sudo) or others user".但是“您很难让 Activity Monitor 显示以 root(通过 sudo)或其他用户身份运行的进程的打开文件”。

So just run Active Monitor by sudo, that`s it所以只需通过 sudo 运行Active Monitor ,就是这样

sudo /Applications/Utilities/Activity\ Monitor.app/Contents/MacOS/Activity\ Monitor

lsof -c /^74016$/ -d^txt -FcfadDtns0

The -F instructs lsof to produce output suitable for consumption by another process, such as Perl or awk. -F指示lsof生成适合其他进程(例如 Perl 或 awk)使用的输出。 In man lsof search for OUTPUT FOR OTHER PROGRAMS for details.man lsof中搜索OUTPUT FOR OTHER PROGRAMS以获取详细信息。 The characters that come after -F represent a field you want to select for output. -F之后的字符代表您要选择输出的字段。 The output then puts each of these characters in front of the respective field.然后输出将这些字符中的每一个放在相应字段的前面。 Example output:示例输出:

p212^@cloginwindow^@
fcwd^@a ^@tDIR^@D0x1000004^@s704^@n/^@
f0^@ar^@tCHR^@D0xf218cacb^@n/dev/null^@
f1^@au^@tCHR^@D0xf218cacb^@n/dev/null^@
f2^@au^@tCHR^@D0xf218cacb^@n/dev/null^@
f3^@ar^@tREG^@D0x1000004^@s216424^@n/Library/Application Support/CrashReporter/SubmitDiagInfo.domains^@
f4^@ar^@tREG^@D0x1000004^@s77^@n/private/etc/security/audit_user^@
f5^@ar^@tREG^@D0x1000004^@s652^@n/private/etc/security/audit_class^@
f6^@ar^@tREG^@D0x1000004^@s358^@n/private/etc/security/audit_control^@
f7^@ar^@tREG^@D0x1000004^@s111033^@n/System/Library/Frameworks/CoreImage.framework/Versions/A/Resources/ci_stdlib.metallib^@
f8^@au^@tIPv4^@d0xc401abd77f1dd1d9^@n*:*^@
f9^@ar^@tREG^@D0x1000004^@s308316^@n/System/Library/Frameworks/CoreImage.framework/Versions/A/Resources/ci_filters.metallib^@
f10^@au^@tREG^@D0x1000004^@s1536^@n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/3902/libraries.maps^@
f11^@au^@tREG^@D0x1000004^@s65536^@n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/3902/libraries.data^@
f12^@au^@tREG^@D0x1000004^@s1536^@n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/Intel(R) HD Graphics 630/functions.maps^@
f13^@au^@tREG^@D0x1000004^@s131072^@n/private/var/folders/4g/3lkhwv6n7_76_1s8snscvhxc0000gp/C/com.apple.loginwindow/com.apple.metal/Intel(R) HD Graphics 630/functions.data^@
p421^@ccoreauthd^@
fcwd^@a ^@tDIR^@D0x1000004^@s704^@n/^@
f0^@ar^@tCHR^@D0xf218cacb^@n/dev/null^@
f1^@au^@tCHR^@D0xf218cacb^@n/dev/null^@
f2^@au^@tCHR^@D0xf218cacb^@n/dev/null^@
p537^@cUserEventAgent^@
fcwd^@a ^@tDIR^@D0x1000004^@s704^@n/^@
f0^@ar^@tCHR^@D0xf218cacb^@n/dev/null^@
f1^@au^@tCHR^@D0xf218cacb^@n/dev/null^@
f2^@au^@tCHR^@D0xf218cacb^@n/dev/null^@
f3^@au^@tunix^@d0xc401abd77b9c8579^@n->0xc401abd77b9c8709^@
f4^@au^@tunix^@d0xc401abd77b9c7129^@n->0xc401abd77b9c8899^@

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM