| 1 | // win32.cc - Helper functions for Microsoft-flavored OSs. | 
|---|
| 2 |  | 
|---|
| 3 | /* Copyright (C) 2002, 2003  Free Software Foundation | 
|---|
| 4 |  | 
|---|
| 5 | This file is part of libgcj. | 
|---|
| 6 |  | 
|---|
| 7 | This software is copyrighted work licensed under the terms of the | 
|---|
| 8 | Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for | 
|---|
| 9 | details.  */ | 
|---|
| 10 |  | 
|---|
| 11 | #include <config.h> | 
|---|
| 12 | #include <platform.h> | 
|---|
| 13 | #include <jvm.h> | 
|---|
| 14 | #include <sys/timeb.h> | 
|---|
| 15 | #include <stdlib.h> | 
|---|
| 16 |  | 
|---|
| 17 | #include <java/lang/ArithmeticException.h> | 
|---|
| 18 | #include <java/lang/UnsupportedOperationException.h> | 
|---|
| 19 | #include <java/util/Properties.h> | 
|---|
| 20 |  | 
|---|
| 21 | static LONG CALLBACK | 
|---|
| 22 | win32_exception_handler (LPEXCEPTION_POINTERS e) | 
|---|
| 23 | { | 
|---|
| 24 | if (e->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) | 
|---|
| 25 | _Jv_ThrowNullPointerException(); | 
|---|
| 26 | else if (e->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO) | 
|---|
| 27 | throw new java::lang::ArithmeticException; | 
|---|
| 28 | else | 
|---|
| 29 | return EXCEPTION_CONTINUE_SEARCH; | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | // Platform-specific executable name | 
|---|
| 33 | static char exec_name[MAX_PATH]; | 
|---|
| 34 | // initialized in _Jv_platform_initialize() | 
|---|
| 35 |  | 
|---|
| 36 | const char *_Jv_ThisExecutable (void) | 
|---|
| 37 | { | 
|---|
| 38 | return exec_name; | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | // Platform-specific VM initialization. | 
|---|
| 42 | void | 
|---|
| 43 | _Jv_platform_initialize (void) | 
|---|
| 44 | { | 
|---|
| 45 | // Initialise winsock for networking | 
|---|
| 46 | WSADATA data; | 
|---|
| 47 | if (WSAStartup (MAKEWORD (1, 1), &data)) | 
|---|
| 48 | MessageBox (NULL, "Error initialising winsock library.", "Error", | 
|---|
| 49 | MB_OK | MB_ICONEXCLAMATION); | 
|---|
| 50 |  | 
|---|
| 51 | // Install exception handler | 
|---|
| 52 | SetUnhandledExceptionFilter (win32_exception_handler); | 
|---|
| 53 |  | 
|---|
| 54 | // Initialize our executable name | 
|---|
| 55 | GetModuleFileName(NULL, exec_name, sizeof(exec_name)); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | // gettimeofday implementation. | 
|---|
| 59 | jlong | 
|---|
| 60 | _Jv_platform_gettimeofday () | 
|---|
| 61 | { | 
|---|
| 62 | struct timeb t; | 
|---|
| 63 | ftime (&t); | 
|---|
| 64 | return t.time * 1000LL + t.millitm; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | // The following definitions "fake out" mingw to think that -mthreads | 
|---|
| 68 | // was enabled and that mingwthr.dll was linked. GCJ-compiled | 
|---|
| 69 | // applications don't need this helper library because we can safely | 
|---|
| 70 | // detect thread death (return from Thread.run()). | 
|---|
| 71 |  | 
|---|
| 72 | int _CRT_MT = 1; | 
|---|
| 73 |  | 
|---|
| 74 | extern "C" int | 
|---|
| 75 | __mingwthr_key_dtor (DWORD, void (*) (void *)) | 
|---|
| 76 | { | 
|---|
| 77 | // FIXME: for now we do nothing; this causes a memory leak of | 
|---|
| 78 | //        approximately 24 bytes per thread created. | 
|---|
| 79 | return 0; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | // Set platform-specific System properties. | 
|---|
| 83 | void | 
|---|
| 84 | _Jv_platform_initProperties (java::util::Properties* newprops) | 
|---|
| 85 | { | 
|---|
| 86 | // A convenience define. | 
|---|
| 87 | #define SET(Prop,Val) \ | 
|---|
| 88 | newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val)) | 
|---|
| 89 |  | 
|---|
| 90 | SET ("file.separator", "\\"); | 
|---|
| 91 | SET ("path.separator", ";"); | 
|---|
| 92 | SET ("line.separator", "\r\n"); | 
|---|
| 93 |  | 
|---|
| 94 | // Use GetCurrentDirectory to set 'user.dir'. | 
|---|
| 95 | DWORD buflen = MAX_PATH; | 
|---|
| 96 | char *buffer = (char *) _Jv_MallocUnchecked (buflen); | 
|---|
| 97 | if (buffer != NULL) | 
|---|
| 98 | { | 
|---|
| 99 | if (GetCurrentDirectory (buflen, buffer)) | 
|---|
| 100 | SET ("user.dir", buffer); | 
|---|
| 101 |  | 
|---|
| 102 | if (GetTempPath (buflen, buffer)) | 
|---|
| 103 | SET ("java.io.tmpdir", buffer); | 
|---|
| 104 |  | 
|---|
| 105 | _Jv_Free (buffer); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | // Use GetUserName to set 'user.name'. | 
|---|
| 109 | buflen = 257;  // UNLEN + 1 | 
|---|
| 110 | buffer = (char *) _Jv_MallocUnchecked (buflen); | 
|---|
| 111 | if (buffer != NULL) | 
|---|
| 112 | { | 
|---|
| 113 | if (GetUserName (buffer, &buflen)) | 
|---|
| 114 | SET ("user.name", buffer); | 
|---|
| 115 | _Jv_Free (buffer); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | // According to the api documentation for 'GetWindowsDirectory()', the | 
|---|
| 119 | // environmental variable HOMEPATH always specifies the user's home | 
|---|
| 120 | // directory or a default directory.  On the 3 windows machines I checked | 
|---|
| 121 | // only 1 had it set.  If it's not set, JDK1.3.1 seems to set it to | 
|---|
| 122 | // the windows directory, so we'll do the same. | 
|---|
| 123 | char *userHome = NULL; | 
|---|
| 124 | if ((userHome = ::getenv ("HOMEPATH")) == NULL ) | 
|---|
| 125 | { | 
|---|
| 126 | // Check HOME since it's what I use. | 
|---|
| 127 | if ((userHome = ::getenv ("HOME")) == NULL ) | 
|---|
| 128 | { | 
|---|
| 129 | // Not found - use the windows directory like JDK1.3.1 does. | 
|---|
| 130 | char *winHome = (char *) _Jv_MallocUnchecked (MAX_PATH); | 
|---|
| 131 | if (winHome != NULL) | 
|---|
| 132 | { | 
|---|
| 133 | if (GetWindowsDirectory (winHome, MAX_PATH)) | 
|---|
| 134 | SET ("user.home", winHome); | 
|---|
| 135 | _Jv_Free (winHome); | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 | } | 
|---|
| 139 | if (userHome != NULL) | 
|---|
| 140 | SET ("user.home", userHome); | 
|---|
| 141 |  | 
|---|
| 142 | // Get and set some OS info. | 
|---|
| 143 | OSVERSIONINFO osvi; | 
|---|
| 144 | ZeroMemory (&osvi, sizeof(OSVERSIONINFO)); | 
|---|
| 145 | osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | 
|---|
| 146 | if (GetVersionEx (&osvi)) | 
|---|
| 147 | { | 
|---|
| 148 | char *buffer = (char *) _Jv_MallocUnchecked (30); | 
|---|
| 149 | if (buffer != NULL) | 
|---|
| 150 | { | 
|---|
| 151 | sprintf (buffer, "%d.%d", (int) osvi.dwMajorVersion, | 
|---|
| 152 | (int) osvi.dwMinorVersion); | 
|---|
| 153 | SET ("os.version", buffer); | 
|---|
| 154 | _Jv_Free (buffer); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | switch (osvi.dwPlatformId) | 
|---|
| 158 | { | 
|---|
| 159 | case VER_PLATFORM_WIN32_WINDOWS: | 
|---|
| 160 | if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) | 
|---|
| 161 | SET ("os.name", "Windows 95"); | 
|---|
| 162 | else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) | 
|---|
| 163 | SET ("os.name", "Windows 98"); | 
|---|
| 164 | else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) | 
|---|
| 165 | SET ("os.name", "Windows Me"); | 
|---|
| 166 | else | 
|---|
| 167 | SET ("os.name", "Windows ??"); | 
|---|
| 168 | break; | 
|---|
| 169 |  | 
|---|
| 170 | case VER_PLATFORM_WIN32_NT: | 
|---|
| 171 | if (osvi.dwMajorVersion <= 4 ) | 
|---|
| 172 | SET ("os.name", "Windows NT"); | 
|---|
| 173 | else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0) | 
|---|
| 174 | SET ("os.name", "Windows 2000"); | 
|---|
| 175 | else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) | 
|---|
| 176 | SET ("os.name", "Windows XP"); | 
|---|
| 177 | else | 
|---|
| 178 | SET ("os.name", "Windows NT ??"); | 
|---|
| 179 | break; | 
|---|
| 180 |  | 
|---|
| 181 | default: | 
|---|
| 182 | SET ("os.name", "Windows UNKNOWN"); | 
|---|
| 183 | break; | 
|---|
| 184 | } | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | // Set the OS architecture. | 
|---|
| 188 | SYSTEM_INFO si; | 
|---|
| 189 | GetSystemInfo (&si); | 
|---|
| 190 | switch (si.wProcessorArchitecture) | 
|---|
| 191 | { | 
|---|
| 192 | case PROCESSOR_ARCHITECTURE_INTEL: | 
|---|
| 193 | SET ("os.arch", "x86"); | 
|---|
| 194 | break; | 
|---|
| 195 | case PROCESSOR_ARCHITECTURE_MIPS: | 
|---|
| 196 | SET ("os.arch", "mips"); | 
|---|
| 197 | break; | 
|---|
| 198 | case PROCESSOR_ARCHITECTURE_ALPHA: | 
|---|
| 199 | SET ("os.arch", "alpha"); | 
|---|
| 200 | break; | 
|---|
| 201 | case PROCESSOR_ARCHITECTURE_PPC: | 
|---|
| 202 | SET ("os.arch", "ppc"); | 
|---|
| 203 | break; | 
|---|
| 204 | case PROCESSOR_ARCHITECTURE_IA64: | 
|---|
| 205 | SET ("os.arch", "ia64"); | 
|---|
| 206 | break; | 
|---|
| 207 | case PROCESSOR_ARCHITECTURE_UNKNOWN: | 
|---|
| 208 | default: | 
|---|
| 209 | SET ("os.arch", "unknown"); | 
|---|
| 210 | break; | 
|---|
| 211 | } | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | /* Store up to SIZE return address of the current program state in | 
|---|
| 215 | ARRAY and return the exact number of values stored.  */ | 
|---|
| 216 | int | 
|---|
| 217 | backtrace (void **__array, int __size) | 
|---|
| 218 | { | 
|---|
| 219 | register void *_ebp __asm__ ("ebp"); | 
|---|
| 220 | register void *_esp __asm__ ("esp"); | 
|---|
| 221 | unsigned int *rfp; | 
|---|
| 222 |  | 
|---|
| 223 | int i=0; | 
|---|
| 224 | for (rfp = *(unsigned int**)_ebp; | 
|---|
| 225 | rfp && i < __size; | 
|---|
| 226 | rfp = *(unsigned int **)rfp) | 
|---|
| 227 | { | 
|---|
| 228 | int diff = *rfp - (unsigned int)rfp; | 
|---|
| 229 | if ((void*)rfp < _esp || diff > 4 * 1024 || diff < 0) break; | 
|---|
| 230 |  | 
|---|
| 231 | __array[i++] = (void*)(rfp[1]-4); | 
|---|
| 232 | } | 
|---|
| 233 | return i; | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | /* Placeholder implementation */ | 
|---|
| 237 | int | 
|---|
| 238 | _Jv_select (int, fd_set *, fd_set *, fd_set *, struct timeval *) | 
|---|
| 239 | { | 
|---|
| 240 | throw new java::lang::UnsupportedOperationException(JvNewStringUTF("_Jv_select() not implemented in Win32")); | 
|---|
| 241 | return 0; | 
|---|
| 242 | } | 
|---|