| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 import re |
| 6 import socket |
| 7 import time |
| 8 |
| 9 |
| 10 def RspChecksum(data): |
| 11 checksum = 0 |
| 12 for char in data: |
| 13 checksum = (checksum + ord(char)) % 0x100 |
| 14 return checksum |
| 15 |
| 16 |
| 17 class GdbRspConnection(object): |
| 18 |
| 19 def __init__(self, addr): |
| 20 self._socket = self._Connect(addr) |
| 21 |
| 22 def _Connect(self, addr): |
| 23 # We have to poll because we do not know when sel_ldr has |
| 24 # successfully done bind() on the TCP port. This is inherently |
| 25 # unreliable. |
| 26 # TODO(mseaborn): Add a more reliable connection mechanism to |
| 27 # sel_ldr's debug stub. |
| 28 timeout_in_seconds = 10 |
| 29 poll_time_in_seconds = 0.1 |
| 30 for i in xrange(int(timeout_in_seconds / poll_time_in_seconds)): |
| 31 # On Mac OS X, we have to create a new socket FD for each retry. |
| 32 sock = socket.socket() |
| 33 try: |
| 34 sock.connect(addr) |
| 35 except socket.error: |
| 36 # Retry after a delay. |
| 37 time.sleep(poll_time_in_seconds) |
| 38 else: |
| 39 return sock |
| 40 raise Exception('Could not connect to sel_ldr\'s debug stub in %i seconds' |
| 41 % timeout_in_seconds) |
| 42 |
| 43 def _GetReply(self): |
| 44 reply = '' |
| 45 while True: |
| 46 data = self._socket.recv(1024) |
| 47 if len(data) == 0: |
| 48 raise AssertionError('EOF on socket reached with ' |
| 49 'incomplete reply message: %r' % reply) |
| 50 reply += data |
| 51 if '#' in data: |
| 52 break |
| 53 match = re.match('\+\$([^#]*)#([0-9a-fA-F]{2})$', reply) |
| 54 if match is None: |
| 55 raise AssertionError('Unexpected reply message: %r' % reply) |
| 56 reply_body = match.group(1) |
| 57 checksum = match.group(2) |
| 58 expected_checksum = '%02x' % RspChecksum(reply_body) |
| 59 if checksum != expected_checksum: |
| 60 raise AssertionError('Bad RSP checksum: %r != %r' % |
| 61 (checksum, expected_checksum)) |
| 62 # Send acknowledgement. |
| 63 self._socket.send('+') |
| 64 return reply_body |
| 65 |
| 66 # Send an rsp message, but don't wait for or expect a reply. |
| 67 def RspSendOnly(self, data): |
| 68 msg = '$%s#%02x' % (data, RspChecksum(data)) |
| 69 return self._socket.send(msg) |
| 70 |
| 71 def RspRequest(self, data): |
| 72 self.RspSendOnly(data) |
| 73 return self._GetReply() |
| 74 |
| 75 def RspInterrupt(self): |
| 76 self._socket.send('\x03') |
| 77 return self._GetReply() |
| OLD | NEW |