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