Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / nacl / gdb_rsp.py
blob7882517a337909b7b59bb8cdf8205fcd1337a33f
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.
7 import re
8 import socket
9 import time
12 def RspChecksum(data):
13 checksum = 0
14 for char in data:
15 checksum = (checksum + ord(char)) % 0x100
16 return checksum
19 class EofOnReplyException(Exception):
21 pass
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
32 # unreliable.
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()
40 try:
41 sock.connect(addr)
42 except socket.error:
43 # Retry after a delay.
44 time.sleep(poll_time_in_seconds)
45 else:
46 return sock
47 raise Exception('Could not connect to sel_ldr\'s debug stub in %i seconds'
48 % timeout_in_seconds)
50 def _GetReply(self):
51 reply = ''
52 while True:
53 data = self._socket.recv(1024)
54 if len(data) == 0:
55 if reply == '+':
56 raise EofOnReplyException()
57 raise AssertionError('EOF on socket reached with '
58 'incomplete reply message: %r' % reply)
59 reply += data
60 if '#' in data:
61 break
62 match = re.match('\+\$([^#]*)#([0-9a-fA-F]{2})$', reply)
63 if match is None:
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('+')
73 return reply_body
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()