Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial Python Call tracer #1

Merged
merged 1 commit into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions PythonTracer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# PythonTracer

This directory consist of _prototype_ Python Call tracer.

**Note:**
1. This is a poor mans implementation not ready for everyday use!!!
2. This implementation significantly slows down the execution speed.

Empty file.
16 changes: 16 additions & 0 deletions PythonTracer/testcov/testfact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/python

from __future__ import print_function

def fact(n):
if n == 0:
return 1
else:
return n*fact(n - 1)

def main():
i = 20
print("fact(%d): %d" %(i, fact(i)))

if __name__ == '__main__':
main()
56 changes: 56 additions & 0 deletions PythonTracer/tracer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/python

# This file is looking into the python tracing support.

#from __future__ import print_function

import sys
import threading
import logging
from testcov import testfact

logger = None
# Enable Logger
logging.basicConfig(level = logging.DEBUG)
logger = logging.getLogger(__name__)
logger.propagate = False

# Create file handler
handler = logging.FileHandler(str(__file__) + '_tracing.log')
handler.setLevel(logging.DEBUG)

# Create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# Add the handlers to the logger
logger.addHandler(handler)

def profilefunc(frame, event, arg):
code = frame.f_code
#print("%s:%d:%s:%s" %(code.co_filename, frame.f_lineno, code.co_name, event))
# Logger doesn't work with multithreaded Python 2.x environment.
# It seems to be an issue with python internals.
# Logger seems to work under Python 3.6.1
if 'threading.py' not in code.co_filename.lower():
logger.debug("%s:%d:%s:%s" %(code.co_filename, frame.f_lineno, code.co_name, event))

def fibo(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibo(n - 2) + fibo(n - 1)

def main():
for i in range(0, 20):
print("fibo(%d): %d, fact(%d): %d" %(i, fibo(i), i, testfact.fact(i)))

if __name__ == '__main__':
#threading.setprofile(profilefunc)
sys.setprofile(profilefunc)
logger.info('Start Logging')
main()
logger.info('Closing Logging')