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(
  64                 NSK_CPP_STUB2(
  65                     SetNativeMethodPrefix
  66                     , jvmti
  67                     , prefix)
  68                 )
  69        )
  70         return JNI_FALSE;
  71 
  72     return JNI_TRUE;
  73 }
  74 
  75 /* ============================================================================= */
  76 
  77 /* Agent initialization procedure */
  78 #ifdef STATIC_BUILD
  79 JNIEXPORT jint JNICALL Agent_OnLoad_SetNativeMethodPrefix002(JavaVM *jvm, char *options, void *reserved) {
  80     return Agent_Initialize(jvm, options, reserved);
  81 }
  82 JNIEXPORT jint JNICALL Agent_OnAttach_SetNativeMethodPrefix002(JavaVM *jvm, char *options, void *reserved) {
  83     return Agent_Initialize(jvm, options, reserved);
  84 }
  85 JNIEXPORT jint JNI_OnLoad_SetNativeMethodPrefix002(JavaVM *jvm, char *options, void *reserved) {
  86     return JNI_VERSION_1_8;
  87 }
  88 #endif
  89 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved)
  90 {
  91     jvmtiCapabilities caps;
  92     int apply;
  93 
  94     if (!NSK_VERIFY(
  95                 nsk_jvmti_parseOptions(options)
  96                 )
  97        )
  98         return JNI_ERR;
  99 
 100     // Parse additional parameters
 101 
 102     // Specify native method prefix
 103     prefix = (char *)nsk_jvmti_findOptionValue("prefix");
 104     if (prefix != NULL) {
 105         NSK_DISPLAY1("Prefix: %s\n", prefix);
 106     }
 107 
 108     // Specify whether prefix should be applied or not
 109     apply = nsk_jvmti_findOptionIntValue("apply", 1);
 110 
 111     if (!NSK_VERIFY(
 112                 (jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL
 113                 )
 114        )
 115         return JNI_ERR;
 116 
 117     if (!NSK_JVMTI_VERIFY(
 118                 NSK_CPP_STUB2(
 119                     GetCapabilities
 120                     , jvmti
 121                     , &caps)
 122                 )
 123        )
 124         return JNI_ERR;
 125 
 126     // Register all necessary JVM capabilities
 127     caps.can_set_native_method_prefix = 1;
 128 
 129     if (!NSK_JVMTI_VERIFY(
 130                 NSK_CPP_STUB2(
 131                     AddCapabilities
 132                     , jvmti
 133                     , &caps)
 134                 )
 135        )
 136         return JNI_ERR;
 137 
 138 
 139     if (apply) {
 140         if (!setMethodPrefix(prefix)) {
 141             NSK_COMPLAIN0("Can't specify prefix for native method lookup.");
 142             return JNI_ERR;
 143         }
 144     }
 145 
 146     return JNI_OK;
 147 }
 148 
 149 }