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 4688 2006-03-24 11:50:00Z 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"
53 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
58 #include "toolbox/logging.h"
59 #include "vm/classcache.h"
60 #include "vm/exceptions.h"
61 #include "vm/global.h"
62 #include "vm/loader.h"
63 #include "vm/options.h"
64 #include "vm/statistics.h"
65 #include "vm/stringlocal.h"
68 #include "vm/jit/asmpart.h"
69 #include "vm/jit/jit.h"
71 #ifdef TYPEINFO_DEBUG_TEST
72 #include "vm/jit/verify/typeinfo.h"
75 #ifdef TYPECHECK_STATISTICS
76 void typecheck_print_statistics(FILE *file);
79 /* getmainclassfromjar *********************************************************
81 Gets the name of the main class form a JAR's manifest file.
83 *******************************************************************************/
85 static char *getmainclassnamefromjar(char *mainstring)
92 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
95 throw_main_exception_exit();
97 /* create JarFile object */
102 throw_main_exception_exit();
105 m = class_resolveclassmethod(c,
107 utf_java_lang_String__void,
108 class_java_lang_Object,
112 throw_main_exception_exit();
114 s = javastring_new_char(mainstring);
116 (void) vm_call_method(m, o, s);
119 throw_main_exception_exit();
121 /* get manifest object */
123 m = class_resolveclassmethod(c,
124 utf_new_char("getManifest"),
125 utf_new_char("()Ljava/util/jar/Manifest;"),
126 class_java_lang_Object,
130 throw_main_exception_exit();
132 o = vm_call_method(m, o);
135 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
140 /* get Main Attributes */
142 m = class_resolveclassmethod(o->vftbl->class,
143 utf_new_char("getMainAttributes"),
144 utf_new_char("()Ljava/util/jar/Attributes;"),
145 class_java_lang_Object,
149 throw_main_exception_exit();
151 o = vm_call_method(m, o);
154 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
159 /* get property Main-Class */
161 m = class_resolveclassmethod(o->vftbl->class,
162 utf_new_char("getValue"),
163 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
164 class_java_lang_Object,
168 throw_main_exception_exit();
170 s = javastring_new_char("Main-Class");
172 o = vm_call_method(m, o, s);
175 throw_main_exception_exit();
177 return javastring_tochar(o);
180 void exit_handler(void);
183 /* main ************************************************************************
187 *******************************************************************************/
189 int main(int argc, char **argv)
191 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
196 /* local variables ********************************************************/
198 JavaVMInitArgs *vm_args;
199 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 if (dbgprocess && jvmti && jdwp) /* is this the parent/debugger process ? */
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); */
318 #if defined(ENABLE_JVMTI)
319 /* if this is the parent process than start the jdwp listening */
321 fprintf(stderr, "jdwp/debugger set herewego brkpt %p\n",&&herewego);
322 setsysbrkpt(HEREWEGOBRK,&&herewego);
323 if (dbgprocess && jdwp) cacaodbglisten(transport);
328 fprintf(stderr,"debuggee: herewe go\n");
334 (void) vm_call_method(m, NULL, oa);
336 /* exception occurred? */
339 throw_main_exception();
343 /* unload the JavaVM */
352 /************* If requested, compile all methods ********************/
359 classcache_name_entry *nmen;
360 classcache_class_entry *clsen;
362 /* create all classes found in the bootclasspath */
363 /* XXX currently only works with zip/jar's */
365 loader_load_all_classes();
367 /* link all classes */
369 for (slot = 0; slot < hashtable_classcache.size; slot++) {
370 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
372 for (; nmen; nmen = nmen->hashlink) {
373 /* iterate over all class entries */
375 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
381 if (!(c->state & CLASS_LINKED)) {
382 if (!link_class(c)) {
383 fprintf(stderr, "Error linking: ");
384 utf_fprint_classname(stderr, c->name);
385 fprintf(stderr, "\n");
387 /* print out exception and cause */
389 exceptions_print_exception(*exceptionptr);
391 /* goto next class */
397 /* compile all class methods */
399 for (i = 0; i < c->methodscount; i++) {
400 m = &(c->methods[i]);
403 if (!jit_compile(m)) {
404 fprintf(stderr, "Error compiling: ");
405 utf_fprint_classname(stderr, c->name);
406 fprintf(stderr, ".");
407 utf_fprint(stderr, m->name);
408 utf_fprint(stderr, m->descriptor);
409 fprintf(stderr, "\n");
411 /* print out exception and cause */
413 exceptions_print_exception(*exceptionptr);
423 /******** If requested, compile a specific method ***************/
425 if (opt_method != NULL) {
428 /* create, load and link the main class */
430 if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
431 throw_main_exception_exit();
433 if (!link_class(mainclass))
434 throw_main_exception_exit();
436 if (opt_signature != NULL) {
437 m = class_resolveclassmethod(mainclass,
438 utf_new_char(opt_method),
439 utf_new_char(opt_signature),
443 m = class_resolveclassmethod(mainclass,
444 utf_new_char(opt_method),
451 char message[MAXLOGTEXT];
452 sprintf(message, "%s%s", opt_method,
453 opt_signature ? opt_signature : "");
456 new_exception_message(string_java_lang_NoSuchMethodException,
459 throw_main_exception_exit();
467 /* keep compiler happy */
474 * These are local overrides for various environment variables in Emacs.
475 * Please do not remove this and leave it at the end of the file, where
476 * Emacs will automagically detect them.
477 * ---------------------------------------------------------------------
480 * indent-tabs-mode: t