Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetBytecodes/bytecodes002/bytecodes002.cpp
40951 views
/*1* Copyright (c) 2003, 2018, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdio.h>24#include <string.h>25#include "jvmti.h"26#include "agent_common.h"27#include "JVMTITools.h"2829extern "C" {303132#define PASSED 033#define STATUS_FAILED 23435static jvmtiEnv *jvmti = NULL;36static jvmtiCapabilities caps;37static jint result = PASSED;38static jboolean printdump = JNI_FALSE;3940#ifdef STATIC_BUILD41JNIEXPORT jint JNICALL Agent_OnLoad_bytecodes002(JavaVM *jvm, char *options, void *reserved) {42return Agent_Initialize(jvm, options, reserved);43}44JNIEXPORT jint JNICALL Agent_OnAttach_bytecodes002(JavaVM *jvm, char *options, void *reserved) {45return Agent_Initialize(jvm, options, reserved);46}47JNIEXPORT jint JNI_OnLoad_bytecodes002(JavaVM *jvm, char *options, void *reserved) {48return JNI_VERSION_1_8;49}50#endif51jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {52jint res;53jvmtiError err;5455if (options != NULL && strcmp(options, "printdump") == 0) {56printdump = JNI_TRUE;57}5859res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);60if (res != JNI_OK || jvmti == NULL) {61printf("Wrong result of a valid call to GetEnv!\n");62return JNI_ERR;63}6465err = jvmti->GetCapabilities(&caps);66if (err != JVMTI_ERROR_NONE) {67printf("(GetCapabilities) unexpected error: %s (%d)\n",68TranslateError(err), err);69return JNI_ERR;70}7172err = jvmti->GetPotentialCapabilities(&caps);73if (err != JVMTI_ERROR_NONE) {74printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",75TranslateError(err), err);76return JNI_ERR;77}7879err = jvmti->AddCapabilities(&caps);80if (err != JVMTI_ERROR_NONE) {81printf("(AddCapabilities) unexpected error: %s (%d)\n",82TranslateError(err), err);83return JNI_ERR;84}8586err = jvmti->GetCapabilities(&caps);87if (err != JVMTI_ERROR_NONE) {88printf("(GetCapabilities) unexpected error: %s (%d)\n",89TranslateError(err), err);90return JNI_ERR;91}9293if (!caps.can_get_bytecodes) {94printf("Warning: GetBytecodes is not implemented\n");95}9697return JNI_OK;98}99100JNIEXPORT jint JNICALL101Java_nsk_jvmti_GetBytecodes_bytecodes002_check(JNIEnv *env, jclass cls) {102jvmtiError err;103jmethodID mid;104jint bytecodeCount;105unsigned char *bytecodes;106107if (jvmti == NULL) {108printf("JVMTI client was not properly loaded!\n");109return STATUS_FAILED;110}111112mid = env->GetMethodID(cls, "<init>", "()V");113if (mid == NULL) {114printf("Cannot get method ID for \"<init>\"!\n");115return STATUS_FAILED;116}117118if (printdump == JNI_TRUE) {119printf(">>> invalid method check ...\n");120}121err = jvmti->GetBytecodes(NULL, &bytecodeCount, &bytecodes);122if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY && !caps.can_get_bytecodes) {123/* It is OK */124} else if (err != JVMTI_ERROR_INVALID_METHODID) {125printf("Error expected: JVMTI_ERROR_INVALID_METHODID,\n");126printf("\tactual: %s (%d)\n", TranslateError(err), err);127result = STATUS_FAILED;128}129130if (printdump == JNI_TRUE) {131printf(">>> (bytecodeCountPtr) null pointer check ...\n");132}133err = jvmti->GetBytecodes(mid, NULL, &bytecodes);134if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY && !caps.can_get_bytecodes) {135/* It is OK */136} else if (err != JVMTI_ERROR_NULL_POINTER) {137printf("Error expected: JVMTI_ERROR_NULL_POINTER,\n");138printf("\tactual: %s (%d)\n", TranslateError(err), err);139result = STATUS_FAILED;140}141142if (printdump == JNI_TRUE) {143printf(">>> (bytecodesPtr) null pointer check ...\n");144}145err = jvmti->GetBytecodes(mid, &bytecodeCount, NULL);146if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY && !caps.can_get_bytecodes) {147/* It is OK */148} else if (err != JVMTI_ERROR_NULL_POINTER) {149printf("Error expected: JVMTI_ERROR_NULL_POINTER,\n");150printf("\tactual: %s (%d)\n", TranslateError(err), err);151result = STATUS_FAILED;152}153154mid = env->GetStaticMethodID(cls, "check", "()I");155if (mid == NULL) {156printf("Cannot get method ID for \"check\"!\n");157return STATUS_FAILED;158}159160if (printdump == JNI_TRUE) {161printf(">>> native method check ...\n");162}163err = jvmti->GetBytecodes(mid, &bytecodeCount, &bytecodes);164if (err != JVMTI_ERROR_NATIVE_METHOD) {165printf("Error expected: JVMTI_ERROR_NATIVE_METHOD,\n");166printf("\tactual: %s (%d)\n", TranslateError(err), err);167result = STATUS_FAILED;168}169170if (printdump == JNI_TRUE) {171printf(">>> ... done\n");172}173174return result;175}176177}178179180