从 strace 输出到另一个窗口

从 strace 输出到另一个窗口

假设我想了解如何作品由斯特雷斯

是否可以将输出重定向strace到另一个窗口?

所以我可以有两个窗口,一个用于撬开我进行所有交互的窗口,另一个用于strace输出的窗口。

答案1

您可以strace -p在一个窗口中使用来跟踪在另一个窗口中运行的命令。该-p选项接受一个或多个进程 ID,因此您可以尝试

strace -p "$(pidof pry)"

如果您至少有一个进程pidof识别为pry.您可能需要进行试验pidof(如果我想跟踪pudb3命令,我需要使用python3而不是pudb3),并且pidof如果存在任何与之相关的歧义,则使用可能不是最好的方法(您可能有多个匹配的命令,但只想要到strace其中之一)。找到正确的流程ps并直接提供选项可能会更容易。

这种方法不太适用于生命周期较短的进程,或者在进程生命周期早期查看输出很重要的情况下,但对于启动时间不是特别有趣的交互式应用程序来说,它可能是可行的。

strace -p使用与strace不使用的区别之一-p是如何处理中断。由 启动的进程strace将具有类似Ctrl-C传递给它的中断(并由 报告strace),但Ctrl-Cinstrace -p会将其分离,并允许进程继续而不进行跟踪(Ctrl-C如果应用程序位于单独的窗口中,则仍然可以在应用程序中工作)。

答案2

以及用于短期进程或在进程生命周期早期查看输出很重要的包装方法:

#!/bin/sh

# the pid could be sent to a file or named pipe the-thing-that-does-
# strace could be watching, though in most cases a human could copy it
echo "PID to strace is $$"

trap : USR2

# block while human copies pid over to strace (or some program acts on
# the above pid being written in that other terminal). Busy loop or
# something instead if this program must not consume input (`sleep`
# complicates the signal handling)
read blocking

# then when strace is up, send this process a USR2 signal. this will
# mean there will be some strace of this script in addition to the
# target, hence the kinda sorta caveat.
exec echo /the/program/of/interest

或者,只需sysdig在另一个终端窗口中运行,就sysdig可以通过名称直接窥探程序:

sysdig -p '%proc.name %evt.type(%evt.args)' \
    proc.name contains /the/program/of/interest

相关内容