[RFC] Reenable compilation with cygwin 1.5 versions

Pierre Muller
2010-03-03T16:54:55Z

I would like to get some comments
on the following patch.

After Corinna Vinschen patch
http://sourceware.org/ml/gdb-patches/2010-02/msg00677.html
GDB cannot be compiled with Cygwin 1.5... versions,
As 1.7 does not support Win9x and WinMe,
I would like to keep both support for
cygwin 1.5 and 1.7.

I started by using
cvs diff -r 1.203 --ifdef=__USE_OLD_CYGWIN_API_ windows-nat.c
and using the idea of Christopher idea in
http://sourceware.org/ml/gdb-patches/2010-03/msg00012.html

but I had to rework quite some time to get
to something that at least compiles for
mingw32, cygwin 1.5 and cygwin 1.7.

I did not really carefully check my patch yet,
but I would like to get some feedback now ..

I defined two macros:
__USE_OLD_CYGWIN_API_
and
__USE_NEW_CYGWIN_API_

but I realize that these kind of names are not
very nice and if someone could suggest better names,
it would be probably better ...

Concerning the remote-fileio.c patch,
I am not sure, but Corinna Vinschen seemed to
suggest that the use of PATH_MAX+1 was
an error anyway, so maybe PATH_MAX is OK
for both old and new cygwin versions.

Pierre Muller
Pascal language support maintainer for GDB

ChangeLog entry (not complete)

2010-03-03 Pierre Muller <muller@ics.u-strasbg.fr>

* remote-fileio.c:
__USE_OLD_CYGWIN_API_: new macro.
(remote_fileio_func_rename): Allow compilation with old
Cygwin API.
* windows-nat.c:
__USE_OLD_CYGWIN_API_, __USE_NEW_CYGWIN_API_: new macros.
Adapt code to allow compilation with old Cygwin API.
(get_module_name): Allow compilation with old Cygwin API.
(windows_make_so): Idem.
(get_image_name): Idem.
(windows_detach): Idem.
(windows_pid_to_exec_file): Idem.
(windows_create_inferior): Idem.
(_initialize_windows_nat): Idem.
(bad_GetModuleFileNameExW): Only define if __USE_NEW_CYGWIN_API_ is
defined.
(_initialize_loadable): Idem.

Index: remote-fileio.c
===================================================================
RCS file: /cvs/src/src/gdb/remote-fileio.c,v
retrieving revision 1.34
diff -u -p -r1.34 remote-fileio.c
--- remote-fileio.c 1 Mar 2010 09:09:24 -0000 1.34
+++ remote-fileio.c 3 Mar 2010 16:11:15 -0000
@@ -35,6 +35,11 @@
#include <sys/time.h>
#ifdef __CYGWIN__
#include <sys/cygwin.h> /* For
cygwin_conv_to_full_posix_path. */
+#include <cygwin/version.h>
+#if
CYGWIN_VERSION_DLL_MAKE_COMBINED(CYGWIN_VERSION_API_MAJOR,CYGWIN_VERSION_API
_MINOR) < 181
+#define __USE_OLD_CYGWIN_API_
+#endif
+
#endif
#include <signal.h>

@@ -1021,14 +1026,24 @@ remote_fileio_func_rename (char *buf)
errno = EISDIR;
else
{
+#ifndef __USE_OLD_CYGWIN_API_
char oldfullpath[PATH_MAX];
char newfullpath[PATH_MAX];
+#else /* __USE_OLD_CYGWIN_API_ */
+ char oldfullpath[PATH_MAX + 1];
+ char newfullpath[PATH_MAX + 1];
+#endif /* __USE_OLD_CYGWIN_API_ */
int len;

+#ifndef __USE_OLD_CYGWIN_API_
cygwin_conv_path (CCP_WIN_A_TO_POSIX, oldpath,
oldfullpath,
PATH_MAX);
cygwin_conv_path (CCP_WIN_A_TO_POSIX, newpath,
newfullpath,
PATH_MAX);
+#else /* __USE_OLD_CYGWIN_API_ */
+ cygwin_conv_to_full_posix_path (oldpath, oldfullpath);
+ cygwin_conv_to_full_posix_path (newpath, newfullpath);
+#endif /* __USE_OLD_CYGWIN_API_ */
len = strlen (oldfullpath);
if (newfullpath[len] == '/'
&& !strncmp (oldfullpath, newfullpath, len))
Index: windows-nat.c
===================================================================
RCS file: /cvs/src/src/gdb/windows-nat.c,v
retrieving revision 1.204
diff -u -p -r1.204 windows-nat.c
--- windows-nat.c 1 Mar 2010 09:09:24 -0000 1.204
+++ windows-nat.c 3 Mar 2010 16:11:15 -0000
@@ -41,6 +41,12 @@
#include <psapi.h>
#ifdef __CYGWIN__
#include <sys/cygwin.h>
+#include <cygwin/version.h>
+#if
CYGWIN_VERSION_DLL_MAKE_COMBINED(CYGWIN_VERSION_API_MAJOR,CYGWIN_VERSION_API
_MINOR) < 181
+#define __USE_OLD_CYGWIN_API_
+#else
+#define __USE_NEW_CYGWIN_API_
+#endif
#endif
#include <signal.h>

@@ -71,11 +77,11 @@
#define DebugBreakProcess dyn_DebugBreakProcess
#define DebugSetProcessKillOnExit dyn_DebugSetProcessKillOnExit
#define EnumProcessModules dyn_EnumProcessModules
-#ifndef __CYGWIN__
+#ifndef __USE_NEW_CYGWIN_API_
#define GetModuleFileNameExA dyn_GetModuleFileNameExA
-#else
+#else /* __USE_NEW_CYGWIN_API_ */
#define GetModuleFileNameExW dyn_GetModuleFileNameExW
-#endif
+#endif /* __USE_NEW_CYGWIN_API_ */
#define GetModuleInformation dyn_GetModuleInformation
#define LookupPrivilegeValueA dyn_LookupPrivilegeValueA
#define OpenProcessToken dyn_OpenProcessToken
@@ -87,13 +93,13 @@ static BOOL WINAPI (*DebugBreakProcess)
static BOOL WINAPI (*DebugSetProcessKillOnExit) (BOOL);
static BOOL WINAPI (*EnumProcessModules) (HANDLE, HMODULE *, DWORD,
LPDWORD);
-#ifndef __CYGWIN__
+#ifndef __USE_NEW_CYGWIN_API_
static DWORD WINAPI (*GetModuleFileNameExA) (HANDLE, HMODULE, LPSTR,
DWORD);
-#else
+#else /* __USE_NEW_CYGWIN_API_ */
static DWORD WINAPI (*GetModuleFileNameExW) (HANDLE, HMODULE, LPWSTR,
DWORD);
-#endif
+#endif /* __USE_NEW_CYGWIN_API_ */
static BOOL WINAPI (*GetModuleInformation) (HANDLE, HMODULE, LPMODULEINFO,
DWORD);
static BOOL WINAPI (*LookupPrivilegeValueA)(LPCSTR, LPCSTR, PLUID);
@@ -492,10 +498,17 @@ get_module_name (LPVOID base_address, ch
HMODULE *DllHandle = dh_buf; /* Set to temporary storage for initial
query */
DWORD cbNeeded;
#ifdef __CYGWIN__
+#ifdef __USE_NEW_CYGWIN_API_
wchar_t pathbuf[PATH_MAX]; /* Temporary storage prior to converting to
posix form. PATH_MAX is always enough
as long as SO_NAME_MAX_PATH_SIZE is
defined
as 512. */
+#else /* not __USE_NOT_CYGWIN_API_ */
+ char pathbuf[PATH_MAX + 1]; /* Temporary storage prior to converting to
+ posix form */
+#endif /* not __USE_NEW_CYGWIN_API_ */
+#else
+ char *pathbuf = dll_name_ret; /* Just copy directly to passed-in
arg */
#endif

cbNeeded = 0;
@@ -524,7 +537,7 @@ get_module_name (LPVOID base_address, ch
if (!base_address || mi.lpBaseOfDll == base_address)
{
/* Try to find the name of the given module */
-#ifdef __CYGWIN__
+#ifdef __USE_NEW_CYGWIN_API_
/* Cygwin prefers that the path be in /x/y/z format */
len = GetModuleFileNameExW (current_process_handle,
DllHandle[i], pathbuf, PATH_MAX);
@@ -533,12 +546,16 @@ get_module_name (LPVOID base_address, ch
if (cygwin_conv_path (CCP_WIN_W_TO_POSIX, pathbuf, dll_name_ret,
PATH_MAX) < 0)
error (_("Error converting dll name to POSIX: %d."), errno);
-#else
+#else /* not __USE_NEW_CYGWIN_API_ */
len = GetModuleFileNameExA (current_process_handle,
- DllHandle[i], dll_name_ret, MAX_PATH);
+ DllHandle[i], pathbuf, MAX_PATH);
+#endif /* not __USE_NEW_CYGWIN_API_ */
if (len == 0)
error (_("Error getting dll name: %u."), (unsigned) GetLastError
());
-#endif
+#ifdef __USE_OLD_CYGWIN_API_
+ /* Cygwin prefers that the path be in /x/y/z format */
+ cygwin_conv_to_full_posix_path (pathbuf, dll_name_ret);
+#endif /* __USE_OLD_CYGWIN_API_ */
return 1; /* success */
}
}
@@ -629,7 +646,7 @@ windows_make_so (const char *name, LPVOI
{
struct so_list *so;
char *p;
-#ifndef __CYGWIN__
+#ifndef __USE_NEW_CYGWIN_API_
char buf[MAX_PATH + 1];
char cwd[MAX_PATH + 1];
WIN32_FIND_DATA w32_fd;
@@ -651,12 +668,13 @@ windows_make_so (const char *name, LPVOI
SetCurrentDirectory (cwd);
}
}
+
if (strcasecmp (buf, "ntdll.dll") == 0)
{
GetSystemDirectory (buf, sizeof (buf));
strcat (buf, "\\ntdll.dll");
}
-#else
+#else /* __USE_NEW_CYGWIN_API_ */
wchar_t buf[PATH_MAX];

buf[0] = L'\0';
@@ -668,7 +686,7 @@ windows_make_so (const char *name, LPVOI
wcscat (buf, L"\\ntdll.dll");
}
}
-#endif
+#endif /* __USE_NEW_CYGWIN_API_ */
so = XZALLOC (struct so_list);
so->lm_info = (struct lm_info *) xmalloc (sizeof (struct lm_info));
so->lm_info->load_addr = load_addr;
@@ -676,6 +694,7 @@ windows_make_so (const char *name, LPVOI
#ifndef __CYGWIN__
strcpy (so->so_name, buf);
#else
+#ifdef __USE_NEW_CYGWIN_API_
if (buf[0])
cygwin_conv_path (CCP_WIN_W_TO_POSIX, buf, so->so_name,
SO_NAME_MAX_PATH_SIZE);
@@ -690,6 +709,9 @@ windows_make_so (const char *name, LPVOI
else
error (_("dll path too long"));
}
+#else /* __USE_OLD_CYGWIN_API_ */
+ cygwin_conv_to_posix_path (buf, so->so_name);
+#endif /* __USE_OLD_CYGWIN_API_ */
/* Record cygwin1.dll .text start/end. */
p = strchr (so->so_name, '\0') - (sizeof ("/cygwin1.dll") - 1);
if (p >= so->so_name && strcasecmp (p, "/cygwin1.dll") == 0)
@@ -728,7 +750,7 @@ windows_make_so (const char *name, LPVOI
static char *
get_image_name (HANDLE h, void *address, int unicode)
{
-#ifdef __CYGWIN__
+#ifdef __USE_NEW_CYGWIN_API_
static char buf[PATH_MAX];
#else
static char buf[(2 * MAX_PATH) + 1];
@@ -763,12 +785,12 @@ get_image_name (HANDLE h, void *address,
WCHAR *unicode_address = (WCHAR *) alloca (len * sizeof (WCHAR));
ReadProcessMemory (h, address_ptr, unicode_address, len * sizeof
(WCHAR),
&done);
-#ifdef __CYGWIN__
+#ifdef __USE_NEW_CYGWIN_API_
wcstombs (buf, unicode_address, PATH_MAX);
#else
WideCharToMultiByte (CP_ACP, 0, unicode_address, len, buf, sizeof
buf,
0, 0);
-#endif
+#endif /* __USE_NEW_CYGWIN_API_ */
}

return buf;
@@ -780,7 +802,7 @@ static int
handle_load_dll (void *dummy)
{
LOAD_DLL_DEBUG_INFO *event = &current_event.u.LoadDll;
-#ifdef __CYGWIN__
+#ifdef __USE_NEW_CYGWIN_API_
char dll_buf[PATH_MAX];
#else
char dll_buf[MAX_PATH + 1];
@@ -1825,9 +1847,13 @@ windows_detach (struct target_ops *ops,
static char *
windows_pid_to_exec_file (int pid)
{
-#ifdef __CYGWIN__
+#ifdef __USE_NEW_CYGWIN_API_
static char path[PATH_MAX];
+#else /* not __USE_NEW_CYGWIN_API_ */
+ static char path[MAX_PATH + 1];
+#endif /* not __USE_NEW_CYGWIN_API_ */

+#ifdef __CYGWIN__
/* Try to find exe name as symlink target of /proc/<pid>/exe */
int nchars;
char procexe[sizeof ("/proc/4294967295/exe")];
@@ -1838,8 +1864,6 @@ windows_pid_to_exec_file (int pid)
path[nchars] = '\0'; /* Got it */
return path;
}
-#else
- static char path[MAX_PATH + 1];
#endif

/* If we get here then either Cygwin is hosed, this isn't a Cygwin
version
@@ -1877,23 +1901,28 @@ static void
windows_create_inferior (struct target_ops *ops, char *exec_file,
char *allargs, char **in_env, int from_tty)
{
-#ifdef __CYGWIN__
+#ifdef __USE_NEW_CYGWIN_API_
STARTUPINFOW si;
wchar_t real_path[PATH_MAX];
wchar_t shell[PATH_MAX]; /* Path to shell */
- const char *sh;
wchar_t *toexec;
wchar_t *cygallargs;
wchar_t *args;
size_t len;
- int tty;
- int ostdin, ostdout, ostderr;
-#else
- STARTUPINFOA si;
- char real_path[PATH_MAX];
+#else /* not __USE_NEW_CYGWIN_API_ */
+ STARTUPINFO si;
+ char real_path[MAXPATHLEN];
char shell[MAX_PATH + 1]; /* Path to shell */
char *toexec;
char *args;
+#endif /* __USE_OLD_CYGWIN_API_ */
+ const char *sh;
+#ifdef __USE_NEW_CYGWIN_API_
+#endif /* __USE_NEW_CYGWIN_API_ */
+#ifdef __CYGWIN__
+ int tty;
+ int ostdin, ostdout, ostderr;
+#else
HANDLE tty;
#endif
PROCESS_INFORMATION pi;
@@ -1917,35 +1946,60 @@ windows_create_inferior (struct target_o
if (!useshell)
{
flags |= DEBUG_ONLY_THIS_PROCESS;
+#ifdef __USE_NEW_CYGWIN_API_
if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, exec_file, real_path,
PATH_MAX * sizeof (wchar_t)) < 0)
error (_("Error starting executable: %d"), errno);
+#else /* not __USE_NEW_CYGWIN_API_ */
+ cygwin_conv_to_win32_path (exec_file, real_path);
+#endif /* not __USE_NEW_CYGWIN_API_ */
toexec = real_path;
+#ifdef __USE_NEW_CYGWIN_API_
len = mbstowcs (NULL, allargs, 0) + 1;
if (len == (size_t) -1)
error (_("Error starting executable: %d"), errno);
cygallargs = (wchar_t *) alloca (len * sizeof (wchar_t));
mbstowcs (cygallargs, allargs, len);
+#endif /* __USE_NEW_CYGWIN_API_ */
}
else
{
+#ifdef __USE_OLD_CYGWIN_API_
+ char *newallargs;
+#endif /* __USE_OLD_CYGWIN_API_ */
sh = getenv ("SHELL");
if (!sh)
sh = "/bin/sh";
+#ifdef __USE_NEW_CYGWIN_API_
if (cygwin_conv_path (CCP_POSIX_TO_WIN_W, sh, shell, PATH_MAX) < 0)
error (_("Error starting executable via shell: %d"), errno);
len = sizeof (L" -c 'exec '") + mbstowcs (NULL, exec_file, 0)
+ mbstowcs (NULL, allargs, 0) + 2;
cygallargs = (wchar_t *) alloca (len * sizeof (wchar_t));
swprintf (cygallargs, len, L" -c 'exec %s %s'", exec_file, allargs);
+#else /* not __USE_NEW_CYGWIN_API_ */
+ cygwin_conv_to_win32_path (sh, shell);
+ newallargs = alloca (sizeof (" -c 'exec '") + strlen (exec_file)
+ + strlen (allargs) + 2);
+ sprintf (newallargs, " -c 'exec %s %s'", exec_file, allargs);
+ allargs = newallargs;
+#endif /* not __USE_NEW_CYGWIN_API_ */
toexec = shell;
flags |= DEBUG_PROCESS;
}
+#ifdef __USE_NEW_CYGWIN_API_
args = (wchar_t *) alloca ((wcslen (toexec) + wcslen (cygallargs) + 2)
* sizeof (wchar_t));
wcscpy (args, toexec);
wcscat (args, L" ");
wcscat (args, cygallargs);
+#else /* not __USE_NEW_CYGWIN_API_ */
+ args = alloca (strlen (toexec) + strlen (allargs) + 2);
+ strcpy (args, toexec);
+ strcat (args, " ");
+ strcat (args, allargs);
+#endif /* not __USE_NEW_CYGWIN_API_ */
+
/* Prepare the environment vars for CreateProcess. */
cygwin_internal (CW_SYNC_WINENV);

@@ -1969,7 +2023,7 @@ windows_create_inferior (struct target_o
dup2 (tty, 2);
}
}
-
+#ifdef __USE_NEW_CYGWIN_API_
windows_init_thread_list ();
ret = CreateProcessW (0,
args, /* command line */
@@ -1981,25 +2035,16 @@ windows_create_inferior (struct target_o
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
+#endif /* __USE_NEW_CYGWIN_API_ */
+#else /* not __CYGWIN__ */
+ toexec = exec_file;
+ flags |= DEBUG_ONLY_THIS_PROCESS;
+
args = alloca (strlen (toexec) + strlen (allargs) + 2);
strcpy (args, toexec);
strcat (args, " ");
strcat (args, allargs);

- toexec = exec_file;
- flags |= DEBUG_ONLY_THIS_PROCESS;
-
if (!inferior_io_terminal)
tty = INVALID_HANDLE_VALUE;
else
@@ -2021,6 +2066,8 @@ windows_create_inferior (struct target_o
si.dwFlags |= STARTF_USESTDHANDLES;
}
}
+#endif /* not __CYGWIN__ */
+#ifndef __USE_NEW_CYGWIN_API_

windows_init_thread_list ();
ret = CreateProcessA (0,
@@ -2033,6 +2080,19 @@ windows_create_inferior (struct target_o
NULL, /* current directory */
&si,
&pi);
+#endif /* not _USE_NEW__CYGWIN__API_ */
+#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
@@ -2299,10 +2359,9 @@ _initialize_windows_nat (void)

init_windows_ops ();

-#ifdef __CYGWIN__
+#ifdef __USE_NEW_CYGWIN_API_
cygwin_internal (CW_SET_DOS_FILE_WARNING, 0);
#endif
-
c = add_com ("dll-symbols", class_files, dll_symbol_command,
_("Load dll library symbols from FILE."));
set_cmd_completer (c, filename_completer);
@@ -2485,7 +2544,7 @@ bad_EnumProcessModules (HANDLE w, HMODUL
{
return FALSE;
}
-#ifndef __CYGWIN__
+#ifndef __USE_NEW_CYGWIN_API_
static DWORD WINAPI
bad_GetModuleFileNameExA (HANDLE w, HMODULE x, LPSTR y, DWORD z)
{
@@ -2547,17 +2606,17 @@ _initialize_loadable (void)
GetProcAddress (hm, "EnumProcessModules");
dyn_GetModuleInformation = (void *)
GetProcAddress (hm, "GetModuleInformation");
-#ifndef __CYGWIN__
+#ifndef __USE_NEW_CYGWIN_API_
dyn_GetModuleFileNameExA = (void *)
GetProcAddress (hm, "GetModuleFileNameExA");
#else
dyn_GetModuleFileNameExW = (void *)
GetProcAddress (hm, "GetModuleFileNameExW");
-#endif
+#endif /* __USE_NEW_CYGWIN_API_ */
}

if (!dyn_EnumProcessModules || !dyn_GetModuleInformation
-#ifndef __CYGWIN__
+#ifndef __USE_NEW_CYGWIN_API_
|| !dyn_GetModuleFileNameExA
#else
|| !dyn_GetModuleFileNameExW
@@ -2568,7 +2627,7 @@ _initialize_loadable (void)
wasn't found in psapi.dll. */
dyn_EnumProcessModules = bad_EnumProcessModules;
dyn_GetModuleInformation = bad_GetModuleInformation;
-#ifndef __CYGWIN__
+#ifndef __USE_NEW_CYGWIN_API_
dyn_GetModuleFileNameExA = bad_GetModuleFileNameExA;
#else
dyn_GetModuleFileNameExW = bad_GetModuleFileNameExW;