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"
57 /* forward declarations *******************************************************/
59 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv);
62 /* main ************************************************************************
66 *******************************************************************************/
68 int main(int argc, char **argv)
70 #if defined(ENABLE_LIBJVM) && !defined(WITH_STATIC_CLASSPATH)
74 #if defined(ENABLE_LIBJVM)
75 /* Variables for JNI_CreateJavaVM dlopen call. */
76 lt_dlhandle libjvm_handle;
77 lt_ptr libjvm_vm_createjvm;
81 bool (*vm_createjvm)(JavaVM **, void **, void *);
82 void (*vm_run)(JavaVM *, JavaVMInitArgs *);
85 JavaVM *vm; /* denotes a Java VM */
87 JavaVMInitArgs *vm_args;
89 /* prepare the options */
91 vm_args = cacao_options_prepare(argc, argv);
93 /* load and initialize a Java VM, return a JNI interface pointer in env */
95 #if defined(ENABLE_LIBJVM) && !defined(WITH_STATIC_CLASSPATH)
96 # if defined(WITH_JRE_LAYOUT)
97 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
99 path = malloc(sizeof(char) * 4096);
101 if (readlink("/proc/self/exe", path, 4095) == -1) {
102 fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
106 /* get the path of the current executable */
108 path = dirname(path);
110 if ((strlen(path) + strlen("/../lib/libjvm") + strlen("0")) > 4096) {
111 fprintf(stderr, "main: libjvm name to long for buffer\n");
115 /* concatinate the library name */
117 strcat(path, "/../lib/libjvm");
119 path = CACAO_LIBDIR"/libjvm";
123 fprintf(stderr, "main: lt_dlinit failed: %s\n", lt_dlerror());
127 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
128 If not found, try the absolute path. */
130 if (!(libjvm_handle = lt_dlopenext("libjvm"))) {
131 /* save the error message */
133 lterror = strdup(lt_dlerror());
135 if (!(libjvm_handle = lt_dlopenext(path))) {
136 /* print the first error message too */
138 fprintf(stderr, "main: lt_dlopenext failed: %s\n", lterror);
140 /* and now the current one */
142 fprintf(stderr, "main: lt_dlopenext failed: %s\n", lt_dlerror());
146 /* free the error string */
148 free((void *) lterror);
151 if (!(libjvm_vm_createjvm = lt_dlsym(libjvm_handle, "vm_createjvm"))) {
152 fprintf(stderr, "main: lt_dlsym failed: %s\n", lt_dlerror());
157 (bool (*)(JavaVM **, void **, void *)) (ptrint) libjvm_vm_createjvm;
160 /* create the Java VM */
162 (void) vm_createjvm(&vm, (void *) &env, vm_args);
164 #if defined(ENABLE_JVMTI)
165 pthread_mutex_init(&dbgcomlock,NULL);
166 if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
169 #if !defined(WITH_STATIC_CLASSPATH) && defined(ENABLE_LIBJVM)
170 if (!(libjvm_vm_run = lt_dlsym(libjvm_handle, "vm_run"))) {
171 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
175 vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
182 /* keep compiler happy */
188 /* cacao_options_prepare *******************************************************
190 Prepare the JavaVMInitArgs.
192 *******************************************************************************/
194 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv)
196 JavaVMInitArgs *vm_args;
199 vm_args = malloc(sizeof(JavaVMInitArgs));
201 vm_args->version = JNI_VERSION_1_2;
202 vm_args->nOptions = argc - 1;
203 vm_args->options = malloc(sizeof(JavaVMOption) * argc);
204 vm_args->ignoreUnrecognized = JNI_FALSE;
206 for (i = 1; i < argc; i++)
207 vm_args->options[i - 1].optionString = argv[i];
214 * These are local overrides for various environment variables in Emacs.
215 * Please do not remove this and leave it at the end of the file, where
216 * Emacs will automagically detect them.
217 * ---------------------------------------------------------------------
220 * indent-tabs-mode: t