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