执行失败script.sh:未知错误

Failed to execute script.sh: unknown error

我想使用 DTrace 查看 "what syscalls are made by my shell script"。

我制作了一个非常简单的shell脚本,shell.sh,并赋予它执行权限:

#!/bin/bash
grep 1 <<< 123

cd 进入了它的目录,运行 这个简单的 DTrace 脚本:

sudo dtrace -n 'syscall:::entry
/pid == $target/
{
    @[probefunc] = count();
}' -c ./trace-me.sh

我得到这个错误输出:

dtrace: failed to execute ./trace-me.sh: unknown error

这里发生了什么?我 运行 csrutil enable --without dtrace。如果我删除 -c arg(并用 pid 替换 $target),DTrace 脚本 运行 没问题。

这只是另一个 Mac 问题吗?我正在 运行正在使用 macOS Sierra 10.12.5 Beta。

感谢@l'L'l 链接到的tip:我能够解决这个问题。

你需要两个 shell。

在 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() && pid != /
{
    @[probefunc] = count();
}' 93827

我们使用 progenyof() 来确保我们跟踪 shell 的 child 个进程 。我添加了 && pid != 因为出于某种原因 progenyof(x) 似乎包括 x.

现在回到 shell A,运行 您想要检查的一些代码:

grep 1 <<< 123

我们在 shell B 中的 DTrace 程序将成功捕获在 shell A 中启动的 child 进程。

需要筛选一些杂音。也许 shell 会推出各种 children。不确定如何更有选择性。


看看 dtruss 如何实现 -f ("follow children as they are forked")...

很有教育意义
less "$(which dtruss)"

相关子句是那些使用 OPT_follow && 过滤器(表示 -f 已启用)或 self->child 变量(表示此线程是 child -p PID).

中指定的过程

了解 ppid 是一个 built-in 变量也很有用,它为您提供 parent PID。