5b78489526c8e2cbc52dd86e9f211ee9d83dbf47
13 class DeleteServer(unittest
.TestCase
):
17 class MemoryTest(DeleteServer
):
19 self
.server
= target
.server()
20 self
.gdb
= testlib
.Gdb()
21 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
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)
29 class InstantHaltTest(DeleteServer
):
31 self
.server
= target
.server()
32 self
.gdb
= testlib
.Gdb()
33 self
.gdb
.command("target extended-remote localhost:%d" % self
.server
.port
)
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"))
44 # def test_change_pc(self):
45 # """Change the PC right as we come out of reset."""
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"))
56 class DebugTest(DeleteServer
):
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
)
68 self
.assertIn("Breakpoint", output
)
69 self
.assertIn("_exit", output
)
70 self
.assertEqual(self
.gdb
.p("status"), 0xc86455d4)
72 def test_turbostep(self
):
73 """Single step a bunch of times."""
74 self
.gdb
.command("p i=0");
77 self
.gdb
.command("stepi")
78 pc
= self
.gdb
.command("p $pc")
79 self
.assertNotEqual(last_pc
, pc
)
85 def test_breakpoint(self
):
87 # The breakpoint should be hit exactly 2 times.
90 self
.assertIn("Breakpoint ", output
)
93 def test_registers(self
):
94 # Get to a point in the code where some registers have actually been
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
)
107 # mcpuid is one of the few registers that should have the high bit set
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")
114 # The instret register should always be changing.
117 # instret = self.gdb.p("$instret")
118 # self.assertNotEqual(instret, last_instret)
119 # last_instret = instret
120 # self.gdb.command("stepi")
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")
128 self
.gdb
.command("p i=123");
129 self
.gdb
.c(wait
=False)
131 output
= self
.gdb
.interrupt()
132 assert "main" in output
133 self
.assertGreater(self
.gdb
.p("j"), 10)
137 class RegsTest(DeleteServer
):
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")
146 self
.gdb
.b("handle_trap")
149 def test_write_gprs(self
):
150 # Note a0 is missing from this list since it's used to hold the
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",
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
)
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'),
172 def test_write_csrs(self
):
173 # As much a test of gdb as of the simulator.
174 self
.gdb
.p("$mscratch=0")
176 self
.assertEqual(self
.gdb
.p("$mscratch"), 0)
177 self
.gdb
.p("$mscratch=123")
179 self
.assertEqual(self
.gdb
.p("$mscratch"), 123)
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")
186 self
.assertEqual(123, self
.gdb
.p("$mscratch"))
187 self
.assertEqual(123, self
.gdb
.p("$x1"))
188 self
.assertEqual(123, self
.gdb
.p("$csr832"))
190 class DownloadTest(DeleteServer
):
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
)
199 for i
in range(length
/ 16):
200 fd
.write(" /* 0x%04x */ " % (i
* 16));
202 value
= random
.randrange(1<<8)
203 fd
.write("%d, " % value
)
204 self
.crc
= binascii
.crc32("%c" % value
, self
.crc
)
207 fd
.write("uint8_t *data = &d[0];\n");
208 fd
.write("uint32_t main() { return crc32a(data, length); }\n")
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
)
220 def test_download(self
):
221 output
= self
.gdb
.load()
222 self
.gdb
.command("b _exit")
224 self
.assertEqual(self
.gdb
.p("status"), self
.crc
)
226 class MprvTest(DeleteServer
):
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
)
236 """Test that the debugger can access memory when MPRV is set."""
237 self
.gdb
.c(wait
=False)
239 output
= self
.gdb
.command("p/x *(int*)(((char*)&data)-0x80000000)")
240 self
.assertIn("0xbead", output
)
242 class Target(object):
244 raise NotImplementedError
246 def compile(self
, *sources
):
247 return testlib
.compile(sources
+
248 ("programs/entry.S", "programs/init.c",
250 "-T", "targets/%s/link.lds" % self
.name
,
252 "-mcmodel=medany"), xlen
=self
.xlen
)
254 class SpikeTarget(Target
):
260 return testlib
.Spike(parsed
.cmd
, halted
=True)
262 class MicroSemiTarget(Target
):
268 return testlib
.Openocd(cmd
=parsed
.cmd
,
269 config
="targets/%s/openocd.cfg" % self
.name
)
277 parser
= argparse
.ArgumentParser()
278 group
= parser
.add_mutually_exclusive_group(required
=True)
280 group
.add_argument("--%s" % t
.name
, action
="store_const", const
=t
,
282 parser
.add_argument("--cmd",
283 help="The command to use to start the debug server.")
284 parser
.add_argument("unittest", nargs
="*")
286 parsed
= parser
.parse_args()
289 target
= parsed
.target()
290 unittest
.main(argv
=[sys
.argv
[0]] + parsed
.unittest
)
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.
297 if __name__
== '__main__':