1 /* native/vm/VMRuntime.c - java/lang/VMRuntime
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
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 1688 2004-12-05 23:59:04Z twisti $
40 #include <sys/utsname.h>
42 #if !defined(STATIC_CLASSPATH)
47 #include "cacao/cacao.h"
49 #include "mm/memory.h"
50 #include "native/jni.h"
51 #include "native/native.h"
52 #include "native/include/java_io_File.h"
53 #include "native/include/java_lang_String.h"
54 #include "native/include/java_lang_Process.h"
55 #include "native/include/java_util_Properties.h" /* java_lang_VMRuntime.h */
56 #include "native/include/java_lang_VMRuntime.h"
57 #include "toolbox/logging.h"
58 #include "vm/builtin.h"
59 #include "vm/exceptions.h"
60 #include "vm/loader.h"
61 #include "vm/tables.h"
62 #include "vm/jit/asmpart.h"
65 /* this should work on BSD */
67 #if defined(__DARWIN__)
68 #include <sys/sysctl.h>
74 /* should we run all finalizers on exit? */
75 static bool finalizeOnExit = false;
77 /* temporary property structure */
79 typedef struct property property;
87 static property *properties = NULL;
90 /* create_property *************************************************************
92 Create a property entry for a command line property definition.
94 *******************************************************************************/
96 void create_property(char *key, char *value)
103 p->next = properties;
108 /* insert_property *************************************************************
110 Used for inserting a property into the system's properties table. Method m
111 (usually put) and the properties table must be given.
113 *******************************************************************************/
115 static void insert_property(methodinfo *m, java_util_Properties *p, char *key,
118 asm_calljavafunction(m,
120 javastring_new_char(key),
121 javastring_new_char(value),
127 * Class: java_lang_VMRuntime
128 * Method: execInternal
129 * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
131 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_VMRuntime_execInternal(JNIEnv *env, jclass clazz, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
133 log_text("Java_java_lang_Runtime_execInternal called");
140 * Class: java/lang/VMRuntime
141 * Method: exitInternal
144 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
149 cacao_shutdown(par1);
154 * Class: java/lang/Runtime
158 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
160 return gc_get_free_bytes();
165 * Class: java/lang/Runtime
169 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
176 * Class: java/lang/Runtime
177 * Method: runFinalization
180 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
182 gc_invoke_finalizers();
187 * Class: java/lang/Runtime
188 * Method: runFinalizersOnExit
191 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
196 finalizeOnExit = value;
201 * Class: java/lang/Runtime
202 * Method: runFinalizationsForExit
205 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
207 /* if (finalizeOnExit) { */
209 /* gc_finalize_all(); */
211 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
212 /*gc_finalize_all();*/
213 /*gc_invoke_finalizers();*/
219 * Class: java/lang/Runtime
220 * Method: totalMemory
223 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
225 return gc_get_heap_size();
230 * Class: java/lang/Runtime
231 * Method: traceInstructions
234 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
241 * Class: java/lang/Runtime
242 * Method: traceMethodCalls
245 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
252 * Class: java_lang_Runtime
253 * Method: availableProcessors
256 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
258 #if defined(_SC_NPROC_ONLN)
259 return (s4) sysconf(_SC_NPROC_ONLN);
261 #elif defined(_SC_NPROCESSORS_ONLN)
262 return (s4) sysconf(_SC_NPROCESSORS_ONLN);
264 #elif defined(__DARWIN__)
265 /* this should work in BSD */
267 int ncpu, mib[2], rc;
273 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
278 host_basic_info_data_t hinfo;
279 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
282 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
283 (host_info_t) &hinfo, &hinfo_count);
285 if (rc != KERN_SUCCESS) {
289 return (s4) hinfo.avail_cpus;
298 * Class: java_lang_Runtime
300 * Signature: (Ljava/lang/String;)I
302 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *par1)
313 log_text("Java_java_lang_VMRuntime_nativeLoad");
316 data = javastring_toutf(par1, 0);
319 log_text("nativeLoad: Error: empty string");
324 buffer_len = utf_strlen(data) + 40;
326 buffer = MNEW(char, buffer_len);
327 strcpy(buffer, "Java_java_lang_VMRuntime_nativeLoad:");
328 utf_sprint(buffer + strlen((char *) data), data);
332 MFREE(buffer, char, buffer_len);
335 #ifndef STATIC_CLASSPATH
336 /*here it could be interesting to store the references in a list eg for nicely cleaning up or for certain platforms*/
337 if (dlopen(data->text,RTLD_NOW | RTLD_GLOBAL)) {
338 /*log_text("LIBLOADED");*/
350 * Class: java/lang/VMRuntime
351 * Method: nativeGetLibname
352 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
354 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_nativeGetLibname(JNIEnv *env, jclass clazz, java_lang_String *pathname, java_lang_String *libname)
362 *exceptionptr = new_nullpointerexception();
366 u = javastring_toutf(libname, 0);
369 log_text("nativeGetLibName: Error: empty string");
373 buffer_len = utf_strlen(u) + 6 /*lib .so */ +1 /*0*/;
374 buffer = MNEW(char, buffer_len);
376 sprintf(buffer, "lib");
377 utf_sprint(buffer + 3, u);
378 strcat(buffer, ".so");
381 log_text("nativeGetLibName:");
385 s = javastring_new_char(buffer);
387 MFREE(buffer, char, buffer_len);
394 * Class: java_lang_VMRuntime
395 * Method: insertSystemProperties
396 * Signature: (Ljava/util/Properties;)V
398 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_insertSystemProperties(JNIEnv *env, jclass clazz, java_util_Properties *p)
401 #define BUFFERSIZE 200
403 char cwd[BUFFERSIZE];
407 struct utsname utsnamebuf;
408 #if !defined(STATIC_CLASSPATH)
414 *exceptionptr = new_nullpointerexception();
418 /* get properties from system */
420 (void) getcwd(cwd, BUFFERSIZE);
421 java_home = getenv("JAVA_HOME");
422 user = getenv("USER");
423 home = getenv("HOME");
426 /* search for method to add properties */
428 m = class_resolveclassmethod(p->header.vftbl->class,
430 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
437 insert_property(m, p, "java.version", VERSION);
438 insert_property(m, p, "java.vendor", "CACAO Team");
439 insert_property(m, p, "java.vendor.url", "http://www.complang.tuwien.ac.at/java/cacao/");
440 insert_property(m, p, "java.home", java_home ? java_home : "null");
441 insert_property(m, p, "java.vm.specification.version", "1.0");
442 insert_property(m, p, "java.vm.specification.vendor", "Sun Microsystems Inc.");
443 insert_property(m, p, "java.vm.specification.name", "Java Virtual Machine Specification");
444 insert_property(m, p, "java.vm.version", VERSION);
445 insert_property(m, p, "java.vm.vendor", "CACAO Team");
446 insert_property(m, p, "java.vm.name", "CACAO");
447 insert_property(m, p, "java.specification.version", "1.4");
448 insert_property(m, p, "java.specification.vendor", "Sun Microsystems Inc.");
449 insert_property(m, p, "java.specification.name", "Java Platform API Specification");
450 insert_property(m, p, "java.class.version", "48.0");
451 insert_property(m, p, "java.class.path", classpath);
453 #if defined(STATIC_CLASSPATH)
454 insert_property(m, p, "java.library.path" , ".");
456 libpathlen = strlen(INSTALL_PREFIX) + strlen(CACAO_LIBRARY_PATH) + 1;
458 if (getenv("CACAO_LIB_OVERRIDE"))
459 libpathlen += strlen(getenv("CACAO_LIB_OVERRIDE")) + 1;
461 if (getenv("LD_LIBRARY_PATH"))
462 libpathlen += strlen(getenv("LD_LIBRARY_PATH")) + 1;
464 libpath = MNEW(char, libpathlen);
466 if (getenv("CACAO_LIB_OVERRIDE")) {
467 strcat(libpath, getenv("CACAO_LIB_OVERRIDE"));
468 strcat(libpath, ":");
471 strcat(libpath, INSTALL_PREFIX);
472 strcat(libpath, CACAO_LIBRARY_PATH);
474 if (getenv("LD_LIBRARY_PATH")) {
475 strcat(libpath, ":");
476 strcat(libpath, getenv("LD_LIBRARY_PATH"));
478 insert_property(m, p, "java.library.path", libpath);
480 MFREE(libpath, char, libpathlen);
483 insert_property(m, p, "java.io.tmpdir", "/tmp");
484 insert_property(m, p, "java.compiler", "cacao.jit");
485 insert_property(m, p, "java.ext.dirs", "");
486 insert_property(m, p, "os.name", utsnamebuf.sysname);
487 insert_property(m, p, "os.arch", utsnamebuf.machine);
488 insert_property(m, p, "os.version", utsnamebuf.release);
489 insert_property(m, p, "file.separator", "/");
490 /* insert_property(m, p, "file.encoding", "null"); -- this must be set properly */
491 insert_property(m, p, "path.separator", ":");
492 insert_property(m, p, "line.separator", "\n");
493 insert_property(m, p, "user.name", user ? user : "null");
494 insert_property(m, p, "user.home", home ? home : "null");
495 insert_property(m, p, "user.dir", cwd ? cwd : "null");
498 /* how do we get them? */
499 { "user.language", "en" },
500 { "user.region", "US" },
501 { "user.country", "US" },
502 { "user.timezone", "Europe/Vienna" },
504 /* XXX do we need this one? */
505 { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
507 insert_property(m, p, "java.protocol.handler.pkgs", "gnu.java.net.protocol");
509 /* insert properties defined on commandline */
514 insert_property(m, p, properties->key, properties->value);
517 properties = properties->next;
526 * Class: java_lang_VMRuntime
530 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
532 return gc_get_max_heap_size();
537 * These are local overrides for various environment variables in Emacs.
538 * Please do not remove this and leave it at the end of the file, where
539 * Emacs will automagically detect them.
540 * ---------------------------------------------------------------------
543 * indent-tabs-mode: t