Created
February 12, 2018 04:58
-
-
Save derwiki/96ad168e897f3159d51986618a9f7938 to your computer and use it in GitHub Desktop.
Example of a decorator that inserts "logs" into `dtruss` output to better help correlate code to syscalls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
$ sudo dtruss -f -t open python main.py | |
18564/0xc29254: open("function-main-start\0", 0x1000000, 0x1B6) = -1 Err#2 | |
18564/0xc29254: open("/anaconda3/lib/python3.6/__pycache__/random.cpython-36.pyc\0", 0x1000000, 0x1B6) = 3 0 | |
18564/0xc29254: open("/anaconda3/lib/python3.6/lib-dynload/math.cpython-36m-darwin.so\0", 0x0, 0x0) = 3 0 | |
18564/0xc29254: open("/anaconda3/lib/python3.6/__pycache__/hashlib.cpython-36.pyc\0", 0x1000000, 0x1B6) = 3 0 | |
18564/0xc29254: open("/anaconda3/lib/python3.6/lib-dynload/_hashlib.cpython-36m-darwin.so\0", 0x0, 0x0) = 3 0 | |
18564/0xc29254: open("/anaconda3/lib/libssl.1.0.0.dylib\0", 0x0, 0x0) = 3 0 | |
18564/0xc29254: open("/anaconda3/lib/libcrypto.1.0.0.dylib\0", 0x0, 0x0) = 3 0 | |
18564/0xc29254: open("/anaconda3/lib/python3.6/lib-dynload/_blake2.cpython-36m-darwin.so\0", 0x0, 0x0) = 3 0 | |
18564/0xc29254: open("/anaconda3/lib/python3.6/lib-dynload/_sha3.cpython-36m-darwin.so\0", 0x0, 0x0) = 3 0 | |
18564/0xc29254: open("/anaconda3/lib/python3.6/__pycache__/bisect.cpython-36.pyc\0", 0x1000000, 0x1B6) = 3 0 | |
18564/0xc29254: open("/anaconda3/lib/python3.6/lib-dynload/_bisect.cpython-36m-darwin.so\0", 0x0, 0x0) = 3 0 | |
18564/0xc29254: open("/anaconda3/lib/python3.6/lib-dynload/_random.cpython-36m-darwin.so\0", 0x0, 0x0) = 3 0 | |
18564/0xc29254: open("/dev/urandom\0", 0x1000000, 0x2C0EDDF) = 3 0 | |
18564/0xc29254: open("function-main-end\0", 0x1000000, 0x1B6) = -1 Err#2 | |
""" | |
class strace_mark(object): | |
def __init__(self, f, *args, **kwargs): | |
self.f = f | |
self.args = args | |
self.kwargs = kwargs | |
def __call__(self): | |
try: | |
open('function-%s-start' % self.f.__name__, 'r') | |
except IOError: | |
pass | |
ret = self.f(*self.args, **self.kwargs) | |
try: | |
open('function-%s-end' % self.f.__name__, 'r') | |
except IOError: | |
pass | |
return ret | |
@strace_mark | |
def main(): | |
from random import randint | |
print(randint(0, 9)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment