xref: /qemu/tests/guest-debug/test_gdbstub.py (revision 70e651279541424404788d55d5b3c40846f04215)
14d48c1bcSIlya Leoshkevich"""Helper functions for gdbstub testing
24d48c1bcSIlya Leoshkevich
34d48c1bcSIlya Leoshkevich"""
44d48c1bcSIlya Leoshkevichfrom __future__ import print_function
53848409eSGustavo Romeroimport argparse
64d48c1bcSIlya Leoshkevichimport gdb
74d48c1bcSIlya Leoshkevichimport os
84d48c1bcSIlya Leoshkevichimport sys
94d48c1bcSIlya Leoshkevichimport traceback
104d48c1bcSIlya Leoshkevich
114d48c1bcSIlya Leoshkevichfail_count = 0
124d48c1bcSIlya Leoshkevich
13*cb241df4SIlya Leoshkevich
14*cb241df4SIlya Leoshkevichdef gdb_exit(status):
15*cb241df4SIlya Leoshkevich    gdb.execute(f"exit {status}")
16*cb241df4SIlya Leoshkevich
17*cb241df4SIlya Leoshkevich
183848409eSGustavo Romeroclass arg_parser(argparse.ArgumentParser):
193848409eSGustavo Romero    def exit(self, status=None, message=""):
203848409eSGustavo Romero        print("Wrong GDB script test argument! " + message)
21*cb241df4SIlya Leoshkevich        gdb_exit(1)
22*cb241df4SIlya Leoshkevich
234d48c1bcSIlya Leoshkevich
244d48c1bcSIlya Leoshkevichdef report(cond, msg):
254d48c1bcSIlya Leoshkevich    """Report success/fail of a test"""
264d48c1bcSIlya Leoshkevich    if cond:
274d48c1bcSIlya Leoshkevich        print("PASS: {}".format(msg))
284d48c1bcSIlya Leoshkevich    else:
294d48c1bcSIlya Leoshkevich        print("FAIL: {}".format(msg))
304d48c1bcSIlya Leoshkevich        global fail_count
314d48c1bcSIlya Leoshkevich        fail_count += 1
324d48c1bcSIlya Leoshkevich
334d48c1bcSIlya Leoshkevich
344d48c1bcSIlya Leoshkevichdef main(test, expected_arch=None):
354d48c1bcSIlya Leoshkevich    """Run a test function
364d48c1bcSIlya Leoshkevich
374d48c1bcSIlya Leoshkevich    This runs as the script it sourced (via -x, via run-test.py)."""
384d48c1bcSIlya Leoshkevich    try:
394d48c1bcSIlya Leoshkevich        inferior = gdb.selected_inferior()
404d48c1bcSIlya Leoshkevich        arch = inferior.architecture()
414d48c1bcSIlya Leoshkevich        print("ATTACHED: {}".format(arch.name()))
424d48c1bcSIlya Leoshkevich        if expected_arch is not None:
434d48c1bcSIlya Leoshkevich            report(arch.name() == expected_arch,
444d48c1bcSIlya Leoshkevich                   "connected to {}".format(expected_arch))
454d48c1bcSIlya Leoshkevich    except (gdb.error, AttributeError):
464d48c1bcSIlya Leoshkevich        print("SKIP: not connected")
47*cb241df4SIlya Leoshkevich        gdb_exit(0)
484d48c1bcSIlya Leoshkevich
494d48c1bcSIlya Leoshkevich    if gdb.parse_and_eval("$pc") == 0:
504d48c1bcSIlya Leoshkevich        print("SKIP: PC not set")
51*cb241df4SIlya Leoshkevich        gdb_exit(0)
524d48c1bcSIlya Leoshkevich
534d48c1bcSIlya Leoshkevich    try:
544d48c1bcSIlya Leoshkevich        test()
554d48c1bcSIlya Leoshkevich    except:
564d48c1bcSIlya Leoshkevich        print("GDB Exception:")
574d48c1bcSIlya Leoshkevich        traceback.print_exc(file=sys.stdout)
584d48c1bcSIlya Leoshkevich        global fail_count
594d48c1bcSIlya Leoshkevich        fail_count += 1
604d48c1bcSIlya Leoshkevich        if "QEMU_TEST_INTERACTIVE" in os.environ:
614d48c1bcSIlya Leoshkevich            import code
624d48c1bcSIlya Leoshkevich            code.InteractiveConsole(locals=globals()).interact()
634d48c1bcSIlya Leoshkevich        raise
644d48c1bcSIlya Leoshkevich
654d48c1bcSIlya Leoshkevich    try:
664d48c1bcSIlya Leoshkevich        gdb.execute("kill")
674d48c1bcSIlya Leoshkevich    except gdb.error:
684d48c1bcSIlya Leoshkevich        pass
694d48c1bcSIlya Leoshkevich
704d48c1bcSIlya Leoshkevich    print("All tests complete: {} failures".format(fail_count))
71*cb241df4SIlya Leoshkevich    gdb_exit(fail_count)
72