Author: Pavel Labath Date: 2021-11-19T18:00:14+01:00 New Revision: f3b7cc8bb2ea599e0c344e8ae16639667d9f0eff
URL: https://github.com/llvm/llvm-project/commit/f3b7cc8bb2ea599e0c344e8ae16639667d9f0eff DIFF: https://github.com/llvm/llvm-project/commit/f3b7cc8bb2ea599e0c344e8ae16639667d9f0eff.diff LOG: [lldb/test] Add ability to terminate connection from a gdb-client handler We were using the client socket close as a way to terminate the handler thread. But this kind of concurrent access to the same socket is not safe. It also complicates running the handler without a dedicated thread (next patch). Instead, here I add an explicit way for a packet handler to request termination. Waiting for lldb to terminate the connection would almost be sufficient, but in the pty test we want to keep the pty open so we can examine its state. Ability to disconnect at an arbitrary point may be useful for testing other aspects of lldb functionality as well. The way this works is that now each packet handler can optionally return a list of responses (instead of just one). One of those responses (it only makes sense for it to be the last one) can be a special RESPONSE_DISCONNECT object, which triggers a disconnection (via a new TerminateConnectionException). As the mock server now cleans up the connection whenever it disconnects, the pty test needs to explicitly dup(2) the descriptors in order to inspect the post-disconnect state. Differential Revision: https://reviews.llvm.org/D114156 Added: Modified: lldb/packages/Python/lldbsuite/test/gdbclientutils.py lldb/test/API/functionalities/gdb_remote_client/TestPty.py Removed: ################################################################################ diff --git a/lldb/packages/Python/lldbsuite/test/gdbclientutils.py b/lldb/packages/Python/lldbsuite/test/gdbclientutils.py index 183e6a0c597a9..7fdf18e38b385 100644 --- a/lldb/packages/Python/lldbsuite/test/gdbclientutils.py +++ b/lldb/packages/Python/lldbsuite/test/gdbclientutils.py @@ -86,6 +86,7 @@ class MockGDBServerResponder: registerCount = 40 packetLog = None + class RESPONSE_DISCONNECT: pass def __init__(self): self.packetLog = [] @@ -327,7 +328,7 @@ def qRegisterInfo(self, num): return "" def k(self): - return "" + return ["W01", self.RESPONSE_DISCONNECT] """ Raised when we receive a packet for which there is no default action. @@ -492,17 +493,20 @@ def _run(self): self._receivedData = "" self._receivedDataOffset = 0 data = None - while True: - try: + try: + while True: data = seven.bitcast_to_string(self._socket.recv()) if data is None or len(data) == 0: break self._receive(data) - except Exception as e: - print("An exception happened when receiving the response from the gdb server. Closing the client...") - traceback.print_exc() - self._socket.close_connection() - break + except self.TerminateConnectionException: + pass + except Exception as e: + print("An exception happened when receiving the response from the gdb server. Closing the client...") + traceback.print_exc() + finally: + self._socket.close_connection() + self._socket.close_server() def _receive(self, data): """ @@ -510,13 +514,10 @@ def _receive(self, data): Any leftover data is kept for parsing the next time around. """ self._receivedData += data - try: + packet = self._parsePacket() + while packet is not None: + self._handlePacket(packet) packet = self._parsePacket() - while packet is not None: - self._handlePacket(packet) - packet = self._parsePacket() - except self.InvalidPacketException: - self._socket.close_connection() def _parsePacket(self): """ @@ -583,6 +584,9 @@ def _parsePacket(self): self._receivedDataOffset = 0 return packet + def _sendPacket(self, packet): + self._socket.sendall(seven.bitcast_to_bytes(frame_packet(packet))) + def _handlePacket(self, packet): if packet is self.PACKET_ACK: # Ignore ACKs from the client. For the future, we can consider @@ -600,14 +604,18 @@ def _handlePacket(self, packet): elif self.responder is not None: # Delegate everything else to our responder response = self.responder.respond(packet) - # Handle packet framing since we don't want to bother tests with it. - if response is not None: - framed = frame_packet(response) - self._socket.sendall(seven.bitcast_to_bytes(framed)) + if not isinstance(response, list): + response = [response] + for part in response: + if part is MockGDBServerResponder.RESPONSE_DISCONNECT: + raise self.TerminateConnectionException() + self._sendPacket(part) PACKET_ACK = object() PACKET_INTERRUPT = object() - class InvalidPacketException(Exception): + class TerminateConnectionException(Exception): pass + class InvalidPacketException(Exception): + pass diff --git a/lldb/test/API/functionalities/gdb_remote_client/TestPty.py b/lldb/test/API/functionalities/gdb_remote_client/TestPty.py index 6f266aa6558ef..fe9d98faa3102 100644 --- a/lldb/test/API/functionalities/gdb_remote_client/TestPty.py +++ b/lldb/test/API/functionalities/gdb_remote_client/TestPty.py @@ -12,10 +12,14 @@ class TestPty(GDBRemoteTestBase): def get_term_attrs(self): import termios - return termios.tcgetattr(self.server._socket._secondary) + return termios.tcgetattr(self._secondary_socket) def setUp(self): super().setUp() + # Duplicate the pty descriptors so we can inspect the pty state after + # they are closed + self._primary_socket = os.dup(self.server._socket._primary.name) + self._secondary_socket = os.dup(self.server._socket._secondary.name) self.orig_attr = self.get_term_attrs() def assert_raw_mode(self, current_attr): _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits