1 /* src/vm/exceptions.c - exception related functions
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, 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: Christian Thalinger
29 Changes: Edwin Steiner
31 $Id: exceptions.c 4921 2006-05-15 14:24:36Z twisti $
45 #include "mm/memory.h"
46 #include "native/native.h"
47 #include "native/include/java_lang_String.h"
48 #include "native/include/java_lang_Throwable.h"
49 #include "toolbox/logging.h"
50 #include "toolbox/util.h"
52 #include "vm/exceptions.h"
53 #include "vm/global.h"
54 #include "vm/loader.h"
55 #include "vm/options.h"
56 #include "vm/stringlocal.h"
58 #include "vm/jit/asmpart.h"
59 #include "vm/jit/jit.h"
60 #include "vm/jit/methodheader.h"
63 /* for raising exceptions from native methods *********************************/
65 #if !defined(ENABLE_THREADS)
66 java_objectheader *_no_threads_exceptionptr = NULL;
70 /* init_system_exceptions ******************************************************
72 Load and link exceptions used in the system.
74 *******************************************************************************/
76 bool exceptions_init(void)
78 /* java/lang/Throwable */
80 if (!(class_java_lang_Throwable =
81 load_class_bootstrap(utf_java_lang_Throwable)) ||
82 !link_class(class_java_lang_Throwable))
86 /* java/lang/VMThrowable */
88 if (!(class_java_lang_VMThrowable =
89 load_class_bootstrap(utf_java_lang_VMThrowable)) ||
90 !link_class(class_java_lang_VMThrowable))
96 if (!(class_java_lang_Error = load_class_bootstrap(utf_java_lang_Error)) ||
97 !link_class(class_java_lang_Error))
100 /* java/lang/NoClassDefFoundError */
102 if (!(class_java_lang_NoClassDefFoundError =
103 load_class_bootstrap(utf_java_lang_NoClassDefFoundError)) ||
104 !link_class(class_java_lang_NoClassDefFoundError))
107 /* java/lang/LinkageError */
109 if (!(class_java_lang_LinkageError =
110 load_class_bootstrap(utf_java_lang_LinkageError)) ||
111 !link_class(class_java_lang_LinkageError))
114 /* java/lang/NoSuchMethodError */
116 if (!(class_java_lang_NoSuchMethodError =
117 load_class_bootstrap(utf_java_lang_NoSuchMethodError)) ||
118 !link_class(class_java_lang_NoSuchMethodError))
121 /* java/lang/OutOfMemoryError */
123 if (!(class_java_lang_OutOfMemoryError =
124 load_class_bootstrap(utf_java_lang_OutOfMemoryError)) ||
125 !link_class(class_java_lang_OutOfMemoryError))
129 /* java/lang/Exception */
131 if (!(class_java_lang_Exception =
132 load_class_bootstrap(utf_java_lang_Exception)) ||
133 !link_class(class_java_lang_Exception))
136 /* java/lang/ClassNotFoundException */
138 if (!(class_java_lang_ClassNotFoundException =
139 load_class_bootstrap(utf_java_lang_ClassNotFoundException)) ||
140 !link_class(class_java_lang_ClassNotFoundException))
143 /* java/lang/IllegalArgumentException */
145 if (!(class_java_lang_IllegalArgumentException =
146 load_class_bootstrap(utf_java_lang_IllegalArgumentException)) ||
147 !link_class(class_java_lang_IllegalArgumentException))
150 /* java/lang/IllegalMonitorStateException */
152 if (!(class_java_lang_IllegalMonitorStateException =
153 load_class_bootstrap(utf_java_lang_IllegalMonitorStateException)) ||
154 !link_class(class_java_lang_IllegalMonitorStateException))
157 /* java/lang/NullPointerException */
159 if (!(class_java_lang_NullPointerException =
160 load_class_bootstrap(utf_java_lang_NullPointerException)) ||
161 !link_class(class_java_lang_NullPointerException))
169 static void throw_exception_exit_intern(bool doexit)
171 java_objectheader *xptr;
175 xptr = *exceptionptr;
178 /* clear exception, because we are calling jit code again */
179 *exceptionptr = NULL;
181 c = xptr->vftbl->class;
183 pss = class_resolveclassmethod(c,
186 class_java_lang_Object,
189 /* print the stacktrace */
192 (void) vm_call_method(pss, xptr);
194 /* This normally means, we are EXTREMLY out of memory or have a */
195 /* serious problem while printStackTrace. But may be another */
196 /* exception, so print it. */
199 java_lang_Throwable *t;
201 t = (java_lang_Throwable *) *exceptionptr;
203 fprintf(stderr, "Exception while printStackTrace(): ");
204 utf_fprint_printable_ascii_classname(stderr, t->header.vftbl->class->name);
206 if (t->detailMessage) {
209 buf = javastring_tochar((java_objectheader *) t->detailMessage);
210 fprintf(stderr, ": %s", buf);
211 MFREE(buf, char, strlen(buf));
214 fprintf(stderr, "\n");
218 utf_fprint_printable_ascii_classname(stderr, c->name);
219 fprintf(stderr, ": printStackTrace()V not found!\n");
232 void throw_exception(void)
234 throw_exception_exit_intern(false);
238 void throw_exception_exit(void)
240 throw_exception_exit_intern(true);
244 void throw_main_exception(void)
246 fprintf(stderr, "Exception in thread \"main\" ");
249 throw_exception_exit_intern(false);
253 void throw_main_exception_exit(void)
255 fprintf(stderr, "Exception in thread \"main\" ");
258 throw_exception_exit_intern(true);
262 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
269 len = strlen(exception);
270 tmp = MNEW(char, len + 1);
271 strncpy(tmp, exception, len);
274 /* convert to classname */
276 for (i = len - 1; i >= 0; i--)
277 if (tmp[i] == '/') tmp[i] = '.';
279 fprintf(stderr, "Exception in thread \"main\" %s", tmp);
281 MFREE(tmp, char, len);
283 if (strlen(message) > 0) {
284 fprintf(stderr, ": ");
286 va_start(ap, message);
287 vfprintf(stderr, message, ap);
291 fprintf(stderr, "\n");
300 /* exceptions_throw_outofmemory_exit *******************************************
302 Just print an: java.lang.InternalError: Out of memory
304 *******************************************************************************/
306 void exceptions_throw_outofmemory_exit(void)
308 throw_cacao_exception_exit(string_java_lang_InternalError,
313 /* new_exception ***************************************************************
315 Creates an exception object with the given name and initalizes it.
318 classname....class name in UTF-8
321 an exception pointer (in any case -- either it is the newly created
322 exception, or an exception thrown while trying to create it).
324 *******************************************************************************/
326 java_objectheader *new_exception(const char *classname)
328 java_objectheader *o;
331 if (!(c = load_class_bootstrap(utf_new_char(classname))))
332 return *exceptionptr;
334 o = native_new_and_init(c);
337 return *exceptionptr;
343 /* new_exception_message *******************************************************
345 Creates an exception object with the given name and initalizes it
346 with the given char message.
349 classname....class name in UTF-8
350 message......message in UTF-8
353 an exception pointer (in any case -- either it is the newly created
354 exception, or an exception thrown while trying to create it).
356 *******************************************************************************/
358 java_objectheader *new_exception_message(const char *classname,
363 s = javastring_new_from_utf_string(message);
365 return *exceptionptr;
367 return new_exception_javastring(classname, s);
371 /* new_exception_throwable *****************************************************
373 Creates an exception object with the given name and initalizes it
374 with the given java/lang/Throwable exception.
377 classname....class name in UTF-8
378 throwable....the given Throwable
381 an exception pointer (in any case -- either it is the newly created
382 exception, or an exception thrown while trying to create it).
384 *******************************************************************************/
386 java_objectheader *new_exception_throwable(const char *classname,
387 java_lang_Throwable *throwable)
389 java_objectheader *o;
392 if (!(c = load_class_bootstrap(utf_new_char(classname))))
393 return *exceptionptr;
395 o = native_new_and_init_throwable(c, throwable);
398 return *exceptionptr;
404 /* new_exception_utfmessage ****************************************************
406 Creates an exception object with the given name and initalizes it
407 with the given utf message.
410 classname....class name in UTF-8
411 message......the message as an utf *
414 an exception pointer (in any case -- either it is the newly created
415 exception, or an exception thrown while trying to create it).
417 *******************************************************************************/
419 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
423 s = javastring_new(message);
425 return *exceptionptr;
427 return new_exception_javastring(classname, s);
431 /* new_exception_javastring ****************************************************
433 Creates an exception object with the given name and initalizes it
434 with the given java/lang/String message.
437 classname....class name in UTF-8
438 message......the message as a java.lang.String
441 an exception pointer (in any case -- either it is the newly created
442 exception, or an exception thrown while trying to create it).
444 *******************************************************************************/
446 java_objectheader *new_exception_javastring(const char *classname,
447 java_lang_String *message)
449 java_objectheader *o;
452 if (!(c = load_class_bootstrap(utf_new_char(classname))))
453 return *exceptionptr;
455 o = native_new_and_init_string(c, message);
458 return *exceptionptr;
464 /* new_exception_int ***********************************************************
466 Creates an exception object with the given name and initalizes it
467 with the given int value.
470 classname....class name in UTF-8
471 i............the integer
474 an exception pointer (in any case -- either it is the newly created
475 exception, or an exception thrown while trying to create it).
477 *******************************************************************************/
479 java_objectheader *new_exception_int(const char *classname, s4 i)
481 java_objectheader *o;
484 if (!(c = load_class_bootstrap(utf_new_char(classname))))
485 return *exceptionptr;
487 o = native_new_and_init_int(c, i);
490 return *exceptionptr;
496 /* new_classformaterror ********************************************************
498 generates a java.lang.ClassFormatError for the classloader
501 c............the class in which the error was found
502 message......UTF-8 format string
505 an exception pointer (in any case -- either it is the newly created
506 exception, or an exception thrown while trying to create it).
508 *******************************************************************************/
510 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
512 java_objectheader *o;
517 /* calculate message length */
522 msglen += utf_bytes(c->name) + strlen(" (");
524 va_start(ap, message);
525 msglen += get_variable_message_length(message, ap);
529 msglen += strlen(")");
531 msglen += strlen("0");
533 /* allocate a buffer */
535 msg = MNEW(char, msglen);
537 /* print message into allocated buffer */
540 utf_copy_classname(msg, c->name);
544 va_start(ap, message);
545 vsprintf(msg + strlen(msg), message, ap);
551 o = new_exception_message(string_java_lang_ClassFormatError, msg);
553 MFREE(msg, char, msglen);
559 /* exceptions_throw_classformaterror *******************************************
561 Generate a java.lang.ClassFormatError for the VM system and throw it.
564 c............the class in which the error was found
565 message......UTF-8 format string
568 an exception pointer (in any case -- either it is the newly created
569 exception, or an exception thrown while trying to create it).
571 *******************************************************************************/
573 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
577 va_start(ap, message);
578 *exceptionptr = new_classformaterror(c, message, ap);
583 /* new_classnotfoundexception **************************************************
585 Generates a java.lang.ClassNotFoundException for the classloader.
588 name.........name of the class not found as a utf *
591 an exception pointer (in any case -- either it is the newly created
592 exception, or an exception thrown while trying to create it).
594 *******************************************************************************/
596 java_objectheader *new_classnotfoundexception(utf *name)
598 java_objectheader *o;
601 s = javastring_new(name);
603 return *exceptionptr;
605 o = native_new_and_init_string(class_java_lang_ClassNotFoundException, s);
608 return *exceptionptr;
614 /* new_noclassdeffounderror ****************************************************
616 Generates a java.lang.NoClassDefFoundError
619 name.........name of the class not found as a utf *
622 an exception pointer (in any case -- either it is the newly created
623 exception, or an exception thrown while trying to create it).
625 *******************************************************************************/
627 java_objectheader *new_noclassdeffounderror(utf *name)
629 java_objectheader *o;
632 s = javastring_new(name);
634 return *exceptionptr;
636 o = native_new_and_init_string(class_java_lang_NoClassDefFoundError, s);
639 return *exceptionptr;
645 /* classnotfoundexception_to_noclassdeffounderror ******************************
647 Check the *exceptionptr for a ClassNotFoundException. If it is one,
648 convert it to a NoClassDefFoundError.
650 *******************************************************************************/
652 void classnotfoundexception_to_noclassdeffounderror(void)
654 java_objectheader *xptr;
655 java_objectheader *cause;
659 cause = *exceptionptr;
661 /* convert ClassNotFoundException's to NoClassDefFoundError's */
663 if (builtin_instanceof(cause, class_java_lang_ClassNotFoundException)) {
664 /* clear exception, because we are calling jit code again */
666 *exceptionptr = NULL;
668 /* create new error */
671 new_exception_javastring(string_java_lang_NoClassDefFoundError,
672 ((java_lang_Throwable *) cause)->detailMessage);
674 /* we had an exception while creating the error */
679 /* set new exception */
681 *exceptionptr = xptr;
686 /* new_internalerror ***********************************************************
688 Generates a java.lang.InternalError for the VM.
691 message......UTF-8 message format string
694 an exception pointer (in any case -- either it is the newly created
695 exception, or an exception thrown while trying to create it).
697 *******************************************************************************/
699 java_objectheader *new_internalerror(const char *message, ...)
701 java_objectheader *o;
706 /* calculate exception message length */
708 va_start(ap, message);
709 msglen = get_variable_message_length(message, ap);
712 /* allocate memory */
714 msg = MNEW(char, msglen);
716 /* generate message */
718 va_start(ap, message);
719 vsprintf(msg, message, ap);
722 /* create exception object */
724 o = new_exception_message(string_java_lang_InternalError, msg);
728 MFREE(msg, char, msglen);
734 /* exceptions_new_linkageerror *************************************************
736 Generates a java.lang.LinkageError with an error message.
739 message......UTF-8 message
740 c............class related to the error. If this is != NULL
741 the name of c is appended to the error message.
744 an exception pointer (in any case -- either it is the newly created
745 exception, or an exception thrown while trying to create it).
747 *******************************************************************************/
749 java_objectheader *exceptions_new_linkageerror(const char *message,
752 java_objectheader *o;
756 /* calculate exception message length */
758 msglen = strlen(message) + 1;
760 msglen += utf_bytes(c->name);
763 /* allocate memory */
765 msg = MNEW(char, msglen);
767 /* generate message */
771 utf_cat_classname(msg, c->name);
774 o = native_new_and_init_string(class_java_lang_LinkageError,
775 javastring_new_from_utf_string(msg));
779 MFREE(msg, char, msglen);
782 return *exceptionptr;
788 /* exceptions_new_nosuchmethoderror ********************************************
790 Generates a java.lang.NoSuchMethodError with an error message.
793 c............class in which the method was not found
794 name.........name of the method
795 desc.........descriptor of the method
798 an exception pointer (in any case -- either it is the newly created
799 exception, or an exception thrown while trying to create it).
801 *******************************************************************************/
803 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
804 utf *name, utf *desc)
806 java_objectheader *o;
810 /* calculate exception message length */
812 msglen = utf_bytes(c->name) + strlen(".") + utf_bytes(name) +
813 utf_bytes(desc) + strlen("0");
815 /* allocate memory */
817 msg = MNEW(char, msglen);
819 /* generate message */
821 utf_copy_classname(msg, c->name);
826 o = native_new_and_init_string(class_java_lang_NoSuchMethodError,
827 javastring_new_from_utf_string(msg));
831 MFREE(msg, char, msglen);
834 return *exceptionptr;
840 /* exceptions_throw_nosuchmethoderror ******************************************
842 Generates a java.lang.NoSuchMethodError with an error message.
845 c............class in which the method was not found
846 name.........name of the method
847 desc.........descriptor of the method
849 *******************************************************************************/
851 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
853 *exceptionptr = exceptions_new_nosuchmethoderror(c, name, desc);
857 /* new_unsupportedclassversionerror ********************************************
859 Generate a java.lang.UnsupportedClassVersionError for the classloader
862 c............class in which the method was not found
863 message......UTF-8 format string
866 an exception pointer (in any case -- either it is the newly created
867 exception, or an exception thrown while trying to create it).
869 *******************************************************************************/
871 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
873 java_objectheader *o;
878 /* calculate exception message length */
880 msglen = utf_bytes(c->name) + strlen(" (") + strlen(")") + strlen("0");
882 va_start(ap, message);
883 msglen += get_variable_message_length(message, ap);
886 /* allocate memory */
888 msg = MNEW(char, msglen);
890 /* generate message */
892 utf_copy_classname(msg, c->name);
895 va_start(ap, message);
896 vsprintf(msg + strlen(msg), message, ap);
901 /* create exception object */
903 o = new_exception_message(string_java_lang_UnsupportedClassVersionError,
908 MFREE(msg, char, msglen);
914 /* new_verifyerror *************************************************************
916 Generates a java.lang.VerifyError for the JIT compiler.
919 m............method in which the error was found
920 message......UTF-8 format string
923 an exception pointer (in any case -- either it is the newly created
924 exception, or an exception thrown while trying to create it).
926 *******************************************************************************/
928 java_objectheader *new_verifyerror(methodinfo *m, const char *message, ...)
930 java_objectheader *o;
935 useinlining = false; /* at least until sure inlining works with exceptions*/
937 /* calculate exception message length */
942 msglen = strlen("(class: ") + utf_bytes(m->class->name) +
943 strlen(", method: ") + utf_bytes(m->name) +
944 strlen(" signature: ") + utf_bytes(m->descriptor) +
945 strlen(") ") + strlen("0");
947 va_start(ap, message);
948 msglen += get_variable_message_length(message, ap);
951 /* allocate memory */
953 msg = MNEW(char, msglen);
955 /* generate message */
958 strcpy(msg, "(class: ");
959 utf_cat_classname(msg, m->class->name);
960 strcat(msg, ", method: ");
961 utf_cat(msg, m->name);
962 strcat(msg, " signature: ");
963 utf_cat(msg, m->descriptor);
967 va_start(ap, message);
968 vsprintf(msg + strlen(msg), message, ap);
971 /* create exception object */
973 o = new_exception_message(string_java_lang_VerifyError, msg);
977 MFREE(msg, char, msglen);
983 /* exceptions_throw_verifyerror_for_stack **************************************
985 throws a java.lang.VerifyError for an invalid stack slot type
988 m............method in which the error was found
989 type.........the expected type
992 an exception pointer (in any case -- either it is the newly created
993 exception, or an exception thrown while trying to create it).
995 *******************************************************************************/
997 void exceptions_throw_verifyerror_for_stack(methodinfo *m,int type)
999 java_objectheader *o;
1004 /* calculate exception message length */
1009 msglen = strlen("(class: ") + utf_bytes(m->class->name) +
1010 strlen(", method: ") + utf_bytes(m->name) +
1011 strlen(" signature: ") + utf_bytes(m->descriptor) +
1012 strlen(") Expecting to find longest-------typename on stack")
1015 /* allocate memory */
1017 msg = MNEW(char, msglen);
1019 /* generate message */
1022 strcpy(msg, "(class: ");
1023 utf_cat_classname(msg, m->class->name);
1024 strcat(msg, ", method: ");
1025 utf_cat(msg, m->name);
1026 strcat(msg, " signature: ");
1027 utf_cat(msg, m->descriptor);
1034 strcat(msg,"Expecting to find ");
1036 case TYPE_INT: typename = "integer"; break;
1037 case TYPE_LNG: typename = "long"; break;
1038 case TYPE_FLT: typename = "float"; break;
1039 case TYPE_DBL: typename = "double"; break;
1040 case TYPE_ADR: typename = "object/array"; break;
1041 default: typename = "<INVALID>"; assert(0); break;
1043 strcat(msg, typename);
1044 strcat(msg, " on stack");
1046 /* create exception object */
1048 o = new_exception_message(string_java_lang_VerifyError, msg);
1052 MFREE(msg, char, msglen);
1057 /* new_arithmeticexception *****************************************************
1059 Generates a java.lang.ArithmeticException for the jit compiler.
1061 *******************************************************************************/
1063 java_objectheader *new_arithmeticexception(void)
1065 java_objectheader *e;
1067 e = new_exception_message(string_java_lang_ArithmeticException,
1068 string_java_lang_ArithmeticException_message);
1071 return *exceptionptr;
1077 /* exceptions_new_arrayindexoutofboundsexception *******************************
1079 Generates a java.lang.ArrayIndexOutOfBoundsException for the VM
1082 *******************************************************************************/
1084 java_objectheader *new_arrayindexoutofboundsexception(s4 index)
1086 java_objectheader *e;
1088 java_objectheader *o;
1089 java_lang_String *s;
1091 /* convert the index into a String, like Sun does */
1093 m = class_resolveclassmethod(class_java_lang_String,
1094 utf_new_char("valueOf"),
1095 utf_new_char("(I)Ljava/lang/String;"),
1096 class_java_lang_Object,
1100 return *exceptionptr;
1102 o = vm_call_method(m, NULL, index);
1104 s = (java_lang_String *) o;
1107 return *exceptionptr;
1109 e = new_exception_javastring(string_java_lang_ArrayIndexOutOfBoundsException,
1113 return *exceptionptr;
1119 /* exceptions_throw_arrayindexoutofboundsexception *****************************
1121 Generates a java.lang.ArrayIndexOutOfBoundsException for the VM
1124 *******************************************************************************/
1126 void exceptions_throw_arrayindexoutofboundsexception(void)
1128 java_objectheader *e;
1130 e = new_exception(string_java_lang_ArrayIndexOutOfBoundsException);
1139 /* new_arraystoreexception *****************************************************
1141 generates a java.lang.ArrayStoreException for the jit compiler
1143 *******************************************************************************/
1145 java_objectheader *new_arraystoreexception(void)
1147 java_objectheader *e;
1149 e = new_exception(string_java_lang_ArrayStoreException);
1150 /* e = native_new_and_init(class_java_lang_ArrayStoreException); */
1153 return *exceptionptr;
1159 /* new_classcastexception ******************************************************
1161 generates a java.lang.ClassCastException for the jit compiler
1163 *******************************************************************************/
1165 java_objectheader *new_classcastexception(void)
1167 java_objectheader *e;
1169 e = new_exception(string_java_lang_ClassCastException);
1172 return *exceptionptr;
1178 /* exceptions_new_illegalargumentexception *************************************
1180 Generates a java.lang.IllegalArgumentException for the VM system.
1182 *******************************************************************************/
1184 java_objectheader *new_illegalargumentexception(void)
1186 java_objectheader *e;
1188 e = native_new_and_init(class_java_lang_IllegalArgumentException);
1191 return *exceptionptr;
1197 /* exceptions_throw_illegalargumentexception ***********************************
1199 Generates a java.lang.IllegalArgumentException for the VM system
1200 and throw it in the VM system.
1202 *******************************************************************************/
1204 void exceptions_throw_illegalargumentexception(void)
1206 *exceptionptr = new_illegalargumentexception();
1210 /* new_illegalmonitorstateexception ********************************************
1212 Generates a java.lang.IllegalMonitorStateException for the VM
1215 *******************************************************************************/
1217 java_objectheader *new_illegalmonitorstateexception(void)
1219 java_objectheader *e;
1221 e = native_new_and_init(class_java_lang_IllegalMonitorStateException);
1224 return *exceptionptr;
1230 /* exceptions_new_negativearraysizeexception ***********************************
1232 Generates a java.lang.NegativeArraySizeException for the VM system.
1234 *******************************************************************************/
1236 java_objectheader *new_negativearraysizeexception(void)
1238 java_objectheader *e;
1240 e = new_exception(string_java_lang_NegativeArraySizeException);
1243 return *exceptionptr;
1249 /* exceptions_throw_negativearraysizeexception *********************************
1251 Generates a java.lang.NegativeArraySizeException for the VM system.
1253 *******************************************************************************/
1255 void exceptions_throw_negativearraysizeexception(void)
1257 *exceptionptr = new_negativearraysizeexception();
1261 /* new_nullpointerexception ****************************************************
1263 generates a java.lang.NullPointerException for the jit compiler
1265 *******************************************************************************/
1267 java_objectheader *new_nullpointerexception(void)
1269 java_objectheader *e;
1271 e = native_new_and_init(class_java_lang_NullPointerException);
1274 return *exceptionptr;
1280 /* exceptions_throw_nullpointerexception ***************************************
1282 Generates a java.lang.NullPointerException for the VM system and
1283 throw it in the VM system.
1285 *******************************************************************************/
1287 void exceptions_throw_nullpointerexception(void)
1289 *exceptionptr = new_nullpointerexception();
1293 /* exceptions_new_stringindexoutofboundsexception ******************************
1295 Generates a java.lang.StringIndexOutOfBoundsException for the VM
1298 *******************************************************************************/
1300 java_objectheader *exceptions_new_stringindexoutofboundsexception(void)
1302 java_objectheader *e;
1304 e = new_exception(string_java_lang_StringIndexOutOfBoundsException);
1307 return *exceptionptr;
1313 /* exceptions_throw_stringindexoutofboundsexception ****************************
1315 Throws a java.lang.StringIndexOutOfBoundsException for the VM
1318 *******************************************************************************/
1320 void exceptions_throw_stringindexoutofboundsexception(void)
1322 *exceptionptr = exceptions_new_stringindexoutofboundsexception();
1326 /* exceptions_handle_exception *************************************************
1328 Try to find an exception handler for the given exception and return it.
1329 If no handler is found, exit the monitor of the method (if any)
1333 xptr.........the exception object
1334 xpc..........PC of where the exception was thrown
1335 pv...........Procedure Value of the current method
1336 sp...........current stack pointer
1339 the address of the first matching exception handler, or
1340 NULL if no handler was found
1342 *******************************************************************************/
1344 u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp)
1350 s4 exceptiontablelength;
1352 classref_or_classinfo cr;
1354 #if defined(ENABLE_THREADS)
1355 java_objectheader *o;
1358 /* get methodinfo pointer from method header */
1360 m = *((methodinfo **) (pv + MethodPointer));
1361 framesize = *((s4 *) (pv + FrameSize));
1362 issync = *((s4 *) (pv + IsSync));
1363 ex = (exceptionentry *) (pv + ExTableStart);
1364 exceptiontablelength = *((s4 *) (pv + ExTableSize));
1366 #if !defined(NDEBUG)
1367 /* print exception trace */
1369 if (opt_verbose || opt_verbosecall || opt_verboseexception)
1370 builtin_trace_exception(xptr, m, xpc, 1);
1373 for (i = 0; i < exceptiontablelength; i++) {
1374 /* ATTENTION: keep this here, as we need to decrement the
1375 pointer before the loop executes! */
1379 /* If the start and end PC is NULL, this means we have the
1380 special case of asm_vm_call_method. So, just return the
1381 proper exception handler. */
1383 if ((ex->startpc == NULL) && (ex->endpc == NULL))
1384 return (u1 *) (ptrint) &asm_vm_call_method_exception_handler;
1386 /* is the xpc is the current catch range */
1388 if ((ex->startpc <= xpc) && (xpc < ex->endpc)) {
1391 /* NULL catches everything */
1393 if (cr.any == NULL) {
1394 #if !defined(NDEBUG)
1395 /* Print stacktrace of exception when caught. */
1397 if (opt_verboseexception) {
1398 exceptions_print_exception(xptr);
1399 stacktrace_print_trace(xptr);
1403 return ex->handlerpc;
1406 /* resolve or load/link the exception class */
1408 if (IS_CLASSREF(cr)) {
1409 c = resolve_classref_eager(cr.ref);
1414 if (!(c->state & CLASS_LOADED))
1415 /* use the methods' classloader */
1416 if (!load_class_from_classloader(c->name,
1417 m->class->classloader))
1420 if (!(c->state & CLASS_LINKED))
1425 /* is the thrown exception an instance of the catch class? */
1427 if (builtin_instanceof(xptr, c)) {
1428 #if !defined(NDEBUG)
1429 /* Print stacktrace of exception when caught. */
1431 if (opt_verboseexception) {
1432 exceptions_print_exception(xptr);
1433 stacktrace_print_trace(xptr);
1437 return ex->handlerpc;
1442 #if defined(ENABLE_THREADS)
1443 /* is this method synchronized? */
1446 /* get synchronization object */
1448 # if defined(__MIPS__) && (SIZEOF_VOID_P == 4)
1449 /* XXX change this if we ever want to use 4-byte stackslots */
1450 o = *((java_objectheader **) (sp + issync - 8));
1452 o = *((java_objectheader **) (sp + issync - SIZEOF_VOID_P));
1457 builtin_monitorexit(o);
1461 /* none of the exceptions catch this one */
1467 /* exceptions_print_exception **************************************************
1469 Prints an exception, the detail message and the cause, if
1470 available, with CACAO internal functions to stdout.
1472 *******************************************************************************/
1474 #if !defined(NDEBUG)
1475 void exceptions_print_exception(java_objectheader *xptr)
1477 java_lang_Throwable *t;
1478 java_lang_Throwable *cause;
1481 t = (java_lang_Throwable *) xptr;
1490 /* print the root exception */
1492 utf_display_printable_ascii_classname(t->header.vftbl->class->name);
1494 if (t->detailMessage) {
1495 u = javastring_toutf(t->detailMessage, false);
1498 utf_display_printable_ascii(u);
1503 /* print the cause if available */
1505 if (cause && (cause != t)) {
1506 printf("Caused by: ");
1507 utf_display_printable_ascii_classname(cause->header.vftbl->class->name);
1509 if (cause->detailMessage) {
1510 u = javastring_toutf(cause->detailMessage, false);
1513 utf_display_printable_ascii(u);
1519 #endif /* !defined(NDEBUG) */
1523 * These are local overrides for various environment variables in Emacs.
1524 * Please do not remove this and leave it at the end of the file, where
1525 * Emacs will automagically detect them.
1526 * ---------------------------------------------------------------------
1529 * indent-tabs-mode: t
1533 * vim:noexpandtab:sw=4:ts=4: