[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
This commit is contained in:
Pavel Labath 2021-11-18 13:56:36 +01:00
parent 28000587e1
commit f3b7cc8bb2
2 changed files with 32 additions and 20 deletions

View File

@ -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

View File

@ -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):