1 /* 2 * Copyright (c) 2008, 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 <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <errno.h> 27 #include "jvmti_tools.h" 28 #include "agent_common.h" 29 30 #define PASSED 0 31 #define STATUS_FAILED 2 32 33 extern "C" { 34 35 static jvmtiEnv* gJvmti = NULL; 36 static volatile jboolean gGotEvent = JNI_FALSE; 37 38 void JNICALL 39 resourceExhausted(jvmtiEnv *jvmti_env, 40 JNIEnv* jni_env, 41 jint flags, 42 const void* reserved, 43 const char* description) 44 { 45 NSK_DISPLAY1("Agent: ResourceExhausted detected: %s\n", description); 46 if (flags & JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR) NSK_DISPLAY0("Agent: JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR\n"); 47 if (flags & JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP) NSK_DISPLAY0("Agent: JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP\n"); 48 if (flags & JVMTI_RESOURCE_EXHAUSTED_THREADS) NSK_DISPLAY0("Agent: JVMTI_RESOURCE_EXHAUSTED_THREADS\n"); 49 gGotEvent = JNI_TRUE; 50 } 51 52 JNIEXPORT jboolean JNICALL 53 Java_nsk_jvmti_ResourceExhausted_Helper_gotExhaustedEvent(JNIEnv* env, jclass cls) 54 { 55 return gGotEvent; 56 } 57 58 JNIEXPORT void JNICALL 59 Java_nsk_jvmti_ResourceExhausted_Helper_resetExhaustedEvent(JNIEnv* env, jclass cls) 60 { 61 gGotEvent = JNI_FALSE; 62 } 63 64 #ifdef STATIC_BUILD 65 JNIEXPORT jint JNICALL Agent_OnLoad_resexhausted(JavaVM *jvm, char *options, void *reserved) { 66 return Agent_Initialize(jvm, options, reserved); 67 } 68 JNIEXPORT jint JNICALL Agent_OnAttach_resexhausted(JavaVM *jvm, char *options, void *reserved) { 69 return Agent_Initialize(jvm, options, reserved); 70 } 71 JNIEXPORT jint JNI_OnLoad_resexhausted(JavaVM *jvm, char *options, void *reserved) { 72 return JNI_VERSION_1_8; 73 } 74 #endif 75 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) 76 { 77 jvmtiEventCallbacks callbacks; 78 jvmtiCapabilities capabilities; 79 80 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 81 return JNI_ERR; 82 83 if (!NSK_VERIFY((gJvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL)) 84 return JNI_ERR; 85 86 memset(&capabilities, 0, sizeof(jvmtiCapabilities)); 87 capabilities.can_generate_resource_exhaustion_heap_events = 1; 88 capabilities.can_generate_resource_exhaustion_threads_events = 1; 89 if (!NSK_JVMTI_VERIFY(gJvmti->AddCapabilities(&capabilities))) 90 return JNI_ERR; 91 92 memset((void *)&callbacks, 0, sizeof(jvmtiEventCallbacks)); 93 callbacks.ResourceExhausted = resourceExhausted; 94 if (!NSK_JVMTI_VERIFY(gJvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)))) 95 return JNI_ERR; 96 97 if (!NSK_JVMTI_VERIFY(gJvmti->SetEventNotificationMode(JVMTI_ENABLE, 98 JVMTI_EVENT_RESOURCE_EXHAUSTED, 99 NULL))) 100 return JNI_ERR; 101 102 return JNI_OK; 103 } 104 105 }