1from __future__ import print_function
2#
3# Test some of the system debug features with the multiarch memory
4# test. It is a port of the original vmlinux focused test case but
5# using the "memory" test instead.
6#
7# This is launched via tests/guest-debug/run-test.py
8#
9
10import gdb
11import sys
12
13failcount = 0
14
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
25
26def check_interrupt(thread):
27    """
28    Check that, if thread is resumed, we go back to the same thread when the
29    program gets interrupted.
30    """
31
32    # Switch to the thread we're going to be running the test in.
33    print("thread ", thread.num)
34    gdb.execute("thr %d" % thread.num)
35
36    # Enter the loop() function on this thread.
37    #
38    # While there are cleaner ways to do this, we want to minimize the number of
39    # side effects on the gdbstub's internal state, since those may mask bugs.
40    # Ideally, there should be no difference between what we're doing here and
41    # the program reaching the loop() function on its own.
42    #
43    # For this to be safe, we only need the prologue of loop() to not have
44    # instructions that may have problems with what we're doing here. We don't
45    # have to worry about anything else, as this function never returns.
46    gdb.execute("set $pc = loop")
47
48    # Continue and then interrupt the task.
49    gdb.post_event(lambda: gdb.execute("interrupt"))
50    gdb.execute("c")
51
52    # Check whether the thread we're in after the interruption is the same we
53    # ran continue from.
54    return (thread.num == gdb.selected_thread().num)
55
56
57def run_test():
58    """
59    Test if interrupting the code always lands us on the same thread when
60    running with scheduler-lock enabled.
61    """
62
63    gdb.execute("set scheduler-locking on")
64    for thread in gdb.selected_inferior().threads():
65        report(check_interrupt(thread),
66               "thread %d resumes correctly on interrupt" % thread.num)
67
68
69#
70# This runs as the script it sourced (via -x, via run-test.py)
71#
72try:
73    inferior = gdb.selected_inferior()
74    arch = inferior.architecture()
75    print("ATTACHED: %s" % arch.name())
76except (gdb.error, AttributeError):
77    print("SKIPPING (not connected)", file=sys.stderr)
78    exit(0)
79
80if gdb.parse_and_eval('$pc') == 0:
81    print("SKIP: PC not set")
82    exit(0)
83if len(gdb.selected_inferior().threads()) == 1:
84    print("SKIP: set to run on a single thread")
85    exit(0)
86
87try:
88    # Run the actual tests
89    run_test()
90except (gdb.error):
91    print("GDB Exception: %s" % (sys.exc_info()[0]))
92    failcount += 1
93    pass
94
95# Finally kill the inferior and exit gdb with a count of failures
96gdb.execute("kill")
97exit(failcount)
98