 Chromium Code Reviews
 Chromium Code Reviews Issue 11236025:
  Test that debug stub works with browser.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src/
    
  
    Issue 11236025:
  Test that debug stub works with browser.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src/| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2012 The Native Client Authors. All rights reserved. | |
| 
Mark Seaborn
2012/10/23 01:14:50
Shouldn't have "The Native Client Authors" in the
 
halyavin
2012/10/23 11:59:29
Done.
 | |
| 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 SEL_LDR_RSP_SOCKET_ADDR = ('localhost', 4014) | |
| 11 | |
| 12 | |
| 13 def EnsurePortIsAvailable(addr=SEL_LDR_RSP_SOCKET_ADDR): | |
| 
Mark Seaborn
2012/10/23 01:14:50
Please remove functions you're not using, like thi
 
halyavin
2012/10/23 11:59:29
Done.
 | |
| 14 # As a sanity check, check that the TCP port is available by binding | |
| 15 # to it ourselves (and then unbinding). Otherwise, we could end up | |
| 16 # talking to an old instance of sel_ldr that is still hanging | |
| 17 # around, or to some unrelated service that uses the same port | |
| 18 # number. Of course, there is still a race condition because an | |
| 19 # unrelated process could bind the port after we unbind. | |
| 20 sock = socket.socket() | |
| 21 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) | |
| 22 sock.bind(addr) | |
| 23 sock.close() | |
| 24 | |
| 25 | |
| 26 def RspChecksum(data): | |
| 27 checksum = 0 | |
| 28 for char in data: | |
| 29 checksum = (checksum + ord(char)) % 0x100 | |
| 30 return checksum | |
| 31 | |
| 32 | |
| 33 class GdbRspConnection(object): | |
| 34 | |
| 35 def __init__(self, addr=SEL_LDR_RSP_SOCKET_ADDR): | |
| 36 self._socket = self._Connect(addr) | |
| 37 | |
| 38 def _Connect(self, addr): | |
| 39 # We have to poll because we do not know when sel_ldr has | |
| 40 # successfully done bind() on the TCP port. This is inherently | |
| 41 # unreliable. | |
| 42 # TODO(mseaborn): Add a more reliable connection mechanism to | |
| 43 # sel_ldr's debug stub. | |
| 44 timeout_in_seconds = 10 | |
| 45 poll_time_in_seconds = 0.1 | |
| 46 for i in xrange(int(timeout_in_seconds / poll_time_in_seconds)): | |
| 47 # On Mac OS X, we have to create a new socket FD for each retry. | |
| 48 sock = socket.socket() | |
| 49 try: | |
| 50 sock.connect(addr) | |
| 51 except socket.error: | |
| 52 # Retry after a delay. | |
| 53 time.sleep(poll_time_in_seconds) | |
| 54 else: | |
| 55 return sock | |
| 56 raise Exception('Could not connect to sel_ldr\'s debug stub in %i seconds' | |
| 57 % timeout_in_seconds) | |
| 58 | |
| 59 def _GetReply(self): | |
| 60 reply = '' | |
| 61 while True: | |
| 62 data = self._socket.recv(1024) | |
| 63 if len(data) == 0: | |
| 64 raise AssertionError('EOF on socket reached with ' | |
| 65 'incomplete reply message: %r' % reply) | |
| 66 reply += data | |
| 67 if '#' in data: | |
| 68 break | |
| 69 print reply | |
| 70 match = re.match('\+\$([^#]*)#([0-9a-fA-F]{2})$', reply) | |
| 71 if match is None: | |
| 72 raise AssertionError('Unexpected reply message: %r' % reply) | |
| 73 reply_body = match.group(1) | |
| 74 checksum = match.group(2) | |
| 75 expected_checksum = '%02x' % RspChecksum(reply_body) | |
| 76 if checksum != expected_checksum: | |
| 77 raise AssertionError('Bad RSP checksum: %r != %r' % | |
| 78 (checksum, expected_checksum)) | |
| 79 # Send acknowledgement. | |
| 80 self._socket.send('+') | |
| 81 return reply_body | |
| 82 | |
| 83 # Send an rsp message, but don't wait for or expect a reply. | |
| 84 def RspSendOnly(self, data): | |
| 85 msg = '$%s#%02x' % (data, RspChecksum(data)) | |
| 86 return self._socket.send(msg) | |
| 87 | |
| 88 def RspRequest(self, data): | |
| 89 self.RspSendOnly(data) | |
| 90 return self._GetReply() | |
| 91 | |
| 92 def RspInterrupt(self): | |
| 93 self._socket.send('\x03') | |
| 94 return self._GetReply() | |
| OLD | NEW |