Path: blob/master/test/hotspot/jtreg/serviceability/AsyncGetCallTrace/libAsyncGetCallTraceTest.cpp
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2019, Google and/or its affiliates. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324#include <assert.h>25#include <dlfcn.h>26#include <stdio.h>27#include <stdlib.h>28#include <string.h>29#include "jvmti.h"3031static jvmtiEnv* jvmti;3233template <class T>34class JvmtiDeallocator {35public:36JvmtiDeallocator() {37elem_ = NULL;38}3940~JvmtiDeallocator() {41jvmti->Deallocate(reinterpret_cast<unsigned char*>(elem_));42}4344T* get_addr() {45return &elem_;46}4748T get() {49return elem_;50}5152private:53T elem_;54};5556static void GetJMethodIDs(jclass klass) {57jint method_count = 0;58JvmtiDeallocator<jmethodID*> methods;59jvmtiError err = jvmti->GetClassMethods(klass, &method_count, methods.get_addr());6061// If ever the GetClassMethods fails, just ignore it, it was worth a try.62if (err != JVMTI_ERROR_NONE) {63fprintf(stderr, "GetJMethodIDs: Error in GetClassMethods: %d\n", err);64}65}6667// AsyncGetCallTrace needs class loading events to be turned on!68static void JNICALL OnClassLoad(jvmtiEnv *jvmti, JNIEnv *jni_env,69jthread thread, jclass klass) {70}7172static void JNICALL OnClassPrepare(jvmtiEnv *jvmti, JNIEnv *jni_env,73jthread thread, jclass klass) {74// We need to do this to "prime the pump" and get jmethodIDs primed.75GetJMethodIDs(klass);76}7778static void JNICALL OnVMInit(jvmtiEnv *jvmti, JNIEnv *jni_env, jthread thread) {79jint class_count = 0;8081// Get any previously loaded classes that won't have gone through the82// OnClassPrepare callback to prime the jmethods for AsyncGetCallTrace.83JvmtiDeallocator<jclass*> classes;84jvmtiError err = jvmti->GetLoadedClasses(&class_count, classes.get_addr());85if (err != JVMTI_ERROR_NONE) {86fprintf(stderr, "OnVMInit: Error in GetLoadedClasses: %d\n", err);87return;88}8990// Prime any class already loaded and try to get the jmethodIDs set up.91jclass *classList = classes.get();92for (int i = 0; i < class_count; ++i) {93GetJMethodIDs(classList[i]);94}95}9697extern "C" {9899static100jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {101jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION);102if (res != JNI_OK || jvmti == NULL) {103fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n");104return JNI_ERR;105}106107jvmtiError err;108jvmtiCapabilities caps;109memset(&caps, 0, sizeof(caps));110caps.can_get_line_numbers = 1;111caps.can_get_source_file_name = 1;112113err = jvmti->AddCapabilities(&caps);114if (err != JVMTI_ERROR_NONE) {115fprintf(stderr, "AgentInitialize: Error in AddCapabilities: %d\n", err);116return JNI_ERR;117}118119jvmtiEventCallbacks callbacks;120memset(&callbacks, 0, sizeof(callbacks));121callbacks.ClassLoad = &OnClassLoad;122callbacks.VMInit = &OnVMInit;123callbacks.ClassPrepare = &OnClassPrepare;124125err = jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks));126if (err != JVMTI_ERROR_NONE) {127fprintf(stderr, "AgentInitialize: Error in SetEventCallbacks: %d\n", err);128return JNI_ERR;129}130131err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL);132if (err != JVMTI_ERROR_NONE) {133fprintf(stderr, "AgentInitialize: Error in SetEventNotificationMode for CLASS_LOAD: %d\n", err);134return JNI_ERR;135}136137err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL);138if (err != JVMTI_ERROR_NONE) {139fprintf(stderr,140"AgentInitialize: Error in SetEventNotificationMode for CLASS_PREPARE: %d\n",141err);142return JNI_ERR;143}144145err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);146if (err != JVMTI_ERROR_NONE) {147fprintf(148stderr, "AgentInitialize: Error in SetEventNotificationMode for VM_INIT: %d\n",149err);150return JNI_ERR;151}152153return JNI_OK;154}155156JNIEXPORT157jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {158return Agent_Initialize(jvm, options, reserved);159}160161JNIEXPORT162jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {163return Agent_Initialize(jvm, options, reserved);164}165166JNIEXPORT167jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {168return JNI_VERSION_1_8;169}170171// A copy of the ASGCT data structures.172typedef struct {173jint lineno; // line number in the source file174jmethodID method_id; // method executed in this frame175} ASGCT_CallFrame;176177typedef struct {178JNIEnv *env_id; // Env where trace was recorded179jint num_frames; // number of frames in this trace180ASGCT_CallFrame *frames; // frames181} ASGCT_CallTrace;182183typedef void (*ASGCTType)(ASGCT_CallTrace *, jint, void *);184185JNIEXPORT jboolean JNICALL186Java_MyPackage_ASGCTBaseTest_checkAsyncGetCallTraceCall(JNIEnv* env, jclass cls) {187ASGCTType agct = reinterpret_cast<ASGCTType>(dlsym(RTLD_DEFAULT, "AsyncGetCallTrace"));188189const int MAX_DEPTH = 16;190ASGCT_CallTrace trace;191ASGCT_CallFrame frames[MAX_DEPTH];192trace.frames = frames;193trace.env_id = env;194trace.num_frames = 0;195196if (agct == NULL) {197fprintf(stderr, "AsyncGetCallTrace not found.\n");198return false;199}200201agct(&trace, MAX_DEPTH, NULL);202203// For now, just check that the first frame is (-3, checkAsyncGetCallTraceCall).204if (trace.num_frames <= 0) {205fprintf(stderr, "The num_frames must be positive: %d\n", trace.num_frames);206return false;207}208209// AsyncGetCallTrace returns -3 as line number for a native frame.210if (trace.frames[0].lineno != -3) {211fprintf(stderr, "lineno is not -3 as expected: %d\n", trace.frames[0].lineno);212return false;213}214215JvmtiDeallocator<char*> name;216if (trace.frames[0].method_id == NULL) {217fprintf(stderr, "First frame method_id is NULL\n");218return false;219}220221jvmtiError err = jvmti->GetMethodName(trace.frames[0].method_id, name.get_addr(), NULL, NULL);222if (err != JVMTI_ERROR_NONE) {223fprintf(stderr, "checkAsyncGetCallTrace: Error in GetMethodName: %d\n", err);224return false;225}226227if (name.get() == NULL) {228fprintf(stderr, "Name is NULL\n");229return false;230}231232return strcmp(name.get(), "checkAsyncGetCallTraceCall") == 0;233}234235}236237238