|
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 <stdlib.h> |
|
25 #include <string.h> |
|
26 #include <jni.h> |
|
27 #include <jvmti.h> |
|
28 #include <aod.h> |
|
29 #include <jvmti_aod.h> |
|
30 |
|
31 #ifdef __cplusplus |
|
32 extern "C" { |
|
33 #endif |
|
34 |
|
35 /* |
|
36 * Expected agent work scenario: |
|
37 * - during initialization agent enables ThreadStart events |
|
38 * - target application starts thread |
|
39 * - agent receives ThreadStart event and tries to find thread provoked this event |
|
40 * in all VM thread groups and finishes work |
|
41 */ |
|
42 |
|
43 #define STARTED_TEST_THREAD_NAME "attach041-TestThread" |
|
44 |
|
45 static Options* options = NULL; |
|
46 static const char* agentName; |
|
47 |
|
48 int tryFindThread(jvmtiEnv *jvmti, jthreadGroup group, const char* threadNameToFind) { |
|
49 jint threadsCount = 0; |
|
50 jthread *threads = NULL; |
|
51 jint groupsCount = 0; |
|
52 jthreadGroup* groups = NULL; |
|
53 jvmtiThreadGroupInfo groupInfo; |
|
54 int i; |
|
55 char threadGroupName[MAX_STRING_LENGTH]; |
|
56 |
|
57 if(!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetThreadGroupInfo, jvmti, group, &groupInfo))) { |
|
58 return 0; |
|
59 } |
|
60 |
|
61 strcpy(threadGroupName, groupInfo.name); |
|
62 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groupInfo.name); |
|
63 |
|
64 NSK_DISPLAY3("%s: trying to find thread '%s' in group '%s'\n", agentName, threadNameToFind, threadGroupName); |
|
65 |
|
66 if(!NSK_JVMTI_VERIFY(NSK_CPP_STUB6(GetThreadGroupChildren, jvmti, group, &threadsCount, &threads, &groupsCount, &groups))) { |
|
67 return 0; |
|
68 } |
|
69 |
|
70 for (i = 0; i < threadsCount; i++) { |
|
71 char threadName[MAX_STRING_LENGTH]; |
|
72 if (!nsk_jvmti_aod_getThreadName(jvmti, threads[i], threadName)) { |
|
73 NSK_COMPLAIN1("%s: failed to get thread name\n", agentName); |
|
74 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads); |
|
75 return 0; |
|
76 } |
|
77 if (!strcmp(threadName, threadNameToFind)) { |
|
78 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads); |
|
79 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups); |
|
80 NSK_DISPLAY3("%s: thread '%s' was found in group '%s'\n", agentName, threadNameToFind, threadGroupName); |
|
81 return 1; |
|
82 } |
|
83 } |
|
84 |
|
85 // threads array isn't needed more |
|
86 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads); |
|
87 |
|
88 NSK_DISPLAY3("%s: thread '%s' wasn't found in group '%s'\n", agentName, threadNameToFind, threadGroupName); |
|
89 |
|
90 if (groupsCount != 0) { |
|
91 for (i = 0; i < groupsCount; i++) { |
|
92 if (tryFindThread(jvmti, groups[i], threadNameToFind)) { |
|
93 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups); |
|
94 return 1; |
|
95 } |
|
96 } |
|
97 } |
|
98 |
|
99 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups); |
|
100 |
|
101 return 0; |
|
102 } |
|
103 |
|
104 void JNICALL threadStartHandler(jvmtiEnv *jvmti, |
|
105 JNIEnv* jni, |
|
106 jthread thread) { |
|
107 char startedThreadName[MAX_STRING_LENGTH]; |
|
108 |
|
109 if (!nsk_jvmti_aod_getThreadName(jvmti, thread, startedThreadName)) { |
|
110 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni); |
|
111 return; |
|
112 } |
|
113 |
|
114 NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, startedThreadName); |
|
115 |
|
116 if (!strcmp(startedThreadName, STARTED_TEST_THREAD_NAME)) { |
|
117 int success = 1; |
|
118 int threadWasFound = 0; |
|
119 jint groupsCount = 0; |
|
120 jthreadGroup *topGroups; |
|
121 int i; |
|
122 |
|
123 if(!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTopThreadGroups, jvmti, &groupsCount, &topGroups))) { |
|
124 NSK_COMPLAIN1("%s: failed to get top thread groups\n", agentName); |
|
125 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni); |
|
126 return; |
|
127 } |
|
128 |
|
129 for (i = 0; i < groupsCount; i++) { |
|
130 if (tryFindThread(jvmti, topGroups[i], startedThreadName)) { |
|
131 threadWasFound = 1; |
|
132 break; |
|
133 } |
|
134 } |
|
135 |
|
136 nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)topGroups); |
|
137 |
|
138 if (!threadWasFound) { |
|
139 success = 0; |
|
140 NSK_COMPLAIN2("%s: failed to find thread '%s'\n", agentName, startedThreadName); |
|
141 } |
|
142 |
|
143 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni); |
|
144 } |
|
145 } |
|
146 |
|
147 #ifdef STATIC_BUILD |
|
148 JNIEXPORT jint JNI_OnLoad_attach041Agent00(JavaVM *jvm, char *options, void *reserved) { |
|
149 return JNI_VERSION_1_8; |
|
150 } |
|
151 #endif |
|
152 |
|
153 JNIEXPORT jint JNICALL |
|
154 #ifdef STATIC_BUILD |
|
155 Agent_OnAttach_attach041Agent00(JavaVM *vm, char *optionsString, void *reserved) |
|
156 #else |
|
157 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) |
|
158 #endif |
|
159 { |
|
160 jvmtiEventCallbacks eventCallbacks; |
|
161 jvmtiEnv* jvmti; |
|
162 JNIEnv* jni; |
|
163 |
|
164 if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL)) |
|
165 return JNI_ERR; |
|
166 |
|
167 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); |
|
168 |
|
169 if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL) |
|
170 return JNI_ERR; |
|
171 |
|
172 if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL)) |
|
173 return JNI_ERR; |
|
174 |
|
175 memset(&eventCallbacks,0, sizeof(eventCallbacks)); |
|
176 eventCallbacks.ThreadStart = threadStartHandler; |
|
177 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &eventCallbacks, sizeof(eventCallbacks))) ) { |
|
178 return JNI_ERR; |
|
179 } |
|
180 |
|
181 if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_THREAD_START))) { |
|
182 return JNI_ERR; |
|
183 } |
|
184 |
|
185 NSK_DISPLAY1("%s: initialization was done\n", agentName); |
|
186 |
|
187 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) |
|
188 return JNI_ERR; |
|
189 |
|
190 return JNI_OK; |
|
191 } |
|
192 |
|
193 #ifdef __cplusplus |
|
194 } |
|
195 #endif |