Skip to content

Commit

Permalink
tracing in exec
Browse files Browse the repository at this point in the history
  • Loading branch information
therkels committed Nov 15, 2024
1 parent b7a7e23 commit 0f50b85
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
6 changes: 6 additions & 0 deletions jac/examples/ginsScripts/simple.jac
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
can foo() {
return 1;
}

with entry {
foo();
a=0;
x=0;
if x >= 0{
a=1;
}
a=-1;
print("hello");
}
57 changes: 55 additions & 2 deletions jac/jaclang/runtimelib/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,56 @@ def get_caller_dir(self) -> str:
chomp_target = chomp_target[1:]
return path.join(caller_dir, self.dir_path)


class CFGTracker:
def __init__(self):
self.count = 0
def start_tracking(self):
"""Start tracking branch coverage"""
sys.settrace(self.trace_callback)
def stop_tracking(self):
"""Stop tracking branch coverage"""
sys.settrace(None)

def trace_callback(self, frame: types.FrameType, event: str, arg: Any) -> Optional[types.TraceFunction]:
"""Trace function to track executed branches"""
if event != 'line':
return self.trace_callback
code = frame.f_code
print(f"{frame.f_lineno}")
# print(f"Function Name: {code.co_name}")
# print(f"Filename: {code.co_filename}")
# print(f"First Line Number: {code.co_firstlineno}")
# print(f"Argument Count: {code.co_argcount}")
# print(f"Constants: {code.co_consts}")
# print(f"Local Variables: {code.co_varnames}")
self.count += 1

# if event != 'line':
# return self.trace_callback

# code = frame.f_code
# if code not in self.cfg_cache:
# self.build_cfg(code)

# # Find current basic block
# blocks = self.cfg_cache[code]
# current_offset = frame.f_lasti

# # Find the block containing this offset
# current_block = None
# for block in blocks.values():
# if block.offset <= current_offset <= block.offset + sum(inst.size for inst in block.instructions):
# current_block = block
# break

# if current_block:
# current_block.hits += 1
# # Record taken branches
# for next_block in current_block.next:
# self.coverage_data[code].add(
# (current_block.offset, next_block.offset))

# return self.trace_callback
class ImportReturn:
"""Import Return Object."""

Expand Down Expand Up @@ -153,7 +202,6 @@ def load_jac_mod_as_item(
)
if not codeobj:
raise ImportError(f"No bytecode found for {jac_file_path}")

exec(codeobj, new_module.__dict__)
return getattr(new_module, name, new_module)
except ImportError as e:
Expand Down Expand Up @@ -351,7 +399,12 @@ def run_import(

try:
with sys_path_context(spec.caller_dir):
tracker = CFGTracker()
tracker.start_tracking()
print("here")
exec(codeobj, module.__dict__)
tracker.stop_tracking()
print(f"count={tracker.count}")
except Exception as e:
logger.error(e)
logger.error(dump_traceback(e))
Expand Down

0 comments on commit 0f50b85

Please sign in to comment.