无法执行script.sh:未知错误 [英] Failed to execute script.sh: unknown error
问题描述
我想使用DTrace来查看"shell脚本进行了哪些系统调用".
I wanted to use DTrace to see "what syscalls are made by my shell script".
我制作了一个非常简单的shell脚本shell.sh
,并为其赋予了执行权限:
I made a very simple shell script, shell.sh
, and gave it execute privileges:
#!/bin/bash
grep 1 <<< 123
我cd
进入其目录,并运行了这个简单的DTrace脚本:
I cd
'd into its directory, and ran this simple DTrace script:
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脚本运行良好.
What happened here? I've run csrutil enable --without dtrace
. The DTrace script runs fine if I remove the -c
arg (and replace $target
with a pid).
这只是另一个Mac陷阱吗?我正在运行macOS Sierra 10.12.5 Beta.
Is this just another Mac gotcha? I'm running macOS Sierra 10.12.5 Beta.
推荐答案
感谢@l'的提示链接:我可以解决此问题.
Thanks to the tip to which @l'L'l linked: I was able to work around this.
您将需要两个外壳.
在外壳A(我们将要检查的外壳)中:
In shell A (the shell we'll be inspecting):
# copy this shell's PID to clipboard (93827 for this example)
echo $$ | pbcopy
在外壳B(将运行DTrace的外壳)中,开始跟踪该PID:
In shell B (the shell which will run DTrace), start tracing that PID:
sudo dtrace -n 'syscall:::entry
/progenyof($1) && pid != $1/
{
@[probefunc] = count();
}' 93827
我们使用 progenyof()
来确保我们跟踪外壳的子进程.我添加了&& pid != $1
,因为出于某些原因,progenyof(x)
似乎包含了x
.
We use progenyof()
to ensure that we trace child processes of the shell. I've added && pid != $1
since for some reason progenyof(x)
seems to include x
.
现在回到外壳A,运行一些您想检查的代码:
Now back in shell A, run some code you'd like to inspect:
grep 1 <<< 123
我们在外壳B中的DTrace程序将成功捕获在外壳A中启动的子进程.
Our DTrace program in shell B will successfully catch the child process launched in shell A.
有一些噪音需要筛选.也许贝壳发射了各种各样的孩子.不确定如何更具选择性.
There's a bit of noise to sift through. Maybe the shell launches a variety of children. Not sure how to be more selective.
看看dtruss如何实现-f
(跟随孩子被分叉")是很有教育意义的.
It's educational to look at how dtruss implements -f
("follow children as they are forked")...
less "$(which dtruss)"
相关子句是使用OPT_follow &&
过滤器(表明已启用-f
)或self->child
变量(表明该线程是-p PID
中指定的进程的子代)的子句.
Relevant clauses are those using an OPT_follow &&
filter (indicates that -f
is enabled) or the self->child
variable (indicates that this thread is a child of the process specified in -p PID
).
知道 ppid
也很有用是一个内置变量,可为您提供父PID.
It's also useful to know that ppid
is a built-in variable which gives you the parent PID.
这篇关于无法执行script.sh:未知错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!