1 /*
  2  * Copyright (c) 2003, 2020, 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 <stdlib.h>
 26 #include <string.h>
 27 #include "jvmti.h"
 28 #include "jvmti_common.h"
 29 
 30 extern "C" {
 31 
 32 typedef struct {
 33   int cnt;
 34   const char **thr_names;
 35 } info;
 36 
 37 static jvmtiEnv *jvmti_env;
 38 static jrawMonitorID starting_agent_thread_lock;
 39 static jrawMonitorID stopping_agent_thread_lock;
 40 static int system_threads_count;
 41 static const char *names0[] = {"main"};
 42 static const char *names1[] = {"main", "thread1"};
 43 static const char *names2[] = {"main", "Thread-"};
 44 static const char *names3[] = {"main", "ForkJoinPool-"};
 45 
 46 /*
 47  * Expected number and names of threads started by test for each test point
 48  */
 49 static info expected_thread_info[] = {
 50     {1, names0}, {1, names0}, {2, names1},
 51     {1, names0}, {2, names2},  {2, names3}
 52 };
 53 
 54 const char VTHREAD_PREFIX[] = "ForkJoinPool";
 55 
 56 
 57 jthread create_jthread(JNIEnv *jni) {
 58   jclass thrClass = jni->FindClass("java/lang/Thread");
 59   jmethodID cid = jni->GetMethodID(thrClass, "<init>", "()V");
 60   return jni->NewObject(thrClass, cid);
 61 }
 62 
 63 static void JNICALL
 64 sys_thread(jvmtiEnv *jvmti, JNIEnv *jni, void *p) {
 65   RawMonitorLocker rml2 = RawMonitorLocker(jvmti, jni, stopping_agent_thread_lock);
 66   {
 67     RawMonitorLocker rml1 = RawMonitorLocker(jvmti, jni, starting_agent_thread_lock);
 68     rml1.notify();
 69   }
 70   rml2.wait();
 71 }
 72 
 73 jint Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
 74   jvmtiCapabilities caps;
 75   jvmtiError err;
 76   jint res;
 77 
 78   res = jvm->GetEnv((void **) &jvmti_env, JVMTI_VERSION_1_1);
 79   if (res != JNI_OK || jvmti_env == NULL) {
 80     LOG("Wrong result of a valid call to GetEnv!\n");
 81     return JNI_ERR;
 82   }
 83   memset(&caps, 0, sizeof(caps));
 84   caps.can_support_virtual_threads = true;
 85 
 86   err = jvmti_env->AddCapabilities(&caps);
 87   if (err != JVMTI_ERROR_NONE) {
 88     LOG("(AddCapabilities) unexpected error: %s (%d)\n", TranslateError(err), err);
 89     return JNI_ERR;
 90   }
 91 
 92   starting_agent_thread_lock = create_raw_monitor(jvmti_env, "_started_agent_thread_lock");
 93   stopping_agent_thread_lock = create_raw_monitor(jvmti_env, "_stopping_agent_thread_lock");
 94 
 95   return JNI_OK;
 96 }
 97 
 98 JNIEXPORT jboolean check_info(JNIEnv *jni, int idx) {
 99   jboolean result = JNI_TRUE;
100   jint threads_count = -1;
101   jthread *threads;
102   int num_unexpected = 0;
103 
104   LOG(" >>> Check point: %d\n", idx);
105 
106   check_jvmti_status(jni, jvmti_env->GetAllThreads(&threads_count, &threads), "Failed in GetAllThreads");
107 
108   for (int i = 0; i < threads_count; i++) {
109     if (!isThreadExpected(jvmti_env, threads[i])) {
110       num_unexpected++;
111       LOG(">>> unexpected:  ");
112     } else {
113       LOG(">>> expected: ");
114     }
115     print_thread_info(jvmti_env, jni, threads[i]);
116   }
117 
118   if (threads_count - num_unexpected != expected_thread_info[idx].cnt + system_threads_count) {
119     LOG("Point %d: number of threads expected: %d, got: %d\n",
120            idx, expected_thread_info[idx].cnt + system_threads_count, threads_count - num_unexpected);
121     return JNI_FALSE;
122   }
123 
124   for (int i = 0; i < expected_thread_info[idx].cnt; i++) {
125     bool found = false;
126     for (int j = 0; j < threads_count && !found; j++) {
127       char *name = get_thread_name(jvmti_env, jni, threads[j]);
128       found = strstr(name, expected_thread_info[idx].thr_names[i]);
129       if (found) {
130         LOG(" >>> found: %s\n", name);
131       }
132       //== name &&
133         //  (idx == POINT_AGENT_THREAD || strlen(name) ==
134           //    strlen(expected_thread_info[idx].thr_names[i]));
135     }
136 
137     if (!found) {
138       LOG("Point %d: thread %s not detected\n",
139              idx, expected_thread_info[idx].thr_names[i]);
140       result = JNI_FALSE;
141     }
142   }
143 
144   deallocate(jvmti_env, jni, threads);
145 
146   return result;
147 }
148 
149 JNIEXPORT void Java_allthr01_startAgentThread(JNIEnv *jni) {
150   RawMonitorLocker rml1 = RawMonitorLocker(jvmti_env, jni, starting_agent_thread_lock);
151   jvmtiError err = jvmti_env->RunAgentThread(create_jthread(jni), sys_thread, NULL,JVMTI_THREAD_NORM_PRIORITY);
152   check_jvmti_status(jni, err, "Failed to run AgentThread");
153   rml1.wait();
154   LOG("Started Agent Thread\n");
155 }
156 
157 JNIEXPORT void Java_allthr01_stopAgentThread(JNIEnv *jni) {
158   RawMonitorLocker rml2 = RawMonitorLocker(jvmti_env, jni, stopping_agent_thread_lock);
159   rml2.notify();
160   LOG("Stopped Agent Thread\n");
161 }
162 
163 
164 
165 JNIEXPORT void JNICALL Java_allthr01_setSysCnt(JNIEnv *env, jclass cls) {
166   jint threadsCount = -1;
167   jthread *threads;
168 
169   check_jvmti_status(env, jvmti_env->GetAllThreads(&threadsCount, &threads), "Failed in GetAllThreads");
170 
171   system_threads_count = threadsCount - 1;
172 
173   for (int i = 0; i < threadsCount; i++) {
174     if (!isThreadExpected(jvmti_env, threads[i])) {
175       system_threads_count--;
176     }
177   }
178 
179   LOG(" >>> number of system threads: %d\n", system_threads_count);
180 
181 }
182 
183 JNIEXPORT jboolean JNICALL
184 Java_allthr01_checkInfo0(JNIEnv *env, jclass cls, jint expected_idx) {
185   return check_info(env, expected_idx);
186 }
187 
188 }