strace找不到带有“无法统计”错误的shell函数

Isa*_*acS 2 shell function strace

我试图调用一个自定义函数funk_astrace但它似乎没有找到它。我确认 funk_a 可以被自己调用。我很欣赏任何意见。

$ source ./strace_sample.sh 
$ funk_a
Earth, Wind, Fire and Water
$ funk_b
Get on up
strace: Can't stat 'funk_a': No such file or directory

$ dpkg -p strace|grep Vers
Version: 4.8-1ubuntu5
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:        14.04
Codename:       trusty
Run Code Online (Sandbox Code Playgroud)

strace_sample.sh

#!/bin/bash

function funk_a {
  echo "Earth, Wind, Fire and Water"
}

function funk_b {
  echo "Get on up"
  strace -o trace_output.txt -c -Ttt funk_a
}
Run Code Online (Sandbox Code Playgroud)

谢谢你。

Sté*_*las 6

strace只能strace执行文件。

funk_a 是一个函数,一个 shell 的编程结构,而不是你可以执行的东西。

唯一strace可以 strace 是一个新的 shell 来评估该函数的主体,例如:

strace -o trace_output.txt -Ttt bash -c "$(typeset -f funk_a); funk_a"
Run Code Online (Sandbox Code Playgroud)

(我删除了,-c因为它没有意义-Ttt)。

但是bash,除了writefunk_a函数进行的一个系统调用之外,您还会看到加载和初始化(以及在清理和退出之后)调用的所有系统。

或者您可以告诉strace在评估funk_a函数时跟踪 shell 的 pid :

strace -o trace_output.txt -Ttt -p "$$" &
funk_a
kill "$!"
Run Code Online (Sandbox Code Playgroud)

但是,当strace附加到 shell 的 PID 时,shell 很可能已经完成了对函数的解释。你可以尝试一些同步

strace -o trace_output.txt  -Ttt -p "$$" &
tail -F trace_output.txt | read # wait for some output in trace_output.txt

funk_a
kill "$!"
Run Code Online (Sandbox Code Playgroud)

但即便如此,根据时间,trace_output.txt也会包括一些使用 interpret 的系统调用tail|read,或者kill可能会strace在它有时间将echo命令的跟踪写入输出文件之前终止。

更好的方法可能是将调用包装funk_a在两个可识别的系统调用之间,例如

strace -fo >(sed -n '1,\|open("///dev/null|d
                     \|open("/dev///null|q;p' > trace_output.txt
  ) -Ttt -p "$$" &
sleep 1 # give enough time for strace to start
exec 3<  ///dev/null # start signal
funk_a
exec 3< /dev///null # end signal
Run Code Online (Sandbox Code Playgroud)