test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetTopThreadGroups/topthrgrp001/topthrgrp001.c
changeset 50260 46c67f5e27c2
equal deleted inserted replaced
50259:01d27ae7a84e 50260:46c67f5e27c2
       
     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 #ifdef __cplusplus
       
    31 extern "C" {
       
    32 #endif
       
    33 
       
    34 #ifndef JNI_ENV_ARG
       
    35 
       
    36 #ifdef __cplusplus
       
    37 #define JNI_ENV_ARG(x, y) y
       
    38 #define JNI_ENV_PTR(x) x
       
    39 #else
       
    40 #define JNI_ENV_ARG(x,y) x, y
       
    41 #define JNI_ENV_PTR(x) (*x)
       
    42 #endif
       
    43 
       
    44 #endif
       
    45 
       
    46 #define PASSED 0
       
    47 #define STATUS_FAILED 2
       
    48 
       
    49 static jvmtiEnv *jvmti = NULL;
       
    50 static jint result = PASSED;
       
    51 static jboolean printdump = JNI_FALSE;
       
    52 
       
    53 #ifdef STATIC_BUILD
       
    54 JNIEXPORT jint JNICALL Agent_OnLoad_topthrgrp001(JavaVM *jvm, char *options, void *reserved) {
       
    55     return Agent_Initialize(jvm, options, reserved);
       
    56 }
       
    57 JNIEXPORT jint JNICALL Agent_OnAttach_topthrgrp001(JavaVM *jvm, char *options, void *reserved) {
       
    58     return Agent_Initialize(jvm, options, reserved);
       
    59 }
       
    60 JNIEXPORT jint JNI_OnLoad_topthrgrp001(JavaVM *jvm, char *options, void *reserved) {
       
    61     return JNI_VERSION_1_8;
       
    62 }
       
    63 #endif
       
    64 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
       
    65     jint res;
       
    66 
       
    67     if (options != NULL && strcmp(options, "printdump") == 0) {
       
    68         printdump = JNI_TRUE;
       
    69     }
       
    70 
       
    71     res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
       
    72         JVMTI_VERSION_1_1);
       
    73     if (res != JNI_OK || jvmti == NULL) {
       
    74         printf("Wrong result of a valid call to GetEnv!\n");
       
    75         return JNI_ERR;
       
    76     }
       
    77 
       
    78     return JNI_OK;
       
    79 }
       
    80 
       
    81 JNIEXPORT jint JNICALL
       
    82 Java_nsk_jvmti_GetTopThreadGroups_topthrgrp001_check(JNIEnv *env, jclass cls) {
       
    83     jvmtiError err;
       
    84     jint i, groupCount = 0;
       
    85     jthreadGroup *groups = NULL;
       
    86     jvmtiThreadGroupInfo inf;
       
    87 
       
    88     if (jvmti == NULL) {
       
    89         printf("JVMTI client was not properly loaded!\n");
       
    90         return STATUS_FAILED;
       
    91     }
       
    92 
       
    93     if (printdump == JNI_TRUE) {
       
    94         printf(">>> getting top thread groups list ...\n");
       
    95     }
       
    96     err = (*jvmti)->GetTopThreadGroups(jvmti, &groupCount, &groups);
       
    97     if (err != JVMTI_ERROR_NONE) {
       
    98         printf("(GetTopThreadGroups) unexpected error: %s (%d)\n",
       
    99                TranslateError(err), err);
       
   100         result = STATUS_FAILED;
       
   101     }
       
   102 
       
   103     if (printdump == JNI_TRUE) {
       
   104         printf(">>> getting info about groups ...\n");
       
   105     }
       
   106     for (i = 0; i < groupCount; i++) {
       
   107         err = (*jvmti)->GetThreadGroupInfo(jvmti, groups[i], &inf);
       
   108         if (err != JVMTI_ERROR_NONE) {
       
   109             printf("(GetThreadGroupInfo#%d) unexpected error: %s (%d)\n",
       
   110                    i, TranslateError(err), err);
       
   111             result = STATUS_FAILED;
       
   112         } else {
       
   113             if (printdump == JNI_TRUE) {
       
   114                 printf(">>> group %d ----------------\n", i);
       
   115                 printf(">>>           name: %s\n", inf.name);
       
   116                 printf(">>>         parent: 0x%p\n", inf.parent);
       
   117                 printf(">>>   max priority: %d\n", inf.max_priority);
       
   118                 printf(">>>      is daemon: %s\n",
       
   119                        (inf.is_daemon==JNI_TRUE) ? "true" : "false");
       
   120             }
       
   121             if (inf.parent != NULL) {
       
   122                 printf("ERROR: thread group is not parentless!\n");
       
   123                 printf("   group name: %s\n", inf.name);
       
   124                 printf("       parent: 0x%p\n", inf.parent);
       
   125                 result = STATUS_FAILED;
       
   126             }
       
   127         }
       
   128     }
       
   129 
       
   130     if (printdump == JNI_TRUE) {
       
   131         printf(">>> done ...\n");
       
   132     }
       
   133 
       
   134     return result;
       
   135 }
       
   136 
       
   137 #ifdef __cplusplus
       
   138 }
       
   139 #endif