1 /* src/cacao/cacao.c - contains main() of cacao
3 Copyright (C) 1996-2005, 2006 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 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
34 $Id: cacao.c 5810 2006-10-20 13:54:54Z twisti $
43 #if defined(ENABLE_LIBJVM)
47 #if defined(WITH_JRE_LAYOUT)
58 #include "native/jni.h"
60 #if defined(ENABLE_JVMTI)
61 # include "native/jvmti/jvmti.h"
62 # include "native/jvmti/cacaodbg.h"
68 /* forward declarations *******************************************************/
70 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv);
73 /* main ************************************************************************
77 *******************************************************************************/
79 int main(int argc, char **argv)
83 #if defined(ENABLE_LIBJVM)
84 /* Variables for JNI_CreateJavaVM dlopen call. */
85 lt_dlhandle libjvm_handle;
86 lt_ptr libjvm_createvm;
89 s4 (*JNI_CreateJavaVM)(JavaVM **, void **, void *);
90 void (*vm_run)(JavaVM *, JavaVMInitArgs *);
93 JavaVM *vm; /* denotes a Java VM */
95 JavaVMInitArgs *vm_args;
97 /* prepare the options */
99 vm_args = cacao_options_prepare(argc, argv);
101 /* load and initialize a Java VM, return a JNI interface pointer in env */
103 #if !defined(WITH_STATIC_CLASSPATH) && defined(ENABLE_LIBJVM)
104 # if defined(WITH_JRE_LAYOUT)
105 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
107 path = malloc(sizeof(char) * 4096);
109 if (readlink("/proc/self/exe", path, 4095) == -1) {
110 fprintf(stderr, "readlink failed: %s\n", strerror(errno));
114 /* get the path of the current executable */
116 path = dirname(path);
118 if ((strlen(path) + strlen("/../lib/libjvm") + strlen("0")) > 4096) {
119 fprintf(stderr, "libjvm name to long for buffer\n");
123 /* concatinate the library name */
125 strcat(path, "/../lib/libjvm");
127 path = CACAO_LIBDIR"/libjvm";
131 fprintf(stderr, "lt_dlinit failed: %s\n", lt_dlerror());
135 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
136 If not found, try the absolute path. */
138 if (!(libjvm_handle = lt_dlopenext("libjvm"))) {
139 if (!(libjvm_handle = lt_dlopenext(path))) {
140 fprintf(stderr, "lt_dlopenext failed: %s\n", lt_dlerror());
145 if (!(libjvm_createvm = lt_dlsym(libjvm_handle, "JNI_CreateJavaVM"))) {
146 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
151 (s4 (*)(JavaVM **, void **, void *)) (ptrint) libjvm_createvm;
154 /* create the Java VM */
156 JNI_CreateJavaVM(&vm, (void *) &env, vm_args);
158 #if defined(ENABLE_JVMTI)
159 pthread_mutex_init(&dbgcomlock,NULL);
160 if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
163 #if !defined(WITH_STATIC_CLASSPATH) && defined(ENABLE_LIBJVM)
164 if (!(libjvm_vm_run = lt_dlsym(libjvm_handle, "vm_run"))) {
165 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
169 vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
176 /* keep compiler happy */
182 /* cacao_options_prepare *******************************************************
184 Prepare the JavaVMInitArgs.
186 *******************************************************************************/
188 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv)
190 JavaVMInitArgs *vm_args;
193 vm_args = malloc(sizeof(JavaVMInitArgs));
195 vm_args->version = JNI_VERSION_1_2;
196 vm_args->nOptions = argc - 1;
197 vm_args->options = malloc(sizeof(JavaVMOption) * argc);
198 vm_args->ignoreUnrecognized = JNI_FALSE;
200 for (i = 1; i < argc; i++)
201 vm_args->options[i - 1].optionString = argv[i];
208 * These are local overrides for various environment variables in Emacs.
209 * Please do not remove this and leave it at the end of the file, where
210 * Emacs will automagically detect them.
211 * ---------------------------------------------------------------------
214 * indent-tabs-mode: t