1 /* 2 * Copyright (c) 2007, 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 <string.h> 25 #include <jvmti.h> 26 #include "agent_common.h" 27 28 #include "nsk_tools.h" 29 #include "JVMTITools.h" 30 #include "jvmti_tools.h" 31 #include "jni_tools.h" 32 33 extern "C" { 34 35 /* ============================================================================= */ 36 37 #define FOO 1 38 39 /* ============================================================================= */ 40 41 static char *prefix = NULL; 42 43 /* ============================================================================= */ 44 45 static jvmtiEnv *jvmti = NULL; 46 47 /* ============================================================================= */ 48 49 JNIEXPORT int JNICALL 50 Java_nsk_jvmti_SetNativeMethodPrefix_SetNativeMethodPrefix002_foo ( 51 JNIEnv *jni 52 , jclass klass 53 ) 54 { 55 NSK_DISPLAY1(" >>> SetNativeMethodPrefix002.foo() (Library: SetNativeMethodPrefix002).\n", prefix); 56 return FOO; 57 } 58 59 /* ============================================================================= */ 60 61 static jboolean setMethodPrefix (char *prefix) 62 { 63 if (!NSK_JVMTI_VERIFY(jvmti->SetNativeMethodPrefix(prefix))) 64 return JNI_FALSE; 65 66 return JNI_TRUE; 67 } 68 69 /* ============================================================================= */ 70 71 /* Agent initialization procedure */ 72 #ifdef STATIC_BUILD 73 JNIEXPORT jint JNICALL Agent_OnLoad_SetNativeMethodPrefix002(JavaVM *jvm, char *options, void *reserved) { 74 return Agent_Initialize(jvm, options, reserved); 75 } 76 JNIEXPORT jint JNICALL Agent_OnAttach_SetNativeMethodPrefix002(JavaVM *jvm, char *options, void *reserved) { 77 return Agent_Initialize(jvm, options, reserved); 78 } 79 JNIEXPORT jint JNI_OnLoad_SetNativeMethodPrefix002(JavaVM *jvm, char *options, void *reserved) { 80 return JNI_VERSION_1_8; 81 } 82 #endif 83 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) 84 { 85 jvmtiCapabilities caps; 86 int apply; 87 88 if (!NSK_VERIFY( 89 nsk_jvmti_parseOptions(options) 90 ) 91 ) 92 return JNI_ERR; 93 94 // Parse additional parameters 95 96 // Specify native method prefix 97 prefix = (char *)nsk_jvmti_findOptionValue("prefix"); 98 if (prefix != NULL) { 99 NSK_DISPLAY1("Prefix: %s\n", prefix); 100 } 101 102 // Specify whether prefix should be applied or not 103 apply = nsk_jvmti_findOptionIntValue("apply", 1); 104 105 if (!NSK_VERIFY( 106 (jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL 107 ) 108 ) 109 return JNI_ERR; 110 111 if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps))) 112 return JNI_ERR; 113 114 // Register all necessary JVM capabilities 115 caps.can_set_native_method_prefix = 1; 116 117 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) 118 return JNI_ERR; 119 120 121 if (apply) { 122 if (!setMethodPrefix(prefix)) { 123 NSK_COMPLAIN0("Can't specify prefix for native method lookup."); 124 return JNI_ERR; 125 } 126 } 127 128 return JNI_OK; 129 } 130 131 }