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
30 #if defined(ENABLE_JRE_LAYOUT)
41 #include "native/jni.h"
42 #include "native/native.h"
44 #if defined(ENABLE_JVMTI)
45 # include "native/jvmti/jvmti.h"
46 # include "native/jvmti/cacaodbg.h"
49 #include "vmcore/os.hpp"
54 /* Defines. *******************************************************************/
56 #define LIBJVM_NAME NATIVE_LIBRARY_PREFIX"jvm"NATIVE_LIBRARY_SUFFIX
59 /* forward declarations *******************************************************/
61 static JavaVMInitArgs* prepare_options(int argc, char** argv);
64 /* main ************************************************************************
68 *******************************************************************************/
70 int main(int argc, char **argv)
72 #if defined(ENABLE_LIBJVM)
75 # if defined(ENABLE_JRE_LAYOUT)
80 #if defined(ENABLE_LIBJVM)
81 /* Variables for JNI_CreateJavaVM dlopen call. */
83 void* libjvm_VM_create;
87 bool (*VM_create)(JavaVM **, void **, void *);
88 void (*vm_run)(JavaVM *, JavaVMInitArgs *);
91 JavaVM *vm; /* denotes a Java VM */
93 JavaVMInitArgs *vm_args;
95 /* prepare the options */
97 vm_args = prepare_options(argc, argv);
99 /* load and initialize a Java VM, return a JNI interface pointer in env */
101 #if defined(ENABLE_LIBJVM)
102 # if defined(ENABLE_JRE_LAYOUT)
103 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
105 path = malloc(sizeof(char) * 4096);
107 if (readlink("/proc/self/exe", path, 4095) == -1) {
108 fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
112 /* get the path of the current executable */
114 path = os::dirname(path);
115 len = os::strlen(path) + os::strlen("/../lib/"LIBJVM_NAME) + os::strlen("0");
118 fprintf(stderr, "main: libjvm name to long for buffer\n");
122 /* concatinate the library name */
124 strcat(path, "/../lib/"LIBJVM_NAME);
126 path = CACAO_LIBDIR"/"LIBJVM_NAME;
129 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
130 If not found, try the absolute path. */
132 libjvm_handle = os::dlopen(LIBJVM_NAME, RTLD_NOW);
134 if (libjvm_handle == NULL) {
135 /* save the error message */
137 lterror = strdup(os::dlerror());
139 libjvm_handle = os::dlopen(path, RTLD_NOW);
141 if (libjvm_handle == NULL) {
142 /* print the first error message too */
144 fprintf(stderr, "main: os::dlopen failed: %s\n", lterror);
146 /* and now the current one */
148 fprintf(stderr, "main: os::dlopen failed: %s\n", os::dlerror());
152 /* free the error string */
154 free((void *) lterror);
157 libjvm_VM_create = os::dlsym(libjvm_handle, "VM_create");
159 if (libjvm_VM_create == NULL) {
160 fprintf(stderr, "main: lt_dlsym failed: %s\n", os::dlerror());
165 (bool (*)(JavaVM **, void **, void *)) (ptrint) libjvm_VM_create;
168 /* create the Java VM */
170 (void) VM_create(&vm, (void**) &env, vm_args);
172 #if defined(ENABLE_JVMTI)
173 # error This should be a JVMTI function.
174 Mutex_init(&dbgcomlock);
175 if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
178 #if defined(ENABLE_LIBJVM)
179 libjvm_vm_run = os::dlsym(libjvm_handle, "vm_run");
181 if (libjvm_vm_run == NULL) {
182 fprintf(stderr, "main: os::dlsym failed: %s\n", os::dlerror());
186 vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
193 /* keep compiler happy */
200 * Prepare the JavaVMInitArgs structure.
202 static JavaVMInitArgs* prepare_options(int argc, char** argv)
204 JavaVMInitArgs* vm_args;
206 vm_args = (JavaVMInitArgs*) malloc(sizeof(JavaVMInitArgs));
208 vm_args->version = JNI_VERSION_1_2;
209 vm_args->nOptions = argc - 1;
210 vm_args->options = (JavaVMOption*) malloc(sizeof(JavaVMOption) * argc);
211 vm_args->ignoreUnrecognized = JNI_FALSE;
213 for (int i = 1; i < argc; i++)
214 vm_args->options[i - 1].optionString = argv[i];
221 * These are local overrides for various environment variables in Emacs.
222 * Please do not remove this and leave it at the end of the file, where
223 * Emacs will automagically detect them.
224 * ---------------------------------------------------------------------
227 * indent-tabs-mode: t