1 /* src/cacao/cacao.c - contains main() of cacao
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, J. Wenninger, Institut f. Computersprachen - TU Wien
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., 51 Franklin Street, Fifth Floor, Boston, MA
32 #if defined(ENABLE_LIBJVM)
36 #if defined(WITH_JRE_LAYOUT)
47 #include "native/jni.h"
49 #if defined(ENABLE_JVMTI)
50 # include "native/jvmti/jvmti.h"
51 # include "native/jvmti/cacaodbg.h"
52 # include "threads/mutex.h"
58 /* forward declarations *******************************************************/
60 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv);
63 /* main ************************************************************************
67 *******************************************************************************/
69 int main(int argc, char **argv)
71 #if defined(ENABLE_LIBJVM)
75 #if defined(ENABLE_LIBJVM)
76 /* Variables for JNI_CreateJavaVM dlopen call. */
77 lt_dlhandle libjvm_handle;
78 lt_ptr libjvm_vm_createjvm;
82 bool (*vm_createjvm)(JavaVM **, void **, void *);
83 void (*vm_run)(JavaVM *, JavaVMInitArgs *);
86 JavaVM *vm; /* denotes a Java VM */
88 JavaVMInitArgs *vm_args;
90 /* prepare the options */
92 vm_args = cacao_options_prepare(argc, argv);
94 /* load and initialize a Java VM, return a JNI interface pointer in env */
96 #if defined(ENABLE_LIBJVM)
97 # if defined(WITH_JRE_LAYOUT)
98 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
100 path = malloc(sizeof(char) * 4096);
102 if (readlink("/proc/self/exe", path, 4095) == -1) {
103 fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
107 /* get the path of the current executable */
109 path = dirname(path);
111 if ((strlen(path) + strlen("/../lib/libjvm") + strlen("0")) > 4096) {
112 fprintf(stderr, "main: libjvm name to long for buffer\n");
116 /* concatinate the library name */
118 strcat(path, "/../lib/libjvm");
120 path = CACAO_LIBDIR"/libjvm";
124 fprintf(stderr, "main: lt_dlinit failed: %s\n", lt_dlerror());
128 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
129 If not found, try the absolute path. */
131 if (!(libjvm_handle = lt_dlopenext("libjvm"))) {
132 /* save the error message */
134 lterror = strdup(lt_dlerror());
136 if (!(libjvm_handle = lt_dlopenext(path))) {
137 /* print the first error message too */
139 fprintf(stderr, "main: lt_dlopenext failed: %s\n", lterror);
141 /* and now the current one */
143 fprintf(stderr, "main: lt_dlopenext failed: %s\n", lt_dlerror());
147 /* free the error string */
149 free((void *) lterror);
152 if (!(libjvm_vm_createjvm = lt_dlsym(libjvm_handle, "vm_createjvm"))) {
153 fprintf(stderr, "main: lt_dlsym failed: %s\n", lt_dlerror());
158 (bool (*)(JavaVM **, void **, void *)) (ptrint) libjvm_vm_createjvm;
161 /* create the Java VM */
163 (void) vm_createjvm(&vm, (void *) &env, vm_args);
165 #if defined(ENABLE_JVMTI)
166 mutex_init(&dbgcomlock);
167 if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
170 #if defined(ENABLE_LIBJVM)
171 if (!(libjvm_vm_run = lt_dlsym(libjvm_handle, "vm_run"))) {
172 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
176 vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
183 /* keep compiler happy */
189 /* cacao_options_prepare *******************************************************
191 Prepare the JavaVMInitArgs.
193 *******************************************************************************/
195 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv)
197 JavaVMInitArgs *vm_args;
200 vm_args = malloc(sizeof(JavaVMInitArgs));
202 vm_args->version = JNI_VERSION_1_2;
203 vm_args->nOptions = argc - 1;
204 vm_args->options = malloc(sizeof(JavaVMOption) * argc);
205 vm_args->ignoreUnrecognized = JNI_FALSE;
207 for (i = 1; i < argc; i++)
208 vm_args->options[i - 1].optionString = argv[i];
215 * These are local overrides for various environment variables in Emacs.
216 * Please do not remove this and leave it at the end of the file, where
217 * Emacs will automagically detect them.
218 * ---------------------------------------------------------------------
221 * indent-tabs-mode: t