* src/threads/Makefile.am: Added mutex.h.
[cacao.git] / src / cacao / cacao.c
1 /* src/cacao/cacao.c - contains main() of cacao
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31
32 #if defined(ENABLE_LIBJVM)
33 # include <ltdl.h>
34 #endif
35
36 #if defined(WITH_JRE_LAYOUT)
37 # include <errno.h>
38 # include <libgen.h>
39 # include <unistd.h>
40 #endif
41
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "vm/types.h"
46
47 #include "native/jni.h"
48
49 #if defined(ENABLE_JVMTI)
50 # include "native/jvmti/jvmti.h"
51 # include "native/jvmti/cacaodbg.h"
52 # include "threads/mutex.h"
53 #endif
54
55 #include "vm/vm.h"
56
57
58 /* forward declarations *******************************************************/
59
60 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv);
61
62
63 /* main ************************************************************************
64
65    The main program.
66    
67 *******************************************************************************/
68
69 int main(int argc, char **argv)
70 {
71 #if defined(ENABLE_LIBJVM)
72         char           *path;
73 #endif
74
75 #if defined(ENABLE_LIBJVM)      
76         /* Variables for JNI_CreateJavaVM dlopen call. */
77         lt_dlhandle     libjvm_handle;
78         lt_ptr          libjvm_vm_createjvm;
79         lt_ptr          libjvm_vm_run;
80         const char     *lterror;
81
82         bool (*vm_createjvm)(JavaVM **, void **, void *);
83         void (*vm_run)(JavaVM *, JavaVMInitArgs *);
84 #endif
85
86         JavaVM         *vm;                 /* denotes a Java VM                  */
87         JNIEnv         *env;
88         JavaVMInitArgs *vm_args;
89
90         /* prepare the options */
91
92         vm_args = cacao_options_prepare(argc, argv);
93         
94         /* load and initialize a Java VM, return a JNI interface pointer in env */
95
96 #if defined(ENABLE_LIBJVM)
97 # if defined(WITH_JRE_LAYOUT)
98         /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
99
100         path = malloc(sizeof(char) * 4096);
101
102         if (readlink("/proc/self/exe", path, 4095) == -1) {
103                 fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
104                 abort();
105         }
106
107         /* get the path of the current executable */
108
109         path = dirname(path);
110
111         if ((strlen(path) + strlen("/../lib/libjvm") + strlen("0")) > 4096) {
112                 fprintf(stderr, "main: libjvm name to long for buffer\n");
113                 abort();
114         }
115
116         /* concatinate the library name */
117
118         strcat(path, "/../lib/libjvm");
119 # else
120         path = CACAO_LIBDIR"/libjvm";
121 # endif
122
123         if (lt_dlinit()) {
124                 fprintf(stderr, "main: lt_dlinit failed: %s\n", lt_dlerror());
125                 abort();
126         }
127
128         /* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
129            If not found, try the absolute path. */
130
131         if (!(libjvm_handle = lt_dlopenext("libjvm"))) {
132                 /* save the error message */
133
134                 lterror = strdup(lt_dlerror());
135
136                 if (!(libjvm_handle = lt_dlopenext(path))) {
137                         /* print the first error message too */
138
139                         fprintf(stderr, "main: lt_dlopenext failed: %s\n", lterror);
140
141                         /* and now the current one */
142
143                         fprintf(stderr, "main: lt_dlopenext failed: %s\n", lt_dlerror());
144                         abort();
145                 }
146
147                 /* free the error string */
148
149                 free((void *) lterror);
150         }
151
152         if (!(libjvm_vm_createjvm = lt_dlsym(libjvm_handle, "vm_createjvm"))) {
153                 fprintf(stderr, "main: lt_dlsym failed: %s\n", lt_dlerror());
154                 abort();
155         }
156
157         vm_createjvm =
158                 (bool (*)(JavaVM **, void **, void *)) (ptrint) libjvm_vm_createjvm;
159 #endif
160
161         /* create the Java VM */
162
163         (void) vm_createjvm(&vm, (void *) &env, vm_args);
164
165 #if defined(ENABLE_JVMTI)
166         mutex_init(&dbgcomlock);
167         if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
168 #endif
169
170 #if defined(ENABLE_LIBJVM)
171         if (!(libjvm_vm_run = lt_dlsym(libjvm_handle, "vm_run"))) {
172                 fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
173                 abort();
174         }
175
176         vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
177 #endif
178
179         /* run the VM */
180
181         vm_run(vm, vm_args);
182
183         /* keep compiler happy */
184
185         return 0;
186 }
187
188
189 /* cacao_options_prepare *******************************************************
190
191    Prepare the JavaVMInitArgs.
192
193 *******************************************************************************/
194
195 static JavaVMInitArgs *cacao_options_prepare(int argc, char **argv)
196 {
197         JavaVMInitArgs *vm_args;
198         s4              i;
199
200         vm_args = malloc(sizeof(JavaVMInitArgs));
201
202         vm_args->version            = JNI_VERSION_1_2;
203         vm_args->nOptions           = argc - 1;
204         vm_args->options            = malloc(sizeof(JavaVMOption) * argc);
205         vm_args->ignoreUnrecognized = JNI_FALSE;
206
207         for (i = 1; i < argc; i++)
208                 vm_args->options[i - 1].optionString = argv[i];
209
210         return vm_args;
211 }
212
213
214 /*
215  * These are local overrides for various environment variables in Emacs.
216  * Please do not remove this and leave it at the end of the file, where
217  * Emacs will automagically detect them.
218  * ---------------------------------------------------------------------
219  * Local variables:
220  * mode: c
221  * indent-tabs-mode: t
222  * c-basic-offset: 4
223  * tab-width: 4
224  * End:
225  */