1 /* src/cacao/cacao.c - contains main() of cacao
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
34 $Id: cacao.c 4892 2006-05-06 18:29:55Z motse $
47 #include "native/jni.h"
48 #include "native/include/java_lang_String.h"
50 #if defined(ENABLE_JVMTI)
51 #include "native/jvmti/jvmti.h"
52 #include "native/jvmti/cacaodbg.h"
54 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
59 #include "toolbox/logging.h"
60 #include "vm/classcache.h"
61 #include "vm/exceptions.h"
62 #include "vm/global.h"
63 #include "vm/loader.h"
64 #include "vm/options.h"
65 #include "vm/statistics.h"
66 #include "vm/stringlocal.h"
69 #include "vm/jit/asmpart.h"
70 #include "vm/jit/jit.h"
72 #ifdef TYPEINFO_DEBUG_TEST
73 #include "vm/jit/verify/typeinfo.h"
76 #ifdef TYPECHECK_STATISTICS
77 void typecheck_print_statistics(FILE *file);
80 /* getmainclassfromjar *********************************************************
82 Gets the name of the main class form a JAR's manifest file.
84 *******************************************************************************/
86 static char *getmainclassnamefromjar(char *mainstring)
93 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
96 throw_main_exception_exit();
98 /* create JarFile object */
103 throw_main_exception_exit();
106 m = class_resolveclassmethod(c,
108 utf_java_lang_String__void,
109 class_java_lang_Object,
113 throw_main_exception_exit();
115 s = javastring_new_from_ascii(mainstring);
117 (void) vm_call_method(m, o, s);
120 throw_main_exception_exit();
122 /* get manifest object */
124 m = class_resolveclassmethod(c,
125 utf_new_char("getManifest"),
126 utf_new_char("()Ljava/util/jar/Manifest;"),
127 class_java_lang_Object,
131 throw_main_exception_exit();
133 o = vm_call_method(m, o);
136 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
141 /* get Main Attributes */
143 m = class_resolveclassmethod(o->vftbl->class,
144 utf_new_char("getMainAttributes"),
145 utf_new_char("()Ljava/util/jar/Attributes;"),
146 class_java_lang_Object,
150 throw_main_exception_exit();
152 o = vm_call_method(m, o);
155 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
160 /* get property Main-Class */
162 m = class_resolveclassmethod(o->vftbl->class,
163 utf_new_char("getValue"),
164 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
165 class_java_lang_Object,
169 throw_main_exception_exit();
171 s = javastring_new_from_ascii("Main-Class");
173 o = vm_call_method(m, o, s);
176 throw_main_exception_exit();
178 return javastring_tochar(o);
181 void exit_handler(void);
184 /* main ************************************************************************
188 *******************************************************************************/
190 int main(int argc, char **argv)
192 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
197 /* local variables ********************************************************/
199 JavaVMInitArgs *vm_args;
200 JavaVM *jvm; /* denotes a Java VM */
202 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
203 stackbottom = &dummy;
206 if (atexit(vm_exit_handler))
207 throw_cacao_exception_exit(string_java_lang_InternalError,
208 "Unable to register exit_handler");
211 /**************************** Program start *****************************/
214 log_text("CACAO started -------------------------------------------------------");
216 /* prepare the options */
218 vm_args = options_prepare(argc, argv);
220 /* load and initialize a Java VM, return a JNI interface pointer in env */
222 JNI_CreateJavaVM(&jvm, (void **) &_Jv_env, vm_args);
224 #if defined(ENABLE_JVMTI)
225 pthread_mutex_init(&dbgcomlock,NULL);
226 set_jvmti_phase(JVMTI_PHASE_START);
229 /* do we have a main class? */
231 if (mainstring == NULL)
235 /* start worker routines **************************************************/
237 if (opt_run == true) {
239 classinfo *mainclass;
241 java_objectarray *oa;
246 /* set return value to OK */
250 if (opt_jar == true) {
251 /* open jar file with java.util.jar.JarFile */
252 mainstring = getmainclassnamefromjar(mainstring);
255 /* load the main class */
257 mainutf = utf_new_char(mainstring);
259 if (!(mainclass = load_class_from_sysloader(mainutf)))
260 throw_main_exception_exit();
262 /* error loading class, clear exceptionptr for new exception */
264 if (*exceptionptr || !mainclass) {
265 /* *exceptionptr = NULL; */
267 /* *exceptionptr = */
268 /* new_exception_message(string_java_lang_NoClassDefFoundError, */
270 throw_main_exception_exit();
273 if (!link_class(mainclass))
274 throw_main_exception_exit();
276 /* find the `main' method of the main class */
278 m = class_resolveclassmethod(mainclass,
279 utf_new_char("main"),
280 utf_new_char("([Ljava/lang/String;)V"),
281 class_java_lang_Object,
285 throw_main_exception_exit();
288 /* there is no main method or it isn't static */
290 if (!m || !(m->flags & ACC_STATIC)) {
291 *exceptionptr = NULL;
294 new_exception_message(string_java_lang_NoSuchMethodError,
296 throw_main_exception_exit();
299 /* build argument array */
301 oalength = vm_args->nOptions - opt_index;
303 oa = builtin_anewarray(oalength, class_java_lang_String);
305 for (i = 0; i < oalength; i++) {
306 s = javastring_new(utf_new_char(vm_args->options[opt_index + i].optionString));
307 oa->data[i] = (java_objectheader *) s;
310 #ifdef TYPEINFO_DEBUG_TEST
311 /* test the typeinfo system */
314 /*class_showmethods(currentThread->group->header.vftbl->class); */
316 #if defined(ENABLE_JVMTI)
317 /* start the jdwp listening */
319 log_text("cacao vm - init VMjdwp");
320 if (!VMjdwpInit()) exit(1);
321 setup_jdwp_thread(transport);
323 fprintf(stderr,"suspend false -> continue debuggee\n");
325 fprintf(stderr,"suspend true -> do no continue debuggee(todo)\n");
330 set_jvmti_phase(JVMTI_PHASE_LIVE);
332 log_text("debuggee: herewe go");
335 (void) vm_call_method(m, NULL, oa);
337 /* exception occurred? */
340 throw_main_exception();
344 /* unload the JavaVM */
354 /* If requested, compile all methods. *************************************/
362 classcache_name_entry *nmen;
363 classcache_class_entry *clsen;
365 /* create all classes found in the bootclasspath */
366 /* XXX currently only works with zip/jar's */
368 loader_load_all_classes();
370 /* link all classes */
372 for (slot = 0; slot < hashtable_classcache.size; slot++) {
373 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
375 for (; nmen; nmen = nmen->hashlink) {
376 /* iterate over all class entries */
378 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
384 if (!(c->state & CLASS_LINKED)) {
385 if (!link_class(c)) {
386 fprintf(stderr, "Error linking: ");
387 utf_fprint_printable_ascii_classname(stderr, c->name);
388 fprintf(stderr, "\n");
390 /* print out exception and cause */
392 exceptions_print_exception(*exceptionptr);
394 /* goto next class */
400 /* compile all class methods */
402 for (i = 0; i < c->methodscount; i++) {
403 m = &(c->methods[i]);
406 if (!jit_compile(m)) {
407 fprintf(stderr, "Error compiling: ");
408 utf_fprint_printable_ascii_classname(stderr, c->name);
409 fprintf(stderr, ".");
410 utf_fprint_printable_ascii(stderr, m->name);
411 utf_fprint_printable_ascii(stderr, m->descriptor);
412 fprintf(stderr, "\n");
414 /* print out exception and cause */
416 exceptions_print_exception(*exceptionptr);
424 #endif /* !defined(NDEBUG) */
427 /* If requested, compile a specific method. *******************************/
430 if (opt_method != NULL) {
433 /* create, load and link the main class */
435 if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
436 throw_main_exception_exit();
438 if (!link_class(mainclass))
439 throw_main_exception_exit();
441 if (opt_signature != NULL) {
442 m = class_resolveclassmethod(mainclass,
443 utf_new_char(opt_method),
444 utf_new_char(opt_signature),
448 m = class_resolveclassmethod(mainclass,
449 utf_new_char(opt_method),
456 char message[MAXLOGTEXT];
457 sprintf(message, "%s%s", opt_method,
458 opt_signature ? opt_signature : "");
461 new_exception_message(string_java_lang_NoSuchMethodException,
464 throw_main_exception_exit();
471 #endif /* !defined(NDEBUG) */
473 /* keep compiler happy */
480 * These are local overrides for various environment variables in Emacs.
481 * Please do not remove this and leave it at the end of the file, where
482 * Emacs will automagically detect them.
483 * ---------------------------------------------------------------------
486 * indent-tabs-mode: t