diff --git a/dashmips/__main__.py b/dashmips/__main__.py index 96d00a6..24240d3 100644 --- a/dashmips/__main__.py +++ b/dashmips/__main__.py @@ -23,7 +23,7 @@ def main_compile(args): def main_debug(args): """Start debug server for mips.""" - debug_mips(host=args.host, port=args.port) + debug_mips(host=args.host, port=args.port, log=args.log) def main(): @@ -34,7 +34,9 @@ def main(): parser.add_argument('-v', '--version', action='version', version='0.0.1') - sbp = parser.add_subparsers(title='commands', dest='command', required=True) + sbp = parser.add_subparsers( + title='commands', dest='command', required=True + ) compileparse = sbp.add_parser('compile', aliases=['c']) debugparse = sbp.add_parser('debug', aliases=['d']) @@ -60,6 +62,10 @@ def main(): debugparse.add_argument( '-i', '--host', default='0.0.0.0', help='run debugger on host' ) + debugparse.add_argument( + '-l', '--log', dest='log', + action='store_true', help='Log all network traffic' + ) debugparse.set_defaults(func=main_debug) prog_args = parser.parse_args() diff --git a/dashmips/debugserver.py b/dashmips/debugserver.py index 2016fba..ecd73ea 100644 --- a/dashmips/debugserver.py +++ b/dashmips/debugserver.py @@ -21,10 +21,12 @@ class DebugMessage: error: bool = False def __post_init__(self): + """Ensure unique breakpoints.""" # set to remove duplicates and sort self.breakpoints = sorted(set(self.breakpoints)) def __iter__(self): + """Make DebugMessage castable to dict.""" return iter(asdict(self).items()) @staticmethod @@ -54,7 +56,8 @@ def respond(self, msg: DebugMessage): msg_to_send = json.dumps(dict(msg)).encode('utf8') self.wfile.write(msg_to_send + b'\r\n\r\n') self.wfile.flush() - print(f"{self.client_address}: Respond {msg}") + if self.server.log: + print(f"{self.client_address}: Respond {msg}") def receive(self) -> Optional[DebugMessage]: """Receive Client Command.""" @@ -62,7 +65,8 @@ def receive(self) -> Optional[DebugMessage]: msg = DebugMessage.from_dict( json.loads(self.rfile.readline().strip()) ) - print(f"{self.client_address}: Receive {msg}") + if self.server.log: + print(f"{self.client_address}: Receive {msg}") return msg except json.JSONDecodeError: return None @@ -71,7 +75,8 @@ def handle(self): """Handle Client Req.""" from dashmips.debugger import Commands try: - print(f"{self.client_address}: Connected") + if self.server.log: + print(f"{self.client_address}: Connected") msg = self.receive() if msg is None: self.respond(DebugMessage(**{ @@ -111,16 +116,19 @@ class MipsDebugServer(TCPServer): def __init__(self, server_address=('0.0.0.0', 9999), + log=False, RequestHandlerClass=MipsDebugRequestHandler, bind_and_activate=True) -> None: """Create Mips Debug Server.""" self.allow_reuse_address = True + self.log = log super().__init__( server_address, RequestHandlerClass, bind_and_activate ) - print(f"Server is listening on {self.socket.getsockname()}") + if self.log: + print(f"Server is listening on {self.socket.getsockname()}") def server_bind(self): """Set reusable address opt.""" @@ -128,9 +136,9 @@ def server_bind(self): self.socket.bind(self.server_address) -def debug_mips(host='localhost', port=9999): +def debug_mips(host='localhost', port=9999, log=False): """Create a debugging instance of mips.""" - with MipsDebugServer(server_address=(host, port)) as server: + with MipsDebugServer(server_address=(host, port), log=log) as server: try: server.allow_reuse_address = True server.serve_forever() diff --git a/dashmips/instructions/special_instructions.py b/dashmips/instructions/special_instructions.py index b8e9d91..c785cc9 100644 --- a/dashmips/instructions/special_instructions.py +++ b/dashmips/instructions/special_instructions.py @@ -4,14 +4,17 @@ def parse(arg): + """Instructions that take no arguments.""" return tuple() @mips_instruction('', parse) def nop(program): + """Do nothing.""" return True @mips_instruction('', parse) def syscall(program): + """Call syscall specified in $v0.""" return Syscalls[program.registers['$v0']](program) diff --git a/dashmips/mips.py b/dashmips/mips.py index d797617..8e4d724 100644 --- a/dashmips/mips.py +++ b/dashmips/mips.py @@ -7,9 +7,10 @@ class MipsException(Exception): - """Mips related errors""" + """Mips related errors.""" def __init__(self, message): + """Create MipsException.""" super().__init__(message) self.message = message diff --git a/dashmips/preprocessor.py b/dashmips/preprocessor.py index e855449..6275592 100644 --- a/dashmips/preprocessor.py +++ b/dashmips/preprocessor.py @@ -88,7 +88,8 @@ def preprocess(file: TextIO) -> MipsProgram: processed_code = code_labels(labels, unprocessed_code) source = [ - SourceLine(file.name, lineno=ol[0], line=ol[1]) for ol in processed_code + SourceLine(file.name, lineno=ol[0], line=ol[1]) + for ol in processed_code ] assert 'main' in labels diff --git a/dashmips/run.py b/dashmips/run.py index 6f243fa..afdc710 100644 --- a/dashmips/run.py +++ b/dashmips/run.py @@ -13,6 +13,7 @@ def run(program: MipsProgram, runnable=lambda _: True): def next_instruction(program): + """Execute One Instruction.""" current_pc = program.registers['pc'] if len(program.source) < current_pc: # We jumped or executed beyond available text diff --git a/dashmips/syscalls/print_syscalls.py b/dashmips/syscalls/print_syscalls.py index 63a4889..0599e13 100644 --- a/dashmips/syscalls/print_syscalls.py +++ b/dashmips/syscalls/print_syscalls.py @@ -39,7 +39,6 @@ def _exit(program): sys.exit(program.registers['$a0']) - @mips_syscall(45) def dump_program(program): """Print json format of program."""