1 /*
2 * Copyright (c) 2024, 2026, 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
24 #include <stdio.h>
25 #include <string.h>
26 #include "jvmti.h"
27 #include "jni.h"
28 #include "jvmti_common.hpp"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 static jvmtiEnv *jvmti = nullptr;
35
36 JNIEXPORT jint JNICALL
37 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
38 jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
39 if (res != JNI_OK || jvmti == nullptr) {
40 LOG("GetEnv failed, res = %d", (int)res);
41 return JNI_ERR;
42 }
43
44 jvmtiCapabilities caps;
45 memset(&caps, 0, sizeof(caps));
46 caps.can_access_local_variables = 1;
47 jvmtiError err = jvmti->AddCapabilities(&caps);
48 if (err != JVMTI_ERROR_NONE) {
49 LOG("AddCapabilities failed: %s (%d)\n", TranslateError(err), err);
50 return JNI_ERR;
51 }
52
53 return JNI_OK;
54 }
55
56 JNIEXPORT void JNICALL
57 Java_ValueGetSetLocal_testLocals(JNIEnv *jni, jclass thisClass, jthread thread, jboolean testSetLocal) {
58 const jint depth = 1;
59
60 LOG("\ntestLocals\n");
61 jobject obj0 = get_local_object(jvmti, jni, thread, depth, 0);
62 jobject obj1 = get_local_object(jvmti, jni, thread, depth, 1);
63 jobject obj2 = get_local_object(jvmti, jni, thread, depth, 2);
64 jobject obj3 = get_local_object(jvmti, jni, thread, depth, 3);
65 jobject obj_this = get_local_instance(jvmti, jni, thread, depth);
66
67 // obj0 is expected to be equal "this"
68 if (!jni->IsSameObject(obj0, obj_this)) {
69 fatal(jni, "Failed: obj0 != obj_this\n");
70 }
71 // obj3 is expected to be equal obj2
72 if (!jni->IsSameObject(obj3, obj2)) {
73 fatal(jni, "Failed: obj3 != obj2\n");
74 }
75
76 if (testSetLocal) {
77 // set obj3 = obj1
78 set_local_object(jvmti, thread, depth, 3, obj1);
79 obj3 = get_local_object(jvmti, jni, thread, depth, 3);
80 if (!jni->IsSameObject(obj3, obj1)) {
81 fatal(jni, "Failed: obj3 != obj1\n");
82 }
83 }
84 }
85
86 #ifdef __cplusplus
87 }
88 #endif
89