* configure.ac (AC_CHECK_ENABLE_LTDL): Removed.
[cacao.git] / src / cacao / cacao.c
1 /* src/cacao/cacao.c - contains main() of cacao
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29
30 #if defined(ENABLE_JRE_LAYOUT)
31 # include <errno.h>
32 # include <libgen.h>
33 # include <unistd.h>
34 #endif
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "vm/types.h"
40
41 #include "native/jni.h"
42 #include "native/native.h"
43
44 #if defined(ENABLE_JVMTI)
45 # include "native/jvmti/jvmti.h"
46 # include "native/jvmti/cacaodbg.h"
47 # include "threads/mutex.h"
48 #endif
49
50 #include "vmcore/system.h"
51
52 #include "vm/vm.h"
53
54
55 /* Defines. *******************************************************************/
56
57 #define LIBJVM_NAME    NATIVE_LIBRARY_PREFIX"jvm"NATIVE_LIBRARY_SUFFIX
58
59
60 /* forward declarations *******************************************************/
61
62 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv);
63
64
65 /* main ************************************************************************
66
67    The main program.
68    
69 *******************************************************************************/
70
71 int main(int argc, char **argv)
72 {
73 #if defined(ENABLE_LIBJVM)
74         char*       path;
75
76 # if defined(ENABLE_JRE_LAYOUT)
77         int         len;
78 # endif
79 #endif
80
81 #if defined(ENABLE_LIBJVM)      
82         /* Variables for JNI_CreateJavaVM dlopen call. */
83         void*       libjvm_handle;
84         void*       libjvm_vm_createjvm;
85         void*       libjvm_vm_run;
86         const char* lterror;
87
88         bool (*vm_createjvm)(JavaVM **, void **, void *);
89         void (*vm_run)(JavaVM *, JavaVMInitArgs *);
90 #endif
91
92         JavaVM         *vm;                 /* denotes a Java VM                  */
93         JNIEnv         *env;
94         JavaVMInitArgs *vm_args;
95
96         /* prepare the options */
97
98         vm_args = cacao_options_prepare(argc, argv);
99         
100         /* load and initialize a Java VM, return a JNI interface pointer in env */
101
102 #if defined(ENABLE_LIBJVM)
103 # if defined(ENABLE_JRE_LAYOUT)
104         /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
105
106         path = malloc(sizeof(char) * 4096);
107
108         if (readlink("/proc/self/exe", path, 4095) == -1) {
109                 fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
110                 abort();
111         }
112
113         /* get the path of the current executable */
114
115         path = dirname(path);
116         len  = strlen(path) + strlen("/../lib/"LIBJVM_NAME) + strlen("0");
117
118         if (len > 4096) {
119                 fprintf(stderr, "main: libjvm name to long for buffer\n");
120                 abort();
121         }
122
123         /* concatinate the library name */
124
125         strcat(path, "/../lib/"LIBJVM_NAME);
126 # else
127         path = CACAO_LIBDIR"/"LIBJVM_NAME;
128 # endif
129
130         /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
131            If not found, try the absolute path. */
132
133         libjvm_handle = system_dlopen(LIBJVM_NAME, RTLD_LAZY);
134
135         if (libjvm_handle == NULL) {
136                 /* save the error message */
137
138                 lterror = strdup(system_dlerror());
139
140                 libjvm_handle = system_dlopen(path, RTLD_LAZY);
141
142                 if (libjvm_handle == NULL) {
143                         /* print the first error message too */
144
145                         fprintf(stderr, "main: system_dlopen failed: %s\n", lterror);
146
147                         /* and now the current one */
148
149                         fprintf(stderr, "main: system_dlopen failed: %s\n",
150                                         system_dlerror());
151                         abort();
152                 }
153
154                 /* free the error string */
155
156                 free((void *) lterror);
157         }
158
159         libjvm_vm_createjvm = system_dlsym(libjvm_handle, "vm_createjvm");
160
161         if (libjvm_vm_createjvm == NULL) {
162                 fprintf(stderr, "main: lt_dlsym failed: %s\n", system_dlerror());
163                 abort();
164         }
165
166         vm_createjvm =
167                 (bool (*)(JavaVM **, void **, void *)) (ptrint) libjvm_vm_createjvm;
168 #endif
169
170         /* create the Java VM */
171
172         (void) vm_createjvm(&vm, (void *) &env, vm_args);
173
174 #if defined(ENABLE_JVMTI)
175         mutex_init(&dbgcomlock);
176         if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
177 #endif
178
179 #if defined(ENABLE_LIBJVM)
180         libjvm_vm_run = system_dlsym(libjvm_handle, "vm_run");
181
182         if (libjvm_vm_run == NULL) {
183                 fprintf(stderr, "main: system_dlsym failed: %s\n", system_dlerror());
184                 abort();
185         }
186
187         vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
188 #endif
189
190         /* run the VM */
191
192         vm_run(vm, vm_args);
193
194         /* keep compiler happy */
195
196         return 0;
197 }
198
199
200 /* cacao_options_prepare *******************************************************
201
202    Prepare the JavaVMInitArgs.
203
204 *******************************************************************************/
205
206 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv)
207 {
208         JavaVMInitArgs *vm_args;
209         s4              i;
210
211         vm_args = malloc(sizeof(JavaVMInitArgs));
212
213         vm_args->version            = JNI_VERSION_1_2;
214         vm_args->nOptions           = argc - 1;
215         vm_args->options            = malloc(sizeof(JavaVMOption) * argc);
216         vm_args->ignoreUnrecognized = JNI_FALSE;
217
218         for (i = 1; i < argc; i++)
219                 vm_args->options[i - 1].optionString = argv[i];
220
221         return vm_args;
222 }
223
224
225 /*
226  * These are local overrides for various environment variables in Emacs.
227  * Please do not remove this and leave it at the end of the file, where
228  * Emacs will automagically detect them.
229  * ---------------------------------------------------------------------
230  * Local variables:
231  * mode: c
232  * indent-tabs-mode: t
233  * c-basic-offset: 4
234  * tab-width: 4
235  * End:
236  */