13 class DeleteServer(unittest
.TestCase
):
17 class InstantHaltTest(DeleteServer
):
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
)
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"))
32 def test_change_pc(self
):
33 """Change the PC right as we come out of reset."""
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"))
44 class DebugTest(DeleteServer
):
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
)
52 def test_turbostep(self
):
53 """Single step a bunch of times."""
54 self
.gdb
.command("p i=0");
57 self
.gdb
.command("stepi")
58 pc
= self
.gdb
.command("p $pc")
59 self
.assertNotEqual(last_pc
, pc
)
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
)
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.
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
)
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
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")
95 # The instret register should always be changing.
98 instret
= self
.gdb
.p("$instret")
99 self
.assertNotEqual(instret
, last_instret
)
100 last_instret
= instret
101 self
.gdb
.command("stepi")
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)
108 self
.gdb
.command("p i=123");
109 self
.gdb
.c(wait
=False)
112 self
.gdb
.command("p i=0");
113 output
= self
.gdb
.c()
114 self
.assertIn("Continuing", output
)
115 self
.assertIn("Remote connection closed", output
)
117 class RegsTest(DeleteServer
):
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")
126 def test_write_gprs(self
):
127 # Note a0 is missing from this list since it's used to hold the
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",
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
)
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'),
149 def test_write_csrs(self
):
150 # As much a test of gdb as of the simulator.
151 self
.gdb
.p("$mscratch=0")
153 self
.assertEqual(self
.gdb
.p("$mscratch"), 0)
154 self
.gdb
.p("$mscratch=123")
156 self
.assertEqual(self
.gdb
.p("$mscratch"), 123)
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")
164 self
.assertEqual(9, self
.gdb
.p("$fflags"))
165 self
.assertEqual(9, self
.gdb
.p("$x1"))
166 self
.assertEqual(9, self
.gdb
.p("$csr1"))
168 class DownloadTest(DeleteServer
):
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
)
176 for i
in range(length
/ 16):
177 fd
.write(" /* 0x%04x */ " % (i
* 16));
179 value
= random
.randrange(1<<8)
180 fd
.write("%d, " % value
)
181 self
.crc
= binascii
.crc32("%c" % value
, self
.crc
)
184 fd
.write("uint8_t *data = &d[0];\n");
187 self
.binary
= target
.compile("checksum.c", "data.c", "start.S",
189 "-T", "standalone.lds",
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
)
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")
203 result
= self
.gdb
.p("$a0")
204 self
.assertEqual(self
.crc
, result
)
206 class MprvTest(DeleteServer
):
208 self
.binary
= target
.compile("mprv.S", "-T", "standalone.lds",
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")
217 """Test that the debugger can access memory when MPRV is set."""
218 self
.gdb
.c(wait
=False)
220 output
= self
.gdb
.command("p/x *(int*)(((char*)&data)-0x80000000)")
221 self
.assertIn("0xbead", output
)
223 class Target(object):
225 raise NotImplementedError
227 def compile(self
, *sources
):
228 return testlib
.compile(*(sources
+
229 ("targets/%s/entry.S" % self
.name
, "programs/init.c",
231 "-T", "targets/%s/link.lds" % self
.name
,
234 class SpikeTarget(Target
):
237 class MicroSemiTarget(Target
):
241 return testlib
.Openocd(cmd
=parsed
.openocd
,
242 config
="targets/%s/openocd.cfg" % self
.name
)
250 parser
= argparse
.ArgumentParser()
251 group
= parser
.add_mutually_exclusive_group(required
=True)
253 group
.add_argument("--%s" % t
.name
, action
="store_const", const
=t
,
255 parser
.add_argument("--openocd", help="The OpenOCD command to use.",
257 parser
.add_argument("unittest", nargs
="*")
259 parsed
= parser
.parse_args()
262 target
= parsed
.target()
263 unittest
.main(argv
=[sys
.argv
[0]] + parsed
.unittest
)
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.
270 if __name__
== '__main__':