Skip to content

Instantly share code, notes, and snippets.

@Anthchirp
Created December 5, 2019 14:03
Show Gist options
  • Save Anthchirp/9a064d5c3971a6b44b26bc5b3ad0b682 to your computer and use it in GitHub Desktop.
Save Anthchirp/9a064d5c3971a6b44b26bc5b3ad0b682 to your computer and use it in GitHub Desktop.
Nice formatting for `strace -e trace=open,close`
from __future__ import absolute_import, division, print_function
import re
import sys
re_close = re.compile(r"close\(([^)]+)\)")
re_open = re.compile(r'open\("([^"]+)"[^)]*\) += +([^ ]+)( .*)?')
open_files = {}
file_filter = ("/scratch/wra62962/files/dials/", "/home/wra62962/")
newdepth = depth = 0
for l in sys.stdin.readlines():
l = l.rstrip()
if l.startswith("open("):
m = re_open.match(l)
assert m, l
if any(m.group(1).startswith(ff) for ff in file_filter):
continue
if m.group(2) == "-1":
if m.group(3).lstrip().startswith("ENOENT"):
continue
outline = l
else:
newdepth = depth + 1
outline = "open('%s') as %s" % (m.group(1), m.group(2))
open_files[m.group(2)] = m.group(1)
elif l.startswith("close("):
m = re_close.match(l)
assert m, l
if m.group(1) not in open_files:
continue
outline = "close(%s) = %s" % (m.group(1), open_files[m.group(1)])
depth = depth - 1
newdepth = depth
del open_files[m.group(1)]
else:
outline = "\033[91m" + l + "\033[0m"
print("%s%s" % (" " * depth, outline))
sys.stdout.flush()
depth = newdepth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment