File: gdb_rsp.py

package info (click to toggle)
simulavr 0.1.2.2-6.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,756 kB
  • ctags: 3,179
  • sloc: ansic: 19,987; sh: 3,623; python: 3,528; makefile: 406; asm: 308; yacc: 145; lex: 48
file content (316 lines) | stat: -rw-r--r-- 7,631 bytes parent folder | download | duplicates (5)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#! /usr/bin/env python
###############################################################################
#
# simulavr - A simulator for the Atmel AVR family of microcontrollers.
# Copyright (C) 2001, 2002  Theodore A. Roth
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
###############################################################################
#
# $Id: gdb_rsp.py,v 1.3 2002/04/11 03:09:45 troth Exp $
#

import socket, struct, array, sys

from registers import Reg

class GdbRSP_Exception(Exception):   pass

class ErrCheckSum(GdbRSP_Exception): pass
class ErrPacket(GdbRSP_Exception):   pass
class ErrReply(GdbRSP_Exception):    pass
class ErrMemRead(GdbRSP_Exception):  pass

class GdbRemoteSerialProtocol:
	"""GDB Remote Serial Protocol client implemntation.

	Simulates what GDB does. This class only implements a minimal subset of
	the remote serial protocol as needed to perform regression testing.

	The following packets types are implemented:
	  g   read registers
	  G   write registers
	  p   read single register
	  P   write single register
	  k   kill request
	  m   read memory
	  M   write memory
	  c   continue
	  C   continue with signal
	  s   step
	  S   step with signal
	  z   remove break or watchpoint
	  Z   insert break or watchpoint
	"""
	
	def __init__(self, host='localhost', port=1212, ofile=None):
		"""Open a connection to a remote target.
		"""
		# where to write the output of print statements
		self.ofile = ofile
		
		# connect to remote target
		self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.socket.connect((host,port))
		self.ack()

	def __del__(self):
		try:
			self.close()
		except:
			# close() most likely already called
			pass

	def close(self):
		self.send('k')
		reply = self.recv()
		self.out('Recv: "%s"' % (reply))
		self.socket.close()

	def out(self, s):
		if self.ofile:
			print >> self.ofile, s

	def cksum(self,pkt):
		sum = 0
		for c in pkt:
			sum += ord(c)

		return sum & 0xff

	def ack(self):
		self.out( 'Send: "+" (Ack)' )
		self.socket.send('+')
		
	def send(self, msg):
		s = '$'+msg+'#'+'%02x'%(self.cksum(msg))
		self.out( 'Sent: "%s"' % (s) )
		self.socket.send(s)
		reply = self.socket.recv(1)
		if reply != '+':
			raise ErrReply, reply
		else:
			self.out( '-> Ack' )

	def recv(self):
		c = self.socket.recv(1)
		if c == '$':
			max_buf = 400
			pkt = ''
			while max_buf:
				c = self.socket.recv(1)
				if c == '#':
					break
				pkt += c
				max_buf -= 1
				
			sum = self.cksum(pkt)
			cccc = self.socket.recv(1)
			cccc += self.socket.recv(1)
			csum = int( cccc, 16 )
			if sum != csum:
				raise ErrCheckSum, 'pkt="%s#%s", %02x : %02x' %(pkt,cccc,sum,csum)

			return pkt
		elif c == '+':
			self.out( 'Ack' )
		else:
			raise ErrPacket, c

		return None

	def str2bin(self, s):
		"""Convert a string of ascii hex digit pairs to an array of 8-bit binary values.
		"""
		arr = array.array('B')
		for i in range( 0, len(s), 2 ):
			arr.append( int(s[i:i+2], 16) )

		return arr

	def bin2str(self, arr):
		"""Convert an array of 8-bit binary values to a string of ascii hex digit pairs.
		"""
		ss = ''
		for i in arr:
			ss += '%02x' % (i)

		return ss

	def read_regs(self):
		self.send('g')
		reply = self.recv()
		self.out( 'Recv: "%s"' % (reply) )

		# little endian + 32 reg (B=8-bit) + SREG (B=8-bit) + SP (H=16-bit) + PC (L=32-bit)
		regs = [ i for i in struct.unpack( '<33BHL', self.str2bin(reply) ) ]
		return regs
	
	def write_regs(self, regs):
		arr = array.array('B')
		arr.fromstring(struct.pack('<33BHL', *regs))
		self.send('G'+self.bin2str(arr))
		reply = self.recv()
		if reply != 'OK':
			raise ErrReply
		self.out( 'Recv: "%s"' % (reply) )

	def read_reg(self, reg):
		self.send('p%x'%(reg))
		reply = self.str2bin(self.recv())
		if reg < Reg.SP:
			val = struct.unpack( '<B', reply )
		elif reg < Reg.PC:
			val = struct.unpack( '<H', reply ) # SP is 16 bit
		else:
			val = struct.unpack( '<L', reply ) # PC is 32 bit

		return val[0]

	def write_reg(self, reg, val):
		arr = array.array('B')
		if reg < Reg.SP:
			arr.fromstring(struct.pack('<B', val))
		elif reg < Reg.PC:
			arr.fromstring(struct.pack('<H', val)) # SP is 16 bit
		else:
			arr.fromstring(struct.pack('<L', val)) # PC is 32 bit

		self.send('P%x=%s' % (reg, self.bin2str(arr)))
		reply = self.recv()
		if reply != 'OK':
			raise ErrReply
		self.out( 'Recv: "%s"' % (reply) )

	def read_mem(self, addr, _len):
		self.send('m%x,%x' % (addr,_len))
		reply = self.recv()
		if reply[0] == 'E':
			raise ErrMemRead
		self.out( 'Recv: "%s"' % (reply) )

		return self.str2bin(reply)

	def write_mem(self, addr, _len, buf):
		self.send( 'M%x,%x:' %(addr,_len) + self.bin2str(buf) )
		reply = self.recv()
		if reply != 'OK':
			raise ErrReply
		self.out( 'Recv: "%s"' % (reply) )

	def handle_reply(self):
		"""The C, c, S, s and ? packets all expect the same reply.
		"""
		while 1:
			reply = self.recv()
			if reply[0] != 'O':
				break

		return reply

	def cont(self, addr=None):
		pkt = 'c'
		if addr != None:
			pkt += '%x' % (addr)
		self.send( pkt )
		return self.handle_reply()

	def cont_with_signal(self, signo=0, addr=None):
		pkt = 'C%02x' % (signo)
		if addr != None:
			pkt += ';%x' % (addr)
		self.send( pkt )
		return self.handle_reply()

	def step(self, addr=None):
		pkt = 's'
		if addr != None:
			pkt += '%x' % (addr)
		self.send( pkt )
		return self.handle_reply()

	def step_with_signal(self, signo=0, addr=None):
		pkt = 'S%02x' % (signo)
		if addr != None:
			pkt += ';%x' % (addr)
		self.send( pkt )
		return self.handle_reply()

	def break_insert(self, _type, addr, _len):
		pkt = 'Z%d,%x,%x' % (_type, addr, _len)
		self.send( pkt )
		reply = self.recv()
		if reply == '':
			self.out( 'Z packets are not supported by target.' )
		elif reply != 'OK':
			raise ErrPacket

	def break_remove(self, _type, addr, _len):
		pkt = 'z%d,%x,%x' % (_type, addr, _len)
		self.send( pkt )
		reply = self.recv()
		if reply == '':
			self.out( 'Z packets are not supported by target.' )
		elif reply != 'OK':
			raise ErrPacket

	def interrupt(self):
		"""Send target an interrupt.
		"""
		self.socket.send(chr(3))

if __name__ == '__main__':
	import signal
	rsp = GdbRemoteSerialProtocol()

	# Try to read/write registers
	regs = rsp.read_regs()
	regs[10] = 10
	rsp.write_regs(regs)
	print rsp.read_regs()

	print rsp.read_reg(12)
	rsp.write_reg(12,0xaa)
	print rsp.read_reg(12)
	print rsp.read_reg(34)				# PC

	# Try to read/write memory
	addr = 0x0
	_len  = 10
	mem = rsp.read_mem(addr,_len)
	print mem
	mem[4] = 10
	rsp.write_mem(addr,len(mem),mem)
	print rsp.read_mem(addr,_len)

	# Write zero's (NOP's) to first 60 bytes of flash
	arr = array.array('B', [ 0x0 for i in range(60) ])
	rsp.write_mem(addr, 60, arr)

	# Set a breakpoint at 8th instruction
	rsp.break_insert(0,0x10,0)
	rsp.cont()
	rsp.break_remove(0,0x10,0)
	rsp.step()
	rsp.step()
	rsp.step()
	rsp.step()

	rsp.break_insert(0,0x10,0)
	rsp.cont_with_signal(signal.SIGHUP)
	rsp.cont()
	
	rsp.close()