test/hotspot/jtreg/vmTestbase/nsk/jvmti/unit/GetAllStackTraces/getallstktr001/getallstktr001.cpp
changeset 51775 dfed97156841
parent 51551 e409244ce72e
equal deleted inserted replaced
51774:79dc492c00ab 51775:dfed97156841
    25 #include <string.h>
    25 #include <string.h>
    26 #include "jvmti.h"
    26 #include "jvmti.h"
    27 #include "jni_tools.h"
    27 #include "jni_tools.h"
    28 #include "agent_common.h"
    28 #include "agent_common.h"
    29 
    29 
    30 #ifdef __cplusplus
       
    31 extern "C" {
    30 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_ARG1(x)
       
    39 #define JNI_ENV_PTR(x) x
       
    40 #else
       
    41 #define JNI_ENV_ARG(x,y) x, y
       
    42 #define JNI_ENV_ARG1(x) x
       
    43 #define JNI_ENV_PTR(x) (*x)
       
    44 #endif
       
    45 
       
    46 #endif
       
    47 
       
    48 #define JVMTI_ENV_ARG  JNI_ENV_ARG
       
    49 #define JVMTI_ENV_ARG1 JNI_ENV_ARG1
       
    50 #define JVMTI_ENV_PTR  JNI_ENV_PTR
       
    51 
    31 
    52 #define STATUS_FAILED 2
    32 #define STATUS_FAILED 2
    53 #define PASSED 0
    33 #define PASSED 0
    54 
    34 
    55 #define JVMTI_ERROR_CHECK(str,res)   \
    35 #define JVMTI_ERROR_CHECK(str,res)   \
    56     if ( res != JVMTI_ERROR_NONE) {  \
    36     if (res != JVMTI_ERROR_NONE) {  \
    57         printf("%s %d\n" ,str, res); \
    37         printf("%s %d\n" ,str, res); \
    58         return res;                  \
    38         return res;                  \
    59     }
    39     }
    60 
    40 
    61 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR(str,res,err) \
    41 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR(str,res,err) \
    62     if ( res != err) {                                \
    42     if (res != err) {                                \
    63         printf("%s unexpected error %d\n", str, res); \
    43         printf("%s unexpected error %d\n", str, res); \
    64         return res;                                   \
    44         return res;                                   \
    65     }
    45     }
    66 
    46 
    67 #define JVMTI_ERROR_CHECK_VOID(str,res) \
    47 #define JVMTI_ERROR_CHECK_VOID(str,res) \
   103 }
    83 }
   104 #endif
    84 #endif
   105 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) {
    85 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) {
   106     jint res;
    86     jint res;
   107 
    87 
   108     res = JNI_ENV_PTR(jvm)->
    88     res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
   109         GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), JVMTI_VERSION_1_1);
       
   110     if (res < 0) {
    89     if (res < 0) {
   111         printf("Wrong result of a valid call to GetEnv!\n");
    90         printf("Wrong result of a valid call to GetEnv!\n");
   112         return JNI_ERR;
    91         return JNI_ERR;
   113     }
    92     }
   114 
    93 
   115     /* Add capabilities */
    94     /* Add capabilities */
   116     res = JVMTI_ENV_PTR(jvmti)->GetPotentialCapabilities(JVMTI_ENV_ARG(jvmti, &jvmti_caps));
    95     res = jvmti->GetPotentialCapabilities(&jvmti_caps);
   117     JVMTI_ERROR_CHECK("GetPotentialCapabilities returned error", res);
    96     JVMTI_ERROR_CHECK("GetPotentialCapabilities returned error", res);
   118 
    97 
   119     res = JVMTI_ENV_PTR(jvmti)->AddCapabilities(JVMTI_ENV_ARG(jvmti, &jvmti_caps));
    98     res = jvmti->AddCapabilities(&jvmti_caps);
   120     JVMTI_ERROR_CHECK("GetPotentialCapabilities returned error", res);
    99     JVMTI_ERROR_CHECK("GetPotentialCapabilities returned error", res);
   121 
   100 
   122     return JNI_OK;
   101     return JNI_OK;
   123 }
   102 }
   124 
   103 
   137 {
   116 {
   138     jvmtiError ret;
   117     jvmtiError ret;
   139     char sz[128];
   118     char sz[128];
   140 
   119 
   141     sprintf(sz, "Raw-monitor");
   120     sprintf(sz, "Raw-monitor");
   142     ret = JVMTI_ENV_PTR(jvmti)->CreateRawMonitor(JVMTI_ENV_ARG(jvmti, sz),
   121     ret = jvmti->CreateRawMonitor(sz, &jraw_monitor);
   143                                                  &jraw_monitor);
       
   144 
   122 
   145     if (ret != JVMTI_ERROR_NONE) {
   123     if (ret != JVMTI_ERROR_NONE) {
   146         printf("Error: Raw monitor create %d \n", ret);
   124         printf("Error: Raw monitor create %d \n", ret);
   147         iGlobalStatus = STATUS_FAILED;
   125         iGlobalStatus = STATUS_FAILED;
   148     }
   126     }
   152 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_RawMonitorEnter(
   130 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_RawMonitorEnter(
   153     JNIEnv * env, jclass cls)
   131     JNIEnv * env, jclass cls)
   154 {
   132 {
   155     jvmtiError ret;
   133     jvmtiError ret;
   156 
   134 
   157     ret = JVMTI_ENV_PTR(jvmti)->RawMonitorEnter(JVMTI_ENV_ARG(jvmti, jraw_monitor));
   135     ret = jvmti->RawMonitorEnter(jraw_monitor);
   158 
   136 
   159     if (ret != JVMTI_ERROR_NONE) {
   137     if (ret != JVMTI_ERROR_NONE) {
   160         printf("Error: Raw monitor enter %d \n", ret);
   138         printf("Error: Raw monitor enter %d \n", ret);
   161         iGlobalStatus = STATUS_FAILED;
   139         iGlobalStatus = STATUS_FAILED;
   162     }
   140     }
   166 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_RawMonitorExit(
   144 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_RawMonitorExit(
   167     JNIEnv * env, jclass cls)
   145     JNIEnv * env, jclass cls)
   168 {
   146 {
   169     jvmtiError ret;
   147     jvmtiError ret;
   170 
   148 
   171     ret = JVMTI_ENV_PTR(jvmti)->RawMonitorExit(JVMTI_ENV_ARG(jvmti, jraw_monitor));
   149     ret = jvmti->RawMonitorExit(jraw_monitor);
   172 
   150 
   173     if (ret != JVMTI_ERROR_NONE) {
   151     if (ret != JVMTI_ERROR_NONE) {
   174         printf("Error: RawMonitorExit %d \n", ret);
   152         printf("Error: RawMonitorExit %d \n", ret);
   175         iGlobalStatus = STATUS_FAILED;
   153         iGlobalStatus = STATUS_FAILED;
   176     }
   154     }
   271      JNIEnv * env, jclass cls)
   249      JNIEnv * env, jclass cls)
   272 {
   250 {
   273     jvmtiError ret;
   251     jvmtiError ret;
   274     int ti;
   252     int ti;
   275 
   253 
   276     ret = JVMTI_ENV_PTR(jvmti)->GetAllStackTraces(JVMTI_ENV_ARG(jvmti, MAX_FRAMES_CNT),
   254     ret = jvmti->GetAllStackTraces(MAX_FRAMES_CNT, &stack_buf1, &threads_count);
   277                                                   &stack_buf1,
       
   278                                                   &threads_count);
       
   279     if (ret != JVMTI_ERROR_NONE) {
   255     if (ret != JVMTI_ERROR_NONE) {
   280         printf("Error: GetAllStackTraces %d \n", ret);
   256         printf("Error: GetAllStackTraces %d \n", ret);
   281         iGlobalStatus = STATUS_FAILED;
   257         iGlobalStatus = STATUS_FAILED;
   282     }
   258     }
   283 
   259 
   284     ret = JVMTI_ENV_PTR(jvmti)->Allocate(JVMTI_ENV_ARG(jvmti, sizeof(jthread) * threads_count),
   260     ret = jvmti->Allocate(sizeof(jthread) * threads_count,
   285                                          (unsigned char**)&thread_list);
   261                           (unsigned char**) &thread_list);
   286     if (ret != JVMTI_ERROR_NONE) {
   262     if (ret != JVMTI_ERROR_NONE) {
   287         printf("Error: Allocate failed with  %d \n", ret);
   263         printf("Error: Allocate failed with  %d \n", ret);
   288         iGlobalStatus = STATUS_FAILED;
   264         iGlobalStatus = STATUS_FAILED;
   289     }
   265     }
   290 
   266 
   291     for (ti = 0; ti < threads_count; ti++) {
   267     for (ti = 0; ti < threads_count; ti++) {
   292         thread_list[ti] =
   268         thread_list[ti] =
   293           (jthread)JNI_ENV_PTR(env)->NewGlobalRef(
   269           (jthread)env->NewGlobalRef(stack_buf1[ti].thread);
   294                      JNI_ENV_ARG(env, stack_buf1[ti].thread));
       
   295     }
   270     }
   296 }
   271 }
   297 
   272 
   298 JNIEXPORT void JNICALL
   273 JNIEXPORT void JNICALL
   299 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetThreadsInfo(
   274 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetThreadsInfo(
   300      JNIEnv * env, jclass cls)
   275      JNIEnv * env, jclass cls)
   301 {
   276 {
   302     jvmtiError ret;
   277     jvmtiError ret;
   303     int ti;
   278     int ti;
   304 
   279 
   305     ret = JVMTI_ENV_PTR(jvmti)->Allocate(JVMTI_ENV_ARG(jvmti, sizeof(jvmtiThreadInfo) * threads_count),
   280     ret = jvmti->Allocate(sizeof(jvmtiThreadInfo) * threads_count,
   306                                          (unsigned char**)&thread_info);
   281                           (unsigned char**)&thread_info);
   307     if (ret != JVMTI_ERROR_NONE) {
   282     if (ret != JVMTI_ERROR_NONE) {
   308         printf("Error: Allocate failed with  %d \n", ret);
   283         printf("Error: Allocate failed with  %d \n", ret);
   309         iGlobalStatus = STATUS_FAILED;
   284         iGlobalStatus = STATUS_FAILED;
   310     }
   285     }
   311 
   286 
   312     for (ti = 0; ti < threads_count; ti++) {
   287     for (ti = 0; ti < threads_count; ti++) {
   313         ret = JVMTI_ENV_PTR(jvmti)->GetThreadInfo(JVMTI_ENV_ARG(jvmti, thread_list[ti]),
   288         ret = jvmti->GetThreadInfo(thread_list[ti], &thread_info[ti]);
   314                                                   &thread_info[ti]);
       
   315         if (ret != JVMTI_ERROR_NONE) {
   289         if (ret != JVMTI_ERROR_NONE) {
   316             printf("Error: GetThreadInfo %d \n", ret);
   290             printf("Error: GetThreadInfo %d \n", ret);
   317             iGlobalStatus = STATUS_FAILED;
   291             iGlobalStatus = STATUS_FAILED;
   318         }
   292         }
   319         printf("GetThreadInfo %d: thread: %s\n", ti, thread_info[ti].name);
   293         printf("GetThreadInfo %d: thread: %s\n", ti, thread_info[ti].name);
   325 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetThreadListStackTraces(
   299 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_GetThreadListStackTraces(
   326      JNIEnv * env, jclass cls)
   300      JNIEnv * env, jclass cls)
   327 {
   301 {
   328     jvmtiError ret;
   302     jvmtiError ret;
   329 
   303 
   330     ret = JVMTI_ENV_PTR(jvmti)->GetThreadListStackTraces(
   304     ret = jvmti->GetThreadListStackTraces(
   331                                                   JVMTI_ENV_ARG(jvmti, threads_count),
   305         threads_count, thread_list, MAX_FRAMES_CNT, &stack_buf2);
   332                                                   thread_list,
       
   333                                                   MAX_FRAMES_CNT,
       
   334                                                   &stack_buf2);
       
   335     if (ret != JVMTI_ERROR_NONE) {
   306     if (ret != JVMTI_ERROR_NONE) {
   336         printf("Error: GetThreadListStackTraces %d \n", ret);
   307         printf("Error: GetThreadListStackTraces %d \n", ret);
   337         iGlobalStatus = STATUS_FAILED;
   308         iGlobalStatus = STATUS_FAILED;
   338     }
   309     }
   339 
   310 
   342 JNIEXPORT void JNICALL
   313 JNIEXPORT void JNICALL
   343 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_ForceGC(
   314 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_ForceGC(
   344      JNIEnv * env, jclass cls)
   315      JNIEnv * env, jclass cls)
   345 {
   316 {
   346     jvmtiError ret;
   317     jvmtiError ret;
   347     ret = JVMTI_ENV_PTR(jvmti)->ForceGarbageCollection(JVMTI_ENV_ARG1(jvmti));
   318     ret = jvmti->ForceGarbageCollection();
   348 
   319 
   349     if (ret != JVMTI_ERROR_NONE) {
   320     if (ret != JVMTI_ERROR_NONE) {
   350         printf("Error: ForceGarbageCollection %d \n", ret);
   321         printf("Error: ForceGarbageCollection %d \n", ret);
   351         iGlobalStatus = STATUS_FAILED;
   322         iGlobalStatus = STATUS_FAILED;
   352     }
   323     }
   363 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_DeallocateBuffers(
   334 Java_nsk_jvmti_unit_GetAllStackTraces_getallstktr001_DeallocateBuffers(
   364      JNIEnv * env, jclass cls)
   335      JNIEnv * env, jclass cls)
   365 {
   336 {
   366     jvmtiError ret;
   337     jvmtiError ret;
   367 
   338 
   368     ret = JVMTI_ENV_PTR(jvmti)->Deallocate(JVMTI_ENV_ARG(jvmti, (unsigned char *)stack_buf1));
   339     ret = jvmti->Deallocate((unsigned char *) stack_buf1);
   369     if (ret != JVMTI_ERROR_NONE) {
   340     if (ret != JVMTI_ERROR_NONE) {
   370         printf("Error: Deallocate stack_buf1 failed with  %d \n", ret);
   341         printf("Error: Deallocate stack_buf1 failed with  %d \n", ret);
   371         iGlobalStatus = STATUS_FAILED;
   342         iGlobalStatus = STATUS_FAILED;
   372     }
   343     }
   373 
   344 
   374     ret = JVMTI_ENV_PTR(jvmti)->Deallocate(JVMTI_ENV_ARG(jvmti, (unsigned char *)stack_buf2));
   345     ret = jvmti->Deallocate((unsigned char *) stack_buf2);
   375     if (ret != JVMTI_ERROR_NONE) {
   346     if (ret != JVMTI_ERROR_NONE) {
   376         printf("Error: Deallocate stack_buf2 failed with  %d \n", ret);
   347         printf("Error: Deallocate stack_buf2 failed with  %d \n", ret);
   377         iGlobalStatus = STATUS_FAILED;
   348         iGlobalStatus = STATUS_FAILED;
   378     }
   349     }
   379 
   350 
   380     ret = JVMTI_ENV_PTR(jvmti)->Deallocate(JVMTI_ENV_ARG(jvmti, (unsigned char *)thread_info));
   351     ret = jvmti->Deallocate((unsigned char *) thread_info);
   381     if (ret != JVMTI_ERROR_NONE) {
   352     if (ret != JVMTI_ERROR_NONE) {
   382         printf("Error: Deallocate thread_info failed with  %d \n", ret);
   353         printf("Error: Deallocate thread_info failed with  %d \n", ret);
   383         iGlobalStatus = STATUS_FAILED;
   354         iGlobalStatus = STATUS_FAILED;
   384     }
   355     }
   385     ret = JVMTI_ENV_PTR(jvmti)->Deallocate(JVMTI_ENV_ARG(jvmti, (unsigned char *)thread_list));
   356     ret = jvmti->Deallocate((unsigned char *) thread_list);
   386     if (ret != JVMTI_ERROR_NONE) {
   357     if (ret != JVMTI_ERROR_NONE) {
   387         printf("Error: Deallocate thread_list failed with  %d \n", ret);
   358         printf("Error: Deallocate thread_list failed with  %d \n", ret);
   388         iGlobalStatus = STATUS_FAILED;
   359         iGlobalStatus = STATUS_FAILED;
   389     }
   360     }
   390 }
   361 }
   391 
   362 
   392 #ifdef __cplusplus
   363 }
   393 }
       
   394 #endif