1 /* native/vm/VMRuntime.c - java/lang/VMRuntime
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Roman Obermaiser
29 Changes: Joseph Wenninger
32 $Id: VMRuntime.c 1825 2004-12-29 12:39:46Z twisti $
42 #include <sys/utsname.h>
44 #if defined(__DARWIN__)
45 # include <mach/mach.h>
48 #if !defined(STATIC_CLASSPATH)
52 #include "cacao/cacao.h"
54 #include "mm/memory.h"
55 #include "native/jni.h"
56 #include "native/native.h"
57 #include "native/include/java_io_File.h"
58 #include "native/include/java_lang_String.h"
59 #include "native/include/java_lang_Process.h"
60 #include "native/include/java_util_Properties.h" /* java_lang_VMRuntime.h */
61 #include "native/include/java_lang_VMRuntime.h"
62 #include "toolbox/logging.h"
63 #include "vm/builtin.h"
64 #include "vm/exceptions.h"
65 #include "vm/loader.h"
66 #include "vm/tables.h"
67 #include "vm/jit/asmpart.h"
70 /* this should work on BSD */
72 #if defined(__DARWIN__)
73 #include <sys/sysctl.h>
79 /* should we run all finalizers on exit? */
80 static bool finalizeOnExit = false;
82 /* temporary property structure */
84 typedef struct property property;
92 static property *properties = NULL;
95 /* create_property *************************************************************
97 Create a property entry for a command line property definition.
99 *******************************************************************************/
101 void create_property(char *key, char *value)
108 p->next = properties;
113 /* insert_property *************************************************************
115 Used for inserting a property into the system's properties table. Method m
116 (usually put) and the properties table must be given.
118 *******************************************************************************/
120 static void insert_property(methodinfo *m, java_util_Properties *p, char *key,
123 asm_calljavafunction(m,
125 javastring_new_char(key),
126 javastring_new_char(value),
132 * Class: java_lang_VMRuntime
133 * Method: execInternal
134 * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
136 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_VMRuntime_execInternal(JNIEnv *env, jclass clazz, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
138 log_text("Java_java_lang_Runtime_execInternal called");
145 * Class: java/lang/VMRuntime
146 * Method: exitInternal
149 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
154 cacao_shutdown(par1);
159 * Class: java/lang/Runtime
163 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
165 return gc_get_free_bytes();
170 * Class: java/lang/Runtime
174 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
181 * Class: java/lang/Runtime
182 * Method: runFinalization
185 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
187 gc_invoke_finalizers();
192 * Class: java/lang/Runtime
193 * Method: runFinalizersOnExit
196 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
201 finalizeOnExit = value;
206 * Class: java/lang/Runtime
207 * Method: runFinalizationsForExit
210 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
212 /* if (finalizeOnExit) { */
214 /* gc_finalize_all(); */
216 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
217 /*gc_finalize_all();*/
218 /*gc_invoke_finalizers();*/
224 * Class: java/lang/Runtime
225 * Method: totalMemory
228 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
230 return gc_get_heap_size();
235 * Class: java/lang/Runtime
236 * Method: traceInstructions
239 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
246 * Class: java/lang/Runtime
247 * Method: traceMethodCalls
250 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
257 * Class: java_lang_Runtime
258 * Method: availableProcessors
261 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
263 #if defined(_SC_NPROC_ONLN)
264 return (s4) sysconf(_SC_NPROC_ONLN);
266 #elif defined(_SC_NPROCESSORS_ONLN)
267 return (s4) sysconf(_SC_NPROCESSORS_ONLN);
269 #elif defined(__DARWIN__)
270 /* this should work in BSD */
272 int ncpu, mib[2], rc;
278 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
283 host_basic_info_data_t hinfo;
284 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
287 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
288 (host_info_t) &hinfo, &hinfo_count);
290 if (rc != KERN_SUCCESS) {
294 return (s4) hinfo.avail_cpus;
303 * Class: java_lang_Runtime
305 * Signature: (Ljava/lang/String;)I
307 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *par1)
318 log_text("Java_java_lang_VMRuntime_nativeLoad");
321 data = javastring_toutf(par1, 0);
324 log_text("nativeLoad: Error: empty string");
329 buffer_len = utf_strlen(data) + 40;
331 buffer = MNEW(char, buffer_len);
332 strcpy(buffer, "Java_java_lang_VMRuntime_nativeLoad:");
333 utf_sprint(buffer + strlen((char *) data), data);
337 MFREE(buffer, char, buffer_len);
340 #ifndef STATIC_CLASSPATH
341 /*here it could be interesting to store the references in a list eg for nicely cleaning up or for certain platforms*/
342 if (dlopen(data->text,RTLD_NOW | RTLD_GLOBAL)) {
343 /*log_text("LIBLOADED");*/
355 * Class: java/lang/VMRuntime
356 * Method: nativeGetLibname
357 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
359 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_nativeGetLibname(JNIEnv *env, jclass clazz, java_lang_String *pathname, java_lang_String *libname)
367 *exceptionptr = new_nullpointerexception();
371 u = javastring_toutf(libname, 0);
374 log_text("nativeGetLibName: Error: empty string");
378 buffer_len = utf_strlen(u) + 6 /*lib .so */ +1 /*0*/;
379 buffer = MNEW(char, buffer_len);
381 sprintf(buffer, "lib");
382 utf_sprint(buffer + 3, u);
383 strcat(buffer, ".so");
386 log_text("nativeGetLibName:");
390 s = javastring_new_char(buffer);
392 MFREE(buffer, char, buffer_len);
399 * Class: java_lang_VMRuntime
400 * Method: insertSystemProperties
401 * Signature: (Ljava/util/Properties;)V
403 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_insertSystemProperties(JNIEnv *env, jclass clazz, java_util_Properties *p)
406 #define BUFFERSIZE 200
408 char cwd[BUFFERSIZE];
412 struct utsname utsnamebuf;
413 #if !defined(STATIC_CLASSPATH)
419 *exceptionptr = new_nullpointerexception();
423 /* get properties from system */
425 (void) getcwd(cwd, BUFFERSIZE);
426 java_home = getenv("JAVA_HOME");
427 user = getenv("USER");
428 home = getenv("HOME");
431 /* search for method to add properties */
433 m = class_resolveclassmethod(p->header.vftbl->class,
435 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
442 insert_property(m, p, "java.version", VERSION);
443 insert_property(m, p, "java.vendor", "CACAO Team");
444 insert_property(m, p, "java.vendor.url", "http://www.cacaojvm.org/");
445 insert_property(m, p, "java.home", java_home ? java_home : INSTALL_PREFIX);
446 insert_property(m, p, "java.vm.specification.version", "1.0");
447 insert_property(m, p, "java.vm.specification.vendor", "Sun Microsystems Inc.");
448 insert_property(m, p, "java.vm.specification.name", "Java Virtual Machine Specification");
449 insert_property(m, p, "java.vm.version", VERSION);
450 insert_property(m, p, "java.vm.vendor", "CACAO Team");
451 insert_property(m, p, "java.vm.name", "CACAO");
452 insert_property(m, p, "java.specification.version", "1.4");
453 insert_property(m, p, "java.specification.vendor", "Sun Microsystems Inc.");
454 insert_property(m, p, "java.specification.name", "Java Platform API Specification");
455 insert_property(m, p, "java.class.version", "48.0");
456 insert_property(m, p, "java.class.path", classpath);
458 #if defined(STATIC_CLASSPATH)
459 insert_property(m, p, "java.library.path" , ".");
461 libpathlen = strlen(INSTALL_PREFIX) + strlen(CACAO_LIBRARY_PATH) + 1;
463 if (getenv("CACAO_LIB_OVERRIDE"))
464 libpathlen += strlen(getenv("CACAO_LIB_OVERRIDE")) + 1;
466 if (getenv("LD_LIBRARY_PATH"))
467 libpathlen += strlen(getenv("LD_LIBRARY_PATH")) + 1;
469 libpath = MNEW(char, libpathlen);
471 if (getenv("CACAO_LIB_OVERRIDE")) {
472 strcat(libpath, getenv("CACAO_LIB_OVERRIDE"));
473 strcat(libpath, ":");
476 strcat(libpath, INSTALL_PREFIX);
477 strcat(libpath, CACAO_LIBRARY_PATH);
479 if (getenv("LD_LIBRARY_PATH")) {
480 strcat(libpath, ":");
481 strcat(libpath, getenv("LD_LIBRARY_PATH"));
483 insert_property(m, p, "java.library.path", libpath);
485 MFREE(libpath, char, libpathlen);
488 insert_property(m, p, "java.io.tmpdir", "/tmp");
489 insert_property(m, p, "java.compiler", "cacao.jit");
490 insert_property(m, p, "java.ext.dirs", "");
491 insert_property(m, p, "os.name", utsnamebuf.sysname);
492 insert_property(m, p, "os.arch", utsnamebuf.machine);
493 insert_property(m, p, "os.version", utsnamebuf.release);
494 insert_property(m, p, "file.separator", "/");
495 /* insert_property(m, p, "file.encoding", "null"); -- this must be set properly */
496 insert_property(m, p, "path.separator", ":");
497 insert_property(m, p, "line.separator", "\n");
498 insert_property(m, p, "user.name", user ? user : "null");
499 insert_property(m, p, "user.home", home ? home : "null");
500 insert_property(m, p, "user.dir", cwd ? cwd : "null");
503 /* disable gthread-jni's portable native sync due to yet unresolved
505 insert_property(m, p, "gnu.classpath.awt.gtk.portable.native.sync", "false");
509 /* how do we get them? */
510 { "user.language", "en" },
511 { "user.region", "US" },
512 { "user.country", "US" },
513 { "user.timezone", "Europe/Vienna" },
515 /* XXX do we need this one? */
516 { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
518 insert_property(m, p, "java.protocol.handler.pkgs", "gnu.java.net.protocol");
520 /* insert properties defined on commandline */
525 insert_property(m, p, properties->key, properties->value);
528 properties = properties->next;
537 * Class: java_lang_VMRuntime
541 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
543 return gc_get_max_heap_size();
548 * These are local overrides for various environment variables in Emacs.
549 * Please do not remove this and leave it at the end of the file, where
550 * Emacs will automagically detect them.
551 * ---------------------------------------------------------------------
554 * indent-tabs-mode: t