Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/Exception/exception001/exception001.cpp
40951 views
/*1* Copyright (c) 2003, 2020, 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 {36char *name;37char *t_cls;38char *t_name;39char *t_sig;40jlocation t_loc;41char *c_cls;42char *c_name;43char *c_sig;44jlocation c_loc;45} writable_exceptionInfo;4647typedef struct {48const char *name;49const char *t_cls;50const char *t_name;51const char *t_sig;52jlocation t_loc;53const char *c_cls;54const char *c_name;55const char *c_sig;56jlocation c_loc;57} exceptionInfo;5859static jvmtiEnv *jvmti = NULL;60static jvmtiCapabilities caps;61static jvmtiEventCallbacks callbacks;62static jint result = PASSED;63static jboolean printdump = JNI_FALSE;64static exceptionInfo exs[] = {65{ "Lnsk/jvmti/Exception/exception001c;",66"Lnsk/jvmti/Exception/exception001b;", "meth1", "()V", 7,67"Lnsk/jvmti/Exception/exception001a;", "run", "()V", 14 },68{ "Ljava/lang/ArithmeticException;",69"Lnsk/jvmti/Exception/exception001b;", "meth2", "(I)I", 3,70"Lnsk/jvmti/Exception/exception001a;", "run", "()V", 24 },71{ "Ljava/lang/ArrayIndexOutOfBoundsException;",72"Lnsk/jvmti/Exception/exception001b;", "meth3", "(I)I", 10,73"Lnsk/jvmti/Exception/exception001a;", "run", "()V", 34 }74};75static int eventsCount = 0;76static int eventsExpected = 0;7778void JNICALL79Exception(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr,80jmethodID method, jlocation location, jobject exception,81jmethodID catch_method, jlocation catch_location) {82jvmtiError err;83writable_exceptionInfo ex;84jclass cls;85char *generic;86size_t i;8788if (printdump == JNI_TRUE) {89printf(">>> retrieving Exception info ...\n");90}91cls = env->GetObjectClass(exception);92err = jvmti_env->GetClassSignature(cls, &ex.name, &generic);93if (err != JVMTI_ERROR_NONE) {94printf("(GetClassSignature) unexpected error: %s (%d)\n",95TranslateError(err), err);96result = STATUS_FAILED;97return;98}99err = jvmti_env->GetMethodDeclaringClass(method, &cls);100if (err != JVMTI_ERROR_NONE) {101printf("(GetMethodDeclaringClass#t) unexpected error: %s (%d)\n",102TranslateError(err), err);103result = STATUS_FAILED;104return;105}106err = jvmti_env->GetClassSignature(cls, &ex.t_cls, &generic);107if (err != JVMTI_ERROR_NONE) {108printf("(GetClassSignature#t) unexpected error: %s (%d)\n",109TranslateError(err), err);110result = STATUS_FAILED;111return;112}113err = jvmti_env->GetMethodName(method,114&ex.t_name, &ex.t_sig, &generic);115if (err != JVMTI_ERROR_NONE) {116printf("(GetMethodName#t) unexpected error: %s (%d)\n",117TranslateError(err), err);118result = STATUS_FAILED;119return;120}121ex.t_loc = location;122err = jvmti_env->GetMethodDeclaringClass(catch_method, &cls);123if (err != JVMTI_ERROR_NONE) {124printf("(GetMethodDeclaringClass#c) unexpected error: %s (%d)\n",125TranslateError(err), err);126result = STATUS_FAILED;127return;128}129err = jvmti_env->GetClassSignature(cls, &ex.c_cls, &generic);130if (err != JVMTI_ERROR_NONE) {131printf("(GetClassSignature#c) unexpected error: %s (%d)\n",132TranslateError(err), err);133result = STATUS_FAILED;134return;135}136err = jvmti_env->GetMethodName(catch_method,137&ex.c_name, &ex.c_sig, &generic);138if (err != JVMTI_ERROR_NONE) {139printf("(GetMethodName#c) unexpected error: %s (%d)\n",140TranslateError(err), err);141result = STATUS_FAILED;142return;143}144ex.c_loc = catch_location;145if (printdump == JNI_TRUE) {146printf(">>> %s\n", ex.name);147printf(">>> thrown at %s.%s%s:0x%x%08x\n",148ex.t_cls, ex.t_name, ex.t_sig,149(jint)(ex.t_loc >> 32), (jint)ex.t_loc);150printf(">>> catch at %s.%s%s:0x%x%08x\n",151ex.c_cls, ex.c_name, ex.c_sig,152(jint)(ex.c_loc >> 32), (jint)ex.c_loc);153printf(">>> ... done\n");154}155for (i = 0; i < sizeof(exs)/sizeof(exceptionInfo); i++) {156if (ex.name != NULL && strcmp(ex.name, exs[i].name) == 0157&& ex.t_cls != NULL && strcmp(ex.t_cls, exs[i].t_cls) == 0158&& ex.t_name != NULL && strcmp(ex.t_name, exs[i].t_name) == 0159&& ex.t_sig != NULL && strcmp(ex.t_sig, exs[i].t_sig) == 0160&& ex.c_cls != NULL && strcmp(ex.c_cls, exs[i].c_cls) == 0161&& ex.c_name != NULL && strcmp(ex.c_name, exs[i].c_name) == 0162&& ex.c_sig != NULL && strcmp(ex.c_sig, exs[i].c_sig) == 0163&& ex.t_loc == exs[i].t_loc && ex.c_loc == exs[i].c_loc) {164eventsCount++;165break;166}167}168if (i == sizeof(exs)/sizeof(exceptionInfo)) {169printf("Unexpected exception event:\n");170printf(" %s\n", ex.name);171printf(" thrown at %s.%s%s:0x%x%08x\n",172ex.t_cls, ex.t_name, ex.t_sig,173(jint)(ex.t_loc >> 32), (jint)ex.t_loc);174printf(" catch at %s.%s%s:0x%x%08x\n",175ex.c_cls, ex.c_name, ex.c_sig,176(jint)(ex.c_loc >> 32), (jint)ex.c_loc);177result = STATUS_FAILED;178}179}180181#ifdef STATIC_BUILD182JNIEXPORT jint JNICALL Agent_OnLoad_exception001(JavaVM *jvm, char *options, void *reserved) {183return Agent_Initialize(jvm, options, reserved);184}185JNIEXPORT jint JNICALL Agent_OnAttach_exception001(JavaVM *jvm, char *options, void *reserved) {186return Agent_Initialize(jvm, options, reserved);187}188JNIEXPORT jint JNI_OnLoad_exception001(JavaVM *jvm, char *options, void *reserved) {189return JNI_VERSION_1_8;190}191#endif192jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {193jvmtiError err;194jint res;195196if (options != NULL && strcmp(options, "printdump") == 0) {197printdump = JNI_TRUE;198}199200res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);201if (res != JNI_OK || jvmti == NULL) {202printf("Wrong result of a valid call to GetEnv!\n");203return JNI_ERR;204}205206err = jvmti->GetPotentialCapabilities(&caps);207if (err != JVMTI_ERROR_NONE) {208printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",209TranslateError(err), err);210return JNI_ERR;211}212213err = jvmti->AddCapabilities(&caps);214if (err != JVMTI_ERROR_NONE) {215printf("(AddCapabilities) unexpected error: %s (%d)\n",216TranslateError(err), err);217return JNI_ERR;218}219220err = jvmti->GetCapabilities(&caps);221if (err != JVMTI_ERROR_NONE) {222printf("(GetCapabilities) unexpected error: %s (%d)\n",223TranslateError(err), err);224return JNI_ERR;225}226227if (caps.can_generate_exception_events) {228callbacks.Exception = &Exception;229err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));230if (err != JVMTI_ERROR_NONE) {231printf("(SetEventCallbacks) unexpected error: %s (%d)\n",232TranslateError(err), err);233return JNI_ERR;234}235} else {236printf("Warning: Exception event is not implemented\n");237}238239return JNI_OK;240}241242JNIEXPORT jint JNICALL243Java_nsk_jvmti_Exception_exception001_check(JNIEnv *env, jclass cls) {244jvmtiError err;245jthread thread;246jclass clz;247jmethodID mid;248249if (jvmti == NULL) {250printf("JVMTI client was not properly loaded!\n");251return STATUS_FAILED;252}253254if (!caps.can_generate_exception_events) {255return result;256}257258clz = env->FindClass("nsk/jvmti/Exception/exception001c");259if (clz == NULL) {260printf("Cannot find exception001c class!\n");261return STATUS_FAILED;262}263clz = env->FindClass("nsk/jvmti/Exception/exception001b");264if (clz == NULL) {265printf("Cannot find exception001b class!\n");266return STATUS_FAILED;267}268clz = env->FindClass("nsk/jvmti/Exception/exception001a");269if (clz == NULL) {270printf("Cannot find exception001a class!\n");271return STATUS_FAILED;272}273mid = env->GetStaticMethodID(clz, "run", "()V");274if (mid == NULL) {275printf("Cannot find method run!\n");276return STATUS_FAILED;277}278279err = jvmti->GetCurrentThread(&thread);280if (err != JVMTI_ERROR_NONE) {281printf("Failed to get current thread: %s (%d)\n", TranslateError(err), err);282result = STATUS_FAILED;283return STATUS_FAILED;284}285286err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,287JVMTI_EVENT_EXCEPTION, thread);288if (err == JVMTI_ERROR_NONE) {289eventsExpected = sizeof(exs)/sizeof(exceptionInfo);290} else {291printf("Failed to enable JVMTI_EVENT_EXCEPTION: %s (%d)\n",292TranslateError(err), err);293result = STATUS_FAILED;294}295296env->CallStaticVoidMethod(clz, mid);297298err = jvmti->SetEventNotificationMode(JVMTI_DISABLE,299JVMTI_EVENT_EXCEPTION, thread);300if (err != JVMTI_ERROR_NONE) {301printf("Failed to disable JVMTI_EVENT_EXCEPTION: %s (%d)\n",302TranslateError(err), err);303result = STATUS_FAILED;304}305306if (eventsCount != eventsExpected) {307printf("Wrong number of exception events: %d, expected: %d\n",308eventsCount, eventsExpected);309result = STATUS_FAILED;310}311return result;312}313314}315316317