binutils-gdb/gdb/testsuite/gdb.base/inferior-args.exp
Andrew Burgess f1f0a06d5b gdbserver: fix handling of single quote arguments
I noticed that passing arguments containing single quotes to gdbserver
didn't work correctly:

  gdb -ex 'set sysroot' --args /tmp/show-args
  Reading symbols from /tmp/show-args...
  (gdb) target extended-remote | gdbserver --once --multi - /tmp/show-args
  Remote debugging using | gdbserver --once --multi - /tmp/show-args
  stdin/stdout redirected
  Process /tmp/show-args created; pid = 176054
  Remote debugging using stdio
  Reading symbols from /lib64/ld-linux-x86-64.so.2...
  (No debugging symbols found in /lib64/ld-linux-x86-64.so.2)
  0x00007ffff7fd3110 in _start () from /lib64/ld-linux-x86-64.so.2
  (gdb) set args \'
  (gdb) r
  The program being debugged has been started already.
  Start it from the beginning? (y or n) y
  Starting program: /tmp/show-args \'
  stdin/stdout redirected
  Process /tmp/show-args created; pid = 176088
  2 args are:
    /tmp/show-args
    \'
  Done.
  [Inferior 1 (process 176088) exited normally]
  (gdb) target native
  Done.  Use the "run" command to start a process.
  (gdb) run
  Starting program: /tmp/show-args \'
  2 args are:
    /tmp/show-args
    '
  Done.
  [Inferior 1 (process 176095) exited normally]
  (gdb) q

The 'shows-args' program used here just prints the arguments passed to
the inferior.

Notice that when starting the inferior using the extended-remote
target the second argument is "\'", while when running using native
target the argument is "'".  The second of these is correct, the \'
used with the "set args" command is just to show GDB that the single
quote is not opening an argument string.

It turns out that the extra backslash is injected on the gdbserver
side when gdbserver processes the arguments that GDB passes it, the
code that does this was added as part of this much larger commit:

  commit 2090129c36c7e582943b7d300968d19b46160d84
  Date:   Thu Dec 22 21:11:11 2016 -0500

      Share fork_inferior et al with gdbserver

In this commit I propose removing the specific code that adds what I
believe is a stray backslash.  I've extended an existing test to cover
this case, and I now see identical behaviour when using an
extended-remote target as with the native target.

This partially fixes PR gdb/27989, though there are still some issues
with newline handling which I'll address in a later commit.

During review I was pointed to this older series:

  https://inbox.sourceware.org/gdb-patches/20211022071933.3478427-1-m.weghorn@posteo.de/

which also includes this fix as part of a larger set of changes.  I'm
giving a Co-Authored-By credit to the author of that original series.
I believe this smaller fix brings some benefits on its own, though the
original series does offer additional improvements.  Once this is
merged I'll take a look at rebasing and resubmitting the original series.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27989

Co-Authored-By: Michael Weghorn <m.weghorn@posteo.de>
Approved-By: Tom Tromey <tom@tromey.com>
2023-10-06 13:02:36 +01:00

125 lines
3.4 KiB
Plaintext

# Copyright 2020-2023 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 running an inferior with arguments.
# This does not work on boards that don't support inferior arguments.
require {!target_info exists noargs}
standard_testfile .c
if {[build_executable "failed to prepare" $testfile $srcfile \
{debug additional_flags=-std=c99}] == -1} {
return
}
proc do_test { method } {
global binfile hex
# The second arg is an empty string on purpose.
set inferior_args { "first arg" "" "third-arg" "'" "\"" " " }
clean_restart $binfile
if { $method == "start" } {
# The start command does not make sense for a stub.
if { [use_gdb_stub] } {
return;
}
if { [gdb_start_cmd $inferior_args] < 0 } {
fail "could not issue start command"
return -1
}
# Consume up to the GDB prompt after the stop.
gdb_test "" ".*main.*" "stop at main"
} elseif { $method == "starti" } {
# The starti command does not make sense for a stub.
if { [use_gdb_stub] } {
return;
}
if { [gdb_starti_cmd $inferior_args] < 0 } {
fail "could not issue start command"
return -1
}
# Consume up to the GDB prompt after the stop.
gdb_test "" "" "stop at first instruction"
# Put a breakpoint and continue until main.
if { ![gdb_breakpoint "main" message] } {
fail "could not set breakpoint on main"
return -1
}
if { [gdb_continue "main"] != 0 } {
fail "could not continue to main"
return -1
}
} elseif { $method == "run" } {
if { ![gdb_breakpoint "main" message] } {
fail "could not set breakpoint on main"
return -1
}
# The run command does not make sense for a stub, but GDB_RUN_CMD
# does the right thing when the target is a stub (start the stub,
# connect to it, and "continue").
#
# This allows us to test arguments passed on the gdbserver command
# line.
if { [gdb_run_cmd $inferior_args] < 0 } {
fail "could not run"
return -1
}
# Consume up to the GDB prompt after the stop.
gdb_test "" ".*main.*" "stop at main"
} elseif { $method == "set args" } {
# Using "set args" does not make sense with a stub.
if { [use_gdb_stub] } {
return;
}
gdb_test_no_output "set args $inferior_args"
if { ![runto_main] } {
return -1
}
} else {
error "invalid method $method"
}
# Now that we are stopped at main, inspect argc/argv.
gdb_test "print argc" " = 7"
gdb_test "print argv\[0\]" " = $hex \".*\""
gdb_test "print argv\[1\]" " = $hex \"first arg\""
gdb_test "print argv\[2\]" " = $hex \"\""
gdb_test "print argv\[3\]" " = $hex \"third-arg\""
gdb_test "print argv\[4\]" " = $hex \"'\""
gdb_test "print argv\[5\]" " = $hex \"\\\\\"\""
gdb_test "print argv\[6\]" " = $hex \" \""
}
foreach_with_prefix method { "start" "starti" "run" "set args" } {
do_test $method
}