Skip to content

Commit

Permalink
[lldb/test] Add ability to terminate connection from a gdb-client han…
Browse files Browse the repository at this point in the history
…dler

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
  • Loading branch information
labath committed Nov 19, 2021
1 parent 2800058 commit f3b7cc8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
46 changes: 27 additions & 19 deletions lldb/packages/Python/lldbsuite/test/gdbclientutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class MockGDBServerResponder:

registerCount = 40
packetLog = None
class RESPONSE_DISCONNECT: pass

def __init__(self):
self.packetLog = []
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -492,31 +493,31 @@ 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):
"""
Collects data, parses and responds to as many packets as exist.
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):
"""
Expand Down Expand Up @@ -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
Expand All @@ -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
6 changes: 5 additions & 1 deletion lldb/test/API/functionalities/gdb_remote_client/TestPty.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit f3b7cc8

Please sign in to comment.