* src/vm/system.c: Removed.
[cacao.git] / src / native / vm / sun / jvm.c
index b4bc9d56543551e45a1786841e5744a7758d05a0..d2d9d3352bc11325f715e367669486ec89cbfd45 100644 (file)
@@ -33,6 +33,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <ltdl.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -44,6 +45,7 @@
 
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 
 #include "vm/types.h"
 
 
 #include "native/vm/java_lang_Class.h"
 #include "native/vm/java_lang_ClassLoader.h"
-#include "native/vm/java_lang_Object.h"
 #include "native/vm/java_lang_Runtime.h"
 #include "native/vm/java_lang_Thread.h"
 #include "native/vm/java_lang_reflect_Constructor.h"
 #include "native/vm/java_lang_reflect_Method.h"
-#include "native/vm/java_util_concurrent_atomic_AtomicLong.h"
 #include "native/vm/reflect.h"
 
 #include "threads/lock-common.h"
@@ -93,6 +93,7 @@
 #include "vm/exceptions.h"
 #include "vm/global.h"
 #include "vm/initialize.h"
+#include "vm/package.h"
 #include "vm/primitive.h"
 #include "vm/properties.h"
 #include "vm/resolve.h"
 
 #include "vmcore/classcache.h"
 #include "vmcore/options.h"
+#include "vmcore/system.h"
 
 
 /* debugging macros ***********************************************************/
 
 #if !defined(NDEBUG)
 
-# define TRACEJVMCALLS(...) \
-    do { \
-        if (opt_TraceJVMCalls) { \
-            log_println(__VA_ARGS__); \
-        } \
+# define TRACEJVMCALLS(...)                                            \
+    do {                                                                               \
+        if (opt_TraceJVMCalls) {                               \
+            log_println(__VA_ARGS__);                  \
+        }                                                                              \
     } while (0)
 
 # define PRINTJVMWARNINGS(...)
@@ -254,7 +256,7 @@ void JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos, jobje
        log_println("JVM_ArrayCopy: src=%p, src_pos=%d, dst=%p, dst_pos=%d, length=%d", src, src_pos, dst, dst_pos, length);
 #endif
 
-       (void) builtin_arraycopy(s, src_pos, d, dst_pos, length);
+       builtin_arraycopy(s, src_pos, d, dst_pos, length);
 }
 
 
@@ -262,10 +264,15 @@ void JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos, jobje
 
 jobject JVM_InitProperties(JNIEnv *env, jobject properties)
 {
-#if PRINTJVM
-       log_println("JVM_InitProperties: properties=%d", properties);
-#endif
-       properties_system_add_all((java_handle_t *) properties);
+       java_handle_t *h;
+
+       TRACEJVMCALLS("JVM_InitProperties(env=%p, properties=%p)", env, properties);
+
+       h = (java_handle_t *) properties;
+
+       properties_system_add_all(h);
+
+       return properties;
 }
 
 
@@ -365,7 +372,9 @@ jlong JVM_MaxMemory(void)
 
 jint JVM_ActiveProcessorCount(void)
 {
-       log_println("JVM_ActiveProcessorCount: IMPLEMENT ME!");
+       TRACEJVMCALLS("JVM_ActiveProcessorCount()");
+
+       return system_processors_online();
 }
 
 
@@ -373,8 +382,8 @@ jint JVM_ActiveProcessorCount(void)
 
 void JVM_FillInStackTrace(JNIEnv *env, jobject receiver)
 {
-       java_lang_Throwable *o;
-       stacktracecontainer *stc;
+       java_lang_Throwable     *o;
+       java_handle_bytearray_t *ba;
 
 #if PRINTJVM
        log_println("JVM_FillInStackTrace: receiver=%p", receiver);
@@ -382,12 +391,12 @@ void JVM_FillInStackTrace(JNIEnv *env, jobject receiver)
 
        o = (java_lang_Throwable *) receiver;
 
-       stc = stacktrace_fillInStackTrace();
+       ba = stacktrace_fillInStackTrace();
 
-       if (stc == NULL)
+       if (ba == NULL)
                return;
 
-    o->backtrace = (java_lang_Object *) stc;
+       o->backtrace = (java_lang_Object *) ba;
 }
 
 
@@ -403,17 +412,24 @@ void JVM_PrintStackTrace(JNIEnv *env, jobject receiver, jobject printable)
 
 jint JVM_GetStackTraceDepth(JNIEnv *env, jobject throwable)
 {
-       java_lang_Throwable *o;
-       stacktracecontainer *stc;
-       stacktracebuffer    *stb;
+       java_lang_Throwable     *o;
+       java_handle_bytearray_t *ba;
+       stacktracebuffer        *stb;
 
-#if PRINTJVM
-       log_println("JVM_GetStackTraceDepth: throwable=%p", throwable);
-#endif
+       TRACEJVMCALLS("JVM_GetStackTraceDepth(env=%p, throwable=%p)", env, throwable);
+
+       if (throwable == NULL) {
+               exceptions_throw_nullpointerexception();
+               return 0;
+       }
 
        o   = (java_lang_Throwable *) throwable;
-       stc = (stacktracecontainer *) o->backtrace;
-       stb = &(stc->stb);
+       ba  = (java_handle_bytearray_t *) o->backtrace;
+
+       if (ba == NULL)
+               return 0;
+
+       stb = (stacktracebuffer *) LLNI_array_data(ba);
 
        return stb->used;
 }
@@ -424,9 +440,9 @@ jint JVM_GetStackTraceDepth(JNIEnv *env, jobject throwable)
 jobject JVM_GetStackTraceElement(JNIEnv *env, jobject throwable, jint index)
 {
        java_lang_Throwable *t;
-       stacktracecontainer *stc;
-       stacktracebuffer    *stb;
-       stacktrace_entry    *ste;
+       java_handle_bytearray_t     *ba;
+       stacktracebuffer            *stb;
+       stacktrace_entry            *ste;
        java_lang_StackTraceElement *o;
        java_lang_String            *declaringclass;
        java_lang_String            *filename;
@@ -437,8 +453,8 @@ jobject JVM_GetStackTraceElement(JNIEnv *env, jobject throwable, jint index)
 #endif
 
        t   = (java_lang_Throwable *) throwable;
-       stc = (stacktracecontainer *) t->backtrace;
-       stb = &(stc->stb);
+       ba  = (java_handle_bytearray_t *) t->backtrace;
+       stb = (stacktracebuffer *) LLNI_array_data(ba);
 
        if ((index < 0) || (index >= stb->used)) {
                /* XXX This should be an IndexOutOfBoundsException (check this
@@ -507,10 +523,22 @@ jint JVM_IHashCode(JNIEnv* env, jobject handle)
 
 void JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms)
 {
-#if PRINTJVM
-       log_println("JVM_MonitorWait: handle=%p, ms=%ld", handle, ms);
+#if defined(ENABLE_THREADS)
+       java_handle_t *o;
+#endif
+
+       TRACEJVMCALLS("JVM_MonitorWait(env=%p, handle=%p, ms=%ld)", env, handle, ms);
+    if (ms < 0) {
+/*             exceptions_throw_illegalargumentexception("argument out of range"); */
+               exceptions_throw_illegalargumentexception();
+               return;
+       }
+
+#if defined(ENABLE_THREADS)
+       o = (java_handle_t *) handle;
+
+       lock_wait_for_object(o, ms, 0);
 #endif
-       _Jv_java_lang_Object_wait((java_lang_Object *) handle, ms, 0);
 }
 
 
@@ -518,10 +546,17 @@ void JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms)
 
 void JVM_MonitorNotify(JNIEnv* env, jobject handle)
 {
-#if PRINTJVM
-       log_println("JVM_MonitorNotify: IMPLEMENT ME!");
+#if defined(ENABLE_THREADS)
+       java_handle_t *o;
+#endif
+
+       TRACEJVMCALLS("JVM_MonitorNotify(env=%p, handle=%p)", env, handle);
+
+#if defined(ENABLE_THREADS)
+       o = (java_handle_t *) handle;
+
+       lock_notify_object(o);
 #endif
-       _Jv_java_lang_Object_notify((java_lang_Object *) handle);
 }
 
 
@@ -529,10 +564,17 @@ void JVM_MonitorNotify(JNIEnv* env, jobject handle)
 
 void JVM_MonitorNotifyAll(JNIEnv* env, jobject handle)
 {
-#if PRINTJVM
-       log_println("JVM_MonitorNotifyAll: handle=%p", handle);
+#if defined(ENABLE_THREADS)
+       java_handle_t *o;
+#endif
+
+       TRACEJVMCALLS("JVM_MonitorNotifyAll(env=%p, handle=%p)", env, handle);
+
+#if defined(ENABLE_THREADS)
+       o = (java_handle_t *) handle;
+
+       lock_notify_all_object(o);
 #endif
-       _Jv_java_lang_Object_notifyAll((java_lang_Object *) handle);
 }
 
 
@@ -700,7 +742,7 @@ jclass JVM_FindClassFromClassLoader(JNIEnv* env, const char* name, jboolean init
        TRACEJVMCALLS("JVM_FindClassFromClassLoader: name=%s, init=%d, loader=%p, throwError=%d", name, init, loader, throwError);
 
        u  = utf_new_char(name);
-       cl = (classloader *) loader;
+       cl = loader_hashtable_classloader_add((java_handle_t *) loader);
 
        c = load_class_from_classloader(u, cl);
 
@@ -742,8 +784,12 @@ jclass JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader,
 
        TRACEJVMCALLS("JVM_DefineClassWithSource(env=%p, name=%s, loader=%p, buf=%p, len=%d, pd=%p, source=%s)", env, name, loader, buf, len, pd, source);
 
-       u  = utf_new_char(name);
-       cl = (classloader *) loader;
+       if (name != NULL)
+               u = utf_new_char(name);
+       else
+               u = NULL;
+
+       cl = loader_hashtable_classloader_add((java_handle_t *) loader);
 
        /* XXX do something with source */
 
@@ -763,7 +809,7 @@ jclass JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name)
 
        TRACEJVMCALLS("JVM_FindLoadedClass(env=%p, loader=%p, name=%p)", env, loader, name);
 
-       cl = (classloader *) loader;
+       cl = loader_hashtable_classloader_add((java_handle_t *) loader);
 
        u = javastring_toutf((java_handle_t *) name, true);
        c = classcache_lookup(cl, u);
@@ -804,9 +850,15 @@ jobjectArray JVM_GetClassInterfaces(JNIEnv *env, jclass cls)
 
 jobject JVM_GetClassLoader(JNIEnv *env, jclass cls)
 {
+       classinfo   *c;
+       classloader *cl;
+
        TRACEJVMCALLS("JVM_GetClassLoader(env=%p, cls=%p)", env, cls);
 
-       return (jobject) _Jv_java_lang_Class_getClassLoader((java_lang_Class *) cls);
+       c  = LLNI_classinfo_unwrap(cls);
+       cl = class_get_classloader(c);
+
+       return (jobject) cl;
 }
 
 
@@ -838,7 +890,24 @@ jobjectArray JVM_GetClassSigners(JNIEnv *env, jclass cls)
 
 void JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers)
 {
-       log_println("JVM_SetClassSigners: IMPLEMENT ME!");
+       classinfo                 *c;
+       java_handle_objectarray_t *hoa;
+
+       TRACEJVMCALLS("JVM_SetClassSigners(env=%p, cls=%p, signers=%p)", env, cls, signers);
+
+       c = LLNI_classinfo_unwrap(cls);
+
+       hoa = (java_handle_objectarray_t *) signers;
+
+    /* This call is ignored for primitive types and arrays.  Signers
+          are only set once, ClassLoader.java, and thus shouldn't be
+          called with an array.  Only the bootstrap loader creates
+          arrays. */
+
+       if (class_is_primitive(c) || class_is_array(c))
+               return;
+
+       LLNI_classinfo_field_set(c, signers, hoa);
 }
 
 
@@ -934,7 +1003,13 @@ jobject JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls)
 
 jobject JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls)
 {
-       log_println("JVM_GetStackAccessControlContext: IMPLEMENT ME!");
+       TRACEJVMCALLS("JVM_GetStackAccessControlContext(env=%p, cls=%p): IMPLEMENT ME!", env, cls);
+
+       /* XXX All stuff I tested so far works without that function.  At
+          some point we have to implement it, but I disable the output
+          for now to make IcedTea happy. */
+
+       return NULL;
 }
 
 
@@ -987,16 +1062,15 @@ jclass JVM_GetComponentType(JNIEnv *env, jclass cls)
 jint JVM_GetClassModifiers(JNIEnv *env, jclass cls)
 {
        classinfo *c;
+       int32_t    flags;
 
-#if PRINTJVM
-       log_println("JVM_GetClassModifiers: cls=%p", cls);
-#endif
+       TRACEJVMCALLS("JVM_GetClassModifiers(env=%p, cls=%p)", env, cls);
 
        c = LLNI_classinfo_unwrap(cls);
 
-       /* XXX is this correct? */
+       flags = class_get_modifiers(c, false);
 
-       return c->flags & ACC_CLASS_REFLECT_MASK;
+       return flags;
 }
 
 
@@ -1040,11 +1114,11 @@ jstring JVM_GetClassSignature(JNIEnv *env, jclass cls)
 {
        classinfo     *c;
        utf           *u;
-       java_object_t *s;
+       java_handle_t *s;
 
        TRACEJVMCALLS("JVM_GetClassSignature(env=%p, cls=%p)", env, cls);
 
-       c = (classinfo *) cls;
+       c = LLNI_classinfo_unwrap(cls);
 
        /* Get the signature of the class. */
 
@@ -1065,8 +1139,8 @@ jstring JVM_GetClassSignature(JNIEnv *env, jclass cls)
 
 jbyteArray JVM_GetClassAnnotations(JNIEnv *env, jclass cls)
 {
-       classinfo               *c           = NULL;
-       java_handle_bytearray_t *annotations = NULL;
+       classinfo               *c           = NULL; /* classinfo for 'cls'  */
+       java_handle_bytearray_t *annotations = NULL; /* unparsed annotations */
 
        TRACEJVMCALLS("JVM_GetClassAnnotations: cls=%p", cls);
 
@@ -1088,8 +1162,8 @@ jbyteArray JVM_GetClassAnnotations(JNIEnv *env, jclass cls)
 
 jbyteArray JVM_GetFieldAnnotations(JNIEnv *env, jobject field)
 {
-       java_lang_reflect_Field *rf = (java_lang_reflect_Field*)field;
-       java_handle_bytearray_t *ba = NULL;
+       java_lang_reflect_Field *rf = NULL; /* java.lang.reflect.Field for 'field' */
+       java_handle_bytearray_t *ba = NULL; /* unparsed annotations */
 
        TRACEJVMCALLS("JVM_GetFieldAnnotations: field=%p", field);
 
@@ -1098,6 +1172,8 @@ jbyteArray JVM_GetFieldAnnotations(JNIEnv *env, jobject field)
                return NULL;
        }
 
+       rf = (java_lang_reflect_Field*)field;
+
        LLNI_field_get_ref(rf, annotations, ba);
 
        return (jbyteArray)ba;
@@ -1108,8 +1184,8 @@ jbyteArray JVM_GetFieldAnnotations(JNIEnv *env, jobject field)
 
 jbyteArray JVM_GetMethodAnnotations(JNIEnv *env, jobject method)
 {
-       java_lang_reflect_Method *rm = (java_lang_reflect_Method*)method;
-       java_handle_bytearray_t  *ba = NULL;
+       java_lang_reflect_Method *rm = NULL; /* java.lang.reflect.Method for 'method' */
+       java_handle_bytearray_t  *ba = NULL; /* unparsed annotations */
 
        TRACEJVMCALLS("JVM_GetMethodAnnotations: method=%p", method);
 
@@ -1118,6 +1194,8 @@ jbyteArray JVM_GetMethodAnnotations(JNIEnv *env, jobject method)
                return NULL;
        }
 
+       rm = (java_lang_reflect_Method*)method;
+
        LLNI_field_get_ref(rm, annotations, ba);
 
        return (jbyteArray)ba;
@@ -1128,8 +1206,8 @@ jbyteArray JVM_GetMethodAnnotations(JNIEnv *env, jobject method)
 
 jbyteArray JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method)
 {
-       java_lang_reflect_Method *rm = (java_lang_reflect_Method*)method;
-       java_handle_bytearray_t  *ba = NULL;
+       java_lang_reflect_Method *rm = NULL; /* java.lang.reflect.Method for 'method' */
+       java_handle_bytearray_t  *ba = NULL; /* unparsed annotation default value */
 
        TRACEJVMCALLS("JVM_GetMethodDefaultAnnotationValue: method=%p", method);
 
@@ -1138,6 +1216,8 @@ jbyteArray JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method)
                return NULL;
        }
 
+       rm = (java_lang_reflect_Method*)method;
+
        LLNI_field_get_ref(rm, annotationDefault, ba);
 
        return (jbyteArray)ba;
@@ -1148,8 +1228,8 @@ jbyteArray JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method)
 
 jbyteArray JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method)
 {
-       java_lang_reflect_Method *rm = (java_lang_reflect_Method*)method;
-       java_handle_bytearray_t  *ba = NULL;
+       java_lang_reflect_Method *rm = NULL; /* java.lang.reflect.Method for 'method' */
+       java_handle_bytearray_t  *ba = NULL; /* unparsed parameter annotations */
 
        TRACEJVMCALLS("JVM_GetMethodParameterAnnotations: method=%p", method);
 
@@ -1158,6 +1238,8 @@ jbyteArray JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method)
                return NULL;
        }
 
+       rm = (java_lang_reflect_Method*)method;
+
        LLNI_field_get_ref(rm, parameterAnnotations, ba);
 
        return (jbyteArray)ba;
@@ -1203,12 +1285,12 @@ jint JVM_GetClassAccessFlags(JNIEnv *env, jclass cls)
 {
        classinfo *c;
 
-#if PRINTJVM
-       log_println("JVM_GetClassAccessFlags: cls=%p", cls);
-#endif
+       TRACEJVMCALLS("JVM_GetClassAccessFlags(env=%p, cls=%p)", env, cls);
 
        c = LLNI_classinfo_unwrap(cls);
 
+       /* Primitive type classes have the correct access flags. */
+
        return c->flags & ACC_CLASS_REFLECT_MASK;
 }
 
@@ -1219,8 +1301,10 @@ jobject JVM_GetClassConstantPool(JNIEnv *env, jclass cls)
 {
 #if defined(ENABLE_ANNOTATIONS)
        sun_reflect_ConstantPool *constantPool    = NULL;
+                     /* constant pool object for the class refered by 'cls' */
        java_lang_Object         *constantPoolOop = (java_lang_Object*)cls;
-       
+                     /* constantPoolOop field of the constant pool object   */
+
        TRACEJVMCALLS("JVM_GetClassConstantPool(env=%p, cls=%p)", env, cls);
 
        constantPool = 
@@ -1246,7 +1330,7 @@ jobject JVM_GetClassConstantPool(JNIEnv *env, jclass cls)
 
 jint JVM_ConstantPoolGetSize(JNIEnv *env, jobject unused, jobject jcpool)
 {
-       classinfo *c;
+       classinfo *c; /* classinfo of the class for which 'this' is the constant pool */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetSize(env=%p, unused=%p, jcpool=%p)", env, unused, jcpool);
 
@@ -1260,9 +1344,9 @@ jint JVM_ConstantPoolGetSize(JNIEnv *env, jobject unused, jobject jcpool)
 
 jclass JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       constant_classref *ref;
-       classinfo         *c;
-       classinfo         *result;
+       constant_classref *ref;    /* classref to the class at constant pool index 'index' */
+       classinfo         *c;      /* classinfo of the class for which 'this' is the constant pool */
+       classinfo         *result; /* classinfo of the class at constant pool index 'index' */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetClassAt(env=%p, jcpool=%p, index=%d)", env, jcpool, index);
 
@@ -1270,8 +1354,10 @@ jclass JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject unused, jobject jcpool, j
 
        ref = (constant_classref *) class_getconstant(c, index, CONSTANT_Class);
 
-       if (ref == NULL)
+       if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return NULL;
+       }
 
        result = resolve_classref_eager(ref);
 
@@ -1283,9 +1369,9 @@ jclass JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject unused, jobject jcpool, j
 
 jclass JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       constant_classref *ref;
-       classinfo         *c;
-       classinfo         *result;
+       constant_classref *ref;    /* classref to the class at constant pool index 'index' */
+       classinfo         *c;      /* classinfo of the class for which 'this' is the constant pool */
+       classinfo         *result; /* classinfo of the class at constant pool index 'index' */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetClassAtIfLoaded(env=%p, unused=%p, jcpool=%p, index=%d)", env, unused, jcpool, index);
 
@@ -1293,11 +1379,14 @@ jclass JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject unused, jobject j
 
        ref = (constant_classref *) class_getconstant(c, index, CONSTANT_Class);
 
-       if (ref == NULL)
+       if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return NULL;
+       }
        
-       if (!resolve_classref(NULL, ref, resolveLazy, true, true, &result))
+       if (!resolve_classref(NULL, ref, resolveLazy, true, true, &result)) {
                return NULL;
+       }
 
        if ((result == NULL) || !(result->state & CLASS_LOADED)) {
                return NULL;
@@ -1311,14 +1400,16 @@ jclass JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject unused, jobject j
 
 jobject JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       constant_FMIref *ref;
-       classinfo *cls = LLNI_classinfo_unwrap(jcpool);
+       constant_FMIref *ref; /* reference to the method in constant pool at index 'index' */
+       classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
        
        TRACEJVMCALLS("JVM_ConstantPoolGetMethodAt: jcpool=%p, index=%d", jcpool, index);
-
+       
+       cls = LLNI_classinfo_unwrap(jcpool);
        ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Methodref);
        
        if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return NULL;
        }
 
@@ -1331,15 +1422,17 @@ jobject JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject unused, jobject jcpool,
 
 jobject JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       constant_FMIref *ref;
-       classinfo *c = NULL;
-       classinfo *cls = LLNI_classinfo_unwrap(jcpool);
+       constant_FMIref *ref; /* reference to the method in constant pool at index 'index' */
+       classinfo *c = NULL;  /* resolved declaring class of the method */
+       classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetMethodAtIfLoaded: jcpool=%p, index=%d", jcpool, index);
 
+       cls = LLNI_classinfo_unwrap(jcpool);
        ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Methodref);
 
        if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return NULL;
        }
 
@@ -1359,14 +1452,16 @@ jobject JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject unused, jobject
 
 jobject JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       constant_FMIref *ref;
-       classinfo *cls = LLNI_classinfo_unwrap(jcpool);
+       constant_FMIref *ref; /* reference to the field in constant pool at index 'index' */
+       classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
        
        TRACEJVMCALLS("JVM_ConstantPoolGetFieldAt: jcpool=%p, index=%d", jcpool, index);
 
+       cls = LLNI_classinfo_unwrap(jcpool);
        ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Fieldref);
 
        if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return NULL;
        }
 
@@ -1378,15 +1473,17 @@ jobject JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject unused, jobject jcpool,
 
 jobject JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       constant_FMIref *ref;
-       classinfo *c;
-       classinfo *cls = LLNI_classinfo_unwrap(jcpool);
+       constant_FMIref *ref; /* reference to the field in constant pool at index 'index' */
+       classinfo *c;         /* resolved declaring class for the field */
+       classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetFieldAtIfLoaded: jcpool=%p, index=%d", jcpool, index);
 
+       cls = LLNI_classinfo_unwrap(jcpool);
        ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Fieldref);
 
        if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return NULL;
        }
 
@@ -1407,6 +1504,9 @@ jobject JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject unused, jobject
 jobjectArray JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
        log_println("JVM_ConstantPoolGetMemberRefInfoAt: jcpool=%p, index=%d, IMPLEMENT ME!", jcpool, index);
+
+       /* TODO: implement. (this is yet unused be OpenJDK but, so very low priority) */
+
        return NULL;
 }
 
@@ -1415,14 +1515,16 @@ jobjectArray JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject unused, job
 
 jint JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       constant_integer *ref;
-       classinfo *cls = LLNI_classinfo_unwrap(jcpool);
+       constant_integer *ref; /* reference to the int value in constant pool at index 'index' */
+       classinfo *cls;        /* classinfo of the class for which 'this' is the constant pool */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetIntAt: jcpool=%p, index=%d", jcpool, index);
 
+       cls = LLNI_classinfo_unwrap(jcpool);
        ref = (constant_integer*)class_getconstant(cls, index, CONSTANT_Integer);
 
        if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return 0;
        }
 
@@ -1434,14 +1536,16 @@ jint JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject unused, jobject jcpool, jint
 
 jlong JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       constant_long *ref;
-       classinfo *cls = LLNI_classinfo_unwrap(jcpool);
+       constant_long *ref; /* reference to the long value in constant pool at index 'index' */
+       classinfo *cls;     /* classinfo of the class for which 'this' is the constant pool */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetLongAt: jcpool=%p, index=%d", jcpool, index);
 
+       cls = LLNI_classinfo_unwrap(jcpool);
        ref = (constant_long*)class_getconstant(cls, index, CONSTANT_Long);
 
        if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return 0;
        }
 
@@ -1453,14 +1557,16 @@ jlong JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject unused, jobject jcpool, jin
 
 jfloat JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       constant_float *ref;
-       classinfo *cls = LLNI_classinfo_unwrap(jcpool);
+       constant_float *ref; /* reference to the float value in constant pool at index 'index' */
+       classinfo *cls;      /* classinfo of the class for which 'this' is the constant pool */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetFloatAt: jcpool=%p, index=%d", jcpool, index);
 
+       cls = LLNI_classinfo_unwrap(jcpool);
        ref = (constant_float*)class_getconstant(cls, index, CONSTANT_Float);
 
        if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return 0;
        }
 
@@ -1472,14 +1578,16 @@ jfloat JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject unused, jobject jcpool, j
 
 jdouble JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       constant_double *ref;
-       classinfo *cls = LLNI_classinfo_unwrap(jcpool);
+       constant_double *ref; /* reference to the double value in constant pool at index 'index' */
+       classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetDoubleAt: jcpool=%p, index=%d", jcpool, index);
 
+       cls = LLNI_classinfo_unwrap(jcpool);
        ref = (constant_double*)class_getconstant(cls, index, CONSTANT_Double);
 
        if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return 0;
        }
 
@@ -1491,14 +1599,16 @@ jdouble JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject unused, jobject jcpool,
 
 jstring JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       utf *ref;
-       classinfo *cls = LLNI_classinfo_unwrap(jcpool);
+       utf *ref;       /* utf object for the string in constant pool at index 'index' */
+       classinfo *cls; /* classinfo of the class for which 'this' is the constant pool */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetStringAt: jcpool=%p, index=%d", jcpool, index);
        
+       cls = LLNI_classinfo_unwrap(jcpool);
        ref = (utf*)class_getconstant(cls, index, CONSTANT_String);
 
        if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return NULL;
        }
 
@@ -1511,14 +1621,16 @@ jstring JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject unused, jobject jcpool,
 
 jstring JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject unused, jobject jcpool, jint index)
 {
-       utf *ref;
-       classinfo *cls = LLNI_classinfo_unwrap(jcpool);
+       utf *ref; /* utf object for the utf8 data in constant pool at index 'index' */
+       classinfo *cls; /* classinfo of the class for which 'this' is the constant pool */
 
        TRACEJVMCALLS("JVM_ConstantPoolGetUTF8At: jcpool=%p, index=%d", jcpool, index);
 
+       cls = LLNI_classinfo_unwrap(jcpool);
        ref = (utf*)class_getconstant(cls, index, CONSTANT_Utf8);
 
        if (ref == NULL) {
+               exceptions_throw_illegalargumentexception();
                return NULL;
        }
 
@@ -1548,9 +1660,8 @@ jobject JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused)
        java_handle_objectarray_t           *classes;
        java_handle_objectarray_t           *packages;
 
-#if PRINTJVM || 1
-       log_println("JVM_AssertionStatusDirectives");
-#endif
+       TRACEJVMCALLS("JVM_AssertionStatusDirectives(env=%p, unused=%p): COMPLETE ME!", env, unused);
+
        /* XXX this is not completely implemented */
 
        c = load_class_bootstrap(utf_new_char("java/lang/AssertionStatusDirectives"));
@@ -1910,9 +2021,8 @@ jint JVM_Available(jint fd, jlong *pbytes)
 
 jlong JVM_Lseek(jint fd, jlong offset, jint whence)
 {
-#if PRINTJVM
-       log_println("JVM_Lseek: fd=%d, offset=%ld, whence=%d", fd, offset, whence);
-#endif
+       TRACEJVMCALLS("JVM_Lseek(fd=%d, offset=%ld, whence=%d)", fd, offset, whence);
+
        return (jlong) lseek(fd, (off_t) offset, whence);
 }
 
@@ -1921,7 +2031,9 @@ jlong JVM_Lseek(jint fd, jlong offset, jint whence)
 
 jint JVM_SetLength(jint fd, jlong length)
 {
-       log_println("JVM_SetLength: IMPLEMENT ME!");
+       TRACEJVMCALLS("JVM_SetLength(fd=%d, length=%ld)", length);
+
+       return ftruncate(fd, length);
 }
 
 
@@ -1929,7 +2041,9 @@ jint JVM_SetLength(jint fd, jlong length)
 
 jint JVM_Sync(jint fd)
 {
-       log_println("JVM_Sync: IMPLEMENT ME!");
+       TRACEJVMCALLS("JVM_Sync(fd=%d)", fd);
+
+       return fsync(fd);
 }
 
 
@@ -1956,10 +2070,30 @@ void JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable)
 
 jboolean JVM_IsThreadAlive(JNIEnv* env, jobject jthread)
 {
-#if PRINTJVM
-       log_println("JVM_IsThreadAlive: jthread=%p", jthread);
-#endif
-       return _Jv_java_lang_Thread_isAlive((java_lang_Thread *) jthread);
+       threadobject *t;
+       bool          equal;
+       bool          result;
+
+       TRACEJVMCALLS("JVM_IsThreadAlive(env=%p, jthread=%p)", env, jthread);
+
+       /* XXX this is just a quick hack */
+
+       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
+               LLNI_equals(t->object, jthread, equal);
+
+               if (equal == true)
+                       break;
+       }
+
+       /* The threadobject is null when a thread is created in Java. The
+          priority is set later during startup. */
+
+       if (t == NULL)
+               return 0;
+
+       result = threads_thread_is_alive(t);
+
+       return result;
 }
 
 
@@ -2054,7 +2188,21 @@ jboolean JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrup
 
 jboolean JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj)
 {
-       log_println("JVM_HoldsLock: IMPLEMENT ME!");
+       java_handle_t *h;
+       bool           result;
+
+       TRACEJVMCALLS("JVM_HoldsLock(env=%p, threadClass=%p, obj=%p)", env, threadClass, obj);
+
+       h = (java_handle_t *) obj;
+
+       if (h == NULL) {
+               exceptions_throw_nullpointerexception();
+               return JNI_FALSE;
+       }
+
+       result = lock_is_held_by_current_thread(h);
+
+       return result;
 }
 
 
@@ -2116,11 +2264,21 @@ jint JVM_ClassLoaderDepth(JNIEnv *env)
 
 jstring JVM_GetSystemPackage(JNIEnv *env, jstring name)
 {
-       log_println("JVM_GetSystemPackage(env=%p, name=%p)");
-       javastring_print((java_handle_t *) name);
-       printf("\n");
+       java_handle_t *s;
+       utf *u;
+       utf *result;
 
-       return NULL;
+       TRACEJVMCALLS("JVM_GetSystemPackage(env=%p, name=%p)", env, name);
+
+/*     s = package_find(name); */
+       u = javastring_toutf(name, false);
+       result = package_find(u);
+       if (result != NULL)
+               s = javastring_new(result);
+       else
+               s = NULL;
+
+       return (jstring) s;
 }
 
 
@@ -2451,7 +2609,22 @@ jint JVM_SendTo(jint fd, char *buf, int len, int flags, struct sockaddr *to, int
 
 jint JVM_SocketAvailable(jint fd, jint *pbytes)
 {
-       log_println("JVM_SocketAvailable: IMPLEMENT ME!");
+#if defined(FIONREAD)
+       int bytes;
+
+       TRACEJVMCALLS("JVM_SocketAvailable(fd=%d, pbytes=%p)", fd, pbytes);
+
+       *pbytes = 0;
+
+       if (ioctl(fd, FIONREAD, &bytes) < 0)
+               return 0;
+
+       *pbytes = bytes;
+
+       return 1;
+#else
+# error FIONREAD not defined
+#endif
 }
 
 
@@ -2459,7 +2632,13 @@ jint JVM_SocketAvailable(jint fd, jint *pbytes)
 
 jint JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen)
 {
-       log_println("JVM_GetSockOpt: IMPLEMENT ME!");
+#if defined(HAVE_GETSOCKOPT)
+       TRACEJVMCALLS("JVM_GetSockOpt(fd=%d, level=%d, optname=%d, optval=%s, optlen=%p)", fd, level, optname, optval, optlen);
+
+       return getsockopt(fd, level, optname, optval, (socklen_t *) optlen);
+#else
+# error getsockopt not available
+#endif
 }
 
 
@@ -2467,9 +2646,13 @@ jint JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen)
 
 jint JVM_SetSockOpt(jint fd, int level, int optname, const char *optval, int optlen)
 {
+#if defined(HAVE_SETSOCKOPT)
        TRACEJVMCALLS("JVM_SetSockOpt(fd=%d, level=%d, optname=%d, optval=%s, optlen=%d)", fd, level, optname, optval, optlen);
 
        return setsockopt(fd, level, optname, optval, optlen);
+#else
+# error setsockopt not available
+#endif
 }
 
 
@@ -2557,15 +2740,7 @@ jboolean JVM_IsSupportedJNIVersion(jint version)
 {
        TRACEJVMCALLS("JVM_IsSupportedJNIVersion(version=%d)", version);
 
-       switch (version) {
-       case JNI_VERSION_1_1:
-       case JNI_VERSION_1_2:
-       case JNI_VERSION_1_4:
-       case JNI_VERSION_1_6:
-               return true;
-       default:
-               return false;
-       }
+       return jni_version_check(version);
 }
 
 
@@ -2786,10 +2961,11 @@ jobject JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args
 
 jboolean JVM_SupportsCX8()
 {
-#if PRINTJVM
-       log_println("JVM_SupportsCX8");
-#endif
-       return _Jv_java_util_concurrent_atomic_AtomicLong_VMSupportsCS8(NULL, NULL);
+       TRACEJVMCALLS("JVM_SupportsCX8()");
+
+       /* IMPLEMENT ME */
+
+       return 0;
 }