简体   繁体   中英

strace output processing

I am using strace in this way:

strace -xf -eopen -o out_configure.log ./configure
strace -xf -eopen -o out_make.log make

Then I am getting clear file list with sed:

sed -n 's/.*open("\(.*\)".*)\s*=.*/\1/p' out_configure.log out_make.log | sort -u

There is an output example:

/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

My problem is non-Full name entries (such as "util.c" or "./util_cfgtree.h"). Is there any way to get full names in strace output?

I have wrote this script:

#!/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

But my knowledge of Python is very poor.

Are there any mistakes or bad solutions?

I think in order to do this, you would also need to trace the chdir() system calls. Post-processing would be more complicated, and you would probably need to switch to awk , perl , python or something else for the post processing, because you would need to interpret each chdir() to track the current working directory as it changes, then, for open() calls that are either relative or local (ie not a full path), you would need to prepend the current path and make any adjustments for things like ../ , etc.

Those are local files related to the application you are making.

you could parse the list to another file and then look for anything starting with ./ or not starting with / and do a find within script to map the paths.

In the below example I was building apache and those same files appeared in a hidden folder (part of its build process in the server folder)

find . -name vhost.o
./server/.libs/vhost.o
./server/vhost.o

ls  server/.libs/
config.o      core_filters.o  eoc_bucket.o    exports.o        libmain.a   listen.o  main.o        protocol.o  request.o     util_cfgtree.o  util_debug.o   util_filter.o  util.o       util_script.o  util_xml.o
connection.o  core.o          error_bucket.o  gen_test_char.o  libmain.la  log.o     mpm_common.o  provider.o  scoreboard.o  util_charset.o  util_ebcdic.o  util_md5.o     util_pcre.o  util_time.o    vhost.o

E2A: Here is a one liner that does what you need:

 command="sed -n 's/.*open(\"\(.*\)\".*)\s*=.*/\1/p' out_configure.log out_make.log | sort -u"; echo "remote: " && echo $command| /bin/sh|grep "^/" ;echo "______________________"; echo "local" && echo $command|/bin/sh|grep -v "^/" |xargs -I {}   find . -name  {} -print

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM