1 /*
   2  * Copyright (c) 2016, Oracle 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 it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 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 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include <stdio.h>
  25 #include <string.h>
  26 #include "jvmti.h"
  27 
  28 #ifdef __cplusplus
  29 extern "C" {
  30 #endif
  31 
  32 #ifndef JNI_ENV_ARG
  33 
  34 #ifdef __cplusplus
  35 #define JNI_ENV_ARG(x, y) y
  36 #define JNI_ENV_PTR(x) x
  37 #else
  38 #define JNI_ENV_ARG(x,y) x, y
  39 #define JNI_ENV_PTR(x) (*x)
  40 #endif
  41 
  42 #endif
  43 
  44 #define TranslateError(err) "JVMTI error"
  45 
  46 #define PASSED 0
  47 #define FAILED 2
  48 
  49 static const char *EXC_CNAME = "java/lang/Exception";
  50 
  51 static jvmtiEnv *jvmti = NULL;
  52 static jint result = PASSED;
  53 static jboolean printdump = JNI_FALSE;
  54 
  55 static int thread_start_events_vm_start = 0;
  56 
  57 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
  58 
  59 JNIEXPORT
  60 jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
  61     return Agent_Initialize(jvm, options, reserved);
  62 }
  63 
  64 JNIEXPORT
  65 jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
  66     return Agent_Initialize(jvm, options, reserved);
  67 }
  68 
  69 JNIEXPORT
  70 jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
  71     return JNI_VERSION_9;
  72 }
  73 
  74 static
  75 jint throw_exc(JNIEnv *env, char *msg) {
  76     jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));
  77 
  78     if (exc_class == NULL) {
  79         printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);
  80         return -1;
  81     }
  82     return JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);
  83 }
  84 
  85 
  86 void JNICALL Callback_ThreadStart(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) {
  87     jvmtiError err;
  88     jvmtiPhase phase;
  89 
  90     err = (*jvmti)->GetPhase(jvmti_env,&phase);
  91     if (err != JVMTI_ERROR_NONE) {
  92         printf("ThreadStart event: GetPhase error: %s (%d)\n", TranslateError(err), err);
  93         result = FAILED;
  94         return;
  95     }
  96 
  97     if (phase == JVMTI_PHASE_START) {
  98         thread_start_events_vm_start++;
  99     }
 100 
 101     if (printdump == JNI_TRUE) {
 102         printf(">>>    ThreadStart event: phase(%d)\n", phase);
 103     }
 104 }
 105 
 106 static
 107 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 108     jint res, size;
 109     jvmtiCapabilities caps;
 110     jvmtiEventCallbacks callbacks;
 111     jvmtiError err;
 112 
 113     if (options != NULL && strcmp(options, "printdump") == 0) {
 114         printdump = JNI_TRUE;
 115     }
 116 
 117     res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
 118         JVMTI_VERSION_9);
 119     if (res != JNI_OK || jvmti == NULL) {
 120         printf("    Error: wrong result of a valid call to GetEnv!\n");
 121         return JNI_ERR;
 122     }
 123 
 124     printf("Enabling following capability: can_generate_early_vmstart\n");
 125     memset(&caps, 0, sizeof(caps));
 126     caps.can_generate_early_vmstart = 1;
 127 
 128     err = (*jvmti)->AddCapabilities(jvmti, &caps);
 129     if (err != JVMTI_ERROR_NONE) {
 130         printf("    Error in AddCapabilites: %s (%d)\n", TranslateError(err), err);
 131         return JNI_ERR;
 132     }
 133 
 134     size = (jint)sizeof(callbacks);
 135 
 136     memset(&callbacks, 0, sizeof(callbacks));
 137     callbacks.ThreadStart = Callback_ThreadStart;
 138 
 139     err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, size);
 140     if (err != JVMTI_ERROR_NONE) {
 141         printf("    Error in SetEventCallbacks: %s (%d)\n", TranslateError(err), err);
 142         return JNI_ERR;
 143     }
 144 
 145     err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_THREAD_START, NULL);
 146     if (err != JVMTI_ERROR_NONE) {
 147         printf("    Error in SetEventNotificationMode: %s (%d)\n", TranslateError(err), err);
 148         return JNI_ERR;
 149     }
 150 
 151     return JNI_OK;
 152 }
 153 
 154 JNIEXPORT jint JNICALL
 155 Java_MAAThreadStart_check(JNIEnv *env, jclass cls) {
 156     jobject loader = NULL;
 157 
 158     if (jvmti == NULL) {
 159         throw_exc(env, "JVMTI client was not properly loaded!\n");
 160         return FAILED;
 161     }
 162 
 163     /*
 164      * Expecting that ThreadStart events are sent during VM Start phase when
 165      * can_generate_early_vmstart capability is enabled
 166      */
 167     if (thread_start_events_vm_start == 0) {
 168         throw_exc(env, "Didn't get ThreadStart events in start phase!\n");
 169         return FAILED;
 170     }
 171 
 172     return result;
 173 }
 174 
 175 #ifdef __cplusplus
 176 }
 177 #endif