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