感谢tip https://stackoverflow.com/a/36760408/5257399@l'L'l 链接到的:我能够解决这个问题。
你需要两个贝壳。
在 shell A(我们将要检查的 shell)中:
# copy this shell's PID to clipboard (93827 for this example)
echo $$ | pbcopy
在 shell B(将运行 DTrace 的 shell)中,开始跟踪该 PID:
sudo dtrace -n 'syscall:::entry
/progenyof($1) && pid != $1/
{
@[probefunc] = count();
}' 93827
We use progenyof() https://docs.oracle.com/cd/E37670_01/E38608/html/dt_progenyof_actsub.html以确保我们追踪子进程外壳的。我已经添加&& pid != $1
因为由于某种原因progenyof(x)
似乎包括x
.
现在回到 shell A,运行一些您想要检查的代码:
grep 1 <<< 123
shell B 中的 DTrace 程序将成功捕获 shell A 中启动的子进程。
有一点噪音需要筛选。也许 shell 会发射各种各样的孩子。不知道如何更有选择性。
了解 dtruss 如何实现是有教育意义的-f
(“跟随孩子们,因为他们被分叉了”)...
less "$(which dtruss)"
相关条款是那些使用OPT_follow &&
过滤器(表明-f
已启用)或self->child
变量(表明该线程是指定进程的子进程)-p PID
).
了解这一点也很有用ppid https://docs.oracle.com/cd/E37670_01/E38608/html/dt_builtin_dlang.html是一个内置变量,它为您提供父 PID。