-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgdb.py
133 lines (108 loc) · 2.94 KB
/
gdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#-----------------------------------------------------------------------------
"""
GDB Server
"""
#-----------------------------------------------------------------------------
import socket
#-----------------------------------------------------------------------------
"""
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 32 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'a'
s.bind((TCP_IP, TCP_PORT))
print 'b'
s.listen(1)
print 'c'
conn, addr = s.accept()
print 'Connection address:', addr
while True:
data = conn.recv(BUFFER_SIZE)
if not data:
break
print "received data:", data
#conn.send(data) # echo
conn.close()
"""
testbuf = (
"+"
"$qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+#c9"
"$qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+#c9"
"$qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+#c9"
"$qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+#c9"
"---+"
"$Hg0#df"
"$Hg0#df"
"$Hg0#df"
"$Hg0#df"
"---+"
"$qTStatus#49"
"$qTStatus#49"
"$qTStatus#49"
"$qTStatus#49"
"---+"
)
#-----------------------------------------------------------------------------
class gdb(object):
def __init__(self, cpu):
self.cpu = cpu
self.port = 3333 # gdb listening port
self.rx_state = self.wait4_start # receive packet state
self.rx_cs = [] # receive packet checksum
self.rx_data = [] # receive packet data
def rx_ack(self):
print('ack')
def rx_nak(self):
print('nak')
def rx_cmd(self, cmd):
print('%s' % cmd)
def csum_good(self, data, cs):
csum = 0
for c in data:
csum += ord(c)
return int(cs, 16) == csum % 256
def wait4_start(self, c):
"""wait for the packet start delimiter"""
if c == '+':
self.rx_ack()
elif c == '-':
self.rx_nak()
elif c == '$':
# get the packet data
self.rx_data = []
self.rx_state = self.wait4_end
else:
# keep on waiting...
pass
def wait4_end(self, c):
"""wait for the packet end delimiter"""
if c == '#':
# get the checksum
self.rx_cs = []
self.rx_state = self.wait4_checksum
else:
# accumulate the packet data
self.rx_data.append(c)
def wait4_checksum(self, c):
"""wait for the checksum characters"""
# accumulate the checksum character
self.rx_cs.append(c)
if len(self.rx_cs) == 2:
# we have the checksum
cs = ''.join(self.rx_cs)
data = ''.join(self.rx_data)
if self.csum_good(data, cs):
self.rx_cmd(data)
else:
# TODO: bad checksum - send a nak
pass
self.rx_state = self.wait4_start
def rx(self, buf):
"""receive a buffer of characters"""
for c in buf:
self.rx_state(c)
def run(self, ui, args):
"""run the gdb server"""
self.rx(testbuf)
#-----------------------------------------------------------------------------