Re: [PATCH v4 8/9] gdb/testsuite: support passing inferior arguments with native-gdbserver board

Tom de Vries
2020-05-15T17:29:18Z

On 13-05-2020 11:47, Michael Weghorn via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
>
> This patch makes it possible to run tests requiring passing arguments to
> the inferior with the native-gdbserver board. The end goal is to write
> a test that verifies passing arguments to the inferior works, and to
> have that test exercise inferior arguments passed on the gdbserver
> command line, when using the native-gdbserver target board (in addition
> to the other boards). This is done in the next patch.
>
> With the native-gdbserver target board, gdbserver is started in
> gdb_reload (implemented in config/gdbserver.exp), called in gdb_run_cmd.
> gdb_run_cmd already supposedly accepts inferior arguments (although that
> feature does not seem to be used anywhere), which it passes to the `run`
> command, for non-stub target boards. I've changed gdb_run_cmd so that
> it forwards these arguments to gdb_reload as well. gdb_reload passes
> them to gdbserver_run, and they eventually make their way to the
> gdbserver command line.
>
> gdb_run_cmd currently accepts `args` (the varargs of tcl), which means
> it receives inferior arguments as a list. This won't work with
> arguments with spaces, because they will end up being formatted with
> curly braces like this:
>
> % set args [list hello "with spaces" world]
> hello {with spaces} world
> % puts "run $args"
> run hello {with spaces} world
>
> I've changed it to accept a single string that is passed to `run` and
> gdb_reload. I've done the same change in gdb_start_cmd and
> gdb_starti_cmd, although these two are not used with native-gdbserver.
>
> I've changed all gdb_reload implementations in the tree to accept a new
> inferior_args argument, although most of them don't do anything with it
> (and don't need to). People maintaining target boards out of tree will
> need to do the same.
>

The curly braces are what you get when you directly convert a list of
strings into a string.

If we do the conversion ourselves, we get the desired outcome:
...
$ cat test.tcl
#!/usr/bin/tclsh

set args [list hello "with spaces" world]

proc string_list_to_string_with_quotes { args } {
set s ""
foreach arg $args {
set s "$s \"$arg\""
}
return $s
}

set args_str [string_list_to_string_with_quotes {*}$args]
puts "run $args_str"
$ ./test.tcl
run "hello" "with spaces" "world"
...

And we can use the normal "args" rather than the inferior_args, as long
as we take care to pass the args from one varargs function to another
using '{*}$args'.

Thanks,
- Tom