1from __future__ import print_function
2#
3# Test the SVE ZReg reports the right amount of data. It uses the
4# sve-ioctl test and examines the register data each time the
5# __sve_ld_done breakpoint is hit.
6#
7# This is launched via tests/guest-debug/run-test.py
8#
9
10import gdb
11import sys
12
13initial_vlen = 0
14failcount = 0
15
16def report(cond, msg):
17    "Report success/fail of test"
18    if cond:
19        print ("PASS: %s" % (msg))
20    else:
21        print ("FAIL: %s" % (msg))
22        global failcount
23        failcount += 1
24
25class TestBreakpoint(gdb.Breakpoint):
26    def __init__(self, sym_name="__sve_ld_done"):
27        super(TestBreakpoint, self).__init__(sym_name)
28        # self.sym, ok = gdb.lookup_symbol(sym_name)
29
30    def stop(self):
31        val_i = gdb.parse_and_eval('i')
32        global initial_vlen
33        try:
34            for i in range(0, int(val_i)):
35                val_z = gdb.parse_and_eval("$z0.b.u[%d]" % i)
36                report(int(val_z) == i, "z0.b.u[%d] == %d" % (i, i))
37            for i in range(i + 1, initial_vlen):
38                val_z = gdb.parse_and_eval("$z0.b.u[%d]" % i)
39                report(int(val_z) == 0, "z0.b.u[%d] == 0" % (i))
40        except gdb.error:
41            report(False, "checking zregs (out of range)")
42
43        # Check the aliased V registers are set and GDB has correctly
44        # created them for us having recognised and handled SVE.
45        try:
46            for i in range(0, 16):
47                val_z = gdb.parse_and_eval("$z0.b.u[%d]" % i)
48                val_v = gdb.parse_and_eval("$v0.b.u[%d]" % i)
49                report(int(val_z) == int(val_v),
50                       "v0.b.u[%d] == z0.b.u[%d]" % (i, i))
51        except gdb.error:
52            report(False, "checking vregs (out of range)")
53
54
55def run_test():
56    "Run through the tests one by one"
57
58    print ("Setup breakpoint")
59    bp = TestBreakpoint()
60
61    global initial_vlen
62    vg = gdb.parse_and_eval("$vg")
63    initial_vlen = int(vg) * 8
64
65    gdb.execute("c")
66
67#
68# This runs as the script it sourced (via -x, via run-test.py)
69#
70try:
71    inferior = gdb.selected_inferior()
72    arch = inferior.architecture()
73    report(arch.name() == "aarch64", "connected to aarch64")
74except (gdb.error, AttributeError):
75    print("SKIPPING (not connected)", file=sys.stderr)
76    exit(0)
77
78try:
79    # Run the actual tests
80    run_test()
81except:
82    print ("GDB Exception: %s" % (sys.exc_info()[0]))
83    failcount += 1
84    import code
85    code.InteractiveConsole(locals=globals()).interact()
86    raise
87
88print("All tests complete: %d failures" % failcount)
89exit(failcount)
90