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"
53 /* Defines. *******************************************************************/
55 #define LIBJVM_NAME NATIVE_LIBRARY_PREFIX"jvm"NATIVE_LIBRARY_SUFFIX
58 /* forward declarations *******************************************************/
60 static JavaVMInitArgs* prepare_options(int argc, char** argv);
63 /* main ************************************************************************
67 *******************************************************************************/
69 int main(int argc, char **argv)
71 #if defined(ENABLE_LIBJVM)
74 # if defined(ENABLE_JRE_LAYOUT)
79 #if defined(ENABLE_LIBJVM)
80 /* Variables for JNI_CreateJavaVM dlopen call. */
82 void* libjvm_VM_create;
86 bool (*VM_create)(JavaVM **, void **, void *);
87 void (*vm_run)(JavaVM *, JavaVMInitArgs *);
90 JavaVM *vm; /* denotes a Java VM */
92 JavaVMInitArgs *vm_args;
94 /* prepare the options */
96 vm_args = prepare_options(argc, argv);
98 /* load and initialize a Java VM, return a JNI interface pointer in env */
100 #if defined(ENABLE_LIBJVM)
101 # if defined(ENABLE_JRE_LAYOUT)
102 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
104 path = (char*) os::malloc(sizeof(char) * 4096);
106 if (readlink("/proc/self/exe", path, 4095) == -1) {
107 fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
111 /* get the path of the current executable */
113 path = os::dirname(path);
114 len = os::strlen(path) + os::strlen("/../lib/"LIBJVM_NAME) + os::strlen("0");
117 fprintf(stderr, "main: libjvm name to long for buffer\n");
121 /* concatinate the library name */
123 strcat(path, "/../lib/"LIBJVM_NAME);
125 path = (char*) CACAO_LIBDIR"/"LIBJVM_NAME;
128 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
129 If not found, try the absolute path. */
131 libjvm_handle = os::dlopen(LIBJVM_NAME, RTLD_NOW);
133 if (libjvm_handle == NULL) {
134 /* save the error message */
136 lterror = strdup(os::dlerror());
138 libjvm_handle = os::dlopen(path, RTLD_NOW);
140 if (libjvm_handle == NULL) {
141 /* print the first error message too */
143 fprintf(stderr, "main: os::dlopen failed: %s\n", lterror);
145 /* and now the current one */
147 fprintf(stderr, "main: os::dlopen failed: %s\n", os::dlerror());
151 /* free the error string */
153 free((void *) lterror);
156 libjvm_VM_create = os::dlsym(libjvm_handle, "VM_create");
158 if (libjvm_VM_create == NULL) {
159 fprintf(stderr, "main: lt_dlsym failed: %s\n", os::dlerror());
164 (bool (*)(JavaVM **, void **, void *)) (ptrint) libjvm_VM_create;
167 /* create the Java VM */
169 (void) VM_create(&vm, (void**) &env, vm_args);
171 #if defined(ENABLE_JVMTI)
172 # error This should be a JVMTI function.
173 Mutex_init(&dbgcomlock);
174 if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
177 #if defined(ENABLE_LIBJVM)
178 libjvm_vm_run = os::dlsym(libjvm_handle, "vm_run");
180 if (libjvm_vm_run == NULL) {
181 fprintf(stderr, "main: os::dlsym failed: %s\n", os::dlerror());
185 vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
192 /* keep compiler happy */
199 * Prepare the JavaVMInitArgs structure.
201 static JavaVMInitArgs* prepare_options(int argc, char** argv)
203 JavaVMInitArgs* vm_args;
205 vm_args = (JavaVMInitArgs*) malloc(sizeof(JavaVMInitArgs));
207 vm_args->version = JNI_VERSION_1_2;
208 vm_args->nOptions = argc - 1;
209 vm_args->options = (JavaVMOption*) malloc(sizeof(JavaVMOption) * argc);
210 vm_args->ignoreUnrecognized = JNI_FALSE;
212 for (int i = 1; i < argc; i++)
213 vm_args->options[i - 1].optionString = argv[i];
220 * These are local overrides for various environment variables in Emacs.
221 * Please do not remove this and leave it at the end of the file, where
222 * Emacs will automagically detect them.
223 * ---------------------------------------------------------------------
226 * indent-tabs-mode: t