#!/usr/bin/env python # -*- coding: utf-8 -*- # copyright 2008 by Christoph Höger # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . """ tracetrap will replace a executable with a script that runs strace on it with the given outfile and the given strace arguments. The original file is saved as .tracetrap and can be restored manually or via tracetrap --restore This is usefull for sysadmins that want to strace processes that will be started at an unpredictable point in time. """ import sys import os import getopt import shutil __version__ = "v0.2" __usageString__ = """ usage: tracetrap.py --restore Where is the path to a trapped executable or: tracetrap.py OPTIONS Where OPTIONS consists of (optionals in [brackets]) -e , --exe=: Path to executable to trap -l , --log=: Path to log file [-a , --traceargs= Extra arguments for strace] [-f , --filter= Apply FILTER Expression to strace output] """ __trapstring__= """#!/usr/bin/env python # -*- coding: utf-8 -*- # trapped version. original is %(original)s import sys import os import re if __name__ == "__main__": argstr = "" for arg in sys.argv[1:]: argstr += " " + arg instr, out, err = os.popen3("strace %(straceargs)s %(original)s.tracetrap" + argstr) filter = "%(filter)s" regexpr = re.compile(filter) logfile = open("%(logfile)s","w") for line in err: if regexpr.match(line): logfile.write(line) logfile.close() """ class Args(): def __init__(self): self.restore = False self.path = None self.log = None self.args = "" self.filter = "" def about(): print "tracetrap.py " + __version__ print "2008 by Christoph Höger " def usage(): print "usage: tracetrap.py [OPTIONS]" print __usageString__ def get_args(): args = Args() try: options, arguments = getopt.getopt(sys.argv[1:],"hvr:e:l:a:f:", ["help","version","restore=","exe=","log=","traceargs=","filter="]) except getopt.GetoptError, err: print "Options error: " + str(err) usage() sys.exit(1) for o, a in options: if o in ("-v", "--version"): about() sys.exit(0) elif o in ("-h", "--help"): usage() sys.exit(0) elif o in ("-r", "--restore"): args.restore = True args.path = a elif o in ("-r", "--restore"): args.restore = True args.path = a elif o in ("-e", "--exe"): args.path = a elif o in ("-l", "--log"): args.log = a elif o in ("-a", "--traceargs"): args.args = a elif o in ("-f", "--filter"): args.filter = a return args def restore(args): if (args.path): original = args.path + ".tracetrap" shutil.move(original, args.path) else: print("No path argument given!") sys.exit(1) def setup_trap(args): if (not args.log): print "-l argument is required" sys.exit(1) if (args.path): shutil.move(args.path, args.path + ".tracetrap") outfile = open(args.path,"w") outfile.write(__trapstring__ % {'original' : args.path, 'logfile':args.log, 'straceargs' : args.args, "filter" : args.filter}) outfile.close() shutil.copymode(args.path + ".tracetrap", args.path) else: print "-e argument is required!" sys.exit(1) if __name__ == "__main__": args = get_args() if (args.restore): restore(args) else: setup_trap(args)