Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ExceptionCatch/excatch001/excatch001.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 *c_cls;38char *c_name;39char *c_sig;40jlocation c_loc;41} writable_exceptionInfo;4243typedef struct {44const char *name;45const char *c_cls;46const char *c_name;47const char *c_sig;48jlocation c_loc;49} exceptionInfo;5051static jvmtiEnv *jvmti = NULL;52static jvmtiCapabilities caps;53static jvmtiEventCallbacks callbacks;54static jint result = PASSED;55static jboolean printdump = JNI_FALSE;56static exceptionInfo exs[] = {57{ "Lnsk/jvmti/ExceptionCatch/excatch001c;",58"Lnsk/jvmti/ExceptionCatch/excatch001a;", "run", "()V", 14 },59{ "Ljava/lang/ArithmeticException;",60"Lnsk/jvmti/ExceptionCatch/excatch001a;", "run", "()V", 24 },61{ "Ljava/lang/ArrayIndexOutOfBoundsException;",62"Lnsk/jvmti/ExceptionCatch/excatch001a;", "run", "()V", 34 }63};64static int eventsCount = 0;65static int eventsExpected = 0;6667void JNICALL68ExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr,69jmethodID method, jlocation location, jobject exception) {70jvmtiError err;71jclass cls;72writable_exceptionInfo ex;73char *generic;74size_t i;7576if (printdump == JNI_TRUE) {77printf(">>> retrieving ExceptionCatch info ...\n");78}79cls = env->GetObjectClass(exception);80err = jvmti_env->GetClassSignature(cls, &ex.name, &generic);81if (err != JVMTI_ERROR_NONE) {82printf("(GetClassSignature#e) unexpected error: %s (%d)\n",83TranslateError(err), err);84result = STATUS_FAILED;85return;86}87err = jvmti_env->GetMethodDeclaringClass(method, &cls);88if (err != JVMTI_ERROR_NONE) {89printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",90TranslateError(err), err);91result = STATUS_FAILED;92return;93}94err = jvmti_env->GetClassSignature(cls, &ex.c_cls, &generic);95if (err != JVMTI_ERROR_NONE) {96printf("(GetClassSignature#c) unexpected error: %s (%d)\n",97TranslateError(err), err);98result = STATUS_FAILED;99return;100}101err = jvmti_env->GetMethodName(method,102&ex.c_name, &ex.c_sig, &generic);103if (err != JVMTI_ERROR_NONE) {104printf("(GetMethodName) unexpected error: %s (%d)\n",105TranslateError(err), err);106result = STATUS_FAILED;107return;108}109ex.c_loc = location;110if (printdump == JNI_TRUE) {111printf(">>> %s\n", ex.name);112printf(">>> catch at %s.%s%s:0x%x%08x\n",113ex.c_cls, ex.c_name, ex.c_sig,114(jint)(ex.c_loc >> 32), (jint)ex.c_loc);115printf(">>> ... done\n");116}117for (i = 0; i < sizeof(exs)/sizeof(exceptionInfo); i++) {118if (ex.name != NULL && strcmp(ex.name, exs[i].name) == 0119&& ex.c_cls != NULL && strcmp(ex.c_cls, exs[i].c_cls) == 0120&& ex.c_name != NULL && strcmp(ex.c_name, exs[i].c_name) == 0121&& ex.c_sig != NULL && strcmp(ex.c_sig, exs[i].c_sig) == 0122&& ex.c_loc == exs[i].c_loc) {123eventsCount++;124break;125}126}127if (i == sizeof(exs)/sizeof(exceptionInfo)) {128printf("Unexpected exception catch event:\n");129printf(" %s\n", ex.name);130printf(" catch at %s.%s%s:0x%x%08x\n",131ex.c_cls, ex.c_name, ex.c_sig,132(jint)(ex.c_loc >> 32), (jint)ex.c_loc);133result = STATUS_FAILED;134}135}136137#ifdef STATIC_BUILD138JNIEXPORT jint JNICALL Agent_OnLoad_excatch001(JavaVM *jvm, char *options, void *reserved) {139return Agent_Initialize(jvm, options, reserved);140}141JNIEXPORT jint JNICALL Agent_OnAttach_excatch001(JavaVM *jvm, char *options, void *reserved) {142return Agent_Initialize(jvm, options, reserved);143}144JNIEXPORT jint JNI_OnLoad_excatch001(JavaVM *jvm, char *options, void *reserved) {145return JNI_VERSION_1_8;146}147#endif148jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {149jvmtiError err;150jint res;151152if (options != NULL && strcmp(options, "printdump") == 0) {153printdump = JNI_TRUE;154}155156res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);157if (res != JNI_OK || jvmti == NULL) {158printf("Wrong result of a valid call to GetEnv!\n");159return JNI_ERR;160}161162err = jvmti->GetPotentialCapabilities(&caps);163if (err != JVMTI_ERROR_NONE) {164printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",165TranslateError(err), err);166return JNI_ERR;167}168169err = jvmti->AddCapabilities(&caps);170if (err != JVMTI_ERROR_NONE) {171printf("(AddCapabilities) unexpected error: %s (%d)\n",172TranslateError(err), err);173return JNI_ERR;174}175176err = jvmti->GetCapabilities(&caps);177if (err != JVMTI_ERROR_NONE) {178printf("(GetCapabilities) unexpected error: %s (%d)\n",179TranslateError(err), err);180return JNI_ERR;181}182183if (caps.can_generate_exception_events) {184callbacks.ExceptionCatch = &ExceptionCatch;185err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));186if (err != JVMTI_ERROR_NONE) {187printf("(SetEventCallbacks) unexpected error: %s (%d)\n",188TranslateError(err), err);189return JNI_ERR;190}191} else {192printf("Warning: Exception event is not implemented\n");193}194195return JNI_OK;196}197198JNIEXPORT jint JNICALL199Java_nsk_jvmti_ExceptionCatch_excatch001_check(JNIEnv *env, jclass cls) {200jvmtiError err;201jclass clz;202jmethodID mid;203204if (jvmti == NULL) {205printf("JVMTI client was not properly loaded!\n");206return STATUS_FAILED;207}208209if (!caps.can_generate_exception_events) {210return result;211}212213clz = env->FindClass("nsk/jvmti/ExceptionCatch/excatch001c");214if (clz == NULL) {215printf("Cannot find excatch001c class!\n");216return STATUS_FAILED;217}218clz = env->FindClass("nsk/jvmti/ExceptionCatch/excatch001b");219if (clz == NULL) {220printf("Cannot find excatch001b class!\n");221return STATUS_FAILED;222}223clz = env->FindClass("nsk/jvmti/ExceptionCatch/excatch001a");224if (clz == NULL) {225printf("Cannot find excatch001a class!\n");226return STATUS_FAILED;227}228mid = env->GetStaticMethodID(clz, "run", "()V");229if (mid == NULL) {230printf("Cannot find method run!\n");231return STATUS_FAILED;232}233234err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,235JVMTI_EVENT_EXCEPTION_CATCH, NULL);236if (err == JVMTI_ERROR_NONE) {237eventsExpected = sizeof(exs)/sizeof(exceptionInfo);238} else {239printf("Failed to enable JVMTI_EVENT_EXCEPTION_CATCH: %s (%d)\n",240TranslateError(err), err);241result = STATUS_FAILED;242}243244env->CallStaticVoidMethod(clz, mid);245246err = jvmti->SetEventNotificationMode(JVMTI_DISABLE,247JVMTI_EVENT_EXCEPTION_CATCH, NULL);248if (err != JVMTI_ERROR_NONE) {249printf("Failed to disable JVMTI_EVENT_EXCEPTION_CATCH: %s (%d)\n",250TranslateError(err), err);251result = STATUS_FAILED;252}253254if (eventsCount != eventsExpected) {255printf("Wrong number of exception catch events: %d, expected: %d\n",256eventsCount, eventsExpected);257result = STATUS_FAILED;258}259return result;260}261262}263264265