public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: "Sebastián Puebla Castro" <spuebla@hotmail.com>
To: <gdb-patches@sourceware.org>
Subject: [PATCH] Pass correct environment to a Windows executable started as an inferior
Date: Thu, 05 Aug 2010 03:57:00 -0000	[thread overview]
Message-ID: <SNT110-W553622E76149C966A25851B3900@phx.gbl> (raw)


This patch fixes a bug found in ports of GDB 7.0/7.1 for Windows: an ejecutable
started as an inferior doesn't receive its own environment, possibly modified,
as expected; instead, it inherits the environment from current GDB instance.

In order to demonstrate the bug, do the following:

* Build a Windows executable from the source code below.

/* env-bug.c */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  char *env_name  = "PATH";
  char *env_value = getenv(env_name);

  if(env_name)
    printf("env-bug: %s=%s\n", env_name, env_value);
  else
    printf("env-bug: environment variable %s not found.\n", env_name);

  env_name  = "FOO";
  env_value = getenv(env_name);

  if(env_name)
    printf("env-bug: %s=%s\n", env_name, env_value);
  else
    printf("env-bug: environment variable %s not found.\n", env_name);

  return 0;
}

* Run gdb in Windows and pass the following commands to it (assuming the
  inferior was loaded):

(gdb) set environment FOO=bar
(gdb) path C:\\Users\\Public\\Desktop
(gdb) run

* Do you see any changes to those environment variables?

The patch fixes the bug by allocating memory for an environment block, then,
it copies the environment variables to the block, and pass its address as a
parameter to CreateProcess.

2010-05-10 Sebastián Puebla <spuebla@hotmail.com>

           * windows-nat.c (windows_create_inferior): Create environment
             block for new inferior.

Index: src/gdb/windows-nat.c
===================================================================
RCS file: /cvs/src/src/gdb/windows-nat.c,v
retrieving revision 1.208
diff -c -p -r1.208 windows-nat.c
*** src/gdb/windows-nat.c    16 Apr 2010 07:49:35 -0000    1.208
--- src/gdb/windows-nat.c    14 May 2010 13:41:51 -0000
*************** windows_create_inferior (struct target_o
*** 1908,1914 ****
    cygwin_buf_t *toexec;
    cygwin_buf_t *cygallargs;
    cygwin_buf_t *args;
!   size_t len;
    int tty;
    int ostdin, ostdout, ostderr;
  #else
--- 1908,1914 ----
    cygwin_buf_t *toexec;
    cygwin_buf_t *cygallargs;
    cygwin_buf_t *args;
!   cygwin_buf_t *env_block;
    int tty;
    int ostdin, ostdout, ostderr;
  #else
*************** windows_create_inferior (struct target_o
*** 1916,1926 ****
--- 1916,1930 ----
    char shell[__PMAX]; /* Path to shell */
    char *toexec;
    char *args;
+   char *env_block;
    HANDLE tty;
  #endif
    PROCESS_INFORMATION pi;
    BOOL ret;
    DWORD flags = 0;
+   size_t env_size = 1;
+   size_t len;
+   size_t i, j;
    const char *inferior_io_terminal = get_inferior_io_terminal ();
  
    if (!exec_file)
*************** windows_create_inferior (struct target_o
*** 2011,2025 ****
        }
      }
  
    windows_init_thread_list ();
    ret = CreateProcess (0,
!                      args,    /* command line */
!                      NULL,    /* Security */
!                      NULL,    /* thread */
!                      TRUE,    /* inherit handles */
!                      flags,   /* start flags */
!                      NULL,    /* environment */
!                      NULL,    /* current directory */
                       &si,
                       &pi);
    if (tty >= 0)
--- 2015,2058 ----
        }
      }
  
+ #ifdef __USEWIDE
+   for(i = 0; !in_env[i]; i++)
+   {
+     env_size += mbstowcs(NULL, in_env[i], 0) + 1;
+   }
+   
+   env_block = (cygwin_buf_t *) alloca(env_size * sizeof(wchar_t));
+ 
+   for(i = j = 0; !in_env[i]; i++)
+   {
+     j += mbstowcs(&env_block[j], in_env[i], env_size) + 1;
+   }
+ #else
+   for(i = 0; !in_env[i]; i++)
+   {
+     env_size += strlen(in_env[i]) + 1;
+   }
+ 
+   env_block = (cygwin_buf_t *) alloca(env_size);
+ 
+   for(i = j = 0; !in_env[i]; i++)
+   {
+     len = strlen(in_env[i]) + 1;
+     memcpy(&env_block[j], in_env[i], len);
+     j += len;
+   }
+ #endif
+   env_block[j] = 0;
+ 
    windows_init_thread_list ();
    ret = CreateProcess (0,
!                      args,            /* command line */
!                      NULL,            /* Security */
!                      NULL,            /* thread */
!                      TRUE,            /* inherit handles */
!                      flags,           /* start flags */
!                      env_block,       /* environment */
!                      NULL,            /* current directory */
                 &si,
                 &pi);
    if (tty >= 0)
*************** windows_create_inferior (struct target_o
*** 2063,2077 ****
        }
      }
  
    windows_init_thread_list ();
    ret = CreateProcessA (0,
!                       args,   /* command line */
!                       NULL,   /* Security */
!                       NULL,   /* thread */
!                       TRUE,   /* inherit handles */
!                       flags,  /* start flags */
!                       NULL,   /* environment */
!                       NULL,   /* current directory */
                        &si,
                        &pi);
    if (tty != INVALID_HANDLE_VALUE)
--- 2096,2125 ----
        }
      }
  
+   for(i = 0; !in_env[i]; i++)
+   {
+     env_size += strlen(in_env[i]) + 1;
+   }
+ 
+   env_block = alloca(env_size);
+ 
+   for(i = j = 0; !in_env[i]; i++)
+   {
+     len = strlen(in_env[i]) + 1;
+     memcpy(&env_block[j], in_env[i], len);
+     j += len;
+   }
+   env_block[j] = 0;
+ 
    windows_init_thread_list ();
    ret = CreateProcessA (0,
!                       args,           /* command line */
!                       NULL,           /* Security */
!                       NULL,           /* thread */
!                       TRUE,           /* inherit handles */
!                       flags,          /* start flags */
!                       env_block,      /* environment */
!                       NULL,           /* current directory */
                        &si,
                        &pi);
    if (tty != INVALID_HANDLE_VALUE)

 		 	   		  

             reply	other threads:[~2010-08-05  3:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-05  3:57 Sebastián Puebla Castro [this message]
2010-11-08 19:26 ` [PATCH/Windows] " Joel Brobecker
2010-11-11  6:17   ` Christopher Faylor

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=SNT110-W553622E76149C966A25851B3900@phx.gbl \
    --to=spuebla@hotmail.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).