Skip to content

Instantly share code, notes, and snippets.

@rampage644
Created August 10, 2014 13:03
Show Gist options
  • Save rampage644/178f1ab3fbcc917b0970 to your computer and use it in GitHub Desktop.
Save rampage644/178f1ab3fbcc917b0970 to your computer and use it in GitHub Desktop.
Python strace log analysis script
#!/usr/bin/env python
import sys
import re
def main():
regexp = re.compile(r'^(\S+)\((.*)\)\s+=\s+(\d+)$')
whitelist = ['read', 'write', 'fstat', 'lseek', 'fcntl']
opened_fd = {}
output = {}
with open(sys.argv[1]) as f:
for line in f:
try:
(func, args, res) = regexp.findall(line)[0]
except IndexError:
continue
arg0 = args.split(',')[0]
# check for open/close
if func == 'open':
opened_fd[int(res)] = arg0
output[int(res)] = []
if func in whitelist:
try:
output[int(arg0)].append(func)
except KeyError as e:
print("Error with {}({}), skipping".format(func, arg0))
if func == 'close':
print(opened_fd[int(arg0)], end=': ')
print(", ".join(set(output[int(arg0)])))
del opened_fd[int(arg0)]
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment