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
44def run_test():
45    "Run through the tests one by one"
46
47    print ("Setup breakpoint")
48    bp = TestBreakpoint()
49
50    global initial_vlen
51    vg = gdb.parse_and_eval("$vg")
52    initial_vlen = int(vg) * 8
53
54    gdb.execute("c")
55
56#
57# This runs as the script it sourced (via -x, via run-test.py)
58#
59try:
60    inferior = gdb.selected_inferior()
61    if inferior.was_attached == False:
62        print("SKIPPING (failed to attach)", file=sys.stderr)
63        exit(0)
64    arch = inferior.architecture()
65    report(arch.name() == "aarch64", "connected to aarch64")
66except (gdb.error, AttributeError):
67    print("SKIPPING (not connected)", file=sys.stderr)
68    exit(0)
69
70try:
71    # These are not very useful in scripts
72    gdb.execute("set pagination off")
73    gdb.execute("set confirm off")
74
75    # Run the actual tests
76    run_test()
77except:
78    print ("GDB Exception: %s" % (sys.exc_info()[0]))
79    failcount += 1
80    import code
81    code.InteractiveConsole(locals=globals()).interact()
82    raise
83
84print("All tests complete: %d failures" % failcount)
85exit(failcount)
86