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")
30 parser
.add_argument("--gdb", help="The gdb binary to use",
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()
41 output
.write(msg
+ "\n")
47 if __name__
== '__main__':
50 # Search for a gdb we can use
52 args
.gdb
= shutil
.which("gdb-multiarch")
54 args
.gdb
= shutil
.which("gdb")
56 print("We need gdb to run the test")
59 output
= open(args
.output
, "w")
63 stderr
= open(args
.stderr
, "w")
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'
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
)
83 gdb_cmd
+= " %s" % (args
.gdb_args
)
84 # run quietly and ignore .gdbinit
85 gdb_cmd
+= " -q -n -batch"
87 gdb_cmd
+= " -ex 'set pagination off'"
88 # disable prompts in case of crash
89 gdb_cmd
+= " -ex 'set confirm off'"
91 gdb_cmd
+= " -ex 'target remote %s'" % (socket_name
)
92 # finally the test script itself
94 gdb_cmd
+= " -x %s" % (args
.test
)
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.
107 log(output
, "GDB crashed? (%d, %d) SKIPPING" % (result
, result
- 128))
112 except subprocess
.TimeoutExpired
:
113 log(output
, "GDB never connected? Killed guest")