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