public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: gdb-patches@sourceware.org
Subject: [PATCH] Fix program invocation by gdbserver on MS-Windows
Date: Tue, 24 Dec 2019 17:39:00 -0000	[thread overview]
Message-ID: <83y2v1vd0i.fsf@gnu.org> (raw)

gdbserver doesn't construct the command-line correctly when it invokes
programs on MS-Windows.  For example, if you invoke

      gdbserver :9999 etags.exe --help

and then connect from GDB and run the inferior, you will see:

  Remote debugging from host 127.0.0.1, port 3546
  --help: no input files specified.
	  Try '--help --help' for a complete list of options.

  Child exited with status 1

Which means that (a) the program exited abnormally; and (b) it
received "--help" in argv[0].

This is because the way we construct the command line in win32-low.c
is incorrect: we should prepend the program's name to the arguments.

The patch below does that.  OK to commit it?

diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog
index 028d1e9..29de8b6 100644
--- a/gdb/gdbserver/ChangeLog
+++ b/gdb/gdbserver/ChangeLog
@@ -1,3 +1,10 @@
+2019-12-24  Eli Zaretskii  <eliz@gnu.org>
+
+	* win32-low.c (create_process): Prepend PROGRAM to ARGS when
+	preparing the command line for CreateProcess.
+	(win32_create_inferior): Reflect the program name in debugging
+	output that shows the process and its command line.
+
 2019-12-19  Christian Biesinger  <cbiesinger@google.com>
 
 	* configure: Regenerate.
diff --git a/gdb/gdbserver/win32-low.c b/gdb/gdbserver/win32-low.c
index 449ed5f..9aaed84 100644
--- a/gdb/gdbserver/win32-low.c
+++ b/gdb/gdbserver/win32-low.c
@@ -557,21 +557,25 @@ create_process (const char *program, char *args,
 {
   const char *inferior_cwd = get_inferior_cwd ();
   BOOL ret;
+  size_t argslen, proglen;
+
+  proglen = strlen (program) + 1;
+  argslen = strlen (args) + proglen;
 
 #ifdef _WIN32_WCE
   wchar_t *p, *wprogram, *wargs, *wcwd = NULL;
-  size_t argslen;
 
-  wprogram = alloca ((strlen (program) + 1) * sizeof (wchar_t));
-  mbstowcs (wprogram, program, strlen (program) + 1);
+  wprogram = (wchar_t *) alloca (proglen * sizeof (wchar_t));
+  mbstowcs (wprogram, program, proglen);
 
   for (p = wprogram; *p; ++p)
     if (L'/' == *p)
       *p = L'\\';
 
-  argslen = strlen (args);
   wargs = alloca ((argslen + 1) * sizeof (wchar_t));
-  mbstowcs (wargs, args, argslen + 1);
+  wcscpy (wargs, wprogram);
+  wcscat (wargs, L" ");
+  mbstowcs (wargs + proglen, args, argslen + 1 - proglen);
 
   if (inferior_cwd != NULL)
     {
@@ -599,20 +603,24 @@ Could not convert the expanded inferior cwd to wide-char."));
 			pi);      /* proc info */
 #else
   STARTUPINFOA si = { sizeof (STARTUPINFOA) };
-
-  ret = CreateProcessA (program,  /* image name */
-			args,     /* command line */
-			NULL,     /* security */
-			NULL,     /* thread */
-			TRUE,     /* inherit handles */
-			flags,    /* start flags */
-			NULL,     /* environment */
+  char *program_and_args = (char *) alloca (argslen + 1);
+
+  strcpy (program_and_args, program);
+  strcat (program_and_args, " ");
+  strcat (program_and_args, args);
+  ret = CreateProcessA (program,           /* image name */
+			program_and_args,  /* command line */
+			NULL,              /* security */
+			NULL,              /* thread */
+			TRUE,              /* inherit handles */
+			flags,             /* start flags */
+			NULL,              /* environment */
 			/* current directory */
 			(inferior_cwd == NULL
 			 ? NULL
 			 : gdb_tilde_expand (inferior_cwd).c_str()),
-			&si,      /* start info */
-			pi);      /* proc info */
+			&si,               /* start info */
+			pi);               /* proc info */
 #endif
 
   return ret;
@@ -663,7 +671,7 @@ win32_create_inferior (const char *program,
   program = real_path;
 #endif
 
-  OUTMSG2 (("Command line is \"%s\"\n", args));
+  OUTMSG2 (("Command line is \"%s %s\"\n", program, args));
 
 #ifdef CREATE_NEW_PROCESS_GROUP
   flags |= CREATE_NEW_PROCESS_GROUP;
@@ -686,12 +694,12 @@ win32_create_inferior (const char *program,
 
   if (!ret)
     {
-      error ("Error creating process \"%s%s\", (error %d): %s\n",
+      error ("Error creating process \"%s %s\", (error %d): %s\n",
 	     program, args, (int) err, strwinerror (err));
     }
   else
     {
-      OUTMSG2 (("Process created: %s\n", (char *) args));
+      OUTMSG2 (("Process created: %s %s\n", program, (char *) args));
     }
 
 #ifndef _WIN32_WCE

             reply	other threads:[~2019-12-24 17:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-24 17:39 Eli Zaretskii [this message]
2020-02-13 13:34 ` Kevin Buettner
2020-02-14  9:56   ` Eli Zaretskii

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=83y2v1vd0i.fsf@gnu.org \
    --to=eliz@gnu.org \
    --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).