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