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
33 $Id: cacao.c 6011 2006-11-16 15:56:44Z twisti $
42 #if defined(ENABLE_LIBJVM)
46 #if defined(WITH_JRE_LAYOUT)
57 #include "native/jni.h"
59 #if defined(ENABLE_JVMTI)
60 # include "native/jvmti/jvmti.h"
61 # include "native/jvmti/cacaodbg.h"
67 /* forward declarations *******************************************************/
69 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv);
72 /* main ************************************************************************
76 *******************************************************************************/
78 int main(int argc, char **argv)
82 #if defined(ENABLE_LIBJVM)
83 /* Variables for JNI_CreateJavaVM dlopen call. */
84 lt_dlhandle libjvm_handle;
85 lt_ptr libjvm_vm_createjvm;
88 bool (*vm_createjvm)(JavaVM **, void **, void *);
89 void (*vm_run)(JavaVM *, JavaVMInitArgs *);
92 JavaVM *vm; /* denotes a Java VM */
94 JavaVMInitArgs *vm_args;
96 /* prepare the options */
98 vm_args = cacao_options_prepare(argc, argv);
100 /* load and initialize a Java VM, return a JNI interface pointer in env */
102 #if !defined(WITH_STATIC_CLASSPATH) && defined(ENABLE_LIBJVM)
103 # if defined(WITH_JRE_LAYOUT)
104 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
106 path = malloc(sizeof(char) * 4096);
108 if (readlink("/proc/self/exe", path, 4095) == -1) {
109 fprintf(stderr, "readlink failed: %s\n", strerror(errno));
113 /* get the path of the current executable */
115 path = dirname(path);
117 if ((strlen(path) + strlen("/../lib/libjvm") + strlen("0")) > 4096) {
118 fprintf(stderr, "libjvm name to long for buffer\n");
122 /* concatinate the library name */
124 strcat(path, "/../lib/libjvm");
126 path = CACAO_LIBDIR"/libjvm";
130 fprintf(stderr, "lt_dlinit failed: %s\n", lt_dlerror());
134 /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
135 If not found, try the absolute path. */
137 if (!(libjvm_handle = lt_dlopenext("libjvm"))) {
138 if (!(libjvm_handle = lt_dlopenext(path))) {
139 fprintf(stderr, "lt_dlopenext failed: %s\n", lt_dlerror());
144 if (!(libjvm_vm_createjvm = lt_dlsym(libjvm_handle, "vm_createjvm"))) {
145 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
150 (bool (*)(JavaVM **, void **, void *)) (ptrint) libjvm_vm_createjvm;
153 /* create the Java VM */
155 (void) vm_createjvm(&vm, (void *) &env, vm_args);
157 #if defined(ENABLE_JVMTI)
158 pthread_mutex_init(&dbgcomlock,NULL);
159 if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
162 #if !defined(WITH_STATIC_CLASSPATH) && defined(ENABLE_LIBJVM)
163 if (!(libjvm_vm_run = lt_dlsym(libjvm_handle, "vm_run"))) {
164 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
168 vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
175 /* keep compiler happy */
181 /* cacao_options_prepare *******************************************************
183 Prepare the JavaVMInitArgs.
185 *******************************************************************************/
187 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv)
189 JavaVMInitArgs *vm_args;
192 vm_args = malloc(sizeof(JavaVMInitArgs));
194 vm_args->version = JNI_VERSION_1_2;
195 vm_args->nOptions = argc - 1;
196 vm_args->options = malloc(sizeof(JavaVMOption) * argc);
197 vm_args->ignoreUnrecognized = JNI_FALSE;
199 for (i = 1; i < argc; i++)
200 vm_args->options[i - 1].optionString = argv[i];
207 * These are local overrides for various environment variables in Emacs.
208 * Please do not remove this and leave it at the end of the file, where
209 * Emacs will automagically detect them.
210 * ---------------------------------------------------------------------
213 * indent-tabs-mode: t