public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH] gdbserver: fix handling of single quote arguments
Date: Wed, 27 Sep 2023 13:21:14 +0100	[thread overview]
Message-ID: <2b98ca58e47638b4760d86bd6e1fa9a9a79fa2ad.1695817255.git.aburgess@redhat.com> (raw)

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.
---
 gdb/testsuite/gdb.base/inferior-args.exp | 7 +++++--
 gdbserver/server.cc                      | 6 ------
 2 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/gdb/testsuite/gdb.base/inferior-args.exp b/gdb/testsuite/gdb.base/inferior-args.exp
index 19bada6d2c7..3d3cd39a706 100644
--- a/gdb/testsuite/gdb.base/inferior-args.exp
+++ b/gdb/testsuite/gdb.base/inferior-args.exp
@@ -29,7 +29,7 @@ proc do_test { method } {
     global binfile hex
 
     # The second arg is an empty string on purpose.
-    set inferior_args { "first arg" "" "third-arg" }
+    set inferior_args { "first arg" "" "third-arg" "'" "\"" " " }
 
     clean_restart $binfile
 
@@ -109,11 +109,14 @@ proc do_test { method } {
     }
 
     # Now that we are stopped at main, inspect argc/argv.
-    gdb_test "print argc" " = 4"
+    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" } {
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index c57270175b4..496b9bebb7d 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -3011,12 +3011,6 @@ handle_v_run (char *own_buf)
 		  need_quote = 1;
 		  break;
 
-		case '\'':
-		  /* Quote single quote.  */
-		  *tmp_full_arg = '\\';
-		  ++tmp_full_arg;
-		  break;
-
 		default:
 		  break;
 		}

base-commit: f586e3409b752748bf213520c2dbb0b44e0005d8
-- 
2.25.4


             reply	other threads:[~2023-09-27 12:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-27 12:21 Andrew Burgess [this message]
2023-09-27 13:01 ` Andreas Schwab
2023-09-27 17:27 ` [PATCHv2 0/5] Fixes for passing arguments to gdbserver Andrew Burgess
2023-09-27 17:27   ` [PATCHv2 1/5] gdbserver: fix handling of single quote arguments Andrew Burgess
2023-09-27 17:27   ` [PATCHv2 2/5] gdbserver: fix handling of trailing empty argument Andrew Burgess
2023-09-27 17:27   ` [PATCHv2 3/5] gdbserver: handle newlines in inferior arguments Andrew Burgess
2023-09-27 17:27   ` [PATCHv2 4/5] gdbserver: cleanup in handle_v_run Andrew Burgess
2023-10-03 19:13     ` Tom Tromey
2023-10-04 14:40       ` Andrew Burgess
2023-10-04 19:35         ` Tom Tromey
2023-09-27 17:27   ` [PATCHv2 5/5] gdb/testsuite: cleanup in gdb.base/args.exp Andrew Burgess
2023-10-05 16:17     ` Tom Tromey
2023-10-05 16:18   ` [PATCHv2 0/5] Fixes for passing arguments to gdbserver Tom Tromey
2023-10-06 12:15     ` Andrew Burgess
2023-10-06 12:56       ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2b98ca58e47638b4760d86bd6e1fa9a9a79fa2ad.1695817255.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).