2002-02-07 06:26:42 +01:00
|
|
|
// win32.cc - Helper functions for Microsoft-flavored OSs.
|
|
|
|
|
2003-02-19 17:28:37 +01:00
|
|
|
/* Copyright (C) 2002, 2003 Free Software Foundation
|
2002-02-07 06:26:42 +01:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <config.h>
|
2003-02-19 17:28:37 +01:00
|
|
|
#include <platform.h>
|
2002-02-12 05:35:32 +01:00
|
|
|
#include <jvm.h>
|
2002-03-08 02:03:56 +01:00
|
|
|
#include <sys/timeb.h>
|
2002-04-07 13:27:00 +02:00
|
|
|
#include <stdlib.h>
|
2002-02-07 06:26:42 +01:00
|
|
|
|
2002-02-12 05:35:32 +01:00
|
|
|
#include <java/lang/ArithmeticException.h>
|
2002-04-07 13:27:00 +02:00
|
|
|
#include <java/util/Properties.h>
|
2002-02-07 19:59:52 +01:00
|
|
|
|
|
|
|
static LONG CALLBACK
|
2002-02-07 06:26:42 +01:00
|
|
|
win32_exception_handler (LPEXCEPTION_POINTERS e)
|
|
|
|
{
|
|
|
|
if (e->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
|
|
|
|
_Jv_ThrowNullPointerException();
|
|
|
|
else if (e->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO)
|
|
|
|
throw new java::lang::ArithmeticException;
|
|
|
|
else
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
2002-02-07 19:59:52 +01:00
|
|
|
|
|
|
|
// Platform-specific VM initialization.
|
|
|
|
void
|
|
|
|
_Jv_platform_initialize (void)
|
|
|
|
{
|
|
|
|
// Initialise winsock for networking
|
|
|
|
WSADATA data;
|
|
|
|
if (WSAStartup (MAKEWORD (1, 1), &data))
|
|
|
|
MessageBox (NULL, "Error initialising winsock library.", "Error",
|
|
|
|
MB_OK | MB_ICONEXCLAMATION);
|
|
|
|
// Install exception handler
|
|
|
|
SetUnhandledExceptionFilter (win32_exception_handler);
|
|
|
|
}
|
2002-02-12 03:25:07 +01:00
|
|
|
|
|
|
|
// gettimeofday implementation.
|
2002-03-08 02:03:56 +01:00
|
|
|
jlong
|
|
|
|
_Jv_platform_gettimeofday ()
|
2002-02-12 03:25:07 +01:00
|
|
|
{
|
2002-03-08 02:03:56 +01:00
|
|
|
struct timeb t;
|
|
|
|
ftime (&t);
|
2002-03-09 23:08:35 +01:00
|
|
|
return t.time * 1000LL + t.millitm;
|
2002-02-12 03:25:07 +01:00
|
|
|
}
|
|
|
|
|
2002-03-09 23:33:49 +01:00
|
|
|
// The following definitions "fake out" mingw to think that -mthreads
|
|
|
|
// was enabled and that mingwthr.dll was linked. GCJ-compiled
|
|
|
|
// applications don't need this helper library because we can safely
|
|
|
|
// detect thread death (return from Thread.run()).
|
|
|
|
|
|
|
|
int _CRT_MT = 1;
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
__mingwthr_key_dtor (DWORD, void (*) (void *))
|
|
|
|
{
|
|
|
|
// FIXME: for now we do nothing; this causes a memory leak of
|
|
|
|
// approximately 24 bytes per thread created.
|
|
|
|
return 0;
|
|
|
|
}
|
2002-04-07 13:27:00 +02:00
|
|
|
|
|
|
|
// Set platform-specific System properties.
|
|
|
|
void
|
|
|
|
_Jv_platform_initProperties (java::util::Properties* newprops)
|
|
|
|
{
|
|
|
|
// A convenience define.
|
|
|
|
#define SET(Prop,Val) \
|
|
|
|
newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
|
|
|
|
|
|
|
|
SET ("file.separator", "\\");
|
|
|
|
SET ("path.separator", ";");
|
|
|
|
SET ("line.separator", "\r\n");
|
|
|
|
|
|
|
|
// Use GetCurrentDirectory to set 'user.dir'.
|
|
|
|
DWORD buflen = MAX_PATH;
|
2002-04-11 17:57:56 +02:00
|
|
|
char *buffer = (char *) _Jv_MallocUnchecked (buflen);
|
2002-04-07 13:27:00 +02:00
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
|
|
|
if (GetCurrentDirectory (buflen, buffer))
|
2002-04-11 17:57:56 +02:00
|
|
|
SET ("user.dir", buffer);
|
|
|
|
|
|
|
|
if (GetTempPath (buflen, buffer))
|
|
|
|
SET ("java.io.tmpdir", buffer);
|
|
|
|
|
2002-04-12 00:23:43 +02:00
|
|
|
_Jv_Free (buffer);
|
2002-04-07 13:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use GetUserName to set 'user.name'.
|
|
|
|
buflen = 257; // UNLEN + 1
|
2002-04-11 17:57:56 +02:00
|
|
|
buffer = (char *) _Jv_MallocUnchecked (buflen);
|
2002-04-07 13:27:00 +02:00
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
|
|
|
if (GetUserName (buffer, &buflen))
|
|
|
|
SET ("user.name", buffer);
|
2002-04-12 00:23:43 +02:00
|
|
|
_Jv_Free (buffer);
|
2002-04-07 13:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// According to the api documentation for 'GetWindowsDirectory()', the
|
|
|
|
// environmental variable HOMEPATH always specifies the user's home
|
|
|
|
// directory or a default directory. On the 3 windows machines I checked
|
|
|
|
// only 1 had it set. If it's not set, JDK1.3.1 seems to set it to
|
|
|
|
// the windows directory, so we'll do the same.
|
2002-04-11 17:57:56 +02:00
|
|
|
char *userHome = NULL;
|
|
|
|
if ((userHome = ::getenv ("HOMEPATH")) == NULL )
|
2002-04-07 13:27:00 +02:00
|
|
|
{
|
|
|
|
// Check HOME since it's what I use.
|
2002-04-11 17:57:56 +02:00
|
|
|
if ((userHome = ::getenv ("HOME")) == NULL )
|
2002-04-07 13:27:00 +02:00
|
|
|
{
|
|
|
|
// Not found - use the windows directory like JDK1.3.1 does.
|
2002-04-11 17:57:56 +02:00
|
|
|
char *winHome = (char *) _Jv_MallocUnchecked (MAX_PATH);
|
|
|
|
if (winHome != NULL)
|
2002-04-07 13:27:00 +02:00
|
|
|
{
|
|
|
|
if (GetWindowsDirectory (winHome, MAX_PATH))
|
2002-04-11 17:57:56 +02:00
|
|
|
SET ("user.home", winHome);
|
2002-04-12 00:23:43 +02:00
|
|
|
_Jv_Free (winHome);
|
2002-04-07 13:27:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-04-11 17:57:56 +02:00
|
|
|
if (userHome != NULL)
|
2002-04-07 13:27:00 +02:00
|
|
|
SET ("user.home", userHome);
|
|
|
|
|
|
|
|
// Get and set some OS info.
|
|
|
|
OSVERSIONINFO osvi;
|
|
|
|
ZeroMemory (&osvi, sizeof(OSVERSIONINFO));
|
|
|
|
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
|
|
|
if (GetVersionEx (&osvi))
|
|
|
|
{
|
2002-04-11 17:57:56 +02:00
|
|
|
char *buffer = (char *) _Jv_MallocUnchecked (30);
|
2002-04-07 13:27:00 +02:00
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
2002-04-11 17:57:56 +02:00
|
|
|
sprintf (buffer, "%d.%d", (int) osvi.dwMajorVersion,
|
|
|
|
(int) osvi.dwMinorVersion);
|
2002-04-07 13:27:00 +02:00
|
|
|
SET ("os.version", buffer);
|
2002-04-12 00:23:43 +02:00
|
|
|
_Jv_Free (buffer);
|
2002-04-07 13:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (osvi.dwPlatformId)
|
|
|
|
{
|
|
|
|
case VER_PLATFORM_WIN32_WINDOWS:
|
|
|
|
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
|
|
|
|
SET ("os.name", "Windows 95");
|
|
|
|
else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
|
|
|
|
SET ("os.name", "Windows 98");
|
|
|
|
else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
|
|
|
|
SET ("os.name", "Windows Me");
|
|
|
|
else
|
|
|
|
SET ("os.name", "Windows ??");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VER_PLATFORM_WIN32_NT:
|
|
|
|
if (osvi.dwMajorVersion <= 4 )
|
|
|
|
SET ("os.name", "Windows NT");
|
|
|
|
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
|
|
|
|
SET ("os.name", "Windows 2000");
|
|
|
|
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
|
|
|
|
SET ("os.name", "Windows XP");
|
|
|
|
else
|
|
|
|
SET ("os.name", "Windows NT ??");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
SET ("os.name", "Windows UNKNOWN");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the OS architecture.
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
GetSystemInfo (&si);
|
2002-04-11 17:57:56 +02:00
|
|
|
switch (si.dwProcessorType)
|
2002-04-07 13:27:00 +02:00
|
|
|
{
|
|
|
|
case PROCESSOR_INTEL_386:
|
|
|
|
SET ("os.arch", "i386");
|
|
|
|
break;
|
|
|
|
case PROCESSOR_INTEL_486:
|
|
|
|
SET ("os.arch", "i486");
|
|
|
|
break;
|
|
|
|
case PROCESSOR_INTEL_PENTIUM:
|
|
|
|
SET ("os.arch", "i586");
|
|
|
|
break;
|
|
|
|
case PROCESSOR_MIPS_R4000:
|
|
|
|
SET ("os.arch", "MIPS4000");
|
|
|
|
break;
|
|
|
|
case PROCESSOR_ALPHA_21064:
|
|
|
|
SET ("os.arch", "ALPHA");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SET ("os.arch", "unknown");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-04-24 03:33:19 +02:00
|
|
|
|
|
|
|
/* Store up to SIZE return address of the current program state in
|
|
|
|
ARRAY and return the exact number of values stored. */
|
|
|
|
int
|
|
|
|
backtrace (void **__array, int __size)
|
|
|
|
{
|
|
|
|
register void *_ebp __asm__ ("ebp");
|
|
|
|
register void *_esp __asm__ ("esp");
|
|
|
|
unsigned int *rfp;
|
|
|
|
|
|
|
|
int i=0;
|
|
|
|
for (rfp = *(unsigned int**)_ebp;
|
|
|
|
rfp && i < __size;
|
|
|
|
rfp = *(unsigned int **)rfp)
|
|
|
|
{
|
|
|
|
int diff = *rfp - (unsigned int)rfp;
|
|
|
|
if ((void*)rfp < _esp || diff > 4 * 1024 || diff < 0) break;
|
|
|
|
|
|
|
|
__array[i++] = (void*)(rfp[1]-4);
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|