author | mgronlun |
Mon, 25 Nov 2019 18:38:01 +0100 | |
changeset 59259 | 127ca611f19b |
parent 50113 | caf115bb98ad |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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 |
||
25 |
#include "precompiled.hpp" |
|
26 |
#include "jvm.h" |
|
27 |
#include "jfr/instrumentation/jfrJvmtiAgent.hpp" |
|
28 |
#include "jfr/jni/jfrJavaSupport.hpp" |
|
29 |
#include "jfr/jni/jfrUpcalls.hpp" |
|
30 |
#include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp" |
|
31 |
#include "jfr/recorder/service/jfrOptionSet.hpp" |
|
32 |
#include "jfr/support/jfrEventClass.hpp" |
|
33 |
#include "logging/log.hpp" |
|
34 |
#include "memory/resourceArea.hpp" |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
35 |
#include "prims/jvmtiEnvBase.hpp" |
50113 | 36 |
#include "prims/jvmtiExport.hpp" |
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
37 |
#include "prims/jvmtiUtil.hpp" |
50113 | 38 |
#include "runtime/interfaceSupport.inline.hpp" |
39 |
#include "runtime/thread.inline.hpp" |
|
40 |
#include "utilities/exceptions.hpp" |
|
41 |
||
42 |
static const size_t ERROR_MSG_BUFFER_SIZE = 256; |
|
43 |
static JfrJvmtiAgent* agent = NULL; |
|
44 |
static jvmtiEnv* jfr_jvmti_env = NULL; |
|
45 |
||
46 |
static void check_jvmti_error(jvmtiEnv* jvmti, jvmtiError errnum, const char* str) { |
|
47 |
if (errnum != JVMTI_ERROR_NONE) { |
|
48 |
char* errnum_str = NULL; |
|
49 |
jvmti->GetErrorName(errnum, &errnum_str); |
|
50 |
log_error(jfr, system)("ERROR: JfrJvmtiAgent: " INT32_FORMAT " (%s): %s\n", |
|
51 |
errnum, |
|
52 |
NULL == errnum_str ? "Unknown" : errnum_str, |
|
53 |
NULL == str ? "" : str); |
|
54 |
} |
|
55 |
} |
|
56 |
||
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
57 |
static bool set_event_notification_mode(jvmtiEventMode mode, |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
58 |
jvmtiEvent event, |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
59 |
jthread event_thread, |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
60 |
...) { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
61 |
assert(jfr_jvmti_env != NULL, "invariant"); |
50113 | 62 |
const jvmtiError jvmti_ret_code = jfr_jvmti_env->SetEventNotificationMode(mode, event, event_thread); |
63 |
check_jvmti_error(jfr_jvmti_env, jvmti_ret_code, "SetEventNotificationMode"); |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
64 |
return jvmti_ret_code == JVMTI_ERROR_NONE; |
50113 | 65 |
} |
66 |
||
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
67 |
static bool update_class_file_load_hook_event(jvmtiEventMode mode) { |
50113 | 68 |
return set_event_notification_mode(mode, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL); |
69 |
} |
|
70 |
||
71 |
static JavaThread* current_java_thread() { |
|
72 |
Thread* this_thread = Thread::current(); |
|
73 |
assert(this_thread != NULL && this_thread->is_Java_thread(), "invariant"); |
|
74 |
return static_cast<JavaThread*>(this_thread); |
|
75 |
} |
|
76 |
||
77 |
// jvmti event callbacks require C linkage |
|
78 |
extern "C" void JNICALL jfr_on_class_file_load_hook(jvmtiEnv *jvmti_env, |
|
79 |
JNIEnv* jni_env, |
|
80 |
jclass class_being_redefined, |
|
81 |
jobject loader, |
|
82 |
const char* name, |
|
83 |
jobject protection_domain, |
|
84 |
jint class_data_len, |
|
85 |
const unsigned char* class_data, |
|
86 |
jint* new_class_data_len, |
|
87 |
unsigned char** new_class_data) { |
|
88 |
if (class_being_redefined == NULL) { |
|
89 |
return; |
|
90 |
} |
|
91 |
JavaThread* jt = JavaThread::thread_from_jni_environment(jni_env); |
|
92 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(jt));; |
|
93 |
ThreadInVMfromNative tvmfn(jt); |
|
94 |
JfrUpcalls::on_retransform(JfrTraceId::get(class_being_redefined), |
|
95 |
class_being_redefined, |
|
96 |
class_data_len, |
|
97 |
class_data, |
|
98 |
new_class_data_len, |
|
99 |
new_class_data, |
|
100 |
jt); |
|
101 |
} |
|
102 |
||
103 |
// caller needs ResourceMark |
|
104 |
static jclass* create_classes_array(jint classes_count, TRAPS) { |
|
105 |
assert(classes_count > 0, "invariant"); |
|
106 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(THREAD)); |
|
107 |
ThreadInVMfromNative tvmfn((JavaThread*)THREAD); |
|
108 |
jclass* const classes = NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(THREAD, jclass, classes_count); |
|
109 |
if (NULL == classes) { |
|
110 |
char error_buffer[ERROR_MSG_BUFFER_SIZE]; |
|
111 |
jio_snprintf(error_buffer, ERROR_MSG_BUFFER_SIZE, |
|
112 |
"Thread local allocation (native) of " SIZE_FORMAT " bytes failed " |
|
113 |
"in retransform classes", sizeof(jclass) * classes_count); |
|
114 |
log_error(jfr, system)("%s", error_buffer); |
|
115 |
JfrJavaSupport::throw_out_of_memory_error(error_buffer, CHECK_NULL); |
|
116 |
} |
|
117 |
return classes; |
|
118 |
} |
|
119 |
||
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
120 |
// caller needs ResourceMark |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
121 |
static void log_and_throw(jvmtiError error, TRAPS) { |
50113 | 122 |
if (!HAS_PENDING_EXCEPTION) { |
123 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(THREAD)); |
|
124 |
ThreadInVMfromNative tvmfn((JavaThread*)THREAD); |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
125 |
const char base_error_msg[] = "JfrJvmtiAgent::retransformClasses failed: "; |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
126 |
size_t length = sizeof base_error_msg; // includes terminating null |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
127 |
const char* const jvmti_error_name = JvmtiUtil::error_name(error); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
128 |
assert(jvmti_error_name != NULL, "invariant"); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
129 |
length += strlen(jvmti_error_name); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
130 |
char* error_msg = NEW_RESOURCE_ARRAY(char, length); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
131 |
jio_snprintf(error_msg, length, "%s%s", base_error_msg, jvmti_error_name); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
132 |
if (JVMTI_ERROR_INVALID_CLASS_FORMAT == error) { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
133 |
JfrJavaSupport::throw_class_format_error(error_msg, THREAD); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
134 |
} else { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
135 |
JfrJavaSupport::throw_runtime_exception(error_msg, THREAD); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
136 |
} |
50113 | 137 |
} |
138 |
} |
|
139 |
||
140 |
static void check_exception_and_log(JNIEnv* env, TRAPS) { |
|
141 |
assert(env != NULL, "invariant"); |
|
142 |
if (env->ExceptionOccurred()) { |
|
143 |
// array index out of bound |
|
144 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(THREAD)); |
|
145 |
ThreadInVMfromNative tvmfn((JavaThread*)THREAD); |
|
146 |
log_error(jfr, system)("GetObjectArrayElement threw an exception"); |
|
147 |
return; |
|
148 |
} |
|
149 |
} |
|
150 |
||
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
151 |
static bool is_valid_jvmti_phase() { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
152 |
return JvmtiEnvBase::get_phase() == JVMTI_PHASE_LIVE; |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
153 |
} |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
154 |
|
50113 | 155 |
void JfrJvmtiAgent::retransform_classes(JNIEnv* env, jobjectArray classes_array, TRAPS) { |
156 |
assert(env != NULL, "invariant"); |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
157 |
assert(classes_array != NULL, "invariant"); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
158 |
assert(is_valid_jvmti_phase(), "invariant"); |
50113 | 159 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(THREAD)); |
160 |
const jint classes_count = env->GetArrayLength(classes_array); |
|
161 |
if (classes_count <= 0) { |
|
162 |
return; |
|
163 |
} |
|
164 |
ResourceMark rm(THREAD); |
|
165 |
jclass* const classes = create_classes_array(classes_count, CHECK); |
|
166 |
assert(classes != NULL, "invariant"); |
|
167 |
for (jint i = 0; i < classes_count; i++) { |
|
168 |
jclass clz = (jclass)env->GetObjectArrayElement(classes_array, i); |
|
169 |
check_exception_and_log(env, THREAD); |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
170 |
classes[i] = clz; |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
171 |
} |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
172 |
{ |
50113 | 173 |
// inspecting the oop/klass requires a thread transition |
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
174 |
ThreadInVMfromNative transition((JavaThread*)THREAD); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
175 |
for (jint i = 0; i < classes_count; ++i) { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
176 |
jclass clz = classes[i]; |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
177 |
if (!JdkJfrEvent::is_a(clz)) { |
50113 | 178 |
// outside the event hierarchy |
179 |
JdkJfrEvent::tag_as_host(clz); |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
183 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(THREAD)); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
184 |
const jvmtiError result = jfr_jvmti_env->RetransformClasses(classes_count, classes); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
185 |
if (result != JVMTI_ERROR_NONE) { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
186 |
log_and_throw(result, THREAD); |
50113 | 187 |
} |
188 |
} |
|
189 |
||
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
190 |
static bool register_callbacks(JavaThread* jt) { |
50113 | 191 |
assert(jfr_jvmti_env != NULL, "invariant"); |
192 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(jt)); |
|
193 |
jvmtiEventCallbacks callbacks; |
|
194 |
/* Set callbacks */ |
|
195 |
memset(&callbacks, 0, sizeof(callbacks)); |
|
196 |
callbacks.ClassFileLoadHook = jfr_on_class_file_load_hook; |
|
197 |
const jvmtiError jvmti_ret_code = jfr_jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
|
198 |
check_jvmti_error(jfr_jvmti_env, jvmti_ret_code, "SetEventCallbacks"); |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
199 |
return jvmti_ret_code == JVMTI_ERROR_NONE; |
50113 | 200 |
} |
201 |
||
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
202 |
static bool register_capabilities(JavaThread* jt) { |
50113 | 203 |
assert(jfr_jvmti_env != NULL, "invariant"); |
204 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(jt)); |
|
205 |
jvmtiCapabilities capabilities; |
|
206 |
/* Add JVMTI capabilities */ |
|
207 |
(void)memset(&capabilities, 0, sizeof(capabilities)); |
|
208 |
capabilities.can_retransform_classes = 1; |
|
209 |
capabilities.can_retransform_any_class = 1; |
|
210 |
const jvmtiError jvmti_ret_code = jfr_jvmti_env->AddCapabilities(&capabilities); |
|
211 |
check_jvmti_error(jfr_jvmti_env, jvmti_ret_code, "Add Capabilities"); |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
212 |
return jvmti_ret_code == JVMTI_ERROR_NONE; |
50113 | 213 |
} |
214 |
||
215 |
static jint create_jvmti_env(JavaThread* jt) { |
|
216 |
assert(jfr_jvmti_env == NULL, "invariant"); |
|
217 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_native(jt)); |
|
218 |
extern struct JavaVM_ main_vm; |
|
219 |
JavaVM* vm = &main_vm; |
|
220 |
return vm->GetEnv((void **)&jfr_jvmti_env, JVMTI_VERSION); |
|
221 |
} |
|
222 |
||
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
223 |
static bool unregister_callbacks(JavaThread* jt) { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
224 |
assert(jfr_jvmti_env != NULL, "invariant"); |
50113 | 225 |
jvmtiEventCallbacks callbacks; |
226 |
/* Set empty callbacks */ |
|
227 |
memset(&callbacks, 0, sizeof(callbacks)); |
|
228 |
const jvmtiError jvmti_ret_code = jfr_jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
|
229 |
check_jvmti_error(jfr_jvmti_env, jvmti_ret_code, "SetEventCallbacks"); |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
230 |
return jvmti_ret_code == JVMTI_ERROR_NONE; |
50113 | 231 |
} |
232 |
||
233 |
JfrJvmtiAgent::JfrJvmtiAgent() {} |
|
234 |
||
235 |
JfrJvmtiAgent::~JfrJvmtiAgent() { |
|
236 |
JavaThread* jt = current_java_thread(); |
|
237 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt)); |
|
238 |
if (jfr_jvmti_env != NULL) { |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
239 |
ThreadToNativeFromVM transition(jt); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
240 |
update_class_file_load_hook_event(JVMTI_DISABLE); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
241 |
unregister_callbacks(jt); |
50113 | 242 |
jfr_jvmti_env->DisposeEnvironment(); |
243 |
jfr_jvmti_env = NULL; |
|
244 |
} |
|
245 |
} |
|
246 |
||
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
247 |
static bool initialize(JavaThread* jt) { |
50113 | 248 |
assert(jt != NULL, "invariant"); |
249 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt)); |
|
250 |
ThreadToNativeFromVM transition(jt); |
|
251 |
if (create_jvmti_env(jt) != JNI_OK) { |
|
252 |
assert(jfr_jvmti_env == NULL, "invariant"); |
|
253 |
return false; |
|
254 |
} |
|
255 |
assert(jfr_jvmti_env != NULL, "invariant"); |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
256 |
if (!register_capabilities(jt)) { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
257 |
return false; |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
258 |
} |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
259 |
if (!register_callbacks(jt)) { |
50113 | 260 |
return false; |
261 |
} |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
262 |
return update_class_file_load_hook_event(JVMTI_ENABLE); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
263 |
} |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
264 |
|
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
265 |
static void log_and_throw_illegal_state_exception(TRAPS) { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
266 |
DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
267 |
const char* const illegal_state_msg = "An attempt was made to start JFR too early in the VM initialization sequence."; |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
268 |
log_error(jfr, system)(illegal_state_msg); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
269 |
log_error(jfr, system)("JFR uses JVMTI RetransformClasses and requires the JVMTI state to have entered JVMTI_PHASE_LIVE."); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
270 |
log_error(jfr, system)("Please initialize JFR in response to event JVMTI_EVENT_VM_INIT instead of JVMTI_EVENT_VM_START."); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
271 |
JfrJavaSupport::throw_illegal_state_exception(illegal_state_msg, THREAD); |
50113 | 272 |
} |
273 |
||
274 |
bool JfrJvmtiAgent::create() { |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
275 |
assert(agent == NULL, "invariant"); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
276 |
JavaThread* const jt = current_java_thread(); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
277 |
if (!is_valid_jvmti_phase()) { |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
278 |
log_and_throw_illegal_state_exception(jt); |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
279 |
return false; |
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
280 |
} |
50113 | 281 |
agent = new JfrJvmtiAgent(); |
282 |
if (agent == NULL) { |
|
283 |
return false; |
|
284 |
} |
|
59259
127ca611f19b
8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
mgronlun
parents:
50113
diff
changeset
|
285 |
if (!initialize(jt)) { |
50113 | 286 |
delete agent; |
287 |
agent = NULL; |
|
288 |
return false; |
|
289 |
} |
|
290 |
return true; |
|
291 |
} |
|
292 |
||
293 |
void JfrJvmtiAgent::destroy() { |
|
294 |
if (agent != NULL) { |
|
295 |
delete agent; |
|
296 |
agent = NULL; |
|
297 |
} |
|
298 |
} |