1 /* src/cacao/cacao.c - 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_LIBJVM)
34 #if defined(ENABLE_JRE_LAYOUT)
45 #include "native/jni.h"
47 #if defined(ENABLE_JVMTI)
48 # include "native/jvmti/jvmti.h"
49 # include "native/jvmti/cacaodbg.h"
50 # include "threads/mutex.h"
56 /* forward declarations *******************************************************/
58 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv);
61 /* main ************************************************************************
65 *******************************************************************************/
67 int main(int argc, char **argv)
69 #if defined(ENABLE_LIBJVM)
73 #if defined(ENABLE_LIBJVM)
74 /* Variables for JNI_CreateJavaVM dlopen call. */
75 lt_dlhandle libjvm_handle;
76 lt_ptr libjvm_vm_createjvm;
80 bool (*vm_createjvm)(JavaVM **, void **, void *);
81 void (*vm_run)(JavaVM *, JavaVMInitArgs *);
84 JavaVM *vm; /* denotes a Java VM */
86 JavaVMInitArgs *vm_args;
88 /* prepare the options */
90 vm_args = cacao_options_prepare(argc, argv);
92 /* load and initialize a Java VM, return a JNI interface pointer in env */
94 #if defined(ENABLE_LIBJVM)
95 # if defined(ENABLE_JRE_LAYOUT)
96 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
98 path = malloc(sizeof(char) * 4096);
100 if (readlink("/proc/self/exe", path, 4095) == -1) {
101 fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
105 /* get the path of the current executable */
107 path = dirname(path);
109 if ((strlen(path) + strlen("/../lib/libjvm") + strlen("0")) > 4096) {
110 fprintf(stderr, "main: libjvm name to long for buffer\n");
114 /* concatinate the library name */
116 strcat(path, "/../lib/libjvm");
118 path = CACAO_LIBDIR"/libjvm";
122 fprintf(stderr, "main: lt_dlinit failed: %s\n", lt_dlerror());
126 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
127 If not found, try the absolute path. */
129 if (!(libjvm_handle = lt_dlopenext("libjvm"))) {
130 /* save the error message */
132 lterror = strdup(lt_dlerror());
134 if (!(libjvm_handle = lt_dlopenext(path))) {
135 /* print the first error message too */
137 fprintf(stderr, "main: lt_dlopenext failed: %s\n", lterror);
139 /* and now the current one */
141 fprintf(stderr, "main: lt_dlopenext failed: %s\n", lt_dlerror());
145 /* free the error string */
147 free((void *) lterror);
150 if (!(libjvm_vm_createjvm = lt_dlsym(libjvm_handle, "vm_createjvm"))) {
151 fprintf(stderr, "main: lt_dlsym failed: %s\n", lt_dlerror());
156 (bool (*)(JavaVM **, void **, void *)) (ptrint) libjvm_vm_createjvm;
159 /* create the Java VM */
161 (void) vm_createjvm(&vm, (void *) &env, vm_args);
163 #if defined(ENABLE_JVMTI)
164 mutex_init(&dbgcomlock);
165 if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
168 #if defined(ENABLE_LIBJVM)
169 if (!(libjvm_vm_run = lt_dlsym(libjvm_handle, "vm_run"))) {
170 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
174 vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
181 /* keep compiler happy */
187 /* cacao_options_prepare *******************************************************
189 Prepare the JavaVMInitArgs.
191 *******************************************************************************/
193 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv)
195 JavaVMInitArgs *vm_args;
198 vm_args = malloc(sizeof(JavaVMInitArgs));
200 vm_args->version = JNI_VERSION_1_2;
201 vm_args->nOptions = argc - 1;
202 vm_args->options = malloc(sizeof(JavaVMOption) * argc);
203 vm_args->ignoreUnrecognized = JNI_FALSE;
205 for (i = 1; i < argc; i++)
206 vm_args->options[i - 1].optionString = argv[i];
213 * These are local overrides for various environment variables in Emacs.
214 * Please do not remove this and leave it at the end of the file, where
215 * Emacs will automagically detect them.
216 * ---------------------------------------------------------------------
219 * indent-tabs-mode: t