|
1 /* |
|
2 * Copyright (c) 2019 SAP SE. 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 |
|
29 #ifdef __cplusplus |
|
30 extern "C" { |
|
31 #endif |
|
32 |
|
33 #ifndef JNI_ENV_ARG |
|
34 |
|
35 #ifdef __cplusplus |
|
36 #define JNI_ENV_ARG(x, y) y |
|
37 #define JNI_ENV_PTR(x) x |
|
38 #else |
|
39 #define JNI_ENV_ARG(x,y) x, y |
|
40 #define JNI_ENV_PTR(x) (*x) |
|
41 #endif |
|
42 |
|
43 #endif |
|
44 |
|
45 #define FAILED -1 |
|
46 |
|
47 static jvmtiEnv *jvmti; |
|
48 |
|
49 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved); |
|
50 |
|
51 static void ShowErrorMessage(jvmtiEnv *jvmti, jvmtiError errCode, const char *message) { |
|
52 char *errMsg; |
|
53 jvmtiError result; |
|
54 |
|
55 result = (*jvmti)->GetErrorName(jvmti, errCode, &errMsg); |
|
56 if (result == JVMTI_ERROR_NONE) { |
|
57 fprintf(stderr, "%s: %s (%d)\n", message, errMsg, errCode); |
|
58 (*jvmti)->Deallocate(jvmti, (unsigned char *)errMsg); |
|
59 } else { |
|
60 fprintf(stderr, "%s (%d)\n", message, errCode); |
|
61 } |
|
62 } |
|
63 |
|
64 JNIEXPORT jint JNICALL |
|
65 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { |
|
66 return Agent_Initialize(jvm, options, reserved); |
|
67 } |
|
68 |
|
69 JNIEXPORT jint JNICALL |
|
70 Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) { |
|
71 return Agent_Initialize(jvm, options, reserved); |
|
72 } |
|
73 |
|
74 JNIEXPORT jint JNICALL |
|
75 JNI_OnLoad(JavaVM *jvm, void *reserved) { |
|
76 jint res; |
|
77 JNIEnv *env; |
|
78 |
|
79 res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &env), |
|
80 JNI_VERSION_9); |
|
81 if (res != JNI_OK || env == NULL) { |
|
82 fprintf(stderr, "Error: GetEnv call failed(%d)!\n", res); |
|
83 return JNI_ERR; |
|
84 } |
|
85 |
|
86 return JNI_VERSION_9; |
|
87 } |
|
88 |
|
89 static |
|
90 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { |
|
91 jint res; |
|
92 jvmtiError err; |
|
93 jvmtiCapabilities caps; |
|
94 |
|
95 printf("Agent_OnLoad started\n"); |
|
96 |
|
97 memset(&caps, 0, sizeof(caps)); |
|
98 |
|
99 res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), |
|
100 JVMTI_VERSION_9); |
|
101 if (res != JNI_OK || jvmti == NULL) { |
|
102 fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n"); |
|
103 return JNI_ERR; |
|
104 } |
|
105 |
|
106 caps.can_get_owned_monitor_info = 1; |
|
107 |
|
108 err = (*jvmti)->AddCapabilities(jvmti, &caps); |
|
109 if (err != JVMTI_ERROR_NONE) { |
|
110 ShowErrorMessage(jvmti, err, |
|
111 "Agent_OnLoad: error in JVMTI AddCapabilities"); |
|
112 return JNI_ERR; |
|
113 } |
|
114 |
|
115 err = (*jvmti)->GetCapabilities(jvmti, &caps); |
|
116 if (err != JVMTI_ERROR_NONE) { |
|
117 ShowErrorMessage(jvmti, err, |
|
118 "Agent_OnLoad: error in JVMTI GetCapabilities"); |
|
119 return JNI_ERR; |
|
120 } |
|
121 |
|
122 if (!caps.can_get_owned_monitor_info) { |
|
123 fprintf(stderr, "Warning: GetOwnedMonitorInfo is not implemented\n"); |
|
124 return JNI_ERR; |
|
125 } |
|
126 |
|
127 printf("Agent_OnLoad finished\n"); |
|
128 return JNI_OK; |
|
129 } |
|
130 |
|
131 JNIEXPORT jint JNICALL |
|
132 Java_GetOwnedMonitorInfoWithEATest_getOwnedMonitorInfo(JNIEnv *env, jclass cls, jobject targetThread, jobjectArray resOwnedMonitors) { |
|
133 jvmtiError err; |
|
134 jvmtiThreadInfo threadInfo; |
|
135 jint monitorCount; |
|
136 jobject* monitors; |
|
137 jint idx; |
|
138 |
|
139 err = (*jvmti)->GetThreadInfo(jvmti, targetThread, &threadInfo); |
|
140 if (err != JVMTI_ERROR_NONE) { |
|
141 ShowErrorMessage(jvmti, err, |
|
142 "getOwnedMonitorsFor: error in JVMTI GetThreadInfo"); |
|
143 return FAILED; |
|
144 } |
|
145 |
|
146 err = (*jvmti)->GetOwnedMonitorInfo(jvmti, targetThread, &monitorCount, &monitors); |
|
147 if (err != JVMTI_ERROR_NONE) { |
|
148 ShowErrorMessage(jvmti, err, |
|
149 "getOwnedMonitorsFor: error in JVMTI GetOwnedMonitorInfo"); |
|
150 return FAILED; |
|
151 } |
|
152 |
|
153 printf("getOwnedMonitorsFor: %s owns %d monitor(s)\n", threadInfo.name, monitorCount); |
|
154 |
|
155 for (idx = 0; idx < monitorCount; idx++) { |
|
156 (*env)->SetObjectArrayElement(env, resOwnedMonitors, idx, monitors[idx]); |
|
157 } |
|
158 |
|
159 (*jvmti)->Deallocate(jvmti, (unsigned char *) monitors); |
|
160 (*jvmti)->Deallocate(jvmti, (unsigned char *) threadInfo.name); |
|
161 return monitorCount; |
|
162 } |
|
163 |
|
164 #ifdef __cplusplus |
|
165 } |
|
166 #endif |