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 a
= 0x86753095555aaaa & ((1<<(size
*8))-1)
93 b
= 0xdeadbeef12345678 & ((1<<(size
*8))-1)
94 self
.gdb
.p("*((%s*)0x%x) = 0x%x" % (data_type
, target
.ram
, a
))
95 self
.gdb
.p("*((%s*)0x%x) = 0x%x" % (data_type
, target
.ram
+ size
, b
))
96 self
.assertEqual(self
.gdb
.p("*((%s*)0x%x)" % (data_type
, target
.ram
)), a
)
97 self
.assertEqual(self
.gdb
.p("*((%s*)0x%x)" % (data_type
, target
.ram
+ size
)), b
)
100 self
.access_test(1, 'char')
103 self
.access_test(2, 'short')
106 self
.access_test(4, 'long')
109 self
.access_test(8, 'long long')
111 def test_block(self
):
114 fd
= file("write.ihex", "w")
116 for i
in range(length
/ line_length
):
117 line_data
= "".join(["%c" % random
.randrange(256) for _
in range(line_length
)])
119 fd
.write(ihex_line(i
* line_length
, 0, line_data
))
122 self
.gdb
.command("restore write.ihex 0x%x" % target
.ram
)
123 for offset
in range(0, length
, 19*4) + [length
-4]:
124 value
= self
.gdb
.p("*((long*)0x%x)" % (target
.ram
+ offset
))
125 written
= ord(data
[offset
]) | \
126 (ord(data
[offset
+1]) << 8) | \
127 (ord(data
[offset
+2]) << 16) | \
128 (ord(data
[offset
+3]) << 24)
129 self
.assertEqual(value
, written
)
131 self
.gdb
.command("dump ihex memory read.ihex 0x%x 0x%x" % (target
.ram
,
132 target
.ram
+ length
))
133 for line
in file("read.ihex"):
134 record_type
, address
, line_data
= ihex_parse(line
)
135 if (record_type
== 0):
136 self
.assertEqual(line_data
, data
[address
:address
+len(line_data
)])
138 class InstantHaltTest(DeleteServer
):
140 self
.server
= target
.server()
141 self
.gdb
= testlib
.Gdb()
142 self
.gdb
.command("set arch riscv:rv%d" % target
.xlen
)
143 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
145 def test_instant_halt(self
):
146 self
.assertEqual(0x1000, self
.gdb
.p("$pc"))
147 # For some reason instret resets to 0.
148 self
.assertLess(self
.gdb
.p("$instret"), 8)
150 self
.assertNotEqual(0x1000, self
.gdb
.p("$pc"))
152 def test_change_pc(self
):
153 """Change the PC right as we come out of reset."""
155 self
.gdb
.command("p *((int*) 0x%x)=0x13" % target
.ram
)
156 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 4))
157 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 8))
158 self
.gdb
.p("$pc=0x%x" % target
.ram
)
160 self
.assertEqual((target
.ram
+ 4), self
.gdb
.p("$pc"))
162 self
.assertEqual((target
.ram
+ 8), self
.gdb
.p("$pc"))
164 class DebugTest(DeleteServer
):
166 # Include malloc so that gdb can make function calls. I suspect this
167 # malloc will silently blow through the memory set aside for it, so be
169 self
.binary
= target
.compile("programs/debug.c", "programs/checksum.c",
170 "programs/tiny-malloc.c", "-DDEFINE_MALLOC", "-DDEFINE_FREE")
171 self
.server
= target
.server()
172 self
.gdb
= testlib
.Gdb()
173 self
.gdb
.command("file %s" % self
.binary
)
174 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
178 def exit(self
, expected_result
= 0xc86455d4):
179 output
= self
.gdb
.c()
180 self
.assertIn("Breakpoint", output
)
181 self
.assertIn("_exit", output
)
182 self
.assertEqual(self
.gdb
.p("status"), expected_result
)
184 def test_function_call(self
):
185 self
.gdb
.b("main:start")
187 text
= "Howdy, Earth!"
188 gdb_length
= self
.gdb
.p('strlen("%s")' % text
)
189 self
.assertEqual(gdb_length
, len(text
))
192 def test_change_string(self
):
193 text
= "This little piggy went to the market."
194 self
.gdb
.b("main:start")
196 self
.gdb
.p('fox = "%s"' % text
)
197 self
.exit(0x43b497b8)
199 def test_turbostep(self
):
200 """Single step a bunch of times."""
201 self
.gdb
.command("p i=0");
207 pc
= self
.gdb
.p("$pc")
208 self
.assertNotEqual(last_pc
, pc
)
209 if (last_pc
and pc
> last_pc
and pc
- last_pc
<= 4):
214 # Some basic sanity that we're not running between breakpoints or
216 self
.assertGreater(jumps
, 10)
217 self
.assertGreater(advances
, 50)
222 def test_symbols(self
):
225 output
= self
.gdb
.c()
226 self
.assertIn(", main ", output
)
227 output
= self
.gdb
.c()
228 self
.assertIn(", rot13 ", output
)
230 def test_breakpoint(self
):
232 # The breakpoint should be hit exactly 2 times.
234 output
= self
.gdb
.c()
236 self
.assertIn("Breakpoint ", output
)
237 #TODO self.assertIn("rot13 ", output)
240 def test_hwbp_1(self
):
241 self
.gdb
.hbreak("rot13")
242 # The breakpoint should be hit exactly 2 times.
244 output
= self
.gdb
.c()
246 self
.assertIn("Breakpoint ", output
)
247 #TODO self.assertIn("rot13 ", output)
250 def test_hwbp_2(self
):
251 self
.gdb
.hbreak("main")
252 self
.gdb
.hbreak("rot13")
253 # We should hit 3 breakpoints.
255 output
= self
.gdb
.c()
257 self
.assertIn("Breakpoint ", output
)
258 #TODO self.assertIn("rot13 ", output)
261 def test_too_many_hwbp(self
):
263 self
.gdb
.hbreak("*rot13 + %d" % (i
* 4))
265 output
= self
.gdb
.c()
266 self
.assertIn("Cannot insert hardware breakpoint", output
)
267 # Clean up, otherwise the hardware breakpoints stay set and future
269 self
.gdb
.command("D")
271 def test_registers(self
):
272 # Get to a point in the code where some registers have actually been
277 # Try both forms to test gdb.
278 for cmd
in ("info all-registers", "info registers all"):
279 output
= self
.gdb
.command(cmd
)
280 self
.assertNotIn("Could not", output
)
281 for reg
in ('zero', 'ra', 'sp', 'gp', 'tp'):
282 self
.assertIn(reg
, output
)
285 # mcpuid is one of the few registers that should have the high bit set
287 # Leave this commented out until gdb and spike agree on the encoding of
288 # mcpuid (which is going to be renamed to misa in any case).
289 #self.assertRegexpMatches(output, ".*mcpuid *0x80")
292 # The instret register should always be changing.
295 # instret = self.gdb.p("$instret")
296 # self.assertNotEqual(instret, last_instret)
297 # last_instret = instret
302 def test_interrupt(self
):
303 """Sending gdb ^C while the program is running should cause it to halt."""
304 self
.gdb
.b("main:start")
307 self
.gdb
.c(wait
=False)
309 output
= self
.gdb
.interrupt()
310 #TODO: assert "main" in output
311 self
.assertGreater(self
.gdb
.p("j"), 10)
315 class StepTest(DeleteServer
):
317 self
.binary
= target
.compile("programs/step.S")
318 self
.server
= target
.server()
319 self
.gdb
= testlib
.Gdb()
320 self
.gdb
.command("file %s" % self
.binary
)
321 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
327 main
= self
.gdb
.p("$pc")
328 for expected
in (4, 8, 0xc, 0x10, 0x18, 0x1c, 0x28, 0x20, 0x2c, 0x2c):
330 pc
= self
.gdb
.p("$pc")
331 self
.assertEqual("%x" % pc
, "%x" % (expected
+ main
))
333 class RegsTest(DeleteServer
):
335 self
.binary
= target
.compile("programs/regs.S")
336 self
.server
= target
.server()
337 self
.gdb
= testlib
.Gdb()
338 self
.gdb
.command("file %s" % self
.binary
)
339 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
342 self
.gdb
.b("handle_trap")
345 def test_write_gprs(self
):
346 regs
= [("x%d" % n
) for n
in range(2, 32)]
348 self
.gdb
.p("$pc=write_regs")
349 for i
, r
in enumerate(regs
):
350 self
.gdb
.command("p $%s=%d" % (r
, (0xdeadbeef<<i
)+17))
351 self
.gdb
.command("p $x1=data")
352 self
.gdb
.command("b all_done")
353 output
= self
.gdb
.c()
354 self
.assertIn("Breakpoint ", output
)
356 # Just to get this data in the log.
357 self
.gdb
.command("x/30gx data")
358 self
.gdb
.command("info registers")
359 for n
in range(len(regs
)):
360 self
.assertEqual(self
.gdb
.x("data+%d" % (8*n
), 'g'),
361 ((0xdeadbeef<<n
)+17) & ((1<<target
.xlen
)-1))
363 def test_write_csrs(self
):
364 # As much a test of gdb as of the simulator.
365 self
.gdb
.p("$mscratch=0")
367 self
.assertEqual(self
.gdb
.p("$mscratch"), 0)
368 self
.gdb
.p("$mscratch=123")
370 self
.assertEqual(self
.gdb
.p("$mscratch"), 123)
372 self
.gdb
.command("p $pc=write_regs")
373 self
.gdb
.command("p $a0=data")
374 self
.gdb
.command("b all_done")
375 self
.gdb
.command("c")
377 self
.assertEqual(123, self
.gdb
.p("$mscratch"))
378 self
.assertEqual(123, self
.gdb
.p("$x1"))
379 self
.assertEqual(123, self
.gdb
.p("$csr832"))
381 class DownloadTest(DeleteServer
):
383 length
= min(2**20, target
.ram_size
- 2048)
384 fd
= file("download.c", "w")
385 fd
.write("#include <stdint.h>\n")
386 fd
.write("unsigned int crc32a(uint8_t *message, unsigned int size);\n")
387 fd
.write("uint32_t length = %d;\n" % length
)
388 fd
.write("uint8_t d[%d] = {\n" % length
)
390 for i
in range(length
/ 16):
391 fd
.write(" /* 0x%04x */ " % (i
* 16));
393 value
= random
.randrange(1<<8)
394 fd
.write("%d, " % value
)
395 self
.crc
= binascii
.crc32("%c" % value
, self
.crc
)
398 fd
.write("uint8_t *data = &d[0];\n");
399 fd
.write("uint32_t main() { return crc32a(data, length); }\n")
405 self
.binary
= target
.compile("download.c", "programs/checksum.c")
406 self
.server
= target
.server()
407 self
.gdb
= testlib
.Gdb()
408 self
.gdb
.command("file %s" % self
.binary
)
409 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
411 def test_download(self
):
412 output
= self
.gdb
.load()
413 self
.gdb
.command("b _exit")
415 self
.assertEqual(self
.gdb
.p("status"), self
.crc
)
417 class MprvTest(DeleteServer
):
419 self
.binary
= target
.compile("programs/mprv.S")
420 self
.server
= target
.server()
421 self
.gdb
= testlib
.Gdb()
422 self
.gdb
.command("file %s" % self
.binary
)
423 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
427 """Test that the debugger can access memory when MPRV is set."""
428 self
.gdb
.c(wait
=False)
431 output
= self
.gdb
.command("p/x *(int*)(((char*)&data)-0x80000000)")
432 self
.assertIn("0xbead", output
)
434 class Target(object):
438 raise NotImplementedError
440 def compile(self
, *sources
):
441 return testlib
.compile(sources
+
442 ("programs/entry.S", "programs/init.c",
444 "-T", "targets/%s/link.lds" % (self
.directory
or self
.name
),
446 "-mcmodel=medany"), xlen
=self
.xlen
)
448 class Spike64Target(Target
):
452 ram_size
= 5 * 1024 * 1024
455 return testlib
.Spike(parsed
.cmd
, halted
=True)
457 class Spike32Target(Target
):
462 ram_size
= 5 * 1024 * 1024
465 return testlib
.Spike(parsed
.cmd
, halted
=True, xlen
=32)
467 class MicroSemiTarget(Target
):
474 return testlib
.Openocd(cmd
=parsed
.cmd
,
475 config
="targets/%s/openocd.cfg" % self
.name
)
484 parser
= argparse
.ArgumentParser(
486 Example command line from the real world:
487 Run all RegsTest cases against a MicroSemi m2gl_m2s board, with custom openocd command:
488 ./gdbserver.py --m2gl_m2s --cmd "$HOME/SiFive/openocd/src/openocd -s $HOME/SiFive/openocd/tcl -d" -- -vf RegsTest
490 group
= parser
.add_mutually_exclusive_group(required
=True)
492 group
.add_argument("--%s" % t
.name
, action
="store_const", const
=t
,
494 parser
.add_argument("--cmd",
495 help="The command to use to start the debug server.")
496 parser
.add_argument("unittest", nargs
="*")
498 parsed
= parser
.parse_args()
501 target
= parsed
.target()
502 unittest
.main(argv
=[sys
.argv
[0]] + parsed
.unittest
)
504 # TROUBLESHOOTING TIPS
505 # If a particular test fails, run just that one test, eg.:
506 # ./tests/gdbserver.py MprvTest.test_mprv
507 # Then inspect gdb.log and spike.log to see what happened in more detail.
509 if __name__
== '__main__':