J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * Copyright 2003 Wily Technology, Inc. |
| 28 | */ |
| 29 | |
| 30 | #include <stdlib.h> |
| 31 | #include <stdio.h> |
| 32 | |
| 33 | #include "JPLISAssert.h" |
| 34 | #include "Utilities.h" |
| 35 | #include "JavaExceptions.h" |
| 36 | |
| 37 | /* |
| 38 | * This module provides various simple JNI and JVMTI utility functionality. |
| 39 | */ |
| 40 | |
| 41 | void * |
| 42 | allocate(jvmtiEnv * jvmtienv, size_t bytecount) { |
| 43 | void * resultBuffer = NULL; |
| 44 | jvmtiError error = JVMTI_ERROR_NONE; |
| 45 | |
| 46 | error = (*jvmtienv)->Allocate(jvmtienv, |
| 47 | bytecount, |
| 48 | (unsigned char**) &resultBuffer); |
| 49 | jplis_assert(error == JVMTI_ERROR_NONE); |
| 50 | if ( error != JVMTI_ERROR_NONE ) { |
| 51 | resultBuffer = NULL; |
| 52 | } |
| 53 | return resultBuffer; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Convenience method that deallocates memory. |
| 58 | * Throws assert on error. |
| 59 | * JVMTI Deallocate can only fail due to internal error, that is, this |
| 60 | * agent has done something wrong or JVMTI has done something wrong. These |
| 61 | * errors aren't interesting to a JPLIS agent and so are not returned. |
| 62 | */ |
| 63 | void |
| 64 | deallocate(jvmtiEnv * jvmtienv, void * buffer) { |
| 65 | jvmtiError error = JVMTI_ERROR_NONE; |
| 66 | |
| 67 | error = (*jvmtienv)->Deallocate(jvmtienv, |
| 68 | (unsigned char*)buffer); |
| 69 | jplis_assert_msg(error == JVMTI_ERROR_NONE, "Can't deallocate memory"); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns whether the passed exception is an instance of the given classname |
| 75 | * Clears any JNI exceptions before returning |
| 76 | */ |
| 77 | jboolean |
| 78 | isInstanceofClassName( JNIEnv * jnienv, |
| 79 | jobject instance, |
| 80 | const char * className) { |
| 81 | jboolean isInstanceof = JNI_FALSE; |
| 82 | jboolean errorOutstanding = JNI_FALSE; |
| 83 | jclass classHandle = NULL; |
| 84 | |
| 85 | jplis_assert(isSafeForJNICalls(jnienv)); |
| 86 | |
| 87 | /* get an instance of unchecked exception for instanceof comparison */ |
| 88 | classHandle = (*jnienv)->FindClass(jnienv, className); |
| 89 | errorOutstanding = checkForAndClearThrowable(jnienv); |
| 90 | jplis_assert(!errorOutstanding); |
| 91 | |
| 92 | if (!errorOutstanding) { |
| 93 | isInstanceof = (*jnienv)->IsInstanceOf(jnienv, instance, classHandle); |
| 94 | errorOutstanding = checkForAndClearThrowable(jnienv); |
| 95 | jplis_assert(!errorOutstanding); |
| 96 | } |
| 97 | |
| 98 | jplis_assert(isSafeForJNICalls(jnienv)); |
| 99 | return isInstanceof; |
| 100 | } |
| 101 | |
| 102 | /* We don't come back from this |
| 103 | */ |
| 104 | void |
| 105 | abortJVM( JNIEnv * jnienv, |
| 106 | const char * message) { |
| 107 | (*jnienv)->FatalError(jnienv, message); |
| 108 | } |