Path: blob/master/src/java.instrument/share/native/libinstrument/Utilities.c
41149 views
/*1* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Copyright 2003 Wily Technology, Inc.27*/2829#include <stdlib.h>30#include <stdio.h>3132#include "JPLISAssert.h"33#include "Utilities.h"34#include "JavaExceptions.h"3536/*37* This module provides various simple JNI and JVMTI utility functionality.38*/3940void *41allocate(jvmtiEnv * jvmtienv, size_t bytecount) {42void * resultBuffer = NULL;43jvmtiError error = JVMTI_ERROR_NONE;4445error = (*jvmtienv)->Allocate(jvmtienv,46bytecount,47(unsigned char**) &resultBuffer);48/* may be called from any phase */49jplis_assert(error == JVMTI_ERROR_NONE);50if ( error != JVMTI_ERROR_NONE ) {51resultBuffer = NULL;52}53return resultBuffer;54}5556/**57* Convenience method that deallocates memory.58* Throws assert on error.59* JVMTI Deallocate can only fail due to internal error, that is, this60* agent has done something wrong or JVMTI has done something wrong. These61* errors aren't interesting to a JPLIS agent and so are not returned.62*/63void64deallocate(jvmtiEnv * jvmtienv, void * buffer) {65jvmtiError error = JVMTI_ERROR_NONE;6667error = (*jvmtienv)->Deallocate(jvmtienv,68(unsigned char*)buffer);69/* may be called from any phase */70jplis_assert_msg(error == JVMTI_ERROR_NONE, "Can't deallocate memory");71return;72}7374/**75* Returns whether the passed exception is an instance of the given classname76* Clears any JNI exceptions before returning77*/78jboolean79isInstanceofClassName( JNIEnv * jnienv,80jobject instance,81const char * className) {82jboolean isInstanceof = JNI_FALSE;83jboolean errorOutstanding = JNI_FALSE;84jclass classHandle = NULL;8586jplis_assert(isSafeForJNICalls(jnienv));8788/* get an instance of unchecked exception for instanceof comparison */89classHandle = (*jnienv)->FindClass(jnienv, className);90errorOutstanding = checkForAndClearThrowable(jnienv);91jplis_assert(!errorOutstanding);9293if (!errorOutstanding) {94isInstanceof = (*jnienv)->IsInstanceOf(jnienv, instance, classHandle);95errorOutstanding = checkForAndClearThrowable(jnienv);96jplis_assert(!errorOutstanding);97}9899jplis_assert(isSafeForJNICalls(jnienv));100return isInstanceof;101}102103/* We don't come back from this104*/105void106abortJVM( JNIEnv * jnienv,107const char * message) {108(*jnienv)->FatalError(jnienv, message);109}110111112