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