1 /*
   2  * Copyright (c) 2013, 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 #include <wchar.h>
  24 #include <string.h>
  25 #include <stdlib.h>
  26 
  27 #include "jvmti.h"
  28 #include "jni_tools.h"
  29 #include "jvmti_tools.h"
  30 #include "agent_common.h"
  31 
  32 extern "C" {
  33 
  34 static int timeout = 0;
  35 
  36 jint JNICALL field_callback(jvmtiHeapReferenceKind kind,
  37                             const jvmtiHeapReferenceInfo* info,
  38                             jlong object_class_tag,
  39                             jlong* object_tag_ptr,
  40                             jvalue value,
  41                             jvmtiPrimitiveType value_type,
  42                             void* user_data) {
  43   (*(int*)user_data)++;
  44   return JVMTI_VISIT_ABORT;
  45 }
  46 
  47 jint JNICALL string_callback(jlong class_tag,
  48                              jlong size,
  49                              jlong* tag_ptr,
  50                              const jchar* value,
  51                              jint value_length,
  52                              void* user_data) {
  53   (*(int*)user_data)++;
  54   return JVMTI_VISIT_ABORT;
  55 }
  56 
  57 jint JNICALL array_callback(jlong class_tag,
  58                             jlong size,
  59                             jlong* tag_ptr,
  60                             jint element_count,
  61                             jvmtiPrimitiveType element_type,
  62                             const void* elements,
  63                             void* user_data) {
  64   (*(int*)user_data)++;
  65   return JVMTI_VISIT_ABORT;
  66 }
  67 
  68 jint JNICALL heap_callback(jlong class_tag,
  69                            jlong size,
  70                            jlong* tag_ptr,
  71                            jint length,
  72                            void* user_data) {
  73   (*(int*)user_data)++;
  74   return JVMTI_VISIT_ABORT;
  75 }
  76 
  77 static void JNICALL
  78 agent(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  79   jvmtiEvent event = JVMTI_EVENT_OBJECT_FREE;
  80   jvmtiHeapCallbacks primitive_callbacks;
  81   int invocations = 0;
  82 
  83   NSK_DISPLAY0("Waiting debugee.\n");
  84   if(!NSK_VERIFY(nsk_jvmti_enableEvents(JVMTI_ENABLE, 1, &event, NULL))) {
  85     return;
  86   }
  87   if(!NSK_VERIFY(nsk_jvmti_waitForSync(timeout))) {
  88     return;
  89   }
  90 
  91   memset(&primitive_callbacks, 0, sizeof(jvmtiHeapCallbacks));
  92   primitive_callbacks.primitive_field_callback = &field_callback;
  93   primitive_callbacks.array_primitive_value_callback = &array_callback;
  94   primitive_callbacks.string_primitive_value_callback = &string_callback;
  95   primitive_callbacks.heap_iteration_callback = &heap_callback;
  96 
  97   NSK_DISPLAY0("Iterating over reachable objects.\n");
  98   if(!NSK_JVMTI_VERIFY(jvmti->IterateThroughHeap(0, NULL, &primitive_callbacks, &invocations))) {
  99     nsk_jvmti_setFailStatus();
 100     return;
 101   }
 102 
 103   if(invocations != 1) {
 104     NSK_COMPLAIN1("Primitive callbacks were invoked more than once: "
 105                   "%d invocations registered.\n",invocations);
 106     nsk_jvmti_setFailStatus();
 107   }
 108 
 109   if(!NSK_VERIFY(nsk_jvmti_resumeSync()))
 110     return;
 111 }
 112 
 113 #ifdef STATIC_BUILD
 114 JNIEXPORT jint JNICALL Agent_OnLoad_Abort(JavaVM *jvm, char *options, void *reserved) {
 115     return Agent_Initialize(jvm, options, reserved);
 116 }
 117 JNIEXPORT jint JNICALL Agent_OnAttach_Abort(JavaVM *jvm, char *options, void *reserved) {
 118     return Agent_Initialize(jvm, options, reserved);
 119 }
 120 JNIEXPORT jint JNI_OnLoad_Abort(JavaVM *jvm, char *options, void *reserved) {
 121     return JNI_VERSION_1_8;
 122 }
 123 #endif
 124 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 125   jvmtiEnv *jvmti;
 126   jvmtiCapabilities caps;
 127   jvmtiEventCallbacks event_callbacks;
 128 
 129   if(!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) {
 130     return JNI_ERR;
 131   }
 132 
 133   nsk_jvmti_parseOptions(options);
 134 
 135   timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 136 
 137   memset(&caps, 0, sizeof(caps));
 138   caps.can_tag_objects = 1;
 139   caps.can_generate_object_free_events = 1;
 140 
 141   if(!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 142     return JNI_ERR;
 143   }
 144 
 145   memset(&event_callbacks, 0, sizeof(jvmtiEventCallbacks));
 146   if(!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&event_callbacks, sizeof(jvmtiEventCallbacks)))) {
 147     return JNI_ERR;
 148   }
 149 
 150   if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agent, NULL))) {
 151     return JNI_ERR;
 152   }
 153 
 154   return JNI_OK;
 155 }
 156 
 157 }