Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/FramePop/framepop001/framepop001.cpp
40955 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 <inttypes.h>26#include "jvmti.h"27#include "agent_common.h"28#include "JVMTITools.h"2930extern "C" {313233#define PASSED 034#define STATUS_FAILED 23536typedef struct {37const char *cls_sig;38const char *name;39const char *sig;40jlocation loc;41} pop_info;4243static jvmtiEnv *jvmti = NULL;44static jvmtiCapabilities caps;45static jvmtiEventCallbacks callbacks;46static jint result = PASSED;47static jboolean printdump = JNI_FALSE;48static size_t eventsExpected = 0;49static size_t eventsCount = 0;50static pop_info pops[] = {51{ "Lnsk/jvmti/FramePop/framepop001;", "chain", "()V", 0 },52{ "Lnsk/jvmti/FramePop/framepop001a;", "dummy", "()V", 3 },53};5455void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env,56jthread thr, jmethodID method, jlocation location) {57jvmtiError err;5859err = jvmti_env->NotifyFramePop(thr, 0);60if (err == JVMTI_ERROR_NONE) {61eventsExpected++;62} else {63printf("(NotifyFramePop#0) unexpected error: %s (%d)\n",64TranslateError(err), err);65result = STATUS_FAILED;66}6768err = jvmti_env->NotifyFramePop(thr, 1);69if (err == JVMTI_ERROR_NONE) {70eventsExpected++;71} else {72printf("(NotifyFramePop#1) unexpected error: %s (%d)\n",73TranslateError(err), err);74result = STATUS_FAILED;75}76}7778void JNICALL FramePop(jvmtiEnv *jvmti_env, JNIEnv *env,79jthread thr, jmethodID method, jboolean wasPopedByException) {80jvmtiError err;81char *cls_sig, *name, *sig, *generic;82jclass cls;83jmethodID mid;84jlocation loc;8586if (printdump == JNI_TRUE) {87printf(">>> retrieving frame pop info ...\n");88}89err = jvmti_env->GetMethodDeclaringClass(method, &cls);90if (err != JVMTI_ERROR_NONE) {91printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",92TranslateError(err), err);93result = STATUS_FAILED;94return;95}96err = jvmti_env->GetClassSignature(cls, &cls_sig, &generic);97if (err != JVMTI_ERROR_NONE) {98printf("(GetClassSignature) unexpected error: %s (%d)\n",99TranslateError(err), err);100result = STATUS_FAILED;101return;102}103err = jvmti_env->GetMethodName(method, &name, &sig, &generic);104if (err != JVMTI_ERROR_NONE) {105printf("(GetMethodName) unexpected error: %s (%d)\n",106TranslateError(err), err);107result = STATUS_FAILED;108return;109}110err = jvmti_env->GetFrameLocation(thr, 0, &mid, &loc);111if (err != JVMTI_ERROR_NONE) {112printf("(GetFrameLocation) unexpected error: %s (%d)\n",113TranslateError(err), err);114result = STATUS_FAILED;115}116if (printdump == JNI_TRUE) {117printf(">>> class: \"%s\"\n", cls_sig);118printf(">>> method: \"%s%s\"\n", name, sig);119printf(">>> location: 0x%x%08x\n",120(jint)(loc >> 32), (jint)loc);121printf(">>> ... done\n");122}123if (eventsCount < sizeof(pops)/sizeof(pop_info)) {124if (cls_sig == NULL ||125strcmp(cls_sig, pops[eventsCount].cls_sig) != 0) {126printf("(pop#%" PRIuPTR ") wrong class: \"%s\"",127eventsCount, cls_sig);128printf(", expected: \"%s\"\n", pops[eventsCount].cls_sig);129result = STATUS_FAILED;130}131if (name == NULL ||132strcmp(name, pops[eventsCount].name) != 0) {133printf("(pop#%" PRIuPTR ") wrong method name: \"%s\"",134eventsCount, name);135printf(", expected: \"%s\"\n", pops[eventsCount].name);136result = STATUS_FAILED;137}138if (sig == NULL ||139strcmp(sig, pops[eventsCount].sig) != 0) {140printf("(pop#%" PRIuPTR ") wrong method sig: \"%s\"",141eventsCount, sig);142printf(", expected: \"%s\"\n", pops[eventsCount].sig);143result = STATUS_FAILED;144}145if (loc != pops[eventsCount].loc) {146printf("(pop#%" PRIuPTR ") wrong location: 0x%x%08x",147eventsCount, (jint)(loc >> 32), (jint)loc);148printf(", expected: 0x%x\n", (jint)pops[eventsCount].loc);149result = STATUS_FAILED;150}151} else {152printf("Unexpected frame pop catched:");153printf(" class: \"%s\"\n", cls_sig);154printf(" method: \"%s%s\"\n", name, sig);155printf(" location: 0x%x%08x\n", (jint)(loc >> 32), (jint)loc);156result = STATUS_FAILED;157}158eventsCount++;159}160161#ifdef STATIC_BUILD162JNIEXPORT jint JNICALL Agent_OnLoad_framepop001(JavaVM *jvm, char *options, void *reserved) {163return Agent_Initialize(jvm, options, reserved);164}165JNIEXPORT jint JNICALL Agent_OnAttach_framepop001(JavaVM *jvm, char *options, void *reserved) {166return Agent_Initialize(jvm, options, reserved);167}168JNIEXPORT jint JNI_OnLoad_framepop001(JavaVM *jvm, char *options, void *reserved) {169return JNI_VERSION_1_8;170}171#endif172jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {173jvmtiError err;174jint res;175176if (options != NULL && strcmp(options, "printdump") == 0) {177printdump = JNI_TRUE;178}179180res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);181if (res != JNI_OK || jvmti == NULL) {182printf("Wrong result of a valid call to GetEnv!\n");183return JNI_ERR;184}185186err = jvmti->GetPotentialCapabilities(&caps);187if (err != JVMTI_ERROR_NONE) {188printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",189TranslateError(err), err);190return JNI_ERR;191}192193err = jvmti->AddCapabilities(&caps);194if (err != JVMTI_ERROR_NONE) {195printf("(AddCapabilities) unexpected error: %s (%d)\n",196TranslateError(err), err);197return JNI_ERR;198}199200err = jvmti->GetCapabilities(&caps);201if (err != JVMTI_ERROR_NONE) {202printf("(GetCapabilities) unexpected error: %s (%d)\n",203TranslateError(err), err);204return JNI_ERR;205}206207if (caps.can_generate_frame_pop_events &&208caps.can_generate_breakpoint_events) {209callbacks.Breakpoint = &Breakpoint;210callbacks.FramePop = &FramePop;211err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));212if (err != JVMTI_ERROR_NONE) {213printf("(SetEventCallbacks) unexpected error: %s (%d)\n",214TranslateError(err), err);215return JNI_ERR;216}217} else {218printf("Warning: FramePop or Breakpoint event is not implemented\n");219}220221return JNI_OK;222}223224JNIEXPORT jint JNICALL225Java_nsk_jvmti_FramePop_framepop001_check(JNIEnv *env, jclass cls) {226jvmtiError err;227jclass clz;228jmethodID mid;229230if (jvmti == NULL) {231printf("JVMTI client was not properly loaded!\n");232return STATUS_FAILED;233}234235if (!caps.can_generate_frame_pop_events ||236!caps.can_generate_breakpoint_events) {237return result;238}239240mid = env->GetStaticMethodID(cls, "chain", "()V");241if (mid == 0) {242printf("Cannot find Method ID for method chain\n");243return STATUS_FAILED;244}245err = jvmti->SetBreakpoint(mid, 0);246if (err != JVMTI_ERROR_NONE) {247printf("Failed to SetBreakpoint: %s (%d)\n",248TranslateError(err), err);249return STATUS_FAILED;250}251err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,252JVMTI_EVENT_FRAME_POP, NULL);253if (err != JVMTI_ERROR_NONE) {254printf("Failed to enable JVMTI_EVENT_FRAME_POP event: %s (%d)\n",255TranslateError(err), err);256result = STATUS_FAILED;257}258err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,259JVMTI_EVENT_BREAKPOINT, NULL);260if (err != JVMTI_ERROR_NONE) {261printf("Failed to enable BREAKPOINT event: %s (%d)\n",262TranslateError(err), err);263result = STATUS_FAILED;264}265266clz = env->FindClass("nsk/jvmti/FramePop/framepop001a");267if (clz == NULL) {268printf("Cannot find framepop001a class!\n");269result = STATUS_FAILED;270return STATUS_FAILED;271}272mid = env->GetStaticMethodID(clz, "dummy", "()V");273if (mid == 0) {274printf("Cannot find Method ID for method dummy\n");275return STATUS_FAILED;276}277env->CallStaticVoidMethod(clz, mid);278279if (eventsCount != eventsExpected) {280printf("Wrong number of frame pop events: %" PRIuPTR ", expected: %" PRIuPTR "\n",281eventsCount, eventsExpected);282result = STATUS_FAILED;283}284285return result;286}287288}289290291