13 def ihex_line(address
, record_type
, data
):
14 assert len(data
) < 128
15 line
= ":%02X%04X%02X" % (len(data
), address
, record_type
)
17 check
+= address
% 256
23 line
+= "%02X" % value
24 line
+= "%02X\n" % ((256-check
)%256)
28 assert line
.startswith(":")
30 data_len
= int(line
[:2], 16)
31 address
= int(line
[2:6], 16)
32 record_type
= int(line
[6:8], 16)
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
38 class DeleteServer(unittest
.TestCase
):
42 class SimpleRegisterTest(DeleteServer
):
44 self
.server
= target
.server()
45 self
.gdb
= testlib
.Gdb()
46 # For now gdb has to be told what the architecture is when it's not
48 self
.gdb
.command("set arch riscv:rv%d" % target
.xlen
)
50 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
53 self
.gdb
.command("p *((int*) 0x%x)=0x13" % target
.ram
)
54 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 4))
55 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 8))
56 self
.gdb
.p("$pc=0x%x" % target
.ram
)
58 def check_reg(self
, name
):
59 a
= random
.randrange(1<<target
.xlen
)
60 b
= random
.randrange(1<<target
.xlen
)
61 self
.gdb
.p("$%s=0x%x" % (name
, a
))
63 self
.assertEqual(self
.gdb
.p("$%s" % name
), a
)
64 self
.gdb
.p("$%s=0x%x" % (name
, b
))
66 self
.assertEqual(self
.gdb
.p("$%s" % name
), b
)
69 # S0 is saved/restored in DSCRATCH
73 # S1 is saved/restored in Debug RAM
77 # T0 is not saved/restored at all
81 # T2 is not saved/restored at all
84 class SimpleMemoryTest(DeleteServer
):
86 self
.server
= target
.server()
87 self
.gdb
= testlib
.Gdb()
88 self
.gdb
.command("set arch riscv:rv%d" % target
.xlen
)
89 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
91 def access_test(self
, size
, data_type
):
92 self
.assertEqual(self
.gdb
.p("sizeof(%s)" % data_type
),
94 a
= 0x86753095555aaaa & ((1<<(size
*8))-1)
95 b
= 0xdeadbeef12345678 & ((1<<(size
*8))-1)
96 self
.gdb
.p("*((%s*)0x%x) = 0x%x" % (data_type
, target
.ram
, a
))
97 self
.gdb
.p("*((%s*)0x%x) = 0x%x" % (data_type
, target
.ram
+ size
, b
))
98 self
.assertEqual(self
.gdb
.p("*((%s*)0x%x)" % (data_type
, target
.ram
)), a
)
99 self
.assertEqual(self
.gdb
.p("*((%s*)0x%x)" % (data_type
, target
.ram
+ size
)), b
)
102 self
.access_test(1, 'char')
105 self
.access_test(2, 'short')
108 self
.access_test(4, 'int')
111 self
.access_test(8, 'long long')
113 def test_block(self
):
116 fd
= file("write.ihex", "w")
118 for i
in range(length
/ line_length
):
119 line_data
= "".join(["%c" % random
.randrange(256) for _
in range(line_length
)])
121 fd
.write(ihex_line(i
* line_length
, 0, line_data
))
124 self
.gdb
.command("restore write.ihex 0x%x" % target
.ram
)
125 for offset
in range(0, length
, 19*4) + [length
-4]:
126 value
= self
.gdb
.p("*((long*)0x%x)" % (target
.ram
+ offset
))
127 written
= ord(data
[offset
]) | \
128 (ord(data
[offset
+1]) << 8) | \
129 (ord(data
[offset
+2]) << 16) | \
130 (ord(data
[offset
+3]) << 24)
131 self
.assertEqual(value
, written
)
133 self
.gdb
.command("dump ihex memory read.ihex 0x%x 0x%x" % (target
.ram
,
134 target
.ram
+ length
))
135 for line
in file("read.ihex"):
136 record_type
, address
, line_data
= ihex_parse(line
)
137 if (record_type
== 0):
138 self
.assertEqual(line_data
, data
[address
:address
+len(line_data
)])
140 class InstantHaltTest(DeleteServer
):
142 self
.server
= target
.server()
143 self
.gdb
= testlib
.Gdb()
144 self
.gdb
.command("set arch riscv:rv%d" % target
.xlen
)
145 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
147 def test_instant_halt(self
):
148 self
.assertEqual(0x1000, self
.gdb
.p("$pc"))
149 # For some reason instret resets to 0.
150 self
.assertLess(self
.gdb
.p("$instret"), 8)
152 self
.assertNotEqual(0x1000, self
.gdb
.p("$pc"))
154 def test_change_pc(self
):
155 """Change the PC right as we come out of reset."""
157 self
.gdb
.command("p *((int*) 0x%x)=0x13" % target
.ram
)
158 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 4))
159 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 8))
160 self
.gdb
.p("$pc=0x%x" % target
.ram
)
162 self
.assertEqual((target
.ram
+ 4), self
.gdb
.p("$pc"))
164 self
.assertEqual((target
.ram
+ 8), self
.gdb
.p("$pc"))
166 class DebugTest(DeleteServer
):
168 # Include malloc so that gdb can make function calls. I suspect this
169 # malloc will silently blow through the memory set aside for it, so be
171 self
.binary
= target
.compile("programs/debug.c", "programs/checksum.c",
172 "programs/tiny-malloc.c", "-DDEFINE_MALLOC", "-DDEFINE_FREE")
173 self
.server
= target
.server()
174 self
.gdb
= testlib
.Gdb()
175 self
.gdb
.command("file %s" % self
.binary
)
176 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
180 def exit(self
, expected_result
= 0xc86455d4):
181 output
= self
.gdb
.c()
182 self
.assertIn("Breakpoint", output
)
183 self
.assertIn("_exit", output
)
184 self
.assertEqual(self
.gdb
.p("status"), expected_result
)
186 def test_function_call(self
):
187 self
.gdb
.b("main:start")
189 text
= "Howdy, Earth!"
190 gdb_length
= self
.gdb
.p('strlen("%s")' % text
)
191 self
.assertEqual(gdb_length
, len(text
))
194 def test_change_string(self
):
195 text
= "This little piggy went to the market."
196 self
.gdb
.b("main:start")
198 self
.gdb
.p('fox = "%s"' % text
)
199 self
.exit(0x43b497b8)
201 def test_turbostep(self
):
202 """Single step a bunch of times."""
203 self
.gdb
.command("p i=0");
209 pc
= self
.gdb
.p("$pc")
210 self
.assertNotEqual(last_pc
, pc
)
211 if (last_pc
and pc
> last_pc
and pc
- last_pc
<= 4):
216 # Some basic sanity that we're not running between breakpoints or
218 self
.assertGreater(jumps
, 10)
219 self
.assertGreater(advances
, 50)
224 def test_symbols(self
):
227 output
= self
.gdb
.c()
228 self
.assertIn(", main ", output
)
229 output
= self
.gdb
.c()
230 self
.assertIn(", rot13 ", output
)
232 def test_breakpoint(self
):
234 # The breakpoint should be hit exactly 2 times.
236 output
= self
.gdb
.c()
238 self
.assertIn("Breakpoint ", output
)
239 #TODO self.assertIn("rot13 ", output)
242 def test_hwbp_1(self
):
243 if target
.instruction_hardware_breakpoint_count
< 1:
246 self
.gdb
.hbreak("rot13")
247 # The breakpoint should be hit exactly 2 times.
249 output
= self
.gdb
.c()
251 self
.assertIn("Breakpoint ", output
)
252 #TODO self.assertIn("rot13 ", output)
255 def test_hwbp_2(self
):
256 if target
.instruction_hardware_breakpoint_count
< 2:
259 self
.gdb
.hbreak("main")
260 self
.gdb
.hbreak("rot13")
261 # We should hit 3 breakpoints.
263 output
= self
.gdb
.c()
265 self
.assertIn("Breakpoint ", output
)
266 #TODO self.assertIn("rot13 ", output)
269 def test_too_many_hwbp(self
):
271 self
.gdb
.hbreak("*rot13 + %d" % (i
* 4))
273 output
= self
.gdb
.c()
274 self
.assertIn("Cannot insert hardware breakpoint", output
)
275 # Clean up, otherwise the hardware breakpoints stay set and future
277 self
.gdb
.command("D")
279 def test_registers(self
):
280 # Get to a point in the code where some registers have actually been
285 # Try both forms to test gdb.
286 for cmd
in ("info all-registers", "info registers all"):
287 output
= self
.gdb
.command(cmd
)
288 self
.assertNotIn("Could not", output
)
289 for reg
in ('zero', 'ra', 'sp', 'gp', 'tp'):
290 self
.assertIn(reg
, output
)
293 # mcpuid is one of the few registers that should have the high bit set
295 # Leave this commented out until gdb and spike agree on the encoding of
296 # mcpuid (which is going to be renamed to misa in any case).
297 #self.assertRegexpMatches(output, ".*mcpuid *0x80")
300 # The instret register should always be changing.
303 # instret = self.gdb.p("$instret")
304 # self.assertNotEqual(instret, last_instret)
305 # last_instret = instret
310 def test_interrupt(self
):
311 """Sending gdb ^C while the program is running should cause it to halt."""
312 self
.gdb
.b("main:start")
315 self
.gdb
.c(wait
=False)
317 output
= self
.gdb
.interrupt()
318 #TODO: assert "main" in output
319 self
.assertGreater(self
.gdb
.p("j"), 10)
323 class StepTest(DeleteServer
):
325 self
.binary
= target
.compile("programs/step.S")
326 self
.server
= target
.server()
327 self
.gdb
= testlib
.Gdb()
328 self
.gdb
.command("file %s" % self
.binary
)
329 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
335 main
= self
.gdb
.p("$pc")
336 for expected
in (4, 8, 0xc, 0x10, 0x18, 0x1c, 0x28, 0x20, 0x2c, 0x2c):
338 pc
= self
.gdb
.p("$pc")
339 self
.assertEqual("%x" % pc
, "%x" % (expected
+ main
))
341 class RegsTest(DeleteServer
):
343 self
.binary
= target
.compile("programs/regs.S")
344 self
.server
= target
.server()
345 self
.gdb
= testlib
.Gdb()
346 self
.gdb
.command("file %s" % self
.binary
)
347 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
350 self
.gdb
.b("handle_trap")
353 def test_write_gprs(self
):
354 regs
= [("x%d" % n
) for n
in range(2, 32)]
356 self
.gdb
.p("$pc=write_regs")
357 for i
, r
in enumerate(regs
):
358 self
.gdb
.command("p $%s=%d" % (r
, (0xdeadbeef<<i
)+17))
359 self
.gdb
.command("p $x1=data")
360 self
.gdb
.command("b all_done")
361 output
= self
.gdb
.c()
362 self
.assertIn("Breakpoint ", output
)
364 # Just to get this data in the log.
365 self
.gdb
.command("x/30gx data")
366 self
.gdb
.command("info registers")
367 for n
in range(len(regs
)):
368 self
.assertEqual(self
.gdb
.x("data+%d" % (8*n
), 'g'),
369 ((0xdeadbeef<<n
)+17) & ((1<<target
.xlen
)-1))
371 def test_write_csrs(self
):
372 # As much a test of gdb as of the simulator.
373 self
.gdb
.p("$mscratch=0")
375 self
.assertEqual(self
.gdb
.p("$mscratch"), 0)
376 self
.gdb
.p("$mscratch=123")
378 self
.assertEqual(self
.gdb
.p("$mscratch"), 123)
380 self
.gdb
.command("p $pc=write_regs")
381 self
.gdb
.command("p $a0=data")
382 self
.gdb
.command("b all_done")
383 self
.gdb
.command("c")
385 self
.assertEqual(123, self
.gdb
.p("$mscratch"))
386 self
.assertEqual(123, self
.gdb
.p("$x1"))
387 self
.assertEqual(123, self
.gdb
.p("$csr832"))
389 class DownloadTest(DeleteServer
):
391 length
= min(2**20, target
.ram_size
- 2048)
392 fd
= file("download.c", "w")
393 fd
.write("#include <stdint.h>\n")
394 fd
.write("unsigned int crc32a(uint8_t *message, unsigned int size);\n")
395 fd
.write("uint32_t length = %d;\n" % length
)
396 fd
.write("uint8_t d[%d] = {\n" % length
)
398 for i
in range(length
/ 16):
399 fd
.write(" /* 0x%04x */ " % (i
* 16));
401 value
= random
.randrange(1<<8)
402 fd
.write("%d, " % value
)
403 self
.crc
= binascii
.crc32("%c" % value
, self
.crc
)
406 fd
.write("uint8_t *data = &d[0];\n");
407 fd
.write("uint32_t main() { return crc32a(data, length); }\n")
413 self
.binary
= target
.compile("download.c", "programs/checksum.c")
414 self
.server
= target
.server()
415 self
.gdb
= testlib
.Gdb()
416 self
.gdb
.command("file %s" % self
.binary
)
417 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
419 def test_download(self
):
420 output
= self
.gdb
.load()
421 self
.gdb
.command("b _exit")
423 self
.assertEqual(self
.gdb
.p("status"), self
.crc
)
425 class MprvTest(DeleteServer
):
427 self
.binary
= target
.compile("programs/mprv.S")
428 self
.server
= target
.server()
429 self
.gdb
= testlib
.Gdb()
430 self
.gdb
.command("file %s" % self
.binary
)
431 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
435 """Test that the debugger can access memory when MPRV is set."""
436 self
.gdb
.c(wait
=False)
439 output
= self
.gdb
.command("p/x *(int*)(((char*)&data)-0x80000000)")
440 self
.assertIn("0xbead", output
)
442 class Target(object):
446 raise NotImplementedError
448 def compile(self
, *sources
):
449 return testlib
.compile(sources
+
450 ("programs/entry.S", "programs/init.c",
452 "-T", "targets/%s/link.lds" % (self
.directory
or self
.name
),
454 "-mcmodel=medany"), xlen
=self
.xlen
)
456 class Spike64Target(Target
):
460 ram_size
= 5 * 1024 * 1024
461 instruction_hardware_breakpoint_count
= 0
464 return testlib
.Spike(parsed
.cmd
, halted
=True)
466 class Spike32Target(Target
):
471 ram_size
= 5 * 1024 * 1024
472 instruction_hardware_breakpoint_count
= 0
475 return testlib
.Spike(parsed
.cmd
, halted
=True, xlen
=32)
477 class MicroSemiTarget(Target
):
482 instruction_hardware_breakpoint_count
= 2
485 return testlib
.Openocd(cmd
=parsed
.cmd
,
486 config
="targets/%s/openocd.cfg" % self
.name
)
495 parser
= argparse
.ArgumentParser(
497 Example command line from the real world:
498 Run all RegsTest cases against a MicroSemi m2gl_m2s board, with custom openocd command:
499 ./gdbserver.py --m2gl_m2s --cmd "$HOME/SiFive/openocd/src/openocd -s $HOME/SiFive/openocd/tcl -d" -- -vf RegsTest
501 group
= parser
.add_mutually_exclusive_group(required
=True)
503 group
.add_argument("--%s" % t
.name
, action
="store_const", const
=t
,
505 parser
.add_argument("--cmd",
506 help="The command to use to start the debug server.")
507 parser
.add_argument("unittest", nargs
="*")
509 parsed
= parser
.parse_args()
512 target
= parsed
.target()
513 unittest
.main(argv
=[sys
.argv
[0]] + parsed
.unittest
)
515 # TROUBLESHOOTING TIPS
516 # If a particular test fails, run just that one test, eg.:
517 # ./tests/gdbserver.py MprvTest.test_mprv
518 # Then inspect gdb.log and spike.log to see what happened in more detail.
520 if __name__
== '__main__':