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