def do_next(self, args): """ Next breakpoint in the same code block """ self.stop = True if not args: args = 0 self.debugger.next() return try: args = int(args) if args <= 0: raise ValueError(args) while args > 0: self.debugger.next() time.sleep(0.1) while True: result = self.debugger.process_messages() res_type = result.get_type() if res_type == result.END: self.quit = True return elif res_type == result.TEXT: write(result.get_text()) elif res_type == result.PROMPT: break args -= 1 except ValueError as val_errno: writeln( jerry_encode("Error: expected a positive integer: %s" % val_errno))
def connect(self): """ Connect to a remote socket at address (host, port). The format of address depends on the address family. """ connect_info = "Connecting to: %s:%s" % (self.address[0], self.address[1]) writeln(jerry_encode(connect_info)) self.socket.connect(self.address)
def _scroll_direction(debugger, direction): """ Helper function for do_scroll """ debugger.src_offset_diff = int(max(math.floor(debugger.display / 3), 1)) if direction == "up": debugger.src_offset -= debugger.src_offset_diff else: debugger.src_offset += debugger.src_offset_diff writeln( debugger.print_source(debugger.display, debugger.src_offset)['value'])
def do_display(self, args): """ Toggle source code display after breakpoints """ if args: line_num = src_check_args(args) if line_num >= 0: self.debugger.display = line_num else: writeln( b"Non-negative integer number expected, 0 turns off this function" )
def do_scroll(self, _): """ Scroll the source up or down """ while True: key = sys.stdin.readline() if key == 'w\n': _scroll_direction(self.debugger, "up") elif key == 's\n': _scroll_direction(self.debugger, "down") elif key == 'q\n': break else: writeln(b"Invalid key")
def src_check_args(args): try: line_num = int(args) if line_num < 0: writeln(b"Error: Non-negative integer number expected") return -1 return line_num except ValueError as val_errno: writeln( jerry_encode("Error: Non-negative integer number expected: %s" % (val_errno))) return -1
def send_client_source(self): # Send no more source message if there is no source if not self.client_sources: self.send_no_more_source() return path = self.client_sources.pop(0) if not path.lower().endswith('.js'): writeln(b"Error: Javascript file expected!") sys.exit(1) return with open(path, 'r') as src_file: content = path + "\0" + src_file.read() self._send_string(content, JERRY_DEBUGGER_CLIENT_SOURCE)
def scope_variables(self, args): index = 0 if args: try: index = int(args) if index < 0: writeln(b"Error: A non negative integer number expected") return b"" except ValueError as val_errno: return b"Error: Non negative integer number expected, %s\n" % ( val_errno) message = struct.pack(self.byte_order + "BB" + self.idx_format, 1 + 4, JERRY_DEBUGGER_GET_SCOPE_VARIABLES, index) self.channel.send_message(self.byte_order, message) self.prompt = False return b""
def do_eval_at(self, args): """ Evaluate JavaScript source code at a scope chain level """ code = '' index = 0 try: args = args.split(" ", 1) index = int(args[0]) if len(args) == 2: code = args[1] if index < 0 or index > 65535: raise ValueError( "Invalid scope chain index: %d (must be between 0 and 65535)" % index) except ValueError as val_errno: writeln(jerry_encode("Error: %s" % (val_errno))) return self.debugger.eval_at(code, index) self.stop = True
def precmd(self, line): self.stop = False if self.debugger.non_interactive: writeln(jerry_encode(line)) return line
def main(): setup_stdio() args = jerry_client_main.arguments_parse() channel = None protocol = None if args.protocol == "tcp": address = None if ":" not in args.address: address = (args.address, 5001) # use default port else: host, port = args.address.split(":") address = (host, int(port)) protocol = Socket(address) elif args.protocol == "serial": from jerry_client_serial import Serial protocol = Serial(args.serial_config) else: writeln(b"Unsupported transmission protocol") return -1 if args.channel == "websocket": channel = WebSocket(protocol=protocol) elif args.channel == "rawpacket": channel = RawPacket(protocol=protocol) else: writeln(b"Unsupported communication channel") return -1 debugger = jerry_client_main.JerryDebugger(channel) debugger.non_interactive = args.non_interactive logging.debug("Connected to JerryScript") prompt = DebuggerPrompt(debugger) prompt.prompt = "(jerry-debugger) " if args.color: debugger.set_colors() if args.display: debugger.display = args.display prompt.do_display(args.display) else: prompt.stop = False if args.exception is not None: prompt.do_exception(str(args.exception)) if args.client_source: debugger.store_client_sources(args.client_source) while True: if prompt.quit: break result = debugger.process_messages() res_type = result.get_type() if res_type == result.END: break elif res_type == result.PROMPT: prompt.cmdloop() sys.stdout.flush() sys.stderr.flush() elif res_type == result.TEXT: write(result.get_text()) continue
def do_EOF(self, _): """ Exit JerryScript debugger """ writeln(b"Unexpected end of input. Connection closed.") self.debugger.quit() self.quit = True self.stop = True
def do_dump(self, args): """ Dump all of the debugger data """ if args: writeln(b"Error: No argument expected") else: jerry_pprint(self.debugger.function_list)
def do_continue(self, _): """ Continue execution """ self.debugger.do_continue() self.stop = True if not self.debugger.non_interactive: writeln(b"Press enter to stop JavaScript execution.")
result = debugger.process_messages() res_type = result.get_type() if res_type == result.END: break elif res_type == result.PROMPT: prompt.cmdloop() sys.stdout.flush() sys.stderr.flush() elif res_type == result.TEXT: write(result.get_text()) continue if __name__ == "__main__": try: main() except socket.error as error_msg: ERRNO = error_msg.errno MSG = jerry_encode(str(error_msg)) if ERRNO == 111: writeln(b"Failed to connect to the JerryScript debugger.") elif ERRNO == 32 or ERRNO == 104: writeln(b"Connection closed.") else: writeln( b"Failed to connect to the JerryScript debugger.\nError: %s" % (MSG)) sys.exit(1)