我以這種方式使用strace的: strace的輸出處理
strace -xf -eopen -o out_configure.log ./configure
strace -xf -eopen -o out_make.log make
然後我得到明確的文件列表中使用sed:
sed -n 's/.*open("\(.*\)".*)\s*=.*/\1/p' out_configure.log out_make.log | sort -u
有一個輸出例子:
/usr/share/locale-langpack/en.utf8/LC_MESSAGES/gcc-4.6.mo
/usr/share/locale-langpack/en.UTF-8/LC_MESSAGES/gcc-4.6.mo
/usr/share/locale-langpack/en.utf8/LC_MESSAGES/grep.mo
/usr/share/locale-langpack/en.UTF-8/LC_MESSAGES/grep.mo
/usr/share/locale-langpack/en.utf8/LC_MESSAGES/make.mo
/usr/share/locale-langpack/en.UTF-8/LC_MESSAGES/make.mo
/usr/share/locale/locale.alias
/usr/share/misc/magic.mgc
/usr/x86_64-linux-gnu/lib64/libc_r.a
/usr/x86_64-linux-gnu/lib64/libc_r.so
/usr/x86_64-linux-gnu/lib64/libsendfile.a
/usr/x86_64-linux-gnu/lib64/libsendfile.so
/usr/x86_64-linux-gnu/lib64/libtruerand.a
/usr/x86_64-linux-gnu/lib64/libtruerand.so
/usr/x86_64-linux-gnu/lib64/libuuid.a
/usr/x86_64-linux-gnu/lib64/libuuid.so
util.c
util_cfgtree.c
./util_cfgtree.h
util_cfgtree.h
util_cfgtree.i
./util_cfgtree.lo
util_cfgtree.lo
util_cfgtree.loT
util_cfgtree.o
util_cfgtree.s
util_charset.c
./util_charset.h
我的問題是非全名條目(如「util.c」或「./util_cfgtree.h」)。 有沒有什麼辦法在strace輸出中獲得全名?
我已經寫了這個劇本:
#!/usr/bin/python
# coding=UTF8
import subprocess
import os
fileList = []
for dirname, dirnames, filenames in os.walk(os.path.abspath('.')):
for filename in filenames:
fileList.append(os.path.join(dirname, filename))
straceProcess = subprocess.Popen("strace -s 1000 -xf -o out_configure.sed.log ./configure".split(), stdout=subprocess.PIPE).communicate()[0]
straceProcess2 = subprocess.Popen("strace -s 1000 -xf -o out_make.sed.log make".split(), stdout=subprocess.PIPE).communicate()[0]
f = open("out_configure.sed.log", "r+")
n = open("out_make.sed.log", "r+")
lines = []
for l in f:
lines.append(l)
for l in n:
lines.append(l)
f.close()
n.close()
pids = {}
filesInUse = []
currentDir = os.path.abspath('.')
for line in lines:
parts = line.split()
if not pids.has_key(parts[0]):
pids[parts[0]] = currentDir
if parts[1].startswith("clone"):
pid = parts[parts.__len__() - 1]
if not pids.has_key(pid):
pids[pid] = os.path.abspath(pids.get(parts[0]))
elif parts[1].startswith("chdir"):
if parts[1].split("\"")[1].startswith("/"):
pids[parts[0]] = os.path.abspath(parts[1].split("\"")[1])
elif parts[1].split("\"")[1].startswith("."):
pids[parts[0]] = os.path.abspath(pids.get(pids[parts[0]]) + '/' + parts[1].split("\"")[1])
else:
pids[parts[0]] = os.path.abspath(pids.get(parts[0]) + '/' + parts[1].split("\"")[1])
elif parts[1].startswith("open("):
if parts[1].split("\"")[1].startswith("/"):
filesInUse.append(os.path.abspath(parts[1].split("\"")[1]))
elif parts[1].split("\"")[1].startswith("."):
filesInUse.append(os.path.abspath(pids.get(parts[0]) + '/' + parts[1].split("\"")[1]))
else:
filesInUse.append(os.path.abspath(pids.get(parts[0]) + '/' + parts[1].split("\"")[1]))
for l in filesInUse:
if l in fileList:
fileList.remove(l)
for l in fileList:
print l
但我的Python的知識是非常差的。
有沒有錯誤或不好的解決方案?
'strace'會記錄系統調用。如果程序要求打開(「util_charset.c」,...)',那就是將被記錄的名稱。您需要知道進程的當前目錄以便能夠解釋相對路徑名(包括'open(「subdir/file.c」,...)')。爲此,您需要知道流程的當前目錄 - 流程啓動時的啓動以及流程運行期間所做的任何更改。如果您有兩個目錄,例如程序源代碼的舊版本和新版本,則可能無法從'strace'輸出中說出哪個版本被跟蹤。 –