我编写了以下代码来确定程序写入哪些文件。我当然想捕获文件名。
strace -f -t -e trace=file -p 8804 2>&1 | grep -oP "\"(.*)\".*O_WRONLY"
Run Code Online (Sandbox Code Playgroud)
这输出类似
/tmp/11111111.txt", O_WRONLY
Run Code Online (Sandbox Code Playgroud)
问题是我无法将所有这些输出传递给任何命令
strace -f -t -e trace=file -p 8804 2>&1 | grep -oP "\"(.*)\".*O_WRONLY" | echo
# does not show anything
Run Code Online (Sandbox Code Playgroud)
而且我无法保存所有这些的输出以供以后使用:
strace -f -t -e trace=file -p 8804 2>&1 | grep -oP "\"(.*)\".*O_WRONLY" > asd.out
# file is empty
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助。:)
您可以将输出写入文件(使用strace -o asd.out
),然后 grep 它:
来自 strace 手册:
-o filename Write the trace output to the file filename rather than
to stderr. Use filename.pid if -ff is used. If the argument begins with
`|' or with `!' then the rest of the argument is treated as a command
and all output is piped to it. This is convenient for piping the
debugging output to a program without affecting the redirections of
executed programs.
Run Code Online (Sandbox Code Playgroud)