This commit introduces C++ wrapper classes for Java heap objects.
[cacao.git] / src / vm / properties.c
index 54641c3fe8ab285d284a2abcb0e9df1b73f65196..599e05879ea0bb81a5ec30eef945aac3825c5417 100644 (file)
@@ -1,9 +1,7 @@
 /* src/vm/properties.c - handling commandline properties
 
-   Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
-   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
-   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
-   J. Wenninger, Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: properties.c 8399 2007-08-22 18:24:23Z twisti $
-
 */
 
 
 #include "config.h"
 
+#include <stdint.h>
+#include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
 #include <sys/utsname.h>
 
-#include "vm/types.h"
-
 #include "mm/memory.h"
 
 #include "native/jni.h"
-
-#include "vm/global.h"                      /* required by java_lang_String.h */
-#include "native/include/java_lang_String.h"
+#include "native/llni.h"
 
 #include "toolbox/list.h"
 #include "toolbox/util.h"
 
+#include "vm/global.h"
 #include "vm/properties.h"
-#include "vm/stringlocal.h"
-#include "vm/vm.h"
+#include "vm/string.hpp"
+#include "vm/vm.hpp"
 
 #include "vm/jit/asmpart.h"
 
+#include "vmcore/class.h"
 #include "vmcore/method.h"
 #include "vmcore/options.h"
+#include "vmcore/os.hpp"
 
 
 /* internal property structure ************************************************/
@@ -62,8 +59,8 @@
 typedef struct list_properties_entry_t list_properties_entry_t;
 
 struct list_properties_entry_t {
-       char       *key;
-       char       *value;
+       const char* key;
+       const char* value;
        listnode_t  linkage;
 };
 
@@ -82,6 +79,8 @@ static list_t *list_properties = NULL;
 
 void properties_init(void)
 {
+       TRACESUBSYSTEMINITIALIZATION("properties_init");
+
        list_properties = list_create(OFFSET(list_properties_entry_t, linkage));
 }
 
@@ -94,21 +93,23 @@ void properties_init(void)
 
 void properties_set(void)
 {
-#if defined(ENABLE_JAVASE)
        int             len;
        char           *p;
 
        char           *java_home;
-       char           *boot_library_path;
        char           *boot_class_path;
+
+#if defined(ENABLE_JAVASE)
        char           *class_path;
+       char           *boot_library_path;
+       char           *extdirs;
+       char           *endorseddirs;
 
-# if defined(WITH_CLASSPATH_GNU)
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
        char           *cwd;
        char           *env_user;
        char           *env_home;
        char           *env_lang;
-       char           *extdirs;
        char           *lang;
        char           *country;
        struct utsname *utsnamebuf;
@@ -117,7 +118,7 @@ void properties_set(void)
 # endif
 #endif
 
-#if defined(WITH_JRE_LAYOUT)
+#if defined(ENABLE_JRE_LAYOUT)
        /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
 
        p = MNEW(char, 4096);
@@ -125,31 +126,35 @@ void properties_set(void)
        if (readlink("/proc/self/exe", p, 4095) == -1)
                vm_abort("properties_set: readlink failed: %s\n", strerror(errno));
 
-       /* Get the path of the current executable. */
+       /* We have a path like:
 
-       p = dirname(p);
+          /path/to/executable/bin/java
 
-# if defined(WITH_CLASSPATH_GNU)
+          or
+          
+          /path/to/executeable/jre/bin/java
 
-       /* Set java.home. */
+          Now let's strip two levels. */
 
-       len = strlen(path) + strlen("/..") + strlen("0");
+       p = os_dirname(p);
+       p = os_dirname(p);
 
-       java_home = MNEW(char, len);
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
 
-       strcpy(java_home, p);
-       strcat(java_home, "/..");
+       /* Set java.home. */
+
+       java_home = strdup(p);
 
        /* Set the path to Java core native libraries. */
 
-       len = strlen(cacao_prefix) + strlen("/lib/classpath") + strlen("0");
+       len = strlen(java_home) + strlen("/lib/classpath") + strlen("0");
 
        boot_library_path = MNEW(char, len);
 
        strcpy(boot_library_path, java_home);
        strcat(boot_library_path, "/lib/classpath");
 
-# elif defined(WITH_CLASSPATH_SUN)
+# elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
 
        /* Find correct java.home.  We check if there is a JRE
           co-located. */
@@ -159,29 +164,37 @@ void properties_set(void)
 
        len =
                strlen(p) +
-               strlen("/../jre/lib/"JAVA_ARCH"/server/libjvm.so") +
+               strlen("/jre/lib/"JAVA_ARCH"/server/libjvm.so") +
                strlen("0");
 
        java_home = MNEW(char, len);
 
        strcpy(java_home, p);
-       strcat(java_home, "/../jre/lib/"JAVA_ARCH"/server/libjvm.so");
+       strcat(java_home, "/jre/lib/"JAVA_ARCH"/server/libjvm.so");
 
        /* Check if that libjvm.so exists. */
 
-       if (access(java_home, F_OK) == 0) {
+       if (os_access(java_home, F_OK) == 0) {
                /* Yes, we add /jre to java.home. */
 
                strcpy(java_home, p);
-               strcat(java_home, "/../jre");
+               strcat(java_home, "/jre");
        }
        else {
                /* No, java.home is parent directory. */
 
                strcpy(java_home, p);
-               strcat(java_home, "/..");
        }
 
+       /* Set the path to Java core native libraries. */
+
+       len = strlen(java_home) + strlen("/lib/"JAVA_ARCH) + strlen("0");
+
+       boot_library_path = MNEW(char, len);
+
+       strcpy(boot_library_path, java_home);
+       strcat(boot_library_path, "/lib/"JAVA_ARCH);
+
 # else
 #  error unknown classpath configuration
 # endif
@@ -193,10 +206,20 @@ void properties_set(void)
 #else
        java_home         = CACAO_PREFIX;
 
-# if defined(WITH_CLASSPATH_GNU)
-       boot_library_path = CLASSPATH_LIBDIR"/classpath";
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+
+       boot_library_path = JAVA_RUNTIME_LIBRARY_LIBDIR"/classpath";
+
+# elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
+
+       boot_library_path = JAVA_RUNTIME_LIBRARY_LIBDIR;
+
+# elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
+
+       /* No boot_library_path required. */
+
 # else
-       boot_library_path = CLASSPATH_LIBDIR;
+#  error unknown classpath configuration
 # endif
 #endif
 
@@ -211,8 +234,8 @@ void properties_set(void)
                strcpy(boot_class_path, p);
        }
        else {
-#if defined(WITH_JRE_LAYOUT)
-# if defined(WITH_CLASSPATH_GNU)
+#if defined(ENABLE_JRE_LAYOUT)
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
 
                len =
                        strlen(java_home) + strlen("/share/cacao/vm.zip:") +
@@ -227,7 +250,7 @@ void properties_set(void)
                strcat(boot_class_path, java_home);
                strcat(boot_class_path, "/share/classpath/glibj.zip");
 
-# elif defined(WITH_CLASSPATH_SUN)
+# elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
 
                /* This is the bootclasspath taken from HotSpot (see
                   hotspot/src/share/vm/runtime/os.cpp
@@ -264,55 +287,55 @@ void properties_set(void)
 #  error unknown classpath configuration
 # endif
 #else
-# if defined(WITH_CLASSPATH_GNU)
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
 
                len =
                        strlen(CACAO_VM_ZIP) +
                        strlen(":") +
-                       strlen(CLASSPATH_CLASSES) +
+                       strlen(JAVA_RUNTIME_LIBRARY_CLASSES) +
                        strlen("0");
 
                boot_class_path = MNEW(char, len);
 
                strcpy(boot_class_path, CACAO_VM_ZIP);
                strcat(boot_class_path, ":");
-               strcat(boot_class_path, CLASSPATH_CLASSES);
+               strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_CLASSES);
 
-# elif defined(WITH_CLASSPATH_SUN)
+# elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
 
                /* This is the bootclasspath taken from HotSpot (see
                   hotspot/src/share/vm/runtime/os.cpp
                   (os::set_boot_path)). */
 
                len =
-                       strlen(CLASSPATH_PREFIX"/lib/resources.jar:") +
-                       strlen(CLASSPATH_PREFIX"/lib/rt.jar:") +
-                       strlen(CLASSPATH_PREFIX"/lib/sunrsasign.jar:") +
-                       strlen(CLASSPATH_PREFIX"/lib/jsse.jar:") +
-                       strlen(CLASSPATH_PREFIX"/lib/jce.jar:") +
-                       strlen(CLASSPATH_PREFIX"/lib/charsets.jar:") +
-                       strlen(CLASSPATH_PREFIX"/classes") +
+                       strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/resources.jar:") +
+                       strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/rt.jar:") +
+                       strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/sunrsasign.jar:") +
+                       strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/jsse.jar:") +
+                       strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/jce.jar:") +
+                       strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/charsets.jar:") +
+                       strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/classes") +
                        strlen("0");
 
                boot_class_path = MNEW(char, len);
 
-               strcpy(boot_class_path, CLASSPATH_PREFIX"/lib/resources.jar:");
-               strcat(boot_class_path, CLASSPATH_PREFIX"/lib/rt.jar:");
-               strcat(boot_class_path, CLASSPATH_PREFIX"/lib/sunrsasign.jar:");
-               strcat(boot_class_path, CLASSPATH_PREFIX"/lib/jsse.jar:");
-               strcat(boot_class_path, CLASSPATH_PREFIX"/lib/jce.jar:");
-               strcat(boot_class_path, CLASSPATH_PREFIX"/lib/charsets.jar:");
-               strcat(boot_class_path, CLASSPATH_PREFIX"/classes");
+               strcpy(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/resources.jar:");
+               strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/rt.jar:");
+               strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/sunrsasign.jar:");
+               strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/jsse.jar:");
+               strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/jce.jar:");
+               strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/charsets.jar:");
+               strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/classes");
 
-# elif defined(WITH_CLASSPATH_CLDC1_1)
+# elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
 
                len =
-                       strlen(CLASSPATH_CLASSES) +
+                       strlen(JAVA_RUNTIME_LIBRARY_CLASSES) +
                        strlen("0");
 
                boot_class_path = MNEW(char, len);
 
-               strcat(boot_class_path, CLASSPATH_CLASSES);
+               strcpy(boot_class_path, JAVA_RUNTIME_LIBRARY_CLASSES);
 
 # else
 #  error unknown classpath configuration
@@ -320,8 +343,10 @@ void properties_set(void)
 #endif
        }
 
-       properties_add("java.boot.class.path", boot_class_path);
        properties_add("sun.boot.class.path", boot_class_path);
+       properties_add("java.boot.class.path", boot_class_path);
+
+#if defined(ENABLE_JAVASE)
 
        /* Set the classpath. */
 
@@ -338,19 +363,13 @@ void properties_set(void)
 
        properties_add("java.class.path", class_path);
 
-#if defined(ENABLE_JAVASE)
-
-       /* get properties from system */
-
-       p = getenv("JAVA_HOME");
-
-       /* fill in system properties */
+       /* Add java.vm properties. */
 
        properties_add("java.vm.specification.version", "1.0");
        properties_add("java.vm.specification.vendor", "Sun Microsystems Inc.");
        properties_add("java.vm.specification.name", "Java Virtual Machine Specification");
        properties_add("java.vm.version", VERSION);
-       properties_add("java.vm.vendor", "CACAO Team");
+       properties_add("java.vm.vendor", "CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO");
        properties_add("java.vm.name", "CACAO");
 
 # if defined(ENABLE_INTRP)
@@ -367,7 +386,7 @@ void properties_set(void)
                properties_add("java.vm.info", "JIT mode");
        }
 
-# if defined(WITH_CLASSPATH_GNU)
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
 
        /* Get properties from system. */
 
@@ -394,10 +413,6 @@ void properties_set(void)
 
        properties_add("java.class.version", CLASS_VERSION);
 
-#  if defined(WITH_STATIC_CLASSPATH)
-       properties_add("gnu.classpath.boot.library.path", ".");
-       properties_add("java.library.path" , ".");
-#  else
        properties_add("gnu.classpath.boot.library.path", boot_library_path);
 
        /* Get and set java.library.path. */
@@ -408,7 +423,6 @@ void properties_set(void)
                java_library_path = "";
 
        properties_add("java.library.path", java_library_path);
-#  endif
 
        properties_add("java.io.tmpdir", "/tmp");
 
@@ -422,18 +436,25 @@ void properties_set(void)
                properties_add("gnu.java.compiler.name", "cacao.jit");
        }
 
-       /* set the java.ext.dirs property */
+       /* Set the java.ext.dirs property. */
 
        len = strlen(java_home) + strlen("/jre/lib/ext") + strlen("0");
 
        extdirs = MNEW(char, len);
 
-       strcpy(extdirs, java_home);
-       strcat(extdirs, "/jre/lib/ext");
+       sprintf(extdirs, "%s/jre/lib/ext", java_home);
 
        properties_add("java.ext.dirs", extdirs);
 
-       properties_add("java.endorsed.dirs", ""CACAO_PREFIX"/jre/lib/endorsed");
+       /* Set the java.ext.endorsed property. */
+
+       len = strlen(java_home) + strlen("/jre/lib/endorsed") + strlen("0");
+
+       endorseddirs = MNEW(char, len);
+
+       sprintf(endorseddirs, "%s/jre/lib/endorsed", java_home);
+
+       properties_add("java.endorsed.dirs", endorseddirs);
 
 #  if defined(DISABLE_GC)
        /* When we disable the GC, we mmap the whole heap to a specific
@@ -451,17 +472,10 @@ void properties_set(void)
        properties_add("os.version", utsnamebuf->release);
 #  endif
 
-#  if defined(WITH_STATIC_CLASSPATH)
-       /* This is just for debugging purposes and can cause troubles in
-       GNU Classpath. */
-
-       properties_add("gnu.cpu.endian", "unknown");
-#  else
-#   if WORDS_BIGENDIAN == 1
+#  if WORDS_BIGENDIAN == 1
        properties_add("gnu.cpu.endian", "big");
-#   else
+#  else
        properties_add("gnu.cpu.endian", "little");
-#   endif
 #  endif
 
        properties_add("file.separator", "/");
@@ -502,9 +516,36 @@ void properties_set(void)
                properties_add("user.country", "US");
        }
 
-# elif defined(WITH_CLASSPATH_SUN)
+# elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
+
+       /* Actually this property is set by OpenJDK, but we need it in
+          nativevm_preinit(). */
+
+       properties_add("sun.boot.library.path", boot_library_path);
+
+       /* Set the java.ext.dirs property. */
+
+       len =
+               strlen(java_home) + strlen("/lib/ext") +
+               strlen(":") +
+               strlen("/usr/java/packages/lib/ext") +
+               strlen("0");
+
+       extdirs = MNEW(char, len);
+
+       sprintf(extdirs, "%s/lib/ext:/usr/java/packages/lib/ext", java_home);
+
+       properties_add("java.ext.dirs", extdirs);
+
+       /* Set the java.ext.endorsed property. */
+
+       len = strlen(java_home) + strlen("/lib/endorsed") + strlen("0");
+
+       endorseddirs = MNEW(char, len);
 
-       /* Nothing to do. */
+       sprintf(endorseddirs, "%s/lib/endorsed", java_home);
+
+       properties_add("java.endorsed.dirs", endorseddirs);
 
 # else
 
@@ -534,17 +575,24 @@ void properties_set(void)
 
 *******************************************************************************/
 
-void properties_add(char *key, char *value)
+void properties_add(const char *key, const char *value)
 {
        list_properties_entry_t *pe;
 
        /* search for the entry */
 
-       for (pe = list_first_unsynced(list_properties); pe != NULL;
-                pe = list_next_unsynced(list_properties, pe)) {
+       for (pe = list_first(list_properties); pe != NULL;
+                pe = list_next(list_properties, pe)) {
                if (strcmp(pe->key, key) == 0) {
                        /* entry was found, replace the value */
 
+#if !defined(NDEBUG)
+                       if (opt_DebugProperties) {
+                               printf("[properties_add: key=%s, old value=%s, new value=%s]\n",
+                                          key, pe->value, value);
+                       }
+#endif
+
                        pe->value = value;
 
                        return;
@@ -553,12 +601,18 @@ void properties_add(char *key, char *value)
 
        /* entry was not found, insert a new one */
 
+#if !defined(NDEBUG)
+       if (opt_DebugProperties) {
+               printf("[properties_add: key=%s, value=%s]\n", key, value);
+       }
+#endif
+
        pe = NEW(list_properties_entry_t);
 
        pe->key   = key;
        pe->value = value;
 
-       list_add_last_unsynced(list_properties, pe);
+       list_add_last(list_properties, pe);
 }
 
 
@@ -568,12 +622,12 @@ void properties_add(char *key, char *value)
 
 *******************************************************************************/
 
-char *properties_get(char *key)
+const char *properties_get(const char *key)
 {
        list_properties_entry_t *pe;
 
-       for (pe = list_first_unsynced(list_properties); pe != NULL;
-                pe = list_next_unsynced(list_properties, pe)) {
+       for (pe = list_first(list_properties); pe != NULL;
+                pe = list_next(list_properties, pe)) {
                if (strcmp(pe->key, key) == 0)
                        return pe->value;
        }
@@ -588,15 +642,18 @@ char *properties_get(char *key)
 
 *******************************************************************************/
 
-void properties_system_add(java_handle_t *p, char *key, char *value)
+void properties_system_add(java_handle_t *p, const char *key, const char *value)
 {
+       classinfo     *c;
        methodinfo    *m;
        java_handle_t *k;
        java_handle_t *v;
 
        /* search for method to add properties */
 
-       m = class_resolveclassmethod(p->vftbl->class,
+       LLNI_class_get(p, c);
+
+       m = class_resolveclassmethod(c,
                                                                 utf_put,
                                                                 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
                                                                 NULL,
@@ -628,13 +685,16 @@ void properties_system_add(java_handle_t *p, char *key, char *value)
 void properties_system_add_all(java_handle_t *p)
 {
        list_properties_entry_t *pe;
+       classinfo               *c;
        methodinfo              *m;
        java_handle_t           *key;
        java_handle_t           *value;
 
        /* search for method to add properties */
 
-       m = class_resolveclassmethod(p->vftbl->class,
+       LLNI_class_get(p, c);
+
+       m = class_resolveclassmethod(c,
                                                                 utf_put,
                                                                 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
                                                                 NULL,
@@ -658,6 +718,27 @@ void properties_system_add_all(java_handle_t *p)
 #endif /* defined(ENABLE_JAVASE) */
 
 
+/* properties_dump *************************************************************
+
+   Dump all property entries.
+
+*******************************************************************************/
+
+void properties_dump(void)
+{
+       list_t                  *l;
+       list_properties_entry_t *pe;
+
+       /* For convenience. */
+
+       l = list_properties;
+
+       for (pe = list_first(l); pe != NULL; pe = list_next(l, pe)) {
+               log_println("[properties_dump: key=%s, value=%s]", pe->key, pe->value);
+       }
+}
+
+
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where