1 /*
   2  * Copyright (c) 2019, 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 <string.h>
  25 #include "jvmti.h"
  26 
  27 extern "C" {
  28 
  29 #define AGENT_NAME "agent1"
  30 
  31 static JavaVM *java_vm = NULL;
  32 static jthread exp_thread = NULL;
  33 static jvmtiEnv *jvmti1 = NULL;
  34 static jint agent1_event_count = 0;
  35 static bool fail_status = false;
  36 
  37 static void
  38 check_jvmti_status(JNIEnv* env, jvmtiError err, const char* msg) {
  39   if (err != JVMTI_ERROR_NONE) {
  40     printf("check_jvmti_status: JVMTI function returned error: %d\n", err);
  41     fail_status = true;
  42     env->FatalError(msg);
  43   }
  44 }
  45 
  46 static void JNICALL
  47 CompiledMethodLoad(jvmtiEnv* jvmti, jmethodID method,
  48                    jint code_size, const void* code_addr,
  49                    jint map_length, const jvmtiAddrLocationMap* map,
  50                    const void* compile_info) {
  51   JNIEnv* env = NULL;
  52   jthread thread = NULL;
  53   char* name = NULL;
  54   char* sign = NULL;
  55   jvmtiError err;
  56 
  57   // Posted on JavaThread's, so it is legal to obtain JNIEnv*
  58   if (java_vm->GetEnv((void **) (&env), JNI_VERSION_9) != JNI_OK) {
  59     printf("CompiledMethodLoad: failed to obtain JNIEnv*\n");
  60     fail_status = true;
  61     return;
  62   }
  63 
  64   jvmti->GetCurrentThread(&thread);
  65   if (!env->IsSameObject(thread, exp_thread)) {
  66     return; // skip events from unexpected threads
  67   }
  68   agent1_event_count++;
  69 
  70   err = jvmti->GetMethodName(method, &name, &sign, NULL);
  71   check_jvmti_status(env, err, "CompiledMethodLoad: Error in JVMTI GetMethodName");
  72 
  73   printf("%s: CompiledMethodLoad: %s%s\n", AGENT_NAME, name, sign);
  74   fflush(0);
  75 }
  76 
  77 JNIEXPORT jint JNICALL
  78 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
  79   jvmtiEventCallbacks callbacks;
  80   jvmtiCapabilities caps;
  81   jvmtiError err;
  82 
  83   java_vm = jvm;
  84   if (jvm->GetEnv((void **) (&jvmti1), JVMTI_VERSION) != JNI_OK) {
  85     printf("Agent_OnLoad: Error in GetEnv in obtaining jvmtiEnv*\n");
  86     fail_status = true;
  87     return JNI_ERR;
  88   }
  89 
  90   memset(&callbacks, 0, sizeof(callbacks));
  91   callbacks.CompiledMethodLoad = &CompiledMethodLoad;
  92 
  93   err = jvmti1->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks));
  94   if (err != JVMTI_ERROR_NONE) {
  95     printf("Agent_OnLoad: Error in JVMTI SetEventCallbacks: %d\n", err);
  96     fail_status = true;
  97     return JNI_ERR;
  98   }
  99 
 100   memset(&caps, 0, sizeof(caps));
 101   caps.can_generate_compiled_method_load_events = 1;
 102 
 103   err = jvmti1->AddCapabilities(&caps);
 104   if (err != JVMTI_ERROR_NONE) {
 105     printf("Agent_OnLoad: Error in JVMTI AddCapabilities: %d\n", err);
 106     fail_status = true;
 107     return JNI_ERR;
 108   }
 109   return JNI_OK;
 110 }
 111 
 112 JNIEXPORT void JNICALL
 113 Java_MyPackage_GenerateEventsTest_agent1GenerateEvents(JNIEnv *env, jclass cls) {
 114   jthread thread = NULL;
 115   jvmtiError err;
 116 
 117   err = jvmti1->GetCurrentThread(&thread);
 118   check_jvmti_status(env, err, "generateEvents1: Error in JVMTI GetCurrentThread");
 119 
 120   exp_thread = (jthread)env->NewGlobalRef(thread);
 121 
 122   err = jvmti1->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
 123   check_jvmti_status(env, err, "generateEvents1: Error in JVMTI SetEventNotificationMode: JVMTI_ENABLE");
 124 
 125   err = jvmti1->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD);
 126   check_jvmti_status(env, err, "generateEvents1: Error in JVMTI GenerateEvents");
 127 
 128   err = jvmti1->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
 129   check_jvmti_status(env, err, "generateEvents1: Error in JVMTI SetEventNotificationMode: JVMTI_DISABLE");
 130 }
 131 
 132 JNIEXPORT jboolean JNICALL
 133 Java_MyPackage_GenerateEventsTest_agent1FailStatus(JNIEnv *env, jclass cls) {
 134   return fail_status;
 135 }
 136 
 137 } // extern "C"