|
1 /* |
|
2 * Copyright (c) 2004, 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 |
|
24 #include <stdlib.h> |
|
25 #include <string.h> |
|
26 #include "jni_tools.h" |
|
27 #include "agent_common.h" |
|
28 #include "jvmti_tools.h" |
|
29 |
|
30 #define PASSED 0 |
|
31 #define STATUS_FAILED 2 |
|
32 |
|
33 #ifdef __cplusplus |
|
34 extern "C" { |
|
35 #endif |
|
36 |
|
37 /* ========================================================================== */ |
|
38 |
|
39 /* scaffold objects */ |
|
40 static jlong timeout = 0; |
|
41 |
|
42 /* event counts */ |
|
43 static int ExceptionEventsCount = 0; |
|
44 static int ExceptionCatchEventsCount = 0; |
|
45 |
|
46 /* ========================================================================== */ |
|
47 |
|
48 /** callback functions **/ |
|
49 |
|
50 static void JNICALL |
|
51 Exception(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread, |
|
52 jmethodID method, jlocation location, jobject exception, |
|
53 jmethodID catch_method, jlocation catch_location) { |
|
54 jclass klass = NULL; |
|
55 char *signature = NULL; |
|
56 |
|
57 ExceptionEventsCount++; |
|
58 |
|
59 if (!NSK_JNI_VERIFY(jni_env, (klass = |
|
60 NSK_CPP_STUB2(GetObjectClass, jni_env, exception)) != NULL)) { |
|
61 nsk_jvmti_setFailStatus(); |
|
62 return; |
|
63 } |
|
64 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature, jvmti_env, |
|
65 klass, &signature, NULL))) { |
|
66 nsk_jvmti_setFailStatus(); |
|
67 return; |
|
68 } |
|
69 NSK_DISPLAY1("Exception event: %s\n", signature); |
|
70 if (signature != NULL) |
|
71 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature); |
|
72 } |
|
73 |
|
74 void JNICALL |
|
75 ExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread, |
|
76 jmethodID method, jlocation location, jobject exception) { |
|
77 jclass klass = NULL; |
|
78 char *signature = NULL; |
|
79 |
|
80 ExceptionCatchEventsCount++; |
|
81 |
|
82 if (!NSK_JNI_VERIFY(jni_env, (klass = |
|
83 NSK_CPP_STUB2(GetObjectClass, jni_env, exception)) != NULL)) { |
|
84 nsk_jvmti_setFailStatus(); |
|
85 return; |
|
86 } |
|
87 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature, jvmti_env, |
|
88 klass, &signature, NULL))) { |
|
89 nsk_jvmti_setFailStatus(); |
|
90 return; |
|
91 } |
|
92 NSK_DISPLAY1("ExceptionCatch event: %s\n", signature); |
|
93 if (signature != NULL) |
|
94 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature); |
|
95 } |
|
96 |
|
97 /* ========================================================================== */ |
|
98 |
|
99 /** Agent algorithm. */ |
|
100 static void JNICALL |
|
101 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { |
|
102 |
|
103 if (!nsk_jvmti_waitForSync(timeout)) |
|
104 return; |
|
105 |
|
106 /* resume debugee and wait for sync */ |
|
107 if (!nsk_jvmti_resumeSync()) |
|
108 return; |
|
109 if (!nsk_jvmti_waitForSync(timeout)) |
|
110 return; |
|
111 |
|
112 NSK_DISPLAY1("Exception events received: %d\n", |
|
113 ExceptionEventsCount); |
|
114 if (!NSK_VERIFY(ExceptionEventsCount == 0)) |
|
115 nsk_jvmti_setFailStatus(); |
|
116 |
|
117 NSK_DISPLAY1("ExceptionCatch events received: %d\n", |
|
118 ExceptionCatchEventsCount); |
|
119 if (!NSK_VERIFY(ExceptionCatchEventsCount == 0)) |
|
120 nsk_jvmti_setFailStatus(); |
|
121 |
|
122 if (!nsk_jvmti_resumeSync()) |
|
123 return; |
|
124 } |
|
125 |
|
126 /* ========================================================================== */ |
|
127 |
|
128 /** Agent library initialization. */ |
|
129 #ifdef STATIC_BUILD |
|
130 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t001a(JavaVM *jvm, char *options, void *reserved) { |
|
131 return Agent_Initialize(jvm, options, reserved); |
|
132 } |
|
133 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t001a(JavaVM *jvm, char *options, void *reserved) { |
|
134 return Agent_Initialize(jvm, options, reserved); |
|
135 } |
|
136 JNIEXPORT jint JNI_OnLoad_ma10t001a(JavaVM *jvm, char *options, void *reserved) { |
|
137 return JNI_VERSION_1_8; |
|
138 } |
|
139 #endif |
|
140 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { |
|
141 jvmtiEnv* jvmti = NULL; |
|
142 jvmtiCapabilities caps; |
|
143 jvmtiEventCallbacks callbacks; |
|
144 |
|
145 NSK_DISPLAY0("Agent_OnLoad\n"); |
|
146 |
|
147 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) |
|
148 return JNI_ERR; |
|
149 |
|
150 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; |
|
151 |
|
152 if (!NSK_VERIFY((jvmti = |
|
153 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) |
|
154 return JNI_ERR; |
|
155 |
|
156 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) |
|
157 return JNI_ERR; |
|
158 |
|
159 memset(&caps, 0, sizeof(caps)); |
|
160 caps.can_generate_exception_events = 1; |
|
161 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) { |
|
162 return JNI_ERR; |
|
163 } |
|
164 |
|
165 memset(&callbacks, 0, sizeof(callbacks)); |
|
166 callbacks.Exception = &Exception; |
|
167 callbacks.ExceptionCatch = &ExceptionCatch; |
|
168 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks))) |
|
169 return JNI_ERR; |
|
170 |
|
171 return JNI_OK; |
|
172 } |
|
173 |
|
174 /* ========================================================================== */ |
|
175 |
|
176 #ifdef __cplusplus |
|
177 } |
|
178 #endif |