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