Re: [PATCH 4/4] [PR gdbserver/25893]: Add gdbserver test for argument with space in it

Simon Marchi
2020-05-07T22:15:13Z

On 2020-04-29 7:16 a.m., Michael Weghorn via Gdb-patches wrote:
> This adds a test to call gdbserver with a program
> that is passed an argument containing a space and
> checks that argv[1] actually contains the value
> of this arg.
>
> gdb/testsuite/ChangeLog:
>
> 2020-04-29 Michael Weghorn <m.weghorn@posteo.de>
>
> PR gdbserver/25893
> * gdb.server/arg-with-space.exp: New test that
> covers running program with arg containing
> a space with gdbserver
> ---
> gdb/testsuite/gdb.server/arg-with-space.exp | 41 +++++++++++++++++++++
> 1 file changed, 41 insertions(+)
> create mode 100644 gdb/testsuite/gdb.server/arg-with-space.exp
>
> diff --git a/gdb/testsuite/gdb.server/arg-with-space.exp b/gdb/testsuite/gdb.server/arg-with-space.exp
> new file mode 100644
> index 0000000000..d768b7ca0f
> --- /dev/null
> +++ b/gdb/testsuite/gdb.server/arg-with-space.exp
> @@ -0,0 +1,41 @@
> +# This testcase is part of GDB, the GNU debugger.
> +#
> +# Copyright 2020 Free Software Foundation, Inc.
> +#
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program. If not, see <http://www.gnu.org/licenses/>.
> +
> +# Test gdbserver to properly handle a program arg with a space in it
> +# as a single argument.
> +
> +load_lib gdbserver-support.exp
> +
> +standard_testfile "normal.c"
> +
> +if {[skip_gdbserver_tests]} {
> + return 0
> +}
> +
> +if {[gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
> + return -1
> +}
> +clean_restart ${testfile}
> +
> +gdbserver_run "\"hello world\""
> +
> +gdb_breakpoint main
> +gdb_continue main
> +
> +# check that program arg was passed properly
> +gdb_test "print argc" "2"
> +gdb_test "print argv\[1\]" "\"hello world\""
> --
> 2.26.2
>

It would be nice to write a test in such a way that it can cover this use case
when using GDB (without GDBserver) too.

It's possible to run the same test using plain GDB with:

make check TESTS="gdb.base/foo.exp"

or

make check TESTS="gdb.base/foo.exp" RUNTESTFLAGS="--target_board=unix"

unix is the default DejaGNU (runtest) board, so these two are equivalent.

It's also possible to run a test using gdbserver as a target:

make check TESTS="gdb.base/foo.exp" RUNTESTFLAGS="--target_board=native-gdbserver"
make check TESTS="gdb.base/foo.exp" RUNTESTFLAGS="--target_board=native-extended-gdbserver"

So it should be possible to write a test that runs a process with arguments
containing spaces. However, it's not currently possible with the testsuite
to pass arguments to a test program using the native-gdbserver board. These
arguments would have to be forwarded all the way to gdbserver's command line,
exactly like when you start gdbserver by hand. This will require a bit of
testsuite refactoring, I'll try to look into it.

With native-extended-gdbserver, the args could be passed to the `run` command,
just like the native debugging case. It's by playing with this that I noticed
gdbserver crashing with this patchset applied when trying to remotely run a
process with arguments.

Simon