/* src/native/vm/openjdk/jvm.cpp - HotSpot VM interface functions
- Copyright (C) 2007, 2008
+ Copyright (C) 2007, 2008, 2009
CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
+ Copyright (C) 2009 Theobroma Systems Ltd.
This file is part of CACAO.
#include <sys/stat.h>
#include <sys/types.h>
-#include "vm/types.h"
+// Include our JNI header before the JVM headers, because the JVM
+// headers include jni.h and we want to override the typedefs in
+// jni.h.
+#include "native/jni.hpp"
-#include "mm/memory.h"
+// We include jvm_md.h before jvm.h as the latter includes the former.
+#include INCLUDE_JVM_MD_H
+#include INCLUDE_JVM_H
+
+#include "fdlibm/fdlibm.h"
+
+#include "mm/memory.hpp"
-#include "native/jni.hpp"
#include "native/llni.h"
-#include "native/native.h"
+#include "native/native.hpp"
#include "native/vm/reflection.hpp"
-#include "native/vm/openjdk/hpi.h"
+#include "native/vm/openjdk/hpi.hpp"
+#include "native/vm/openjdk/management.hpp"
-#include "threads/lock-common.h"
+#include "threads/lock.hpp"
#include "threads/thread.hpp"
+#include "threads/threadlist.hpp"
-#include "toolbox/logging.h"
+#include "toolbox/logging.hpp"
#include "toolbox/list.hpp"
-#include "vm/array.h"
+#include "vm/array.hpp"
#if defined(ENABLE_ASSERTION)
-#include "vm/assertion.h"
+#include "vm/assertion.hpp"
#endif
#include "vm/jit/builtin.hpp"
-#include "vm/classcache.h"
+#include "vm/classcache.hpp"
#include "vm/exceptions.hpp"
#include "vm/global.h"
#include "vm/globals.hpp"
-#include "vm/initialize.h"
+#include "vm/initialize.hpp"
#include "vm/javaobjects.hpp"
#include "vm/options.h"
#include "vm/os.hpp"
#include "vm/package.hpp"
#include "vm/primitive.hpp"
#include "vm/properties.hpp"
-#include "vm/resolve.h"
-#include "vm/signallocal.h"
+#include "vm/resolve.hpp"
+#include "vm/signallocal.hpp"
#include "vm/string.hpp"
#include "vm/vm.hpp"
} \
} while (0)
-# define PRINTJVMWARNINGS(x)
-/* do { \ */
-/* if (opt_PrintJVMWarnings) { \ */
-/* log_println x; \ */
-/* } \ */
-/* } while (0) */
+# define PRINTJVMWARNINGS(x) \
+ do { \
+ if (opt_PrintWarnings) { \
+ log_println x; \
+ } \
+ } while (0)
#else
#endif
-typedef struct {
- /* Naming convention of RE build version string: n.n.n[_uu[c]][-<identifier>]-bxx */
- unsigned int jvm_version; /* Consists of major, minor, micro (n.n.n) */
- /* and build number (xx) */
- unsigned int update_version : 8; /* Update release version (uu) */
- unsigned int special_update_version : 8; /* Special update release version (c) */
- unsigned int reserved1 : 16;
- unsigned int reserved2;
-
- /* The following bits represents JVM supports that JDK has dependency on.
- * JDK can use these bits to determine which JVM version
- * and support it has to maintain runtime compatibility.
- *
- * When a new bit is added in a minor or update release, make sure
- * the new bit is also added in the main/baseline.
- */
- unsigned int is_attachable : 1;
- unsigned int : 31;
- unsigned int : 32;
- unsigned int : 32;
-} jvm_version_info;
-
-
-/*
- * A structure used to a capture exception table entry in a Java method.
- */
-typedef struct {
- jint start_pc;
- jint end_pc;
- jint handler_pc;
- jint catchType;
-} JVM_ExceptionTableEntryType;
-
-
// Interface functions are exported as C functions.
extern "C" {
return 0;
}
- java_handle_bytearray_t* ba = jlt.get_backtrace();
+ ByteArray ba(jlt.get_backtrace());
- if (ba == NULL)
+ if (ba.is_null())
return 0;
// We need a critical section here as the stacktrace structure is
LLNI_CRITICAL_START;
- stacktrace_t* st = (stacktrace_t *) LLNI_array_data(ba);
+ stacktrace_t* st = (stacktrace_t *) ba.get_raw_data_ptr();
int32_t depth = st->length;
TRACEJVMCALLS(("JVM_GetStackTraceElement(env=%p, throwable=%p, index=%d)", env, throwable, index));
java_lang_Throwable jlt(throwable);
- java_handle_bytearray_t* ba = jlt.get_backtrace();
+ ByteArray ba(jlt.get_backtrace());
- // We need a critical section here as the stacktrace structure is
+ // XXX We need a critical section here as the stacktrace structure is
// mapped onto a Java byte-array.
- LLNI_CRITICAL_START;
-
- stacktrace_t* st = (stacktrace_t *) LLNI_array_data(ba);
-
- if ((index < 0) || (index >= st->length)) {
- /* XXX This should be an IndexOutOfBoundsException (check this
- again). */
- exceptions_throw_arrayindexoutofboundsexception();
- return NULL;
- }
-
- // Get the stacktrace entry.
- stacktrace_entry_t* ste = &(st->entries[index]);
-
- // Get the codeinfo, methodinfo and classinfo.
- codeinfo* code = ste->code;
- methodinfo* m = code->m;
- classinfo* c = m->clazz;
-
- // Get filename.
- java_handle_t* filename;
-
- if (!(m->flags & ACC_NATIVE)) {
- if (c->sourcefile != NULL)
- filename = javastring_new(c->sourcefile);
- else
- filename = NULL;
- }
- else
- filename = NULL;
-
- // Get line number.
- int32_t linenumber;
+ stacktrace_t* st = (stacktrace_t *) ba.get_raw_data_ptr();
- if (m->flags & ACC_NATIVE) {
- linenumber = -2;
- }
- else {
- /* FIXME The linenumbertable_linenumber_for_pc could change
- the methodinfo pointer when hitting an inlined method. */
-
- linenumber = linenumbertable_linenumber_for_pc(&m, code, ste->pc);
- linenumber = (linenumber == 0) ? -1 : linenumber;
- }
-
- LLNI_CRITICAL_END;
-
- // Get declaring class name.
- java_handle_t* declaringclass = class_get_classname(c);
-
- // Allocate a new StackTraceElement object.
- java_lang_StackTraceElement jlste(declaringclass, javastring_new(m->name), filename, linenumber);
-
- if (jlste.is_null())
- return NULL;
-
- return (jobject) jlste.get_handle();
+ return stacktrace_get_StackTraceElement(st, index);
}
/* JVM_GetLastErrorString */
-jint JVM_GetLastErrorString(char *buf, int len)
+jint JVM_GetLastErrorString(char* buf, int len)
{
TRACEJVMCALLS(("JVM_GetLastErrorString(buf=%p, len=%d", buf, len));
- return hpi_system->GetLastErrorString(buf, len);
+ HPI& hpi = VM::get_current()->get_hpi();
+ return hpi.get_system().GetLastErrorString(buf, len);
}
/* JVM_NativePath */
-char *JVM_NativePath(char *path)
+char *JVM_NativePath(char* path)
{
TRACEJVMCALLS(("JVM_NativePath(path=%s)", path));
- return hpi_file->NativePath(path);
+ HPI& hpi = VM::get_current()->get_hpi();
+ return hpi.get_file().NativePath(path);
}
oa = class_get_interfaces(c);
- return (jobjectArray) oa;
+ return oa;
}
void JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers)
{
- classinfo *c;
- java_handle_objectarray_t *hoa;
-
TRACEJVMCALLS(("JVM_SetClassSigners(env=%p, cls=%p, signers=%p)", env, cls, signers));
- c = LLNI_classinfo_unwrap(cls);
+ classinfo* c = LLNI_classinfo_unwrap(cls);
- hoa = (java_handle_objectarray_t *) signers;
+ ObjectArray oa(signers);
/* This call is ignored for primitive types and arrays. Signers
are only set once, ClassLoader.java, and thus shouldn't be
if (class_is_primitive(c) || class_is_array(c))
return;
- LLNI_classinfo_field_set(c, signers, hoa);
+ // XXX: Fix this!
+ LLNI_classinfo_field_set(c, signers, (java_objectarray_t*) oa.get_handle());
}
oa = class_get_declaredclasses(c, false);
- return (jobjectArray) oa;
+ return oa;
}
oa = class_get_declaredfields(c, publicOnly);
- return (jobjectArray) oa;
+ return oa;
}
java_handle_objectarray_t* oa = class_get_declaredmethods(c, publicOnly);
- return (jobjectArray) oa;
+ return oa;
}
oa = class_get_declaredconstructors(c, publicOnly);
- return (jobjectArray) oa;
+ return oa;
}
jobject JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused)
{
- java_handle_objectarray_t *classes;
- java_handle_objectarray_t *packages;
- java_booleanarray_t *classEnabled;
- java_booleanarray_t *packageEnabled;
#if defined(ENABLE_ASSERTION)
- java_handle_t *js;
- s4 i, j;
+ java_handle_t* js;
+ s4 i, j;
#endif
TRACEJVMCALLS(("JVM_AssertionStatusDirectives(env=%p, unused=%p)", env, unused));
#if defined(ENABLE_ASSERTION)
- classes = builtin_anewarray(assertion_class_count, class_java_lang_Object);
+ ObjectArray classes(assertion_class_count, class_java_lang_Object);
#else
- classes = builtin_anewarray(0, class_java_lang_Object);
+ ObjectArray classes(0, class_java_lang_Object);
#endif
- if (classes == NULL)
+ if (classes.is_null())
return NULL;
#if defined(ENABLE_ASSERTION)
- packages = builtin_anewarray(assertion_package_count, class_java_lang_Object);
+ ObjectArray packages(assertion_package_count, class_java_lang_Object);
#else
- packages = builtin_anewarray(0, class_java_lang_Object);
+ ObjectArray packages(0, class_java_lang_Object);
#endif
- if (packages == NULL)
+ if (packages.is_null())
return NULL;
#if defined(ENABLE_ASSERTION)
- classEnabled = builtin_newarray_boolean(assertion_class_count);
+ BooleanArray classEnabled(assertion_class_count);
#else
- classEnabled = builtin_newarray_boolean(0);
+ BooleanArray classEnabled(0);
#endif
- if (classEnabled == NULL)
+ if (classEnabled.is_null())
return NULL;
#if defined(ENABLE_ASSERTION)
- packageEnabled = builtin_newarray_boolean(assertion_package_count);
+ BooleanArray packageEnabled(assertion_package_count);
#else
- packageEnabled = builtin_newarray_boolean(0);
+ BooleanArray packageEnabled(0);
#endif
- if (packageEnabled == NULL)
+ if (packageEnabled.is_null())
return NULL;
#if defined(ENABLE_ASSERTION)
}
if (item->package == false) {
- classes->data[i] = js;
- classEnabled->data[i] = (jboolean) item->enabled;
+ classes.set_element(i, js);
+ classEnabled.set_element(i, (jboolean) item->enabled);
i += 1;
}
else {
- packages->data[j] = js;
- packageEnabled->data[j] = (jboolean) item->enabled;
+ packages.set_element(j, js);
+ packageEnabled.set_element(j, (jboolean) item->enabled);
j += 1;
}
}
/* set instance fields */
- java_lang_AssertionStatusDirectives jlasd(classes, classEnabled, packages, packageEnabled);
+ java_lang_AssertionStatusDirectives jlasd(
+ classes.get_handle(),
+ classEnabled.get_handle(),
+ packages.get_handle(),
+ packageEnabled.get_handle());
return (jobject) jlasd.get_handle();
}
*/
#define JVM_EEXIST -100
-jint JVM_Open(const char *fname, jint flags, jint mode)
+jint JVM_Open(const char* fname, jint flags, jint mode)
{
int result;
TRACEJVMCALLS(("JVM_Open(fname=%s, flags=%d, mode=%d)", fname, flags, mode));
- result = hpi_file->Open(fname, flags, mode);
+ HPI& hpi = VM::get_current()->get_hpi();
+ result = hpi.get_file().Open(fname, flags, mode);
if (result >= 0) {
return result;
{
TRACEJVMCALLS(("JVM_Close(fd=%d)", fd));
- return hpi_file->Close(fd);
+ HPI& hpi = VM::get_current()->get_hpi();
+ return hpi.get_file().Close(fd);
}
/* JVM_Read */
-jint JVM_Read(jint fd, char *buf, jint nbytes)
+jint JVM_Read(jint fd, char* buf, jint nbytes)
{
TRACEJVMCALLS(("JVM_Read(fd=%d, buf=%p, nbytes=%d)", fd, buf, nbytes));
- return (jint) hpi_file->Read(fd, buf, nbytes);
+ HPI& hpi = VM::get_current()->get_hpi();
+ return (jint) hpi.get_file().Read(fd, buf, nbytes);
}
/* JVM_Write */
-jint JVM_Write(jint fd, char *buf, jint nbytes)
+jint JVM_Write(jint fd, char* buf, jint nbytes)
{
TRACEJVMCALLS(("JVM_Write(fd=%d, buf=%s, nbytes=%d)", fd, buf, nbytes));
- return (jint) hpi_file->Write(fd, buf, nbytes);
+ HPI& hpi = VM::get_current()->get_hpi();
+ return (jint) hpi.get_file().Write(fd, buf, nbytes);
}
/* JVM_Available */
-jint JVM_Available(jint fd, jlong *pbytes)
+jint JVM_Available(jint fd, jlong* pbytes)
{
TRACEJVMCALLS(("JVM_Available(fd=%d, pbytes=%p)", fd, pbytes));
- return hpi_file->Available(fd, pbytes);
+ HPI& hpi = VM::get_current()->get_hpi();
+ return hpi.get_file().Available(fd, pbytes);
}
{
TRACEJVMCALLS(("JVM_Lseek(fd=%d, offset=%ld, whence=%d)", fd, offset, whence));
- return hpi_file->Seek(fd, (off_t) offset, whence);
+ HPI& hpi = VM::get_current()->get_hpi();
+ return hpi.get_file().Seek(fd, (off_t) offset, whence);
}
{
TRACEJVMCALLS(("JVM_SetLength(fd=%d, length=%ld)", length));
- return hpi_file->SetLength(fd, length);
+ HPI& hpi = VM::get_current()->get_hpi();
+ return hpi.get_file().SetLength(fd, length);
}
{
TRACEJVMCALLS(("JVM_Sync(fd=%d)", fd));
- return hpi_file->Sync(fd);
+ HPI& hpi = VM::get_current()->get_hpi();
+ return hpi.get_file().Sync(fd);
}
h = (java_handle_t *) jthread;
t = thread_get_thread(h);
- /* The threadobject is null when a thread is created in Java. The
- priority is set later during startup. */
+ /* The threadobject is null when a thread is created in Java. */
if (t == NULL)
return 0;
void JVM_SuspendThread(JNIEnv* env, jobject jthread)
{
- log_println("JVM_SuspendThread: Deprecated. Not implemented.");
+ java_handle_t *h;
+ threadobject *t;
+
+ TRACEJVMCALLS(("JVM_SuspendThread(env=%p, jthread=%p)", env, jthread));
+
+ if (opt_PrintWarnings)
+ log_println("JVM_SuspendThread: Deprecated, do not use!");
+
+ h = (java_handle_t *) jthread;
+ t = thread_get_thread(h);
+
+ /* The threadobject is null when a thread is created in Java. */
+
+ if (t == NULL)
+ return;
+
+ threads_suspend_thread(t, SUSPEND_REASON_JAVA);
}
void JVM_ResumeThread(JNIEnv* env, jobject jthread)
{
- log_println("JVM_ResumeThread: Deprecated. Not implemented.");
+ java_handle_t *h;
+ threadobject *t;
+
+ TRACEJVMCALLS(("JVM_ResumeThread(env=%p, jthread=%p)", env, jthread));
+
+ if (opt_PrintWarnings)
+ log_println("JVM_ResumeThread: Deprecated, do not use!");
+
+ h = (java_handle_t *) jthread;
+ t = thread_get_thread(h);
+
+ /* The threadobject is null when a thread is created in Java. */
+
+ if (t == NULL)
+ return;
+
+ threads_resume_thread(t, SUSPEND_REASON_JAVA);
}
h = (java_handle_t *) jthread;
t = thread_get_thread(h);
+ /* The threadobject is null when a thread is created in Java. */
+
if (t == NULL)
return;
h = (java_handle_t *) jthread;
t = thread_get_thread(h);
+ /* The threadobject is null when a thread is created in Java. */
+
+ if (t == NULL)
+ return JNI_FALSE;
+
interrupted = thread_is_interrupted(t);
if (interrupted && clear_interrupted)
jobject JVM_CurrentClassLoader(JNIEnv *env)
{
- /* XXX if a method in a class in a trusted loader is in a
- doPrivileged, return NULL */
+ TRACEJVMCALLS(("JVM_CurrentClassLoader(env=%p)", env));
+ PRINTJVMWARNINGS(("JVM_CurrentClassLoader is deprecated, do not use it."));
- log_println("JVM_CurrentClassLoader: IMPLEMENT ME!");
-
- return NULL;
+ return stacktrace_first_nonsystem_classloader();
}
{
TRACEJVMCALLS(("JVM_GetClassContext(env=%p)", env));
- return (jobjectArray) stacktrace_getClassContext();
+ return stacktrace_getClassContext();
}
jobject JVM_LatestUserDefinedLoader(JNIEnv *env)
{
- classloader_t *cl;
-
TRACEJVMCALLS(("JVM_LatestUserDefinedLoader(env=%p)", env));
- cl = stacktrace_first_nonnull_classloader();
-
- return (jobject) cl;
+ return stacktrace_first_nonnull_classloader();
}
jint JVM_GetArrayLength(JNIEnv *env, jobject arr)
{
- java_handle_t *a;
-
TRACEJVMCALLS(("JVM_GetArrayLength(arr=%p)", arr));
- a = (java_handle_t *) arr;
+ if (arr == NULL) {
+ exceptions_throw_nullpointerexception();
+ return -1;
+ }
+
+ Array a(arr);
- return array_length_get(a);
+ // Check for exception in constructor.
+ if (a.is_null()) {
+ return -1;
+ }
+
+ return a.get_length();
}
jobject JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index)
{
- java_handle_t *a;
- java_handle_t *o;
-
TRACEJVMCALLS(("JVM_GetArrayElement(env=%p, arr=%p, index=%d)", env, arr, index));
- a = (java_handle_t *) arr;
+ Array a(arr);
/* if (!class_is_array(a->objheader.vftbl->class)) { */
/* exceptions_throw_illegalargumentexception(); */
/* return NULL; */
/* } */
- o = array_element_get(a, index);
-
- return (jobject) o;
+ return a.get_boxed_element(index);
}
void JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val)
{
- java_handle_t *a;
- java_handle_t *value;
-
TRACEJVMCALLS(("JVM_SetArrayElement(env=%p, arr=%p, index=%d, val=%p)", env, arr, index, val));
- a = (java_handle_t *) arr;
- value = (java_handle_t *) val;
+ Array a(arr);
- array_element_set(a, index, value);
+ a.set_boxed_element(index, val);
}
jobject JVM_NewArray(JNIEnv *env, jclass eltClass, jint length)
{
- classinfo *c;
- classinfo *pc;
- java_handle_t *a;
- java_handle_objectarray_t *oa;
-
TRACEJVMCALLS(("JVM_NewArray(env=%p, eltClass=%p, length=%d)", env, eltClass, length));
if (eltClass == NULL) {
return NULL;
}
- /* NegativeArraySizeException is checked in builtin_newarray. */
+ /* NegativeArraySizeException is checked by array constructor. */
- c = LLNI_classinfo_unwrap(eltClass);
+ classinfo* c = LLNI_classinfo_unwrap(eltClass);
/* Create primitive or object array. */
if (class_is_primitive(c)) {
- pc = Primitive::get_arrayclass_by_name(c->name);
+ classinfo* pc = Primitive::get_arrayclass_by_name(c->name);
/* void arrays are not allowed. */
return NULL;
}
- a = builtin_newarray(length, pc);
+ Array a(length, pc);
- return (jobject) a;
+ return (jobject) a.get_handle();
}
else {
- oa = builtin_anewarray(length, c);
+ ObjectArray oa(length, c);
- return (jobject) oa;
+ return (jobject) oa.get_handle();
}
}
jobject JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim)
{
classinfo *c;
- java_handle_intarray_t *ia;
int32_t length;
long *dims;
int32_t value;
c = LLNI_classinfo_unwrap(eltClass);
- ia = (java_handle_intarray_t *) dim;
-
- length = array_length_get((java_handle_t *) ia);
+ IntArray ia(dim);
/* We check here for exceptions thrown in array_length_get,
otherwise these exceptions get overwritten by the following
IllegalArgumentException. */
- if (length < 0)
+ if (ia.is_null())
return NULL;
+ length = ia.get_length();
+
if ((length <= 0) || (length > /* MAX_DIM */ 255)) {
exceptions_throw_illegalargumentexception();
return NULL;
dims = MNEW(long, length);
for (i = 0; i < length; i++) {
- value = LLNI_array_direct(ia, i);
+ value = ia.get_element(i);
dims[i] = (long) value;
}
/* Create an array-class if necessary. */
- if (class_is_primitive(c))
+ if (class_is_primitive(c)) {
ac = Primitive::get_arrayclass_by_name(c->name);
+
+ // Arrays of void are not allowed.
+ if (ac == NULL) {
+ exceptions_throw_illegalargumentexception();
+ return NULL;
+ }
+
+ if (length > 1)
+ ac = class_multiarray_of((length - 1), ac, true);
+ }
else
- ac = class_array_of(c, true);
+ ac = class_multiarray_of(length, c, true);
if (ac == NULL)
return NULL;
+ /* Allocate a new array on the heap. */
+
a = builtin_multianewarray(length, (java_handle_t *) ac, dims);
return (jobject) a;
{
TRACEJVMCALLS(("JVM_InitializeSocketLibrary()"));
- return hpi_initialize_socket_library();
+ HPI& hpi = VM::get_current()->get_hpi();
+ return hpi.initialize_socket_library();
}
jint JVM_Send(jint fd, char *buf, jint nBytes, jint flags)
{
- log_println("JVM_Send: IMPLEMENT ME!");
+ TRACEJVMCALLSENTER(("JVM_Send(fd=%d, buf=%p, nBytes=%d, flags=%d", fd, buf, nBytes, flags));
- return 0;
+ int result = os::send(fd, buf, nBytes, flags);
+
+ TRACEJVMCALLSEXIT(("->%d", result));
+
+ return result;
}
/* JVM_LoadLibrary */
-void *JVM_LoadLibrary(const char *name)
+void* JVM_LoadLibrary(const char* name)
{
- utf* u;
- void* handle;
-
TRACEJVMCALLSENTER(("JVM_LoadLibrary(name=%s)", name));
- u = utf_new_char(name);
-
- handle = native_library_open(u);
+ utf* u = utf_new_char(name);
+ NativeLibrary nl(u);
+ void* handle = nl.open();
+
TRACEJVMCALLSEXIT(("->%p", handle));
return handle;
{
TRACEJVMCALLS(("JVM_UnloadLibrary(handle=%p)", handle));
- native_library_close(handle);
+ NativeLibrary nl(handle);
+ nl.close();
}
/* JVM_FindLibraryEntry */
-void *JVM_FindLibraryEntry(void *handle, const char *name)
+void *JVM_FindLibraryEntry(void* handle, const char* name)
{
void* symbol;
TRACEJVMCALLSENTER(("JVM_FindLibraryEntry(handle=%p, name=%s)", handle, name));
- symbol = hpi_library->FindLibraryEntry(handle, name);
+ HPI& hpi = VM::get_current()->get_hpi();
+ symbol = hpi.get_library().FindLibraryEntry(handle, name);
TRACEJVMCALLSEXIT(("->%p", symbol));
/* JVM_IsNaN */
-jboolean JVM_IsNaN(jdouble a)
+jboolean JVM_IsNaN(jdouble d)
{
- log_println("JVM_IsNaN: IMPLEMENT ME!");
+ bool result;
- return 0;
+ TRACEJVMCALLSENTER(("JVM_IsNaN(d=%f)", d));
+
+ result = isnan(d);
+
+ TRACEJVMCALLSEXIT(("->%d", result));
+
+ return result;
}
JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void)
{
- java_object_t *o;
-
TRACEJVMCALLS(("JVM_RawMonitorCreate()"));
- o = NEW(java_object_t);
-
- lock_init_object_lock(o);
+ Mutex* m = new Mutex();
- return o;
+ return m;
}
/* JVM_RawMonitorDestroy */
-JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon)
+JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void* mon)
{
TRACEJVMCALLS(("JVM_RawMonitorDestroy(mon=%p)", mon));
- FREE(mon, java_object_t);
+ delete ((Mutex*) mon);
}
/* JVM_RawMonitorEnter */
-JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon)
+JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void* mon)
{
TRACEJVMCALLS(("JVM_RawMonitorEnter(mon=%p)", mon));
- (void) lock_monitor_enter((java_object_t *) mon);
+ ((Mutex*) mon)->lock();
return 0;
}
/* JVM_RawMonitorExit */
-JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon)
+JNIEXPORT void JNICALL JVM_RawMonitorExit(void* mon)
{
TRACEJVMCALLS(("JVM_RawMonitorExit(mon=%p)", mon));
- (void) lock_monitor_exit((java_object_t *) mon);
+ ((Mutex*) mon)->unlock();
}
jobjectArray JVM_GetAllThreads(JNIEnv *env, jclass dummy)
{
- log_println("JVM_GetAllThreads: IMPLEMENT ME!");
+ // Get a list of all active threads.
+ List<threadobject*> active_threads;
+ ThreadList::get_active_java_threads(active_threads);
- return NULL;
+ // Allocate array to hold the java.lang.Thread objects.
+ int32_t length = active_threads.size();
+ ObjectArray oa(length, class_java_lang_Thread);
+
+ if (oa.is_null())
+ return NULL;
+
+ // Iterate over all threads (which were active just a second ago).
+ int32_t index = 0;
+ for (List<threadobject*>::iterator it = active_threads.begin(); it != active_threads.end(); it++) {
+ threadobject* t = *it;
+
+ java_handle_t* h = thread_get_object(t);
+ assert(h != NULL);
+
+ oa.set_element(index, h);
+
+ index++;
+ }
+
+ return oa.get_handle();
}
jobjectArray JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads)
{
- log_println("JVM_DumpThreads: IMPLEMENT ME!");
+ int32_t i;
- return NULL;
+ TRACEJVMCALLS(("JVM_DumpThreads((env=%p, threadClass=%p, threads=%p)", env, threadClass, threads));
+
+ if (threads == NULL) {
+ exceptions_throw_nullpointerexception();
+ return NULL;
+ }
+
+ ObjectArray oa(threads);
+
+ // Get length of the threads array.
+ int32_t length = oa.get_length();
+
+ if (length <= 0) {
+ exceptions_throw_illegalargumentexception();
+ return NULL;
+ }
+
+ // Allocate array to hold stacktraces.
+ classinfo* arrayclass = class_array_of(class_java_lang_StackTraceElement, true);
+ ObjectArray oaresult(length, arrayclass);
+
+ if (oaresult.is_null()) {
+ return NULL;
+ }
+
+ // Iterate over all passed thread objects.
+ for (i = 0; i < length; i++) {
+ java_handle_t* thread = oa.get_element(i);
+
+ // Get thread for the given thread object.
+ threadobject* t = thread_get_thread(thread);
+
+ // The threadobject is null when a thread is created in Java.
+ if (t == NULL)
+ continue;
+
+ // Get stacktrace for given thread.
+ stacktrace_t* st = stacktrace_get_of_thread(t);
+
+ // Convert stacktrace into array of StackTraceElements.
+ java_handle_objectarray_t* oaste = stacktrace_get_StackTraceElements(st);
+
+ if (oaste == NULL)
+ return NULL;
+
+ oaresult.set_element(i, oaste);
+ }
+
+ return oaresult.get_handle();
}
{
TRACEJVMCALLS(("JVM_GetManagement(version=%d)", version));
- /* TODO We current don't support the management interface. */
-
- return NULL;
+ return Management::get_jmm_interface(version);
}
jobjectArray JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass)
{
- classinfo *c;
- methodinfo *m;
- java_handle_objectarray_t *oa;
-
TRACEJVMCALLS(("JVM_GetEnclosingMethodInfo(env=%p, ofClass=%p)", env, ofClass));
- c = LLNI_classinfo_unwrap(ofClass);
+ classinfo* c = LLNI_classinfo_unwrap(ofClass);
if ((c == NULL) || class_is_primitive(c))
return NULL;
- m = class_get_enclosingmethod_raw(c);
+ methodinfo* m = class_get_enclosingmethod_raw(c);
if (m == NULL)
return NULL;
- oa = builtin_anewarray(3, class_java_lang_Object);
+ ObjectArray oa(3, class_java_lang_Object);
- if (oa == NULL)
+ if (oa.is_null())
return NULL;
- array_objectarray_element_set(oa, 0, (java_handle_t *) LLNI_classinfo_wrap(m->clazz));
- array_objectarray_element_set(oa, 1, javastring_new(m->name));
- array_objectarray_element_set(oa, 2, javastring_new(m->descriptor));
+ oa.set_element(0, (java_handle_t *) LLNI_classinfo_wrap(m->clazz));
+ oa.set_element(1, javastring_new(m->name));
+ oa.set_element(2, javastring_new(m->descriptor));
- return (jobjectArray) oa;
+ return oa.get_handle();
}
jintArray JVM_GetThreadStateValues(JNIEnv* env, jint javaThreadState)
{
- java_handle_intarray_t *ia;
-
TRACEJVMCALLS(("JVM_GetThreadStateValues(env=%p, javaThreadState=%d)",
env, javaThreadState));
switch (javaThreadState) {
case THREAD_STATE_NEW:
- ia = builtin_newarray_int(1);
+ {
+ IntArray ia(1);
- if (ia == NULL)
- return NULL;
+ if (ia.is_null())
+ return NULL;
- array_intarray_element_set(ia, 0, THREAD_STATE_NEW);
- break;
+ ia.set_element(0, THREAD_STATE_NEW);
+ return ia.get_handle();
+ }
case THREAD_STATE_RUNNABLE:
- ia = builtin_newarray_int(1);
+ {
+ IntArray ia(1);
- if (ia == NULL)
- return NULL;
+ if (ia.is_null())
+ return NULL;
- array_intarray_element_set(ia, 0, THREAD_STATE_RUNNABLE);
- break;
+ ia.set_element(0, THREAD_STATE_RUNNABLE);
+ return ia.get_handle();
+ }
case THREAD_STATE_BLOCKED:
- ia = builtin_newarray_int(1);
+ {
+ IntArray ia(1);
- if (ia == NULL)
- return NULL;
+ if (ia.is_null())
+ return NULL;
- array_intarray_element_set(ia, 0, THREAD_STATE_BLOCKED);
- break;
+ ia.set_element(0, THREAD_STATE_BLOCKED);
+ return ia.get_handle();
+ }
case THREAD_STATE_WAITING:
- ia = builtin_newarray_int(2);
+ {
+ IntArray ia(2);
- if (ia == NULL)
- return NULL;
+ if (ia.is_null())
+ return NULL;
- array_intarray_element_set(ia, 0, THREAD_STATE_WAITING);
- /* XXX Implement parked stuff. */
-/* array_intarray_element_set(ia, 1, PARKED); */
- break;
+ ia.set_element(0, THREAD_STATE_WAITING);
+ ia.set_element(1, THREAD_STATE_PARKED);
+ return ia.get_handle();
+ }
case THREAD_STATE_TIMED_WAITING:
- ia = builtin_newarray_int(3);
+ {
+ IntArray ia(2);
- if (ia == NULL)
- return NULL;
+ if (ia.is_null())
+ return NULL;
- /* XXX Not sure about that one. */
-/* array_intarray_element_set(ia, 0, SLEEPING); */
- array_intarray_element_set(ia, 0, THREAD_STATE_TIMED_WAITING);
- /* XXX Implement parked stuff. */
-/* array_intarray_element_set(ia, 2, PARKED); */
- break;
+ /* XXX Not sure about that one. */
+/* ia.set_element(0, SLEEPING); */
+ ia.set_element(0, THREAD_STATE_TIMED_WAITING);
+ ia.set_element(1, THREAD_STATE_TIMED_PARKED);
+ return ia.get_handle();
+ }
case THREAD_STATE_TERMINATED:
- ia = builtin_newarray_int(1);
+ {
+ IntArray ia(1);
- if (ia == NULL)
- return NULL;
+ if (ia.is_null())
+ return NULL;
- array_intarray_element_set(ia, 0, THREAD_STATE_TERMINATED);
- break;
+ ia.set_element(0, THREAD_STATE_TERMINATED);
+ return ia.get_handle();
+ }
default:
/* Unknown state - probably incompatible JDK version */
return NULL;
}
-
- return (jintArray) ia;
}
jobjectArray JVM_GetThreadStateNames(JNIEnv* env, jint javaThreadState, jintArray values)
{
- java_handle_intarray_t *ia;
- java_handle_objectarray_t *oa;
- java_object_t *s;
+ java_object_t* s;
TRACEJVMCALLS(("JVM_GetThreadStateNames(env=%p, javaThreadState=%d, values=%p)",
env, javaThreadState, values));
- ia = (java_handle_intarray_t *) values;
+ IntArray ia(values);
/* If new thread states are added in future JDK and VM versions,
this should check if the JDK version is compatible with thread
switch (javaThreadState) {
case THREAD_STATE_NEW:
- assert(ia->header.size == 1 && ia->data[0] == THREAD_STATE_NEW);
+ {
+ assert(ia.get_length() == 1 && ia.get_element(0) == THREAD_STATE_NEW);
- oa = builtin_anewarray(1, class_java_lang_String);
+ ObjectArray oa(1, class_java_lang_String);
- if (oa == NULL)
- return NULL;
+ if (oa.is_null())
+ return NULL;
- s = javastring_new(utf_new_char("NEW"));
+ s = javastring_new(utf_new_char("NEW"));
- if (s == NULL)
- return NULL;
+ if (s == NULL)
+ return NULL;
- array_objectarray_element_set(oa, 0, s);
- break;
+ oa.set_element(0, s);
+ return oa.get_handle();
+ }
case THREAD_STATE_RUNNABLE:
- oa = builtin_anewarray(1, class_java_lang_String);
+ {
+ ObjectArray oa(1, class_java_lang_String);
- if (oa == NULL)
- return NULL;
+ if (oa.is_null())
+ return NULL;
- s = javastring_new(utf_new_char("RUNNABLE"));
+ s = javastring_new(utf_new_char("RUNNABLE"));
- if (s == NULL)
- return NULL;
+ if (s == NULL)
+ return NULL;
- array_objectarray_element_set(oa, 0, s);
- break;
+ oa.set_element(0, s);
+ return oa.get_handle();
+ }
case THREAD_STATE_BLOCKED:
- oa = builtin_anewarray(1, class_java_lang_String);
+ {
+ ObjectArray oa(1, class_java_lang_String);
- if (oa == NULL)
- return NULL;
+ if (oa.is_null())
+ return NULL;
- s = javastring_new(utf_new_char("BLOCKED"));
+ s = javastring_new(utf_new_char("BLOCKED"));
- if (s == NULL)
- return NULL;
+ if (s == NULL)
+ return NULL;
- array_objectarray_element_set(oa, 0, s);
- break;
+ oa.set_element(0, s);
+ return oa.get_handle();
+ }
case THREAD_STATE_WAITING:
- oa = builtin_anewarray(2, class_java_lang_String);
+ {
+ ObjectArray oa(2, class_java_lang_String);
- if (oa == NULL)
- return NULL;
+ if (oa.is_null())
+ return NULL;
- s = javastring_new(utf_new_char("WAITING.OBJECT_WAIT"));
-/* s = javastring_new(utf_new_char("WAITING.PARKED")); */
+ s = javastring_new(utf_new_char("WAITING.OBJECT_WAIT"));
- if (s == NULL)
- return NULL;
+ if (s == NULL)
+ return NULL;
+
+ oa.set_element(0, s);
- array_objectarray_element_set(oa, 0, s);
-/* array_objectarray_element_set(oa, 1, s); */
- break;
+ s = javastring_new(utf_new_char("WAITING.PARKED"));
+
+ if (s == NULL)
+ return NULL;
+
+ oa.set_element(1, s);
+ return oa.get_handle();
+ }
case THREAD_STATE_TIMED_WAITING:
- oa = builtin_anewarray(3, class_java_lang_String);
+ {
+ ObjectArray oa(2, class_java_lang_String);
- if (oa == NULL)
- return NULL;
+ if (oa.is_null())
+ return NULL;
-/* s = javastring_new(utf_new_char("TIMED_WAITING.SLEEPING")); */
- s = javastring_new(utf_new_char("TIMED_WAITING.OBJECT_WAIT"));
-/* s = javastring_new(utf_new_char("TIMED_WAITING.PARKED")); */
+/* s = javastring_new(utf_new_char("TIMED_WAITING.SLEEPING")); */
+ s = javastring_new(utf_new_char("TIMED_WAITING.OBJECT_WAIT"));
- if (s == NULL)
- return NULL;
+ if (s == NULL)
+ return NULL;
+
+ oa.set_element(0, s);
-/* array_objectarray_element_set(oa, 0, s); */
- array_objectarray_element_set(oa, 0, s);
-/* array_objectarray_element_set(oa, 2, s); */
- break;
+ s = javastring_new(utf_new_char("TIMED_WAITING.PARKED"));
+
+ if (s == NULL)
+ return NULL;
+
+ oa.set_element(1, s);
+ return oa.get_handle();
+ }
case THREAD_STATE_TERMINATED:
- oa = builtin_anewarray(1, class_java_lang_String);
+ {
+ ObjectArray oa(1, class_java_lang_String);
- if (oa == NULL)
- return NULL;
+ if (oa.is_null())
+ return NULL;
- s = javastring_new(utf_new_char("TERMINATED"));
+ s = javastring_new(utf_new_char("TERMINATED"));
- if (s == NULL)
- return NULL;
+ if (s == NULL)
+ return NULL;
- array_objectarray_element_set(oa, 0, s);
- break;
+ oa.set_element(0, s);
+ return oa.get_handle();
+ }
default:
/* Unknown state - probably incompatible JDK version */
return NULL;
}
-
- return (jobjectArray) oa;
}
void JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size)
{
- log_println("JVM_GetVersionInfo: IMPLEMENT ME!");
+ TRACEJVMCALLS(("JVM_GetVersionInfo(env=%p, info=%p, info_size=%zd)", env, info, info_size));
+
+ memset(info, 0, info_size);
+
+ info->jvm_version = ((VERSION_MAJOR & 0xff) << 24) | ((VERSION_MINOR & 0xff) << 16) | (VERSION_MICRO & 0xff);
+ info->update_version = 0;
+ info->special_update_version = 0;
+ info->is_attach_supported = 0;
+ info->is_kernel_jvm = 0;
}
* c-basic-offset: 4
* tab-width: 4
* End:
+ * vim:noexpandtab:sw=4:ts=4:
*/