* src/vm/jit/powerpc64/linux/md-abi.h: fixed linkage area defines for 64bit abi.
[cacao.git] / src / native / native.c
index 0a6cfdf63c1331d36b4b788319a3e4a120fe7a4e..2b55c1d7edb66a153e280df302a6c758622762e3 100644 (file)
@@ -30,7 +30,7 @@
 
    Changes: Christian Thalinger
 
-   $Id: native.c 4357 2006-01-22 23:33:38Z twisti $
+   $Id: native.c 5200 2006-07-31 16:30:33Z twisti $
 
 */
 
 
 #include <assert.h>
 
-#if !defined(ENABLE_STATICVM)
+#if !defined(WITH_STATIC_CLASSPATH)
 # include <ltdl.h>
 #endif
 
 #include "vm/types.h"
 
-#include "cacao/cacao.h"
 #include "mm/memory.h"
 #include "native/jni.h"
 #include "native/native.h"
 #include "native/include/java_lang_Throwable.h"
+
+#if defined(ENABLE_THREADS)
+# include "threads/native/lock.h"
+#else
+# include "threads/none/lock.h"
+#endif
+
 #include "toolbox/logging.h"
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/options.h"
 #include "vm/resolve.h"
 #include "vm/stringlocal.h"
+#include "vm/vm.h"
 #include "vm/jit/asmpart.h"
 #include "vm/jit/jit.h"
 
+#if defined(ENABLE_JVMTI)
+#include "native/jvmti/cacaodbg.h"
+#endif
+
 
 /* include table of native functions ******************************************/
 
@@ -72,6 +83,9 @@
 
 #include "native/include/gnu_classpath_VMStackWalker.h"
 #include "native/include/gnu_classpath_VMSystemProperties.h"
+#include "native/include/gnu_java_lang_management_VMClassLoadingMXBeanImpl.h"
+#include "native/include/gnu_java_lang_management_VMMemoryMXBeanImpl.h"
+#include "native/include/gnu_java_lang_management_VMRuntimeMXBeanImpl.h"
 #include "native/include/java_lang_Class.h"
 #include "native/include/java_lang_Object.h"
 #include "native/include/java_lang_VMClass.h"
 #include "native/include/java_lang_VMSystem.h"
 #include "native/include/java_lang_VMThread.h"
 #include "native/include/java_lang_VMThrowable.h"
+#include "native/include/java_lang_management_VMManagementFactory.h"
 #include "native/include/java_lang_reflect_Constructor.h"
 #include "native/include/java_lang_reflect_Field.h"
 #include "native/include/java_lang_reflect_Method.h"
 #include "native/include/java_lang_reflect_VMProxy.h"
 #include "native/include/java_security_VMAccessController.h"
 
-#if defined(ENABLE_STATICVM)
+#if defined(ENABLE_JVMTI)
+#include "native/include/gnu_classpath_jdwp_event_EventRequest.h"
+#include "native/include/java_nio_ByteBuffer.h"
+#include "native/include/gnu_classpath_jdwp_VMVirtualMachine.h"
+#include "native/include/gnu_classpath_jdwp_VMFrame.h"
+#include "native/include/gnu_classpath_jdwp_VMMethod.h"
+#endif
+
+#if defined(WITH_STATIC_CLASSPATH)
 
 /* these are required to prevent compiler warnings */
 
 
 #include "native/nativetable.inc"
 
-#else /* defined(ENABLE_STATICVM) */
+#else /* defined(WITH_STATIC_CLASSPATH) */
 
-/* Ensure that symbols for functions implemented within CACAO are used and    */
-/* exported to dlopen.                                                        */
+/* Ensure that symbols for functions implemented within CACAO are used
+   and exported to dlopen. */
 
 static functionptr dummynativetable[] = {
        (functionptr) Java_gnu_classpath_VMStackWalker_getClassContext,
 
        (functionptr) Java_gnu_classpath_VMSystemProperties_preInit,
 
+       (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_getLoadedClassCount,
+       (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_getUnloadedClassCount,
+       (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_isVerbose,
+       (functionptr) Java_gnu_java_lang_management_VMClassLoadingMXBeanImpl_setVerbose,
+
+       (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getHeapMemoryUsage,
+       (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getNonHeapMemoryUsage,
+       (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_getObjectPendingFinalizationCount,
+       (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_isVerbose,
+       (functionptr) Java_gnu_java_lang_management_VMMemoryMXBeanImpl_setVerbose,
+
+       (functionptr) Java_gnu_java_lang_management_VMRuntimeMXBeanImpl_getInputArguments,
+       (functionptr) Java_gnu_java_lang_management_VMRuntimeMXBeanImpl_getStartTime,
+
        (functionptr) Java_java_lang_VMClass_isInstance,
        (functionptr) Java_java_lang_VMClass_isAssignableFrom,
        (functionptr) Java_java_lang_VMClass_isInterface,
@@ -186,10 +223,14 @@ static functionptr dummynativetable[] = {
        (functionptr) Java_java_lang_VMThrowable_fillInStackTrace,
        (functionptr) Java_java_lang_VMThrowable_getStackTrace,
 
-       (functionptr) Java_java_lang_reflect_Constructor_getModifiers,
+       (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryPoolNames,
+       (functionptr) Java_java_lang_management_VMManagementFactory_getMemoryManagerNames,
+       (functionptr) Java_java_lang_management_VMManagementFactory_getGarbageCollectorNames,
+
+       (functionptr) Java_java_lang_reflect_Constructor_getModifiersInternal,
        (functionptr) Java_java_lang_reflect_Constructor_constructNative,
 
-       (functionptr) Java_java_lang_reflect_Field_getModifiers,
+       (functionptr) Java_java_lang_reflect_Field_getModifiersInternal,
        (functionptr) Java_java_lang_reflect_Field_getType,
        (functionptr) Java_java_lang_reflect_Field_get,
        (functionptr) Java_java_lang_reflect_Field_getBoolean,
@@ -210,7 +251,7 @@ static functionptr dummynativetable[] = {
        (functionptr) Java_java_lang_reflect_Field_setFloat,
        (functionptr) Java_java_lang_reflect_Field_setDouble,
 
-       (functionptr) Java_java_lang_reflect_Method_getModifiers,
+       (functionptr) Java_java_lang_reflect_Method_getModifiersInternal,
        (functionptr) Java_java_lang_reflect_Method_getReturnType,
        (functionptr) Java_java_lang_reflect_Method_getParameterTypes,
        (functionptr) Java_java_lang_reflect_Method_getExceptionTypes,
@@ -221,14 +262,43 @@ static functionptr dummynativetable[] = {
        (functionptr) Java_java_lang_reflect_VMProxy_generateProxyClass,
 
        (functionptr) Java_java_security_VMAccessController_getStack,
+
+#if defined(ENABLE_JVMTI)
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_suspendThread,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_resumeThread,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSuspendCount,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClassesCount,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassStatus,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllClassMethods,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getClassMethod,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrames,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrame,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getFrameCount,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getThreadStatus,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getLoadRequests,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_executeMethod,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getSourceFile,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_registerEvent,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_unregisterEvent,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_clearEvents,
+       (functionptr) Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClasses,
+       (functionptr) Java_gnu_classpath_jdwp_VMFrame_setValue,
+       (functionptr) Java_gnu_classpath_jdwp_VMFrame_getValue,
+       (functionptr) Java_gnu_classpath_jdwp_VMMethod_getName,
+       (functionptr) Java_gnu_classpath_jdwp_VMMethod_getSignature,
+       (functionptr) Java_gnu_classpath_jdwp_VMMethod_getModifiers,
+       (functionptr) Java_gnu_classpath_jdwp_VMMethod_getLineTable,
+       (functionptr) Java_gnu_classpath_jdwp_VMMethod_getVariableTable
+#endif
+
 };
 
-#endif /* defined(ENABLE_STATICVM) */
+#endif /* defined(WITH_STATIC_CLASSPATH) */
 
 
 /* tables for methods *********************************************************/
 
-#ifdef ENABLE_STATICVM
+#if defined(WITH_STATIC_CLASSPATH)
 #define NATIVETABLESIZE  (sizeof(nativetable)/sizeof(struct nativeref))
 
 /* table for fast string comparison */
@@ -241,8 +311,8 @@ static bool nativecompdone = false;
 
 /* global variables ***********************************************************/
 
-#if !defined(ENABLE_STATICVM)
-static hashtable hashtable_library;
+#if !defined(WITH_STATIC_CLASSPATH)
+static hashtable *hashtable_library;
 static lt_dlhandle mainhandle;
 #endif
 
@@ -255,12 +325,12 @@ static lt_dlhandle mainhandle;
 
 bool native_init(void)
 {
-#if !defined(ENABLE_STATICVM)
+#if !defined(WITH_STATIC_CLASSPATH)
        void *p;
 
-       /* We need to access the dummy native table, not only to remove a warning */
-       /* but to be sure that the table is not optimized away (gcc does this     */
-       /* since 3.4).                                                            */
+       /* We need to access the dummy native table, not only to remove a
+          warning but to be sure that the table is not optimized away
+          (gcc does this since 3.4). */
 
        p = &dummynativetable;
 
@@ -280,8 +350,9 @@ bool native_init(void)
 
        /* initialize library hashtable, 10 entries should be enough */
 
-       hashtable_create(&hashtable_library, 10);
+       hashtable_library = NEW(hashtable);
 
+       hashtable_create(hashtable_library, 10);
 #endif
 
        /* everything's ok */
@@ -296,7 +367,7 @@ bool native_init(void)
 
 *******************************************************************************/
 
-#if !defined(ENABLE_STATICVM)
+#if !defined(WITH_STATIC_CLASSPATH)
 void native_hashtable_library_add(utf *filename, java_objectheader *loader,
                                                                  lt_dlhandle handle)
 {
@@ -305,11 +376,13 @@ void native_hashtable_library_add(utf *filename, java_objectheader *loader,
        u4   key;                           /* hashkey                            */
        u4   slot;                          /* slot in hashtable                  */
 
+       LOCK_MONITOR_ENTER(hashtable_library->header);
+
        /* normally addresses are aligned to 4, 8 or 16 bytes */
 
        key  = ((u4) (ptrint) loader) >> 4;        /* align to 16-byte boundaries */
-       slot = key & (hashtable_library.size - 1);
-       le   = hashtable_library.ptr[slot];
+       slot = key & (hashtable_library->size - 1);
+       le   = hashtable_library->ptr[slot];
 
        /* search external hash chain for the entry */
 
@@ -322,21 +395,21 @@ void native_hashtable_library_add(utf *filename, java_objectheader *loader,
 
        /* no loader found? create a new entry */
 
-       if (!le) {
+       if (le == NULL) {
                le = NEW(hashtable_library_loader_entry);
 
-               le->loader = loader;
+               le->loader   = loader;
                le->namelink = NULL;
 
                /* insert entry into hashtable */
 
                le->hashlink =
-                       (hashtable_library_loader_entry *) hashtable_library.ptr[slot];
-               hashtable_library.ptr[slot] = le;
+                       (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
+               hashtable_library->ptr[slot] = le;
 
                /* update number of hashtable-entries */
 
-               hashtable_library.entries++;
+               hashtable_library->entries++;
        }
 
 
@@ -345,8 +418,11 @@ void native_hashtable_library_add(utf *filename, java_objectheader *loader,
        ne = le->namelink;
 
        while (ne) {
-               if (ne->name == filename)
+               if (ne->name == filename) {
+                       LOCK_MONITOR_EXIT(hashtable_library->header);
+
                        return;
+               }
 
                ne = ne->hashlink;                  /* next element in external chain */
        }
@@ -355,15 +431,17 @@ void native_hashtable_library_add(utf *filename, java_objectheader *loader,
 
        ne = NEW(hashtable_library_name_entry);
 
-       ne->name = filename;
+       ne->name   = filename;
        ne->handle = handle;
 
        /* insert entry into external chain */
 
        ne->hashlink = le->namelink;
        le->namelink = ne;
+
+       LOCK_MONITOR_EXIT(hashtable_library->header);
 }
-#endif /* !defined(ENABLE_STATICVM) */
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
 /* native_hashtable_library_find ***********************************************
@@ -372,7 +450,7 @@ void native_hashtable_library_add(utf *filename, java_objectheader *loader,
 
 *******************************************************************************/
 
-#if !defined(ENABLE_STATICVM)
+#if !defined(WITH_STATIC_CLASSPATH)
 hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
                                                                                                                        java_objectheader *loader)
 {
@@ -384,8 +462,8 @@ hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
        /* normally addresses are aligned to 4, 8 or 16 bytes */
 
        key  = ((u4) (ptrint) loader) >> 4;        /* align to 16-byte boundaries */
-       slot = key & (hashtable_library.size - 1);
-       le   = hashtable_library.ptr[slot];
+       slot = key & (hashtable_library->size - 1);
+       le   = hashtable_library->ptr[slot];
 
        /* search external hash chain for the entry */
 
@@ -416,7 +494,7 @@ hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
 
        return ne;
 }
-#endif /* !defined(ENABLE_STATICVM) */
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
 /* native_findfunction *********************************************************
@@ -430,7 +508,7 @@ hashtable_library_name_entry *native_hashtable_library_find(utf *filename,
 
 *******************************************************************************/
 
-#if defined(ENABLE_STATICVM)
+#if defined(WITH_STATIC_CLASSPATH)
 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
                                                                bool isstatic)
 {
@@ -475,7 +553,7 @@ functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
 
        return NULL;
 }
-#endif /* defined(ENABLE_STATICVM) */
+#endif /* defined(WITH_STATIC_CLASSPATH) */
 
 
 /* native_make_overloaded_function *********************************************
@@ -484,7 +562,7 @@ functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
 
 *******************************************************************************/
 
-#if !defined(ENABLE_STATICVM)
+#if !defined(WITH_STATIC_CLASSPATH)
 static char *native_make_overloaded_function(char *name, utf *desc)
 {
        char *newname;
@@ -608,16 +686,16 @@ functionptr native_resolve_function(methodinfo *m)
 
        if (opt_verbosejni) {
                printf("[Dynamic-linking native method ");
-               utf_display_classname(m->class->name);
+               utf_display_printable_ascii_classname(m->class->name);
                printf(".");
-               utf_display(m->name);
+               utf_display_printable_ascii(m->name);
                printf(" ... ");
        }
                
        /* calculate length of native function name */
 
-       namelen = strlen("Java_") + utf_strlen(m->class->name) + strlen("_") +
-               utf_strlen(m->name) + strlen("0");
+       namelen = strlen("Java_") + utf_get_number_of_u2s(m->class->name) + strlen("_") +
+               utf_get_number_of_u2s(m->name) + strlen("0");
 
        /* check for underscores in class name */
 
@@ -691,28 +769,28 @@ functionptr native_resolve_function(methodinfo *m)
 
        newname = native_make_overloaded_function(name, m->descriptor);
 
-       /* check the library hash entries of the classloader of the methods's     */
-       /* class                                                                  */
+       /* check the library hash entries of the classloader of the
+          methods's class  */
 
        sym = NULL;
 
        /* normally addresses are aligned to 4, 8 or 16 bytes */
 
        key  = ((u4) (ptrint) m->class->classloader) >> 4;    /* align to 16-byte */
-       slot = key & (hashtable_library.size - 1);
-       le   = hashtable_library.ptr[slot];
+       slot = key & (hashtable_library->size - 1);
+       le   = hashtable_library->ptr[slot];
 
        /* iterate through loaders in this hash slot */
 
-       while (le && !sym) {
+       while ((le != NULL) && (sym == NULL)) {
                /* iterate through names in this loader */
 
                ne = le->namelink;
                        
-               while (ne && !sym) {
+               while ((ne != NULL) && (sym == NULL)) {
                        sym = lt_dlsym(ne->handle, name);
 
-                       if (!sym)
+                       if (sym == NULL)
                                sym = lt_dlsym(ne->handle, newname);
 
                        ne = ne->hashlink;
@@ -721,32 +799,41 @@ functionptr native_resolve_function(methodinfo *m)
                le = le->hashlink;
        }
 
-       if (sym)
+       if (sym != NULL)
                if (opt_verbosejni)
                        printf("JNI ]\n");
 
 
-       /* if not found, try to find the native function symbol in the main       */
-       /* program                                                                */
+       /* If not found, try to find the native function symbol in the
+          main program. */
 
-       if (!sym) {
+       if (sym == NULL) {
                sym = lt_dlsym(mainhandle, name);
 
-               if (!sym)
+               if (sym == NULL)
                        sym = lt_dlsym(mainhandle, newname);
 
-               if (sym)
+               if (sym != NULL)
                        if (opt_verbosejni)
                                printf("internal ]\n");
        }
 
 
+#if defined(ENABLE_JVMTI)
+       /* fire Native Method Bind event */
+       if (jvmti) jvmti_NativeMethodBind(m, sym, &sym);
+#endif
+
        /* no symbol found? throw exception */
 
-       if (!sym)
+       if (sym == NULL) {
+               if (opt_verbosejni)
+                       printf("failed ]\n");
+
                *exceptionptr =
                        new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
                                                                         m->name);
+       }
 
        /* release memory */
 
@@ -754,7 +841,7 @@ functionptr native_resolve_function(methodinfo *m)
 
        return (functionptr) (ptrint) sym;
 }
-#endif /* !defined(ENABLE_STATICVM) */
+#endif /* !defined(WITH_STATIC_CLASSPATH) */
 
 
 /* native_new_and_init *********************************************************
@@ -791,7 +878,7 @@ java_objectheader *native_new_and_init(classinfo *c)
 
        /* call initializer */
 
-       ASM_CALLJAVAFUNCTION(m, o, NULL, NULL, NULL);
+       (void) vm_call_method(m, o);
 
        return o;
 }
@@ -827,7 +914,7 @@ java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
 
        /* call initializer */
 
-       ASM_CALLJAVAFUNCTION(m, o, s, NULL, NULL);
+       (void) vm_call_method(m, o, s);
 
        return o;
 }
@@ -859,7 +946,7 @@ java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
 
        /* call initializer */
 
-       ASM_CALLJAVAFUNCTION(m, o, (void *) (ptrint) i, NULL, NULL);
+       (void) vm_call_method(m, o, i);
 
        return o;
 }
@@ -891,7 +978,7 @@ java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwab
 
        /* call initializer */
 
-       ASM_CALLJAVAFUNCTION(m, o, t, NULL, NULL);
+       (void) vm_call_method(m, o, t);
 
        return o;
 }