xref: /qemu/tests/tcg/multiarch/gdbstub/interrupt.py (revision 70e651279541424404788d55d5b3c40846f04215)
1761e3c10SMatheus Branco Borellafrom __future__ import print_function
2761e3c10SMatheus Branco Borella#
37893e42dSPhilippe Mathieu-Daudé# Test some of the system debug features with the multiarch memory
4761e3c10SMatheus Branco Borella# test. It is a port of the original vmlinux focused test case but
5761e3c10SMatheus Branco Borella# using the "memory" test instead.
6761e3c10SMatheus Branco Borella#
7761e3c10SMatheus Branco Borella# This is launched via tests/guest-debug/run-test.py
8761e3c10SMatheus Branco Borella#
9761e3c10SMatheus Branco Borella
10761e3c10SMatheus Branco Borellaimport gdb
11*cb241df4SIlya Leoshkevichfrom test_gdbstub import gdb_exit, main, report
12761e3c10SMatheus Branco Borella
13761e3c10SMatheus Branco Borella
14761e3c10SMatheus Branco Borelladef check_interrupt(thread):
15761e3c10SMatheus Branco Borella    """
16761e3c10SMatheus Branco Borella    Check that, if thread is resumed, we go back to the same thread when the
17761e3c10SMatheus Branco Borella    program gets interrupted.
18761e3c10SMatheus Branco Borella    """
19761e3c10SMatheus Branco Borella
20761e3c10SMatheus Branco Borella    # Switch to the thread we're going to be running the test in.
21761e3c10SMatheus Branco Borella    print("thread ", thread.num)
22761e3c10SMatheus Branco Borella    gdb.execute("thr %d" % thread.num)
23761e3c10SMatheus Branco Borella
24761e3c10SMatheus Branco Borella    # Enter the loop() function on this thread.
25761e3c10SMatheus Branco Borella    #
26761e3c10SMatheus Branco Borella    # While there are cleaner ways to do this, we want to minimize the number of
27761e3c10SMatheus Branco Borella    # side effects on the gdbstub's internal state, since those may mask bugs.
28761e3c10SMatheus Branco Borella    # Ideally, there should be no difference between what we're doing here and
29761e3c10SMatheus Branco Borella    # the program reaching the loop() function on its own.
30761e3c10SMatheus Branco Borella    #
31761e3c10SMatheus Branco Borella    # For this to be safe, we only need the prologue of loop() to not have
32761e3c10SMatheus Branco Borella    # instructions that may have problems with what we're doing here. We don't
33761e3c10SMatheus Branco Borella    # have to worry about anything else, as this function never returns.
34761e3c10SMatheus Branco Borella    gdb.execute("set $pc = loop")
35761e3c10SMatheus Branco Borella
36761e3c10SMatheus Branco Borella    # Continue and then interrupt the task.
37761e3c10SMatheus Branco Borella    gdb.post_event(lambda: gdb.execute("interrupt"))
38761e3c10SMatheus Branco Borella    gdb.execute("c")
39761e3c10SMatheus Branco Borella
40761e3c10SMatheus Branco Borella    # Check whether the thread we're in after the interruption is the same we
41761e3c10SMatheus Branco Borella    # ran continue from.
42761e3c10SMatheus Branco Borella    return (thread.num == gdb.selected_thread().num)
43761e3c10SMatheus Branco Borella
44761e3c10SMatheus Branco Borella
45761e3c10SMatheus Branco Borelladef run_test():
46761e3c10SMatheus Branco Borella    """
47761e3c10SMatheus Branco Borella    Test if interrupting the code always lands us on the same thread when
48761e3c10SMatheus Branco Borella    running with scheduler-lock enabled.
49761e3c10SMatheus Branco Borella    """
504d48c1bcSIlya Leoshkevich    if len(gdb.selected_inferior().threads()) == 1:
514d48c1bcSIlya Leoshkevich        print("SKIP: set to run on a single thread")
52*cb241df4SIlya Leoshkevich        gdb_exit(0)
53761e3c10SMatheus Branco Borella
54761e3c10SMatheus Branco Borella    gdb.execute("set scheduler-locking on")
55761e3c10SMatheus Branco Borella    for thread in gdb.selected_inferior().threads():
56761e3c10SMatheus Branco Borella        report(check_interrupt(thread),
57761e3c10SMatheus Branco Borella               "thread %d resumes correctly on interrupt" % thread.num)
58761e3c10SMatheus Branco Borella
59761e3c10SMatheus Branco Borella
604d48c1bcSIlya Leoshkevichmain(run_test)
61