【问题标题】:Is there a way to capture the exit code of the child command when run through strace?有没有办法在通过 strace 运行时捕获子命令的退出代码?
【发布时间】:2019-04-09 15:29:09
【问题描述】:

我正在尝试使用 strace 跟踪运行单元测试所涉及的文件。有没有办法在我执行此操作时捕获测试运行的退出代码?

@cmd="php -d auto_prepend_file=auto_prepend_for_tests.php /data/tests/sdk/bin/phpunit.phar --configuration /data/tests/conf/database.xml
 my_test.php"
open(IN, "-|", "strace -ff $pid -e trace=file @cmd 2>&1")

【问题讨论】:

    标签: perl phpunit strace


    【解决方案1】:

    Strace 将以与被跟踪程序相同的退出代码退出。到目前为止一切顺利。

    open 看起来有点perlish,是不是我们说的perl? 然后来自perldoc -f open 的这一点适用:

    关闭任何管道文件句柄会导致父进程等待 child 完成,然后返回 $? 中的状态值?和 “${^CHILD_ERROR_NATIVE}”。

    所以,是的,您可以通过调用 close IN 并查看 $? 中的值来获取代码。

    【讨论】:

    • 感谢您的回复。是的,那是 perl 脚本。我试图在通过此脚本运行 phpunit 测试时加载所有文件。我看到那美元?当我知道测试返回exitcode 139 时,总是返回exitcode 0。这是我尝试过的:``` open(IN, "-|", "strace -ff $pid -e trace=file @cmd 2>&1") ||死; # 打印 $?;逼近) ? print "退出代码是 $?" : 死();打印 $?;打印“${^CHILD_ERROR_NATIVE}”;打印 $CHILD_ERROR; ```
    • @Prathyusha 如果你在没有 strace 的情况下运行 phpunit,它真的返回 139 吗?我看到一些错误报告,它们总是返回 0:github.com/sebastianbergmann/phpunit/issues/1659
    • 我很肯定。在控制台上单独运行命令并看到它返回 139。此外,尝试了一个返回退出代码 1 的不同测试。仍然在 perl 脚本中看到exitcode 0。供参考:我在gitlab.com/ole.tange/tangetools/blob/master/tracefile/tracefile 中使用了类似的代码
    • @Prathyusha 也许你可以在输出中寻找像[pid 12345] +++ exited with 42 +++ 这样的消息
    • 解决方案。我在错误的地方寻找退出代码。这就是我所做的。 open(IN, "-|", "strace -ff $pid -e trace=file @cmd 2>&1") || die; while(<IN>) { // some logic } close(IN); print "exitcode=" . ($? >> 8);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 2020-10-16
    • 2011-09-22
    相关资源
    最近更新 更多