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 5195 2006-07-31 15:26:10Z twisti $
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(ENABLE_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);
185 /* main ************************************************************************
189 *******************************************************************************/
191 int main(int argc, char **argv)
195 /* local variables ********************************************************/
197 JavaVMInitArgs *vm_args;
198 JavaVM *jvm; /* denotes a Java VM */
200 if (atexit(vm_exit_handler))
201 throw_cacao_exception_exit(string_java_lang_InternalError,
202 "Unable to register exit_handler");
205 /**************************** Program start *****************************/
208 log_text("CACAO started -------------------------------------------------------");
210 /* prepare the options */
212 vm_args = options_prepare(argc, argv);
214 /* load and initialize a Java VM, return a JNI interface pointer in env */
216 JNI_CreateJavaVM(&jvm, (void *) &_Jv_env, vm_args);
218 #if defined(ENABLE_JVMTI)
219 pthread_mutex_init(&dbgcomlock,NULL);
220 if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
223 /* do we have a main class? */
225 if (mainstring == NULL)
229 /* start worker routines **************************************************/
231 if (opt_run == true) {
233 classinfo *mainclass;
235 java_objectarray *oa;
240 /* set return value to OK */
244 if (opt_jar == true) {
245 /* open jar file with java.util.jar.JarFile */
246 mainstring = getmainclassnamefromjar(mainstring);
249 /* load the main class */
251 mainutf = utf_new_char(mainstring);
253 if (!(mainclass = load_class_from_sysloader(mainutf)))
254 throw_main_exception_exit();
256 /* error loading class, clear exceptionptr for new exception */
258 if (*exceptionptr || !mainclass) {
259 /* *exceptionptr = NULL; */
261 /* *exceptionptr = */
262 /* new_exception_message(string_java_lang_NoClassDefFoundError, */
264 throw_main_exception_exit();
267 if (!link_class(mainclass))
268 throw_main_exception_exit();
270 /* find the `main' method of the main class */
272 m = class_resolveclassmethod(mainclass,
273 utf_new_char("main"),
274 utf_new_char("([Ljava/lang/String;)V"),
275 class_java_lang_Object,
279 throw_main_exception_exit();
282 /* there is no main method or it isn't static */
284 if (!m || !(m->flags & ACC_STATIC)) {
285 *exceptionptr = NULL;
288 new_exception_message(string_java_lang_NoSuchMethodError,
290 throw_main_exception_exit();
293 /* build argument array */
295 oalength = vm_args->nOptions - opt_index;
297 oa = builtin_anewarray(oalength, class_java_lang_String);
299 for (i = 0; i < oalength; i++) {
300 s = javastring_new(utf_new_char(vm_args->options[opt_index + i].optionString));
301 oa->data[i] = (java_objectheader *) s;
304 #ifdef TYPEINFO_DEBUG_TEST
305 /* test the typeinfo system */
308 /*class_showmethods(currentThread->group->header.vftbl->class); */
310 #if defined(ENABLE_JVMTI)
311 jvmti_set_phase(JVMTI_PHASE_LIVE);
314 /* increase total started thread count */
316 _Jv_jvm->total_started_thread_count++;
318 /* start the main thread */
320 (void) vm_call_method(m, NULL, oa);
322 /* exception occurred? */
325 throw_main_exception();
329 /* unload the JavaVM */
339 /* If requested, compile all methods. *************************************/
347 classcache_name_entry *nmen;
348 classcache_class_entry *clsen;
350 /* create all classes found in the bootclasspath */
351 /* XXX currently only works with zip/jar's */
353 loader_load_all_classes();
355 /* link all classes */
357 for (slot = 0; slot < hashtable_classcache.size; slot++) {
358 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
360 for (; nmen; nmen = nmen->hashlink) {
361 /* iterate over all class entries */
363 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
369 if (!(c->state & CLASS_LINKED)) {
370 if (!link_class(c)) {
371 fprintf(stderr, "Error linking: ");
372 utf_fprint_printable_ascii_classname(stderr, c->name);
373 fprintf(stderr, "\n");
375 /* print out exception and cause */
377 exceptions_print_exception(*exceptionptr);
379 /* goto next class */
385 /* compile all class methods */
387 for (i = 0; i < c->methodscount; i++) {
388 m = &(c->methods[i]);
391 if (!jit_compile(m)) {
392 fprintf(stderr, "Error compiling: ");
393 utf_fprint_printable_ascii_classname(stderr, c->name);
394 fprintf(stderr, ".");
395 utf_fprint_printable_ascii(stderr, m->name);
396 utf_fprint_printable_ascii(stderr, m->descriptor);
397 fprintf(stderr, "\n");
399 /* print out exception and cause */
401 exceptions_print_exception(*exceptionptr);
409 #endif /* !defined(NDEBUG) */
412 /* If requested, compile a specific method. *******************************/
415 if (opt_method != NULL) {
418 /* create, load and link the main class */
420 if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
421 throw_main_exception_exit();
423 if (!link_class(mainclass))
424 throw_main_exception_exit();
426 if (opt_signature != NULL) {
427 m = class_resolveclassmethod(mainclass,
428 utf_new_char(opt_method),
429 utf_new_char(opt_signature),
433 m = class_resolveclassmethod(mainclass,
434 utf_new_char(opt_method),
441 char message[MAXLOGTEXT];
442 sprintf(message, "%s%s", opt_method,
443 opt_signature ? opt_signature : "");
446 new_exception_message(string_java_lang_NoSuchMethodException,
449 throw_main_exception_exit();
456 #endif /* !defined(NDEBUG) */
458 /* keep compiler happy */
465 * These are local overrides for various environment variables in Emacs.
466 * Please do not remove this and leave it at the end of the file, where
467 * Emacs will automagically detect them.
468 * ---------------------------------------------------------------------
471 * indent-tabs-mode: t