1 /*
  2  * Copyright (c) 2003, 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 <stdio.h>
 25 #include <string.h>
 26 #include "jvmti.h"
 27 #include "agent_common.h"
 28 #include "JVMTITools.h"
 29 
 30 extern "C" {
 31 
 32 
 33 #define PASSED  0
 34 #define STATUS_FAILED  2
 35 
 36 typedef struct {
 37     const char *name;
 38     int priority;
 39     int is_daemon;
 40 } info;
 41 
 42 static jvmtiEnv *jvmti = NULL;
 43 static jint result = PASSED;
 44 static jvmtiThreadInfo inf;
 45 static info threads[] = {
 46     { "main", JVMTI_THREAD_NORM_PRIORITY, 0 },
 47     { "thread1", JVMTI_THREAD_MIN_PRIORITY + 2, 1 },
 48     { "Thread-", JVMTI_THREAD_MIN_PRIORITY, 1 },
 49     { "vthread", JVMTI_THREAD_NORM_PRIORITY, 1 }
 50 };
 51 
 52 #ifdef STATIC_BUILD
 53 JNIEXPORT jint JNICALL Agent_OnLoad_thrinfo001(JavaVM *jvm, char *options, void *reserved) {
 54     return Agent_Initialize(jvm, options, reserved);
 55 }
 56 JNIEXPORT jint JNICALL Agent_OnAttach_thrinfo001(JavaVM *jvm, char *options, void *reserved) {
 57     return Agent_Initialize(jvm, options, reserved);
 58 }
 59 JNIEXPORT jint JNI_OnLoad_thrinfo001(JavaVM *jvm, char *options, void *reserved) {
 60     return JNI_VERSION_1_8;
 61 }
 62 #endif
 63 jint  Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 64     jint res;
 65     jvmtiCapabilities caps;
 66 
 67     res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
 68     if (res != JNI_OK || jvmti == NULL) {
 69         printf("Wrong result of a valid call to GetEnv !\n");
 70         return JNI_ERR;
 71     }
 72 
 73     memset(&caps, 0, sizeof(caps));
 74     caps.can_support_virtual_threads = 1;
 75     res = jvmti->AddCapabilities(&caps);
 76     if (res != JVMTI_ERROR_NONE) {
 77       printf("error in JVMTI AddCapabilities: %d\n", res);
 78       return JNI_ERR;
 79     }
 80     return JNI_OK;
 81 }
 82 
 83 JNIEXPORT void JNICALL Java_nsk_jvmti_GetThreadInfo_thrinfo001_checkInfo(JNIEnv *env, jclass cls,
 84         jthread thr, jthreadGroup group, jint ind) {
 85     jvmtiError err;
 86 
 87     err = jvmti->GetThreadInfo(thr, &inf);
 88     if (err != JVMTI_ERROR_NONE) {
 89         printf("(GetThreadInfo#%d) unexpected error: %s (%d)\n",
 90             ind, TranslateError(err), err);
 91         result = STATUS_FAILED;
 92     }
 93 
 94     if (inf.name == NULL ||
 95             strstr(inf.name, threads[ind].name) != inf.name ||
 96             (ind < 2 && strlen(inf.name) != strlen(threads[ind].name))) {
 97         printf("Thread %s: incorrect name: %s\n", threads[ind].name, inf.name);
 98         result = STATUS_FAILED;
 99     }
100     if (inf.priority != threads[ind].priority) {
101         printf("Thread %s: priority expected: %d, got: %d\n",
102             threads[ind].name, threads[ind].priority, inf.priority);
103         result = STATUS_FAILED;
104     }
105     if (inf.is_daemon != threads[ind].is_daemon) {
106         printf("Thread %s: is_daemon expected: %d, got: %d\n",
107            threads[ind].name, threads[ind].is_daemon, inf.is_daemon);
108         result = STATUS_FAILED;
109     }
110     if (!env->IsSameObject(group, inf.thread_group)) {
111         printf("Thread %s: invalid thread group\n", threads[ind].name);
112         result = STATUS_FAILED;
113     }
114 }
115 
116 JNIEXPORT jint JNICALL Java_nsk_jvmti_GetThreadInfo_thrinfo001_getRes(JNIEnv *env, jclass cls) {
117     return result;
118 }
119 
120 }