All tests pass with spike now.
[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.server = target.server()
32 self.gdb = testlib.Gdb()
33 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
34
35 # TODO: make work
36 # def test_instant_halt(self):
37 # self.assertEqual(0x1000, self.gdb.p("$pc"))
38 # # For some reason instret resets to 0.
39 # self.assertLess(self.gdb.p("$instret"), 8)
40 # self.gdb.command("stepi")
41 # self.assertNotEqual(0x1000, self.gdb.p("$pc"))
42
43 # TODO: make work
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*) 0x%x)=0x13" % target.ram)
48 # self.gdb.command("p *((int*) 0x%x)=0x13" % (target.ram + 4))
49 # self.gdb.command("p *((int*) 0x%x)=0x13" % (target.ram + 8))
50 # self.gdb.p("$pc=0x%x" % target.ram)
51 # self.gdb.command("stepi")
52 # self.assertEqual((target.ram + 4), self.gdb.p("$pc"))
53 # self.gdb.command("stepi")
54 # self.assertEqual((target.ram + 4), 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()
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 # Get to a point in the code where some registers have actually been
95 # used.
96 self.gdb.b("rot13")
97 self.gdb.c()
98 self.gdb.c()
99 # Try both forms to test gdb.
100 for cmd in ("info all-registers", "info registers all"):
101 output = self.gdb.command(cmd)
102 self.assertNotIn("Could not", output)
103 for reg in ('zero', 'ra', 'sp', 'gp', 'tp'):
104 self.assertIn(reg, output)
105
106 #TODO
107 # mcpuid is one of the few registers that should have the high bit set
108 # (for rv64).
109 # Leave this commented out until gdb and spike agree on the encoding of
110 # mcpuid (which is going to be renamed to misa in any case).
111 #self.assertRegexpMatches(output, ".*mcpuid *0x80")
112
113 #TODO:
114 # The instret register should always be changing.
115 #last_instret = None
116 #for _ in range(5):
117 # instret = self.gdb.p("$instret")
118 # self.assertNotEqual(instret, last_instret)
119 # last_instret = instret
120 # self.gdb.command("stepi")
121
122 self.exit()
123
124 def test_interrupt(self):
125 """Sending gdb ^C while the program is running should cause it to halt."""
126 self.gdb.b("main:start")
127 self.gdb.c()
128 self.gdb.command("p i=123");
129 self.gdb.c(wait=False)
130 time.sleep(0.1)
131 output = self.gdb.interrupt()
132 assert "main" in output
133 self.assertGreater(self.gdb.p("j"), 10)
134 self.gdb.p("i=0");
135 self.exit()
136
137 class RegsTest(DeleteServer):
138 def setUp(self):
139 self.binary = target.compile("programs/regs.S")
140 self.server = target.server()
141 self.gdb = testlib.Gdb()
142 self.gdb.command("file %s" % self.binary)
143 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
144 self.gdb.command("load")
145 self.gdb.b("main")
146 self.gdb.b("handle_trap")
147 self.gdb.c()
148
149 def test_write_gprs(self):
150 # Note a0 is missing from this list since it's used to hold the
151 # address.
152 regs = ("ra", "sp", "gp", "tp", "t0", "t1", "t2", "fp", "s1",
153 "a1", "a2", "a3", "a4", "a5", "a6", "a7", "s2", "s3", "s4",
154 "s5", "s6", "s7", "s8", "s9", "s10", "s11", "t3", "t4", "t5",
155 "t6")
156
157 self.gdb.p("$pc=write_regs")
158 for i, r in enumerate(regs):
159 self.gdb.command("p $%s=%d" % (r, (0xdeadbeef<<i)+17))
160 self.gdb.command("p $a0=data")
161 self.gdb.command("b all_done")
162 output = self.gdb.c()
163 self.assertIn("Breakpoint ", output)
164
165 # Just to get this data in the log.
166 self.gdb.command("x/30gx data")
167 self.gdb.command("info registers")
168 for n in range(len(regs)):
169 self.assertEqual(self.gdb.x("data+%d" % (8*n), 'g'),
170 (0xdeadbeef<<n)+17)
171
172 def test_write_csrs(self):
173 # As much a test of gdb as of the simulator.
174 self.gdb.p("$mscratch=0")
175 self.gdb.stepi()
176 self.assertEqual(self.gdb.p("$mscratch"), 0)
177 self.gdb.p("$mscratch=123")
178 self.gdb.stepi()
179 self.assertEqual(self.gdb.p("$mscratch"), 123)
180
181 self.gdb.command("p $pc=write_regs")
182 self.gdb.command("p $a0=data")
183 self.gdb.command("b all_done")
184 self.gdb.command("c")
185
186 self.assertEqual(123, self.gdb.p("$mscratch"))
187 self.assertEqual(123, self.gdb.p("$x1"))
188 self.assertEqual(123, self.gdb.p("$csr832"))
189
190 class DownloadTest(DeleteServer):
191 def setUp(self):
192 length = 2**20
193 fd = file("download.c", "w")
194 fd.write("#include <stdint.h>\n")
195 fd.write("unsigned int crc32a(uint8_t *message, unsigned int size);\n")
196 fd.write("uint32_t length = %d;\n" % length)
197 fd.write("uint8_t d[%d] = {\n" % length)
198 self.crc = 0
199 for i in range(length / 16):
200 fd.write(" /* 0x%04x */ " % (i * 16));
201 for _ in range(16):
202 value = random.randrange(1<<8)
203 fd.write("%d, " % value)
204 self.crc = binascii.crc32("%c" % value, self.crc)
205 fd.write("\n");
206 fd.write("};\n");
207 fd.write("uint8_t *data = &d[0];\n");
208 fd.write("uint32_t main() { return crc32a(data, length); }\n")
209 fd.close()
210
211 if self.crc < 0:
212 self.crc += 2**32
213
214 self.binary = target.compile("download.c", "programs/checksum.c")
215 self.server = target.server()
216 self.gdb = testlib.Gdb()
217 self.gdb.command("file %s" % self.binary)
218 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
219
220 def test_download(self):
221 output = self.gdb.load()
222 self.gdb.command("b _exit")
223 self.gdb.c()
224 self.assertEqual(self.gdb.p("status"), self.crc)
225
226 class MprvTest(DeleteServer):
227 def setUp(self):
228 self.binary = target.compile("programs/mprv.S")
229 self.server = target.server()
230 self.gdb = testlib.Gdb()
231 self.gdb.command("file %s" % self.binary)
232 self.gdb.command("target extended-remote localhost:%d" % self.server.port)
233 self.gdb.load()
234
235 def test_mprv(self):
236 """Test that the debugger can access memory when MPRV is set."""
237 self.gdb.c(wait=False)
238 self.gdb.interrupt()
239 output = self.gdb.command("p/x *(int*)(((char*)&data)-0x80000000)")
240 self.assertIn("0xbead", output)
241
242 class Target(object):
243 def server(self):
244 raise NotImplementedError
245
246 def compile(self, *sources):
247 return testlib.compile(sources +
248 ("programs/entry.S", "programs/init.c",
249 "-I", "../env",
250 "-T", "targets/%s/link.lds" % self.name,
251 "-nostartfiles",
252 "-mcmodel=medany"), xlen=self.xlen)
253
254 class SpikeTarget(Target):
255 name = "spike"
256 xlen = 64
257 ram = 0x80010000
258
259 def server(self):
260 return testlib.Spike(parsed.cmd, halted=True)
261
262 class MicroSemiTarget(Target):
263 name = "m2gl_m2s"
264 xlen = 32
265 ram = 0x80000000
266
267 def server(self):
268 return testlib.Openocd(cmd=parsed.cmd,
269 config="targets/%s/openocd.cfg" % self.name)
270
271 targets = [
272 SpikeTarget,
273 MicroSemiTarget
274 ]
275
276 def main():
277 parser = argparse.ArgumentParser()
278 group = parser.add_mutually_exclusive_group(required=True)
279 for t in targets:
280 group.add_argument("--%s" % t.name, action="store_const", const=t,
281 dest="target")
282 parser.add_argument("--cmd",
283 help="The command to use to start the debug server.")
284 parser.add_argument("unittest", nargs="*")
285 global parsed
286 parsed = parser.parse_args()
287
288 global target
289 target = parsed.target()
290 unittest.main(argv=[sys.argv[0]] + parsed.unittest)
291
292 # TROUBLESHOOTING TIPS
293 # If a particular test fails, run just that one test, eg.:
294 # ./tests/gdbserver.py MprvTest.test_mprv
295 # Then inspect gdb.log and spike.log to see what happened in more detail.
296
297 if __name__ == '__main__':
298 sys.exit(main())