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
19 from tempfile
import TemporaryDirectory
22 parser
= argparse
.ArgumentParser(description
="A gdbstub test runner")
23 parser
.add_argument("--qemu", help="Qemu binary for test",
25 parser
.add_argument("--qargs", help="Qemu arguments for test")
26 parser
.add_argument("--binary", help="Binary to debug",
28 parser
.add_argument("--test", help="GDB test script",
30 parser
.add_argument("--gdb", help="The gdb binary to use", default
=None)
32 return parser
.parse_args()
34 if __name__
== '__main__':
37 # Search for a gdb we can use
39 args
.gdb
= shutil
.which("gdb-multiarch")
41 args
.gdb
= shutil
.which("gdb")
43 print("We need gdb to run the test")
46 socket_dir
= TemporaryDirectory("qemu-gdbstub")
47 socket_name
= os
.path
.join(socket_dir
.name
, "gdbstub.socket")
49 # Launch QEMU with binary
50 if "system" in args
.qemu
:
51 cmd
= "%s %s %s -s -S" % (args
.qemu
, args
.qargs
, args
.binary
)
53 cmd
= "%s %s -g %s %s" % (args
.qemu
, args
.qargs
, socket_name
,
56 inferior
= subprocess
.Popen(shlex
.split(cmd
))
58 # Now launch gdb with our test and collect the result
59 gdb_cmd
= "%s %s" % (args
.gdb
, args
.binary
)
60 # run quietly and ignore .gdbinit
61 gdb_cmd
+= " -q -n -batch"
62 # disable prompts in case of crash
63 gdb_cmd
+= " -ex 'set confirm off'"
65 if "system" in args
.qemu
:
66 gdb_cmd
+= " -ex 'target remote localhost:1234'"
68 gdb_cmd
+= " -ex 'target remote %s'" % (socket_name
)
69 # finally the test script itself
70 gdb_cmd
+= " -x %s" % (args
.test
)
72 print("GDB CMD: %s" % (gdb_cmd
))
74 result
= subprocess
.call(gdb_cmd
, shell
=True);
76 # A negative result is the result of an internal gdb failure like
77 # a crash. We force a return of 0 so we don't fail the test on
78 # account of broken external tools.
80 print("GDB crashed? SKIPPING")
85 except subprocess
.TimeoutExpired
:
86 print("GDB never connected? Killed guest")