1 /*
   2  * Copyright (c) 2004, 2018, 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 <stdlib.h>
  26 #include "jvmti.h"
  27 #include "agent_common.h"
  28 #include "jni_tools.h"
  29 #include "jvmti_tools.h"
  30 #include "JVMTITools.h"
  31 #include "nsk_list.h"
  32 
  33 extern "C" {
  34 
  35 /* ============================================================================= */
  36 
  37 /* scaffold objects */
  38 static jvmtiEnv *jvmti = NULL;
  39 static jlong timeout = 0;
  40 static jrawMonitorID syncLock = NULL;
  41 
  42 static int methodLoadCount = 0;
  43 static int methodUnloadCount = 0;
  44 
  45 #define NAME_LENGTH 50
  46 const void *plist = NULL;
  47 
  48 typedef struct nsk_jvmti_CompiledMethodIDStruct {
  49     jmethodID method;
  50     const void* code_addr;
  51     char name[NAME_LENGTH];
  52 } nsk_jvmti_CompiledMethod;
  53 
  54 
  55 /* ============================================================================= */
  56 
  57 /* callbacks */
  58 void JNICALL
  59 cbCompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size,
  60                 const void* code_addr, jint map_length,
  61                 const jvmtiAddrLocationMap* map, const void* compile_info) {
  62     char *name;
  63     char *sign;
  64     char *genc;
  65 
  66     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &sign, &genc))) {
  67         nsk_jvmti_setFailStatus();
  68         return;
  69     }
  70 
  71     if (!strncmp(name,"javaMethod", 8)) {
  72         nsk_jvmti_CompiledMethod *rec =
  73             (nsk_jvmti_CompiledMethod *)malloc(sizeof(nsk_jvmti_CompiledMethod));
  74 
  75         rec->method = method;
  76         rec->code_addr = code_addr;
  77         strncpy(rec->name, name, NAME_LENGTH);
  78         rec->name[NAME_LENGTH - 1] = '\0';
  79 
  80         if (!NSK_VERIFY(nsk_list_add(plist, rec))) {
  81             nsk_jvmti_setFailStatus();
  82             free((void *)rec);
  83         } else {
  84             NSK_DISPLAY0(">>>JVMTI_EVENT_COMPILED_METHOD_LOAD received for\n");
  85             NSK_DISPLAY1("\t\tmethod: %s\n", rec->name);
  86 
  87             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(syncLock)))
  88                 nsk_jvmti_setFailStatus();
  89 
  90             methodLoadCount++;
  91 
  92             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(syncLock)))
  93                 nsk_jvmti_setFailStatus();
  94 
  95         }
  96     }
  97 
  98     if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)name))) {
  99         nsk_jvmti_setFailStatus();
 100     }
 101     if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)sign))) {
 102         nsk_jvmti_setFailStatus();
 103     }
 104     if (genc != NULL)
 105         if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)genc))) {
 106             nsk_jvmti_setFailStatus();
 107         }
 108 }
 109 
 110 void JNICALL
 111 cbCompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method,
 112                 const void* code_addr) {
 113 
 114     nsk_jvmti_CompiledMethod *rec;
 115 
 116     int count = nsk_list_getCount(plist);
 117     int i;
 118 
 119     for (i = 0; i < count; i ++) {
 120         rec = (nsk_jvmti_CompiledMethod *)nsk_list_get(plist, i);
 121         if ((rec->code_addr == code_addr) && (rec->method == method)) {
 122             NSK_DISPLAY0(">>>JVMTI_EVENT_COMPILED_METHOD_UNLOAD received for\n");
 123             NSK_DISPLAY1("\t\tmethod: %s\n", rec->name);
 124 
 125             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(syncLock)))
 126                 nsk_jvmti_setFailStatus();
 127 
 128             methodUnloadCount++;
 129 
 130             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(syncLock)))
 131                 nsk_jvmti_setFailStatus();
 132 
 133             free(rec);
 134             nsk_list_remove(plist, i);
 135             return;
 136         }
 137 
 138     }
 139 
 140 }
 141 
 142 /* ============================================================================= */
 143 
 144 static int
 145 enableEvent(jvmtiEventMode enable, jvmtiEvent event) {
 146     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(enable, event, NULL))) {
 147         nsk_jvmti_setFailStatus();
 148         return NSK_FALSE;
 149     }
 150 
 151     return NSK_TRUE;
 152 }
 153 
 154 int checkEvents() {
 155 
 156     int result = methodUnloadCount <= methodLoadCount;
 157 
 158     if (result) {
 159         NSK_DISPLAY0("Received correct number of events:\n");
 160         NSK_DISPLAY1("\t\tCOMPILED_METHOD_LOAD events number = %d\n",
 161                                 methodLoadCount);
 162         NSK_DISPLAY1("\t\tCOMPILED_METHOD_UNLOAD events number = %d\n",
 163                                 methodUnloadCount);
 164     } else {
 165         NSK_COMPLAIN0("Received incorrect number of events:\n");
 166         NSK_COMPLAIN1("\t\tCOMPILED_METHOD_LOAD events number = %d\n",
 167                                 methodLoadCount);
 168         NSK_COMPLAIN1("\t\tCOMPILED_METHOD_UNLOAD events number = %d\n",
 169                                 methodUnloadCount);
 170     }
 171 
 172     return result;
 173 }
 174 
 175 /* ============================================================================= */
 176 
 177 static int
 178 setCallBacks() {
 179 
 180     jvmtiEventCallbacks eventCallbacks;
 181 
 182     memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 183 
 184     eventCallbacks.CompiledMethodLoad        = cbCompiledMethodLoad;
 185     eventCallbacks.CompiledMethodUnload      = cbCompiledMethodUnload;
 186 
 187     return NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)));
 188 }
 189 
 190 /* ============================================================================= */
 191 
 192 /** Agent algorithm. */
 193 static void JNICALL
 194 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
 195 
 196     int i;
 197 
 198     int attempts = nsk_jvmti_findOptionIntValue("attempts", 1);
 199 
 200     for (i = 0; i < attempts; i++) {
 201 
 202         if (!nsk_jvmti_waitForSync(timeout))
 203             return;
 204 
 205         if (!checkEvents())
 206             nsk_jvmti_setFailStatus();
 207 
 208         NSK_DISPLAY0("Let debuggee to continue\n");
 209         if (!nsk_jvmti_resumeSync())
 210             return;
 211     }
 212 
 213     {
 214         int count = nsk_list_getCount(plist);
 215 
 216         while (count > 0) {
 217             free((void *)nsk_list_get(plist, 0));
 218             nsk_list_remove(plist, 0);
 219             count = nsk_list_getCount(plist);
 220         }
 221 
 222     }
 223 
 224     if (!NSK_JVMTI_VERIFY(jvmti->DestroyRawMonitor(syncLock)))
 225         nsk_jvmti_setFailStatus();
 226 
 227 }
 228 
 229 /* ============================================================================= */
 230 
 231 /** Agent library initialization. */
 232 #ifdef STATIC_BUILD
 233 JNIEXPORT jint JNICALL Agent_OnLoad_em07t002(JavaVM *jvm, char *options, void *reserved) {
 234     return Agent_Initialize(jvm, options, reserved);
 235 }
 236 JNIEXPORT jint JNICALL Agent_OnAttach_em07t002(JavaVM *jvm, char *options, void *reserved) {
 237     return Agent_Initialize(jvm, options, reserved);
 238 }
 239 JNIEXPORT jint JNI_OnLoad_em07t002(JavaVM *jvm, char *options, void *reserved) {
 240     return JNI_VERSION_1_8;
 241 }
 242 #endif
 243 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 244 
 245     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 246         return JNI_ERR;
 247 
 248     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 249 
 250     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 251         return JNI_ERR;
 252 
 253     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_syncLock", &syncLock))) {
 254         nsk_jvmti_setFailStatus();
 255         return JNI_ERR;
 256     }
 257 
 258     if (!NSK_VERIFY((plist = (const void *)nsk_list_create()) != NULL))
 259         return JNI_ERR;
 260 
 261     {
 262         jvmtiCapabilities caps;
 263         memset(&caps, 0, sizeof(caps));
 264 
 265         caps.can_generate_compiled_method_load_events = 1;
 266         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
 267             return JNI_ERR;
 268     }
 269 
 270     if (!setCallBacks()) {
 271         return JNI_ERR;
 272     }
 273 
 274     if (!enableEvent(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD) ||
 275                 !enableEvent(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) {
 276         return JNI_ERR;
 277     }
 278 
 279     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 280         return JNI_ERR;
 281 
 282     return JNI_OK;
 283 }
 284 
 285 /* ============================================================================= */
 286 
 287 
 288 }