jdk/src/share/instrument/JavaExceptions.c
author pchelko
Wed, 23 Apr 2014 17:56:05 +0400
changeset 24529 c580bcb3aabc
parent 5506 202f599c92aa
permissions -rw-r--r--
8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order Reviewed-by: anthony, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Copyright 2003 Wily Technology, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include    <jni.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include    <jvmti.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include    "JPLISAssert.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include    "Utilities.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include    "JavaExceptions.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This module contains utility routines for manipulating Java throwables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and JNIEnv throwable state from native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static jthrowable   sFallbackInternalError  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Local forward declarations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/* insist on having a throwable. If we already have one, return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * If not, map to fallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
forceFallback(jthrowable potentialException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
forceFallback(jthrowable potentialException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    if ( potentialException == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return sFallbackInternalError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return potentialException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *  Returns true if it properly sets up a fallback exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
initializeFallbackError(JNIEnv* jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    sFallbackInternalError = createInternalError(jnienv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    return (sFallbackInternalError != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *  Map everything to InternalError.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
mapAllCheckedToInternalErrorMapper( JNIEnv *    jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                    jthrowable  throwableToMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    jthrowable  mappedThrowable = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    jstring     message         = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    jplis_assert(throwableToMap != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    jplis_assert(!isUnchecked(jnienv, throwableToMap));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    message = getMessageFromThrowable(jnienv, throwableToMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    mappedThrowable = createInternalError(jnienv, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    return mappedThrowable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
checkForThrowable(  JNIEnv*     jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    return (*jnienv)->ExceptionCheck(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
isSafeForJNICalls(  JNIEnv * jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    return !(*jnienv)->ExceptionCheck(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
logThrowable(   JNIEnv * jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    if ( checkForThrowable(jnienv) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        (*jnienv)->ExceptionDescribe(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *  Creates an exception or error with the fully qualified classname (ie java/lang/Error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *  and message passed to its constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
createThrowable(    JNIEnv *        jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    const char *    className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    jstring         message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    jthrowable  exception           = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    jmethodID   constructor         = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    jclass      exceptionClass      = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    jboolean    errorOutstanding    = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    jplis_assert(className != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /* create new VMError with message from exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    exceptionClass = (*jnienv)->FindClass(jnienv, className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    if (!errorOutstanding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        constructor = (*jnienv)->GetMethodID(   jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                                exceptionClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                                "<init>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                                "(Ljava/lang/String;)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    if (!errorOutstanding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        exception = (*jnienv)->NewObject(jnienv, exceptionClass, constructor, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    return exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
createInternalError(JNIEnv * jnienv, jstring message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    return createThrowable( jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            "java/lang/InternalError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
createThrowableFromJVMTIErrorCode(JNIEnv * jnienv, jvmtiError errorCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    const char * throwableClassName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    const char * message            = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    jstring messageString           = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    switch ( errorCode ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        case JVMTI_ERROR_NULL_POINTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                throwableClassName = "java/lang/NullPointerException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        case JVMTI_ERROR_ILLEGAL_ARGUMENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                throwableClassName = "java/lang/IllegalArgumentException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        case JVMTI_ERROR_OUT_OF_MEMORY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                throwableClassName = "java/lang/OutOfMemoryError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        case JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                throwableClassName = "java/lang/ClassCircularityError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        case JVMTI_ERROR_FAILS_VERIFICATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                throwableClassName = "java/lang/VerifyError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                message = "class redefinition failed: attempted to add a method";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                message = "class redefinition failed: attempted to change the schema (add/remove fields)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                message = "class redefinition failed: attempted to change superclass or interfaces";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                message = "class redefinition failed: attempted to delete a method";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                message = "class redefinition failed: attempted to change the class modifiers";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                message = "class redefinition failed: attempted to change method modifiers";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        case JVMTI_ERROR_UNSUPPORTED_VERSION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                throwableClassName = "java/lang/UnsupportedClassVersionError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        case JVMTI_ERROR_NAMES_DONT_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                throwableClassName = "java/lang/NoClassDefFoundError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                message = "class names don't match";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        case JVMTI_ERROR_INVALID_CLASS_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                throwableClassName = "java/lang/ClassFormatError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        case JVMTI_ERROR_UNMODIFIABLE_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                throwableClassName = "java/lang/instrument/UnmodifiableClassException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        case JVMTI_ERROR_INVALID_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                throwableClassName = "java/lang/InternalError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                message = "class redefinition failed: invalid class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        case JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                message = "unsupported operation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        case JVMTI_ERROR_INTERNAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                throwableClassName = "java/lang/InternalError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    if ( message != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        jboolean errorOutstanding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        messageString = (*jnienv)->NewStringUTF(jnienv, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        jplis_assert_msg(!errorOutstanding, "can't create exception java string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    return createThrowable( jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                            throwableClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                            messageString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *  Calls toString() on the given message which is the same call made by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 *  Exception when passed a throwable to its constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
jstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
getMessageFromThrowable(    JNIEnv*     jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                            jthrowable  exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    jclass      exceptionClass      = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    jmethodID   method              = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    jstring     message             = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    jboolean    errorOutstanding    = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /* call getMessage on exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    exceptionClass = (*jnienv)->GetObjectClass(jnienv, exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    if (!errorOutstanding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        method = (*jnienv)->GetMethodID(jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                        exceptionClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                        "toString",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                        "()Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    if (!errorOutstanding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        message = (*jnienv)->CallObjectMethod(jnienv, exception, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    return message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 *  Returns whether the exception given is an unchecked exception:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *  a subclass of Error or RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
isUnchecked(    JNIEnv*     jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                jthrowable  exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    jboolean result = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    result =    (exception == NULL) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                isInstanceofClassName(jnienv, exception, "java/lang/Error") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                isInstanceofClassName(jnienv, exception, "java/lang/RuntimeException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 *  Returns the current throwable, if any. Clears the throwable state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 *  Clients can use this to preserve the current throwable state on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
preserveThrowable(JNIEnv * jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    jthrowable result = (*jnienv)->ExceptionOccurred(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    if ( result != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        (*jnienv)->ExceptionClear(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 *  Installs the supplied throwable into the JNIEnv if the throwable is not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 *  Clients can use this to preserve the current throwable state on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
restoreThrowable(   JNIEnv *    jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    jthrowable  preservedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    throwThrowable( jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    preservedException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
throwThrowable(     JNIEnv *    jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    jthrowable  exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    if ( exception != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        jint result = (*jnienv)->Throw(jnienv, exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        jplis_assert_msg(result == JNI_OK, "throwThrowable failed to re-throw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
 *  Always clears the JNIEnv throwable state. Returns true if an exception was present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 *  before the clearing operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
checkForAndClearThrowable(  JNIEnv *    jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    jboolean result = (*jnienv)->ExceptionCheck(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    if ( result ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        (*jnienv)->ExceptionClear(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
/* creates a java.lang.InternalError and installs it into the JNIEnv */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
createAndThrowInternalError(JNIEnv * jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    jthrowable internalError = createInternalError( jnienv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    throwThrowable(jnienv, forceFallback(internalError));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
createAndThrowThrowableFromJVMTIErrorCode(JNIEnv * jnienv, jvmtiError errorCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    jthrowable throwable = createThrowableFromJVMTIErrorCode(jnienv, errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    throwThrowable(jnienv, forceFallback(throwable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
mapThrownThrowableIfNecessary(  JNIEnv *                jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                CheckedExceptionMapper  mapper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    jthrowable  originalThrowable   = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    jthrowable  resultThrowable     = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    originalThrowable = preserveThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /* the throwable is now cleared, so JNI calls are safe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    if ( originalThrowable != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        /* if there is an exception: we can just throw it if it is unchecked. If checked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * we need to map it (mapper is conditional, will vary by usage, hence the callback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if ( isUnchecked(jnienv, originalThrowable) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            resultThrowable = originalThrowable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            resultThrowable = (*mapper) (jnienv, originalThrowable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /* re-establish the correct throwable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    if ( resultThrowable != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        throwThrowable(jnienv, forceFallback(resultThrowable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
}