如何在 Mac OS X 中跟踪程序的系统调用?

我想跟踪由 找到命令 调试一些性能问题发出的系统调用,但是我不知道如何在 Mac OS X Yosemite 上做到这一点。我怎样才能跟踪任意程序的系统调用,就像 Strace在 FreeBSD 上做的那样?我对跟踪与文件系统相关的调用特别感兴趣。

91395 次浏览

You can use dtruss like in

sudo dtruss find ~/repo -depth 2 -type d -name '.git'

The manual page of that utility will help you to tailor the use of the tool to your needs.

Under current versions of macOS, executables under paths covered by SIP (like /usr/bin) cannot be traced.

You can bypass this by making a copy of the executable in your home directory and tracing the copy:

cp /usr/bin/find find
codesign --remove-signature ./find
sudo dtruss ./find …

You needed to remove the code signature from the new find executable, otherwise SIP still notices that a system file is being accessed (credit: @Anmol Singh Jaggi).