Made some progress towards working with spike.
[riscv-tests.git] / debug / gdbserver.py
1 #!/usr/bin/python
2
3 import os
4 import sys
5 import argparse
6 import testlib
7 import unittest
8 import tempfile
9 import time
10 import random
11 import binascii
12
13 class DeleteServer(unittest.TestCase):
14 def tearDown(self):
15 del self.server
16
17 class MemoryTest(DeleteServer):
18 def setUp(self):
19 self.server = target.server()
20 self.gdb = testlib.Gdb()
21 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
22
23 def test_32(self):
24 self.gdb.p("*((int*)0x%x) = 0x8675309" % target.ram)
25 self.gdb.p("*((int*)0x%x) = 0xdeadbeef" % (target.ram + 4))
26 self.assertEqual(self.gdb.p("*((int*)0x%x)" % target.ram), 0x8675309)
27 self.assertEqual(self.gdb.p("*((int*)0x%x)" % (target.ram + 4)), 0xdeadbeef)
28
29 class InstantHaltTest(DeleteServer):
30 def setUp(self):
31 self.binary = target.compile("programs/debug.c")
32 self.server = target.server(self.binary, halted=True)
33 self.gdb = testlib.Gdb()
34 self.gdb.command("file %s" % self.binary)
35 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
36
37 def test_instant_halt(self):
38 self.assertEqual(0x1000, self.gdb.p("$pc"))
39 # For some reason instret resets to 0.
40 self.assertLess(self.gdb.p("$instret"), 8)
41 self.gdb.command("stepi")
42 self.assertNotEqual(0x1000, self.gdb.p("$pc"))
43
44 def test_change_pc(self):
45 """Change the PC right as we come out of reset."""
46 # 0x13 is nop
47 self.gdb.command("p *((int*) 0x80000000)=0x13")
48 self.gdb.command("p *((int*) 0x80000004)=0x13")
49 self.gdb.command("p *((int*) 0x80000008)=0x13")
50 self.gdb.command("p $pc=0x80000000")
51 self.gdb.command("stepi")
52 self.assertEqual(0x80000004, self.gdb.p("$pc"))
53 self.gdb.command("stepi")
54 self.assertEqual(0x80000008, self.gdb.p("$pc"))
55
56 class DebugTest(DeleteServer):
57 def setUp(self):
58 self.binary = target.compile("programs/debug.c", "programs/checksum.c")
59 self.server = target.server()
60 self.gdb = testlib.Gdb()
61 self.gdb.command("file %s" % self.binary)
62 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
63 self.gdb.load(self.binary)
64 self.gdb.b("_exit")
65
66 def exit(self):
67 output = self.gdb.c()
68 self.assertIn("Breakpoint", output)
69 self.assertIn("_exit", output)
70 self.assertEqual(self.gdb.p("status"), 0xc86455d4)
71
72 def test_turbostep(self):
73 """Single step a bunch of times."""
74 self.gdb.command("p i=0");
75 last_pc = None
76 for _ in range(100):
77 self.gdb.command("stepi")
78 pc = self.gdb.command("p $pc")
79 self.assertNotEqual(last_pc, pc)
80 last_pc = pc
81
82 def test_exit(self):
83 self.exit()
84
85 def test_breakpoint(self):
86 self.gdb.b("rot13")
87 # The breakpoint should be hit exactly 2 times.
88 for i in range(2):
89 output = self.gdb.c()
90 self.assertIn("Breakpoint ", output)
91 self.exit()
92
93 def test_registers(self):
94 self.gdb.b("rot13")
95 self.gdb.c()
96 # Try both forms to test gdb.
97 for cmd in ("info all-registers", "info registers all"):
98 output = self.gdb.command(cmd)
99 self.assertNotIn("Could not", output)
100 for reg in ('zero', 'ra', 'sp', 'gp', 'tp'):
101 self.assertIn(reg, output)
102 # mcpuid is one of the few registers that should have the high bit set
103 # (for rv64).
104 # Leave this commented out until gdb and spike agree on the encoding of
105 # mcpuid (which is going to be renamed to misa in any case).
106 #self.assertRegexpMatches(output, ".*mcpuid *0x80")
107
108 # The instret register should always be changing.
109 last_instret = None
110 for _ in range(5):
111 instret = self.gdb.p("$instret")
112 self.assertNotEqual(instret, last_instret)
113 last_instret = instret
114 self.gdb.command("stepi")
115
116 self.exit()
117
118 def test_interrupt(self):
119 """Sending gdb ^C while the program is running should cause it to halt."""
120 self.gdb.b("main:start")
121 self.gdb.c()
122 self.gdb.command("p i=123");
123 self.gdb.c(wait=False)
124 time.sleep(0.1)
125 output = self.gdb.interrupt()
126 assert "main" in output
127 self.assertGreater(self.gdb.p("j"), 10)
128 self.gdb.p("i=0");
129 self.exit()
130
131 class RegsTest(DeleteServer):
132 def setUp(self):
133 self.binary = target.compile("programs/regs.S")
134 self.server = target.server()
135 self.gdb = testlib.Gdb()
136 self.gdb.command("file %s" % self.binary)
137 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
138 self.gdb.command("load")
139
140 def test_write_gprs(self):
141 # Note a0 is missing from this list since it's used to hold the
142 # address.
143 regs = ("ra", "sp", "gp", "tp", "t0", "t1", "t2", "fp", "s1",
144 "a1", "a2", "a3", "a4", "a5", "a6", "a7", "s2", "s3", "s4",
145 "s5", "s6", "s7", "s8", "s9", "s10", "s11", "t3", "t4", "t5",
146 "t6")
147
148 self.gdb.command("p $pc=write_regs")
149 for i, r in enumerate(regs):
150 self.gdb.command("p $%s=%d" % (r, (0xdeadbeef<<i)+17))
151 self.gdb.command("p $a0=data")
152 self.gdb.command("b all_done")
153 output = self.gdb.command("c")
154 self.assertIn("Breakpoint 1", output)
155
156 # Just to get this data in the log.
157 self.gdb.command("x/30gx data")
158 self.gdb.command("info registers")
159 for n in range(len(regs)):
160 self.assertEqual(self.gdb.x("data+%d" % (8*n), 'g'),
161 (0xdeadbeef<<n)+17)
162
163 def test_write_csrs(self):
164 # As much a test of gdb as of the simulator.
165 self.gdb.p("$mscratch=0")
166 self.gdb.stepi()
167 self.assertEqual(self.gdb.p("$mscratch"), 0)
168 self.gdb.p("$mscratch=123")
169 self.gdb.stepi()
170 self.assertEqual(self.gdb.p("$mscratch"), 123)
171
172 self.gdb.command("p $fflags=9")
173 self.gdb.command("p $pc=write_regs")
174 self.gdb.command("p $a0=data")
175 self.gdb.command("b all_done")
176 self.gdb.command("c")
177
178 self.assertEqual(9, self.gdb.p("$fflags"))
179 self.assertEqual(9, self.gdb.p("$x1"))
180 self.assertEqual(9, self.gdb.p("$csr1"))
181
182 class DownloadTest(DeleteServer):
183 def setUp(self):
184 length = 2**20
185 fd = file("data.c", "w")
186 # extern uint8_t *data;
187 # extern uint32_t length;
188 #
189 # uint32_t main()
190 #{
191 # /* Compute a simple checksum. */
192 # return crc32a(data, length);
193 #}
194 fd.write("#include <stdint.h>\n")
195 fd.write("uint32_t length = %d;\n" % length)
196 fd.write("uint8_t d[%d] = {\n" % length)
197 self.crc = 0
198 for i in range(length / 16):
199 fd.write(" /* 0x%04x */ " % (i * 16));
200 for _ in range(16):
201 value = random.randrange(1<<8)
202 fd.write("%d, " % value)
203 self.crc = binascii.crc32("%c" % value, self.crc)
204 fd.write("\n");
205 fd.write("};\n");
206 fd.write("uint8_t *data = &d[0];\n");
207 fd.close()
208
209 self.binary = target.compile("checksum.c", "data.c", "start.S",
210 "-mcmodel=medany",
211 "-T", "standalone.lds",
212 "-nostartfiles"
213 )
214 self.server = target.server(None, halted=True)
215 self.gdb = testlib.Gdb()
216 self.gdb.command("file %s" % self.binary)
217 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
218
219 def test_download(self):
220 output = self.gdb.command("load")
221 self.assertNotIn("failed", output)
222 self.assertIn("Transfer rate", output)
223 self.gdb.command("b done")
224 self.gdb.c()
225 result = self.gdb.p("$a0")
226 self.assertEqual(self.crc, result)
227
228 class MprvTest(DeleteServer):
229 def setUp(self):
230 self.binary = target.compile("mprv.S", "-T", "standalone.lds",
231 "-nostartfiles")
232 self.server = target.server(None, halted=True)
233 self.gdb = testlib.Gdb()
234 self.gdb.command("file %s" % self.binary)
235 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
236 self.gdb.command("load")
237
238 def test_mprv(self):
239 """Test that the debugger can access memory when MPRV is set."""
240 self.gdb.c(wait=False)
241 self.gdb.interrupt()
242 output = self.gdb.command("p/x *(int*)(((char*)&data)-0x80000000)")
243 self.assertIn("0xbead", output)
244
245 class Target(object):
246 def server(self):
247 raise NotImplementedError
248
249 def compile(self, *sources):
250 return testlib.compile(sources +
251 ("targets/%s/entry.S" % self.name, "programs/init.c",
252 "-I", "../env",
253 "-T", "targets/%s/link.lds" % self.name,
254 "-nostartfiles",
255 "-mcmodel=medany"), xlen=self.xlen)
256
257 class SpikeTarget(Target):
258 name = "spike"
259 xlen = 64
260 ram = 0x80010000
261
262 def server(self):
263 return testlib.Spike(parsed.cmd, halted=True)
264
265 class MicroSemiTarget(Target):
266 name = "m2gl_m2s"
267 xlen = 32
268 ram = 0x80000000
269
270 def server(self):
271 return testlib.Openocd(cmd=parsed.cmd,
272 config="targets/%s/openocd.cfg" % self.name)
273
274 targets = [
275 SpikeTarget,
276 MicroSemiTarget
277 ]
278
279 def main():
280 parser = argparse.ArgumentParser()
281 group = parser.add_mutually_exclusive_group(required=True)
282 for t in targets:
283 group.add_argument("--%s" % t.name, action="store_const", const=t,
284 dest="target")
285 parser.add_argument("--cmd",
286 help="The command to use to start the debug server.")
287 parser.add_argument("unittest", nargs="*")
288 global parsed
289 parsed = parser.parse_args()
290
291 global target
292 target = parsed.target()
293 unittest.main(argv=[sys.argv[0]] + parsed.unittest)
294
295 # TROUBLESHOOTING TIPS
296 # If a particular test fails, run just that one test, eg.:
297 # ./tests/gdbserver.py MprvTest.test_mprv
298 # Then inspect gdb.log and spike.log to see what happened in more detail.
299
300 if __name__ == '__main__':
301 sys.exit(main())