1 /* src/vm/properties.cpp - handling commandline properties
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
36 #include "mm/memory.hpp"
38 #include "native/llni.h"
40 #include "vm/class.hpp"
41 #include "vm/global.h"
42 #include "vm/method.hpp"
43 #include "vm/options.h"
45 #include "vm/properties.hpp"
46 #include "vm/string.hpp"
49 #include "vm/jit/asmpart.h"
53 * Constructor fills the properties list with default values.
55 Properties::Properties()
60 char *boot_class_path;
62 #if defined(ENABLE_JAVASE)
64 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
65 struct utsname *utsnamebuf;
69 #if defined(ENABLE_JRE_LAYOUT)
70 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
74 if (os::readlink("/proc/self/exe", p, 4095) == -1)
75 os::abort_errno("readlink failed");
77 /* We have a path like:
79 /path/to/executable/bin/java
83 /path/to/executeable/jre/bin/java
85 Now let's strip two levels. */
90 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
94 char* java_home = strdup(p);
96 /* Set the path to Java core native libraries. */
98 len = strlen(java_home) + strlen("/lib/classpath") + strlen("0");
100 char* boot_library_path = MNEW(char, len);
102 strcpy(boot_library_path, java_home);
103 strcat(boot_library_path, "/lib/classpath");
105 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
107 /* Find correct java.home. We check if there is a JRE
110 /* NOTE: We use the server VM here as it should be available on
111 all architectures. */
115 strlen("/jre/lib/"JAVA_ARCH"/server/libjvm.so") +
118 char* java_home = MNEW(char, len);
120 strcpy(java_home, p);
121 strcat(java_home, "/jre/lib/"JAVA_ARCH"/server/libjvm.so");
123 // Check if that libjvm.so exists.
124 if (os::access(java_home, F_OK) == 0) {
125 // Yes, we add /jre to java.home.
126 strcpy(java_home, p);
127 strcat(java_home, "/jre");
130 // No, java.home is parent directory.
131 strcpy(java_home, p);
134 /* Set the path to Java core native libraries. */
136 len = strlen(java_home) + strlen("/lib/"JAVA_ARCH) + strlen("0");
138 char* boot_library_path = MNEW(char, len);
140 strcpy(boot_library_path, java_home);
141 strcat(boot_library_path, "/lib/"JAVA_ARCH);
144 # error unknown classpath configuration
152 const char* java_home = CACAO_PREFIX;
154 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
156 const char* boot_library_path = JAVA_RUNTIME_LIBRARY_LIBDIR"/classpath";
158 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
160 const char* boot_library_path = JAVA_RUNTIME_LIBRARY_LIBDIR;
162 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
164 // No boot_library_path required.
167 # error unknown classpath configuration
171 put("java.home", java_home);
173 /* Set the bootclasspath. */
175 p = os::getenv("BOOTCLASSPATH");
178 boot_class_path = MNEW(char, strlen(p) + strlen("0"));
179 strcpy(boot_class_path, p);
182 #if defined(ENABLE_JRE_LAYOUT)
183 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
186 strlen(java_home) + strlen("/share/cacao/vm.zip:") +
187 strlen(java_home) + strlen("/share/classpath/glibj.zip") +
190 boot_class_path = MNEW(char, len);
192 strcpy(boot_class_path, java_home);
193 strcat(boot_class_path, "/share/cacao/vm.zip");
194 strcat(boot_class_path, ":");
195 strcat(boot_class_path, java_home);
196 strcat(boot_class_path, "/share/classpath/glibj.zip");
198 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
200 /* This is the bootclasspath taken from HotSpot (see
201 hotspot/src/share/vm/runtime/os.cpp
202 (os::set_boot_path)). */
205 strlen(java_home) + strlen("/lib/resources.jar:") +
206 strlen(java_home) + strlen("/lib/rt.jar:") +
207 strlen(java_home) + strlen("/lib/sunrsasign.jar:") +
208 strlen(java_home) + strlen("/lib/jsse.jar:") +
209 strlen(java_home) + strlen("/lib/jce.jar:") +
210 strlen(java_home) + strlen("/lib/charsets.jar:") +
211 strlen(java_home) + strlen("/classes") +
214 boot_class_path = MNEW(char, len);
216 strcpy(boot_class_path, java_home);
217 strcat(boot_class_path, "/lib/resources.jar:");
218 strcat(boot_class_path, java_home);
219 strcat(boot_class_path, "/lib/rt.jar:");
220 strcat(boot_class_path, java_home);
221 strcat(boot_class_path, "/lib/sunrsasign.jar:");
222 strcat(boot_class_path, java_home);
223 strcat(boot_class_path, "/lib/jsse.jar:");
224 strcat(boot_class_path, java_home);
225 strcat(boot_class_path, "/lib/jce.jar:");
226 strcat(boot_class_path, java_home);
227 strcat(boot_class_path, "/lib/charsets.jar:");
228 strcat(boot_class_path, java_home);
229 strcat(boot_class_path, "/classes");
232 # error unknown classpath configuration
235 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
238 strlen(CACAO_VM_ZIP) +
240 strlen(JAVA_RUNTIME_LIBRARY_CLASSES) +
243 boot_class_path = MNEW(char, len);
245 strcpy(boot_class_path, CACAO_VM_ZIP);
246 strcat(boot_class_path, ":");
247 strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_CLASSES);
249 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
251 /* This is the bootclasspath taken from HotSpot (see
252 hotspot/src/share/vm/runtime/os.cpp
253 (os::set_boot_path)). */
256 strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/resources.jar:") +
257 strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/rt.jar:") +
258 strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/sunrsasign.jar:") +
259 strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/jsse.jar:") +
260 strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/jce.jar:") +
261 strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/lib/charsets.jar:") +
262 strlen(JAVA_RUNTIME_LIBRARY_PREFIX"/classes") +
265 boot_class_path = MNEW(char, len);
267 strcpy(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/resources.jar:");
268 strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/rt.jar:");
269 strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/sunrsasign.jar:");
270 strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/jsse.jar:");
271 strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/jce.jar:");
272 strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/lib/charsets.jar:");
273 strcat(boot_class_path, JAVA_RUNTIME_LIBRARY_PREFIX"/classes");
275 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
278 strlen(JAVA_RUNTIME_LIBRARY_CLASSES) +
281 boot_class_path = MNEW(char, len);
283 strcpy(boot_class_path, JAVA_RUNTIME_LIBRARY_CLASSES);
286 # error unknown classpath configuration
291 put("sun.boot.class.path", boot_class_path);
292 put("java.boot.class.path", boot_class_path);
294 #if defined(ENABLE_JAVASE)
296 /* Set the classpath. */
298 p = os::getenv("CLASSPATH");
303 class_path = MNEW(char, strlen(p) + strlen("0"));
304 strcpy(class_path, p);
307 class_path = MNEW(char, strlen(".") + strlen("0"));
308 strcpy(class_path, ".");
311 put("java.class.path", class_path);
313 // Add java.vm properties.
314 put("java.vm.specification.version", "1.0");
315 put("java.vm.specification.vendor", "Sun Microsystems Inc.");
316 put("java.vm.specification.name", "Java Virtual Machine Specification");
317 put("java.vm.version", VERSION_FULL);
318 put("java.vm.vendor", "CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO");
319 put("java.vm.name", "CACAO");
321 # if defined(ENABLE_INTRP)
323 /* XXX We don't support java.lang.Compiler */
324 /* put("java.compiler", "cacao.intrp"); */
325 put("java.vm.info", "interpreted mode");
330 /* XXX We don't support java.lang.Compiler */
331 /* put("java.compiler", "cacao.jit"); */
332 put("java.vm.info", "compiled mode");
335 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
337 /* Get properties from system. */
339 char* cwd = os::getcwd();
341 char* env_user = os::getenv("USER");
342 char* env_home = os::getenv("HOME");
343 char* env_lang = os::getenv("LANG");
345 utsnamebuf = NEW(struct utsname);
349 put("java.runtime.version", VERSION_FULL);
350 put("java.runtime.name", "CACAO");
352 put("java.specification.version", "1.5");
353 put("java.specification.vendor", "Sun Microsystems Inc.");
354 put("java.specification.name", "Java Platform API Specification");
356 put("java.version", JAVA_VERSION);
357 put("java.vendor", "GNU Classpath");
358 put("java.vendor.url", "http://www.gnu.org/software/classpath/");
360 put("java.class.version", CLASS_VERSION);
362 put("gnu.classpath.boot.library.path", boot_library_path);
364 // Get and set java.library.path.
365 const char* java_library_path = os::getenv("LD_LIBRARY_PATH");
367 if (java_library_path == NULL)
368 java_library_path = "";
370 put("java.library.path", java_library_path);
372 put("java.io.tmpdir", "/tmp");
374 # if defined(ENABLE_INTRP)
376 put("gnu.java.compiler.name", "cacao.intrp");
381 put("gnu.java.compiler.name", "cacao.jit");
384 /* Set the java.ext.dirs property. */
386 len = strlen(java_home) + strlen("/jre/lib/ext") + strlen("0");
388 char* extdirs = MNEW(char, len);
390 sprintf(extdirs, "%s/jre/lib/ext", java_home);
392 put("java.ext.dirs", extdirs);
394 /* Set the java.ext.endorsed property. */
396 len = strlen(java_home) + strlen("/jre/lib/endorsed") + strlen("0");
398 char* endorseddirs = MNEW(char, len);
400 sprintf(endorseddirs, "%s/jre/lib/endorsed", java_home);
402 put("java.endorsed.dirs", endorseddirs);
404 # if defined(DISABLE_GC)
405 /* When we disable the GC, we mmap the whole heap to a specific
406 address, so we can compare call traces. For this reason we have
407 to add the same properties on different machines, otherwise
408 more memory may be allocated (e.g. strlen("i386")
409 vs. strlen("alpha"). */
411 put("os.arch", "unknown");
412 put("os.name", "unknown");
413 put("os.version", "unknown");
415 put("os.arch", JAVA_ARCH);
416 put("os.name", utsnamebuf->sysname);
417 put("os.version", utsnamebuf->release);
420 # if WORDS_BIGENDIAN == 1
421 put("gnu.cpu.endian", "big");
423 put("gnu.cpu.endian", "little");
426 put("file.separator", "/");
427 put("path.separator", ":");
428 put("line.separator", "\n");
430 put("user.name", env_user ? env_user : "null");
431 put("user.home", env_home ? env_home : "null");
432 put("user.dir", cwd ? cwd : "null");
436 if (env_lang != NULL) {
437 /* get the local stuff from the environment */
439 if (strlen(env_lang) <= 2) {
440 put("user.language", env_lang);
443 if ((env_lang[2] == '_') && (strlen(env_lang) >= 5)) {
444 char* lang = MNEW(char, 3);
445 strncpy(lang, (char*) &env_lang[0], 2);
448 char* country = MNEW(char, 3);
449 strncpy(country, (char*) &env_lang[3], 2);
452 put("user.language", lang);
453 put("user.country", country);
458 /* if no default locale was specified, use `en_US' */
460 put("user.language", "en");
461 put("user.country", "US");
464 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
466 /* Actually this property is set by OpenJDK, but we need it in
467 nativevm_preinit(). */
469 put("sun.boot.library.path", boot_library_path);
471 // Set the java.ext.dirs property.
473 strlen(java_home) + strlen("/lib/ext") +
475 strlen("/usr/java/packages/lib/ext") +
478 char* extdirs = MNEW(char, len);
480 sprintf(extdirs, "%s/lib/ext:/usr/java/packages/lib/ext", java_home);
482 put("java.ext.dirs", extdirs);
484 // Set the java.ext.endorsed property.
485 len = strlen(java_home) + strlen("/lib/endorsed") + strlen("0");
487 char* endorseddirs = MNEW(char, len);
489 sprintf(endorseddirs, "%s/lib/endorsed", java_home);
491 put("java.endorsed.dirs", endorseddirs);
495 # error unknown classpath configuration
499 #elif defined(ENABLE_JAVAME_CLDC1_1)
501 put("microedition.configuration", "CLDC-1.1");
502 put("microedition.platform", "generic");
503 put("microedition.encoding", "ISO8859_1");
504 put("microedition.profiles", "");
508 # error unknown Java configuration
515 * Add the given property to the given Java system properties.
517 * @param p Java properties object.
519 * @param value Value.
521 void Properties::put(java_handle_t* p, const char* key, const char* value)
523 // Get Properties.put() method to add properties.
525 LLNI_class_get(p, c);
527 methodinfo* m = class_resolveclassmethod(c,
529 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
536 // Add to the Java system properties.
537 java_handle_t* k = javastring_new_from_utf_string(key);
538 java_handle_t* v = javastring_new_from_utf_string(value);
540 (void) vm_call_method(m, p, k, v);
545 * Put the given property into the internal property map. If there's
546 * already an entry with the same key, replace it.
549 * @param value Value.
551 void Properties::put(const char* key, const char* value)
553 // Try to find the key.
554 std::map<const char*, const char*>::iterator it = _properties.find(key);
556 // The key is already in the map.
557 if (it != _properties.end()) {
559 if (opt_DebugProperties) {
560 printf("[Properties::put: key=%s, old value=%s, new value=%s]\n",
561 key, it->second, value);
565 // Replace the value in the current entry.
571 // The key was not found, insert the pair.
573 if (opt_DebugProperties) {
574 printf("[Properties::put: key=%s, value=%s]\n", key, value);
578 _properties.insert(std::make_pair(key, value));
583 * Get a property entry from the internal property map.
587 * @return Value associated with the key or NULL when not found.
589 const char* Properties::get(const char* key)
591 // Try to find the key.
592 std::map<const char*, const char*>::iterator it = _properties.find(key);
594 // The key is not in the map.
595 if (it == _properties.end())
604 * Fill the given Java system properties with all properties from the
605 * internal properties map.
607 * @param p Java Properties object.
609 #if defined(ENABLE_JAVASE)
610 void Properties::fill(java_handle_t* p)
612 // Get Properties.put() method to add properties.
614 LLNI_class_get(p, c);
616 methodinfo* m = class_resolveclassmethod(c,
618 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
625 // Iterator over all properties.
626 for (std::map<const char*, const char*>::iterator it = _properties.begin(); it != _properties.end(); it++) {
627 // Put into the Java system properties.
628 java_handle_t* key = javastring_new_from_utf_string(it->first);
629 java_handle_t* value = javastring_new_from_utf_string(it->second);
631 (void) vm_call_method(m, p, key, value);
638 * Dump all property entries.
641 void Properties::dump()
643 for (std::map<const char*, const char*>::iterator it = _properties.begin(); it != _properties.end(); it++) {
644 log_println("[Properties::dump: key=%s, value=%s]", it->first, it->second);
651 * These are local overrides for various environment variables in Emacs.
652 * Please do not remove this and leave it at the end of the file, where
653 * Emacs will automagically detect them.
654 * ---------------------------------------------------------------------
657 * indent-tabs-mode: t
661 * vim:noexpandtab:sw=4:ts=4: