1 // win32.cc - Helper functions for Microsoft-flavored OSs.
3 /* Copyright (C) 2002, 2003 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
14 #include <sys/timeb.h>
17 #include <java/lang/ArithmeticException.h>
18 #include <java/util/Properties.h>
21 win32_exception_handler (LPEXCEPTION_POINTERS e
)
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
;
28 return EXCEPTION_CONTINUE_SEARCH
;
31 // Platform-specific executable name
32 static char exec_name
[MAX_PATH
];
33 // initialized in _Jv_platform_initialize()
35 const char *_Jv_ThisExecutable (void)
40 // Platform-specific VM initialization.
42 _Jv_platform_initialize (void)
44 // Initialise winsock for networking
46 if (WSAStartup (MAKEWORD (1, 1), &data
))
47 MessageBox (NULL
, "Error initialising winsock library.", "Error",
48 MB_OK
| MB_ICONEXCLAMATION
);
50 // Install exception handler
51 SetUnhandledExceptionFilter (win32_exception_handler
);
53 // Initialize our executable name
54 GetModuleFileName(NULL
, exec_name
, sizeof(exec_name
));
57 // gettimeofday implementation.
59 _Jv_platform_gettimeofday ()
63 return t
.time
* 1000LL + t
.millitm
;
66 // The following definitions "fake out" mingw to think that -mthreads
67 // was enabled and that mingwthr.dll was linked. GCJ-compiled
68 // applications don't need this helper library because we can safely
69 // detect thread death (return from Thread.run()).
74 __mingwthr_key_dtor (DWORD
, void (*) (void *))
76 // FIXME: for now we do nothing; this causes a memory leak of
77 // approximately 24 bytes per thread created.
81 // Set platform-specific System properties.
83 _Jv_platform_initProperties (java::util::Properties
* newprops
)
85 // A convenience define.
86 #define SET(Prop,Val) \
87 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
89 SET ("file.separator", "\\");
90 SET ("path.separator", ";");
91 SET ("line.separator", "\r\n");
93 // Use GetCurrentDirectory to set 'user.dir'.
94 DWORD buflen
= MAX_PATH
;
95 char *buffer
= (char *) _Jv_MallocUnchecked (buflen
);
98 if (GetCurrentDirectory (buflen
, buffer
))
99 SET ("user.dir", buffer
);
101 if (GetTempPath (buflen
, buffer
))
102 SET ("java.io.tmpdir", buffer
);
107 // Use GetUserName to set 'user.name'.
108 buflen
= 257; // UNLEN + 1
109 buffer
= (char *) _Jv_MallocUnchecked (buflen
);
112 if (GetUserName (buffer
, &buflen
))
113 SET ("user.name", buffer
);
117 // According to the api documentation for 'GetWindowsDirectory()', the
118 // environmental variable HOMEPATH always specifies the user's home
119 // directory or a default directory. On the 3 windows machines I checked
120 // only 1 had it set. If it's not set, JDK1.3.1 seems to set it to
121 // the windows directory, so we'll do the same.
122 char *userHome
= NULL
;
123 if ((userHome
= ::getenv ("HOMEPATH")) == NULL
)
125 // Check HOME since it's what I use.
126 if ((userHome
= ::getenv ("HOME")) == NULL
)
128 // Not found - use the windows directory like JDK1.3.1 does.
129 char *winHome
= (char *) _Jv_MallocUnchecked (MAX_PATH
);
132 if (GetWindowsDirectory (winHome
, MAX_PATH
))
133 SET ("user.home", winHome
);
138 if (userHome
!= NULL
)
139 SET ("user.home", userHome
);
141 // Get and set some OS info.
143 ZeroMemory (&osvi
, sizeof(OSVERSIONINFO
));
144 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
145 if (GetVersionEx (&osvi
))
147 char *buffer
= (char *) _Jv_MallocUnchecked (30);
150 sprintf (buffer
, "%d.%d", (int) osvi
.dwMajorVersion
,
151 (int) osvi
.dwMinorVersion
);
152 SET ("os.version", buffer
);
156 switch (osvi
.dwPlatformId
)
158 case VER_PLATFORM_WIN32_WINDOWS
:
159 if (osvi
.dwMajorVersion
== 4 && osvi
.dwMinorVersion
== 0)
160 SET ("os.name", "Windows 95");
161 else if (osvi
.dwMajorVersion
== 4 && osvi
.dwMinorVersion
== 10)
162 SET ("os.name", "Windows 98");
163 else if (osvi
.dwMajorVersion
== 4 && osvi
.dwMinorVersion
== 90)
164 SET ("os.name", "Windows Me");
166 SET ("os.name", "Windows ??");
169 case VER_PLATFORM_WIN32_NT
:
170 if (osvi
.dwMajorVersion
<= 4 )
171 SET ("os.name", "Windows NT");
172 else if (osvi
.dwMajorVersion
== 5 && osvi
.dwMinorVersion
== 0)
173 SET ("os.name", "Windows 2000");
174 else if (osvi
.dwMajorVersion
== 5 && osvi
.dwMinorVersion
== 1)
175 SET ("os.name", "Windows XP");
177 SET ("os.name", "Windows NT ??");
181 SET ("os.name", "Windows UNKNOWN");
186 // Set the OS architecture.
189 switch (si
.dwProcessorType
)
191 case PROCESSOR_INTEL_386
:
192 SET ("os.arch", "i386");
194 case PROCESSOR_INTEL_486
:
195 SET ("os.arch", "i486");
197 case PROCESSOR_INTEL_PENTIUM
:
198 SET ("os.arch", "i586");
200 case PROCESSOR_MIPS_R4000
:
201 SET ("os.arch", "MIPS4000");
203 case PROCESSOR_ALPHA_21064
:
204 SET ("os.arch", "ALPHA");
207 SET ("os.arch", "unknown");
212 /* Store up to SIZE return address of the current program state in
213 ARRAY and return the exact number of values stored. */
215 backtrace (void **__array
, int __size
)
217 register void *_ebp
__asm__ ("ebp");
218 register void *_esp
__asm__ ("esp");
222 for (rfp
= *(unsigned int**)_ebp
;
224 rfp
= *(unsigned int **)rfp
)
226 int diff
= *rfp
- (unsigned int)rfp
;
227 if ((void*)rfp
< _esp
|| diff
> 4 * 1024 || diff
< 0) break;
229 __array
[i
++] = (void*)(rfp
[1]-4);