Author: Pavel Labath Date: 2021-04-13T17:10:32+02:00 New Revision: 872b1da6ad276515ccfb1481009d23b129c72cac
URL: https://github.com/llvm/llvm-project/commit/872b1da6ad276515ccfb1481009d23b129c72cac DIFF: https://github.com/llvm/llvm-project/commit/872b1da6ad276515ccfb1481009d23b129c72cac.diff LOG: [lldb/test] s/add_no_ack_remote_stream/do_handshake These two functions are doing the same thing, only one of them is sending the packets immediately and the other "queues" them to be sent later. The first one is better as in case of errors, the backtrace will point straight to the place that caused them. Modify the first method to avoid duplication, and ten standardize on it. Added: Modified: lldb/packages/Python/lldbsuite/test/tools/lldb-server/gdbremote_testcase.py lldb/packages/Python/lldbsuite/test/tools/lldb-server/lldbgdbserverutils.py lldb/test/API/tools/lldb-server/TestAppleSimulatorOSType.py lldb/test/API/tools/lldb-server/TestGdbRemoteAttachOrWait.py lldb/test/API/tools/lldb-server/TestGdbRemoteAttachWait.py lldb/test/API/tools/lldb-server/TestGdbRemoteCompletion.py lldb/test/API/tools/lldb-server/TestGdbRemoteHostInfo.py lldb/test/API/tools/lldb-server/TestLldbGdbServer.py lldb/test/API/tools/lldb-server/commandline/TestGdbRemoteConnection.py Removed: ################################################################################ diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-server/gdbremote_testcase.py b/lldb/packages/Python/lldbsuite/test/tools/lldb-server/gdbremote_testcase.py index 5964b24203757..5451877a7bdd4 100644 --- a/lldb/packages/Python/lldbsuite/test/tools/lldb-server/gdbremote_testcase.py +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-server/gdbremote_testcase.py @@ -521,8 +521,9 @@ def prep_debug_monitor_and_inferior( server = self.connect_to_debug_monitor(attach_pid=attach_pid) self.assertIsNotNone(server) + self.do_handshake() + # Build the expected protocol stream - self.add_no_ack_remote_stream() if inferior_env: for name, value in inferior_env.items(): self.add_set_environment_packets(name, value) @@ -531,60 +532,13 @@ def prep_debug_monitor_and_inferior( return {"inferior": inferior, "server": server} - def expect_socket_recv( - self, - sock, - expected_content_regex - ): - response = "" - timeout_time = time.time() + self.DEFAULT_TIMEOUT - - while not expected_content_regex.match( - response) and time.time() < timeout_time: - can_read, _, _ = select.select([sock], [], [], self.DEFAULT_TIMEOUT) - if can_read and sock in can_read: - recv_bytes = sock.recv(4096) - if recv_bytes: - response += seven.bitcast_to_string(recv_bytes) - - self.assertTrue(expected_content_regex.match(response)) - - def expect_socket_send(self, sock, content): - request_bytes_remaining = content - timeout_time = time.time() + self.DEFAULT_TIMEOUT - - while len(request_bytes_remaining) > 0 and time.time() < timeout_time: - _, can_write, _ = select.select([], [sock], [], self.DEFAULT_TIMEOUT) - if can_write and sock in can_write: - written_byte_count = sock.send(request_bytes_remaining.encode()) - request_bytes_remaining = request_bytes_remaining[ - written_byte_count:] - self.assertEqual(len(request_bytes_remaining), 0) - - def do_handshake(self, stub_socket): - # Write the ack. - self.expect_socket_send(stub_socket, "+") - - # Send the start no ack mode packet. - NO_ACK_MODE_REQUEST = "$QStartNoAckMode#b0" - bytes_sent = stub_socket.send(NO_ACK_MODE_REQUEST.encode()) - self.assertEqual(bytes_sent, len(NO_ACK_MODE_REQUEST)) - - # Receive the ack and "OK" - self.expect_socket_recv(stub_socket, re.compile( - r"^\+\$OK#[0-9a-fA-F]{2}$")) - - # Send the final ack. - self.expect_socket_send(stub_socket, "+") - - def add_no_ack_remote_stream(self): - self.test_sequence.add_log_lines( - ["read packet: +", - "read packet: $QStartNoAckMode#b0", - "send packet: +", - "send packet: $OK#9a", - "read packet: +"], - True) + def do_handshake(self): + server = self._server + server.send_ack() + server.send_packet(b"QStartNoAckMode") + self.assertEqual(server.get_normal_packet(), b"+") + self.assertEqual(server.get_normal_packet(), b"OK") + server.send_ack() def add_verified_launch_packets(self, launch_args): self.test_sequence.add_log_lines( diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-server/lldbgdbserverutils.py b/lldb/packages/Python/lldbsuite/test/tools/lldb-server/lldbgdbserverutils.py index eba6f322db9b3..a769cb1226fc4 100644 --- a/lldb/packages/Python/lldbsuite/test/tools/lldb-server/lldbgdbserverutils.py +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-server/lldbgdbserverutils.py @@ -912,6 +912,19 @@ def __init__(self, sock, proc = None): def send_raw(self, frame): self._sock.sendall(frame) + def send_ack(self): + self.send_raw(b"+") + + def send_packet(self, packet): + self.send_raw(b'$%s#%02x'%(packet, self._checksum(packet))) + + @staticmethod + def _checksum(packet): + checksum = 0 + for c in six.iterbytes(packet): + checksum += c + return checksum % 256 + def _read(self, q): while not q: new_bytes = self._sock.recv(4096) @@ -962,6 +975,19 @@ def get_raw_output_packet(self): def get_raw_normal_packet(self): return self._read(self._normal_queue) + @staticmethod + def _get_payload(frame): + payload = frame[1:-3] + checksum = int(frame[-2:], 16) + if checksum != Server._checksum(payload): + raise ChecksumMismatch + return payload + + def get_normal_packet(self): + frame = self.get_raw_normal_packet() + if frame == b"+": return frame + return self._get_payload(frame) + def get_accumulated_output(self): return self._accumulated_output diff --git a/lldb/test/API/tools/lldb-server/TestAppleSimulatorOSType.py b/lldb/test/API/tools/lldb-server/TestAppleSimulatorOSType.py index 467e6cdc69766..dc5785fd49a9e 100644 --- a/lldb/test/API/tools/lldb-server/TestAppleSimulatorOSType.py +++ b/lldb/test/API/tools/lldb-server/TestAppleSimulatorOSType.py @@ -94,7 +94,7 @@ def check_simulator_ostype(self, sdk, platform_name, arch=platform.machine()): server = self.connect_to_debug_monitor(attach_pid=pid) # Setup packet sequences - self.add_no_ack_remote_stream() + self.do_handshake() self.add_process_info_collection_packets() self.test_sequence.add_log_lines( ["read packet: " + diff --git a/lldb/test/API/tools/lldb-server/TestGdbRemoteAttachOrWait.py b/lldb/test/API/tools/lldb-server/TestGdbRemoteAttachOrWait.py index c91e94b7b30d0..dd71e5077d47c 100644 --- a/lldb/test/API/tools/lldb-server/TestGdbRemoteAttachOrWait.py +++ b/lldb/test/API/tools/lldb-server/TestGdbRemoteAttachOrWait.py @@ -65,7 +65,7 @@ def test_launch_after_attach_with_vAttachOrWait(self): server = self.connect_to_debug_monitor() self.assertIsNotNone(server) - self.add_no_ack_remote_stream() + self.do_handshake() self.test_sequence.add_log_lines([ # Do the attach. "read packet: $vAttachOrWait;{}#00".format(lldbgdbserverutils.gdbremote_hex_encode_string(exe)), diff --git a/lldb/test/API/tools/lldb-server/TestGdbRemoteAttachWait.py b/lldb/test/API/tools/lldb-server/TestGdbRemoteAttachWait.py index d2ba674a8be6b..deb82ed5183b1 100644 --- a/lldb/test/API/tools/lldb-server/TestGdbRemoteAttachWait.py +++ b/lldb/test/API/tools/lldb-server/TestGdbRemoteAttachWait.py @@ -36,7 +36,7 @@ def launch_inferior(): # Launch the first inferior (we shouldn't attach to this one). launch_inferior() - self.add_no_ack_remote_stream() + self.do_handshake() self.test_sequence.add_log_lines([ # Do the attach. "read packet: $vAttachWait;{}#00".format(lldbgdbserverutils.gdbremote_hex_encode_string(exe)), diff --git a/lldb/test/API/tools/lldb-server/TestGdbRemoteCompletion.py b/lldb/test/API/tools/lldb-server/TestGdbRemoteCompletion.py index 22af21d132da8..064aefe8d94cd 100644 --- a/lldb/test/API/tools/lldb-server/TestGdbRemoteCompletion.py +++ b/lldb/test/API/tools/lldb-server/TestGdbRemoteCompletion.py @@ -29,7 +29,7 @@ def init_lldb_server(self): self.sock = self.create_socket() self._server = Server(self.sock, server) - self.add_no_ack_remote_stream() + self.do_handshake() def generate_hex_path(self, target): return str(os.path.join(self.getBuildDir(), target)).encode().hex() diff --git a/lldb/test/API/tools/lldb-server/TestGdbRemoteHostInfo.py b/lldb/test/API/tools/lldb-server/TestGdbRemoteHostInfo.py index 6a4bdc1bb9c4c..01c2a38e2757e 100644 --- a/lldb/test/API/tools/lldb-server/TestGdbRemoteHostInfo.py +++ b/lldb/test/API/tools/lldb-server/TestGdbRemoteHostInfo.py @@ -77,7 +77,7 @@ def get_qHostInfo_response(self): # Launch the debug monitor stub, attaching to the inferior. server = self.connect_to_debug_monitor() self.assertIsNotNone(server) - self.add_no_ack_remote_stream() + self.do_handshake() # Request qHostInfo and get response self.add_host_info_collection_packets() diff --git a/lldb/test/API/tools/lldb-server/TestLldbGdbServer.py b/lldb/test/API/tools/lldb-server/TestLldbGdbServer.py index 7090abf00fc5a..02bd3637906b4 100644 --- a/lldb/test/API/tools/lldb-server/TestLldbGdbServer.py +++ b/lldb/test/API/tools/lldb-server/TestLldbGdbServer.py @@ -28,7 +28,7 @@ def test_thread_suffix_supported(self): server = self.connect_to_debug_monitor() self.assertIsNotNone(server) - self.add_no_ack_remote_stream() + self.do_handshake() self.test_sequence.add_log_lines( ["lldb-server < 26> read packet: $QThreadSuffixSupported#e4", "lldb-server < 6> send packet: $OK#9a"], @@ -41,7 +41,7 @@ def test_list_threads_in_stop_reply_supported(self): server = self.connect_to_debug_monitor() self.assertIsNotNone(server) - self.add_no_ack_remote_stream() + self.do_handshake() self.test_sequence.add_log_lines( ["lldb-server < 27> read packet: $QListThreadsInStopReply#21", "lldb-server < 6> send packet: $OK#9a"], diff --git a/lldb/test/API/tools/lldb-server/commandline/TestGdbRemoteConnection.py b/lldb/test/API/tools/lldb-server/commandline/TestGdbRemoteConnection.py index a82f4a8279b3e..514e5539fd948 100644 --- a/lldb/test/API/tools/lldb-server/commandline/TestGdbRemoteConnection.py +++ b/lldb/test/API/tools/lldb-server/commandline/TestGdbRemoteConnection.py @@ -1,12 +1,13 @@ from __future__ import print_function import gdbremote_testcase +import random import select import socket from lldbsuite.test.decorators import * from lldbsuite.test.lldbtest import * +from lldbgdbserverutils import Server import lldbsuite.test.lldbplatformutil -import random if lldbplatformutil.getHostPlatform() == "windows": import ctypes @@ -132,7 +133,7 @@ def test_reverse_connect(self): # Reverse connect is the default connection method. self.connect_to_debug_monitor() # Verify we can do the handshake. If that works, we'll call it good. - self.do_handshake(self.sock) + self.do_handshake() @skipIfRemote def test_named_pipe(self): @@ -165,6 +166,7 @@ def test_named_pipe(self): # Trim null byte, convert to int addr = (addr[0], int(port[:-1])) self.sock.connect(addr) + self._server = Server(self.sock, server) # Verify we can do the handshake. If that works, we'll call it good. - self.do_handshake(self.sock) + self.do_handshake() _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits