Add block test.
[riscv-tests.git] / debug / gdbserver.py
1 #!/usr/bin/python
2
3 import os
4 import sys
5 import argparse
6 import testlib
7 import unittest
8 import tempfile
9 import time
10 import random
11 import binascii
12
13 def ihex_line(address, record_type, data):
14 assert len(data) < 128
15 line = ":%02X%04X%02X" % (len(data), address, record_type)
16 check = len(data)
17 check += address % 256
18 check += address >> 8
19 check += record_type
20 for char in data:
21 value = ord(char)
22 check += value
23 line += "%02X" % value
24 line += "%02X\n" % ((256-check)%256)
25 return line
26
27 def ihex_parse(line):
28 assert line.startswith(":")
29 line = line[1:]
30 data_len = int(line[:2], 16)
31 address = int(line[2:6], 16)
32 record_type = int(line[6:8], 16)
33 data = ""
34 for i in range(data_len):
35 data += "%c" % int(line[8+2*i:10+2*i], 16)
36 return record_type, address, data
37
38 class DeleteServer(unittest.TestCase):
39 def tearDown(self):
40 del self.server
41
42 class MemoryTest(DeleteServer):
43 def setUp(self):
44 self.server = target.server()
45 self.gdb = testlib.Gdb()
46 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
47
48 def access_test(self, size, data_type):
49 a = 0x86753095555aaaa & ((1<<(size*8))-1)
50 b = 0xdeadbeef12345678 & ((1<<(size*8))-1)
51 self.gdb.p("*((%s*)0x%x) = 0x%x" % (data_type, target.ram, a))
52 self.gdb.p("*((%s*)0x%x) = 0x%x" % (data_type, target.ram + size, b))
53 self.assertEqual(self.gdb.p("*((%s*)0x%x)" % (data_type, target.ram)), a)
54 self.assertEqual(self.gdb.p("*((%s*)0x%x)" % (data_type, target.ram + size)), b)
55
56 def test_8(self):
57 self.access_test(1, 'char')
58
59 def test_16(self):
60 self.access_test(2, 'short')
61
62 def test_32(self):
63 self.access_test(4, 'long')
64
65 def test_64(self):
66 self.access_test(8, 'long long')
67
68 def test_block(self):
69 length = 1024
70 line_length = 16
71 fd = file("write.ihex", "w")
72 data = ""
73 for i in range(length / line_length):
74 line_data = "".join(["%c" % random.randrange(256) for _ in range(line_length)])
75 data += line_data
76 fd.write(ihex_line(i * line_length, 0, line_data))
77 fd.close()
78
79 self.gdb.command("restore write.ihex 0x%x" % target.ram)
80 for offset in range(0, length, 19*4) + [length-4]:
81 value = self.gdb.p("*((long*)0x%x)" % (target.ram + offset))
82 written = ord(data[offset]) | \
83 (ord(data[offset+1]) << 8) | \
84 (ord(data[offset+2]) << 16) | \
85 (ord(data[offset+3]) << 24)
86 self.assertEqual(value, written)
87
88 self.gdb.command("dump ihex memory read.ihex 0x%x 0x%x" % (target.ram,
89 target.ram + length))
90 for line in file("read.ihex"):
91 record_type, address, line_data = ihex_parse(line)
92 if (record_type == 0):
93 self.assertEqual(line_data, data[address:address+len(line_data)])
94
95 class InstantHaltTest(DeleteServer):
96 def setUp(self):
97 self.server = target.server()
98 self.gdb = testlib.Gdb()
99 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
100
101 def test_instant_halt(self):
102 self.assertEqual(0x1000, self.gdb.p("$pc"))
103 # For some reason instret resets to 0.
104 self.assertLess(self.gdb.p("$instret"), 8)
105 self.gdb.stepi()
106 self.assertNotEqual(0x1000, self.gdb.p("$pc"))
107
108 def test_change_pc(self):
109 """Change the PC right as we come out of reset."""
110 # 0x13 is nop
111 self.gdb.command("p *((int*) 0x%x)=0x13" % target.ram)
112 self.gdb.command("p *((int*) 0x%x)=0x13" % (target.ram + 4))
113 self.gdb.command("p *((int*) 0x%x)=0x13" % (target.ram + 8))
114 self.gdb.p("$pc=0x%x" % target.ram)
115 self.gdb.stepi()
116 self.assertEqual((target.ram + 4), self.gdb.p("$pc"))
117 self.gdb.stepi()
118 self.assertEqual((target.ram + 4), self.gdb.p("$pc"))
119
120 class DebugTest(DeleteServer):
121 def setUp(self):
122 self.binary = target.compile("programs/debug.c", "programs/checksum.c")
123 self.server = target.server()
124 self.gdb = testlib.Gdb()
125 self.gdb.command("file %s" % self.binary)
126 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
127 self.gdb.load()
128 self.gdb.b("_exit")
129
130 def exit(self):
131 output = self.gdb.c()
132 self.assertIn("Breakpoint", output)
133 self.assertIn("_exit", output)
134 self.assertEqual(self.gdb.p("status"), 0xc86455d4)
135
136 def test_turbostep(self):
137 """Single step a bunch of times."""
138 self.gdb.command("p i=0");
139 last_pc = None
140 for _ in range(100):
141 self.gdb.stepi()
142 pc = self.gdb.command("p $pc")
143 self.assertNotEqual(last_pc, pc)
144 last_pc = pc
145
146 def test_exit(self):
147 self.exit()
148
149 def test_breakpoint(self):
150 self.gdb.b("rot13")
151 # The breakpoint should be hit exactly 2 times.
152 for i in range(2):
153 output = self.gdb.c()
154 self.assertIn("Breakpoint ", output)
155 self.exit()
156
157 def test_registers(self):
158 # Get to a point in the code where some registers have actually been
159 # used.
160 self.gdb.b("rot13")
161 self.gdb.c()
162 self.gdb.c()
163 # Try both forms to test gdb.
164 for cmd in ("info all-registers", "info registers all"):
165 output = self.gdb.command(cmd)
166 self.assertNotIn("Could not", output)
167 for reg in ('zero', 'ra', 'sp', 'gp', 'tp'):
168 self.assertIn(reg, output)
169
170 #TODO
171 # mcpuid is one of the few registers that should have the high bit set
172 # (for rv64).
173 # Leave this commented out until gdb and spike agree on the encoding of
174 # mcpuid (which is going to be renamed to misa in any case).
175 #self.assertRegexpMatches(output, ".*mcpuid *0x80")
176
177 #TODO:
178 # The instret register should always be changing.
179 #last_instret = None
180 #for _ in range(5):
181 # instret = self.gdb.p("$instret")
182 # self.assertNotEqual(instret, last_instret)
183 # last_instret = instret
184 # self.gdb.stepi()
185
186 self.exit()
187
188 def test_interrupt(self):
189 """Sending gdb ^C while the program is running should cause it to halt."""
190 self.gdb.b("main:start")
191 self.gdb.c()
192 self.gdb.command("p i=123");
193 self.gdb.c(wait=False)
194 time.sleep(0.1)
195 output = self.gdb.interrupt()
196 assert "main" in output
197 self.assertGreater(self.gdb.p("j"), 10)
198 self.gdb.p("i=0");
199 self.exit()
200
201 class RegsTest(DeleteServer):
202 def setUp(self):
203 self.binary = target.compile("programs/regs.S")
204 self.server = target.server()
205 self.gdb = testlib.Gdb()
206 self.gdb.command("file %s" % self.binary)
207 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
208 self.gdb.load()
209 self.gdb.b("main")
210 self.gdb.b("handle_trap")
211 self.gdb.c()
212
213 def test_write_gprs(self):
214 # Note a0 is missing from this list since it's used to hold the
215 # address.
216 regs = ("ra", "sp", "gp", "tp", "t0", "t1", "t2", "fp", "s1",
217 "a1", "a2", "a3", "a4", "a5", "a6", "a7", "s2", "s3", "s4",
218 "s5", "s6", "s7", "s8", "s9", "s10", "s11", "t3", "t4", "t5",
219 "t6")
220
221 self.gdb.p("$pc=write_regs")
222 for i, r in enumerate(regs):
223 self.gdb.command("p $%s=%d" % (r, (0xdeadbeef<<i)+17))
224 self.gdb.command("p $a0=data")
225 self.gdb.command("b all_done")
226 output = self.gdb.c()
227 self.assertIn("Breakpoint ", output)
228
229 # Just to get this data in the log.
230 self.gdb.command("x/30gx data")
231 self.gdb.command("info registers")
232 for n in range(len(regs)):
233 self.assertEqual(self.gdb.x("data+%d" % (8*n), 'g'),
234 (0xdeadbeef<<n)+17)
235
236 def test_write_csrs(self):
237 # As much a test of gdb as of the simulator.
238 self.gdb.p("$mscratch=0")
239 self.gdb.stepi()
240 self.assertEqual(self.gdb.p("$mscratch"), 0)
241 self.gdb.p("$mscratch=123")
242 self.gdb.stepi()
243 self.assertEqual(self.gdb.p("$mscratch"), 123)
244
245 self.gdb.command("p $pc=write_regs")
246 self.gdb.command("p $a0=data")
247 self.gdb.command("b all_done")
248 self.gdb.command("c")
249
250 self.assertEqual(123, self.gdb.p("$mscratch"))
251 self.assertEqual(123, self.gdb.p("$x1"))
252 self.assertEqual(123, self.gdb.p("$csr832"))
253
254 class DownloadTest(DeleteServer):
255 def setUp(self):
256 length = 2**20
257 fd = file("download.c", "w")
258 fd.write("#include <stdint.h>\n")
259 fd.write("unsigned int crc32a(uint8_t *message, unsigned int size);\n")
260 fd.write("uint32_t length = %d;\n" % length)
261 fd.write("uint8_t d[%d] = {\n" % length)
262 self.crc = 0
263 for i in range(length / 16):
264 fd.write(" /* 0x%04x */ " % (i * 16));
265 for _ in range(16):
266 value = random.randrange(1<<8)
267 fd.write("%d, " % value)
268 self.crc = binascii.crc32("%c" % value, self.crc)
269 fd.write("\n");
270 fd.write("};\n");
271 fd.write("uint8_t *data = &d[0];\n");
272 fd.write("uint32_t main() { return crc32a(data, length); }\n")
273 fd.close()
274
275 if self.crc < 0:
276 self.crc += 2**32
277
278 self.binary = target.compile("download.c", "programs/checksum.c")
279 self.server = target.server()
280 self.gdb = testlib.Gdb()
281 self.gdb.command("file %s" % self.binary)
282 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
283
284 def test_download(self):
285 output = self.gdb.load()
286 self.gdb.command("b _exit")
287 self.gdb.c()
288 self.assertEqual(self.gdb.p("status"), self.crc)
289
290 class MprvTest(DeleteServer):
291 def setUp(self):
292 self.binary = target.compile("programs/mprv.S")
293 self.server = target.server()
294 self.gdb = testlib.Gdb()
295 self.gdb.command("file %s" % self.binary)
296 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
297 self.gdb.load()
298
299 def test_mprv(self):
300 """Test that the debugger can access memory when MPRV is set."""
301 self.gdb.c(wait=False)
302 self.gdb.interrupt()
303 output = self.gdb.command("p/x *(int*)(((char*)&data)-0x80000000)")
304 self.assertIn("0xbead", output)
305
306 class Target(object):
307 def server(self):
308 raise NotImplementedError
309
310 def compile(self, *sources):
311 return testlib.compile(sources +
312 ("programs/entry.S", "programs/init.c",
313 "-I", "../env",
314 "-T", "targets/%s/link.lds" % self.name,
315 "-nostartfiles",
316 "-mcmodel=medany"), xlen=self.xlen)
317
318 class SpikeTarget(Target):
319 name = "spike"
320 xlen = 64
321 ram = 0x80010000
322
323 def server(self):
324 return testlib.Spike(parsed.cmd, halted=True)
325
326 class MicroSemiTarget(Target):
327 name = "m2gl_m2s"
328 xlen = 32
329 ram = 0x80000000
330
331 def server(self):
332 return testlib.Openocd(cmd=parsed.cmd,
333 config="targets/%s/openocd.cfg" % self.name)
334
335 targets = [
336 SpikeTarget,
337 MicroSemiTarget
338 ]
339
340 def main():
341 parser = argparse.ArgumentParser(
342 epilog="""
343 Example command line from the real world:
344 Run all RegsTest cases against a MicroSemi m2gl_m2s board, with custom openocd command:
345 ./gdbserver.py --m2gl_m2s --cmd "$HOME/SiFive/openocd/src/openocd -s $HOME/SiFive/openocd/tcl -d" -- -vf RegsTest
346 """)
347 group = parser.add_mutually_exclusive_group(required=True)
348 for t in targets:
349 group.add_argument("--%s" % t.name, action="store_const", const=t,
350 dest="target")
351 parser.add_argument("--cmd",
352 help="The command to use to start the debug server.")
353 parser.add_argument("unittest", nargs="*")
354 global parsed
355 parsed = parser.parse_args()
356
357 global target
358 target = parsed.target()
359 unittest.main(argv=[sys.argv[0]] + parsed.unittest)
360
361 # TROUBLESHOOTING TIPS
362 # If a particular test fails, run just that one test, eg.:
363 # ./tests/gdbserver.py MprvTest.test_mprv
364 # Then inspect gdb.log and spike.log to see what happened in more detail.
365
366 if __name__ == '__main__':
367 sys.exit(main())