Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetBytecodes/bytecodes001/bytecodes001.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 23435typedef struct {36const char *name;37const char *sig;38jboolean stat;39jint count;40unsigned char *codes;41} info;4243static jvmtiEnv *jvmti = NULL;44static jvmtiCapabilities caps;45static jint result = PASSED;46static jboolean printdump = JNI_FALSE;4748static unsigned char m0[] = { 0x2A, 0xB7, 0x00, 0x01, 0xB1 };49static unsigned char m1[] = { 0xB1 };50static unsigned char m2[] = { 0x1A, 0xBC, 0x06, 0x4C, 0x2B, 0xB0 };51static info meth_tab[3] = {52{ "<init>", "()V", JNI_FALSE, 5, m0 },53{ "meth1", "()V", JNI_FALSE, 1, m1 },54{ "meth2", "(I)[F", JNI_TRUE, 6, m2 }55};5657#ifdef STATIC_BUILD58JNIEXPORT jint JNICALL Agent_OnLoad_bytecodes001(JavaVM *jvm, char *options, void *reserved) {59return Agent_Initialize(jvm, options, reserved);60}61JNIEXPORT jint JNICALL Agent_OnAttach_bytecodes001(JavaVM *jvm, char *options, void *reserved) {62return Agent_Initialize(jvm, options, reserved);63}64JNIEXPORT jint JNI_OnLoad_bytecodes001(JavaVM *jvm, char *options, void *reserved) {65return JNI_VERSION_1_8;66}67#endif68jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {69jint res;70jvmtiError err;7172if (options != NULL && strcmp(options, "printdump") == 0) {73printdump = JNI_TRUE;74}7576res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);77if (res != JNI_OK || jvmti == NULL) {78printf("Wrong result of a valid call to GetEnv!\n");79return JNI_ERR;80}8182err = jvmti->GetCapabilities(&caps);83if (err != JVMTI_ERROR_NONE) {84printf("(GetCapabilities) unexpected error: %s (%d)\n",85TranslateError(err), err);86return JNI_ERR;87}8889err = jvmti->GetPotentialCapabilities(&caps);90if (err != JVMTI_ERROR_NONE) {91printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",92TranslateError(err), err);93return JNI_ERR;94}9596err = jvmti->AddCapabilities(&caps);97if (err != JVMTI_ERROR_NONE) {98printf("(AddCapabilities) unexpected error: %s (%d)\n",99TranslateError(err), err);100return JNI_ERR;101}102103err = jvmti->GetCapabilities(&caps);104if (err != JVMTI_ERROR_NONE) {105printf("(GetCapabilities) unexpected error: %s (%d)\n",106TranslateError(err), err);107return JNI_ERR;108}109110if (!caps.can_get_bytecodes) {111printf("Warning: GetBytecodes is not implemented\n");112}113114return JNI_OK;115}116117void checkMeth(JNIEnv *env, jclass cl, int meth_ind) {118jvmtiError err;119jmethodID mid = NULL;120jint count = -1;121unsigned char *codes = NULL;122int i;123124if (jvmti == NULL) {125printf("JVMTI client was not properly loaded!\n");126result = STATUS_FAILED;127return;128}129130if (meth_tab[meth_ind].stat == JNI_TRUE) {131mid = env->GetStaticMethodID(cl,132meth_tab[meth_ind].name, meth_tab[meth_ind].sig);133} else {134mid = env->GetMethodID(cl,135meth_tab[meth_ind].name, meth_tab[meth_ind].sig);136}137if (mid == NULL) {138printf("\"%s%s\": cannot get method ID!\n",139meth_tab[meth_ind].name, meth_tab[meth_ind].sig);140result = STATUS_FAILED;141return;142}143144if (printdump == JNI_TRUE) {145printf(">>> \"%s%s\" check ...\n",146meth_tab[meth_ind].name, meth_tab[meth_ind].sig);147}148149err = jvmti->GetBytecodes(mid, &count, &codes);150if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY && !caps.can_get_bytecodes) {151/* It is OK */152return;153} else if (err != JVMTI_ERROR_NONE) {154printf("(GetBytecodes#%s) unexpected error: %s (%d)\n",155meth_tab[meth_ind].name, TranslateError(err), err);156result = STATUS_FAILED;157}158159if (count != meth_tab[meth_ind].count) {160printf("\"%s%s\": byte codes count expected: %d, actual: %d\n",161meth_tab[meth_ind].name, meth_tab[meth_ind].sig,162meth_tab[meth_ind].count, count);163result = STATUS_FAILED;164return;165}166for (i = 0; i < count; i++) {167if (codes[i] != meth_tab[meth_ind].codes[i]) {168printf("\"%s%s\": [%d] byte expected: 0x%x, actual: 0x%x\n",169meth_tab[meth_ind].name, meth_tab[meth_ind].sig, i,170meth_tab[meth_ind].codes[i], codes[i]);171result = STATUS_FAILED;172}173}174}175176JNIEXPORT jint JNICALL Java_nsk_jvmti_GetBytecodes_bytecodes001_check(JNIEnv *env, jclass cls) {177checkMeth(env, cls, 0);178checkMeth(env, cls, 1);179checkMeth(env, cls, 2);180return result;181}182183}184185186