1 /*
2 * Copyright (c) 2003, 2021, 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
25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.inline.hpp"
27 #include "classfile/moduleEntry.hpp"
28 #include "classfile/vmClasses.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "code/nmethod.hpp"
31 #include "code/pcDesc.hpp"
32 #include "code/scopeDesc.hpp"
33 #include "gc/shared/oopStorageSet.hpp"
34 #include "interpreter/interpreter.hpp"
35 #include "jvmtifiles/jvmtiEnv.hpp"
36 #include "logging/log.hpp"
37 #include "logging/logStream.hpp"
38 #include "memory/allocation.inline.hpp"
39 #include "memory/resourceArea.hpp"
40 #include "memory/universe.hpp"
41 #include "oops/klass.inline.hpp"
42 #include "oops/objArrayKlass.hpp"
43 #include "oops/objArrayOop.hpp"
44 #include "oops/oop.inline.hpp"
45 #include "oops/oopHandle.inline.hpp"
46 #include "prims/jvmtiCodeBlobEvents.hpp"
47 #include "prims/jvmtiEventController.hpp"
48 #include "prims/jvmtiEventController.inline.hpp"
49 #include "prims/jvmtiExport.hpp"
50 #include "prims/jvmtiImpl.hpp"
51 #include "prims/jvmtiManageCapabilities.hpp"
52 #include "prims/jvmtiRawMonitor.hpp"
53 #include "prims/jvmtiRedefineClasses.hpp"
54 #include "prims/jvmtiTagMap.hpp"
55 #include "prims/jvmtiThreadState.inline.hpp"
56 #include "runtime/arguments.hpp"
57 #include "runtime/fieldDescriptor.inline.hpp"
58 #include "runtime/handles.inline.hpp"
59 #include "runtime/interfaceSupport.inline.hpp"
60 #include "runtime/javaCalls.hpp"
61 #include "runtime/jniHandles.inline.hpp"
62 #include "runtime/objectMonitor.hpp"
63 #include "runtime/objectMonitor.inline.hpp"
64 #include "runtime/os.hpp"
65 #include "runtime/osThread.hpp"
66 #include "runtime/safepointVerifiers.hpp"
67 #include "runtime/serviceThread.hpp"
68 #include "runtime/thread.inline.hpp"
69 #include "runtime/threadSMR.hpp"
70 #include "runtime/vframe.inline.hpp"
71 #include "runtime/vm_version.hpp"
72 #include "utilities/macros.hpp"
73
74 #ifdef JVMTI_TRACE
75 #define EVT_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_SENT) != 0) { SafeResourceMark rm; log_trace(jvmti) out; }
76 #define EVT_TRIG_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_TRIGGER) != 0) { SafeResourceMark rm; log_trace(jvmti) out; }
77 #else
78 #define EVT_TRIG_TRACE(evt,out)
79 #define EVT_TRACE(evt,out)
80 #endif
81
82 ///////////////////////////////////////////////////////////////
83 //
84 // JvmtiEventTransition
85 //
86 // TO DO --
87 // more handle purging
88
89 // Use this for JavaThreads and state is _thread_in_vm.
90 class JvmtiJavaThreadEventTransition : StackObj {
91 private:
92 ResourceMark _rm;
93 ThreadToNativeFromVM _transition;
94 HandleMark _hm;
95
96 public:
JvmtiJavaThreadEventTransition(JavaThread * thread)97 JvmtiJavaThreadEventTransition(JavaThread *thread) :
98 _rm(),
99 _transition(thread),
100 _hm(thread) {};
101 };
102
103 // For JavaThreads which are not in _thread_in_vm state
104 // and other system threads use this.
105 class JvmtiThreadEventTransition : StackObj {
106 private:
107 ResourceMark _rm;
108 HandleMark _hm;
109 JavaThreadState _saved_state;
110 JavaThread *_jthread;
111
112 public:
JvmtiThreadEventTransition(Thread * thread)113 JvmtiThreadEventTransition(Thread *thread) : _rm(), _hm(thread) {
114 if (thread->is_Java_thread()) {
115 _jthread = thread->as_Java_thread();
116 _saved_state = _jthread->thread_state();
117 if (_saved_state == _thread_in_Java) {
118 ThreadStateTransition::transition_from_java(_jthread, _thread_in_native);
119 } else {
120 ThreadStateTransition::transition(_jthread, _saved_state, _thread_in_native);
121 }
122 } else {
123 _jthread = NULL;
124 }
125 }
126
~JvmtiThreadEventTransition()127 ~JvmtiThreadEventTransition() {
128 if (_jthread != NULL)
129 ThreadStateTransition::transition_from_native(_jthread, _saved_state);
130 }
131 };
132
133
134 ///////////////////////////////////////////////////////////////
135 //
136 // JvmtiEventMark
137 //
138
139 class JvmtiEventMark : public StackObj {
140 private:
141 JavaThread *_thread;
142 JNIEnv* _jni_env;
143 JvmtiThreadState::ExceptionState _saved_exception_state;
144 #if 0
145 JNIHandleBlock* _hblock;
146 #endif
147
148 public:
JvmtiEventMark(JavaThread * thread)149 JvmtiEventMark(JavaThread *thread) : _thread(thread),
150 _jni_env(thread->jni_environment()),
151 _saved_exception_state(JvmtiThreadState::ES_CLEARED) {
152 #if 0
153 _hblock = thread->active_handles();
154 _hblock->clear_thoroughly(); // so we can be safe
155 #else
156 // we want to use the code above - but that needs the JNIHandle changes - later...
157 // for now, steal JNI push local frame code
158 JvmtiThreadState *state = thread->jvmti_thread_state();
159 // we are before an event.
160 // Save current jvmti thread exception state.
161 if (state != NULL) {
162 _saved_exception_state = state->get_exception_state();
163 }
164
165 JNIHandleBlock* old_handles = thread->active_handles();
166 JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
167 assert(new_handles != NULL, "should not be NULL");
168 new_handles->set_pop_frame_link(old_handles);
169 thread->set_active_handles(new_handles);
170 #endif
171 assert(thread == JavaThread::current(), "thread must be current!");
172 thread->frame_anchor()->make_walkable(thread);
173 };
174
~JvmtiEventMark()175 ~JvmtiEventMark() {
176 #if 0
177 _hblock->clear(); // for consistency with future correct behavior
178 #else
179 // we want to use the code above - but that needs the JNIHandle changes - later...
180 // for now, steal JNI pop local frame code
181 JNIHandleBlock* old_handles = _thread->active_handles();
182 JNIHandleBlock* new_handles = old_handles->pop_frame_link();
183 assert(new_handles != NULL, "should not be NULL");
184 _thread->set_active_handles(new_handles);
185 // Note that we set the pop_frame_link to NULL explicitly, otherwise
186 // the release_block call will release the blocks.
187 old_handles->set_pop_frame_link(NULL);
188 JNIHandleBlock::release_block(old_handles, _thread); // may block
189 #endif
190
191 JvmtiThreadState* state = _thread->jvmti_thread_state();
192 // we are continuing after an event.
193 if (state != NULL) {
194 // Restore the jvmti thread exception state.
195 state->restore_exception_state(_saved_exception_state);
196 }
197 }
198
199 #if 0
200 jobject to_jobject(oop obj) { return obj == NULL? NULL : _hblock->allocate_handle_fast(obj); }
201 #else
202 // we want to use the code above - but that needs the JNIHandle changes - later...
203 // for now, use regular make_local
to_jobject(oop obj)204 jobject to_jobject(oop obj) { return JNIHandles::make_local(_thread,obj); }
205 #endif
206
to_jclass(Klass * klass)207 jclass to_jclass(Klass* klass) { return (klass == NULL ? NULL : (jclass)to_jobject(klass->java_mirror())); }
208
to_jmethodID(const methodHandle & method)209 jmethodID to_jmethodID(const methodHandle& method) { return method->jmethod_id(); }
210
jni_env()211 JNIEnv* jni_env() { return _jni_env; }
212 };
213
214 class JvmtiThreadEventMark : public JvmtiEventMark {
215 private:
216 jthread _jt;
217
218 public:
JvmtiThreadEventMark(JavaThread * thread)219 JvmtiThreadEventMark(JavaThread *thread) :
220 JvmtiEventMark(thread) {
221 _jt = (jthread)(to_jobject(thread->threadObj()));
222 };
jni_thread()223 jthread jni_thread() { return _jt; }
224 };
225
226 class JvmtiClassEventMark : public JvmtiThreadEventMark {
227 private:
228 jclass _jc;
229
230 public:
JvmtiClassEventMark(JavaThread * thread,Klass * klass)231 JvmtiClassEventMark(JavaThread *thread, Klass* klass) :
232 JvmtiThreadEventMark(thread) {
233 _jc = to_jclass(klass);
234 };
jni_class()235 jclass jni_class() { return _jc; }
236 };
237
238 class JvmtiMethodEventMark : public JvmtiThreadEventMark {
239 private:
240 jmethodID _mid;
241
242 public:
JvmtiMethodEventMark(JavaThread * thread,const methodHandle & method)243 JvmtiMethodEventMark(JavaThread *thread, const methodHandle& method) :
244 JvmtiThreadEventMark(thread),
245 _mid(to_jmethodID(method)) {};
jni_methodID()246 jmethodID jni_methodID() { return _mid; }
247 };
248
249 class JvmtiLocationEventMark : public JvmtiMethodEventMark {
250 private:
251 jlocation _loc;
252
253 public:
JvmtiLocationEventMark(JavaThread * thread,const methodHandle & method,address location)254 JvmtiLocationEventMark(JavaThread *thread, const methodHandle& method, address location) :
255 JvmtiMethodEventMark(thread, method),
256 _loc(location - method->code_base()) {};
location()257 jlocation location() { return _loc; }
258 };
259
260 class JvmtiExceptionEventMark : public JvmtiLocationEventMark {
261 private:
262 jobject _exc;
263
264 public:
JvmtiExceptionEventMark(JavaThread * thread,const methodHandle & method,address location,Handle exception)265 JvmtiExceptionEventMark(JavaThread *thread, const methodHandle& method, address location, Handle exception) :
266 JvmtiLocationEventMark(thread, method, location),
267 _exc(to_jobject(exception())) {};
exception()268 jobject exception() { return _exc; }
269 };
270
271 class JvmtiClassFileLoadEventMark : public JvmtiThreadEventMark {
272 private:
273 const char *_class_name;
274 jobject _jloader;
275 jobject _protection_domain;
276 jclass _class_being_redefined;
277
278 public:
JvmtiClassFileLoadEventMark(JavaThread * thread,Symbol * name,Handle class_loader,Handle prot_domain,Klass * class_being_redefined)279 JvmtiClassFileLoadEventMark(JavaThread *thread, Symbol* name,
280 Handle class_loader, Handle prot_domain, Klass* class_being_redefined) : JvmtiThreadEventMark(thread) {
281 _class_name = name != NULL? name->as_utf8() : NULL;
282 _jloader = (jobject)to_jobject(class_loader());
283 _protection_domain = (jobject)to_jobject(prot_domain());
284 if (class_being_redefined == NULL) {
285 _class_being_redefined = NULL;
286 } else {
287 _class_being_redefined = (jclass)to_jclass(class_being_redefined);
288 }
289 };
class_name()290 const char *class_name() {
291 return _class_name;
292 }
jloader()293 jobject jloader() {
294 return _jloader;
295 }
protection_domain()296 jobject protection_domain() {
297 return _protection_domain;
298 }
class_being_redefined()299 jclass class_being_redefined() {
300 return _class_being_redefined;
301 }
302 };
303
304 //////////////////////////////////////////////////////////////////////////////
305
306 int JvmtiExport::_field_access_count = 0;
307 int JvmtiExport::_field_modification_count = 0;
308
309 bool JvmtiExport::_can_access_local_variables = false;
310 bool JvmtiExport::_can_hotswap_or_post_breakpoint = false;
311 bool JvmtiExport::_can_modify_any_class = false;
312 bool JvmtiExport::_can_walk_any_space = false;
313
314 uint64_t JvmtiExport::_redefinition_count = 0;
315 bool JvmtiExport::_all_dependencies_are_recorded = false;
316
317 //
318 // field access management
319 //
320
321 // interpreter generator needs the address of the counter
get_field_access_count_addr()322 address JvmtiExport::get_field_access_count_addr() {
323 // We don't grab a lock because we don't want to
324 // serialize field access between all threads. This means that a
325 // thread on another processor can see the wrong count value and
326 // may either miss making a needed call into post_field_access()
327 // or will make an unneeded call into post_field_access(). We pay
328 // this price to avoid slowing down the VM when we aren't watching
329 // field accesses.
330 // Other access/mutation safe by virtue of being in VM state.
331 return (address)(&_field_access_count);
332 }
333
334 //
335 // field modification management
336 //
337
338 // interpreter generator needs the address of the counter
get_field_modification_count_addr()339 address JvmtiExport::get_field_modification_count_addr() {
340 // We don't grab a lock because we don't
341 // want to serialize field modification between all threads. This
342 // means that a thread on another processor can see the wrong
343 // count value and may either miss making a needed call into
344 // post_field_modification() or will make an unneeded call into
345 // post_field_modification(). We pay this price to avoid slowing
346 // down the VM when we aren't watching field modifications.
347 // Other access/mutation safe by virtue of being in VM state.
348 return (address)(&_field_modification_count);
349 }
350
351
352 ///////////////////////////////////////////////////////////////
353 // Functions needed by java.lang.instrument for starting up javaagent.
354 ///////////////////////////////////////////////////////////////
355
356 jint
get_jvmti_interface(JavaVM * jvm,void ** penv,jint version)357 JvmtiExport::get_jvmti_interface(JavaVM *jvm, void **penv, jint version) {
358 // The JVMTI_VERSION_INTERFACE_JVMTI part of the version number
359 // has already been validated in JNI GetEnv().
360 int major, minor, micro;
361
362 // micro version doesn't matter here (yet?)
363 decode_version_values(version, &major, &minor, µ);
364 switch (major) {
365 case 1:
366 switch (minor) {
367 case 0: // version 1.0.<micro> is recognized
368 case 1: // version 1.1.<micro> is recognized
369 case 2: // version 1.2.<micro> is recognized
370 break;
371
372 default:
373 return JNI_EVERSION; // unsupported minor version number
374 }
375 break;
376 case 9:
377 switch (minor) {
378 case 0: // version 9.0.<micro> is recognized
379 break;
380 default:
381 return JNI_EVERSION; // unsupported minor version number
382 }
383 break;
384 case 11:
385 switch (minor) {
386 case 0: // version 11.0.<micro> is recognized
387 break;
388 default:
389 return JNI_EVERSION; // unsupported minor version number
390 }
391 break;
392 default:
393 // Starting from 13 we do not care about minor version anymore
394 if (major < 13 || major > Abstract_VM_Version::vm_major_version()) {
395 return JNI_EVERSION; // unsupported major version number
396 }
397 }
398
399 if (JvmtiEnv::get_phase() == JVMTI_PHASE_LIVE) {
400 JavaThread* current_thread = JavaThread::current();
401 // transition code: native to VM
402 ThreadInVMfromNative __tiv(current_thread);
403 VM_ENTRY_BASE(jvmtiEnv*, JvmtiExport::get_jvmti_interface, current_thread)
404 debug_only(VMNativeEntryWrapper __vew;)
405
406 JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version);
407 *penv = jvmti_env->jvmti_external(); // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv*
408 return JNI_OK;
409
410 } else if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
411 // not live, no thread to transition
412 JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version);
413 *penv = jvmti_env->jvmti_external(); // actual type is jvmtiEnv* -- not to be confused with JvmtiEnv*
414 return JNI_OK;
415
416 } else {
417 // Called at the wrong time
418 *penv = NULL;
419 return JNI_EDETACHED;
420 }
421 }
422
423 void
add_default_read_edges(Handle h_module,TRAPS)424 JvmtiExport::add_default_read_edges(Handle h_module, TRAPS) {
425 if (!Universe::is_module_initialized()) {
426 return; // extra safety
427 }
428 assert(!h_module.is_null(), "module should always be set");
429
430 // Invoke the transformedByAgent method
431 JavaValue result(T_VOID);
432 JavaCalls::call_static(&result,
433 vmClasses::module_Modules_klass(),
434 vmSymbols::transformedByAgent_name(),
435 vmSymbols::transformedByAgent_signature(),
436 h_module,
437 THREAD);
438
439 if (HAS_PENDING_EXCEPTION) {
440 LogTarget(Trace, jvmti) log;
441 LogStream log_stream(log);
442 java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
443 log_stream.cr();
444 CLEAR_PENDING_EXCEPTION;
445 return;
446 }
447 }
448
449 jvmtiError
add_module_reads(Handle module,Handle to_module,TRAPS)450 JvmtiExport::add_module_reads(Handle module, Handle to_module, TRAPS) {
451 if (!Universe::is_module_initialized()) {
452 return JVMTI_ERROR_NONE; // extra safety
453 }
454 assert(!module.is_null(), "module should always be set");
455 assert(!to_module.is_null(), "to_module should always be set");
456
457 // Invoke the addReads method
458 JavaValue result(T_VOID);
459 JavaCalls::call_static(&result,
460 vmClasses::module_Modules_klass(),
461 vmSymbols::addReads_name(),
462 vmSymbols::addReads_signature(),
463 module,
464 to_module,
465 THREAD);
466
467 if (HAS_PENDING_EXCEPTION) {
468 LogTarget(Trace, jvmti) log;
469 LogStream log_stream(log);
470 java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
471 log_stream.cr();
472 CLEAR_PENDING_EXCEPTION;
473 return JVMTI_ERROR_INTERNAL;
474 }
475 return JVMTI_ERROR_NONE;
476 }
477
478 jvmtiError
add_module_exports(Handle module,Handle pkg_name,Handle to_module,TRAPS)479 JvmtiExport::add_module_exports(Handle module, Handle pkg_name, Handle to_module, TRAPS) {
480 if (!Universe::is_module_initialized()) {
481 return JVMTI_ERROR_NONE; // extra safety
482 }
483 assert(!module.is_null(), "module should always be set");
484 assert(!to_module.is_null(), "to_module should always be set");
485 assert(!pkg_name.is_null(), "pkg_name should always be set");
486
487 // Invoke the addExports method
488 JavaValue result(T_VOID);
489 JavaCalls::call_static(&result,
490 vmClasses::module_Modules_klass(),
491 vmSymbols::addExports_name(),
492 vmSymbols::addExports_signature(),
493 module,
494 pkg_name,
495 to_module,
496 THREAD);
497
498 if (HAS_PENDING_EXCEPTION) {
499 Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
500 LogTarget(Trace, jvmti) log;
501 LogStream log_stream(log);
502 java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
503 log_stream.cr();
504 CLEAR_PENDING_EXCEPTION;
505 if (ex_name == vmSymbols::java_lang_IllegalArgumentException()) {
506 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
507 }
508 return JVMTI_ERROR_INTERNAL;
509 }
510 return JVMTI_ERROR_NONE;
511 }
512
513 jvmtiError
add_module_opens(Handle module,Handle pkg_name,Handle to_module,TRAPS)514 JvmtiExport::add_module_opens(Handle module, Handle pkg_name, Handle to_module, TRAPS) {
515 if (!Universe::is_module_initialized()) {
516 return JVMTI_ERROR_NONE; // extra safety
517 }
518 assert(!module.is_null(), "module should always be set");
519 assert(!to_module.is_null(), "to_module should always be set");
520 assert(!pkg_name.is_null(), "pkg_name should always be set");
521
522 // Invoke the addOpens method
523 JavaValue result(T_VOID);
524 JavaCalls::call_static(&result,
525 vmClasses::module_Modules_klass(),
526 vmSymbols::addOpens_name(),
527 vmSymbols::addExports_signature(),
528 module,
529 pkg_name,
530 to_module,
531 THREAD);
532
533 if (HAS_PENDING_EXCEPTION) {
534 Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
535 LogTarget(Trace, jvmti) log;
536 LogStream log_stream(log);
537 java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
538 log_stream.cr();
539 CLEAR_PENDING_EXCEPTION;
540 if (ex_name == vmSymbols::java_lang_IllegalArgumentException()) {
541 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
542 }
543 return JVMTI_ERROR_INTERNAL;
544 }
545 return JVMTI_ERROR_NONE;
546 }
547
548 jvmtiError
add_module_uses(Handle module,Handle service,TRAPS)549 JvmtiExport::add_module_uses(Handle module, Handle service, TRAPS) {
550 if (!Universe::is_module_initialized()) {
551 return JVMTI_ERROR_NONE; // extra safety
552 }
553 assert(!module.is_null(), "module should always be set");
554 assert(!service.is_null(), "service should always be set");
555
556 // Invoke the addUses method
557 JavaValue result(T_VOID);
558 JavaCalls::call_static(&result,
559 vmClasses::module_Modules_klass(),
560 vmSymbols::addUses_name(),
561 vmSymbols::addUses_signature(),
562 module,
563 service,
564 THREAD);
565
566 if (HAS_PENDING_EXCEPTION) {
567 LogTarget(Trace, jvmti) log;
568 LogStream log_stream(log);
569 java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
570 log_stream.cr();
571 CLEAR_PENDING_EXCEPTION;
572 return JVMTI_ERROR_INTERNAL;
573 }
574 return JVMTI_ERROR_NONE;
575 }
576
577 jvmtiError
add_module_provides(Handle module,Handle service,Handle impl_class,TRAPS)578 JvmtiExport::add_module_provides(Handle module, Handle service, Handle impl_class, TRAPS) {
579 if (!Universe::is_module_initialized()) {
580 return JVMTI_ERROR_NONE; // extra safety
581 }
582 assert(!module.is_null(), "module should always be set");
583 assert(!service.is_null(), "service should always be set");
584 assert(!impl_class.is_null(), "impl_class should always be set");
585
586 // Invoke the addProvides method
587 JavaValue result(T_VOID);
588 JavaCalls::call_static(&result,
589 vmClasses::module_Modules_klass(),
590 vmSymbols::addProvides_name(),
591 vmSymbols::addProvides_signature(),
592 module,
593 service,
594 impl_class,
595 THREAD);
596
597 if (HAS_PENDING_EXCEPTION) {
598 LogTarget(Trace, jvmti) log;
599 LogStream log_stream(log);
600 java_lang_Throwable::print(PENDING_EXCEPTION, &log_stream);
601 log_stream.cr();
602 CLEAR_PENDING_EXCEPTION;
603 return JVMTI_ERROR_INTERNAL;
604 }
605 return JVMTI_ERROR_NONE;
606 }
607
608 void
decode_version_values(jint version,int * major,int * minor,int * micro)609 JvmtiExport::decode_version_values(jint version, int * major, int * minor,
610 int * micro) {
611 *major = (version & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR;
612 *minor = (version & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR;
613 *micro = (version & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO;
614 }
615
enter_primordial_phase()616 void JvmtiExport::enter_primordial_phase() {
617 JvmtiEnvBase::set_phase(JVMTI_PHASE_PRIMORDIAL);
618 }
619
enter_early_start_phase()620 void JvmtiExport::enter_early_start_phase() {
621 set_early_vmstart_recorded(true);
622 }
623
enter_start_phase()624 void JvmtiExport::enter_start_phase() {
625 JvmtiEnvBase::set_phase(JVMTI_PHASE_START);
626 }
627
enter_onload_phase()628 void JvmtiExport::enter_onload_phase() {
629 JvmtiEnvBase::set_phase(JVMTI_PHASE_ONLOAD);
630 }
631
enter_live_phase()632 void JvmtiExport::enter_live_phase() {
633 JvmtiEnvBase::set_phase(JVMTI_PHASE_LIVE);
634 }
635
636 //
637 // JVMTI events that the VM posts to the debugger and also startup agent
638 // and call the agent's premain() for java.lang.instrument.
639 //
640
post_early_vm_start()641 void JvmtiExport::post_early_vm_start() {
642 EVT_TRIG_TRACE(JVMTI_EVENT_VM_START, ("Trg Early VM start event triggered" ));
643
644 // can now enable some events
645 JvmtiEventController::vm_start();
646
647 JvmtiEnvIterator it;
648 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
649 // Only early vmstart envs post early VMStart event
650 if (env->early_vmstart_env() && env->is_enabled(JVMTI_EVENT_VM_START)) {
651 EVT_TRACE(JVMTI_EVENT_VM_START, ("Evt Early VM start event sent" ));
652 JavaThread *thread = JavaThread::current();
653 JvmtiThreadEventMark jem(thread);
654 JvmtiJavaThreadEventTransition jet(thread);
655 jvmtiEventVMStart callback = env->callbacks()->VMStart;
656 if (callback != NULL) {
657 (*callback)(env->jvmti_external(), jem.jni_env());
658 }
659 }
660 }
661 }
662
post_vm_start()663 void JvmtiExport::post_vm_start() {
664 EVT_TRIG_TRACE(JVMTI_EVENT_VM_START, ("Trg VM start event triggered" ));
665
666 // can now enable some events
667 JvmtiEventController::vm_start();
668
669 JvmtiEnvIterator it;
670 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
671 // Early vmstart envs do not post normal VMStart event
672 if (!env->early_vmstart_env() && env->is_enabled(JVMTI_EVENT_VM_START)) {
673 EVT_TRACE(JVMTI_EVENT_VM_START, ("Evt VM start event sent" ));
674
675 JavaThread *thread = JavaThread::current();
676 JvmtiThreadEventMark jem(thread);
677 JvmtiJavaThreadEventTransition jet(thread);
678 jvmtiEventVMStart callback = env->callbacks()->VMStart;
679 if (callback != NULL) {
680 (*callback)(env->jvmti_external(), jem.jni_env());
681 }
682 }
683 }
684 }
685
686 static OopStorage* _jvmti_oop_storage = NULL;
687 static OopStorage* _weak_tag_storage = NULL;
688
jvmti_oop_storage()689 OopStorage* JvmtiExport::jvmti_oop_storage() {
690 assert(_jvmti_oop_storage != NULL, "not yet initialized");
691 return _jvmti_oop_storage;
692 }
693
weak_tag_storage()694 OopStorage* JvmtiExport::weak_tag_storage() {
695 assert(_weak_tag_storage != NULL, "not yet initialized");
696 return _weak_tag_storage;
697 }
698
initialize_oop_storage()699 void JvmtiExport::initialize_oop_storage() {
700 // OopStorage needs to be created early in startup and unconditionally
701 // because of OopStorageSet static array indices.
702 _jvmti_oop_storage = OopStorageSet::create_strong("JVMTI OopStorage", mtServiceability);
703 _weak_tag_storage = OopStorageSet::create_weak("JVMTI Tag Weak OopStorage", mtServiceability);
704 _weak_tag_storage->register_num_dead_callback(&JvmtiTagMap::gc_notification);
705 }
706
post_vm_initialized()707 void JvmtiExport::post_vm_initialized() {
708 EVT_TRIG_TRACE(JVMTI_EVENT_VM_INIT, ("Trg VM init event triggered" ));
709
710 // can now enable events
711 JvmtiEventController::vm_init();
712
713 JvmtiEnvIterator it;
714 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
715 if (env->is_enabled(JVMTI_EVENT_VM_INIT)) {
716 EVT_TRACE(JVMTI_EVENT_VM_INIT, ("Evt VM init event sent" ));
717
718 JavaThread *thread = JavaThread::current();
719 JvmtiThreadEventMark jem(thread);
720 JvmtiJavaThreadEventTransition jet(thread);
721 jvmtiEventVMInit callback = env->callbacks()->VMInit;
722 if (callback != NULL) {
723 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
724 }
725 }
726 }
727 }
728
729
post_vm_death()730 void JvmtiExport::post_vm_death() {
731 EVT_TRIG_TRACE(JVMTI_EVENT_VM_DEATH, ("Trg VM death event triggered" ));
732
733 JvmtiEnvIterator it;
734 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
735 if (env->is_enabled(JVMTI_EVENT_VM_DEATH)) {
736 EVT_TRACE(JVMTI_EVENT_VM_DEATH, ("Evt VM death event sent" ));
737
738 JavaThread *thread = JavaThread::current();
739 JvmtiEventMark jem(thread);
740 JvmtiJavaThreadEventTransition jet(thread);
741 jvmtiEventVMDeath callback = env->callbacks()->VMDeath;
742 if (callback != NULL) {
743 (*callback)(env->jvmti_external(), jem.jni_env());
744 }
745 }
746 }
747
748 JvmtiEnvBase::set_phase(JVMTI_PHASE_DEAD);
749 JvmtiEventController::vm_death();
750 }
751
752 char**
get_all_native_method_prefixes(int * count_ptr)753 JvmtiExport::get_all_native_method_prefixes(int* count_ptr) {
754 // Have to grab JVMTI thread state lock to be sure environment doesn't
755 // go away while we iterate them. No locks during VM bring-up.
756 if (Threads::number_of_threads() == 0 || SafepointSynchronize::is_at_safepoint()) {
757 return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr);
758 } else {
759 MutexLocker mu(JvmtiThreadState_lock);
760 return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr);
761 }
762 }
763
764 // Convert an external thread reference to a JavaThread found on the
765 // specified ThreadsList. The ThreadsListHandle in the caller "protects"
766 // the returned JavaThread *.
767 //
768 // If thread_oop_p is not NULL, then the caller wants to use the oop
769 // after this call so the oop is returned. On success, *jt_pp is set
770 // to the converted JavaThread * and JVMTI_ERROR_NONE is returned.
771 // On error, returns various JVMTI_ERROR_* values.
772 //
773 jvmtiError
cv_external_thread_to_JavaThread(ThreadsList * t_list,jthread thread,JavaThread ** jt_pp,oop * thread_oop_p)774 JvmtiExport::cv_external_thread_to_JavaThread(ThreadsList * t_list,
775 jthread thread,
776 JavaThread ** jt_pp,
777 oop * thread_oop_p) {
778 assert(t_list != NULL, "must have a ThreadsList");
779 assert(jt_pp != NULL, "must have a return JavaThread pointer");
780 // thread_oop_p is optional so no assert()
781
782 oop thread_oop = JNIHandles::resolve_external_guard(thread);
783 if (thread_oop == NULL) {
784 // NULL jthread, GC'ed jthread or a bad JNI handle.
785 return JVMTI_ERROR_INVALID_THREAD;
786 }
787 // Looks like an oop at this point.
788
789 if (!thread_oop->is_a(vmClasses::Thread_klass())) {
790 // The oop is not a java.lang.Thread.
791 return JVMTI_ERROR_INVALID_THREAD;
792 }
793 // Looks like a java.lang.Thread oop at this point.
794
795 if (thread_oop_p != NULL) {
796 // Return the oop to the caller; the caller may still want
797 // the oop even if this function returns an error.
798 *thread_oop_p = thread_oop;
799 }
800
801 JavaThread * java_thread = java_lang_Thread::thread(thread_oop);
802 if (java_thread == NULL) {
803 // The java.lang.Thread does not contain a JavaThread * so it has
804 // not yet run or it has died.
805 return JVMTI_ERROR_THREAD_NOT_ALIVE;
806 }
807 // Looks like a live JavaThread at this point.
808
809 // We do not check the EnableThreadSMRExtraValidityChecks option
810 // for this includes() call because JVM/TI's spec is tighter.
811 if (!t_list->includes(java_thread)) {
812 // Not on the JavaThreads list so it is not alive.
813 return JVMTI_ERROR_THREAD_NOT_ALIVE;
814 }
815
816 // Return a live JavaThread that is "protected" by the
817 // ThreadsListHandle in the caller.
818 *jt_pp = java_thread;
819
820 return JVMTI_ERROR_NONE;
821 }
822
823 // Convert an oop to a JavaThread found on the specified ThreadsList.
824 // The ThreadsListHandle in the caller "protects" the returned
825 // JavaThread *.
826 //
827 // On success, *jt_pp is set to the converted JavaThread * and
828 // JVMTI_ERROR_NONE is returned. On error, returns various
829 // JVMTI_ERROR_* values.
830 //
831 jvmtiError
cv_oop_to_JavaThread(ThreadsList * t_list,oop thread_oop,JavaThread ** jt_pp)832 JvmtiExport::cv_oop_to_JavaThread(ThreadsList * t_list, oop thread_oop,
833 JavaThread ** jt_pp) {
834 assert(t_list != NULL, "must have a ThreadsList");
835 assert(thread_oop != NULL, "must have an oop");
836 assert(jt_pp != NULL, "must have a return JavaThread pointer");
837
838 if (!thread_oop->is_a(vmClasses::Thread_klass())) {
839 // The oop is not a java.lang.Thread.
840 return JVMTI_ERROR_INVALID_THREAD;
841 }
842 // Looks like a java.lang.Thread oop at this point.
843
844 JavaThread * java_thread = java_lang_Thread::thread(thread_oop);
845 if (java_thread == NULL) {
846 // The java.lang.Thread does not contain a JavaThread * so it has
847 // not yet run or it has died.
848 return JVMTI_ERROR_THREAD_NOT_ALIVE;
849 }
850 // Looks like a live JavaThread at this point.
851
852 // We do not check the EnableThreadSMRExtraValidityChecks option
853 // for this includes() call because JVM/TI's spec is tighter.
854 if (!t_list->includes(java_thread)) {
855 // Not on the JavaThreads list so it is not alive.
856 return JVMTI_ERROR_THREAD_NOT_ALIVE;
857 }
858
859 // Return a live JavaThread that is "protected" by the
860 // ThreadsListHandle in the caller.
861 *jt_pp = java_thread;
862
863 return JVMTI_ERROR_NONE;
864 }
865
866 class JvmtiClassFileLoadHookPoster : public StackObj {
867 private:
868 Symbol* _h_name;
869 Handle _class_loader;
870 Handle _h_protection_domain;
871 unsigned char ** _data_ptr;
872 unsigned char ** _end_ptr;
873 JavaThread * _thread;
874 jint _curr_len;
875 unsigned char * _curr_data;
876 JvmtiEnv * _curr_env;
877 JvmtiCachedClassFileData ** _cached_class_file_ptr;
878 JvmtiThreadState * _state;
879 Klass* _class_being_redefined;
880 JvmtiClassLoadKind _load_kind;
881 bool _has_been_modified;
882
883 public:
JvmtiClassFileLoadHookPoster(Symbol * h_name,Handle class_loader,Handle h_protection_domain,unsigned char ** data_ptr,unsigned char ** end_ptr,JvmtiCachedClassFileData ** cache_ptr)884 inline JvmtiClassFileLoadHookPoster(Symbol* h_name, Handle class_loader,
885 Handle h_protection_domain,
886 unsigned char **data_ptr, unsigned char **end_ptr,
887 JvmtiCachedClassFileData **cache_ptr) {
888 _h_name = h_name;
889 _class_loader = class_loader;
890 _h_protection_domain = h_protection_domain;
891 _data_ptr = data_ptr;
892 _end_ptr = end_ptr;
893 _thread = JavaThread::current();
894 _curr_len = *end_ptr - *data_ptr;
895 _curr_data = *data_ptr;
896 _curr_env = NULL;
897 _cached_class_file_ptr = cache_ptr;
898 _has_been_modified = false;
899
900 _state = _thread->jvmti_thread_state();
901 if (_state != NULL) {
902 _class_being_redefined = _state->get_class_being_redefined();
903 _load_kind = _state->get_class_load_kind();
904 Klass* klass = (_class_being_redefined == NULL) ? NULL : _class_being_redefined;
905 if (_load_kind != jvmti_class_load_kind_load && klass != NULL) {
906 ModuleEntry* module_entry = InstanceKlass::cast(klass)->module();
907 assert(module_entry != NULL, "module_entry should always be set");
908 if (module_entry->is_named() &&
909 module_entry->module() != NULL &&
910 !module_entry->has_default_read_edges()) {
911 if (!module_entry->set_has_default_read_edges()) {
912 // We won a potential race.
913 // Add read edges to the unnamed modules of the bootstrap and app class loaders
914 Handle class_module(_thread, module_entry->module()); // Obtain j.l.r.Module
915 JvmtiExport::add_default_read_edges(class_module, _thread);
916 }
917 }
918 }
919 // Clear class_being_redefined flag here. The action
920 // from agent handler could generate a new class file load
921 // hook event and if it is not cleared the new event generated
922 // from regular class file load could have this stale redefined
923 // class handle info.
924 _state->clear_class_being_redefined();
925 } else {
926 // redefine and retransform will always set the thread state
927 _class_being_redefined = NULL;
928 _load_kind = jvmti_class_load_kind_load;
929 }
930 }
931
post()932 void post() {
933 post_all_envs();
934 copy_modified_data();
935 }
936
has_been_modified()937 bool has_been_modified() { return _has_been_modified; }
938
939 private:
post_all_envs()940 void post_all_envs() {
941 if (_load_kind != jvmti_class_load_kind_retransform) {
942 // for class load and redefine,
943 // call the non-retransformable agents
944 JvmtiEnvIterator it;
945 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
946 if (!env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) {
947 // non-retransformable agents cannot retransform back,
948 // so no need to cache the original class file bytes
949 post_to_env(env, false);
950 }
951 }
952 }
953 JvmtiEnvIterator it;
954 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
955 // retransformable agents get all events
956 if (env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) {
957 // retransformable agents need to cache the original class file
958 // bytes if changes are made via the ClassFileLoadHook
959 post_to_env(env, true);
960 }
961 }
962 }
963
post_to_env(JvmtiEnv * env,bool caching_needed)964 void post_to_env(JvmtiEnv* env, bool caching_needed) {
965 if (env->phase() == JVMTI_PHASE_PRIMORDIAL && !env->early_class_hook_env()) {
966 return;
967 }
968 unsigned char *new_data = NULL;
969 jint new_len = 0;
970 JvmtiClassFileLoadEventMark jem(_thread, _h_name, _class_loader,
971 _h_protection_domain,
972 _class_being_redefined);
973 JvmtiJavaThreadEventTransition jet(_thread);
974 jvmtiEventClassFileLoadHook callback = env->callbacks()->ClassFileLoadHook;
975 if (callback != NULL) {
976 (*callback)(env->jvmti_external(), jem.jni_env(),
977 jem.class_being_redefined(),
978 jem.jloader(), jem.class_name(),
979 jem.protection_domain(),
980 _curr_len, _curr_data,
981 &new_len, &new_data);
982 }
983 if (new_data != NULL) {
984 // this agent has modified class data.
985 _has_been_modified = true;
986 if (caching_needed && *_cached_class_file_ptr == NULL) {
987 // data has been changed by the new retransformable agent
988 // and it hasn't already been cached, cache it
989 JvmtiCachedClassFileData *p;
990 p = (JvmtiCachedClassFileData *)os::malloc(
991 offset_of(JvmtiCachedClassFileData, data) + _curr_len, mtInternal);
992 if (p == NULL) {
993 vm_exit_out_of_memory(offset_of(JvmtiCachedClassFileData, data) + _curr_len,
994 OOM_MALLOC_ERROR,
995 "unable to allocate cached copy of original class bytes");
996 }
997 p->length = _curr_len;
998 memcpy(p->data, _curr_data, _curr_len);
999 *_cached_class_file_ptr = p;
1000 }
1001
1002 if (_curr_data != *_data_ptr) {
1003 // curr_data is previous agent modified class data.
1004 // And this has been changed by the new agent so
1005 // we can delete it now.
1006 _curr_env->Deallocate(_curr_data);
1007 }
1008
1009 // Class file data has changed by the current agent.
1010 _curr_data = new_data;
1011 _curr_len = new_len;
1012 // Save the current agent env we need this to deallocate the
1013 // memory allocated by this agent.
1014 _curr_env = env;
1015 }
1016 }
1017
copy_modified_data()1018 void copy_modified_data() {
1019 // if one of the agent has modified class file data.
1020 // Copy modified class data to new resources array.
1021 if (_curr_data != *_data_ptr) {
1022 *_data_ptr = NEW_RESOURCE_ARRAY(u1, _curr_len);
1023 memcpy(*_data_ptr, _curr_data, _curr_len);
1024 *_end_ptr = *_data_ptr + _curr_len;
1025 _curr_env->Deallocate(_curr_data);
1026 }
1027 }
1028 };
1029
is_early_phase()1030 bool JvmtiExport::is_early_phase() {
1031 return JvmtiEnvBase::get_phase() <= JVMTI_PHASE_PRIMORDIAL;
1032 }
1033
has_early_class_hook_env()1034 bool JvmtiExport::has_early_class_hook_env() {
1035 JvmtiEnvIterator it;
1036 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1037 if (env->early_class_hook_env()) {
1038 return true;
1039 }
1040 }
1041 return false;
1042 }
1043
1044 bool JvmtiExport::_should_post_class_file_load_hook = false;
1045
1046 // this entry is for class file load hook on class load, redefine and retransform
post_class_file_load_hook(Symbol * h_name,Handle class_loader,Handle h_protection_domain,unsigned char ** data_ptr,unsigned char ** end_ptr,JvmtiCachedClassFileData ** cache_ptr)1047 bool JvmtiExport::post_class_file_load_hook(Symbol* h_name,
1048 Handle class_loader,
1049 Handle h_protection_domain,
1050 unsigned char **data_ptr,
1051 unsigned char **end_ptr,
1052 JvmtiCachedClassFileData **cache_ptr) {
1053 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1054 return false;
1055 }
1056
1057 JvmtiClassFileLoadHookPoster poster(h_name, class_loader,
1058 h_protection_domain,
1059 data_ptr, end_ptr,
1060 cache_ptr);
1061 poster.post();
1062 return poster.has_been_modified();
1063 }
1064
report_unsupported(bool on)1065 void JvmtiExport::report_unsupported(bool on) {
1066 // If any JVMTI service is turned on, we need to exit before native code
1067 // tries to access nonexistant services.
1068 if (on) {
1069 vm_exit_during_initialization("Java Kernel does not support JVMTI.");
1070 }
1071 }
1072
1073
oop_to_klass(oop obj)1074 static inline Klass* oop_to_klass(oop obj) {
1075 Klass* k = obj->klass();
1076
1077 // if the object is a java.lang.Class then return the java mirror
1078 if (k == vmClasses::Class_klass()) {
1079 if (!java_lang_Class::is_primitive(obj)) {
1080 k = java_lang_Class::as_Klass(obj);
1081 assert(k != NULL, "class for non-primitive mirror must exist");
1082 }
1083 }
1084 return k;
1085 }
1086
1087 class JvmtiObjectAllocEventMark : public JvmtiClassEventMark {
1088 private:
1089 jobject _jobj;
1090 jlong _size;
1091 public:
JvmtiObjectAllocEventMark(JavaThread * thread,oop obj)1092 JvmtiObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) {
1093 _jobj = (jobject)to_jobject(obj);
1094 _size = obj->size() * wordSize;
1095 };
jni_jobject()1096 jobject jni_jobject() { return _jobj; }
size()1097 jlong size() { return _size; }
1098 };
1099
1100 class JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark {
1101 private:
1102 jint _code_size;
1103 const void *_code_data;
1104 jint _map_length;
1105 jvmtiAddrLocationMap *_map;
1106 const void *_compile_info;
1107 public:
JvmtiCompiledMethodLoadEventMark(JavaThread * thread,nmethod * nm,void * compile_info_ptr=NULL)1108 JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL)
1109 : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) {
1110 _code_data = nm->code_begin();
1111 _code_size = nm->code_size();
1112 _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL.
1113 JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &_map, &_map_length);
1114 }
~JvmtiCompiledMethodLoadEventMark()1115 ~JvmtiCompiledMethodLoadEventMark() {
1116 FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, _map);
1117 }
1118
code_size()1119 jint code_size() { return _code_size; }
code_data()1120 const void *code_data() { return _code_data; }
map_length()1121 jint map_length() { return _map_length; }
map()1122 const jvmtiAddrLocationMap* map() { return _map; }
compile_info()1123 const void *compile_info() { return _compile_info; }
1124 };
1125
1126
1127
1128 class JvmtiMonitorEventMark : public JvmtiThreadEventMark {
1129 private:
1130 jobject _jobj;
1131 public:
JvmtiMonitorEventMark(JavaThread * thread,oop object)1132 JvmtiMonitorEventMark(JavaThread *thread, oop object)
1133 : JvmtiThreadEventMark(thread){
1134 _jobj = to_jobject(object);
1135 }
jni_object()1136 jobject jni_object() { return _jobj; }
1137 };
1138
1139 ///////////////////////////////////////////////////////////////
1140 //
1141 // pending CompiledMethodUnload support
1142 //
1143
post_compiled_method_unload(jmethodID method,const void * code_begin)1144 void JvmtiExport::post_compiled_method_unload(
1145 jmethodID method, const void *code_begin) {
1146 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1147 return;
1148 }
1149 JavaThread* thread = JavaThread::current();
1150 EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
1151 ("[%s] method compile unload event triggered",
1152 JvmtiTrace::safe_get_thread_name(thread)));
1153
1154 // post the event for each environment that has this event enabled.
1155 JvmtiEnvIterator it;
1156 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1157 if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) {
1158 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1159 continue;
1160 }
1161 EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
1162 ("[%s] class compile method unload event sent jmethodID " PTR_FORMAT,
1163 JvmtiTrace::safe_get_thread_name(thread), p2i(method)));
1164
1165 ResourceMark rm(thread);
1166
1167 JvmtiEventMark jem(thread);
1168 JvmtiJavaThreadEventTransition jet(thread);
1169 jvmtiEventCompiledMethodUnload callback = env->callbacks()->CompiledMethodUnload;
1170 if (callback != NULL) {
1171 (*callback)(env->jvmti_external(), method, code_begin);
1172 }
1173 }
1174 }
1175 }
1176
1177 ///////////////////////////////////////////////////////////////
1178 //
1179 // JvmtiExport
1180 //
1181
post_raw_breakpoint(JavaThread * thread,Method * method,address location)1182 void JvmtiExport::post_raw_breakpoint(JavaThread *thread, Method* method, address location) {
1183 HandleMark hm(thread);
1184 methodHandle mh(thread, method);
1185
1186 JvmtiThreadState *state = thread->jvmti_thread_state();
1187 if (state == NULL) {
1188 return;
1189 }
1190 EVT_TRIG_TRACE(JVMTI_EVENT_BREAKPOINT, ("[%s] Trg Breakpoint triggered",
1191 JvmtiTrace::safe_get_thread_name(thread)));
1192 JvmtiEnvThreadStateIterator it(state);
1193 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1194 ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_BREAKPOINT);
1195 if (!ets->breakpoint_posted() && ets->is_enabled(JVMTI_EVENT_BREAKPOINT)) {
1196 ThreadState old_os_state = thread->osthread()->get_state();
1197 thread->osthread()->set_state(BREAKPOINTED);
1198 EVT_TRACE(JVMTI_EVENT_BREAKPOINT, ("[%s] Evt Breakpoint sent %s.%s @ " INTX_FORMAT,
1199 JvmtiTrace::safe_get_thread_name(thread),
1200 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1201 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1202 location - mh()->code_base() ));
1203
1204 JvmtiEnv *env = ets->get_env();
1205 JvmtiLocationEventMark jem(thread, mh, location);
1206 JvmtiJavaThreadEventTransition jet(thread);
1207 jvmtiEventBreakpoint callback = env->callbacks()->Breakpoint;
1208 if (callback != NULL) {
1209 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1210 jem.jni_methodID(), jem.location());
1211 }
1212
1213 ets->set_breakpoint_posted();
1214 thread->osthread()->set_state(old_os_state);
1215 }
1216 }
1217 }
1218
1219 //////////////////////////////////////////////////////////////////////////////
1220
1221 bool JvmtiExport::_can_get_source_debug_extension = false;
1222 bool JvmtiExport::_can_maintain_original_method_order = false;
1223 bool JvmtiExport::_can_post_interpreter_events = false;
1224 bool JvmtiExport::_can_post_on_exceptions = false;
1225 bool JvmtiExport::_can_post_breakpoint = false;
1226 bool JvmtiExport::_can_post_field_access = false;
1227 bool JvmtiExport::_can_post_field_modification = false;
1228 bool JvmtiExport::_can_post_method_entry = false;
1229 bool JvmtiExport::_can_post_method_exit = false;
1230 bool JvmtiExport::_can_pop_frame = false;
1231 bool JvmtiExport::_can_force_early_return = false;
1232 bool JvmtiExport::_can_get_owned_monitor_info = false;
1233
1234 bool JvmtiExport::_early_vmstart_recorded = false;
1235
1236 bool JvmtiExport::_should_post_single_step = false;
1237 bool JvmtiExport::_should_post_field_access = false;
1238 bool JvmtiExport::_should_post_field_modification = false;
1239 bool JvmtiExport::_should_post_class_load = false;
1240 bool JvmtiExport::_should_post_class_prepare = false;
1241 bool JvmtiExport::_should_post_class_unload = false;
1242 bool JvmtiExport::_should_post_thread_life = false;
1243 bool JvmtiExport::_should_clean_up_heap_objects = false;
1244 bool JvmtiExport::_should_post_native_method_bind = false;
1245 bool JvmtiExport::_should_post_dynamic_code_generated = false;
1246 bool JvmtiExport::_should_post_data_dump = false;
1247 bool JvmtiExport::_should_post_compiled_method_load = false;
1248 bool JvmtiExport::_should_post_compiled_method_unload = false;
1249 bool JvmtiExport::_should_post_monitor_contended_enter = false;
1250 bool JvmtiExport::_should_post_monitor_contended_entered = false;
1251 bool JvmtiExport::_should_post_monitor_wait = false;
1252 bool JvmtiExport::_should_post_monitor_waited = false;
1253 bool JvmtiExport::_should_post_garbage_collection_start = false;
1254 bool JvmtiExport::_should_post_garbage_collection_finish = false;
1255 bool JvmtiExport::_should_post_object_free = false;
1256 bool JvmtiExport::_should_post_resource_exhausted = false;
1257 bool JvmtiExport::_should_post_vm_object_alloc = false;
1258 bool JvmtiExport::_should_post_sampled_object_alloc = false;
1259 bool JvmtiExport::_should_post_on_exceptions = false;
1260
1261 ////////////////////////////////////////////////////////////////////////////////////////////////
1262
1263
1264 //
1265 // JVMTI single step management
1266 //
at_single_stepping_point(JavaThread * thread,Method * method,address location)1267 void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) {
1268 assert(JvmtiExport::should_post_single_step(), "must be single stepping");
1269
1270 HandleMark hm(thread);
1271 methodHandle mh(thread, method);
1272
1273 // update information about current location and post a step event
1274 JvmtiThreadState *state = thread->jvmti_thread_state();
1275 if (state == NULL) {
1276 return;
1277 }
1278 EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Trg Single Step triggered",
1279 JvmtiTrace::safe_get_thread_name(thread)));
1280 if (!state->hide_single_stepping()) {
1281 if (state->is_pending_step_for_popframe()) {
1282 state->process_pending_step_for_popframe();
1283 }
1284 if (state->is_pending_step_for_earlyret()) {
1285 state->process_pending_step_for_earlyret();
1286 }
1287 JvmtiExport::post_single_step(thread, mh(), location);
1288 }
1289 }
1290
1291
expose_single_stepping(JavaThread * thread)1292 void JvmtiExport::expose_single_stepping(JavaThread *thread) {
1293 JvmtiThreadState *state = thread->jvmti_thread_state();
1294 if (state != NULL) {
1295 state->clear_hide_single_stepping();
1296 }
1297 }
1298
1299
hide_single_stepping(JavaThread * thread)1300 bool JvmtiExport::hide_single_stepping(JavaThread *thread) {
1301 JvmtiThreadState *state = thread->jvmti_thread_state();
1302 if (state != NULL && state->is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
1303 state->set_hide_single_stepping();
1304 return true;
1305 } else {
1306 return false;
1307 }
1308 }
1309
post_class_load(JavaThread * thread,Klass * klass)1310 void JvmtiExport::post_class_load(JavaThread *thread, Klass* klass) {
1311 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1312 return;
1313 }
1314 HandleMark hm(thread);
1315
1316 EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_LOAD, ("[%s] Trg Class Load triggered",
1317 JvmtiTrace::safe_get_thread_name(thread)));
1318 JvmtiThreadState* state = thread->jvmti_thread_state();
1319 if (state == NULL) {
1320 return;
1321 }
1322 JvmtiEnvThreadStateIterator it(state);
1323 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1324 if (ets->is_enabled(JVMTI_EVENT_CLASS_LOAD)) {
1325 JvmtiEnv *env = ets->get_env();
1326 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1327 continue;
1328 }
1329 EVT_TRACE(JVMTI_EVENT_CLASS_LOAD, ("[%s] Evt Class Load sent %s",
1330 JvmtiTrace::safe_get_thread_name(thread),
1331 klass==NULL? "NULL" : klass->external_name() ));
1332 JvmtiClassEventMark jem(thread, klass);
1333 JvmtiJavaThreadEventTransition jet(thread);
1334 jvmtiEventClassLoad callback = env->callbacks()->ClassLoad;
1335 if (callback != NULL) {
1336 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class());
1337 }
1338 }
1339 }
1340 }
1341
1342
post_class_prepare(JavaThread * thread,Klass * klass)1343 void JvmtiExport::post_class_prepare(JavaThread *thread, Klass* klass) {
1344 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1345 return;
1346 }
1347 HandleMark hm(thread);
1348
1349 EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("[%s] Trg Class Prepare triggered",
1350 JvmtiTrace::safe_get_thread_name(thread)));
1351 JvmtiThreadState* state = thread->jvmti_thread_state();
1352 if (state == NULL) {
1353 return;
1354 }
1355 JvmtiEnvThreadStateIterator it(state);
1356 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1357 if (ets->is_enabled(JVMTI_EVENT_CLASS_PREPARE)) {
1358 JvmtiEnv *env = ets->get_env();
1359 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1360 continue;
1361 }
1362 EVT_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("[%s] Evt Class Prepare sent %s",
1363 JvmtiTrace::safe_get_thread_name(thread),
1364 klass==NULL? "NULL" : klass->external_name() ));
1365 JvmtiClassEventMark jem(thread, klass);
1366 JvmtiJavaThreadEventTransition jet(thread);
1367 jvmtiEventClassPrepare callback = env->callbacks()->ClassPrepare;
1368 if (callback != NULL) {
1369 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class());
1370 }
1371 }
1372 }
1373 }
1374
post_class_unload(Klass * klass)1375 void JvmtiExport::post_class_unload(Klass* klass) {
1376 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1377 return;
1378 }
1379
1380 // postings to the service thread so that it can perform them in a safe
1381 // context and in-order.
1382 ResourceMark rm;
1383 // JvmtiDeferredEvent copies the string.
1384 JvmtiDeferredEvent event = JvmtiDeferredEvent::class_unload_event(klass->name()->as_C_string());
1385 ServiceThread::enqueue_deferred_event(&event);
1386 }
1387
1388
post_class_unload_internal(const char * name)1389 void JvmtiExport::post_class_unload_internal(const char* name) {
1390 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1391 return;
1392 }
1393 assert(Thread::current()->is_service_thread(), "must be called from ServiceThread");
1394 JavaThread *thread = JavaThread::current();
1395 HandleMark hm(thread);
1396
1397 EVT_TRIG_TRACE(EXT_EVENT_CLASS_UNLOAD, ("[?] Trg Class Unload triggered" ));
1398 if (JvmtiEventController::is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) {
1399
1400 JvmtiEnvIterator it;
1401 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1402 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1403 continue;
1404 }
1405 if (env->is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) {
1406 EVT_TRACE(EXT_EVENT_CLASS_UNLOAD, ("[?] Evt Class Unload sent %s", name));
1407
1408 JvmtiEventMark jem(thread);
1409 JvmtiJavaThreadEventTransition jet(thread);
1410 jvmtiExtensionEvent callback = env->ext_callbacks()->ClassUnload;
1411 if (callback != NULL) {
1412 (*callback)(env->jvmti_external(), jem.jni_env(), name);
1413 }
1414 }
1415 }
1416 }
1417 }
1418
1419
post_thread_start(JavaThread * thread)1420 void JvmtiExport::post_thread_start(JavaThread *thread) {
1421 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1422 return;
1423 }
1424 assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
1425
1426 EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_START, ("[%s] Trg Thread Start event triggered",
1427 JvmtiTrace::safe_get_thread_name(thread)));
1428
1429 // do JVMTI thread initialization (if needed)
1430 JvmtiEventController::thread_started(thread);
1431
1432 // Do not post thread start event for hidden java thread.
1433 if (JvmtiEventController::is_enabled(JVMTI_EVENT_THREAD_START) &&
1434 !thread->is_hidden_from_external_view()) {
1435 JvmtiEnvIterator it;
1436 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1437 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1438 continue;
1439 }
1440 if (env->is_enabled(JVMTI_EVENT_THREAD_START)) {
1441 EVT_TRACE(JVMTI_EVENT_THREAD_START, ("[%s] Evt Thread Start event sent",
1442 JvmtiTrace::safe_get_thread_name(thread) ));
1443
1444 JvmtiThreadEventMark jem(thread);
1445 JvmtiJavaThreadEventTransition jet(thread);
1446 jvmtiEventThreadStart callback = env->callbacks()->ThreadStart;
1447 if (callback != NULL) {
1448 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1449 }
1450 }
1451 }
1452 }
1453 }
1454
1455
post_thread_end(JavaThread * thread)1456 void JvmtiExport::post_thread_end(JavaThread *thread) {
1457 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
1458 return;
1459 }
1460 EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_END, ("[%s] Trg Thread End event triggered",
1461 JvmtiTrace::safe_get_thread_name(thread)));
1462
1463 JvmtiThreadState *state = thread->jvmti_thread_state();
1464 if (state == NULL) {
1465 return;
1466 }
1467
1468 // Do not post thread end event for hidden java thread.
1469 if (state->is_enabled(JVMTI_EVENT_THREAD_END) &&
1470 !thread->is_hidden_from_external_view()) {
1471
1472 JvmtiEnvThreadStateIterator it(state);
1473 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1474 if (ets->is_enabled(JVMTI_EVENT_THREAD_END)) {
1475 JvmtiEnv *env = ets->get_env();
1476 if (env->phase() == JVMTI_PHASE_PRIMORDIAL) {
1477 continue;
1478 }
1479 EVT_TRACE(JVMTI_EVENT_THREAD_END, ("[%s] Evt Thread End event sent",
1480 JvmtiTrace::safe_get_thread_name(thread) ));
1481
1482 JvmtiThreadEventMark jem(thread);
1483 JvmtiJavaThreadEventTransition jet(thread);
1484 jvmtiEventThreadEnd callback = env->callbacks()->ThreadEnd;
1485 if (callback != NULL) {
1486 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
1487 }
1488 }
1489 }
1490 }
1491 }
1492
post_object_free(JvmtiEnv * env,jlong tag)1493 void JvmtiExport::post_object_free(JvmtiEnv* env, jlong tag) {
1494 assert(env->is_enabled(JVMTI_EVENT_OBJECT_FREE), "checking");
1495
1496 EVT_TRIG_TRACE(JVMTI_EVENT_OBJECT_FREE, ("[?] Trg Object Free triggered" ));
1497 EVT_TRACE(JVMTI_EVENT_OBJECT_FREE, ("[?] Evt Object Free sent"));
1498
1499 jvmtiEventObjectFree callback = env->callbacks()->ObjectFree;
1500 if (callback != NULL) {
1501 (*callback)(env->jvmti_external(), tag);
1502 }
1503 }
1504
post_resource_exhausted(jint resource_exhausted_flags,const char * description)1505 void JvmtiExport::post_resource_exhausted(jint resource_exhausted_flags, const char* description) {
1506
1507 JavaThread *thread = JavaThread::current();
1508
1509 log_error(jvmti)("Posting Resource Exhausted event: %s",
1510 description != nullptr ? description : "unknown");
1511
1512 // JDK-8213834: handlers of ResourceExhausted may attempt some analysis
1513 // which often requires running java.
1514 // This will cause problems on threads not able to run java, e.g. compiler
1515 // threads. To forestall these problems, we therefore suppress sending this
1516 // event from threads which are not able to run java.
1517 if (!thread->can_call_java()) {
1518 return;
1519 }
1520
1521 EVT_TRIG_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("Trg resource exhausted event triggered" ));
1522
1523 JvmtiEnvIterator it;
1524 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
1525 if (env->is_enabled(JVMTI_EVENT_RESOURCE_EXHAUSTED)) {
1526 EVT_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("Evt resource exhausted event sent" ));
1527
1528 JvmtiThreadEventMark jem(thread);
1529 JvmtiJavaThreadEventTransition jet(thread);
1530 jvmtiEventResourceExhausted callback = env->callbacks()->ResourceExhausted;
1531 if (callback != NULL) {
1532 (*callback)(env->jvmti_external(), jem.jni_env(),
1533 resource_exhausted_flags, NULL, description);
1534 }
1535 }
1536 }
1537 }
1538
post_method_entry(JavaThread * thread,Method * method,frame current_frame)1539 void JvmtiExport::post_method_entry(JavaThread *thread, Method* method, frame current_frame) {
1540 HandleMark hm(thread);
1541 methodHandle mh(thread, method);
1542
1543 EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("[%s] Trg Method Entry triggered %s.%s",
1544 JvmtiTrace::safe_get_thread_name(thread),
1545 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1546 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
1547
1548 JvmtiThreadState* state = thread->jvmti_thread_state();
1549 if (state == NULL || !state->is_interp_only_mode()) {
1550 // for any thread that actually wants method entry, interp_only_mode is set
1551 return;
1552 }
1553
1554 state->incr_cur_stack_depth();
1555
1556 if (state->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) {
1557 JvmtiEnvThreadStateIterator it(state);
1558 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1559 if (ets->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) {
1560 EVT_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("[%s] Evt Method Entry sent %s.%s",
1561 JvmtiTrace::safe_get_thread_name(thread),
1562 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1563 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
1564
1565 JvmtiEnv *env = ets->get_env();
1566 JvmtiMethodEventMark jem(thread, mh);
1567 JvmtiJavaThreadEventTransition jet(thread);
1568 jvmtiEventMethodEntry callback = env->callbacks()->MethodEntry;
1569 if (callback != NULL) {
1570 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_methodID());
1571 }
1572 }
1573 }
1574 }
1575 }
1576
post_method_exit(JavaThread * thread,Method * method,frame current_frame)1577 void JvmtiExport::post_method_exit(JavaThread* thread, Method* method, frame current_frame) {
1578 HandleMark hm(thread);
1579 methodHandle mh(thread, method);
1580
1581 JvmtiThreadState *state = thread->jvmti_thread_state();
1582
1583 if (state == NULL || !state->is_interp_only_mode()) {
1584 // for any thread that actually wants method exit, interp_only_mode is set
1585 return;
1586 }
1587
1588 // return a flag when a method terminates by throwing an exception
1589 // i.e. if an exception is thrown and it's not caught by the current method
1590 bool exception_exit = state->is_exception_detected() && !state->is_exception_caught();
1591 Handle result;
1592 jvalue value;
1593 value.j = 0L;
1594
1595 if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
1596 // if the method hasn't been popped because of an exception then we populate
1597 // the return_value parameter for the callback. At this point we only have
1598 // the address of a "raw result" and we just call into the interpreter to
1599 // convert this into a jvalue.
1600 if (!exception_exit) {
1601 oop oop_result;
1602 BasicType type = current_frame.interpreter_frame_result(&oop_result, &value);
1603 if (is_reference_type(type)) {
1604 result = Handle(thread, oop_result);
1605 value.l = JNIHandles::make_local(thread, result());
1606 }
1607 }
1608 }
1609
1610 // Deferred transition to VM, so we can stash away the return oop before GC
1611 // Note that this transition is not needed when throwing an exception, because
1612 // there is no oop to retain.
1613 JavaThread* current = thread; // for JRT_BLOCK
1614 JRT_BLOCK
1615 post_method_exit_inner(thread, mh, state, exception_exit, current_frame, value);
1616 JRT_BLOCK_END
1617
1618 if (result.not_null() && !mh->is_native()) {
1619 // We have to restore the oop on the stack for interpreter frames
1620 *(oop*)current_frame.interpreter_frame_tos_address() = result();
1621 }
1622 }
1623
post_method_exit_inner(JavaThread * thread,methodHandle & mh,JvmtiThreadState * state,bool exception_exit,frame current_frame,jvalue & value)1624 void JvmtiExport::post_method_exit_inner(JavaThread* thread,
1625 methodHandle& mh,
1626 JvmtiThreadState *state,
1627 bool exception_exit,
1628 frame current_frame,
1629 jvalue& value) {
1630 EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_EXIT, ("[%s] Trg Method Exit triggered %s.%s",
1631 JvmtiTrace::safe_get_thread_name(thread),
1632 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1633 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
1634
1635 if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
1636 JvmtiEnvThreadStateIterator it(state);
1637 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1638 if (ets->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
1639 EVT_TRACE(JVMTI_EVENT_METHOD_EXIT, ("[%s] Evt Method Exit sent %s.%s",
1640 JvmtiTrace::safe_get_thread_name(thread),
1641 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1642 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
1643
1644 JvmtiEnv *env = ets->get_env();
1645 JvmtiMethodEventMark jem(thread, mh);
1646 JvmtiJavaThreadEventTransition jet(thread);
1647 jvmtiEventMethodExit callback = env->callbacks()->MethodExit;
1648 if (callback != NULL) {
1649 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1650 jem.jni_methodID(), exception_exit, value);
1651 }
1652 }
1653 }
1654 }
1655
1656 JvmtiEnvThreadStateIterator it(state);
1657 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1658 if (ets->has_frame_pops()) {
1659 int cur_frame_number = state->cur_stack_depth();
1660
1661 if (ets->is_frame_pop(cur_frame_number)) {
1662 // we have a NotifyFramePop entry for this frame.
1663 // now check that this env/thread wants this event
1664 if (ets->is_enabled(JVMTI_EVENT_FRAME_POP)) {
1665 EVT_TRACE(JVMTI_EVENT_FRAME_POP, ("[%s] Evt Frame Pop sent %s.%s",
1666 JvmtiTrace::safe_get_thread_name(thread),
1667 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1668 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
1669
1670 // we also need to issue a frame pop event for this frame
1671 JvmtiEnv *env = ets->get_env();
1672 JvmtiMethodEventMark jem(thread, mh);
1673 JvmtiJavaThreadEventTransition jet(thread);
1674 jvmtiEventFramePop callback = env->callbacks()->FramePop;
1675 if (callback != NULL) {
1676 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1677 jem.jni_methodID(), exception_exit);
1678 }
1679 }
1680 // remove the frame's entry
1681 {
1682 MutexLocker mu(JvmtiThreadState_lock);
1683 ets->clear_frame_pop(cur_frame_number);
1684 }
1685 }
1686 }
1687 }
1688
1689 state->decr_cur_stack_depth();
1690 }
1691
1692
1693 // Todo: inline this for optimization
post_single_step(JavaThread * thread,Method * method,address location)1694 void JvmtiExport::post_single_step(JavaThread *thread, Method* method, address location) {
1695 HandleMark hm(thread);
1696 methodHandle mh(thread, method);
1697
1698 JvmtiThreadState *state = thread->jvmti_thread_state();
1699 if (state == NULL) {
1700 return;
1701 }
1702 JvmtiEnvThreadStateIterator it(state);
1703 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1704 ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_SINGLE_STEP);
1705 if (!ets->single_stepping_posted() && ets->is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
1706 EVT_TRACE(JVMTI_EVENT_SINGLE_STEP, ("[%s] Evt Single Step sent %s.%s @ " INTX_FORMAT,
1707 JvmtiTrace::safe_get_thread_name(thread),
1708 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1709 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1710 location - mh()->code_base() ));
1711
1712 JvmtiEnv *env = ets->get_env();
1713 JvmtiLocationEventMark jem(thread, mh, location);
1714 JvmtiJavaThreadEventTransition jet(thread);
1715 jvmtiEventSingleStep callback = env->callbacks()->SingleStep;
1716 if (callback != NULL) {
1717 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1718 jem.jni_methodID(), jem.location());
1719 }
1720
1721 ets->set_single_stepping_posted();
1722 }
1723 }
1724 }
1725
post_exception_throw(JavaThread * thread,Method * method,address location,oop exception)1726 void JvmtiExport::post_exception_throw(JavaThread *thread, Method* method, address location, oop exception) {
1727 HandleMark hm(thread);
1728 methodHandle mh(thread, method);
1729 Handle exception_handle(thread, exception);
1730
1731 JvmtiThreadState *state = thread->jvmti_thread_state();
1732 if (state == NULL) {
1733 return;
1734 }
1735
1736 EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION, ("[%s] Trg Exception thrown triggered",
1737 JvmtiTrace::safe_get_thread_name(thread)));
1738 if (!state->is_exception_detected()) {
1739 state->set_exception_detected();
1740 JvmtiEnvThreadStateIterator it(state);
1741 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1742 if (ets->is_enabled(JVMTI_EVENT_EXCEPTION) && (exception != NULL)) {
1743
1744 EVT_TRACE(JVMTI_EVENT_EXCEPTION,
1745 ("[%s] Evt Exception thrown sent %s.%s @ " INTX_FORMAT,
1746 JvmtiTrace::safe_get_thread_name(thread),
1747 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1748 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1749 location - mh()->code_base() ));
1750
1751 JvmtiEnv *env = ets->get_env();
1752 JvmtiExceptionEventMark jem(thread, mh, location, exception_handle);
1753
1754 // It's okay to clear these exceptions here because we duplicate
1755 // this lookup in InterpreterRuntime::exception_handler_for_exception.
1756 EXCEPTION_MARK;
1757
1758 bool should_repeat;
1759 vframeStream st(thread);
1760 assert(!st.at_end(), "cannot be at end");
1761 Method* current_method = NULL;
1762 // A GC may occur during the Method::fast_exception_handler_bci_for()
1763 // call below if it needs to load the constraint class. Using a
1764 // methodHandle to keep the 'current_method' from being deallocated
1765 // if GC happens.
1766 methodHandle current_mh = methodHandle(thread, current_method);
1767 int current_bci = -1;
1768 do {
1769 current_method = st.method();
1770 current_mh = methodHandle(thread, current_method);
1771 current_bci = st.bci();
1772 do {
1773 should_repeat = false;
1774 Klass* eh_klass = exception_handle()->klass();
1775 current_bci = Method::fast_exception_handler_bci_for(
1776 current_mh, eh_klass, current_bci, THREAD);
1777 if (HAS_PENDING_EXCEPTION) {
1778 exception_handle = Handle(thread, PENDING_EXCEPTION);
1779 CLEAR_PENDING_EXCEPTION;
1780 should_repeat = true;
1781 }
1782 } while (should_repeat && (current_bci != -1));
1783 st.next();
1784 } while ((current_bci < 0) && (!st.at_end()));
1785
1786 jmethodID catch_jmethodID;
1787 if (current_bci < 0) {
1788 catch_jmethodID = 0;
1789 current_bci = 0;
1790 } else {
1791 catch_jmethodID = jem.to_jmethodID(current_mh);
1792 }
1793
1794 JvmtiJavaThreadEventTransition jet(thread);
1795 jvmtiEventException callback = env->callbacks()->Exception;
1796 if (callback != NULL) {
1797 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1798 jem.jni_methodID(), jem.location(),
1799 jem.exception(),
1800 catch_jmethodID, current_bci);
1801 }
1802 }
1803 }
1804 }
1805
1806 // frames may get popped because of this throw, be safe - invalidate cached depth
1807 state->invalidate_cur_stack_depth();
1808 }
1809
1810
notice_unwind_due_to_exception(JavaThread * thread,Method * method,address location,oop exception,bool in_handler_frame)1811 void JvmtiExport::notice_unwind_due_to_exception(JavaThread *thread, Method* method, address location, oop exception, bool in_handler_frame) {
1812 HandleMark hm(thread);
1813 methodHandle mh(thread, method);
1814 Handle exception_handle(thread, exception);
1815
1816 JvmtiThreadState *state = thread->jvmti_thread_state();
1817 if (state == NULL) {
1818 return;
1819 }
1820 EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION_CATCH,
1821 ("[%s] Trg unwind_due_to_exception triggered %s.%s @ %s" INTX_FORMAT " - %s",
1822 JvmtiTrace::safe_get_thread_name(thread),
1823 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1824 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1825 location==0? "no location:" : "",
1826 location==0? 0 : location - mh()->code_base(),
1827 in_handler_frame? "in handler frame" : "not handler frame" ));
1828
1829 if (state->is_exception_detected()) {
1830
1831 state->invalidate_cur_stack_depth();
1832 if (!in_handler_frame) {
1833 // Not in exception handler.
1834 if(state->is_interp_only_mode()) {
1835 // method exit and frame pop events are posted only in interp mode.
1836 // When these events are enabled code should be in running in interp mode.
1837 jvalue no_value;
1838 no_value.j = 0L;
1839 JvmtiExport::post_method_exit_inner(thread, mh, state, true, thread->last_frame(), no_value);
1840 // The cached cur_stack_depth might have changed from the
1841 // operations of frame pop or method exit. We are not 100% sure
1842 // the cached cur_stack_depth is still valid depth so invalidate
1843 // it.
1844 state->invalidate_cur_stack_depth();
1845 }
1846 } else {
1847 // In exception handler frame. Report exception catch.
1848 assert(location != NULL, "must be a known location");
1849 // Update cur_stack_depth - the frames above the current frame
1850 // have been unwound due to this exception:
1851 assert(!state->is_exception_caught(), "exception must not be caught yet.");
1852 state->set_exception_caught();
1853
1854 JvmtiEnvThreadStateIterator it(state);
1855 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1856 if (ets->is_enabled(JVMTI_EVENT_EXCEPTION_CATCH) && (exception_handle() != NULL)) {
1857 EVT_TRACE(JVMTI_EVENT_EXCEPTION_CATCH,
1858 ("[%s] Evt ExceptionCatch sent %s.%s @ " INTX_FORMAT,
1859 JvmtiTrace::safe_get_thread_name(thread),
1860 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1861 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1862 location - mh()->code_base() ));
1863
1864 JvmtiEnv *env = ets->get_env();
1865 JvmtiExceptionEventMark jem(thread, mh, location, exception_handle);
1866 JvmtiJavaThreadEventTransition jet(thread);
1867 jvmtiEventExceptionCatch callback = env->callbacks()->ExceptionCatch;
1868 if (callback != NULL) {
1869 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1870 jem.jni_methodID(), jem.location(),
1871 jem.exception());
1872 }
1873 }
1874 }
1875 }
1876 }
1877 }
1878
jni_GetField_probe(JavaThread * thread,jobject jobj,oop obj,Klass * klass,jfieldID fieldID,bool is_static)1879 oop JvmtiExport::jni_GetField_probe(JavaThread *thread, jobject jobj, oop obj,
1880 Klass* klass, jfieldID fieldID, bool is_static) {
1881 if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) {
1882 // At least one field access watch is set so we have more work to do.
1883 post_field_access_by_jni(thread, obj, klass, fieldID, is_static);
1884 // event posting can block so refetch oop if we were passed a jobj
1885 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj);
1886 }
1887 return obj;
1888 }
1889
post_field_access_by_jni(JavaThread * thread,oop obj,Klass * klass,jfieldID fieldID,bool is_static)1890 void JvmtiExport::post_field_access_by_jni(JavaThread *thread, oop obj,
1891 Klass* klass, jfieldID fieldID, bool is_static) {
1892 // We must be called with a Java context in order to provide reasonable
1893 // values for the klazz, method, and location fields. The callers of this
1894 // function don't make the call unless there is a Java context.
1895 assert(thread->has_last_Java_frame(), "must be called with a Java context");
1896
1897 ResourceMark rm;
1898 fieldDescriptor fd;
1899 // if get_field_descriptor finds fieldID to be invalid, then we just bail
1900 bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd);
1901 assert(valid_fieldID == true,"post_field_access_by_jni called with invalid fieldID");
1902 if (!valid_fieldID) return;
1903 // field accesses are not watched so bail
1904 if (!fd.is_field_access_watched()) return;
1905
1906 HandleMark hm(thread);
1907 Handle h_obj;
1908 if (!is_static) {
1909 // non-static field accessors have an object, but we need a handle
1910 assert(obj != NULL, "non-static needs an object");
1911 h_obj = Handle(thread, obj);
1912 }
1913 post_field_access(thread,
1914 thread->last_frame().interpreter_frame_method(),
1915 thread->last_frame().interpreter_frame_bcp(),
1916 klass, h_obj, fieldID);
1917 }
1918
post_field_access(JavaThread * thread,Method * method,address location,Klass * field_klass,Handle object,jfieldID field)1919 void JvmtiExport::post_field_access(JavaThread *thread, Method* method,
1920 address location, Klass* field_klass, Handle object, jfieldID field) {
1921
1922 HandleMark hm(thread);
1923 methodHandle mh(thread, method);
1924
1925 JvmtiThreadState *state = thread->jvmti_thread_state();
1926 if (state == NULL) {
1927 return;
1928 }
1929 EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("[%s] Trg Field Access event triggered",
1930 JvmtiTrace::safe_get_thread_name(thread)));
1931 JvmtiEnvThreadStateIterator it(state);
1932 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
1933 if (ets->is_enabled(JVMTI_EVENT_FIELD_ACCESS)) {
1934 EVT_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("[%s] Evt Field Access event sent %s.%s @ " INTX_FORMAT,
1935 JvmtiTrace::safe_get_thread_name(thread),
1936 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
1937 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
1938 location - mh()->code_base() ));
1939
1940 JvmtiEnv *env = ets->get_env();
1941 JvmtiLocationEventMark jem(thread, mh, location);
1942 jclass field_jclass = jem.to_jclass(field_klass);
1943 jobject field_jobject = jem.to_jobject(object());
1944 JvmtiJavaThreadEventTransition jet(thread);
1945 jvmtiEventFieldAccess callback = env->callbacks()->FieldAccess;
1946 if (callback != NULL) {
1947 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
1948 jem.jni_methodID(), jem.location(),
1949 field_jclass, field_jobject, field);
1950 }
1951 }
1952 }
1953 }
1954
jni_SetField_probe(JavaThread * thread,jobject jobj,oop obj,Klass * klass,jfieldID fieldID,bool is_static,char sig_type,jvalue * value)1955 oop JvmtiExport::jni_SetField_probe(JavaThread *thread, jobject jobj, oop obj,
1956 Klass* klass, jfieldID fieldID, bool is_static,
1957 char sig_type, jvalue *value) {
1958 if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) {
1959 // At least one field modification watch is set so we have more work to do.
1960 post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value);
1961 // event posting can block so refetch oop if we were passed a jobj
1962 if (jobj != NULL) return JNIHandles::resolve_non_null(jobj);
1963 }
1964 return obj;
1965 }
1966
post_field_modification_by_jni(JavaThread * thread,oop obj,Klass * klass,jfieldID fieldID,bool is_static,char sig_type,jvalue * value)1967 void JvmtiExport::post_field_modification_by_jni(JavaThread *thread, oop obj,
1968 Klass* klass, jfieldID fieldID, bool is_static,
1969 char sig_type, jvalue *value) {
1970 // We must be called with a Java context in order to provide reasonable
1971 // values for the klazz, method, and location fields. The callers of this
1972 // function don't make the call unless there is a Java context.
1973 assert(thread->has_last_Java_frame(), "must be called with Java context");
1974
1975 ResourceMark rm;
1976 fieldDescriptor fd;
1977 // if get_field_descriptor finds fieldID to be invalid, then we just bail
1978 bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd);
1979 assert(valid_fieldID == true,"post_field_modification_by_jni called with invalid fieldID");
1980 if (!valid_fieldID) return;
1981 // field modifications are not watched so bail
1982 if (!fd.is_field_modification_watched()) return;
1983
1984 HandleMark hm(thread);
1985
1986 Handle h_obj;
1987 if (!is_static) {
1988 // non-static field accessors have an object, but we need a handle
1989 assert(obj != NULL, "non-static needs an object");
1990 h_obj = Handle(thread, obj);
1991 }
1992 post_field_modification(thread,
1993 thread->last_frame().interpreter_frame_method(),
1994 thread->last_frame().interpreter_frame_bcp(),
1995 klass, h_obj, fieldID, sig_type, value);
1996 }
1997
post_raw_field_modification(JavaThread * thread,Method * method,address location,Klass * field_klass,Handle object,jfieldID field,char sig_type,jvalue * value)1998 void JvmtiExport::post_raw_field_modification(JavaThread *thread, Method* method,
1999 address location, Klass* field_klass, Handle object, jfieldID field,
2000 char sig_type, jvalue *value) {
2001
2002 if (sig_type == JVM_SIGNATURE_INT || sig_type == JVM_SIGNATURE_BOOLEAN ||
2003 sig_type == JVM_SIGNATURE_BYTE || sig_type == JVM_SIGNATURE_CHAR ||
2004 sig_type == JVM_SIGNATURE_SHORT) {
2005 // 'I' instructions are used for byte, char, short and int.
2006 // determine which it really is, and convert
2007 fieldDescriptor fd;
2008 bool found = JvmtiEnv::get_field_descriptor(field_klass, field, &fd);
2009 // should be found (if not, leave as is)
2010 if (found) {
2011 jint ival = value->i;
2012 // convert value from int to appropriate type
2013 switch (fd.field_type()) {
2014 case T_BOOLEAN:
2015 sig_type = JVM_SIGNATURE_BOOLEAN;
2016 value->i = 0; // clear it
2017 value->z = (jboolean)ival;
2018 break;
2019 case T_BYTE:
2020 sig_type = JVM_SIGNATURE_BYTE;
2021 value->i = 0; // clear it
2022 value->b = (jbyte)ival;
2023 break;
2024 case T_CHAR:
2025 sig_type = JVM_SIGNATURE_CHAR;
2026 value->i = 0; // clear it
2027 value->c = (jchar)ival;
2028 break;
2029 case T_SHORT:
2030 sig_type = JVM_SIGNATURE_SHORT;
2031 value->i = 0; // clear it
2032 value->s = (jshort)ival;
2033 break;
2034 case T_INT:
2035 // nothing to do
2036 break;
2037 default:
2038 // this is an integer instruction, should be one of above
2039 ShouldNotReachHere();
2040 break;
2041 }
2042 }
2043 }
2044
2045 assert(sig_type != JVM_SIGNATURE_ARRAY, "array should have sig_type == 'L'");
2046 bool handle_created = false;
2047
2048 // convert oop to JNI handle.
2049 if (sig_type == JVM_SIGNATURE_CLASS) {
2050 handle_created = true;
2051 value->l = (jobject)JNIHandles::make_local(thread, cast_to_oop(value->l));
2052 }
2053
2054 post_field_modification(thread, method, location, field_klass, object, field, sig_type, value);
2055
2056 // Destroy the JNI handle allocated above.
2057 if (handle_created) {
2058 JNIHandles::destroy_local(value->l);
2059 }
2060 }
2061
post_field_modification(JavaThread * thread,Method * method,address location,Klass * field_klass,Handle object,jfieldID field,char sig_type,jvalue * value_ptr)2062 void JvmtiExport::post_field_modification(JavaThread *thread, Method* method,
2063 address location, Klass* field_klass, Handle object, jfieldID field,
2064 char sig_type, jvalue *value_ptr) {
2065
2066 HandleMark hm(thread);
2067 methodHandle mh(thread, method);
2068
2069 JvmtiThreadState *state = thread->jvmti_thread_state();
2070 if (state == NULL) {
2071 return;
2072 }
2073 EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_MODIFICATION,
2074 ("[%s] Trg Field Modification event triggered",
2075 JvmtiTrace::safe_get_thread_name(thread)));
2076
2077 JvmtiEnvThreadStateIterator it(state);
2078 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2079 if (ets->is_enabled(JVMTI_EVENT_FIELD_MODIFICATION)) {
2080 EVT_TRACE(JVMTI_EVENT_FIELD_MODIFICATION,
2081 ("[%s] Evt Field Modification event sent %s.%s @ " INTX_FORMAT,
2082 JvmtiTrace::safe_get_thread_name(thread),
2083 (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
2084 (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
2085 location - mh()->code_base() ));
2086
2087 JvmtiEnv *env = ets->get_env();
2088 JvmtiLocationEventMark jem(thread, mh, location);
2089 jclass field_jclass = jem.to_jclass(field_klass);
2090 jobject field_jobject = jem.to_jobject(object());
2091 JvmtiJavaThreadEventTransition jet(thread);
2092 jvmtiEventFieldModification callback = env->callbacks()->FieldModification;
2093 if (callback != NULL) {
2094 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2095 jem.jni_methodID(), jem.location(),
2096 field_jclass, field_jobject, field, sig_type, *value_ptr);
2097 }
2098 }
2099 }
2100 }
2101
post_native_method_bind(Method * method,address * function_ptr)2102 void JvmtiExport::post_native_method_bind(Method* method, address* function_ptr) {
2103 JavaThread* thread = JavaThread::current();
2104 assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
2105
2106 HandleMark hm(thread);
2107 methodHandle mh(thread, method);
2108
2109 EVT_TRIG_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("[%s] Trg Native Method Bind event triggered",
2110 JvmtiTrace::safe_get_thread_name(thread)));
2111
2112 if (JvmtiEventController::is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) {
2113 JvmtiEnvIterator it;
2114 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2115 if (env->is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) {
2116 EVT_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("[%s] Evt Native Method Bind event sent",
2117 JvmtiTrace::safe_get_thread_name(thread) ));
2118
2119 JvmtiMethodEventMark jem(thread, mh);
2120 JvmtiJavaThreadEventTransition jet(thread);
2121 JNIEnv* jni_env = (env->phase() == JVMTI_PHASE_PRIMORDIAL) ? NULL : jem.jni_env();
2122 jvmtiEventNativeMethodBind callback = env->callbacks()->NativeMethodBind;
2123 if (callback != NULL) {
2124 (*callback)(env->jvmti_external(), jni_env, jem.jni_thread(),
2125 jem.jni_methodID(), (void*)(*function_ptr), (void**)function_ptr);
2126 }
2127 }
2128 }
2129 }
2130 }
2131
2132 // Returns a record containing inlining information for the given nmethod
create_inline_record(nmethod * nm)2133 jvmtiCompiledMethodLoadInlineRecord* create_inline_record(nmethod* nm) {
2134 jint numstackframes = 0;
2135 jvmtiCompiledMethodLoadInlineRecord* record = (jvmtiCompiledMethodLoadInlineRecord*)NEW_RESOURCE_OBJ(jvmtiCompiledMethodLoadInlineRecord);
2136 record->header.kind = JVMTI_CMLR_INLINE_INFO;
2137 record->header.next = NULL;
2138 record->header.majorinfoversion = JVMTI_CMLR_MAJOR_VERSION_1;
2139 record->header.minorinfoversion = JVMTI_CMLR_MINOR_VERSION_0;
2140 record->numpcs = 0;
2141 for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) {
2142 if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue;
2143 record->numpcs++;
2144 }
2145 record->pcinfo = (PCStackInfo*)(NEW_RESOURCE_ARRAY(PCStackInfo, record->numpcs));
2146 int scope = 0;
2147 for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) {
2148 if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue;
2149 void* pc_address = (void*)p->real_pc(nm);
2150 assert(pc_address != NULL, "pc_address must be non-null");
2151 record->pcinfo[scope].pc = pc_address;
2152 numstackframes=0;
2153 for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) {
2154 numstackframes++;
2155 }
2156 assert(numstackframes != 0, "numstackframes must be nonzero.");
2157 record->pcinfo[scope].methods = (jmethodID *)NEW_RESOURCE_ARRAY(jmethodID, numstackframes);
2158 record->pcinfo[scope].bcis = (jint *)NEW_RESOURCE_ARRAY(jint, numstackframes);
2159 record->pcinfo[scope].numstackframes = numstackframes;
2160 int stackframe = 0;
2161 for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) {
2162 // sd->method() can be NULL for stubs but not for nmethods. To be completely robust, include an assert that we should never see a null sd->method()
2163 guarantee(sd->method() != NULL, "sd->method() cannot be null.");
2164 record->pcinfo[scope].methods[stackframe] = sd->method()->jmethod_id();
2165 record->pcinfo[scope].bcis[stackframe] = sd->bci();
2166 stackframe++;
2167 }
2168 scope++;
2169 }
2170 return record;
2171 }
2172
post_compiled_method_load(nmethod * nm)2173 void JvmtiExport::post_compiled_method_load(nmethod *nm) {
2174 guarantee(!nm->is_unloading(), "nmethod isn't unloaded or unloading");
2175 if (JvmtiEnv::get_phase() < JVMTI_PHASE_PRIMORDIAL) {
2176 return;
2177 }
2178 JavaThread* thread = JavaThread::current();
2179
2180 EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
2181 ("[%s] method compile load event triggered",
2182 JvmtiTrace::safe_get_thread_name(thread)));
2183
2184 JvmtiEnvIterator it;
2185 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2186 post_compiled_method_load(env, nm);
2187 }
2188 }
2189
2190 // post a COMPILED_METHOD_LOAD event for a given environment
post_compiled_method_load(JvmtiEnv * env,nmethod * nm)2191 void JvmtiExport::post_compiled_method_load(JvmtiEnv* env, nmethod *nm) {
2192 if (env->phase() == JVMTI_PHASE_PRIMORDIAL || !env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) {
2193 return;
2194 }
2195 jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad;
2196 if (callback == NULL) {
2197 return;
2198 }
2199 JavaThread* thread = JavaThread::current();
2200
2201 EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
2202 ("[%s] method compile load event sent %s.%s ",
2203 JvmtiTrace::safe_get_thread_name(thread),
2204 (nm->method() == NULL) ? "NULL" : nm->method()->klass_name()->as_C_string(),
2205 (nm->method() == NULL) ? "NULL" : nm->method()->name()->as_C_string()));
2206 ResourceMark rm(thread);
2207 HandleMark hm(thread);
2208
2209 // Add inlining information
2210 jvmtiCompiledMethodLoadInlineRecord* inlinerecord = create_inline_record(nm);
2211 // Pass inlining information through the void pointer
2212 JvmtiCompiledMethodLoadEventMark jem(thread, nm, inlinerecord);
2213 JvmtiJavaThreadEventTransition jet(thread);
2214 (*callback)(env->jvmti_external(), jem.jni_methodID(),
2215 jem.code_size(), jem.code_data(), jem.map_length(),
2216 jem.map(), jem.compile_info());
2217 }
2218
post_dynamic_code_generated_internal(const char * name,const void * code_begin,const void * code_end)2219 void JvmtiExport::post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) {
2220 assert(name != NULL && name[0] != '\0', "sanity check");
2221
2222 JavaThread* thread = JavaThread::current();
2223 // In theory everyone coming thru here is in_vm but we need to be certain
2224 // because a callee will do a vm->native transition
2225 ThreadInVMfromUnknown __tiv;
2226
2227 EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
2228 ("[%s] method dynamic code generated event triggered",
2229 JvmtiTrace::safe_get_thread_name(thread)));
2230 JvmtiEnvIterator it;
2231 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2232 if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) {
2233 EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
2234 ("[%s] dynamic code generated event sent for %s",
2235 JvmtiTrace::safe_get_thread_name(thread), name));
2236 JvmtiEventMark jem(thread);
2237 JvmtiJavaThreadEventTransition jet(thread);
2238 jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char));
2239 jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated;
2240 if (callback != NULL) {
2241 (*callback)(env->jvmti_external(), name, (void*)code_begin, length);
2242 }
2243 }
2244 }
2245 }
2246
post_dynamic_code_generated(const char * name,const void * code_begin,const void * code_end)2247 void JvmtiExport::post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) {
2248 jvmtiPhase phase = JvmtiEnv::get_phase();
2249 if (phase == JVMTI_PHASE_PRIMORDIAL || phase == JVMTI_PHASE_START) {
2250 post_dynamic_code_generated_internal(name, code_begin, code_end);
2251 } else {
2252 // It may not be safe to post the event from this thread. Defer all
2253 // postings to the service thread so that it can perform them in a safe
2254 // context and in-order.
2255 JvmtiDeferredEvent event = JvmtiDeferredEvent::dynamic_code_generated_event(
2256 name, code_begin, code_end);
2257 ServiceThread::enqueue_deferred_event(&event);
2258 }
2259 }
2260
2261
2262 // post a DYNAMIC_CODE_GENERATED event for a given environment
2263 // used by GenerateEvents
post_dynamic_code_generated(JvmtiEnv * env,const char * name,const void * code_begin,const void * code_end)2264 void JvmtiExport::post_dynamic_code_generated(JvmtiEnv* env, const char *name,
2265 const void *code_begin, const void *code_end)
2266 {
2267 JavaThread* thread = JavaThread::current();
2268 EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
2269 ("[%s] dynamic code generated event triggered (by GenerateEvents)",
2270 JvmtiTrace::safe_get_thread_name(thread)));
2271 if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) {
2272 EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
2273 ("[%s] dynamic code generated event sent for %s",
2274 JvmtiTrace::safe_get_thread_name(thread), name));
2275 JvmtiEventMark jem(thread);
2276 JvmtiJavaThreadEventTransition jet(thread);
2277 jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char));
2278 jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated;
2279 if (callback != NULL) {
2280 (*callback)(env->jvmti_external(), name, (void*)code_begin, length);
2281 }
2282 }
2283 }
2284
2285 // post a DynamicCodeGenerated event while holding locks in the VM.
post_dynamic_code_generated_while_holding_locks(const char * name,address code_begin,address code_end)2286 void JvmtiExport::post_dynamic_code_generated_while_holding_locks(const char* name,
2287 address code_begin, address code_end)
2288 {
2289 // register the stub with the current dynamic code event collector
2290 // Cannot take safepoint here so do not use state_for to get
2291 // jvmti thread state.
2292 // The collector and/or state might be NULL if JvmtiDynamicCodeEventCollector
2293 // has been initialized while JVMTI_EVENT_DYNAMIC_CODE_GENERATED was disabled.
2294 JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
2295 if (state != NULL) {
2296 JvmtiDynamicCodeEventCollector *collector = state->get_dynamic_code_event_collector();
2297 if (collector != NULL) {
2298 collector->register_stub(name, code_begin, code_end);
2299 }
2300 }
2301 }
2302
2303 // Collect all the vm internally allocated objects which are visible to java world
record_vm_internal_object_allocation(oop obj)2304 void JvmtiExport::record_vm_internal_object_allocation(oop obj) {
2305 Thread* thread = Thread::current_or_null();
2306 if (thread != NULL && thread->is_Java_thread()) {
2307 // Can not take safepoint here.
2308 NoSafepointVerifier no_sfpt;
2309 // Cannot take safepoint here so do not use state_for to get
2310 // jvmti thread state.
2311 JvmtiThreadState *state = thread->as_Java_thread()->jvmti_thread_state();
2312 if (state != NULL) {
2313 // state is non NULL when VMObjectAllocEventCollector is enabled.
2314 JvmtiVMObjectAllocEventCollector *collector;
2315 collector = state->get_vm_object_alloc_event_collector();
2316 if (collector != NULL && collector->is_enabled()) {
2317 // Don't record classes as these will be notified via the ClassLoad
2318 // event.
2319 if (obj->klass() != vmClasses::Class_klass()) {
2320 collector->record_allocation(obj);
2321 }
2322 }
2323 }
2324 }
2325 }
2326
2327 // Collect all the sampled allocated objects.
record_sampled_internal_object_allocation(oop obj)2328 void JvmtiExport::record_sampled_internal_object_allocation(oop obj) {
2329 Thread* thread = Thread::current_or_null();
2330 if (thread != NULL && thread->is_Java_thread()) {
2331 // Can not take safepoint here.
2332 NoSafepointVerifier no_sfpt;
2333 // Cannot take safepoint here so do not use state_for to get
2334 // jvmti thread state.
2335 JvmtiThreadState *state = thread->as_Java_thread()->jvmti_thread_state();
2336 if (state != NULL) {
2337 // state is non NULL when SampledObjectAllocEventCollector is enabled.
2338 JvmtiSampledObjectAllocEventCollector *collector;
2339 collector = state->get_sampled_object_alloc_event_collector();
2340
2341 if (collector != NULL && collector->is_enabled()) {
2342 collector->record_allocation(obj);
2343 }
2344 }
2345 }
2346 }
2347
post_garbage_collection_finish()2348 void JvmtiExport::post_garbage_collection_finish() {
2349 Thread *thread = Thread::current(); // this event is posted from VM-Thread.
2350 EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2351 ("[%s] garbage collection finish event triggered",
2352 JvmtiTrace::safe_get_thread_name(thread)));
2353 JvmtiEnvIterator it;
2354 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2355 if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) {
2356 EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
2357 ("[%s] garbage collection finish event sent",
2358 JvmtiTrace::safe_get_thread_name(thread)));
2359 JvmtiThreadEventTransition jet(thread);
2360 // JNIEnv is NULL here because this event is posted from VM Thread
2361 jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish;
2362 if (callback != NULL) {
2363 (*callback)(env->jvmti_external());
2364 }
2365 }
2366 }
2367 }
2368
post_garbage_collection_start()2369 void JvmtiExport::post_garbage_collection_start() {
2370 Thread* thread = Thread::current(); // this event is posted from vm-thread.
2371 EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START,
2372 ("[%s] garbage collection start event triggered",
2373 JvmtiTrace::safe_get_thread_name(thread)));
2374 JvmtiEnvIterator it;
2375 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2376 if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_START)) {
2377 EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START,
2378 ("[%s] garbage collection start event sent",
2379 JvmtiTrace::safe_get_thread_name(thread)));
2380 JvmtiThreadEventTransition jet(thread);
2381 // JNIEnv is NULL here because this event is posted from VM Thread
2382 jvmtiEventGarbageCollectionStart callback = env->callbacks()->GarbageCollectionStart;
2383 if (callback != NULL) {
2384 (*callback)(env->jvmti_external());
2385 }
2386 }
2387 }
2388 }
2389
post_data_dump()2390 void JvmtiExport::post_data_dump() {
2391 Thread *thread = Thread::current();
2392 EVT_TRIG_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST,
2393 ("[%s] data dump request event triggered",
2394 JvmtiTrace::safe_get_thread_name(thread)));
2395 JvmtiEnvIterator it;
2396 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2397 if (env->is_enabled(JVMTI_EVENT_DATA_DUMP_REQUEST)) {
2398 EVT_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST,
2399 ("[%s] data dump request event sent",
2400 JvmtiTrace::safe_get_thread_name(thread)));
2401 JvmtiThreadEventTransition jet(thread);
2402 // JNIEnv is NULL here because this event is posted from VM Thread
2403 jvmtiEventDataDumpRequest callback = env->callbacks()->DataDumpRequest;
2404 if (callback != NULL) {
2405 (*callback)(env->jvmti_external());
2406 }
2407 }
2408 }
2409 }
2410
post_monitor_contended_enter(JavaThread * thread,ObjectMonitor * obj_mntr)2411 void JvmtiExport::post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) {
2412 oop object = obj_mntr->object();
2413 JvmtiThreadState *state = thread->jvmti_thread_state();
2414 if (state == NULL) {
2415 return;
2416 }
2417
2418 HandleMark hm(thread);
2419 Handle h(thread, object);
2420
2421 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
2422 ("[%s] monitor contended enter event triggered",
2423 JvmtiTrace::safe_get_thread_name(thread)));
2424
2425 JvmtiEnvThreadStateIterator it(state);
2426 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2427 if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTER)) {
2428 EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
2429 ("[%s] monitor contended enter event sent",
2430 JvmtiTrace::safe_get_thread_name(thread)));
2431 JvmtiMonitorEventMark jem(thread, h());
2432 JvmtiEnv *env = ets->get_env();
2433 JvmtiThreadEventTransition jet(thread);
2434 jvmtiEventMonitorContendedEnter callback = env->callbacks()->MonitorContendedEnter;
2435 if (callback != NULL) {
2436 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object());
2437 }
2438 }
2439 }
2440 }
2441
post_monitor_contended_entered(JavaThread * thread,ObjectMonitor * obj_mntr)2442 void JvmtiExport::post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) {
2443 oop object = obj_mntr->object();
2444 JvmtiThreadState *state = thread->jvmti_thread_state();
2445 if (state == NULL) {
2446 return;
2447 }
2448
2449 HandleMark hm(thread);
2450 Handle h(thread, object);
2451
2452 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
2453 ("[%s] monitor contended entered event triggered",
2454 JvmtiTrace::safe_get_thread_name(thread)));
2455
2456 JvmtiEnvThreadStateIterator it(state);
2457 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2458 if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED)) {
2459 EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
2460 ("[%s] monitor contended enter event sent",
2461 JvmtiTrace::safe_get_thread_name(thread)));
2462 JvmtiMonitorEventMark jem(thread, h());
2463 JvmtiEnv *env = ets->get_env();
2464 JvmtiThreadEventTransition jet(thread);
2465 jvmtiEventMonitorContendedEntered callback = env->callbacks()->MonitorContendedEntered;
2466 if (callback != NULL) {
2467 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object());
2468 }
2469 }
2470 }
2471 }
2472
post_monitor_wait(JavaThread * thread,oop object,jlong timeout)2473 void JvmtiExport::post_monitor_wait(JavaThread *thread, oop object,
2474 jlong timeout) {
2475 JvmtiThreadState *state = thread->jvmti_thread_state();
2476 if (state == NULL) {
2477 return;
2478 }
2479
2480 HandleMark hm(thread);
2481 Handle h(thread, object);
2482
2483 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAIT,
2484 ("[%s] monitor wait event triggered",
2485 JvmtiTrace::safe_get_thread_name(thread)));
2486
2487 JvmtiEnvThreadStateIterator it(state);
2488 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2489 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAIT)) {
2490 EVT_TRACE(JVMTI_EVENT_MONITOR_WAIT,
2491 ("[%s] monitor wait event sent",
2492 JvmtiTrace::safe_get_thread_name(thread)));
2493 JvmtiMonitorEventMark jem(thread, h());
2494 JvmtiEnv *env = ets->get_env();
2495 JvmtiThreadEventTransition jet(thread);
2496 jvmtiEventMonitorWait callback = env->callbacks()->MonitorWait;
2497 if (callback != NULL) {
2498 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2499 jem.jni_object(), timeout);
2500 }
2501 }
2502 }
2503 }
2504
post_monitor_waited(JavaThread * thread,ObjectMonitor * obj_mntr,jboolean timed_out)2505 void JvmtiExport::post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) {
2506 oop object = obj_mntr->object();
2507 JvmtiThreadState *state = thread->jvmti_thread_state();
2508 if (state == NULL) {
2509 return;
2510 }
2511
2512 HandleMark hm(thread);
2513 Handle h(thread, object);
2514
2515 EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2516 ("[%s] monitor waited event triggered",
2517 JvmtiTrace::safe_get_thread_name(thread)));
2518
2519 JvmtiEnvThreadStateIterator it(state);
2520 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2521 if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) {
2522 EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED,
2523 ("[%s] monitor waited event sent",
2524 JvmtiTrace::safe_get_thread_name(thread)));
2525 JvmtiMonitorEventMark jem(thread, h());
2526 JvmtiEnv *env = ets->get_env();
2527 JvmtiThreadEventTransition jet(thread);
2528 jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited;
2529 if (callback != NULL) {
2530 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2531 jem.jni_object(), timed_out);
2532 }
2533 }
2534 }
2535 }
2536
post_vm_object_alloc(JavaThread * thread,oop object)2537 void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) {
2538 EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Trg vm object alloc triggered",
2539 JvmtiTrace::safe_get_thread_name(thread)));
2540 if (object == NULL) {
2541 return;
2542 }
2543 HandleMark hm(thread);
2544 Handle h(thread, object);
2545 JvmtiEnvIterator it;
2546 for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
2547 if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) {
2548 EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("[%s] Evt vmobject alloc sent %s",
2549 JvmtiTrace::safe_get_thread_name(thread),
2550 object==NULL? "NULL" : object->klass()->external_name()));
2551
2552 JvmtiObjectAllocEventMark jem(thread, h());
2553 JvmtiJavaThreadEventTransition jet(thread);
2554 jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc;
2555 if (callback != NULL) {
2556 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2557 jem.jni_jobject(), jem.jni_class(), jem.size());
2558 }
2559 }
2560 }
2561 }
2562
post_sampled_object_alloc(JavaThread * thread,oop object)2563 void JvmtiExport::post_sampled_object_alloc(JavaThread *thread, oop object) {
2564 JvmtiThreadState *state = thread->jvmti_thread_state();
2565 if (state == NULL) {
2566 return;
2567 }
2568
2569 EVT_TRIG_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
2570 ("[%s] Trg sampled object alloc triggered",
2571 JvmtiTrace::safe_get_thread_name(thread)));
2572 if (object == NULL) {
2573 return;
2574 }
2575 HandleMark hm(thread);
2576 Handle h(thread, object);
2577
2578 JvmtiEnvThreadStateIterator it(state);
2579 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
2580 if (ets->is_enabled(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC)) {
2581 EVT_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
2582 ("[%s] Evt sampled object alloc sent %s",
2583 JvmtiTrace::safe_get_thread_name(thread),
2584 object == NULL ? "NULL" : object->klass()->external_name()));
2585
2586 JvmtiEnv *env = ets->get_env();
2587 JvmtiObjectAllocEventMark jem(thread, h());
2588 JvmtiJavaThreadEventTransition jet(thread);
2589 jvmtiEventSampledObjectAlloc callback = env->callbacks()->SampledObjectAlloc;
2590 if (callback != NULL) {
2591 (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
2592 jem.jni_jobject(), jem.jni_class(), jem.size());
2593 }
2594 }
2595 }
2596 }
2597
2598 ////////////////////////////////////////////////////////////////////////////////////////////////
2599
cleanup_thread(JavaThread * thread)2600 void JvmtiExport::cleanup_thread(JavaThread* thread) {
2601 assert(JavaThread::current() == thread, "thread is not current");
2602 MutexLocker mu(thread, JvmtiThreadState_lock);
2603
2604 if (thread->jvmti_thread_state() != NULL) {
2605 // This has to happen after the thread state is removed, which is
2606 // why it is not in post_thread_end_event like its complement
2607 // Maybe both these functions should be rolled into the posts?
2608 JvmtiEventController::thread_ended(thread);
2609 }
2610 }
2611
clear_detected_exception(JavaThread * thread)2612 void JvmtiExport::clear_detected_exception(JavaThread* thread) {
2613 assert(JavaThread::current() == thread, "thread is not current");
2614
2615 JvmtiThreadState* state = thread->jvmti_thread_state();
2616 if (state != NULL) {
2617 state->clear_exception_state();
2618 }
2619 }
2620
2621 // Onload raw monitor transition.
transition_pending_onload_raw_monitors()2622 void JvmtiExport::transition_pending_onload_raw_monitors() {
2623 JvmtiPendingMonitors::transition_raw_monitors();
2624 }
2625
2626 ////////////////////////////////////////////////////////////////////////////////////////////////
2627 #if INCLUDE_SERVICES
2628 // Attach is disabled if SERVICES is not included
2629
2630 // type for the Agent_OnAttach entry point
2631 extern "C" {
2632 typedef jint (JNICALL *OnAttachEntry_t)(JavaVM*, char *, void *);
2633 }
2634
load_agent_library(const char * agent,const char * absParam,const char * options,outputStream * st)2635 jint JvmtiExport::load_agent_library(const char *agent, const char *absParam,
2636 const char *options, outputStream* st) {
2637 char ebuf[1024] = {0};
2638 char buffer[JVM_MAXPATHLEN];
2639 void* library = NULL;
2640 jint result = JNI_ERR;
2641 const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS;
2642 size_t num_symbol_entries = ARRAY_SIZE(on_attach_symbols);
2643
2644 // The abs paramter should be "true" or "false"
2645 bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0);
2646
2647 // Initially marked as invalid. It will be set to valid if we can find the agent
2648 AgentLibrary *agent_lib = new AgentLibrary(agent, options, is_absolute_path, NULL);
2649
2650 // Check for statically linked in agent. If not found then if the path is
2651 // absolute we attempt to load the library. Otherwise we try to load it
2652 // from the standard dll directory.
2653
2654 if (!os::find_builtin_agent(agent_lib, on_attach_symbols, num_symbol_entries)) {
2655 if (is_absolute_path) {
2656 library = os::dll_load(agent, ebuf, sizeof ebuf);
2657 } else {
2658 // Try to load the agent from the standard dll directory
2659 if (os::dll_locate_lib(buffer, sizeof(buffer), Arguments::get_dll_dir(),
2660 agent)) {
2661 library = os::dll_load(buffer, ebuf, sizeof ebuf);
2662 }
2663 if (library == NULL) {
2664 // not found - try OS default library path
2665 if (os::dll_build_name(buffer, sizeof(buffer), agent)) {
2666 library = os::dll_load(buffer, ebuf, sizeof ebuf);
2667 }
2668 }
2669 }
2670 if (library != NULL) {
2671 agent_lib->set_os_lib(library);
2672 agent_lib->set_valid();
2673 }
2674 }
2675 // If the library was loaded then we attempt to invoke the Agent_OnAttach
2676 // function
2677 if (agent_lib->valid()) {
2678 // Lookup the Agent_OnAttach function
2679 OnAttachEntry_t on_attach_entry = NULL;
2680 on_attach_entry = CAST_TO_FN_PTR(OnAttachEntry_t,
2681 os::find_agent_function(agent_lib, false, on_attach_symbols, num_symbol_entries));
2682 if (on_attach_entry == NULL) {
2683 // Agent_OnAttach missing - unload library
2684 if (!agent_lib->is_static_lib()) {
2685 os::dll_unload(library);
2686 }
2687 st->print_cr("%s is not available in %s",
2688 on_attach_symbols[0], agent_lib->name());
2689 delete agent_lib;
2690 } else {
2691 // Invoke the Agent_OnAttach function
2692 JavaThread* THREAD = JavaThread::current(); // For exception macros.
2693 {
2694 extern struct JavaVM_ main_vm;
2695 JvmtiThreadEventMark jem(THREAD);
2696 JvmtiJavaThreadEventTransition jet(THREAD);
2697
2698 result = (*on_attach_entry)(&main_vm, (char*)options, NULL);
2699 }
2700
2701 // Agent_OnAttach may have used JNI
2702 if (HAS_PENDING_EXCEPTION) {
2703 CLEAR_PENDING_EXCEPTION;
2704 }
2705
2706 // If OnAttach returns JNI_OK then we add it to the list of
2707 // agent libraries so that we can call Agent_OnUnload later.
2708 if (result == JNI_OK) {
2709 Arguments::add_loaded_agent(agent_lib);
2710 } else {
2711 if (!agent_lib->is_static_lib()) {
2712 os::dll_unload(library);
2713 }
2714 delete agent_lib;
2715 }
2716
2717 // Agent_OnAttach executed so completion status is JNI_OK
2718 st->print_cr("return code: %d", result);
2719 result = JNI_OK;
2720 }
2721 } else {
2722 st->print_cr("%s was not loaded.", agent);
2723 if (*ebuf != '\0') {
2724 st->print_cr("%s", ebuf);
2725 }
2726 }
2727 return result;
2728 }
2729
2730 #endif // INCLUDE_SERVICES
2731 ////////////////////////////////////////////////////////////////////////////////////////////////
2732
2733 // Setup current current thread for event collection.
setup_jvmti_thread_state()2734 void JvmtiEventCollector::setup_jvmti_thread_state() {
2735 // set this event collector to be the current one.
2736 JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
2737 // state can only be NULL if the current thread is exiting which
2738 // should not happen since we're trying to configure for event collection
2739 guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
2740 if (is_vm_object_alloc_event()) {
2741 JvmtiVMObjectAllocEventCollector *prev = state->get_vm_object_alloc_event_collector();
2742
2743 // If we have a previous collector and it is disabled, it means this allocation came from a
2744 // callback induced VM Object allocation, do not register this collector then.
2745 if (prev && !prev->is_enabled()) {
2746 return;
2747 }
2748 _prev = prev;
2749 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
2750 } else if (is_dynamic_code_event()) {
2751 _prev = state->get_dynamic_code_event_collector();
2752 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);
2753 } else if (is_sampled_object_alloc_event()) {
2754 JvmtiSampledObjectAllocEventCollector *prev = state->get_sampled_object_alloc_event_collector();
2755
2756 if (prev) {
2757 // JvmtiSampledObjectAllocEventCollector wants only one active collector
2758 // enabled. This allows to have a collector detect a user code requiring
2759 // a sample in the callback.
2760 return;
2761 }
2762 state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*) this);
2763 }
2764
2765 _unset_jvmti_thread_state = true;
2766 }
2767
2768 // Unset current event collection in this thread and reset it with previous
2769 // collector.
unset_jvmti_thread_state()2770 void JvmtiEventCollector::unset_jvmti_thread_state() {
2771 if (!_unset_jvmti_thread_state) {
2772 return;
2773 }
2774
2775 JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
2776 if (state != NULL) {
2777 // restore the previous event collector (if any)
2778 if (is_vm_object_alloc_event()) {
2779 if (state->get_vm_object_alloc_event_collector() == this) {
2780 state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
2781 } else {
2782 // this thread's jvmti state was created during the scope of
2783 // the event collector.
2784 }
2785 } else if (is_dynamic_code_event()) {
2786 if (state->get_dynamic_code_event_collector() == this) {
2787 state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
2788 } else {
2789 // this thread's jvmti state was created during the scope of
2790 // the event collector.
2791 }
2792 } else if (is_sampled_object_alloc_event()) {
2793 if (state->get_sampled_object_alloc_event_collector() == this) {
2794 state->set_sampled_object_alloc_event_collector((JvmtiSampledObjectAllocEventCollector*)_prev);
2795 } else {
2796 // this thread's jvmti state was created during the scope of
2797 // the event collector.
2798 }
2799 }
2800 }
2801 }
2802
2803 // create the dynamic code event collector
JvmtiDynamicCodeEventCollector()2804 JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) {
2805 if (JvmtiExport::should_post_dynamic_code_generated()) {
2806 setup_jvmti_thread_state();
2807 }
2808 }
2809
2810 // iterate over any code blob descriptors collected and post a
2811 // DYNAMIC_CODE_GENERATED event to the profiler.
~JvmtiDynamicCodeEventCollector()2812 JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
2813 assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
2814 // iterate over any code blob descriptors that we collected
2815 if (_code_blobs != NULL) {
2816 for (int i=0; i<_code_blobs->length(); i++) {
2817 JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
2818 JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
2819 FreeHeap(blob);
2820 }
2821 delete _code_blobs;
2822 }
2823 unset_jvmti_thread_state();
2824 }
2825
2826 // register a stub
register_stub(const char * name,address start,address end)2827 void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
2828 if (_code_blobs == NULL) {
2829 _code_blobs = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<JvmtiCodeBlobDesc*>(1, mtServiceability);
2830 }
2831 _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
2832 }
2833
2834 // Setup current thread to record vm allocated objects.
JvmtiObjectAllocEventCollector()2835 JvmtiObjectAllocEventCollector::JvmtiObjectAllocEventCollector() :
2836 _allocated(NULL), _enable(false), _post_callback(NULL) {
2837 }
2838
2839 // Post vm_object_alloc event for vm allocated objects visible to java
2840 // world.
generate_call_for_allocated()2841 void JvmtiObjectAllocEventCollector::generate_call_for_allocated() {
2842 if (_allocated) {
2843 set_enabled(false);
2844 for (int i = 0; i < _allocated->length(); i++) {
2845 oop obj = _allocated->at(i).resolve();
2846 _post_callback(JavaThread::current(), obj);
2847 // Release OopHandle
2848 _allocated->at(i).release(JvmtiExport::jvmti_oop_storage());
2849
2850 }
2851 delete _allocated, _allocated = NULL;
2852 }
2853 }
2854
record_allocation(oop obj)2855 void JvmtiObjectAllocEventCollector::record_allocation(oop obj) {
2856 assert(is_enabled(), "Object alloc event collector is not enabled");
2857 if (_allocated == NULL) {
2858 _allocated = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<OopHandle>(1, mtServiceability);
2859 }
2860 _allocated->push(OopHandle(JvmtiExport::jvmti_oop_storage(), obj));
2861 }
2862
2863 // Disable collection of VMObjectAlloc events
NoJvmtiVMObjectAllocMark()2864 NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
2865 // a no-op if VMObjectAlloc event is not enabled
2866 if (!JvmtiExport::should_post_vm_object_alloc()) {
2867 return;
2868 }
2869 Thread* thread = Thread::current_or_null();
2870 if (thread != NULL && thread->is_Java_thread()) {
2871 JavaThread* current_thread = thread->as_Java_thread();
2872 JvmtiThreadState *state = current_thread->jvmti_thread_state();
2873 if (state != NULL) {
2874 JvmtiVMObjectAllocEventCollector *collector;
2875 collector = state->get_vm_object_alloc_event_collector();
2876 if (collector != NULL && collector->is_enabled()) {
2877 _collector = collector;
2878 _collector->set_enabled(false);
2879 }
2880 }
2881 }
2882 }
2883
2884 // Re-Enable collection of VMObjectAlloc events (if previously enabled)
~NoJvmtiVMObjectAllocMark()2885 NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
2886 if (was_enabled()) {
2887 _collector->set_enabled(true);
2888 }
2889 };
2890
2891 // Setup current thread to record vm allocated objects.
JvmtiVMObjectAllocEventCollector()2892 JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() {
2893 if (JvmtiExport::should_post_vm_object_alloc()) {
2894 _enable = true;
2895 setup_jvmti_thread_state();
2896 _post_callback = JvmtiExport::post_vm_object_alloc;
2897 }
2898 }
2899
~JvmtiVMObjectAllocEventCollector()2900 JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
2901 if (_enable) {
2902 generate_call_for_allocated();
2903 }
2904 unset_jvmti_thread_state();
2905 }
2906
object_alloc_is_safe_to_sample()2907 bool JvmtiSampledObjectAllocEventCollector::object_alloc_is_safe_to_sample() {
2908 Thread* thread = Thread::current();
2909 // Really only sample allocations if this is a JavaThread and not the compiler
2910 // thread.
2911 if (!thread->is_Java_thread() || thread->is_Compiler_thread()) {
2912 return false;
2913 }
2914
2915 if (MultiArray_lock->owner() == thread) {
2916 return false;
2917 }
2918 return true;
2919 }
2920
2921 // Setup current thread to record sampled allocated objects.
JvmtiSampledObjectAllocEventCollector()2922 JvmtiSampledObjectAllocEventCollector::JvmtiSampledObjectAllocEventCollector() {
2923 if (JvmtiExport::should_post_sampled_object_alloc()) {
2924 if (!object_alloc_is_safe_to_sample()) {
2925 return;
2926 }
2927
2928 _enable = true;
2929 setup_jvmti_thread_state();
2930 _post_callback = JvmtiExport::post_sampled_object_alloc;
2931 }
2932 }
2933
~JvmtiSampledObjectAllocEventCollector()2934 JvmtiSampledObjectAllocEventCollector::~JvmtiSampledObjectAllocEventCollector() {
2935 if (!_enable) {
2936 return;
2937 }
2938
2939 generate_call_for_allocated();
2940 unset_jvmti_thread_state();
2941
2942 // Unset the sampling collector as present in assertion mode only.
2943 assert(Thread::current()->is_Java_thread(),
2944 "Should always be in a Java thread");
2945 }
2946
JvmtiGCMarker()2947 JvmtiGCMarker::JvmtiGCMarker() {
2948 // if there aren't any JVMTI environments then nothing to do
2949 if (!JvmtiEnv::environments_might_exist()) {
2950 return;
2951 }
2952
2953 if (JvmtiExport::should_post_garbage_collection_start()) {
2954 JvmtiExport::post_garbage_collection_start();
2955 }
2956
2957 if (SafepointSynchronize::is_at_safepoint()) {
2958 // Do clean up tasks that need to be done at a safepoint
2959 JvmtiEnvBase::check_for_periodic_clean_up();
2960 }
2961 }
2962
~JvmtiGCMarker()2963 JvmtiGCMarker::~JvmtiGCMarker() {
2964 // if there aren't any JVMTI environments then nothing to do
2965 if (!JvmtiEnv::environments_might_exist()) {
2966 return;
2967 }
2968
2969 // JVMTI notify gc finish
2970 if (JvmtiExport::should_post_garbage_collection_finish()) {
2971 JvmtiExport::post_garbage_collection_finish();
2972 }
2973 }
2974