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
11from test_gdbstub import main, report
12
13
14def check_interrupt(thread):
15    """
16    Check that, if thread is resumed, we go back to the same thread when the
17    program gets interrupted.
18    """
19
20    # Switch to the thread we're going to be running the test in.
21    print("thread ", thread.num)
22    gdb.execute("thr %d" % thread.num)
23
24    # Enter the loop() function on this thread.
25    #
26    # While there are cleaner ways to do this, we want to minimize the number of
27    # side effects on the gdbstub's internal state, since those may mask bugs.
28    # Ideally, there should be no difference between what we're doing here and
29    # the program reaching the loop() function on its own.
30    #
31    # For this to be safe, we only need the prologue of loop() to not have
32    # instructions that may have problems with what we're doing here. We don't
33    # have to worry about anything else, as this function never returns.
34    gdb.execute("set $pc = loop")
35
36    # Continue and then interrupt the task.
37    gdb.post_event(lambda: gdb.execute("interrupt"))
38    gdb.execute("c")
39
40    # Check whether the thread we're in after the interruption is the same we
41    # ran continue from.
42    return (thread.num == gdb.selected_thread().num)
43
44
45def run_test():
46    """
47    Test if interrupting the code always lands us on the same thread when
48    running with scheduler-lock enabled.
49    """
50    if len(gdb.selected_inferior().threads()) == 1:
51        print("SKIP: set to run on a single thread")
52        exit(0)
53
54    gdb.execute("set scheduler-locking on")
55    for thread in gdb.selected_inferior().threads():
56        report(check_interrupt(thread),
57               "thread %d resumes correctly on interrupt" % thread.num)
58
59
60main(run_test)
61