xref: /openbmc/qemu/tests/guest-debug/run-test.py (revision 7d87775f)
1#!/usr/bin/env python3
2#
3# Run a gdbstub test case
4#
5# Copyright (c) 2019 Linaro
6#
7# Author: Alex Bennée <alex.bennee@linaro.org>
8#
9# This work is licensed under the terms of the GNU GPL, version 2 or later.
10# See the COPYING file in the top-level directory.
11#
12# SPDX-License-Identifier: GPL-2.0-or-later
13
14import argparse
15import subprocess
16import shutil
17import shlex
18import os
19from time import sleep
20from tempfile import TemporaryDirectory
21
22def get_args():
23    parser = argparse.ArgumentParser(description="A gdbstub test runner")
24    parser.add_argument("--qemu", help="Qemu binary for test",
25                        required=True)
26    parser.add_argument("--qargs", help="Qemu arguments for test")
27    parser.add_argument("--binary", help="Binary to debug",
28                        required=True)
29    parser.add_argument("--test", help="GDB test script")
30    parser.add_argument('test_args', nargs='*',
31                        help="Additional args for GDB test script. "
32                        "The args should be preceded by -- to avoid confusion "
33                        "with flags for runner script")
34    parser.add_argument("--gdb", help="The gdb binary to use",
35                        default=None)
36    parser.add_argument("--gdb-args", help="Additional gdb arguments")
37    parser.add_argument("--output", help="A file to redirect output to")
38    parser.add_argument("--stderr", help="A file to redirect stderr to")
39
40    return parser.parse_args()
41
42
43def log(output, msg):
44    if output:
45        output.write(msg + "\n")
46        output.flush()
47    else:
48        print(msg)
49
50
51if __name__ == '__main__':
52    args = get_args()
53
54    # Search for a gdb we can use
55    if not args.gdb:
56        args.gdb = shutil.which("gdb-multiarch")
57    if not args.gdb:
58        args.gdb = shutil.which("gdb")
59    if not args.gdb:
60        print("We need gdb to run the test")
61        exit(-1)
62    if args.output:
63        output = open(args.output, "w")
64    else:
65        output = None
66    if args.stderr:
67        stderr = open(args.stderr, "w")
68    else:
69        stderr = None
70
71    socket_dir = TemporaryDirectory("qemu-gdbstub")
72    socket_name = os.path.join(socket_dir.name, "gdbstub.socket")
73
74    # Launch QEMU with binary
75    if "system" in args.qemu:
76        cmd = f'{args.qemu} {args.qargs} {args.binary}' \
77            f' -S -gdb unix:path={socket_name},server=on'
78    else:
79        cmd = f'{args.qemu} {args.qargs} -g {socket_name} {args.binary}'
80
81    log(output, "QEMU CMD: %s" % (cmd))
82    inferior = subprocess.Popen(shlex.split(cmd))
83
84    # Now launch gdb with our test and collect the result
85    gdb_cmd = "%s %s" % (args.gdb, args.binary)
86    if args.gdb_args:
87        gdb_cmd += " %s" % (args.gdb_args)
88    # run quietly and ignore .gdbinit
89    gdb_cmd += " -q -n -batch"
90    # disable pagination
91    gdb_cmd += " -ex 'set pagination off'"
92    # disable prompts in case of crash
93    gdb_cmd += " -ex 'set confirm off'"
94    # connect to remote
95    gdb_cmd += " -ex 'target remote %s'" % (socket_name)
96    # finally the test script itself
97    if args.test:
98        if args.test_args:
99            gdb_cmd += f" -ex \"py sys.argv={args.test_args}\""
100        gdb_cmd += " -x %s" % (args.test)
101
102
103    sleep(1)
104    log(output, "GDB CMD: %s" % (gdb_cmd))
105
106    gdb_env = dict(os.environ)
107    gdb_pythonpath = gdb_env.get("PYTHONPATH", "").split(os.pathsep)
108    gdb_pythonpath.append(os.path.dirname(os.path.realpath(__file__)))
109    gdb_env["PYTHONPATH"] = os.pathsep.join(gdb_pythonpath)
110    result = subprocess.call(gdb_cmd, shell=True, stdout=output, stderr=stderr,
111                             env=gdb_env)
112
113    # A result of greater than 128 indicates a fatal signal (likely a
114    # crash due to gdb internal failure). That's a problem for GDB and
115    # not the test so we force a return of 0 so we don't fail the test on
116    # account of broken external tools.
117    if result > 128:
118        log(output, "GDB crashed? (%d, %d) SKIPPING" % (result, result - 128))
119        exit(0)
120
121    try:
122        inferior.wait(2)
123    except subprocess.TimeoutExpired:
124        log(output, "GDB never connected? Killed guest")
125        inferior.kill()
126
127    exit(result)
128