From f3b7cc8bb2ea599e0c344e8ae16639667d9f0eff Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 18 Nov 2021 13:56:36 +0100 Subject: [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 --- .../Python/lldbsuite/test/gdbclientutils.py | 46 +++++++++++++--------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'lldb/packages/Python/lldbsuite/test/gdbclientutils.py') diff --git a/lldb/packages/Python/lldbsuite/test/gdbclientutils.py b/lldb/packages/Python/lldbsuite/test/gdbclientutils.py index 183e6a0..7fdf18e 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 @@ class MockGDBServerResponder: 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 @@ class MockGDBServer: 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 @@ class MockGDBServer: 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 @@ class MockGDBServer: 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 @@ class MockGDBServer: 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 -- cgit v1.1