1 /*
   2  * Copyright (c) 2007, 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 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <string.h>
  26 #include <jni.h>
  27 #include <jvmti.h>
  28 #include <aod.h>
  29 #include <jvmti_aod.h>
  30 
  31 extern "C" {
  32 
  33 /*
  34  * Expected agent work scenario:
  35  *  - during initialization agent enables ThreadStart events
  36  *  - target application starts thread
  37  *  - agent receives ThreadStart event and tries to find thread provoked this event
  38  *    in all VM thread groups and finishes work
  39  */
  40 
  41 #define STARTED_TEST_THREAD_NAME "attach041-TestThread"
  42 
  43 static Options* options = NULL;
  44 static const char* agentName;
  45 
  46 int tryFindThread(jvmtiEnv *jvmti, jthreadGroup group, const char* threadNameToFind) {
  47     jint threadsCount = 0;
  48     jthread *threads = NULL;
  49     jint groupsCount = 0;
  50     jthreadGroup* groups = NULL;
  51     jvmtiThreadGroupInfo groupInfo;
  52     int i;
  53     char threadGroupName[MAX_STRING_LENGTH];
  54 
  55     if(!NSK_JVMTI_VERIFY(jvmti->GetThreadGroupInfo(group, &groupInfo))) {
  56         return 0;
  57     }
  58 
  59     strcpy(threadGroupName, groupInfo.name);
  60     nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groupInfo.name);
  61 
  62     NSK_DISPLAY3("%s: trying to find thread '%s' in group '%s'\n", agentName, threadNameToFind, threadGroupName);
  63 
  64     if(!NSK_JVMTI_VERIFY(jvmti->GetThreadGroupChildren(group, &threadsCount, &threads, &groupsCount, &groups))) {
  65         return 0;
  66     }
  67 
  68     for (i = 0; i < threadsCount; i++) {
  69         char threadName[MAX_STRING_LENGTH];
  70         if (!nsk_jvmti_aod_getThreadName(jvmti, threads[i], threadName)) {
  71             NSK_COMPLAIN1("%s: failed to get thread name\n", agentName);
  72             nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
  73             return 0;
  74         }
  75         if (!strcmp(threadName, threadNameToFind)) {
  76             nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
  77             nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups);
  78             NSK_DISPLAY3("%s: thread '%s' was found in group '%s'\n", agentName, threadNameToFind, threadGroupName);
  79             return 1;
  80         }
  81     }
  82 
  83     // threads array isn't needed more
  84     nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
  85 
  86     NSK_DISPLAY3("%s: thread '%s' wasn't found in group '%s'\n", agentName, threadNameToFind, threadGroupName);
  87 
  88     if (groupsCount != 0) {
  89         for (i = 0; i < groupsCount; i++) {
  90             if (tryFindThread(jvmti, groups[i], threadNameToFind)) {
  91                 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups);
  92                 return 1;
  93             }
  94         }
  95     }
  96 
  97     nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups);
  98 
  99     return 0;
 100 }
 101 
 102 void JNICALL threadStartHandler(jvmtiEnv *jvmti,
 103         JNIEnv* jni,
 104         jthread thread) {
 105     char startedThreadName[MAX_STRING_LENGTH];
 106 
 107     if (!nsk_jvmti_aod_getThreadName(jvmti, thread, startedThreadName)) {
 108         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);
 109         return;
 110     }
 111 
 112     NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, startedThreadName);
 113 
 114     if (!strcmp(startedThreadName, STARTED_TEST_THREAD_NAME)) {
 115         int success = 1;
 116         int threadWasFound = 0;
 117         jint groupsCount = 0;
 118         jthreadGroup *topGroups;
 119         int i;
 120 
 121         if(!NSK_JVMTI_VERIFY(jvmti->GetTopThreadGroups(&groupsCount, &topGroups))) {
 122             NSK_COMPLAIN1("%s: failed to get top thread groups\n", agentName);
 123             nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);
 124             return;
 125         }
 126 
 127         for (i = 0; i < groupsCount; i++) {
 128             if (tryFindThread(jvmti, topGroups[i], startedThreadName)) {
 129                 threadWasFound = 1;
 130                 break;
 131             }
 132         }
 133 
 134         nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)topGroups);
 135 
 136         if (!threadWasFound) {
 137             success = 0;
 138             NSK_COMPLAIN2("%s: failed to find thread '%s'\n", agentName, startedThreadName);
 139         }
 140 
 141         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);
 142     }
 143 }
 144 
 145 #ifdef STATIC_BUILD
 146 JNIEXPORT jint JNI_OnLoad_attach041Agent00(JavaVM *jvm, char *options, void *reserved) {
 147     return JNI_VERSION_1_8;
 148 }
 149 #endif
 150 
 151 JNIEXPORT jint JNICALL
 152 #ifdef STATIC_BUILD
 153 Agent_OnAttach_attach041Agent00(JavaVM *vm, char *optionsString, void *reserved)
 154 #else
 155 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 156 #endif
 157 {
 158     jvmtiEventCallbacks eventCallbacks;
 159     jvmtiEnv* jvmti;
 160     JNIEnv* jni;
 161 
 162     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 163         return JNI_ERR;
 164 
 165     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 166 
 167     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 168         return JNI_ERR;
 169 
 170     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 171         return JNI_ERR;
 172 
 173     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 174     eventCallbacks.ThreadStart = threadStartHandler;
 175     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) {
 176         return JNI_ERR;
 177     }
 178 
 179     if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_THREAD_START))) {
 180         return JNI_ERR;
 181     }
 182 
 183     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 184 
 185     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 186         return JNI_ERR;
 187 
 188     return JNI_OK;
 189 }
 190 
 191 }