1 /*
   2  * Copyright (c) 2004, 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 <string.h>
  25 #include "jvmti.h"
  26 #include "agent_common.h"
  27 #include "jni_tools.h"
  28 #include "jvmti_tools.h"
  29 
  30 extern "C" {
  31 
  32 /* ============================================================================= */
  33 
  34 static jlong timeout = 0;
  35 static jvmtiEnv* st_jvmti = NULL;
  36 static const char *storage_data = "local_storage_data";
  37 static void *storage_ptr = NULL;
  38 static const char debugeeClassSignature[] = "Lnsk/jvmti/IterateOverInstancesOfClass/iterinstcls006;";
  39 
  40 /* ============================================================================= */
  41 
  42 jvmtiIterationControl JNICALL
  43 heapObjectCallback(jlong class_tag,
  44                    jlong size,
  45                    jlong* tag_ptr,
  46                    void* storage_data) {
  47 
  48     if (!NSK_JVMTI_VERIFY(st_jvmti->SetEnvironmentLocalStorage(storage_data))) {
  49         nsk_jvmti_setFailStatus();
  50     }
  51 
  52     if (!NSK_JVMTI_VERIFY(st_jvmti->GetEnvironmentLocalStorage(&storage_ptr))) {
  53         nsk_jvmti_setFailStatus();
  54     }
  55 
  56     /*  Iterate over only first object */
  57     return JVMTI_ITERATION_ABORT;
  58 }
  59 
  60 /* ============================================================================= */
  61 
  62 /** Agent algorithm. */
  63 static void JNICALL
  64 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  65 
  66     NSK_DISPLAY0("Wait for debugee start\n");
  67     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
  68         return;
  69 
  70     do {
  71         jclass debugeeClass = NULL;
  72 
  73         NSK_DISPLAY1("Find debugee class: %s\n", debugeeClassSignature);
  74         debugeeClass = nsk_jvmti_classBySignature(debugeeClassSignature);
  75         if (debugeeClass == NULL) {
  76             nsk_jvmti_setFailStatus();
  77             break;
  78         }
  79 
  80         NSK_DISPLAY0("Calling IterateOverInstancesOfClass with filter JVMTI_HEAP_OBJECT_EITHER\n");
  81         {
  82             if (!NSK_JVMTI_VERIFY(
  83                     jvmti->IterateOverInstancesOfClass(debugeeClass,
  84                                                        JVMTI_HEAP_OBJECT_EITHER,
  85                                                        heapObjectCallback,
  86                                                        (void *) storage_data))) {
  87                 nsk_jvmti_setFailStatus();
  88             }
  89         }
  90 
  91         if (storage_data != storage_ptr) {
  92             NSK_COMPLAIN2("Local storage address was corrupted: %p ,\n\texpected value: %p\n",
  93                              storage_ptr, storage_data);
  94             nsk_jvmti_setFailStatus();
  95         }
  96 
  97         if (strcmp(storage_data, (char *)storage_ptr) != 0) {
  98             NSK_COMPLAIN2("Local storage was corrupted: %s ,\n\texpected value: %s\n",
  99                              (char *)storage_ptr, storage_data);
 100             nsk_jvmti_setFailStatus();
 101         }
 102     } while (0);
 103 
 104     NSK_DISPLAY0("Let debugee to finish\n");
 105     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 106         return;
 107 }
 108 
 109 /* ============================================================================= */
 110 
 111 /** Agent library initialization. */
 112 #ifdef STATIC_BUILD
 113 JNIEXPORT jint JNICALL Agent_OnLoad_iterinstcls006(JavaVM *jvm, char *options, void *reserved) {
 114     return Agent_Initialize(jvm, options, reserved);
 115 }
 116 JNIEXPORT jint JNICALL Agent_OnAttach_iterinstcls006(JavaVM *jvm, char *options, void *reserved) {
 117     return Agent_Initialize(jvm, options, reserved);
 118 }
 119 JNIEXPORT jint JNI_OnLoad_iterinstcls006(JavaVM *jvm, char *options, void *reserved) {
 120     return JNI_VERSION_1_8;
 121 }
 122 #endif
 123 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 124     jvmtiEnv* jvmti = NULL;
 125 
 126     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 127         return JNI_ERR;
 128 
 129     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 130 
 131     if (!NSK_VERIFY((jvmti =
 132             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 133         return JNI_ERR;
 134 
 135     /* save pointer to environment to use it in callbacks */
 136     st_jvmti = jvmti;
 137 
 138     {
 139         jvmtiCapabilities caps;
 140 
 141         memset(&caps, 0, sizeof(caps));
 142         caps.can_tag_objects = 1;
 143         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 144             return JNI_ERR;
 145         }
 146     }
 147 
 148     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 149         return JNI_ERR;
 150 
 151     return JNI_OK;
 152 }
 153 
 154 /* ============================================================================= */
 155 
 156 }