From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22243 invoked by alias); 30 Mar 2011 12:15:37 -0000 Received: (qmail 21890 invoked by uid 22791); 30 Mar 2011 12:15:31 -0000 X-SWARE-Spam-Status: No, hits=-1.3 required=5.0 tests=AWL,BAYES_00,MSGID_MULTIPLE_AT,TW_CP,TW_SW X-Spam-Check-By: sourceware.org Received: from mailhost.u-strasbg.fr (HELO mailhost.u-strasbg.fr) (130.79.200.157) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 30 Mar 2011 12:15:23 +0000 Received: from md2.u-strasbg.fr (md2.u-strasbg.fr [130.79.200.187]) by mailhost.u-strasbg.fr (8.14.3/jtpda-5.5pre1) with ESMTP id p2UCFKcO065333 for ; Wed, 30 Mar 2011 14:15:20 +0200 (CEST) (envelope-from pierre.muller@ics-cnrs.unistra.fr) Received: from mailserver.u-strasbg.fr (ms7.u-strasbg.fr [130.79.204.16]) by md2.u-strasbg.fr (8.14.4/jtpda-5.5pre1) with ESMTP id p2UC5IdS021105 for ; Wed, 30 Mar 2011 14:05:18 +0200 (CEST) (envelope-from pierre.muller@ics-cnrs.unistra.fr) Received: from E6510Muller (gw-ics.u-strasbg.fr [130.79.210.225]) (user=mullerp mech=LOGIN) by mailserver.u-strasbg.fr (8.14.4/jtpda-5.5pre1) with ESMTP id p2UC5HDn057458 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO) for ; Wed, 30 Mar 2011 14:05:18 +0200 (CEST) (envelope-from pierre.muller@ics-cnrs.unistra.fr) From: "Pierre Muller" To: Subject: [RFC 9/9] Unify windows specifics into common/windows-hdep files Date: Wed, 30 Mar 2011 12:15:00 -0000 Message-ID: <00ba01cbeed2$bbf50150$33df03f0$@muller@ics-cnrs.unistra.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-03/txt/msg01202.txt.bz2 Patch that adapts windows-nat.c source code to new windows-hdep code. I left one #ifdef USE_WIDE_WINAPI in with a comment: /* To use ascii char strings in wide strings, we should use "%S" instead of "%s" in the format string, but Cygwin doesn't seem to honor this. */ This will require special handling in my followup patch concerning support of MSYS shell. Pierre Muller GDB pascal language maintainer ChangeLog entry: 2011-03-30 Pierre Muller * windows-nat.c (windows-hdep.h): New inclide. Remove macros moved to windows-hdep.h. (get_module_name): Adapt to windows-hdep code. (windows_make_so): Likewise. (windows_create_inferior): Reorganize using new windows-hdep code. (bad_GetModuleFileNameEx): New function relacing both... (bad_GetModuleFileNameExA, bad_GetModuleFileNameExW): Removed. (_initialize_loadable): Use _G_SUFFIX macro to unify search for GetModuleFileNameEx{A/W} without using ifdefs. diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index 4334755..dd5cb0d 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -63,6 +63,7 @@ #include "i386-tdep.h" #include "i387-tdep.h" +#include "common/windows-hdep.h" #include "windows-tdep.h" #include "windows-nat.h" #include "i386-nat.h" @@ -93,49 +94,14 @@ static BOOL WINAPI (*OpenProcessToken)(HANDLE, DWORD, PHANDLE); static BOOL WINAPI (*GetCurrentConsoleFont) (HANDLE, BOOL, CONSOLE_FONT_INFO *); static COORD WINAPI (*GetConsoleFontSize) (HANDLE, DWORD); +static DWORD WINAPI (*GetModuleFileNameEx) (HANDLE, HMODULE, LPGSTR, DWORD); static struct target_ops windows_ops; -#undef STARTUPINFO -#undef CreateProcess -#undef GetModuleFileNameEx - -#ifndef __CYGWIN__ -# define __PMAX (MAX_PATH + 1) - static DWORD WINAPI (*GetModuleFileNameEx) (HANDLE, HMODULE, LPSTR, DWORD); -# define STARTUPINFO STARTUPINFOA -# define CreateProcess CreateProcessA -# define GetModuleFileNameEx_name "GetModuleFileNameExA" -# define bad_GetModuleFileNameEx bad_GetModuleFileNameExA -#else -# define __PMAX PATH_MAX +#ifdef __CYGWIN__ /* The starting and ending address of the cygwin1.dll text segment. */ static CORE_ADDR cygwin_load_start; static CORE_ADDR cygwin_load_end; -# if CYGWIN_VERSION_DLL_MAKE_COMBINED(CYGWIN_VERSION_API_MAJOR,CYGWIN_VERSION_API _MINOR) >= 181 -# define __USEWIDE - typedef wchar_t cygwin_buf_t; - static DWORD WINAPI (*GetModuleFileNameEx) (HANDLE, HMODULE, - LPWSTR, DWORD); -# define STARTUPINFO STARTUPINFOW -# define CreateProcess CreateProcessW -# define GetModuleFileNameEx_name "GetModuleFileNameExW" -# define bad_GetModuleFileNameEx bad_GetModuleFileNameExW -# else -# define CCP_POSIX_TO_WIN_W 1 -# define CCP_WIN_W_TO_POSIX 3 -# define cygwin_conv_path(op, from, to, size) \ - (op == CCP_WIN_W_TO_POSIX) ? \ - cygwin_conv_to_full_posix_path (from, to) : \ - cygwin_conv_to_win32_path (from, to) - typedef char cygwin_buf_t; - static DWORD WINAPI (*GetModuleFileNameEx) (HANDLE, HMODULE, LPSTR, DWORD); -# define STARTUPINFO STARTUPINFOA -# define CreateProcess CreateProcessA -# define GetModuleFileNameEx_name "GetModuleFileNameExA" -# define bad_GetModuleFileNameEx bad_GetModuleFileNameExA -# define CW_SET_DOS_FILE_WARNING -1 /* no-op this for older Cygwin */ -# endif #endif static int have_saved_context; /* True if we've saved context from a @@ -530,16 +496,16 @@ get_module_name (LPVOID base_address, char *dll_name_ret) HMODULE *DllHandle = dh_buf; /* Set to temporary storage for initial query. */ DWORD cbNeeded; -#ifdef __CYGWIN__ - cygwin_buf_t pathbuf[__PMAX]; /* Temporary storage prior to converting to - posix form. __PMAX is always enough - as long as SO_NAME_MAX_PATH_SIZE is defined - as 512. */ -#endif +/* Temporary storage prior to converting to posix form. __PMAX is always + enough as long as SO_NAME_MAX_PATH_SIZE is defined as 512. */ + win_buf_t pathbuf[__PMAX]; cbNeeded = 0; /* Find size of buffer needed to handle list of modules loaded in inferior. */ + DEBUG_EVENTS (("get_module_name for handle 0x%x\n", + (unsigned int) (uintptr_t) current_process_handle)); + if (!EnumProcessModules (current_process_handle, DllHandle, sizeof (HMODULE), &cbNeeded) || !cbNeeded) goto failed; @@ -564,22 +530,14 @@ get_module_name (LPVOID base_address, char *dll_name_ret) if (!base_address || mi.lpBaseOfDll == base_address) { /* Try to find the name of the given module. */ -#ifdef __CYGWIN__ - /* Cygwin prefers that the path be in /x/y/z format. */ len = GetModuleFileNameEx (current_process_handle, DllHandle[i], pathbuf, __PMAX); if (len == 0) error (_("Error getting dll name: %lu."), GetLastError ()); - if (cygwin_conv_path (CCP_WIN_W_TO_POSIX, pathbuf, dll_name_ret, - __PMAX) < 0) + /* Cygwin prefers that the path be in /x/y/z format. */ + if (windows_conv_path (WINDOWS_NATIVE_TO_POSIX, pathbuf, + dll_name_ret, __PMAX) < 0) error (_("Error converting dll name to POSIX: %d."), errno); -#else - len = GetModuleFileNameEx (current_process_handle, - DllHandle[i], dll_name_ret, __PMAX); - if (len == 0) - error (_("Error getting dll name: %u."), - (unsigned) GetLastError ()); -#endif return 1; /* success */ } } @@ -694,27 +652,20 @@ windows_make_so (const char *name, LPVOID load_addr) } if (strcasecmp (buf, "ntdll.dll") == 0) { - GetSystemDirectory (buf, sizeof (buf)); + GetSystemDirectoryA (buf, sizeof (buf)); strcat (buf, "\\ntdll.dll"); } #else - cygwin_buf_t buf[__PMAX]; + win_buf_t buf[__PMAX]; buf[0] = 0; if (access (name, F_OK) != 0) { if (strcasecmp (name, "ntdll.dll") == 0) -#ifdef __USEWIDE - { - GetSystemDirectoryW (buf, sizeof (buf) / sizeof (wchar_t)); - wcscat (buf, L"\\ntdll.dll"); - } -#else { - GetSystemDirectoryA (buf, sizeof (buf) / sizeof (wchar_t)); - strcat (buf, "\\ntdll.dll"); + GetSystemDirectory (buf, sizeof (buf) / sizeof (win_buf_t)); + windows_strcat (buf, _G("\\ntdll.dll")); } -#endif } #endif so = XZALLOC (struct so_list); @@ -725,8 +676,8 @@ windows_make_so (const char *name, LPVOID load_addr) strcpy (so->so_name, buf); #else if (buf[0]) - cygwin_conv_path (CCP_WIN_W_TO_POSIX, buf, so->so_name, - SO_NAME_MAX_PATH_SIZE); + windows_conv_path (WINDOWS_NATIVE_TO_POSIX, buf, so->so_name, + SO_NAME_MAX_PATH_SIZE); else { char *rname = realpath (name, NULL); @@ -1978,22 +1929,22 @@ windows_create_inferior (struct target_ops *ops, char *exec_file, char *allargs, char **in_env, int from_tty) { STARTUPINFO si; -#ifdef __CYGWIN__ - cygwin_buf_t real_path[__PMAX]; - cygwin_buf_t shell[__PMAX]; /* Path to shell */ + win_buf_t real_path[__PMAX]; + win_buf_t shell[__PMAX]; /* Path to shell */ const char *sh; - cygwin_buf_t *toexec; - cygwin_buf_t *cygallargs; - cygwin_buf_t *args; + char *entry_exec_file = exec_file; + win_buf_t *toexec; + win_buf_t *cygallargs; + win_buf_t *args; size_t len; + int i, j; +#ifdef __CYGWIN__ int tty; int ostdin, ostdout, ostderr; #else - char real_path[__PMAX]; - char shell[__PMAX]; /* Path to shell */ - char *toexec; - char *args; HANDLE tty; + char ashell [__PMAX]; + char msys_exec_file [__PMAX]; #endif PROCESS_INFORMATION pi; BOOL ret; @@ -2012,60 +1963,52 @@ windows_create_inferior (struct target_ops *ops, char *exec_file, if (new_console) windows_set_console_info (&si, &flags); -#ifdef __CYGWIN__ if (!useshell) { flags |= DEBUG_ONLY_THIS_PROCESS; - if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, exec_file, real_path, - __PMAX * sizeof (cygwin_buf_t)) < 0) + if (windows_conv_path (WINDOWS_POSIX_TO_NATIVE, exec_file, real_path, + __PMAX * sizeof (win_buf_t)) < 0) error (_("Error starting executable: %d"), errno); toexec = real_path; -#ifdef __USEWIDE - len = mbstowcs (NULL, allargs, 0) + 1; + len = (size_t) (windows_a_to_g_strlen (allargs)) + 1; if (len == (size_t) -1) error (_("Error starting executable: %d"), errno); - cygallargs = (wchar_t *) alloca (len * sizeof (wchar_t)); - mbstowcs (cygallargs, allargs, len); -#else - cygallargs = allargs; -#endif + cygallargs = (win_buf_t *) alloca (len * sizeof (win_buf_t)); + windows_a_to_g_strncpy (cygallargs, allargs, len); } +#ifdef __CYGWIN__ else { sh = getenv ("SHELL"); if (!sh) sh = "/bin/sh"; - if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, sh, shell, __PMAX) < 0) + if (windows_conv_path (WINDOWS_POSIX_TO_NATIVE, sh, shell, __PMAX) + < 0) error (_("Error starting executable via shell: %d"), errno); -#ifdef __USEWIDE - len = sizeof (L" -c 'exec '") + mbstowcs (NULL, exec_file, 0) - + mbstowcs (NULL, allargs, 0) + 2; - cygallargs = (wchar_t *) alloca (len * sizeof (wchar_t)); + len = sizeof (_G(" -c 'exec '")) + windows_a_to_g_strlen (exec_file) + + windows_a_to_g_strlen (allargs) + 2; + cygallargs = (win_buf_t *) alloca (len * sizeof (win_buf_t)); + +#ifdef USE_WIDE_WINAPI + /* To use ascii char strings in wide strings, we should + use "%S" instead of "%s" in the format string, but Cygwin doesn't + seem to honor this. */ swprintf (cygallargs, len, L" -c 'exec %s %s'", exec_file, allargs); #else - cygallargs = (char *) - alloca (sizeof (" -c 'exec '") + strlen (exec_file) - + strlen (allargs) + 2); sprintf (cygallargs, " -c 'exec %s %s'", exec_file, allargs); #endif toexec = shell; flags |= DEBUG_PROCESS; } - -#ifdef __USEWIDE - args = (cygwin_buf_t *) alloca ((wcslen (toexec) + wcslen (cygallargs) + 2) - * sizeof (wchar_t)); - wcscpy (args, toexec); - wcscat (args, L" "); - wcscat (args, cygallargs); -#else - args = (cygwin_buf_t *) alloca (strlen (toexec) + strlen (cygallargs) + 2); - strcpy (args, toexec); - strcat (args, " "); - strcat (args, cygallargs); #endif - /* Prepare the environment vars for CreateProcess. */ + args = (win_buf_t *) alloca ((windows_strlen (toexec) + + windows_strlen (cygallargs) + 2) + * sizeof (win_buf_t)); + windows_strcpy (args, toexec); + windows_strcat (args, _G(" ")); + windows_strcat (args, cygallargs); +#ifdef __CYGWIN__ cygwin_internal (CW_SYNC_WINENV); if (!inferior_io_terminal) @@ -2088,37 +2031,7 @@ windows_create_inferior (struct target_ops *ops, char *exec_file, dup2 (tty, 2); } } - - 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) - { - close (tty); - dup2 (ostdin, 0); - dup2 (ostdout, 1); - dup2 (ostderr, 2); - close (ostdin); - close (ostdout); - close (ostderr); - } #else - toexec = exec_file; - args = alloca (strlen (toexec) + strlen (allargs) + 2); - strcpy (args, toexec); - strcat (args, " "); - strcat (args, allargs); - - flags |= DEBUG_ONLY_THIS_PROCESS; - if (!inferior_io_terminal) tty = INVALID_HANDLE_VALUE; else @@ -2140,22 +2053,34 @@ windows_create_inferior (struct target_ops *ops, char *exec_file, si.dwFlags |= STARTF_USESTDHANDLES; } } +#endif 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); + ret = CreateProcess (0, + args, /* command line */ + NULL, /* Security */ + NULL, /* thread */ + TRUE, /* inherit handles */ + flags, /* start flags */ + NULL, /* environment */ + NULL, /* current directory */ + &si, + &pi); +#ifdef __CYGWIN__ + if (tty >= 0) + { + close (tty); + dup2 (ostdin, 0); + dup2 (ostdout, 1); + dup2 (ostderr, 2); + close (ostdin); + close (ostdout); + close (ostderr); + } +#else if (tty != INVALID_HANDLE_VALUE) CloseHandle (tty); #endif - if (!ret) error (_("Error creating process %s, (error %d)."), exec_file, (unsigned) GetLastError ()); @@ -2624,19 +2549,11 @@ bad_EnumProcessModules (HANDLE w, HMODULE *x, DWORD y, LPDWORD z) return FALSE; } -#ifdef __USEWIDE -static DWORD WINAPI -bad_GetModuleFileNameExW (HANDLE w, HMODULE x, LPWSTR y, DWORD z) -{ - return 0; -} -#else static DWORD WINAPI -bad_GetModuleFileNameExA (HANDLE w, HMODULE x, LPSTR y, DWORD z) +bad_GetModuleFileNameEx (HANDLE w, HMODULE x, LPGSTR y, DWORD z) { return 0; } -#endif static BOOL WINAPI bad_GetModuleInformation (HANDLE w, HMODULE x, LPMODULEINFO y, DWORD z) @@ -2681,10 +2598,12 @@ _initialize_loadable (void) GetProcAddress (hm, "DebugBreakProcess"); DebugSetProcessKillOnExit = (void *) GetProcAddress (hm, "DebugSetProcessKillOnExit"); - GetConsoleFontSize = (void *) + GetConsoleFontSize = (void *) GetProcAddress (hm, "GetConsoleFontSize"); - GetCurrentConsoleFont = (void *) + GetCurrentConsoleFont = (void *) GetProcAddress (hm, "GetCurrentConsoleFont"); + EnumProcessModules = (void *) + GetProcAddress (hm, "K32EnumProcessModules"); } /* Set variables to dummy versions of these processes if the function @@ -2706,12 +2625,13 @@ _initialize_loadable (void) hm = LoadLibrary ("psapi.dll"); if (hm) { - EnumProcessModules = (void *) - GetProcAddress (hm, "EnumProcessModules"); + if (!EnumProcessModules) + EnumProcessModules = (void *) + GetProcAddress (hm, "EnumProcessModules"); GetModuleInformation = (void *) GetProcAddress (hm, "GetModuleInformation"); GetModuleFileNameEx = (void *) - GetProcAddress (hm, GetModuleFileNameEx_name); + GetProcAddress (hm, _G_SUFFIX ("GetModuleFileNameEx")); } if (!EnumProcessModules || !GetModuleInformation || !GetModuleFileNameEx) -- 1.7.4