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 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
49 self
.gdb
.command("p *((int*) 0x%x)=0x13" % target
.ram
)
50 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 4))
51 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 8))
52 self
.gdb
.p("$pc=0x%x" % target
.ram
)
54 def check_reg(self
, name
):
55 a
= random
.randrange(1<<target
.xlen
)
56 b
= random
.randrange(1<<target
.xlen
)
57 self
.gdb
.p("$%s=0x%x" % (name
, a
))
59 self
.assertEqual(self
.gdb
.p("$%s" % name
), a
)
60 self
.gdb
.p("$%s=0x%x" % (name
, b
))
62 self
.assertEqual(self
.gdb
.p("$%s" % name
), b
)
65 # S0 is saved/restored in DSCRATCH
69 # S1 is saved/restored in Debug RAM
73 # T0 is not saved/restored at all
77 # T2 is not saved/restored at all
80 class SimpleMemoryTest(DeleteServer
):
82 self
.server
= target
.server()
83 self
.gdb
= testlib
.Gdb()
84 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
86 def access_test(self
, size
, data_type
):
87 a
= 0x86753095555aaaa & ((1<<(size
*8))-1)
88 b
= 0xdeadbeef12345678 & ((1<<(size
*8))-1)
89 self
.gdb
.p("*((%s*)0x%x) = 0x%x" % (data_type
, target
.ram
, a
))
90 self
.gdb
.p("*((%s*)0x%x) = 0x%x" % (data_type
, target
.ram
+ size
, b
))
91 self
.assertEqual(self
.gdb
.p("*((%s*)0x%x)" % (data_type
, target
.ram
)), a
)
92 self
.assertEqual(self
.gdb
.p("*((%s*)0x%x)" % (data_type
, target
.ram
+ size
)), b
)
95 self
.access_test(1, 'char')
98 self
.access_test(2, 'short')
101 self
.access_test(4, 'long')
104 self
.access_test(8, 'long long')
106 def test_block(self
):
109 fd
= file("write.ihex", "w")
111 for i
in range(length
/ line_length
):
112 line_data
= "".join(["%c" % random
.randrange(256) for _
in range(line_length
)])
114 fd
.write(ihex_line(i
* line_length
, 0, line_data
))
117 self
.gdb
.command("restore write.ihex 0x%x" % target
.ram
)
118 for offset
in range(0, length
, 19*4) + [length
-4]:
119 value
= self
.gdb
.p("*((long*)0x%x)" % (target
.ram
+ offset
))
120 written
= ord(data
[offset
]) | \
121 (ord(data
[offset
+1]) << 8) | \
122 (ord(data
[offset
+2]) << 16) | \
123 (ord(data
[offset
+3]) << 24)
124 self
.assertEqual(value
, written
)
126 self
.gdb
.command("dump ihex memory read.ihex 0x%x 0x%x" % (target
.ram
,
127 target
.ram
+ length
))
128 for line
in file("read.ihex"):
129 record_type
, address
, line_data
= ihex_parse(line
)
130 if (record_type
== 0):
131 self
.assertEqual(line_data
, data
[address
:address
+len(line_data
)])
133 class InstantHaltTest(DeleteServer
):
135 self
.server
= target
.server()
136 self
.gdb
= testlib
.Gdb()
137 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
139 def test_instant_halt(self
):
140 self
.assertEqual(0x1000, self
.gdb
.p("$pc"))
141 # For some reason instret resets to 0.
142 self
.assertLess(self
.gdb
.p("$instret"), 8)
144 self
.assertNotEqual(0x1000, self
.gdb
.p("$pc"))
146 def test_change_pc(self
):
147 """Change the PC right as we come out of reset."""
149 self
.gdb
.command("p *((int*) 0x%x)=0x13" % target
.ram
)
150 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 4))
151 self
.gdb
.command("p *((int*) 0x%x)=0x13" % (target
.ram
+ 8))
152 self
.gdb
.p("$pc=0x%x" % target
.ram
)
154 self
.assertEqual((target
.ram
+ 4), self
.gdb
.p("$pc"))
156 self
.assertEqual((target
.ram
+ 8), self
.gdb
.p("$pc"))
158 class DebugTest(DeleteServer
):
160 self
.binary
= target
.compile("programs/debug.c", "programs/checksum.c")
161 self
.server
= target
.server()
162 self
.gdb
= testlib
.Gdb()
163 self
.gdb
.command("file %s" % self
.binary
)
164 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
169 output
= self
.gdb
.c()
170 self
.assertIn("Breakpoint", output
)
171 #TODO self.assertIn("_exit", output)
172 #TODO self.assertEqual(self.gdb.p("status"), 0xc86455d4)
173 # Use a0 until gdb can resolve "status"
174 self
.assertEqual(self
.gdb
.p("$a0") & 0xffffffff, 0xc86455d4)
176 def test_turbostep(self
):
177 """Single step a bunch of times."""
178 self
.gdb
.command("p i=0");
182 pc
= self
.gdb
.command("p $pc")
183 self
.assertNotEqual(last_pc
, pc
)
189 def test_breakpoint(self
):
191 # The breakpoint should be hit exactly 2 times.
193 output
= self
.gdb
.c()
195 self
.assertIn("Breakpoint ", output
)
196 #TODO self.assertIn("rot13 ", output)
200 self
.gdb
.hbreak("rot13")
201 # The breakpoint should be hit exactly 2 times.
203 output
= self
.gdb
.c()
205 self
.assertIn("Breakpoint ", output
)
206 #TODO self.assertIn("rot13 ", output)
209 def test_registers(self
):
210 # Get to a point in the code where some registers have actually been
215 # Try both forms to test gdb.
216 for cmd
in ("info all-registers", "info registers all"):
217 output
= self
.gdb
.command(cmd
)
218 self
.assertNotIn("Could not", output
)
219 for reg
in ('zero', 'ra', 'sp', 'gp', 'tp'):
220 self
.assertIn(reg
, output
)
223 # mcpuid is one of the few registers that should have the high bit set
225 # Leave this commented out until gdb and spike agree on the encoding of
226 # mcpuid (which is going to be renamed to misa in any case).
227 #self.assertRegexpMatches(output, ".*mcpuid *0x80")
230 # The instret register should always be changing.
233 # instret = self.gdb.p("$instret")
234 # self.assertNotEqual(instret, last_instret)
235 # last_instret = instret
240 def test_interrupt(self
):
241 """Sending gdb ^C while the program is running should cause it to halt."""
242 self
.gdb
.b("main:start")
245 self
.gdb
.c(wait
=False)
247 output
= self
.gdb
.interrupt()
248 #TODO: assert "main" in output
249 self
.assertGreater(self
.gdb
.p("j"), 10)
253 class RegsTest(DeleteServer
):
255 self
.binary
= target
.compile("programs/regs.S")
256 self
.server
= target
.server()
257 self
.gdb
= testlib
.Gdb()
258 self
.gdb
.command("file %s" % self
.binary
)
259 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
262 self
.gdb
.b("handle_trap")
265 def test_write_gprs(self
):
266 regs
= [("x%d" % n
) for n
in range(2, 32)]
268 self
.gdb
.p("$pc=write_regs")
269 for i
, r
in enumerate(regs
):
270 self
.gdb
.command("p $%s=%d" % (r
, (0xdeadbeef<<i
)+17))
271 self
.gdb
.command("p $x1=data")
272 self
.gdb
.command("b all_done")
273 output
= self
.gdb
.c()
274 self
.assertIn("Breakpoint ", output
)
276 # Just to get this data in the log.
277 self
.gdb
.command("x/30gx data")
278 self
.gdb
.command("info registers")
279 for n
in range(len(regs
)):
280 self
.assertEqual(self
.gdb
.x("data+%d" % (8*n
), 'g'),
281 ((0xdeadbeef<<n
)+17) & ((1<<target
.xlen
)-1))
283 def test_write_csrs(self
):
284 # As much a test of gdb as of the simulator.
285 self
.gdb
.p("$mscratch=0")
287 self
.assertEqual(self
.gdb
.p("$mscratch"), 0)
288 self
.gdb
.p("$mscratch=123")
290 self
.assertEqual(self
.gdb
.p("$mscratch"), 123)
292 self
.gdb
.command("p $pc=write_regs")
293 self
.gdb
.command("p $a0=data")
294 self
.gdb
.command("b all_done")
295 self
.gdb
.command("c")
297 self
.assertEqual(123, self
.gdb
.p("$mscratch"))
298 self
.assertEqual(123, self
.gdb
.p("$x1"))
299 self
.assertEqual(123, self
.gdb
.p("$csr832"))
301 class DownloadTest(DeleteServer
):
304 fd
= file("download.c", "w")
305 fd
.write("#include <stdint.h>\n")
306 fd
.write("unsigned int crc32a(uint8_t *message, unsigned int size);\n")
307 fd
.write("uint32_t length = %d;\n" % length
)
308 fd
.write("uint8_t d[%d] = {\n" % length
)
310 for i
in range(length
/ 16):
311 fd
.write(" /* 0x%04x */ " % (i
* 16));
313 value
= random
.randrange(1<<8)
314 fd
.write("%d, " % value
)
315 self
.crc
= binascii
.crc32("%c" % value
, self
.crc
)
318 fd
.write("uint8_t *data = &d[0];\n");
319 fd
.write("uint32_t main() { return crc32a(data, length); }\n")
325 self
.binary
= target
.compile("download.c", "programs/checksum.c")
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
)
331 def test_download(self
):
332 output
= self
.gdb
.load()
333 self
.gdb
.command("b _exit")
335 self
.assertEqual(self
.gdb
.p("status"), self
.crc
)
337 class MprvTest(DeleteServer
):
339 self
.binary
= target
.compile("programs/mprv.S")
340 self
.server
= target
.server()
341 self
.gdb
= testlib
.Gdb()
342 self
.gdb
.command("file %s" % self
.binary
)
343 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
347 """Test that the debugger can access memory when MPRV is set."""
348 self
.gdb
.c(wait
=False)
350 output
= self
.gdb
.command("p/x *(int*)(((char*)&data)-0x80000000)")
351 self
.assertIn("0xbead", output
)
353 class Target(object):
357 raise NotImplementedError
359 def compile(self
, *sources
):
360 return testlib
.compile(sources
+
361 ("programs/entry.S", "programs/init.c",
363 "-T", "targets/%s/link.lds" % (self
.directory
or self
.name
),
365 "-mcmodel=medany"), xlen
=self
.xlen
)
367 class Spike64Target(Target
):
373 return testlib
.Spike(parsed
.cmd
, halted
=True)
375 class Spike32Target(Target
):
382 return testlib
.Spike(parsed
.cmd
, halted
=True, xlen
=32)
384 class MicroSemiTarget(Target
):
390 return testlib
.Openocd(cmd
=parsed
.cmd
,
391 config
="targets/%s/openocd.cfg" % self
.name
)
400 parser
= argparse
.ArgumentParser(
402 Example command line from the real world:
403 Run all RegsTest cases against a MicroSemi m2gl_m2s board, with custom openocd command:
404 ./gdbserver.py --m2gl_m2s --cmd "$HOME/SiFive/openocd/src/openocd -s $HOME/SiFive/openocd/tcl -d" -- -vf RegsTest
406 group
= parser
.add_mutually_exclusive_group(required
=True)
408 group
.add_argument("--%s" % t
.name
, action
="store_const", const
=t
,
410 parser
.add_argument("--cmd",
411 help="The command to use to start the debug server.")
412 parser
.add_argument("unittest", nargs
="*")
414 parsed
= parser
.parse_args()
417 target
= parsed
.target()
418 unittest
.main(argv
=[sys
.argv
[0]] + parsed
.unittest
)
420 # TROUBLESHOOTING TIPS
421 # If a particular test fails, run just that one test, eg.:
422 # ./tests/gdbserver.py MprvTest.test_mprv
423 # Then inspect gdb.log and spike.log to see what happened in more detail.
425 if __name__
== '__main__':