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 time
import sleep
20 from tempfile
import TemporaryDirectory
23 parser
= argparse
.ArgumentParser(description
="A gdbstub test runner")
24 parser
.add_argument("--qemu", help="Qemu binary for test",
26 parser
.add_argument("--qargs", help="Qemu arguments for test")
27 parser
.add_argument("--binary", help="Binary to debug",
29 parser
.add_argument("--test", help="GDB test script",
31 parser
.add_argument("--gdb", help="The gdb binary to use",
33 parser
.add_argument("--output", help="A file to redirect output to")
35 return parser
.parse_args()
40 output
.write(msg
+ "\n")
46 if __name__
== '__main__':
49 # Search for a gdb we can use
51 args
.gdb
= shutil
.which("gdb-multiarch")
53 args
.gdb
= shutil
.which("gdb")
55 print("We need gdb to run the test")
58 output
= open(args
.output
, "w")
62 socket_dir
= TemporaryDirectory("qemu-gdbstub")
63 socket_name
= os
.path
.join(socket_dir
.name
, "gdbstub.socket")
65 # Launch QEMU with binary
66 if "system" in args
.qemu
:
67 cmd
= "%s %s %s -gdb unix:path=%s,server=on" % (args
.qemu
,
72 cmd
= "%s %s -g %s %s" % (args
.qemu
, args
.qargs
, socket_name
,
75 log(output
, "QEMU CMD: %s" % (cmd
))
76 inferior
= subprocess
.Popen(shlex
.split(cmd
))
78 # Now launch gdb with our test and collect the result
79 gdb_cmd
= "%s %s" % (args
.gdb
, args
.binary
)
80 # run quietly and ignore .gdbinit
81 gdb_cmd
+= " -q -n -batch"
82 # disable prompts in case of crash
83 gdb_cmd
+= " -ex 'set confirm off'"
85 gdb_cmd
+= " -ex 'target remote %s'" % (socket_name
)
86 # finally the test script itself
87 gdb_cmd
+= " -x %s" % (args
.test
)
91 log(output
, "GDB CMD: %s" % (gdb_cmd
))
93 result
= subprocess
.call(gdb_cmd
, shell
=True, stdout
=output
)
95 # A negative result is the result of an internal gdb failure like
96 # a crash. We force a return of 0 so we don't fail the test on
97 # account of broken external tools.
99 print("GDB crashed? SKIPPING")
104 except subprocess
.TimeoutExpired
:
105 print("GDB never connected? Killed guest")