b5363e644c340edfcd194b2e3ea75d6b937b3fdd
[riscv-isa-sim.git] / tests / gdbserver.py
1 #!/usr/bin/python
2
3 import os
4 import testlib
5 import unittest
6 import tempfile
7 import time
8
9 class DebugTest(unittest.TestCase):
10 def setUp(self):
11 self.binary = testlib.compile("debug.c")
12 self.spike = testlib.spike(self.binary, halted=False)
13 self.gdb = testlib.Gdb()
14 self.gdb.command("file %s" % self.binary)
15 self.gdb.command("target extended-remote localhost:9824")
16 self.gdb.command("p i=0");
17
18 def tearDown(self):
19 self.spike.kill()
20 self.spike.wait()
21
22 def test_turbostep(self):
23 """Single step a bunch of times."""
24 last_pc = None
25 for _ in range(100):
26 self.gdb.command("stepi")
27 pc = self.gdb.command("p $pc")
28 self.assertNotEqual(last_pc, pc)
29 last_pc = pc
30
31 def test_exit(self):
32 output = self.gdb.command("c")
33 self.assertIn("Continuing", output)
34 self.assertIn("Remote connection closed", output)
35
36 def test_breakpoint(self):
37 self.gdb.command("b print_row")
38 # The breakpoint should be hit exactly 10 times.
39 for _ in range(10):
40 output = self.gdb.command("c")
41 self.assertIn("Continuing", output)
42 self.assertIn("Breakpoint 1", output)
43 output = self.gdb.command("c")
44 self.assertIn("Continuing", output)
45 self.assertIn("Remote connection closed", output)
46
47 def test_registers(self):
48 # Try both forms to test gdb.
49 for cmd in ("info all-registers", "info registers all"):
50 output = self.gdb.command(cmd)
51 self.assertNotIn("Could not", output)
52 for reg in ('zero', 'ra', 'sp', 'gp', 'tp'):
53 self.assertIn(reg, output)
54 # mcpuid is one of the few registers that should have the high bit set
55 # (for rv64).
56 # Leave this commented out until gdb and spike agree on the encoding of
57 # mcpuid (which is going to be renamed to misa in any case).
58 #self.assertRegexpMatches(output, ".*mcpuid *0x80")
59
60 # The time register should always be changing.
61 last_time = None
62 for _ in range(5):
63 time = self.gdb.p("$time")
64 self.assertNotEqual(time, last_time)
65 last_time = time
66 self.gdb.command("stepi")
67
68 class RegsTest(unittest.TestCase):
69 def setUp(self):
70 self.binary = testlib.compile("regs.s")
71 self.spike = testlib.spike(self.binary, halted=False)
72 self.gdb = testlib.Gdb()
73 self.gdb.command("file %s" % self.binary)
74 self.gdb.command("target extended-remote localhost:9824")
75
76 def tearDown(self):
77 self.spike.kill()
78 self.spike.wait()
79
80 def test_write_gprs(self):
81 # Note a0 is missing from this list since it's used to hold the
82 # address.
83 regs = ("ra", "sp", "gp", "tp", "t0", "t1", "t2", "fp", "s1",
84 "a1", "a2", "a3", "a4", "a5", "a6", "a7", "s2", "s3", "s4",
85 "s5", "s6", "s7", "s8", "s9", "s10", "s11", "t3", "t4", "t5",
86 "t6")
87
88 self.gdb.command("p $pc=write_regs")
89 for i, r in enumerate(regs):
90 self.gdb.command("p $%s=%d" % (r, (0xdeadbeef<<i)+17))
91 self.gdb.command("p $a0=data")
92 self.gdb.command("b all_done")
93 output = self.gdb.command("c")
94 self.assertIn("Breakpoint 1", output)
95
96 # Just to get this data in the log.
97 self.gdb.command("x/30gx data")
98 self.gdb.command("info registers")
99 for n in range(len(regs)):
100 self.assertEqual(self.gdb.x("data+%d" % (8*n), 'g'),
101 (0xdeadbeef<<n)+17)
102
103 def test_write_csrs(self):
104 # As much a test of gdb as of the simulator.
105 self.gdb.p("$mscratch=0")
106 self.gdb.stepi()
107 self.assertEqual(self.gdb.p("$mscratch"), 0)
108 self.gdb.p("$mscratch=123")
109 self.gdb.stepi()
110 self.assertEqual(self.gdb.p("$mscratch"), 123)
111
112 self.gdb.command("p $fflags=9")
113 self.gdb.command("p $pc=write_regs")
114 self.gdb.command("p $a0=data")
115 self.gdb.command("b all_done")
116 self.gdb.command("c")
117
118 self.assertEqual(9, self.gdb.p("$fflags"))
119 self.assertEqual(9, self.gdb.p("$x1"))
120 self.assertEqual(9, self.gdb.p("$csr1"))
121
122 if __name__ == '__main__':
123 unittest.main()