When I posted the previous patch for review Andreas Schwab pointed out that passing a trailing empty argument also doesn't work. The fix for this is in the same area of code as the previous patch, but is sufficiently different that I felt it deserved a patch of its own. 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 abc "" (gdb) run 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 abc 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 abc 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 there is only a single argument 'abc', while when using the native target there is a second argument, the blank line, representing the empty argument. The problem here is that the vRun packet coming from GDB looks like this (I've removing the trailing checksum): $vRun;PROGRAM_NAME;616263; If we compare this to a packet with only a single argument and no trailing empty argument: $vRun;PROGRAM_NAME;616263 Notice the lack of the trailing ';' character here. The problem is that gdbserver processes this string in a loop. At each point we maintain a pointer to the character just after a ';', and then we process everything up to either the next ';' character, or to the end of the string. We break out of this loop when the character we start with (in that loop iteration) is the null-character. This means in the trailing empty argument case, we abort the loop before doing anything with the empty argument. In this commit I've updated the loop, we now break out using a 'break' statement at the end of the loop if the (sub-)string we just processed was empty, with this change we now notice the trailing empty argument. I've updated the test case to cover this issue. Approved-By: Tom Tromey <tom@tromey.com>
128 lines
3.5 KiB
Plaintext
128 lines
3.5 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. The last argument
|
|
# must be the empty argument -- we once had a bug where that
|
|
# wouldn't work!
|
|
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" " = 8"
|
|
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 \" \""
|
|
gdb_test "print argv\[7\]" " = $hex \"\""
|
|
}
|
|
|
|
foreach_with_prefix method { "start" "starti" "run" "set args" } {
|
|
do_test $method
|
|
}
|