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