1 /* src/cacao/cacao.cpp - contains main() of cacao
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
28 #if defined(ENABLE_JRE_LAYOUT)
38 #include "native/jni.hpp"
39 #include "native/native.hpp"
41 #if defined(ENABLE_JVMTI)
42 # include "native/jvmti/jvmti.h"
43 # include "native/jvmti/cacaodbg.h"
50 /* Defines. *******************************************************************/
52 #define LIBJVM_NAME NATIVE_LIBRARY_PREFIX"jvm"NATIVE_LIBRARY_SUFFIX
55 /* forward declarations *******************************************************/
57 static JavaVMInitArgs* prepare_options(int argc, char** argv);
60 /* main ************************************************************************
64 *******************************************************************************/
66 int main(int argc, char **argv)
68 #if defined(ENABLE_LIBJVM)
71 # if defined(ENABLE_JRE_LAYOUT)
76 #if defined(ENABLE_LIBJVM)
77 /* Variables for JNI_CreateJavaVM dlopen call. */
79 void* libjvm_VM_create;
83 bool (*VM_create)(JavaVM**, void**, void*);
84 void (*vm_run)(JavaVM*, JavaVMInitArgs*);
87 // Prepare the options.
88 JavaVMInitArgs* vm_args = prepare_options(argc, argv);
90 /* load and initialize a Java VM, return a JNI interface pointer in env */
92 #if defined(ENABLE_LIBJVM)
93 # if defined(ENABLE_JRE_LAYOUT)
94 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
96 path = (char*) os::malloc(sizeof(char) * 4096);
98 if (readlink("/proc/self/exe", path, 4095) == -1) {
99 fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
103 /* get the path of the current executable */
105 path = os::dirname(path);
106 len = os::strlen(path) + os::strlen("/../lib/"LIBJVM_NAME) + os::strlen("0");
109 fprintf(stderr, "main: libjvm name to long for buffer\n");
113 /* concatenate the library name */
115 strcat(path, "/../lib/"LIBJVM_NAME);
117 path = (char*) CACAO_LIBDIR"/"LIBJVM_NAME;
120 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
121 If not found, try the absolute path. */
123 libjvm_handle = os::dlopen(LIBJVM_NAME, RTLD_NOW);
125 if (libjvm_handle == NULL) {
126 /* save the error message */
128 lterror = strdup(os::dlerror());
130 libjvm_handle = os::dlopen(path, RTLD_NOW);
132 if (libjvm_handle == NULL) {
133 /* print the first error message too */
135 fprintf(stderr, "main: os::dlopen failed: %s\n", lterror);
137 /* and now the current one */
139 fprintf(stderr, "main: os::dlopen failed: %s\n", os::dlerror());
143 // Free the error string.
144 os::free((void*) lterror);
147 libjvm_VM_create = os::dlsym(libjvm_handle, "VM_create");
149 if (libjvm_VM_create == NULL) {
150 fprintf(stderr, "main: lt_dlsym failed: %s\n", os::dlerror());
154 VM_create = (bool (*)(JavaVM**, void**, void*)) (uintptr_t) libjvm_VM_create;
157 // Create the Java VM.
159 void* env; // We use a void* instead of a JNIEnv* here to prevent a compiler warning.
161 (void) VM_create(&vm, &env, vm_args);
163 #if defined(ENABLE_JVMTI)
164 # error This should be a JVMTI function.
165 Mutex_init(&dbgcomlock);
166 if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
169 #if defined(ENABLE_LIBJVM)
170 libjvm_vm_run = os::dlsym(libjvm_handle, "vm_run");
172 if (libjvm_vm_run == NULL) {
173 fprintf(stderr, "main: os::dlsym failed: %s\n", os::dlerror());
177 vm_run = (void (*)(JavaVM*, JavaVMInitArgs*)) (uintptr_t) libjvm_vm_run;
183 // Keep compiler happy.
189 * Prepare the JavaVMInitArgs structure.
191 static JavaVMInitArgs* prepare_options(int argc, char** argv)
193 JavaVMInitArgs* vm_args;
195 vm_args = (JavaVMInitArgs*) malloc(sizeof(JavaVMInitArgs));
197 vm_args->version = JNI_VERSION_1_2;
198 vm_args->nOptions = argc - 1;
199 vm_args->options = (JavaVMOption*) malloc(sizeof(JavaVMOption) * argc);
200 vm_args->ignoreUnrecognized = JNI_FALSE;
202 for (int i = 1; i < argc; i++)
203 vm_args->options[i - 1].optionString = argv[i];
210 * These are local overrides for various environment variables in Emacs.
211 * Please do not remove this and leave it at the end of the file, where
212 * Emacs will automagically detect them.
213 * ---------------------------------------------------------------------
216 * indent-tabs-mode: t