1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 # This file is based on gdb_rsp.py file from NaCl repository.
12 def RspChecksum(data
):
15 checksum
= (checksum
+ ord(char
)) % 0x100
19 class EofOnReplyException(Exception):
24 class GdbRspConnection(object):
26 def __init__(self
, addr
):
27 self
._socket
= self
._Connect
(addr
)
29 def _Connect(self
, addr
):
30 # We have to poll because we do not know when sel_ldr has
31 # successfully done bind() on the TCP port. This is inherently
33 # TODO(mseaborn): Add a more reliable connection mechanism to
34 # sel_ldr's debug stub.
35 timeout_in_seconds
= 10
36 poll_time_in_seconds
= 0.1
37 for i
in xrange(int(timeout_in_seconds
/ poll_time_in_seconds
)):
38 # On Mac OS X, we have to create a new socket FD for each retry.
39 sock
= socket
.socket()
43 # Retry after a delay.
44 time
.sleep(poll_time_in_seconds
)
47 raise Exception('Could not connect to sel_ldr\'s debug stub in %i seconds'
53 data
= self
._socket
.recv(1024)
56 raise EofOnReplyException()
57 raise AssertionError('EOF on socket reached with '
58 'incomplete reply message: %r' % reply
)
62 match
= re
.match('\+\$([^#]*)#([0-9a-fA-F]{2})$', reply
)
64 raise AssertionError('Unexpected reply message: %r' % reply
)
65 reply_body
= match
.group(1)
66 checksum
= match
.group(2)
67 expected_checksum
= '%02x' % RspChecksum(reply_body
)
68 if checksum
!= expected_checksum
:
69 raise AssertionError('Bad RSP checksum: %r != %r' %
70 (checksum
, expected_checksum
))
71 # Send acknowledgement.
72 self
._socket
.send('+')
75 # Send an rsp message, but don't wait for or expect a reply.
76 def RspSendOnly(self
, data
):
77 msg
= '$%s#%02x' % (data
, RspChecksum(data
))
78 return self
._socket
.send(msg
)
80 def RspRequest(self
, data
):
81 self
.RspSendOnly(data
)
82 return self
._GetReply
()
84 def RspInterrupt(self
):
85 self
._socket
.send('\x03')
86 return self
._GetReply
()