13 MSTATUS_UIE
= 0x00000001
14 MSTATUS_SIE
= 0x00000002
15 MSTATUS_HIE
= 0x00000004
16 MSTATUS_MIE
= 0x00000008
17 MSTATUS_UPIE
= 0x00000010
18 MSTATUS_SPIE
= 0x00000020
19 MSTATUS_HPIE
= 0x00000040
20 MSTATUS_MPIE
= 0x00000080
21 MSTATUS_SPP
= 0x00000100
22 MSTATUS_HPP
= 0x00000600
23 MSTATUS_MPP
= 0x00001800
24 MSTATUS_FS
= 0x00006000
25 MSTATUS_XS
= 0x00018000
26 MSTATUS_MPRV
= 0x00020000
27 MSTATUS_PUM
= 0x00040000
28 MSTATUS_MXR
= 0x00080000
29 MSTATUS_VM
= 0x1F000000
30 MSTATUS32_SD
= 0x80000000
31 MSTATUS64_SD
= 0x8000000000000000
35 return testlib
.Gdb(parsed
.gdb
)
39 def ihex_line(address
, record_type
, data
):
40 assert len(data
) < 128
41 line
= ":%02X%04X%02X" % (len(data
), address
, record_type
)
43 check
+= address
% 256
49 line
+= "%02X" % value
50 line
+= "%02X\n" % ((256-check
)%256)
54 assert line
.startswith(":")
56 data_len
= int(line
[:2], 16)
57 address
= int(line
[2:6], 16)
58 record_type
= int(line
[6:8], 16)
60 for i
in range(data_len
):
61 data
+= "%c" % int(line
[8+2*i
:10+2*i
], 16)
62 return record_type
, address
, data
64 def readable_binary_string(s
):
65 return "".join("%02x" % ord(c
) for c
in s
)
67 class DeleteServer(unittest
.TestCase
):
71 class SimpleRegisterTest(DeleteServer
):
73 self
.server
= target
.server()
75 # For now gdb has to be told what the architecture is when it's not
77 self
.gdb
.command("set arch riscv:rv%d" % target
.xlen
)
79 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
82 self
.gdb
.command("p *((int*) 0x%x)=0x13" % target
.ram
)
83 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 4))
84 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 8))
85 self
.gdb
.p("$pc=0x%x" % target
.ram
)
87 def check_reg(self
, name
):
88 a
= random
.randrange(1<<target
.xlen
)
89 b
= random
.randrange(1<<target
.xlen
)
90 self
.gdb
.p("$%s=0x%x" % (name
, a
))
92 self
.assertEqual(self
.gdb
.p("$%s" % name
), a
)
93 self
.gdb
.p("$%s=0x%x" % (name
, b
))
95 self
.assertEqual(self
.gdb
.p("$%s" % name
), b
)
98 # S0 is saved/restored in DSCRATCH
102 # S1 is saved/restored in Debug RAM
106 # T0 is not saved/restored at all
110 # T2 is not saved/restored at all
113 class SimpleMemoryTest(DeleteServer
):
115 self
.server
= target
.server()
117 self
.gdb
.command("set arch riscv:rv%d" % target
.xlen
)
118 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
120 def access_test(self
, size
, data_type
):
121 self
.assertEqual(self
.gdb
.p("sizeof(%s)" % data_type
),
123 a
= 0x86753095555aaaa & ((1<<(size
*8))-1)
124 b
= 0xdeadbeef12345678 & ((1<<(size
*8))-1)
125 self
.gdb
.p("*((%s*)0x%x) = 0x%x" % (data_type
, target
.ram
, a
))
126 self
.gdb
.p("*((%s*)0x%x) = 0x%x" % (data_type
, target
.ram
+ size
, b
))
127 self
.assertEqual(self
.gdb
.p("*((%s*)0x%x)" % (data_type
, target
.ram
)), a
)
128 self
.assertEqual(self
.gdb
.p("*((%s*)0x%x)" % (data_type
, target
.ram
+ size
)), b
)
131 self
.access_test(1, 'char')
134 self
.access_test(2, 'short')
137 self
.access_test(4, 'int')
140 self
.access_test(8, 'long long')
142 def test_block(self
):
145 a
= tempfile
.NamedTemporaryFile(suffix
=".ihex")
147 for i
in range(length
/ line_length
):
148 line_data
= "".join(["%c" % random
.randrange(256) for _
in range(line_length
)])
150 a
.write(ihex_line(i
* line_length
, 0, line_data
))
153 self
.gdb
.command("restore %s 0x%x" % (a
.name
, target
.ram
))
154 for offset
in range(0, length
, 19*4) + [length
-4]:
155 value
= self
.gdb
.p("*((int*)0x%x)" % (target
.ram
+ offset
))
156 written
= ord(data
[offset
]) | \
157 (ord(data
[offset
+1]) << 8) | \
158 (ord(data
[offset
+2]) << 16) | \
159 (ord(data
[offset
+3]) << 24)
160 self
.assertEqual(value
, written
)
162 b
= tempfile
.NamedTemporaryFile(suffix
=".ihex")
163 self
.gdb
.command("dump ihex memory %s 0x%x 0x%x" % (b
.name
, target
.ram
,
164 target
.ram
+ length
))
166 record_type
, address
, line_data
= ihex_parse(line
)
167 if (record_type
== 0):
168 self
.assertEqual(readable_binary_string(line_data
),
169 readable_binary_string(data
[address
:address
+len(line_data
)]))
171 class InstantHaltTest(DeleteServer
):
173 self
.server
= target
.server()
175 self
.gdb
.command("set arch riscv:rv%d" % target
.xlen
)
176 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
178 def test_instant_halt(self
):
179 self
.assertEqual(target
.reset_vector
, self
.gdb
.p("$pc"))
180 # mcycle and minstret have no defined reset value.
181 mstatus
= self
.gdb
.p("$mstatus")
182 self
.assertEqual(mstatus
& (MSTATUS_MIE | MSTATUS_MPRV |
185 def test_change_pc(self
):
186 """Change the PC right as we come out of reset."""
188 self
.gdb
.command("p *((int*) 0x%x)=0x13" % target
.ram
)
189 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 4))
190 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 8))
191 self
.gdb
.p("$pc=0x%x" % target
.ram
)
193 self
.assertEqual((target
.ram
+ 4), self
.gdb
.p("$pc"))
195 self
.assertEqual((target
.ram
+ 8), self
.gdb
.p("$pc"))
197 class DebugTest(DeleteServer
):
199 # Include malloc so that gdb can make function calls. I suspect this
200 # malloc will silently blow through the memory set aside for it, so be
202 self
.binary
= target
.compile("programs/debug.c", "programs/checksum.c",
203 "programs/tiny-malloc.c", "-DDEFINE_MALLOC", "-DDEFINE_FREE")
204 self
.server
= target
.server()
206 self
.gdb
.command("file %s" % self
.binary
)
207 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
211 def exit(self
, expected_result
= 0xc86455d4):
212 output
= self
.gdb
.c()
213 self
.assertIn("Breakpoint", output
)
214 self
.assertIn("_exit", output
)
215 self
.assertEqual(self
.gdb
.p("status"), expected_result
)
217 def test_function_call(self
):
218 self
.gdb
.b("main:start")
220 text
= "Howdy, Earth!"
221 gdb_length
= self
.gdb
.p('strlen("%s")' % text
)
222 self
.assertEqual(gdb_length
, len(text
))
225 def test_change_string(self
):
226 text
= "This little piggy went to the market."
227 self
.gdb
.b("main:start")
229 self
.gdb
.p('fox = "%s"' % text
)
230 self
.exit(0x43b497b8)
232 def test_turbostep(self
):
233 """Single step a bunch of times."""
234 self
.gdb
.command("p i=0");
240 pc
= self
.gdb
.p("$pc")
241 self
.assertNotEqual(last_pc
, pc
)
242 if (last_pc
and pc
> last_pc
and pc
- last_pc
<= 4):
247 # Some basic sanity that we're not running between breakpoints or
249 self
.assertGreater(jumps
, 10)
250 self
.assertGreater(advances
, 50)
255 def test_symbols(self
):
258 output
= self
.gdb
.c()
259 self
.assertIn(", main ", output
)
260 output
= self
.gdb
.c()
261 self
.assertIn(", rot13 ", output
)
263 def test_breakpoint(self
):
265 # The breakpoint should be hit exactly 2 times.
267 output
= self
.gdb
.c()
269 self
.assertIn("Breakpoint ", output
)
270 #TODO self.assertIn("rot13 ", output)
273 def test_hwbp_1(self
):
274 if target
.instruction_hardware_breakpoint_count
< 1:
277 self
.gdb
.hbreak("rot13")
278 # The breakpoint should be hit exactly 2 times.
280 output
= self
.gdb
.c()
282 self
.assertIn("Breakpoint ", output
)
283 #TODO self.assertIn("rot13 ", output)
286 def test_hwbp_2(self
):
287 if target
.instruction_hardware_breakpoint_count
< 2:
290 self
.gdb
.hbreak("main")
291 self
.gdb
.hbreak("rot13")
292 # We should hit 3 breakpoints.
294 output
= self
.gdb
.c()
296 self
.assertIn("Breakpoint ", output
)
297 #TODO self.assertIn("rot13 ", output)
300 def test_too_many_hwbp(self
):
302 self
.gdb
.hbreak("*rot13 + %d" % (i
* 4))
304 output
= self
.gdb
.c()
305 self
.assertIn("Cannot insert hardware breakpoint", output
)
306 # Clean up, otherwise the hardware breakpoints stay set and future
308 self
.gdb
.command("D")
310 def test_registers(self
):
311 # Get to a point in the code where some registers have actually been
316 # Try both forms to test gdb.
317 for cmd
in ("info all-registers", "info registers all"):
318 output
= self
.gdb
.command(cmd
)
319 self
.assertNotIn("Could not", output
)
320 for reg
in ('zero', 'ra', 'sp', 'gp', 'tp'):
321 self
.assertIn(reg
, output
)
324 # mcpuid is one of the few registers that should have the high bit set
326 # Leave this commented out until gdb and spike agree on the encoding of
327 # mcpuid (which is going to be renamed to misa in any case).
328 #self.assertRegexpMatches(output, ".*mcpuid *0x80")
331 # The instret register should always be changing.
334 # instret = self.gdb.p("$instret")
335 # self.assertNotEqual(instret, last_instret)
336 # last_instret = instret
341 def test_interrupt(self
):
342 """Sending gdb ^C while the program is running should cause it to halt."""
343 self
.gdb
.b("main:start")
346 self
.gdb
.c(wait
=False)
348 output
= self
.gdb
.interrupt()
349 #TODO: assert "main" in output
350 self
.assertGreater(self
.gdb
.p("j"), 10)
354 class StepTest(DeleteServer
):
356 self
.binary
= target
.compile("programs/step.S")
357 self
.server
= target
.server()
359 self
.gdb
.command("file %s" % self
.binary
)
360 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
366 main
= self
.gdb
.p("$pc")
367 for expected
in (4, 8, 0xc, 0x10, 0x18, 0x1c, 0x28, 0x20, 0x2c, 0x2c):
369 pc
= self
.gdb
.p("$pc")
370 self
.assertEqual("%x" % pc
, "%x" % (expected
+ main
))
372 class RegsTest(DeleteServer
):
374 self
.binary
= target
.compile("programs/regs.S")
375 self
.server
= target
.server()
377 self
.gdb
.command("file %s" % self
.binary
)
378 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
381 self
.gdb
.b("handle_trap")
384 def test_write_gprs(self
):
385 regs
= [("x%d" % n
) for n
in range(2, 32)]
387 self
.gdb
.p("$pc=write_regs")
388 for i
, r
in enumerate(regs
):
389 self
.gdb
.command("p $%s=%d" % (r
, (0xdeadbeef<<i
)+17))
390 self
.gdb
.command("p $x1=data")
391 self
.gdb
.command("b all_done")
392 output
= self
.gdb
.c()
393 self
.assertIn("Breakpoint ", output
)
395 # Just to get this data in the log.
396 self
.gdb
.command("x/30gx data")
397 self
.gdb
.command("info registers")
398 for n
in range(len(regs
)):
399 self
.assertEqual(self
.gdb
.x("data+%d" % (8*n
), 'g'),
400 ((0xdeadbeef<<n
)+17) & ((1<<target
.xlen
)-1))
402 def test_write_csrs(self
):
403 # As much a test of gdb as of the simulator.
404 self
.gdb
.p("$mscratch=0")
406 self
.assertEqual(self
.gdb
.p("$mscratch"), 0)
407 self
.gdb
.p("$mscratch=123")
409 self
.assertEqual(self
.gdb
.p("$mscratch"), 123)
411 self
.gdb
.command("p $pc=write_regs")
412 self
.gdb
.command("p $a0=data")
413 self
.gdb
.command("b all_done")
414 self
.gdb
.command("c")
416 self
.assertEqual(123, self
.gdb
.p("$mscratch"))
417 self
.assertEqual(123, self
.gdb
.p("$x1"))
418 self
.assertEqual(123, self
.gdb
.p("$csr832"))
420 class DownloadTest(DeleteServer
):
422 length
= min(2**20, target
.ram_size
- 2048)
423 download_c
= tempfile
.NamedTemporaryFile(prefix
="download_", suffix
=".c")
424 download_c
.write("#include <stdint.h>\n")
425 download_c
.write("unsigned int crc32a(uint8_t *message, unsigned int size);\n")
426 download_c
.write("uint32_t length = %d;\n" % length
)
427 download_c
.write("uint8_t d[%d] = {\n" % length
)
429 for i
in range(length
/ 16):
430 download_c
.write(" /* 0x%04x */ " % (i
* 16));
432 value
= random
.randrange(1<<8)
433 download_c
.write("%d, " % value
)
434 self
.crc
= binascii
.crc32("%c" % value
, self
.crc
)
435 download_c
.write("\n");
436 download_c
.write("};\n");
437 download_c
.write("uint8_t *data = &d[0];\n");
438 download_c
.write("uint32_t main() { return crc32a(data, length); }\n")
444 self
.binary
= target
.compile(download_c
.name
, "programs/checksum.c")
445 self
.server
= target
.server()
447 self
.gdb
.command("file %s" % self
.binary
)
448 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
450 def test_download(self
):
451 output
= self
.gdb
.load()
452 self
.gdb
.command("b _exit")
454 self
.assertEqual(self
.gdb
.p("status"), self
.crc
)
456 class MprvTest(DeleteServer
):
458 self
.binary
= target
.compile("programs/mprv.S")
459 self
.server
= target
.server()
461 self
.gdb
.command("file %s" % self
.binary
)
462 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
466 """Test that the debugger can access memory when MPRV is set."""
467 self
.gdb
.c(wait
=False)
470 output
= self
.gdb
.command("p/x *(int*)(((char*)&data)-0x80000000)")
471 self
.assertIn("0xbead", output
)
473 class PrivTest(DeleteServer
):
475 self
.binary
= target
.compile("programs/priv.S")
476 self
.server
= target
.server()
478 self
.gdb
.command("file %s" % self
.binary
)
479 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
482 misa
= self
.gdb
.p("$misa")
483 self
.supported
= set()
485 self
.supported
.add(0)
487 self
.supported
.add(1)
489 self
.supported
.add(2)
490 self
.supported
.add(3)
493 """Test reading/writing priv."""
494 for privilege
in range(4):
495 self
.gdb
.p("$priv=%d" % privilege
)
497 actual
= self
.gdb
.p("$priv")
498 self
.assertIn(actual
, self
.supported
)
499 if privilege
in self
.supported
:
500 self
.assertEqual(actual
, privilege
)
502 def test_change(self
):
503 """Test that the core's privilege level actually changes."""
505 if 0 not in self
.supported
:
506 # TODO: return not applicable
513 self
.gdb
.p("$priv=3")
514 main
= self
.gdb
.p("$pc")
516 self
.assertEqual("%x" % self
.gdb
.p("$pc"), "%x" % (main
+4))
519 self
.gdb
.p("$priv=0")
521 # Should have taken an exception, so be nowhere near main.
522 pc
= self
.gdb
.p("$pc")
523 self
.assertTrue(pc
< main
or pc
> main
+ 0x100)
525 class Target(object):
529 raise NotImplementedError
531 def compile(self
, *sources
):
532 binary_name
= "%s_%s" % (
534 os
.path
.basename(os
.path
.splitext(sources
[0])[0]))
536 self
.temporary_binary
= tempfile
.NamedTemporaryFile(
537 prefix
=binary_name
+ "_")
538 binary_name
= self
.temporary_binary
.name
539 testlib
.compile(sources
+
540 ("programs/entry.S", "programs/init.c",
542 "-T", "targets/%s/link.lds" % (self
.directory
or self
.name
),
549 class SpikeTarget(Target
):
552 ram_size
= 5 * 1024 * 1024
553 instruction_hardware_breakpoint_count
= 0
554 reset_vector
= 0x1000
556 class Spike64Target(SpikeTarget
):
561 return testlib
.Spike(parsed
.cmd
, halted
=True)
563 class Spike32Target(SpikeTarget
):
568 return testlib
.Spike(parsed
.cmd
, halted
=True, xlen
=32)
570 class FreedomE300Target(Target
):
571 name
= "freedom-e300"
575 instruction_hardware_breakpoint_count
= 2
578 return testlib
.Openocd(cmd
=parsed
.cmd
,
579 config
="targets/%s/openocd.cfg" % self
.name
)
588 parser
= argparse
.ArgumentParser(
590 Example command line from the real world:
591 Run all RegsTest cases against a MicroSemi m2gl_m2s board, with custom openocd command:
592 ./gdbserver.py --m2gl_m2s --cmd "$HOME/SiFive/openocd/src/openocd -s $HOME/SiFive/openocd/tcl -d" -- -vf RegsTest
594 group
= parser
.add_mutually_exclusive_group(required
=True)
596 group
.add_argument("--%s" % t
.name
, action
="store_const", const
=t
,
598 parser
.add_argument("--cmd",
599 help="The command to use to start the debug server.")
600 parser
.add_argument("--gdb",
601 help="The command to use to start gdb.")
602 parser
.add_argument("--isolate", action
="store_true",
603 help="Try to run in such a way that multiple instances can run at "
604 "the same time. This may make it harder to debug a failure if it "
606 parser
.add_argument("unittest", nargs
="*")
608 parsed
= parser
.parse_args()
611 target
= parsed
.target()
612 unittest
.main(argv
=[sys
.argv
[0]] + parsed
.unittest
)
614 # TROUBLESHOOTING TIPS
615 # If a particular test fails, run just that one test, eg.:
616 # ./tests/gdbserver.py MprvTest.test_mprv
617 # Then inspect gdb.log and spike.log to see what happened in more detail.
619 if __name__
== '__main__':