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
33 def ihex_line(address
, record_type
, data
):
34 assert len(data
) < 128
35 line
= ":%02X%04X%02X" % (len(data
), address
, record_type
)
37 check
+= address
% 256
43 line
+= "%02X" % value
44 line
+= "%02X\n" % ((256-check
)%256)
48 assert line
.startswith(":")
50 data_len
= int(line
[:2], 16)
51 address
= int(line
[2:6], 16)
52 record_type
= int(line
[6:8], 16)
54 for i
in range(data_len
):
55 data
+= "%c" % int(line
[8+2*i
:10+2*i
], 16)
56 return record_type
, address
, data
58 class DeleteServer(unittest
.TestCase
):
62 class SimpleRegisterTest(DeleteServer
):
64 self
.server
= target
.server()
65 self
.gdb
= testlib
.Gdb()
66 # For now gdb has to be told what the architecture is when it's not
68 self
.gdb
.command("set arch riscv:rv%d" % target
.xlen
)
70 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
73 self
.gdb
.command("p *((int*) 0x%x)=0x13" % target
.ram
)
74 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 4))
75 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 8))
76 self
.gdb
.p("$pc=0x%x" % target
.ram
)
78 def check_reg(self
, name
):
79 a
= random
.randrange(1<<target
.xlen
)
80 b
= random
.randrange(1<<target
.xlen
)
81 self
.gdb
.p("$%s=0x%x" % (name
, a
))
83 self
.assertEqual(self
.gdb
.p("$%s" % name
), a
)
84 self
.gdb
.p("$%s=0x%x" % (name
, b
))
86 self
.assertEqual(self
.gdb
.p("$%s" % name
), b
)
89 # S0 is saved/restored in DSCRATCH
93 # S1 is saved/restored in Debug RAM
97 # T0 is not saved/restored at all
101 # T2 is not saved/restored at all
104 class SimpleMemoryTest(DeleteServer
):
106 self
.server
= target
.server()
107 self
.gdb
= testlib
.Gdb()
108 self
.gdb
.command("set arch riscv:rv%d" % target
.xlen
)
109 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
111 def access_test(self
, size
, data_type
):
112 self
.assertEqual(self
.gdb
.p("sizeof(%s)" % data_type
),
114 a
= 0x86753095555aaaa & ((1<<(size
*8))-1)
115 b
= 0xdeadbeef12345678 & ((1<<(size
*8))-1)
116 self
.gdb
.p("*((%s*)0x%x) = 0x%x" % (data_type
, target
.ram
, a
))
117 self
.gdb
.p("*((%s*)0x%x) = 0x%x" % (data_type
, target
.ram
+ size
, b
))
118 self
.assertEqual(self
.gdb
.p("*((%s*)0x%x)" % (data_type
, target
.ram
)), a
)
119 self
.assertEqual(self
.gdb
.p("*((%s*)0x%x)" % (data_type
, target
.ram
+ size
)), b
)
122 self
.access_test(1, 'char')
125 self
.access_test(2, 'short')
128 self
.access_test(4, 'int')
131 self
.access_test(8, 'long long')
133 def test_block(self
):
136 fd
= file("write.ihex", "w")
138 for i
in range(length
/ line_length
):
139 line_data
= "".join(["%c" % random
.randrange(256) for _
in range(line_length
)])
141 fd
.write(ihex_line(i
* line_length
, 0, line_data
))
144 self
.gdb
.command("restore write.ihex 0x%x" % target
.ram
)
145 for offset
in range(0, length
, 19*4) + [length
-4]:
146 value
= self
.gdb
.p("*((int*)0x%x)" % (target
.ram
+ offset
))
147 written
= ord(data
[offset
]) | \
148 (ord(data
[offset
+1]) << 8) | \
149 (ord(data
[offset
+2]) << 16) | \
150 (ord(data
[offset
+3]) << 24)
151 self
.assertEqual(value
, written
)
153 self
.gdb
.command("dump ihex memory read.ihex 0x%x 0x%x" % (target
.ram
,
154 target
.ram
+ length
))
155 for line
in file("read.ihex"):
156 record_type
, address
, line_data
= ihex_parse(line
)
157 if (record_type
== 0):
158 self
.assertEqual(line_data
, data
[address
:address
+len(line_data
)])
160 class InstantHaltTest(DeleteServer
):
162 self
.server
= target
.server()
163 self
.gdb
= testlib
.Gdb()
164 self
.gdb
.command("set arch riscv:rv%d" % target
.xlen
)
165 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
167 def test_instant_halt(self
):
168 self
.assertEqual(target
.reset_vector
, self
.gdb
.p("$pc"))
169 # mcycle and minstret have no defined reset value.
170 mstatus
= self
.gdb
.p("$mstatus")
171 self
.assertEqual(mstatus
& (MSTATUS_MIE | MSTATUS_MPRV |
174 def test_change_pc(self
):
175 """Change the PC right as we come out of reset."""
177 self
.gdb
.command("p *((int*) 0x%x)=0x13" % target
.ram
)
178 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 4))
179 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 8))
180 self
.gdb
.p("$pc=0x%x" % target
.ram
)
182 self
.assertEqual((target
.ram
+ 4), self
.gdb
.p("$pc"))
184 self
.assertEqual((target
.ram
+ 8), self
.gdb
.p("$pc"))
186 class DebugTest(DeleteServer
):
188 # Include malloc so that gdb can make function calls. I suspect this
189 # malloc will silently blow through the memory set aside for it, so be
191 self
.binary
= target
.compile("programs/debug.c", "programs/checksum.c",
192 "programs/tiny-malloc.c", "-DDEFINE_MALLOC", "-DDEFINE_FREE")
193 self
.server
= target
.server()
194 self
.gdb
= testlib
.Gdb()
195 self
.gdb
.command("file %s" % self
.binary
)
196 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
200 def exit(self
, expected_result
= 0xc86455d4):
201 output
= self
.gdb
.c()
202 self
.assertIn("Breakpoint", output
)
203 self
.assertIn("_exit", output
)
204 self
.assertEqual(self
.gdb
.p("status"), expected_result
)
206 def test_function_call(self
):
207 self
.gdb
.b("main:start")
209 text
= "Howdy, Earth!"
210 gdb_length
= self
.gdb
.p('strlen("%s")' % text
)
211 self
.assertEqual(gdb_length
, len(text
))
214 def test_change_string(self
):
215 text
= "This little piggy went to the market."
216 self
.gdb
.b("main:start")
218 self
.gdb
.p('fox = "%s"' % text
)
219 self
.exit(0x43b497b8)
221 def test_turbostep(self
):
222 """Single step a bunch of times."""
223 self
.gdb
.command("p i=0");
229 pc
= self
.gdb
.p("$pc")
230 self
.assertNotEqual(last_pc
, pc
)
231 if (last_pc
and pc
> last_pc
and pc
- last_pc
<= 4):
236 # Some basic sanity that we're not running between breakpoints or
238 self
.assertGreater(jumps
, 10)
239 self
.assertGreater(advances
, 50)
244 def test_symbols(self
):
247 output
= self
.gdb
.c()
248 self
.assertIn(", main ", output
)
249 output
= self
.gdb
.c()
250 self
.assertIn(", rot13 ", output
)
252 def test_breakpoint(self
):
254 # The breakpoint should be hit exactly 2 times.
256 output
= self
.gdb
.c()
258 self
.assertIn("Breakpoint ", output
)
259 #TODO self.assertIn("rot13 ", output)
262 def test_hwbp_1(self
):
263 if target
.instruction_hardware_breakpoint_count
< 1:
266 self
.gdb
.hbreak("rot13")
267 # The breakpoint should be hit exactly 2 times.
269 output
= self
.gdb
.c()
271 self
.assertIn("Breakpoint ", output
)
272 #TODO self.assertIn("rot13 ", output)
275 def test_hwbp_2(self
):
276 if target
.instruction_hardware_breakpoint_count
< 2:
279 self
.gdb
.hbreak("main")
280 self
.gdb
.hbreak("rot13")
281 # We should hit 3 breakpoints.
283 output
= self
.gdb
.c()
285 self
.assertIn("Breakpoint ", output
)
286 #TODO self.assertIn("rot13 ", output)
289 def test_too_many_hwbp(self
):
291 self
.gdb
.hbreak("*rot13 + %d" % (i
* 4))
293 output
= self
.gdb
.c()
294 self
.assertIn("Cannot insert hardware breakpoint", output
)
295 # Clean up, otherwise the hardware breakpoints stay set and future
297 self
.gdb
.command("D")
299 def test_registers(self
):
300 # Get to a point in the code where some registers have actually been
305 # Try both forms to test gdb.
306 for cmd
in ("info all-registers", "info registers all"):
307 output
= self
.gdb
.command(cmd
)
308 self
.assertNotIn("Could not", output
)
309 for reg
in ('zero', 'ra', 'sp', 'gp', 'tp'):
310 self
.assertIn(reg
, output
)
313 # mcpuid is one of the few registers that should have the high bit set
315 # Leave this commented out until gdb and spike agree on the encoding of
316 # mcpuid (which is going to be renamed to misa in any case).
317 #self.assertRegexpMatches(output, ".*mcpuid *0x80")
320 # The instret register should always be changing.
323 # instret = self.gdb.p("$instret")
324 # self.assertNotEqual(instret, last_instret)
325 # last_instret = instret
330 def test_interrupt(self
):
331 """Sending gdb ^C while the program is running should cause it to halt."""
332 self
.gdb
.b("main:start")
335 self
.gdb
.c(wait
=False)
337 output
= self
.gdb
.interrupt()
338 #TODO: assert "main" in output
339 self
.assertGreater(self
.gdb
.p("j"), 10)
343 class StepTest(DeleteServer
):
345 self
.binary
= target
.compile("programs/step.S")
346 self
.server
= target
.server()
347 self
.gdb
= testlib
.Gdb()
348 self
.gdb
.command("file %s" % self
.binary
)
349 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
355 main
= self
.gdb
.p("$pc")
356 for expected
in (4, 8, 0xc, 0x10, 0x18, 0x1c, 0x28, 0x20, 0x2c, 0x2c):
358 pc
= self
.gdb
.p("$pc")
359 self
.assertEqual("%x" % pc
, "%x" % (expected
+ main
))
361 class RegsTest(DeleteServer
):
363 self
.binary
= target
.compile("programs/regs.S")
364 self
.server
= target
.server()
365 self
.gdb
= testlib
.Gdb()
366 self
.gdb
.command("file %s" % self
.binary
)
367 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
370 self
.gdb
.b("handle_trap")
373 def test_write_gprs(self
):
374 regs
= [("x%d" % n
) for n
in range(2, 32)]
376 self
.gdb
.p("$pc=write_regs")
377 for i
, r
in enumerate(regs
):
378 self
.gdb
.command("p $%s=%d" % (r
, (0xdeadbeef<<i
)+17))
379 self
.gdb
.command("p $x1=data")
380 self
.gdb
.command("b all_done")
381 output
= self
.gdb
.c()
382 self
.assertIn("Breakpoint ", output
)
384 # Just to get this data in the log.
385 self
.gdb
.command("x/30gx data")
386 self
.gdb
.command("info registers")
387 for n
in range(len(regs
)):
388 self
.assertEqual(self
.gdb
.x("data+%d" % (8*n
), 'g'),
389 ((0xdeadbeef<<n
)+17) & ((1<<target
.xlen
)-1))
391 def test_write_csrs(self
):
392 # As much a test of gdb as of the simulator.
393 self
.gdb
.p("$mscratch=0")
395 self
.assertEqual(self
.gdb
.p("$mscratch"), 0)
396 self
.gdb
.p("$mscratch=123")
398 self
.assertEqual(self
.gdb
.p("$mscratch"), 123)
400 self
.gdb
.command("p $pc=write_regs")
401 self
.gdb
.command("p $a0=data")
402 self
.gdb
.command("b all_done")
403 self
.gdb
.command("c")
405 self
.assertEqual(123, self
.gdb
.p("$mscratch"))
406 self
.assertEqual(123, self
.gdb
.p("$x1"))
407 self
.assertEqual(123, self
.gdb
.p("$csr832"))
409 class DownloadTest(DeleteServer
):
411 length
= min(2**20, target
.ram_size
- 2048)
412 download_c
= tempfile
.NamedTemporaryFile(prefix
="download_", suffix
=".c")
413 download_c
.write("#include <stdint.h>\n")
414 download_c
.write("unsigned int crc32a(uint8_t *message, unsigned int size);\n")
415 download_c
.write("uint32_t length = %d;\n" % length
)
416 download_c
.write("uint8_t d[%d] = {\n" % length
)
418 for i
in range(length
/ 16):
419 download_c
.write(" /* 0x%04x */ " % (i
* 16));
421 value
= random
.randrange(1<<8)
422 download_c
.write("%d, " % value
)
423 self
.crc
= binascii
.crc32("%c" % value
, self
.crc
)
424 download_c
.write("\n");
425 download_c
.write("};\n");
426 download_c
.write("uint8_t *data = &d[0];\n");
427 download_c
.write("uint32_t main() { return crc32a(data, length); }\n")
433 self
.binary
= target
.compile(download_c
.name
, "programs/checksum.c")
434 self
.server
= target
.server()
435 self
.gdb
= testlib
.Gdb()
436 self
.gdb
.command("file %s" % self
.binary
)
437 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
439 def test_download(self
):
440 output
= self
.gdb
.load()
441 self
.gdb
.command("b _exit")
443 self
.assertEqual(self
.gdb
.p("status"), self
.crc
)
445 class MprvTest(DeleteServer
):
447 self
.binary
= target
.compile("programs/mprv.S")
448 self
.server
= target
.server()
449 self
.gdb
= testlib
.Gdb()
450 self
.gdb
.command("file %s" % self
.binary
)
451 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
455 """Test that the debugger can access memory when MPRV is set."""
456 self
.gdb
.c(wait
=False)
459 output
= self
.gdb
.command("p/x *(int*)(((char*)&data)-0x80000000)")
460 self
.assertIn("0xbead", output
)
462 class Target(object):
466 raise NotImplementedError
468 def compile(self
, *sources
):
469 binary_name
= "%s_%s" % (
471 os
.path
.basename(os
.path
.splitext(sources
[0])[0]))
473 self
.temporary_binary
= tempfile
.NamedTemporaryFile(
474 prefix
=binary_name
+ "_")
475 binary_name
= self
.temporary_binary
.name
476 testlib
.compile(sources
+
477 ("programs/entry.S", "programs/init.c",
479 "-T", "targets/%s/link.lds" % (self
.directory
or self
.name
),
486 class Spike64Target(Target
):
490 ram_size
= 5 * 1024 * 1024
491 instruction_hardware_breakpoint_count
= 0
492 reset_vector
= 0x1000
495 return testlib
.Spike(parsed
.cmd
, halted
=True)
497 class Spike32Target(Target
):
502 ram_size
= 5 * 1024 * 1024
503 instruction_hardware_breakpoint_count
= 0
504 reset_vector
= 0x1000
507 return testlib
.Spike(parsed
.cmd
, halted
=True, xlen
=32)
509 class MicroSemiTarget(Target
):
514 instruction_hardware_breakpoint_count
= 2
517 return testlib
.Openocd(cmd
=parsed
.cmd
,
518 config
="targets/%s/openocd.cfg" % self
.name
)
527 parser
= argparse
.ArgumentParser(
529 Example command line from the real world:
530 Run all RegsTest cases against a MicroSemi m2gl_m2s board, with custom openocd command:
531 ./gdbserver.py --m2gl_m2s --cmd "$HOME/SiFive/openocd/src/openocd -s $HOME/SiFive/openocd/tcl -d" -- -vf RegsTest
533 group
= parser
.add_mutually_exclusive_group(required
=True)
535 group
.add_argument("--%s" % t
.name
, action
="store_const", const
=t
,
537 parser
.add_argument("--cmd",
538 help="The command to use to start the debug server.")
539 parser
.add_argument("--isolate", action
="store_true",
540 help="Try to run in such a way that multiple instances can run at "
541 "the same time. This may make it harder to debug a failure if it "
543 parser
.add_argument("unittest", nargs
="*")
545 parsed
= parser
.parse_args()
548 target
= parsed
.target()
549 unittest
.main(argv
=[sys
.argv
[0]] + parsed
.unittest
)
551 # TROUBLESHOOTING TIPS
552 # If a particular test fails, run just that one test, eg.:
553 # ./tests/gdbserver.py MprvTest.test_mprv
554 # Then inspect gdb.log and spike.log to see what happened in more detail.
556 if __name__
== '__main__':