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 __init__(self, is_func, byte_code_cp, source, source_name, line, column, name, lines, offsets): self.is_func = bool(is_func) self.byte_code_cp = byte_code_cp self.source = re.split(b"\r\n|[\r\n]", source) if source_name: self.source_name = os.path.relpath(source_name, jerry_encode(os.getcwd())) self.source_name = self.source_name.replace(b'\\', b'/') else: self.source_name = source_name self.name = name self.lines = {} self.offsets = {} self.line = line self.column = column self.first_breakpoint_line = lines[0] self.first_breakpoint_offset = offsets[0] if len(self.source) > 1 and not self.source[-1]: self.source.pop() for i, _line in enumerate(lines): offset = offsets[i] breakpoint = JerryBreakpoint(_line, offset, self) self.lines[_line] = breakpoint self.offsets[offset] = breakpoint
def _form_table(self, table): result = "" col_width = [max(len(x) for x in col) for col in zip(*table)] for line in table: result += " | ".join("{:{}}".format(x, col_width[i]) for i, x in enumerate(line)) + " \n" return jerry_encode(result)
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 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_string(self, args_text, message_type, index=0): # 1: length of type byte # 4: length of an uint32 value message_header = 1 + 4 args = jerry_encode(args_text) # Add scope chain index if message_type == JERRY_DEBUGGER_EVAL: args = struct.pack(self.byte_order + "I", index) + args size = len(args) max_fragment = min(self.max_message_size - message_header, size) message = struct.pack(self.byte_order + "BBI", max_fragment + message_header, message_type, size) if size == max_fragment: self.channel.send_message(self.byte_order, message + args) return self.channel.send_message(self.byte_order, message + args[0:max_fragment]) offset = max_fragment if message_type == JERRY_DEBUGGER_EVAL: message_type = JERRY_DEBUGGER_EVAL_PART else: message_type = JERRY_DEBUGGER_CLIENT_SOURCE_PART # 1: length of type byte message_header = 1 max_fragment = self.max_message_size - message_header while offset < size: next_fragment = min(max_fragment, size - offset) message = struct.pack(self.byte_order + "BB", next_fragment + message_header, message_type) prev_offset = offset offset += next_fragment self.channel.send_message(self.byte_order, message + args[prev_offset:offset])
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 do_break(self, args_text): """ Insert breakpoints on the given lines or functions """ args = jerry_encode(args_text) write(self.debugger.set_break(args))
def precmd(self, line): self.stop = False if self.debugger.non_interactive: writeln(jerry_encode(line)) return line
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)