1"""Test GDB syscall catchpoints.
2
3SPDX-License-Identifier: GPL-2.0-or-later
4"""
5from test_gdbstub import main, report
6
7
8def check_state(expected):
9    """Check the catch_syscalls_state value"""
10    actual = gdb.parse_and_eval("catch_syscalls_state").string()
11    report(actual == expected, "{} == {}".format(actual, expected))
12
13
14def run_test():
15    """Run through the tests one by one"""
16    gdb.Breakpoint("main")
17    gdb.execute("continue")
18
19    # Check that GDB stops for pipe2/read calls/returns, but not for write.
20    gdb.execute("delete")
21    try:
22        gdb.execute("catch syscall pipe2 read")
23    except gdb.error as exc:
24        exc_str = str(exc)
25        if "not supported on this architecture" in exc_str:
26            print("SKIP: {}".format(exc_str))
27            return
28        raise
29    for _ in range(2):
30        gdb.execute("continue")
31        check_state("pipe2")
32    for _ in range(2):
33        gdb.execute("continue")
34        check_state("read")
35
36    # Check that deletion works.
37    gdb.execute("delete")
38    gdb.Breakpoint("end_of_main")
39    gdb.execute("continue")
40    check_state("end")
41
42    # Check that catch-all works (libc should at least call exit).
43    gdb.execute("delete")
44    gdb.execute("catch syscall")
45    gdb.execute("continue")
46    gdb.execute("delete")
47    gdb.execute("continue")
48
49    exitcode = int(gdb.parse_and_eval("$_exitcode"))
50    report(exitcode == 0, "{} == 0".format(exitcode))
51
52
53main(run_test)
54