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
25 $Id: cacao.c 7698 2007-04-13 10:12:04Z twisti $
34 #if defined(ENABLE_LIBJVM)
38 #if defined(WITH_JRE_LAYOUT)
49 #include "native/jni.h"
51 #if defined(ENABLE_JVMTI)
52 # include "native/jvmti/jvmti.h"
53 # include "native/jvmti/cacaodbg.h"
59 /* forward declarations *******************************************************/
61 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv);
64 /* main ************************************************************************
68 *******************************************************************************/
70 int main(int argc, char **argv)
72 #if defined(ENABLE_LIBJVM) && !defined(WITH_STATIC_CLASSPATH)
76 #if defined(ENABLE_LIBJVM)
77 /* Variables for JNI_CreateJavaVM dlopen call. */
78 lt_dlhandle libjvm_handle;
79 lt_ptr libjvm_vm_createjvm;
83 bool (*vm_createjvm)(JavaVM **, void **, void *);
84 void (*vm_run)(JavaVM *, JavaVMInitArgs *);
87 JavaVM *vm; /* denotes a Java VM */
89 JavaVMInitArgs *vm_args;
91 /* prepare the options */
93 vm_args = cacao_options_prepare(argc, argv);
95 /* load and initialize a Java VM, return a JNI interface pointer in env */
97 #if defined(ENABLE_LIBJVM) && !defined(WITH_STATIC_CLASSPATH)
98 # if defined(WITH_JRE_LAYOUT)
99 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
101 path = malloc(sizeof(char) * 4096);
103 if (readlink("/proc/self/exe", path, 4095) == -1) {
104 fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
108 /* get the path of the current executable */
110 path = dirname(path);
112 if ((strlen(path) + strlen("/../lib/libjvm") + strlen("0")) > 4096) {
113 fprintf(stderr, "main: libjvm name to long for buffer\n");
117 /* concatinate the library name */
119 strcat(path, "/../lib/libjvm");
121 path = CACAO_LIBDIR"/libjvm";
125 fprintf(stderr, "main: lt_dlinit failed: %s\n", lt_dlerror());
129 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
130 If not found, try the absolute path. */
132 if (!(libjvm_handle = lt_dlopenext("libjvm"))) {
133 /* save the error message */
135 lterror = strdup(lt_dlerror());
137 if (!(libjvm_handle = lt_dlopenext(path))) {
138 /* print the first error message too */
140 fprintf(stderr, "main: lt_dlopenext failed: %s\n", lterror);
142 /* and now the current one */
144 fprintf(stderr, "main: lt_dlopenext failed: %s\n", lt_dlerror());
148 /* free the error string */
150 free((void *) lterror);
153 if (!(libjvm_vm_createjvm = lt_dlsym(libjvm_handle, "vm_createjvm"))) {
154 fprintf(stderr, "main: lt_dlsym failed: %s\n", lt_dlerror());
159 (bool (*)(JavaVM **, void **, void *)) (ptrint) libjvm_vm_createjvm;
162 /* create the Java VM */
164 (void) vm_createjvm(&vm, (void *) &env, vm_args);
166 #if defined(ENABLE_JVMTI)
167 pthread_mutex_init(&dbgcomlock,NULL);
168 if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
171 #if !defined(WITH_STATIC_CLASSPATH) && defined(ENABLE_LIBJVM)
172 if (!(libjvm_vm_run = lt_dlsym(libjvm_handle, "vm_run"))) {
173 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
177 vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
184 /* keep compiler happy */
190 /* cacao_options_prepare *******************************************************
192 Prepare the JavaVMInitArgs.
194 *******************************************************************************/
196 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv)
198 JavaVMInitArgs *vm_args;
201 vm_args = malloc(sizeof(JavaVMInitArgs));
203 vm_args->version = JNI_VERSION_1_2;
204 vm_args->nOptions = argc - 1;
205 vm_args->options = malloc(sizeof(JavaVMOption) * argc);
206 vm_args->ignoreUnrecognized = JNI_FALSE;
208 for (i = 1; i < argc; i++)
209 vm_args->options[i - 1].optionString = argv[i];
216 * These are local overrides for various environment variables in Emacs.
217 * Please do not remove this and leave it at the end of the file, where
218 * Emacs will automagically detect them.
219 * ---------------------------------------------------------------------
222 * indent-tabs-mode: t