Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/ModuleAwareAgents/ThreadStart/libMAAThreadStart.c
41159 views
/*1* Copyright (c) 2016, 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"2627#ifdef __cplusplus28extern "C" {29#endif3031#ifndef JNI_ENV_ARG3233#ifdef __cplusplus34#define JNI_ENV_ARG(x, y) y35#define JNI_ENV_PTR(x) x36#else37#define JNI_ENV_ARG(x,y) x, y38#define JNI_ENV_PTR(x) (*x)39#endif4041#endif4243#define TranslateError(err) "JVMTI error"4445#define PASSED 046#define FAILED 24748static const char *EXC_CNAME = "java/lang/Exception";4950static jvmtiEnv *jvmti = NULL;51static jint result = PASSED;52static jboolean printdump = JNI_FALSE;5354static int thread_start_events_vm_start = 0;5556static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);5758JNIEXPORT59jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {60return Agent_Initialize(jvm, options, reserved);61}6263JNIEXPORT64jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {65return Agent_Initialize(jvm, options, reserved);66}6768JNIEXPORT69jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {70return JNI_VERSION_9;71}7273static74jint throw_exc(JNIEnv *env, char *msg) {75jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));7677if (exc_class == NULL) {78printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);79return -1;80}81return JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);82}838485void JNICALL Callback_ThreadStart(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) {86jvmtiError err;87jvmtiPhase phase;8889err = (*jvmti)->GetPhase(jvmti_env,&phase);90if (err != JVMTI_ERROR_NONE) {91printf("ThreadStart event: GetPhase error: %s (%d)\n", TranslateError(err), err);92result = FAILED;93return;94}9596if (phase == JVMTI_PHASE_START) {97thread_start_events_vm_start++;98}99100if (printdump == JNI_TRUE) {101printf(">>> ThreadStart event: phase(%d)\n", phase);102}103}104105static106jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {107jint res, size;108jvmtiCapabilities caps;109jvmtiEventCallbacks callbacks;110jvmtiError err;111112if (options != NULL && strcmp(options, "printdump") == 0) {113printdump = JNI_TRUE;114}115116res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),117JVMTI_VERSION_9);118if (res != JNI_OK || jvmti == NULL) {119printf(" Error: wrong result of a valid call to GetEnv!\n");120return JNI_ERR;121}122123printf("Enabling following capability: can_generate_early_vmstart\n");124memset(&caps, 0, sizeof(caps));125caps.can_generate_early_vmstart = 1;126127err = (*jvmti)->AddCapabilities(jvmti, &caps);128if (err != JVMTI_ERROR_NONE) {129printf(" Error in AddCapabilites: %s (%d)\n", TranslateError(err), err);130return JNI_ERR;131}132133size = (jint)sizeof(callbacks);134135memset(&callbacks, 0, sizeof(callbacks));136callbacks.ThreadStart = Callback_ThreadStart;137138err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, size);139if (err != JVMTI_ERROR_NONE) {140printf(" Error in SetEventCallbacks: %s (%d)\n", TranslateError(err), err);141return JNI_ERR;142}143144err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_THREAD_START, NULL);145if (err != JVMTI_ERROR_NONE) {146printf(" Error in SetEventNotificationMode: %s (%d)\n", TranslateError(err), err);147return JNI_ERR;148}149150return JNI_OK;151}152153JNIEXPORT jint JNICALL154Java_MAAThreadStart_check(JNIEnv *env, jclass cls) {155jobject loader = NULL;156157if (jvmti == NULL) {158throw_exc(env, "JVMTI client was not properly loaded!\n");159return FAILED;160}161162/*163* Expecting that ThreadStart events are sent during VM Start phase when164* can_generate_early_vmstart capability is enabled.165*/166if (thread_start_events_vm_start == 0) {167throw_exc(env, "Didn't get ThreadStart events in VM early start phase!\n");168return FAILED;169}170171return result;172}173174#ifdef __cplusplus175}176#endif177178179