public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Michael Weghorn <m.weghorn@posteo.de>
To: gdb-patches@sourceware.org
Cc: Michael Weghorn <m.weghorn@posteo.de>
Subject: [PATCH 4/8] PR28392 Add a '--no-escape-args' option for gdb and gdbserver
Date: Fri, 22 Oct 2021 07:19:29 +0000	[thread overview]
Message-ID: <20211022071933.3478427-5-m.weghorn@posteo.de> (raw)
In-Reply-To: <20211022071933.3478427-1-m.weghorn@posteo.de>

This introduces a new '--no-escape-args' option
for gdb and gdbserver.

When the startup-with-shell option is enabled, args
passed directly on 'gdb --args <args>' or
'gdbserver <args>' invocation are by default escaped
so that they are passed to the inferior as passed on
invocation, no globbing or variable substitution happens
when the args are passed to the shell that is
started by the GDB process later.
(For gdbserver, this is the case since
commit bea571ebd78ee29cb94adf648fbcda1e109e1be6,
"Use construct_inferior_arguments which handles special chars".)

Only args set e.g. via 'set args <args>' or
'run <args>' are not escaped.

For the 'gdb --args' case, directly settings unescaped
args on gdb invocation was possible e.g. by using the
"--eval-command='set args <args>'", while this possibility
did not exist for gdbserver.

The new '--no-escape-args' option disables this escaping of
args passed directly on invocation.

For gdbserver, using this new option allows having the behaviour
from before commit bea571ebd78ee29cb94adf648fbcda1e109e1be6,
while keeping the behaviour unified between GDB and GDBserver.

(For more reasoning, s. the discussion in PR28392,
"gdb server no longer supports argument globbing and variable
substitution".)
---
 gdb/infcmd.c        |  4 ++--
 gdb/inferior.h      |  2 +-
 gdb/main.c          |  6 +++++-
 gdbserver/server.cc | 28 ++++++++++++++++++++++++----
 4 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index b55a56c020d..4283f3ba201 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -123,10 +123,10 @@ show_inferior_tty_command (struct ui_file *file, int from_tty,
 }
 
 void
-set_inferior_args_vector (int argc, char **argv)
+set_inferior_args_vector (int argc, char **argv, bool escape_args)
 {
   gdb::array_view<char * const> args (argv, argc);
-  std::string n = construct_inferior_arguments (args);
+  std::string n = construct_inferior_arguments (args, escape_args);
   current_inferior ()->set_args (std::move (n));
 }
 
diff --git a/gdb/inferior.h b/gdb/inferior.h
index e9210b1258d..0896bb1f459 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -204,7 +204,7 @@ extern void post_create_inferior (int from_tty);
 
 extern void attach_command (const char *, int);
 
-extern void set_inferior_args_vector (int, char **);
+extern void set_inferior_args_vector (int, char **, bool escape_args);
 
 extern void registers_info (const char *, int);
 
diff --git a/gdb/main.c b/gdb/main.c
index ca4ccc375dc..2b979d010c1 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -628,6 +628,7 @@ captured_main_1 (struct captured_main_args *context)
 
   static int quiet = 0;
   static int set_args = 0;
+  static int escape_args = 1;
   static int inhibit_home_gdbinit = 0;
 
   /* Pointers to various arguments from command line.  */
@@ -833,6 +834,7 @@ captured_main_1 (struct captured_main_args *context)
       {"statistics", no_argument, 0, OPT_STATISTICS},
       {"write", no_argument, &write_files_1, 1},
       {"args", no_argument, &set_args, 1},
+      {"no-escape-args", no_argument, &escape_args, 0},
       {"l", required_argument, 0, 'l'},
       {"return-child-result", no_argument, &return_child_result, 1},
       {0, no_argument, 0, 0}
@@ -1070,7 +1072,7 @@ captured_main_1 (struct captured_main_args *context)
       symarg = argv[optind];
       execarg = argv[optind];
       ++optind;
-      set_inferior_args_vector (argc - optind, &argv[optind]);
+      set_inferior_args_vector (argc - optind, &argv[optind], escape_args);
     }
   else
     {
@@ -1399,6 +1401,8 @@ This is the GNU debugger.  Usage:\n\n\
   fputs_unfiltered (_("\
 Selection of debuggee and its files:\n\n\
   --args             Arguments after executable-file are passed to inferior.\n\
+  --no-escape-args   Don't escape args passed via '--args' when invoking the\n\
+                     inferior in a shell.\n\
   --core=COREFILE    Analyze the core dump COREFILE.\n\
   --exec=EXECFILE    Use EXECFILE as the executable.\n\
   --pid=PID          Attach to running process PID.\n\
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index 3926be19fd1..14bd1af0404 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -3456,10 +3456,20 @@ gdbserver_usage (FILE *stream)
 	   "  --startup-with-shell\n"
 	   "                        Start PROG using a shell.  I.e., execs a shell that\n"
 	   "                        then execs PROG.  (default)\n"
+	   "                        To make use of globbing and variable subsitution for\n"
+	   "                        arguments passed directly on gdbserver invocation,\n"
+	   "                        see the --no-escape-args command line option in\n"
+	   "                        addition\n"
 	   "  --no-startup-with-shell\n"
 	   "                        Exec PROG directly instead of using a shell.\n"
-	   "                        Disables argument globbing and variable substitution\n"
-	   "                        on UNIX-like systems.\n"
+	   "  --no-escape-args\n"
+	   "                        If PROG is started using a shell (see the\n"
+	   "                        --[no-]startup-with-shell option),\n"
+	   "                        ARGS passed directly on gdbserver invocation are\n"
+	   "                        escaped, so no globbing or variable substitution\n"
+	   "                        happens for those. This option disables escaping, so\n"
+	   "                        globbing and variable substituation in the shell\n"
+	   "                        are done for ARGS on UNIX-like systems.\n"
 	   "\n"
 	   "Debug options:\n"
 	   "\n"
@@ -3689,6 +3699,7 @@ captured_main (int argc, char *argv[])
   volatile int attach = 0;
   int was_running;
   bool selftest = false;
+  bool escape_args = true;
 #if GDB_SELF_TEST
   std::vector<const char *> selftest_filters;
 
@@ -3825,6 +3836,8 @@ captured_main (int argc, char *argv[])
 	startup_with_shell = true;
       else if (strcmp (*next_arg, "--no-startup-with-shell") == 0)
 	startup_with_shell = false;
+      else if (strcmp (*next_arg, "--no-escape-args") == 0)
+        escape_args = false;
       else if (strcmp (*next_arg, "--once") == 0)
 	run_once = true;
       else if (strcmp (*next_arg, "--selftest") == 0)
@@ -3933,8 +3946,15 @@ captured_main (int argc, char *argv[])
       program_path.set (make_unique_xstrdup (next_arg[0]));
       for (i = 1; i < n; i++)
         {
-          std::string escaped_arg = escape_arg_as_necessary (next_arg[i]);
-          program_args.push_back (xstrdup (escaped_arg.c_str ()));
+          if (escape_args)
+            {
+              std::string escaped_arg = escape_arg_as_necessary (next_arg[i]);
+              program_args.push_back (xstrdup (escaped_arg.c_str ()));
+            }
+          else
+            {
+              program_args.push_back (xstrdup (next_arg[i]));
+            }
         }
 
       /* Wait till we are at first instruction in program.  */
-- 
2.33.0


  parent reply	other threads:[~2021-10-22  7:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-22  7:19 [PATCH 0/8] Unify escaping/quoting of args in GDB/GDBserver Michael Weghorn
2021-10-22  7:19 ` [PATCH 1/8] gdbsupport: Extract escaping from 'construct_inferior_arguments' Michael Weghorn
2021-10-22  7:19 ` [PATCH 2/8] gdbsupport: Make escaping in construct_inferior_arguments optional Michael Weghorn
2021-10-22  7:19 ` [PATCH 3/8] PR27989 PR28446 gdbserver: Only escape args passed directly on invocation Michael Weghorn
2021-10-22  7:19 ` Michael Weghorn [this message]
2021-10-22  7:19 ` [PATCH 5/8] Extract helper function from 'fork_inferior' Michael Weghorn
2021-10-22  7:19 ` [PATCH 6/8] Add a 'fork_inferior' overload taking vector of args Michael Weghorn
2021-10-22  7:19 ` [PATCH 7/8] gdbserver: Support args with spaces for no-startup-with-shell case Michael Weghorn
2021-10-22  7:19 ` [PATCH 8/8] gdb: Support some escaping of args with startup-with-shell being off Michael Weghorn
2021-12-06 10:47 ` [PING] [PATCH 0/8] Unify escaping/quoting of args in GDB/GDBserver Michael Weghorn
2021-12-13  7:49   ` [PING 2] " Michael Weghorn
2022-01-04  4:22     ` [PING 3] " Michael Weghorn
2022-01-04 14:26       ` Simon Marchi

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=20211022071933.3478427-5-m.weghorn@posteo.de \
    --to=m.weghorn@posteo.de \
    --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).