Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
To: <gdb-patches@sourceware.org>
Subject: RE: [RFA] testsuite: Add a test for passing of environment variables to inferior
Date: Tue, 04 Oct 2011 16:11:00 -0000	[thread overview]
Message-ID: <003901cc82b0$471e3a00$d55aae00$@muller@ics-cnrs.unistra.fr> (raw)
In-Reply-To: <20111004151236.GC15757@calimero.vinschen.de>



> -----Message d'origine-----
> De : gdb-patches-owner@sourceware.org [mailto:gdb-patches-
> owner@sourceware.org] De la part de Corinna Vinschen
> Envoyé : mardi 4 octobre 2011 17:13
> À : gdb-patches@sourceware.org
> Objet : Re: [RFA] testsuite: Add a test for passing of environment variables
> to inferior
> 
> On Oct  4 16:49, Pierre Muller wrote:
> > > De : gdb-patches-owner@sourceware.org [mailto:gdb-patches-
> > > owner@sourceware.org] De la part de Corinna Vinschen
> > > Yes, that would be necessary.  I'm wondering if we can't just utilze the
> > > global environ variable for this and spare us all the hassle.  Something
> > > along these lines:
> > >
> > >   char **old_env = environ;
> > >   environ = in_env;
> > >   cygwin_internal (CW_SYNC_WINENV);
> > >   CreateProcessW (NULL environment pointer);
> > >   environ = old_env;
> >
> >   I checked it out, but it still
> > fails for the last test, the missing TEST_GDB_VAR1
> > variable doesn't get removed from GDB environment list
> > apparently (This is probably related to the internals
> > of cygwin_internal (CW_SYNC_WINENV), no?
> 
> Right.  The problem is that there's no simple Win32 call to set (or
> clear) the Windows environment in one go.  So CW_SYNC_WINENV only sets
> but never unsets because that would require to check each Windows
> variable if it exists in the POSIX env or not.  That was more effort
> than seemed to make sense at the time.
> 
> >   Manually resetting all variables should help...
> 
> Yes, but that's far from elegant.  I don't mean your patch, but the fact
> that it seems to be necessary at all.  Maybe we should introduce some
> new cygwin_internal call, which produces a Win32 environment from a
> POSIX environment without affecting the Win32 environment of the calling
> process:
> 
>   LPWCH out_env = (LPWCH) cygwin_internal (CW_CREATE_WIN32_ENV, in_env);
>   CreateProcess (..., CREATE_UNICODE_ENVIRONMENT, ..., out_env, ...);
>   free (new_env);
> 
> But that would only work for newer Cygwin releases, so that's just
> a future option.
> 
> > The patch below does not generate any failure on my test,
> > but I am still not sure what happens if you try to
> > remove an environment variable that was set when GDB started...
> 
> GDB should not be affected.  Its POSIX environment is still intact and
> that's all what's used when interacting with the underlying Cygwin POSIX
> calls.  The only variable which should have an effect is $PATH, and that's
> restored by the final cygwin_internal(CW_SYNC_WINENV) call.  So, AFAICS,
> your patch should be fine.

  Nor completely yet,
here is an update that unsets all environment
variables both in environ list before calling
CreateProcess and in in_env list after...

  This passes the new updated test I just sent.



2011-10-04  Pierre Muller  <muller@ics.u-strasbg.fr>

	* windows-nat.c (windows_create_inferior): Handle in_env
	array for Cygwin GDB.
	* testsuite/gdb.base/setshow.exp (gdb_test): Add test to check
	that 'unset environment' works.


Index: windows-nat.c
===================================================================
RCS file: /cvs/src/src/gdb/windows-nat.c,v
retrieving revision 1.219
diff -u -p -r1.219 windows-nat.c
--- windows-nat.c	28 Sep 2011 09:07:54 -0000	1.219
+++ windows-nat.c	4 Oct 2011 16:05:36 -0000
@@ -1980,8 +1980,9 @@ windows_create_inferior (struct target_o
   cygwin_buf_t *toexec;
   cygwin_buf_t *cygallargs;
   cygwin_buf_t *args;
+  char **old_env;
   size_t len;
-  int tty;
+  int tty, i;
   int ostdin, ostdout, ostderr;
 #else
   char real_path[__PMAX];
@@ -2066,6 +2067,19 @@ windows_create_inferior (struct target_o
   strcat (args, cygallargs);
 #endif
 
+  /* Reset all environment variables to avoid leftover on next run. */
+  for (i = 0; environ[i] && *environ[i]; i++)
+    {
+      char *equalpos;
+      char *copy = alloca (strlen(environ[i]) + 1);
+      strcpy (copy, environ[i]);
+      equalpos = strchr (copy, '=');
+      if (equalpos)
+	*equalpos = '\0';
+      SetEnvironmentVariableA (copy, NULL);
+    }
+  old_env = environ;
+  environ = in_env;
   /* Prepare the environment vars for CreateProcess.  */
   cygwin_internal (CW_SYNC_WINENV);
 
@@ -2101,6 +2115,20 @@ windows_create_inferior (struct target_o
 		       NULL,	/* current directory */
 		       &si,
 		       &pi);
+  /* Reset all environment variables to avoid leftover on next run. */
+  for (i = 0; in_env[i] && *in_env[i]; i++)
+    {
+      char *equalpos = strchr (in_env[i], '=');
+      if (equalpos)
+	*equalpos = '\0';
+      SetEnvironmentVariableA (in_env[i], NULL);
+      if (equalpos)
+	*equalpos = '=';
+    }
+  /* Restore normal GDB environment variables.  */
+  environ = old_env;
+  cygwin_internal (CW_SYNC_WINENV);
+
   if (tty >= 0)
     {
       close (tty);



  parent reply	other threads:[~2011-10-04 16:11 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-04 12:36 Pierre Muller
2011-10-04 13:45 ` Corinna Vinschen
2011-10-04 14:43   ` Eli Zaretskii
2011-10-04 14:48     ` Pierre Muller
2011-10-04 14:50   ` Pierre Muller
2011-10-04 15:13     ` Corinna Vinschen
2011-10-04 16:09       ` [RFA-v2] " Pierre Muller
2011-10-04 16:11       ` Pierre Muller [this message]
2011-10-05 11:40         ` [RFA] " Corinna Vinschen
2011-10-05 12:08           ` Pierre Muller
2011-10-05 12:52             ` Corinna Vinschen
2011-10-06 14:51               ` Christopher Faylor
2011-10-06 15:03                 ` Corinna Vinschen
2011-10-07 12:26                   ` Christopher Faylor
2011-10-07 13:53                     ` Corinna Vinschen
     [not found]       ` <12954.5351061553$1317744599@news.gmane.org>
2011-10-04 17:27         ` [RFA-v2] " Tom Tromey
2011-10-04 21:35           ` [RFA-v3] " Pierre Muller
     [not found]           ` <29288.743020925$1317764135@news.gmane.org>
2011-10-05 13:53             ` Tom Tromey
2011-10-05 14:25               ` Pierre Muller

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='003901cc82b0$471e3a00$d55aae00$@muller@ics-cnrs.unistra.fr' \
    --to=pierre.muller@ics-cnrs.unistra.fr \
    --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