spapr: Clean up local variable shadowing in spapr_dt_cpus()
[qemu/kevin.git] / tests / guest-debug / run-test.py
blobb13b27d4b193139934f99ab162930be9af7896ce
1 #!/usr/bin/env python3
3 # Run a gdbstub test case
5 # Copyright (c) 2019 Linaro
7 # Author: Alex Bennée <alex.bennee@linaro.org>
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.
12 # SPDX-License-Identifier: GPL-2.0-or-later
14 import argparse
15 import subprocess
16 import shutil
17 import shlex
18 import os
19 from time import sleep
20 from tempfile import TemporaryDirectory
22 def 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("--gdb", help="The gdb binary to use",
31 default=None)
32 parser.add_argument("--gdb-args", help="Additional gdb arguments")
33 parser.add_argument("--output", help="A file to redirect output to")
34 parser.add_argument("--stderr", help="A file to redirect stderr to")
36 return parser.parse_args()
39 def log(output, msg):
40 if output:
41 output.write(msg + "\n")
42 output.flush()
43 else:
44 print(msg)
47 if __name__ == '__main__':
48 args = get_args()
50 # Search for a gdb we can use
51 if not args.gdb:
52 args.gdb = shutil.which("gdb-multiarch")
53 if not args.gdb:
54 args.gdb = shutil.which("gdb")
55 if not args.gdb:
56 print("We need gdb to run the test")
57 exit(-1)
58 if args.output:
59 output = open(args.output, "w")
60 else:
61 output = None
62 if args.stderr:
63 stderr = open(args.stderr, "w")
64 else:
65 stderr = None
67 socket_dir = TemporaryDirectory("qemu-gdbstub")
68 socket_name = os.path.join(socket_dir.name, "gdbstub.socket")
70 # Launch QEMU with binary
71 if "system" in args.qemu:
72 cmd = f'{args.qemu} {args.qargs} {args.binary}' \
73 f' -S -gdb unix:path={socket_name},server=on'
74 else:
75 cmd = f'{args.qemu} {args.qargs} -g {socket_name} {args.binary}'
77 log(output, "QEMU CMD: %s" % (cmd))
78 inferior = subprocess.Popen(shlex.split(cmd))
80 # Now launch gdb with our test and collect the result
81 gdb_cmd = "%s %s" % (args.gdb, args.binary)
82 if args.gdb_args:
83 gdb_cmd += " %s" % (args.gdb_args)
84 # run quietly and ignore .gdbinit
85 gdb_cmd += " -q -n -batch"
86 # disable pagination
87 gdb_cmd += " -ex 'set pagination off'"
88 # disable prompts in case of crash
89 gdb_cmd += " -ex 'set confirm off'"
90 # connect to remote
91 gdb_cmd += " -ex 'target remote %s'" % (socket_name)
92 # finally the test script itself
93 if args.test:
94 gdb_cmd += " -x %s" % (args.test)
97 sleep(1)
98 log(output, "GDB CMD: %s" % (gdb_cmd))
100 result = subprocess.call(gdb_cmd, shell=True, stdout=output, stderr=stderr)
102 # A result of greater than 128 indicates a fatal signal (likely a
103 # crash due to gdb internal failure). That's a problem for GDB and
104 # not the test so we force a return of 0 so we don't fail the test on
105 # account of broken external tools.
106 if result > 128:
107 log(output, "GDB crashed? (%d, %d) SKIPPING" % (result, result - 128))
108 exit(0)
110 try:
111 inferior.wait(2)
112 except subprocess.TimeoutExpired:
113 log(output, "GDB never connected? Killed guest")
114 inferior.kill()
116 exit(result)