summaryrefslogtreecommitdiff
path: root/libjava/win32.cc
blob: af1163fbfa9ec42acc2ba28b6817309e1bae6b86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// win32.cc - Helper functions for Microsoft-flavored OSs.

/* Copyright (C) 2002  Free Software Foundation

   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>
#include <jvm.h>
#include <sys/timeb.h>
#include <stdlib.h>

#include "platform.h"
#include <java/lang/ArithmeticException.h>
#include <java/util/Properties.h>

static LONG CALLBACK
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;
}

// 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);
}

// gettimeofday implementation.
jlong
_Jv_platform_gettimeofday ()
{
  struct timeb t;
  ftime (&t);
  return t.time * 1000LL + t.millitm;
}

// 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;
}

// 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;
  char *buffer = (char *) _Jv_MallocUnchecked (buflen);
  if (buffer != NULL)
    {
      if (GetCurrentDirectory (buflen, buffer))
	SET ("user.dir", buffer);

      if (GetTempPath (buflen, buffer))
	SET ("java.io.tmpdir", buffer);

      _Jv_Free (buffer);
    }
  
  // Use GetUserName to set 'user.name'.
  buflen = 257;  // UNLEN + 1
  buffer = (char *) _Jv_MallocUnchecked (buflen);
  if (buffer != NULL)
    {
      if (GetUserName (buffer, &buflen))
        SET ("user.name", buffer);
      _Jv_Free (buffer);
    }

  // 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.
  char *userHome = NULL;
  if ((userHome = ::getenv ("HOMEPATH")) == NULL )
    {
      // Check HOME since it's what I use.
      if ((userHome = ::getenv ("HOME")) == NULL )
        {
          // Not found - use the windows directory like JDK1.3.1 does.
          char *winHome = (char *) _Jv_MallocUnchecked (MAX_PATH);
          if (winHome != NULL)
            {
              if (GetWindowsDirectory (winHome, MAX_PATH))
		SET ("user.home", winHome);
              _Jv_Free (winHome);
            }
        }
     }
  if (userHome != NULL)
    SET ("user.home", userHome);

  // Get and set some OS info.
  OSVERSIONINFO osvi;
  ZeroMemory (&osvi, sizeof(OSVERSIONINFO));
  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  if (GetVersionEx (&osvi))
    {
      char *buffer = (char *) _Jv_MallocUnchecked (30);
      if (buffer != NULL)
        {
          sprintf (buffer, "%d.%d", (int) osvi.dwMajorVersion,
		   (int) osvi.dwMinorVersion);
          SET ("os.version", buffer);
          _Jv_Free (buffer);
        }

      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);
  switch (si.dwProcessorType)
    {
      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;
    }
}

/* 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;
}