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