1 /*
2  * Copyright (c) 2007, 2019, 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 <jni.h>
24 #include <jvmti.h>
25 #include "agent_common.h"
26 #include <string.h>
27 #include "jvmti_tools.h"
28 #include "JVMTITools.h"
29 #include "jni_tools.h"
30 
31 extern "C" {
32 
33 #define DIR_NAME "newclass"
34 #define PATH_FORMAT "%s%02d/%s"
35 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS104/hs104t002/MyThread"
36 #define SEARCH_NAME "nsk/jvmti/scenarios/hotswap/HS104/hs104t002/MyThread"
37 
38 static jvmtiEnv * jvmti;
39 
40 #ifdef STATIC_BUILD
Agent_OnLoad_hs104t002(JavaVM * jvm,char * options,void * reserved)41 JNIEXPORT jint JNICALL Agent_OnLoad_hs104t002(JavaVM *jvm, char *options, void *reserved) {
42     return Agent_Initialize(jvm, options, reserved);
43 }
Agent_OnAttach_hs104t002(JavaVM * jvm,char * options,void * reserved)44 JNIEXPORT jint JNICALL Agent_OnAttach_hs104t002(JavaVM *jvm, char *options, void *reserved) {
45     return Agent_Initialize(jvm, options, reserved);
46 }
JNI_OnLoad_hs104t002(JavaVM * jvm,char * options,void * reserved)47 JNIEXPORT jint JNI_OnLoad_hs104t002(JavaVM *jvm, char *options, void *reserved) {
48     return JNI_VERSION_1_8;
49 }
50 #endif
Agent_Initialize(JavaVM * vm,char * options,void * reserved)51 jint  Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
52     if (!NSK_VERIFY (JNI_OK == vm->GetEnv((void **)&jvmti, JVMTI_VERSION_1_1))) {
53         nsk_printf("#error Agent :: Could not load JVMTI interface.\n");
54         return JNI_ERR;
55     } else {
56         jvmtiCapabilities caps;
57         if (!nsk_jvmti_parseOptions(options)) {
58             nsk_printf("# error agent Failed to parse options \n");
59             return JNI_ERR;
60         }
61         memset(&caps, 0, sizeof(caps));
62         caps.can_redefine_classes = 1;
63         if (!NSK_JVMTI_VERIFY (jvmti->AddCapabilities(&caps))) {
64             nsk_printf("#error Agent :: occured while adding capabilities.\n");
65             return JNI_ERR;
66         }
67     }
68     return JNI_OK;
69 }
70 
71 JNIEXPORT void JNICALL
Java_nsk_jvmti_scenarios_hotswap_HS104_hs104t002_hs104t002_redefineClasses(JNIEnv * jni,jclass classObject)72 Java_nsk_jvmti_scenarios_hotswap_HS104_hs104t002_hs104t002_redefineClasses(
73     JNIEnv *jni, jclass classObject) {
74     jclass cla;
75     char fileName[512];
76 
77     if (!NSK_JNI_VERIFY(jni, (cla = jni->FindClass(SEARCH_NAME)) != NULL)) {
78         nsk_printf(" Agent :: Failed to get class.\n");
79         nsk_jvmti_agentFailed();
80         return;
81     }
82     nsk_jvmti_getFileName(0, FILE_NAME, fileName,
83             sizeof(fileName)/sizeof(char));
84     if (nsk_jvmti_redefineClass(jvmti, cla, fileName)) {
85         nsk_printf(" Agent :: Redefine successfull.\n");
86     } else {
87         nsk_printf("# error Agent :: Redefine failed.\n");
88         nsk_jvmti_agentFailed();
89     }
90 }
91 
92 }
93