1 // natRuntime.cc - Implementation of native side of Runtime class.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 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
18 #include <java-props.h>
19 #include <java-stack.h>
20 #include <java/lang/Long.h>
21 #include <java/lang/Runtime.h>
22 #include <java/lang/UnknownError.h>
23 #include <java/lang/UnsatisfiedLinkError.h>
24 #include <gnu/gcj/runtime/FinalizerThread.h>
25 #include <java/io/File.h>
26 #include <java/util/TimeZone.h>
27 #include <java/lang/StringBuffer.h>
28 #include <java/lang/Process.h>
29 #include <java/lang/ConcreteProcess.h>
30 #include <java/lang/ClassLoader.h>
43 #ifdef HAVE_LANGINFO_H
52 /* FIXME: we don't always need this. The next libtool will let us use
53 AC_LTDL_PREOPEN to see if we do. */
54 extern const lt_dlsymlist lt_preloaded_symbols
[1] = { { 0, 0 } };
63 find_symbol (lt_dlhandle handle
, lt_ptr data
)
65 lookup_data
*ld
= (lookup_data
*) data
;
66 ld
->result
= lt_dlsym (handle
, ld
->symname
);
67 return ld
->result
!= NULL
;
71 _Jv_FindSymbolInExecutable (const char *symname
)
74 data
.symname
= symname
;
76 lt_dlforeach (find_symbol
, (lt_ptr
) &data
);
83 _Jv_FindSymbolInExecutable (const char *)
93 java::lang::Runtime::runFinalizationForExit ()
96 _Jv_RunAllFinalizers ();
100 java::lang::Runtime::exitInternal (jint status
)
102 // Make status right for Unix. This is perhaps strange.
103 if (status
< 0 || status
> 255)
110 java::lang::Runtime::freeMemory (void)
112 return _Jv_GCFreeMemory ();
116 java::lang::Runtime::gc (void)
122 // List of names for JNI_OnLoad.
123 static const char *onload_names
[] = _Jv_platform_onload_names
;
127 java::lang::Runtime::_load (jstring path
, jboolean do_search
)
129 JvSynchronize
sync (this);
130 using namespace java::lang
;
132 jint len
= _Jv_GetStringUTFLength (path
);
133 char buf
[len
+ 1 + strlen (_Jv_platform_solib_prefix
)
134 + strlen (_Jv_platform_solib_suffix
)];
138 strcpy (buf
, _Jv_platform_solib_prefix
);
139 offset
= strlen (_Jv_platform_solib_prefix
);
141 jsize total
= JvGetStringUTFRegion (path
, 0, path
->length(), &buf
[offset
]);
142 buf
[offset
+ total
] = '\0';
144 char *lib_name
= buf
;
148 ClassLoader
*look
= _Jv_StackTrace::GetFirstNonSystemClassLoader ();
152 // Don't include solib prefix in string passed to
154 jstring name
= look
->findLibrary(JvNewStringUTF(&buf
[offset
]));
157 len
= _Jv_GetStringUTFLength (name
);
158 lib_name
= (char *) _Jv_AllocBytes(len
+ 1);
159 total
= JvGetStringUTFRegion (name
, 0,
160 name
->length(), lib_name
);
161 lib_name
[total
] = '\0';
162 // Don't append suffixes any more; we have the full file
170 // FIXME: make sure path is absolute.
172 // Synchronize on java.lang.Class. This is to protect the class chain from
173 // concurrent modification by class registration calls which may be run
174 // during the dlopen().
175 JvSynchronize
sync (&java::lang::Class::class$
);
176 h
= do_search
? lt_dlopenext (lib_name
) : lt_dlopen (lib_name
);
180 const char *msg
= lt_dlerror ();
181 jstring str
= JvNewStringLatin1 (lib_name
);
182 str
= str
->concat (JvNewStringLatin1 (": "));
183 str
= str
->concat (JvNewStringLatin1 (msg
));
184 throw new UnsatisfiedLinkError (str
);
187 // Search for JNI_OnLoad function.
189 const char **name
= onload_names
;
190 while (*name
!= NULL
)
192 onload
= lt_dlsym (h
, *name
);
200 JavaVM
*vm
= _Jv_GetJavaVM ();
206 jint vers
= ((jint (JNICALL
*) (JavaVM
*, void *)) onload
) (vm
, NULL
);
207 if (vers
!= JNI_VERSION_1_1
&& vers
!= JNI_VERSION_1_2
208 && vers
!= JNI_VERSION_1_4
)
210 // FIXME: unload the library.
211 throw new UnsatisfiedLinkError (JvNewStringLatin1 ("unrecognized version from JNI_OnLoad"));
215 throw new UnknownError
216 (JvNewStringLatin1 (do_search
217 ? "Runtime.loadLibrary not implemented"
218 : "Runtime.load not implemented"));
219 #endif /* USE_LTDL */
223 java::lang::Runtime::loadLibraryInternal (jstring lib
)
225 JvSynchronize
sync (this);
226 using namespace java::lang
;
228 jint len
= _Jv_GetStringUTFLength (lib
);
230 jsize total
= JvGetStringUTFRegion (lib
, 0, lib
->length(), buf
);
232 // FIXME: make sure path is absolute.
233 lt_dlhandle h
= lt_dlopenext (buf
);
237 #endif /* USE_LTDL */
241 java::lang::Runtime::init (void)
245 // Set module load path.
246 lt_dlsetsearchpath (_Jv_Module_Load_Path
);
247 // Make sure self is opened.
253 java::lang::Runtime::runFinalization (void)
255 gnu::gcj::runtime::FinalizerThread::finalizerReady ();
259 java::lang::Runtime::totalMemory (void)
261 return _Jv_GCTotalMemory ();
265 java::lang::Runtime::maxMemory (void)
267 // We don't have a maximum. FIXME: we might if we ask the GC for
269 return Long::MAX_VALUE
;
273 java::lang::Runtime::traceInstructions (jboolean
)
279 java::lang::Runtime::traceMethodCalls (jboolean
)
284 java::lang::Process
*
285 java::lang::Runtime::execInternal (jstringArray cmd
,
289 return new java::lang::ConcreteProcess (cmd
, env
, dir
);
293 java::lang::Runtime::availableProcessors (void)
295 // FIXME: find the real value.
300 java::lang::Runtime::nativeGetLibname (jstring pathname
, jstring libname
)
302 java::lang::StringBuffer
*sb
= new java::lang::StringBuffer ();
303 sb
->append(pathname
);
304 if (pathname
->length() > 0)
305 sb
->append (_Jv_platform_file_separator
);
307 sb
->append (JvNewStringLatin1 (_Jv_platform_solib_prefix
));
309 sb
->append (JvNewStringLatin1 (_Jv_platform_solib_suffix
));
311 return sb
->toString();