Skip to content
45 changes: 45 additions & 0 deletions log_between_modules.r2py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
TYPE="type"
ARGS="args"
RETURN="return"
EXCP="exceptions"
TARGET="target"
FUNC="func"
OBJC="objc"


OLD_LOG = CHILD_CONTEXT_DEF["log"]


class LogToFile():
def __init__(self, arg, filename):
self.argument = arg
self.filename = filename


def writetofile(self):
libfile = dy_import_module("librepyfile.r2py")
librepyfile = libfile.open(self.filename)
librepyfile.write(self.argument)
librepyfile.close()

log_file_def = {"obj-type":LogToFile,
"name":"LogToFile",
"writetofile":{TYPE:FUNC,ARGS:None, EXCP:Exception,RETURN:None,TARGET:LogToFile.writetofile}
}



def log_between_modules(arg):
logObj = LogToFile(arg, "tracer.txt")
logObj.writetofile()


# Mapping our function to log()
CHILD_CONTEXT_DEF["log"] = {TYPE:FUNC,ARGS:((str),),EXCP:Exception,RETURN:None,TARGET:log_between_modules}

#restoring log, giving it a new name
CHILD_CONTEXT_DEF["the_original_log_function"] = OLD_LOG


# Dispatch
secure_dispatch_module()
21 changes: 21 additions & 0 deletions seclayer.r2py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
TYPE="type"
ARGS="args"
RETURN="return"
EXCP="exceptions"
TARGET="target"
FUNC="func"
OBJC="objc"


# OLD_SLEEP = CHILD_CONTEXT_DEF["log"]

# dy_import_module_symbols("oldlog.r2py")




# Mapping our function to sleep()
# CHILD_CONTEXT_DEF["log"] = OLD_SLEEP

# Dispatch
secure_dispatch_module()
37 changes: 17 additions & 20 deletions strace.py → strace.r2py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""
Author: Armon Dadgar
Description:
This namespace can be used as an intermediary logging namespace to
log calls to the Repy API functions.
strace.r2py is a diagnostic, debugging utility for for the repyV2 api
with similar api calls that is used as an intermediary logging namespace to
log calls of the Repy API functions.

Header:
Call-time function [instance] [args] [ = result ]
"""

# These API functions do _not_ return an object, and can be handled uniformly.
Expand Down Expand Up @@ -35,6 +39,9 @@ def getruntime():
return round(_orig_getruntime(), 5)





# Do a traced function call
def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print_result=True):
# Store the time, function call and arguments
Expand All @@ -54,7 +61,7 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print
# Print if there is no return
if no_return:
PRINT_LOCK.acquire(True)
print call_string
log(call_string)
PRINT_LOCK.release()

# Get the result
Expand All @@ -64,7 +71,7 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print
# On an exception, print the call at least
except Exception, e:
PRINT_LOCK.acquire(True)
print call_string,"->",str(type(e))+" "+str(e)
log(call_string + "->" + str(type(e)) + " " + str(e))
PRINT_LOCK.release()
raise

Expand All @@ -80,9 +87,8 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print
call_string += " = " + str_result

PRINT_LOCK.acquire(True)
print call_string
log(call_string)
PRINT_LOCK.release()

return result


Expand Down Expand Up @@ -147,6 +153,7 @@ def writeat(self,*args,**kwargs):
return traced_call(self.fileo,"file.writeat",self.fileo.writeat,args,kwargs,True)


# This class is used for VNObjects
class VNObj():
# Store the virt object
def __init__(self,virt):
Expand Down Expand Up @@ -210,7 +217,6 @@ def wrapped_listenforconnection(*args, **kwargs):
# Return the socket object
return TCPServerObj(sock)


# Wrap the call to createlock
def wrapped_createlock(*args,**kwargs):
# Trace the call to get the lock
Expand All @@ -227,7 +233,6 @@ def wrapped_openfile(*args,**kwargs):
# Return the wrapped object
return FileObj(fileo)


# Wrap the call to createvirtualnamespace
def wrapped_virtual_namespace(*args,**kwargs):
# Trace the call to get the object
Expand All @@ -239,33 +244,25 @@ def wrap_all():
# Handle the normal calls
for call in NON_OBJ_API_CALLS:
CHILD_CONTEXT[call] = NonObjAPICall(call).call

# Wrap openconnection
CHILD_CONTEXT["openconnection"] = wrapped_openconnection

# Wrap listenformessage
CHILD_CONTEXT["listenformessage"] = wrapped_listenformessage

# Wrap listenforconnection
CHILD_CONTEXT["listenforconnection"] = wrapped_listenforconnection

# Wrap createlock
CHILD_CONTEXT["createlock"] = wrapped_createlock

# Wrap openfile
CHILD_CONTEXT["openfile"] = wrapped_openfile

# Wrap createvirtualnamespace
CHILD_CONTEXT["createvirtualnamespace"] = wrapped_virtual_namespace


# Wrap all the functions
wrap_all()

# Print the header
print "Call-time function [instance] [args] [ = result ]"

# Dispatch the next module
dy_dispatch_module()
# # Wrap all the functions
wrap_all()


# Dispatch the next module
dy_dispatch_module()
14 changes: 14 additions & 0 deletions tests/ut_seattlelib_log_between_modules_test.r2py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py


log("This is a test")
libfile = dy_import_module("librepyfile.r2py")

#open traced file
librepyfile = libfile.open("tracer.txt")
tracedcall = librepyfile.readline()
librepyfile.close()

if("This is a test" not in tracedcall):
the_original_log_function("Log between modules does not work openconnection")

180 changes: 180 additions & 0 deletions tests/ut_seattlelib_strace_fileobj.r2py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
"""
<Purpose>
this unit test checks the functionality of the file traced object
Function Tested:
openfile(filename, create)
file.close()
file.readat(sizelimit, offset)
file.writeat(data, offset)
"""

#pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py


#sleep is because there is threading issue between namespace initilization and start of UT
sleep(5)

#librepyfile is used for 2 files handles
#one file handle is for the ut (reading)
#the other is for the security module (writing)
#data is passed between security module and unit test
libfile = dy_import_module("librepyfile.r2py")

#open file using trace call
fielobj = openfile("testfile.txt", True)

#open traced file
librepyfile = libfile.open("tracer.txt")
tracedcall = librepyfile.readline()
librepyfile.close()

"""
<Expected return example>
1.42788 openfile ('testfile.txt', True) = <Namespace wrapped file:
<emulfile.emulated_file object at 0x7fa2e91808e8>>
"""

#Check apicall to traced call
if("openfile ('testfile.txt', True)" not in tracedcall):
the_original_log_function("Trace api call failed to trace correct \
function, openfile ('testfile.txt', True)")

if("emulfile.emulated_file object"not in tracedcall):
the_original_log_function("Failed to trace trace correct function \
as correct object")


#write something using trace call
fielobj.writeat("asdfasdf",0)

#open traced file
librepyfile = libfile.open("tracer.txt")
tracedcall = librepyfile.readline()
librepyfile.close()

"""
<Expected return example>
1.43081 file.writeat <Namespace wrapped file:
<emulfile.emulated_file object at 0x7fa2e91808e8>> ('asdfasdf', 0)
"""

#Check apicall to traced call
if("file.writeat" not in tracedcall):
the_original_log_function("Trace api call failed to trace correct \
function, writeat")

if("(\'asdfasdf\', 0)" not in tracedcall):
the_original_log_function("Trace api call failed to trace correct \
function with correct parameters, fielobj.writeat(\'asdfasdf\',0)")

if("emulfile.emulated_file object"not in tracedcall):
the_original_log_function("Failed to trace trace correct function \
as correct object")



#close file using trace call
fielobj.close()

#open traced file
librepyfile = libfile.open("tracer.txt")
tracedcall = librepyfile.readline()
librepyfile.close()

"""
<Expected return example>
1.43271 file.close <Namespace wrapped file:
emulfile.emulated_file object at 0x7fa2e91808e8>>>> ('asdfasdf', 0)
"""

#Check apicall to traced call
if("file.close" not in tracedcall):
the_original_log_function("Trace api call failed to trace \
correct function, close")

if("emulfile.emulated_file object"not in tracedcall):
the_original_log_function("Failed to trace trace correct \
function as correct object")


#open existing file with trace call
fielobj = openfile("testfile.txt", True)

#open traced file
librepyfile = libfile.open("tracer.txt")
tracedcall = librepyfile.readline()
librepyfile.close()

"""
<Expected return example>
1.43453 openfile ('testfile.txt', True) = <Namespace wrapped file:
<emulfile.emulated_file object at 0x7fa2e9180940>>)
"""

#Check apicall to traced call
if("openfile" not in tracedcall):
the_original_log_function("Trace api call failed to trace \
correct function, openfile")

if("(\'testfile.txt\', True)" not in tracedcall):
the_original_log_function("Trace api call failed to trace correct \
function with correct parameters, openfile(\'testfile.txt\', True)")

if("emulfile.emulated_file object"not in tracedcall):
the_original_log_function("Failed to trace trace correct \
function as correct object")

#read from file usiung trace call
fielobj.readat(0,4)

#open traced file
librepyfile = libfile.open("tracer.txt")
tracedcall = librepyfile.readline()
librepyfile.close()


"""
<Expected return example>
1.43618 file.readat <Namespace wrapped file:
<emulfile.emulated_file object at 0x7fa2e9180940>> (0, 4) = 0940>>)
"""

#Check apicall to traced call
if("file.readat" not in tracedcall):
the_original_log_function("Trace api call failed to trace \
correct function, readat")

if("(0, 4)" not in tracedcall):
the_original_log_function("Trace api call failed to trace correct\
function with correct parameters, fileobj.readat (0, 4)")

if("emulfile.emulated_file object"not in tracedcall):
the_original_log_function("Failed to trace trace correct \
function as correct object")

#close file using trace call
fielobj.close()

#open traced file
librepyfile = libfile.open("tracer.txt")
tracedcall = librepyfile.readline()
librepyfile.close()

"""
<Expected return example>
1.43795 file.close <Namespace wrapped file:
<emulfile.emulated_file object at 0x7fa2e9180940>>> (0, 4) = 0940>>)
"""

#Check apicall to traced call
if("file.close" not in tracedcall):
the_original_log_function("Trace api call failed to \
trace correct function, close")

if("emulfile.emulated_file object"not in tracedcall):
the_original_log_function("Failed to trace trace \
correct function as correct object")


#gets tested in Ut_stattlelib_strace_nonobjapi.r2py
removefile("testfile.txt")
Loading