1 /* nat/Runtime.c - java/lang/Runtime
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 1494 2004-11-12 13:34:26Z twisti $
40 #include <sys/utsname.h>
41 #include "exceptions.h"
45 #include "exceptions.h"
51 #include "toolbox/logging.h"
52 #include "toolbox/memory.h"
53 #include "nat/java_io_File.h"
54 #include "nat/java_lang_String.h"
55 #include "nat/java_lang_Process.h"
56 #include "nat/java_util_Properties.h" /* needed for java_lang_VMRuntime.h */
57 #include "nat/java_lang_VMRuntime.h"
60 #ifndef STATIC_CLASSPATH
64 /* this should work on BSD */
66 #if defined(__DARWIN__)
67 #include <sys/sysctl.h>
73 /* should we run all finalizers on exit? */
74 static bool finalizeOnExit = false;
76 /* temporary property structure */
78 typedef struct property property;
86 static property *properties = NULL;
89 /* create_property *************************************************************
91 Create a property entry for a command line property definition.
93 *******************************************************************************/
95 void create_property(char *key, char *value)
102 p->next = properties;
107 /* insert_property *************************************************************
109 Used for inserting a property into the system's properties table. Method m
110 (usually put) and the properties table must be given.
112 *******************************************************************************/
114 static void insert_property(methodinfo *m, java_util_Properties *p, char *key,
117 asm_calljavafunction(m,
119 javastring_new_char(key),
120 javastring_new_char(value),
126 * Class: java_lang_VMRuntime
127 * Method: execInternal
128 * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
130 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_VMRuntime_execInternal(JNIEnv *env, jclass clazz, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
132 log_text("Java_java_lang_Runtime_execInternal called");
139 * Class: java/lang/VMRuntime
140 * Method: exitInternal
143 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
148 cacao_shutdown(par1);
153 * Class: java/lang/Runtime
157 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
159 return gc_get_free_bytes();
164 * Class: java/lang/Runtime
168 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
175 * Class: java/lang/Runtime
176 * Method: runFinalization
179 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
181 gc_invoke_finalizers();
186 * Class: java/lang/Runtime
187 * Method: runFinalizersOnExit
190 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
195 finalizeOnExit = value;
200 * Class: java/lang/Runtime
201 * Method: runFinalizationsForExit
204 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
206 /* if (finalizeOnExit) { */
208 /* gc_finalize_all(); */
210 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
211 /*gc_finalize_all();*/
212 /*gc_invoke_finalizers();*/
218 * Class: java/lang/Runtime
219 * Method: totalMemory
222 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
224 return gc_get_heap_size();
229 * Class: java/lang/Runtime
230 * Method: traceInstructions
233 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
240 * Class: java/lang/Runtime
241 * Method: traceMethodCalls
244 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
251 * Class: java_lang_Runtime
252 * Method: availableProcessors
255 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
257 #if defined(_SC_NPROC_ONLN)
258 return (s4) sysconf(_SC_NPROC_ONLN);
260 #elif defined(_SC_NPROCESSORS_ONLN)
261 return (s4) sysconf(_SC_NPROCESSORS_ONLN);
263 #elif defined(__DARWIN__)
264 /* this should work in BSD */
266 int ncpu, mib[2], rc;
272 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
277 host_basic_info_data_t hinfo;
278 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
281 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
282 (host_info_t) &hinfo, &hinfo_count);
284 if (rc != KERN_SUCCESS) {
288 return (s4) hinfo.avail_cpus;
297 * Class: java_lang_Runtime
299 * Signature: (Ljava/lang/String;)I
301 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *par1)
309 log_text("Java_java_lang_VMRuntime_nativeLoad");
311 data = javastring_toutf(par1, 0);
314 log_text("nativeLoad: Error: empty string");
318 buffer_len = utf_strlen(data) + 40;
321 buffer = MNEW(char, buffer_len);
322 strcpy(buffer, "Java_java_lang_VMRuntime_nativeLoad:");
323 utf_sprint(buffer + strlen((char *) data), data);
327 MFREE(buffer, char, buffer_len);
330 #ifndef STATIC_CLASSPATH
331 /*here it could be interesting to store the references in a list eg for nicely cleaning up or for certain platforms*/
332 if (dlopen(data->text,RTLD_NOW | RTLD_GLOBAL)) {
333 log_text("LIBLOADED");
345 * Class: java_lang_VMRuntime
346 * Method: nativeGetLibname
347 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
349 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_nativeGetLibname(JNIEnv *env, jclass clazz, java_lang_String *par1, java_lang_String *par2)
354 java_lang_String *resultString;
355 data = javastring_toutf(par2, 0);
358 log_text("nativeGetLibName: Error: empty string");
362 buffer_len = utf_strlen(data) + 6 /*lib .so */ +1 /*0*/;
363 buffer = MNEW(char, buffer_len);
364 sprintf(buffer,"lib");
365 utf_sprint(buffer+3,data);
366 strcat(buffer,".so");
368 log_text("nativeGetLibName:");
372 resultString=javastring_new_char(buffer);
374 MFREE(buffer, char, buffer_len);
380 * Class: java_lang_VMRuntime
381 * Method: insertSystemProperties
382 * Signature: (Ljava/util/Properties;)V
384 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_insertSystemProperties(JNIEnv *env, jclass clazz, java_util_Properties *p)
387 #define BUFFERSIZE 200
389 char cwd[BUFFERSIZE];
393 struct utsname utsnamebuf;
396 *exceptionptr = new_exception(string_java_lang_NullPointerException);
400 /* get properties from system */
402 (void) getcwd(cwd, BUFFERSIZE);
403 java_home = getenv("JAVA_HOME");
404 user = getenv("USER");
405 home = getenv("HOME");
408 /* search for method to add properties */
410 m = class_resolveclassmethod(p->header.vftbl->class,
412 utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
419 insert_property(m, p, "java.version", VERSION);
420 insert_property(m, p, "java.vendor", "CACAO Team");
421 insert_property(m, p, "java.vendor.url", "http://www.complang.tuwien.ac.at/java/cacao/");
422 insert_property(m, p, "java.home", java_home ? java_home : "null");
423 insert_property(m, p, "java.vm.specification.version", "1.0");
424 insert_property(m, p, "java.vm.specification.vendor", "Sun Microsystems Inc.");
425 insert_property(m, p, "java.vm.specification.name", "Java Virtual Machine Specification");
426 insert_property(m, p, "java.vm.version", VERSION);
427 insert_property(m, p, "java.vm.vendor", "CACAO Team");
428 insert_property(m, p, "java.vm.name", "CACAO");
429 insert_property(m, p, "java.specification.version", "1.4");
430 insert_property(m, p, "java.specification.vendor", "Sun Microsystems Inc.");
431 insert_property(m, p, "java.specification.name", "Java Platform API Specification");
432 insert_property(m, p, "java.class.version", "48.0");
433 insert_property(m, p, "java.class.path", classpath);
434 #if defined(STATIC_CLASSPATH)
435 insert_property(m, p, "java.library.path" , ".");
437 insert_property(m, p, "java.library.path" , getenv("LD_LIBRARY_PATH"));
439 insert_property(m, p, "java.io.tmpdir", "/tmp");
440 insert_property(m, p, "java.compiler", "cacao.jit");
441 insert_property(m, p, "java.ext.dirs", "null");
442 insert_property(m, p, "os.name", utsnamebuf.sysname);
443 insert_property(m, p, "os.arch", utsnamebuf.machine);
444 insert_property(m, p, "os.version", utsnamebuf.release);
445 insert_property(m, p, "file.separator", "/");
446 /* insert_property(m, p, "file.encoding", "null"); -- this must be set properly */
447 insert_property(m, p, "path.separator", ":");
448 insert_property(m, p, "line.separator", "\n");
449 insert_property(m, p, "user.name", user ? user : "null");
450 insert_property(m, p, "user.home", home ? home : "null");
451 insert_property(m, p, "user.dir", cwd ? cwd : "null");
454 /* how do we get them? */
455 { "user.language", "en" },
456 { "user.region", "US" },
457 { "user.country", "US" },
458 { "user.timezone", "Europe/Vienna" },
460 /* XXX do we need this one? */
461 { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
464 /* insert properties defined on commandline */
469 insert_property(m, p, properties->key, properties->value);
472 properties = properties->next;
481 * Class: java_lang_VMRuntime
485 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
487 return gc_get_max_heap_size();
492 * These are local overrides for various environment variables in Emacs.
493 * Please do not remove this and leave it at the end of the file, where
494 * Emacs will automagically detect them.
495 * ---------------------------------------------------------------------
498 * indent-tabs-mode: t