Wed, 03 Jul 2019 20:42:37 +0800
Merge
1 /*
2 * Copyright (c) 2003, 2014, 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 */
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "interpreter/interpreter.hpp"
28 #include "interpreter/oopMapCache.hpp"
29 #include "jvmtifiles/jvmtiEnv.hpp"
30 #include "memory/resourceArea.hpp"
31 #include "oops/instanceKlass.hpp"
32 #include "prims/jvmtiAgentThread.hpp"
33 #include "prims/jvmtiEventController.inline.hpp"
34 #include "prims/jvmtiImpl.hpp"
35 #include "prims/jvmtiRedefineClasses.hpp"
36 #include "runtime/atomic.hpp"
37 #include "runtime/deoptimization.hpp"
38 #include "runtime/handles.hpp"
39 #include "runtime/handles.inline.hpp"
40 #include "runtime/interfaceSupport.hpp"
41 #include "runtime/javaCalls.hpp"
42 #include "runtime/os.hpp"
43 #include "runtime/serviceThread.hpp"
44 #include "runtime/signature.hpp"
45 #include "runtime/thread.inline.hpp"
46 #include "runtime/vframe.hpp"
47 #include "runtime/vframe_hp.hpp"
48 #include "runtime/vm_operations.hpp"
49 #include "utilities/exceptions.hpp"
51 //
52 // class JvmtiAgentThread
53 //
54 // JavaThread used to wrap a thread started by an agent
55 // using the JVMTI method RunAgentThread.
56 //
58 JvmtiAgentThread::JvmtiAgentThread(JvmtiEnv* env, jvmtiStartFunction start_fn, const void *start_arg)
59 : JavaThread(start_function_wrapper) {
60 _env = env;
61 _start_fn = start_fn;
62 _start_arg = start_arg;
63 }
65 void
66 JvmtiAgentThread::start_function_wrapper(JavaThread *thread, TRAPS) {
67 // It is expected that any Agent threads will be created as
68 // Java Threads. If this is the case, notification of the creation
69 // of the thread is given in JavaThread::thread_main().
70 assert(thread->is_Java_thread(), "debugger thread should be a Java Thread");
71 assert(thread == JavaThread::current(), "sanity check");
73 JvmtiAgentThread *dthread = (JvmtiAgentThread *)thread;
74 dthread->call_start_function();
75 }
77 void
78 JvmtiAgentThread::call_start_function() {
79 ThreadToNativeFromVM transition(this);
80 _start_fn(_env->jvmti_external(), jni_environment(), (void*)_start_arg);
81 }
84 //
85 // class GrowableCache - private methods
86 //
88 void GrowableCache::recache() {
89 int len = _elements->length();
91 FREE_C_HEAP_ARRAY(address, _cache, mtInternal);
92 _cache = NEW_C_HEAP_ARRAY(address,len+1, mtInternal);
94 for (int i=0; i<len; i++) {
95 _cache[i] = _elements->at(i)->getCacheValue();
96 //
97 // The cache entry has gone bad. Without a valid frame pointer
98 // value, the entry is useless so we simply delete it in product
99 // mode. The call to remove() will rebuild the cache again
100 // without the bad entry.
101 //
102 if (_cache[i] == NULL) {
103 assert(false, "cannot recache NULL elements");
104 remove(i);
105 return;
106 }
107 }
108 _cache[len] = NULL;
110 _listener_fun(_this_obj,_cache);
111 }
113 bool GrowableCache::equals(void* v, GrowableElement *e2) {
114 GrowableElement *e1 = (GrowableElement *) v;
115 assert(e1 != NULL, "e1 != NULL");
116 assert(e2 != NULL, "e2 != NULL");
118 return e1->equals(e2);
119 }
121 //
122 // class GrowableCache - public methods
123 //
125 GrowableCache::GrowableCache() {
126 _this_obj = NULL;
127 _listener_fun = NULL;
128 _elements = NULL;
129 _cache = NULL;
130 }
132 GrowableCache::~GrowableCache() {
133 clear();
134 delete _elements;
135 FREE_C_HEAP_ARRAY(address, _cache, mtInternal);
136 }
138 void GrowableCache::initialize(void *this_obj, void listener_fun(void *, address*) ) {
139 _this_obj = this_obj;
140 _listener_fun = listener_fun;
141 _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<GrowableElement*>(5,true);
142 recache();
143 }
145 // number of elements in the collection
146 int GrowableCache::length() {
147 return _elements->length();
148 }
150 // get the value of the index element in the collection
151 GrowableElement* GrowableCache::at(int index) {
152 GrowableElement *e = (GrowableElement *) _elements->at(index);
153 assert(e != NULL, "e != NULL");
154 return e;
155 }
157 int GrowableCache::find(GrowableElement* e) {
158 return _elements->find(e, GrowableCache::equals);
159 }
161 // append a copy of the element to the end of the collection
162 void GrowableCache::append(GrowableElement* e) {
163 GrowableElement *new_e = e->clone();
164 _elements->append(new_e);
165 recache();
166 }
168 // insert a copy of the element using lessthan()
169 void GrowableCache::insert(GrowableElement* e) {
170 GrowableElement *new_e = e->clone();
171 _elements->append(new_e);
173 int n = length()-2;
174 for (int i=n; i>=0; i--) {
175 GrowableElement *e1 = _elements->at(i);
176 GrowableElement *e2 = _elements->at(i+1);
177 if (e2->lessThan(e1)) {
178 _elements->at_put(i+1, e1);
179 _elements->at_put(i, e2);
180 }
181 }
183 recache();
184 }
186 // remove the element at index
187 void GrowableCache::remove (int index) {
188 GrowableElement *e = _elements->at(index);
189 assert(e != NULL, "e != NULL");
190 _elements->remove(e);
191 delete e;
192 recache();
193 }
195 // clear out all elements, release all heap space and
196 // let our listener know that things have changed.
197 void GrowableCache::clear() {
198 int len = _elements->length();
199 for (int i=0; i<len; i++) {
200 delete _elements->at(i);
201 }
202 _elements->clear();
203 recache();
204 }
206 void GrowableCache::oops_do(OopClosure* f) {
207 int len = _elements->length();
208 for (int i=0; i<len; i++) {
209 GrowableElement *e = _elements->at(i);
210 e->oops_do(f);
211 }
212 }
214 void GrowableCache::metadata_do(void f(Metadata*)) {
215 int len = _elements->length();
216 for (int i=0; i<len; i++) {
217 GrowableElement *e = _elements->at(i);
218 e->metadata_do(f);
219 }
220 }
222 void GrowableCache::gc_epilogue() {
223 int len = _elements->length();
224 for (int i=0; i<len; i++) {
225 _cache[i] = _elements->at(i)->getCacheValue();
226 }
227 }
229 //
230 // class JvmtiBreakpoint
231 //
233 JvmtiBreakpoint::JvmtiBreakpoint() {
234 _method = NULL;
235 _bci = 0;
236 _class_holder = NULL;
237 }
239 JvmtiBreakpoint::JvmtiBreakpoint(Method* m_method, jlocation location) {
240 _method = m_method;
241 _class_holder = _method->method_holder()->klass_holder();
242 #ifdef CHECK_UNHANDLED_OOPS
243 // _class_holder can't be wrapped in a Handle, because JvmtiBreakpoints are
244 // sometimes allocated on the heap.
245 //
246 // The code handling JvmtiBreakpoints allocated on the stack can't be
247 // interrupted by a GC until _class_holder is reachable by the GC via the
248 // oops_do method.
249 Thread::current()->allow_unhandled_oop(&_class_holder);
250 #endif // CHECK_UNHANDLED_OOPS
251 assert(_method != NULL, "_method != NULL");
252 _bci = (int) location;
253 assert(_bci >= 0, "_bci >= 0");
254 }
256 void JvmtiBreakpoint::copy(JvmtiBreakpoint& bp) {
257 _method = bp._method;
258 _bci = bp._bci;
259 _class_holder = bp._class_holder;
260 }
262 bool JvmtiBreakpoint::lessThan(JvmtiBreakpoint& bp) {
263 Unimplemented();
264 return false;
265 }
267 bool JvmtiBreakpoint::equals(JvmtiBreakpoint& bp) {
268 return _method == bp._method
269 && _bci == bp._bci;
270 }
272 bool JvmtiBreakpoint::is_valid() {
273 // class loader can be NULL
274 return _method != NULL &&
275 _bci >= 0;
276 }
278 address JvmtiBreakpoint::getBcp() {
279 return _method->bcp_from(_bci);
280 }
282 void JvmtiBreakpoint::each_method_version_do(method_action meth_act) {
283 ((Method*)_method->*meth_act)(_bci);
285 // add/remove breakpoint to/from versions of the method that are EMCP.
286 Thread *thread = Thread::current();
287 instanceKlassHandle ikh = instanceKlassHandle(thread, _method->method_holder());
288 Symbol* m_name = _method->name();
289 Symbol* m_signature = _method->signature();
291 // search previous versions if they exist
292 for (InstanceKlass* pv_node = ikh->previous_versions();
293 pv_node != NULL;
294 pv_node = pv_node->previous_versions()) {
295 Array<Method*>* methods = pv_node->methods();
297 for (int i = methods->length() - 1; i >= 0; i--) {
298 Method* method = methods->at(i);
299 // Only set breakpoints in running EMCP methods.
300 if (method->is_running_emcp() &&
301 method->name() == m_name &&
302 method->signature() == m_signature) {
303 RC_TRACE(0x00000800, ("%sing breakpoint in %s(%s)",
304 meth_act == &Method::set_breakpoint ? "sett" : "clear",
305 method->name()->as_C_string(),
306 method->signature()->as_C_string()));
308 (method->*meth_act)(_bci);
309 break;
310 }
311 }
312 }
313 }
315 void JvmtiBreakpoint::set() {
316 each_method_version_do(&Method::set_breakpoint);
317 }
319 void JvmtiBreakpoint::clear() {
320 each_method_version_do(&Method::clear_breakpoint);
321 }
323 void JvmtiBreakpoint::print() {
324 #ifndef PRODUCT
325 const char *class_name = (_method == NULL) ? "NULL" : _method->klass_name()->as_C_string();
326 const char *method_name = (_method == NULL) ? "NULL" : _method->name()->as_C_string();
328 tty->print("Breakpoint(%s,%s,%d,%p)",class_name, method_name, _bci, getBcp());
329 #endif
330 }
333 //
334 // class VM_ChangeBreakpoints
335 //
336 // Modify the Breakpoints data structure at a safepoint
337 //
339 void VM_ChangeBreakpoints::doit() {
340 switch (_operation) {
341 case SET_BREAKPOINT:
342 _breakpoints->set_at_safepoint(*_bp);
343 break;
344 case CLEAR_BREAKPOINT:
345 _breakpoints->clear_at_safepoint(*_bp);
346 break;
347 default:
348 assert(false, "Unknown operation");
349 }
350 }
352 void VM_ChangeBreakpoints::oops_do(OopClosure* f) {
353 // The JvmtiBreakpoints in _breakpoints will be visited via
354 // JvmtiExport::oops_do.
355 if (_bp != NULL) {
356 _bp->oops_do(f);
357 }
358 }
360 void VM_ChangeBreakpoints::metadata_do(void f(Metadata*)) {
361 // Walk metadata in breakpoints to keep from being deallocated with RedefineClasses
362 if (_bp != NULL) {
363 _bp->metadata_do(f);
364 }
365 }
367 //
368 // class JvmtiBreakpoints
369 //
370 // a JVMTI internal collection of JvmtiBreakpoint
371 //
373 JvmtiBreakpoints::JvmtiBreakpoints(void listener_fun(void *,address *)) {
374 _bps.initialize(this,listener_fun);
375 }
377 JvmtiBreakpoints:: ~JvmtiBreakpoints() {}
379 void JvmtiBreakpoints::oops_do(OopClosure* f) {
380 _bps.oops_do(f);
381 }
383 void JvmtiBreakpoints::metadata_do(void f(Metadata*)) {
384 _bps.metadata_do(f);
385 }
387 void JvmtiBreakpoints::gc_epilogue() {
388 _bps.gc_epilogue();
389 }
391 void JvmtiBreakpoints::print() {
392 #ifndef PRODUCT
393 ResourceMark rm;
395 int n = _bps.length();
396 for (int i=0; i<n; i++) {
397 JvmtiBreakpoint& bp = _bps.at(i);
398 tty->print("%d: ", i);
399 bp.print();
400 tty->cr();
401 }
402 #endif
403 }
406 void JvmtiBreakpoints::set_at_safepoint(JvmtiBreakpoint& bp) {
407 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
409 int i = _bps.find(bp);
410 if (i == -1) {
411 _bps.append(bp);
412 bp.set();
413 }
414 }
416 void JvmtiBreakpoints::clear_at_safepoint(JvmtiBreakpoint& bp) {
417 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
419 int i = _bps.find(bp);
420 if (i != -1) {
421 _bps.remove(i);
422 bp.clear();
423 }
424 }
426 int JvmtiBreakpoints::length() { return _bps.length(); }
428 int JvmtiBreakpoints::set(JvmtiBreakpoint& bp) {
429 if ( _bps.find(bp) != -1) {
430 return JVMTI_ERROR_DUPLICATE;
431 }
432 VM_ChangeBreakpoints set_breakpoint(VM_ChangeBreakpoints::SET_BREAKPOINT, &bp);
433 VMThread::execute(&set_breakpoint);
434 return JVMTI_ERROR_NONE;
435 }
437 int JvmtiBreakpoints::clear(JvmtiBreakpoint& bp) {
438 if ( _bps.find(bp) == -1) {
439 return JVMTI_ERROR_NOT_FOUND;
440 }
442 VM_ChangeBreakpoints clear_breakpoint(VM_ChangeBreakpoints::CLEAR_BREAKPOINT, &bp);
443 VMThread::execute(&clear_breakpoint);
444 return JVMTI_ERROR_NONE;
445 }
447 void JvmtiBreakpoints::clearall_in_class_at_safepoint(Klass* klass) {
448 bool changed = true;
449 // We are going to run thru the list of bkpts
450 // and delete some. This deletion probably alters
451 // the list in some implementation defined way such
452 // that when we delete entry i, the next entry might
453 // no longer be at i+1. To be safe, each time we delete
454 // an entry, we'll just start again from the beginning.
455 // We'll stop when we make a pass thru the whole list without
456 // deleting anything.
457 while (changed) {
458 int len = _bps.length();
459 changed = false;
460 for (int i = 0; i < len; i++) {
461 JvmtiBreakpoint& bp = _bps.at(i);
462 if (bp.method()->method_holder() == klass) {
463 bp.clear();
464 _bps.remove(i);
465 // This changed 'i' so we have to start over.
466 changed = true;
467 break;
468 }
469 }
470 }
471 }
473 //
474 // class JvmtiCurrentBreakpoints
475 //
477 JvmtiBreakpoints *JvmtiCurrentBreakpoints::_jvmti_breakpoints = NULL;
478 address * JvmtiCurrentBreakpoints::_breakpoint_list = NULL;
481 JvmtiBreakpoints& JvmtiCurrentBreakpoints::get_jvmti_breakpoints() {
482 if (_jvmti_breakpoints != NULL) return (*_jvmti_breakpoints);
483 _jvmti_breakpoints = new JvmtiBreakpoints(listener_fun);
484 assert(_jvmti_breakpoints != NULL, "_jvmti_breakpoints != NULL");
485 return (*_jvmti_breakpoints);
486 }
488 void JvmtiCurrentBreakpoints::listener_fun(void *this_obj, address *cache) {
489 JvmtiBreakpoints *this_jvmti = (JvmtiBreakpoints *) this_obj;
490 assert(this_jvmti != NULL, "this_jvmti != NULL");
492 debug_only(int n = this_jvmti->length(););
493 assert(cache[n] == NULL, "cache must be NULL terminated");
495 set_breakpoint_list(cache);
496 }
499 void JvmtiCurrentBreakpoints::oops_do(OopClosure* f) {
500 if (_jvmti_breakpoints != NULL) {
501 _jvmti_breakpoints->oops_do(f);
502 }
503 }
505 void JvmtiCurrentBreakpoints::metadata_do(void f(Metadata*)) {
506 if (_jvmti_breakpoints != NULL) {
507 _jvmti_breakpoints->metadata_do(f);
508 }
509 }
511 void JvmtiCurrentBreakpoints::gc_epilogue() {
512 if (_jvmti_breakpoints != NULL) {
513 _jvmti_breakpoints->gc_epilogue();
514 }
515 }
517 ///////////////////////////////////////////////////////////////
518 //
519 // class VM_GetOrSetLocal
520 //
522 // Constructor for non-object getter
523 VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, int index, BasicType type)
524 : _thread(thread)
525 , _calling_thread(NULL)
526 , _depth(depth)
527 , _index(index)
528 , _type(type)
529 , _set(false)
530 , _jvf(NULL)
531 , _result(JVMTI_ERROR_NONE)
532 {
533 }
535 // Constructor for object or non-object setter
536 VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, int index, BasicType type, jvalue value)
537 : _thread(thread)
538 , _calling_thread(NULL)
539 , _depth(depth)
540 , _index(index)
541 , _type(type)
542 , _value(value)
543 , _set(true)
544 , _jvf(NULL)
545 , _result(JVMTI_ERROR_NONE)
546 {
547 }
549 // Constructor for object getter
550 VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth, int index)
551 : _thread(thread)
552 , _calling_thread(calling_thread)
553 , _depth(depth)
554 , _index(index)
555 , _type(T_OBJECT)
556 , _set(false)
557 , _jvf(NULL)
558 , _result(JVMTI_ERROR_NONE)
559 {
560 }
562 vframe *VM_GetOrSetLocal::get_vframe() {
563 if (!_thread->has_last_Java_frame()) {
564 return NULL;
565 }
566 RegisterMap reg_map(_thread);
567 vframe *vf = _thread->last_java_vframe(®_map);
568 int d = 0;
569 while ((vf != NULL) && (d < _depth)) {
570 vf = vf->java_sender();
571 d++;
572 }
573 return vf;
574 }
576 javaVFrame *VM_GetOrSetLocal::get_java_vframe() {
577 vframe* vf = get_vframe();
578 if (vf == NULL) {
579 _result = JVMTI_ERROR_NO_MORE_FRAMES;
580 return NULL;
581 }
582 javaVFrame *jvf = (javaVFrame*)vf;
584 if (!vf->is_java_frame()) {
585 _result = JVMTI_ERROR_OPAQUE_FRAME;
586 return NULL;
587 }
588 return jvf;
589 }
591 // Check that the klass is assignable to a type with the given signature.
592 // Another solution could be to use the function Klass::is_subtype_of(type).
593 // But the type class can be forced to load/initialize eagerly in such a case.
594 // This may cause unexpected consequences like CFLH or class-init JVMTI events.
595 // It is better to avoid such a behavior.
596 bool VM_GetOrSetLocal::is_assignable(const char* ty_sign, Klass* klass, Thread* thread) {
597 assert(ty_sign != NULL, "type signature must not be NULL");
598 assert(thread != NULL, "thread must not be NULL");
599 assert(klass != NULL, "klass must not be NULL");
601 int len = (int) strlen(ty_sign);
602 if (ty_sign[0] == 'L' && ty_sign[len-1] == ';') { // Need pure class/interface name
603 ty_sign++;
604 len -= 2;
605 }
606 TempNewSymbol ty_sym = SymbolTable::new_symbol(ty_sign, len, thread);
607 if (klass->name() == ty_sym) {
608 return true;
609 }
610 // Compare primary supers
611 int super_depth = klass->super_depth();
612 int idx;
613 for (idx = 0; idx < super_depth; idx++) {
614 if (klass->primary_super_of_depth(idx)->name() == ty_sym) {
615 return true;
616 }
617 }
618 // Compare secondary supers
619 Array<Klass*>* sec_supers = klass->secondary_supers();
620 for (idx = 0; idx < sec_supers->length(); idx++) {
621 if (((Klass*) sec_supers->at(idx))->name() == ty_sym) {
622 return true;
623 }
624 }
625 return false;
626 }
628 // Checks error conditions:
629 // JVMTI_ERROR_INVALID_SLOT
630 // JVMTI_ERROR_TYPE_MISMATCH
631 // Returns: 'true' - everything is Ok, 'false' - error code
633 bool VM_GetOrSetLocal::check_slot_type(javaVFrame* jvf) {
634 Method* method_oop = jvf->method();
635 if (!method_oop->has_localvariable_table()) {
636 // Just to check index boundaries
637 jint extra_slot = (_type == T_LONG || _type == T_DOUBLE) ? 1 : 0;
638 if (_index < 0 || _index + extra_slot >= method_oop->max_locals()) {
639 _result = JVMTI_ERROR_INVALID_SLOT;
640 return false;
641 }
642 return true;
643 }
645 jint num_entries = method_oop->localvariable_table_length();
646 if (num_entries == 0) {
647 _result = JVMTI_ERROR_INVALID_SLOT;
648 return false; // There are no slots
649 }
650 int signature_idx = -1;
651 int vf_bci = jvf->bci();
652 LocalVariableTableElement* table = method_oop->localvariable_table_start();
653 for (int i = 0; i < num_entries; i++) {
654 int start_bci = table[i].start_bci;
655 int end_bci = start_bci + table[i].length;
657 // Here we assume that locations of LVT entries
658 // with the same slot number cannot be overlapped
659 if (_index == (jint) table[i].slot && start_bci <= vf_bci && vf_bci <= end_bci) {
660 signature_idx = (int) table[i].descriptor_cp_index;
661 break;
662 }
663 }
664 if (signature_idx == -1) {
665 _result = JVMTI_ERROR_INVALID_SLOT;
666 return false; // Incorrect slot index
667 }
668 Symbol* sign_sym = method_oop->constants()->symbol_at(signature_idx);
669 const char* signature = (const char *) sign_sym->as_utf8();
670 BasicType slot_type = char2type(signature[0]);
672 switch (slot_type) {
673 case T_BYTE:
674 case T_SHORT:
675 case T_CHAR:
676 case T_BOOLEAN:
677 slot_type = T_INT;
678 break;
679 case T_ARRAY:
680 slot_type = T_OBJECT;
681 break;
682 };
683 if (_type != slot_type) {
684 _result = JVMTI_ERROR_TYPE_MISMATCH;
685 return false;
686 }
688 jobject jobj = _value.l;
689 if (_set && slot_type == T_OBJECT && jobj != NULL) { // NULL reference is allowed
690 // Check that the jobject class matches the return type signature.
691 JavaThread* cur_thread = JavaThread::current();
692 HandleMark hm(cur_thread);
694 Handle obj = Handle(cur_thread, JNIHandles::resolve_external_guard(jobj));
695 NULL_CHECK(obj, (_result = JVMTI_ERROR_INVALID_OBJECT, false));
696 KlassHandle ob_kh = KlassHandle(cur_thread, obj->klass());
697 NULL_CHECK(ob_kh, (_result = JVMTI_ERROR_INVALID_OBJECT, false));
699 if (!is_assignable(signature, ob_kh(), cur_thread)) {
700 _result = JVMTI_ERROR_TYPE_MISMATCH;
701 return false;
702 }
703 }
704 return true;
705 }
707 static bool can_be_deoptimized(vframe* vf) {
708 return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());
709 }
711 bool VM_GetOrSetLocal::doit_prologue() {
712 _jvf = get_java_vframe();
713 NULL_CHECK(_jvf, false);
715 if (_jvf->method()->is_native()) {
716 if (getting_receiver() && !_jvf->method()->is_static()) {
717 return true;
718 } else {
719 _result = JVMTI_ERROR_OPAQUE_FRAME;
720 return false;
721 }
722 }
724 if (!check_slot_type(_jvf)) {
725 return false;
726 }
727 return true;
728 }
730 void VM_GetOrSetLocal::doit() {
731 InterpreterOopMap oop_mask;
732 _jvf->method()->mask_for(_jvf->bci(), &oop_mask);
733 if (oop_mask.is_dead(_index)) {
734 // The local can be invalid and uninitialized in the scope of current bci
735 _result = JVMTI_ERROR_INVALID_SLOT;
736 return;
737 }
738 if (_set) {
739 // Force deoptimization of frame if compiled because it's
740 // possible the compiler emitted some locals as constant values,
741 // meaning they are not mutable.
742 if (can_be_deoptimized(_jvf)) {
744 // Schedule deoptimization so that eventually the local
745 // update will be written to an interpreter frame.
746 Deoptimization::deoptimize_frame(_jvf->thread(), _jvf->fr().id());
748 // Now store a new value for the local which will be applied
749 // once deoptimization occurs. Note however that while this
750 // write is deferred until deoptimization actually happens
751 // can vframe created after this point will have its locals
752 // reflecting this update so as far as anyone can see the
753 // write has already taken place.
755 // If we are updating an oop then get the oop from the handle
756 // since the handle will be long gone by the time the deopt
757 // happens. The oop stored in the deferred local will be
758 // gc'd on its own.
759 if (_type == T_OBJECT) {
760 _value.l = (jobject) (JNIHandles::resolve_external_guard(_value.l));
761 }
762 // Re-read the vframe so we can see that it is deoptimized
763 // [ Only need because of assert in update_local() ]
764 _jvf = get_java_vframe();
765 ((compiledVFrame*)_jvf)->update_local(_type, _index, _value);
766 return;
767 }
768 StackValueCollection *locals = _jvf->locals();
769 HandleMark hm;
771 switch (_type) {
772 case T_INT: locals->set_int_at (_index, _value.i); break;
773 case T_LONG: locals->set_long_at (_index, _value.j); break;
774 case T_FLOAT: locals->set_float_at (_index, _value.f); break;
775 case T_DOUBLE: locals->set_double_at(_index, _value.d); break;
776 case T_OBJECT: {
777 Handle ob_h(JNIHandles::resolve_external_guard(_value.l));
778 locals->set_obj_at (_index, ob_h);
779 break;
780 }
781 default: ShouldNotReachHere();
782 }
783 _jvf->set_locals(locals);
784 } else {
785 if (_jvf->method()->is_native() && _jvf->is_compiled_frame()) {
786 assert(getting_receiver(), "Can only get here when getting receiver");
787 oop receiver = _jvf->fr().get_native_receiver();
788 _value.l = JNIHandles::make_local(_calling_thread, receiver);
789 } else {
790 StackValueCollection *locals = _jvf->locals();
792 if (locals->at(_index)->type() == T_CONFLICT) {
793 memset(&_value, 0, sizeof(_value));
794 _value.l = NULL;
795 return;
796 }
798 switch (_type) {
799 case T_INT: _value.i = locals->int_at (_index); break;
800 case T_LONG: _value.j = locals->long_at (_index); break;
801 case T_FLOAT: _value.f = locals->float_at (_index); break;
802 case T_DOUBLE: _value.d = locals->double_at(_index); break;
803 case T_OBJECT: {
804 // Wrap the oop to be returned in a local JNI handle since
805 // oops_do() no longer applies after doit() is finished.
806 oop obj = locals->obj_at(_index)();
807 _value.l = JNIHandles::make_local(_calling_thread, obj);
808 break;
809 }
810 default: ShouldNotReachHere();
811 }
812 }
813 }
814 }
817 bool VM_GetOrSetLocal::allow_nested_vm_operations() const {
818 return true; // May need to deoptimize
819 }
822 VM_GetReceiver::VM_GetReceiver(
823 JavaThread* thread, JavaThread* caller_thread, jint depth)
824 : VM_GetOrSetLocal(thread, caller_thread, depth, 0) {}
826 /////////////////////////////////////////////////////////////////////////////////////////
828 //
829 // class JvmtiSuspendControl - see comments in jvmtiImpl.hpp
830 //
832 bool JvmtiSuspendControl::suspend(JavaThread *java_thread) {
833 // external suspend should have caught suspending a thread twice
835 // Immediate suspension required for JPDA back-end so JVMTI agent threads do
836 // not deadlock due to later suspension on transitions while holding
837 // raw monitors. Passing true causes the immediate suspension.
838 // java_suspend() will catch threads in the process of exiting
839 // and will ignore them.
840 java_thread->java_suspend();
842 // It would be nice to have the following assertion in all the time,
843 // but it is possible for a racing resume request to have resumed
844 // this thread right after we suspended it. Temporarily enable this
845 // assertion if you are chasing a different kind of bug.
846 //
847 // assert(java_lang_Thread::thread(java_thread->threadObj()) == NULL ||
848 // java_thread->is_being_ext_suspended(), "thread is not suspended");
850 if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) {
851 // check again because we can get delayed in java_suspend():
852 // the thread is in process of exiting.
853 return false;
854 }
856 return true;
857 }
859 bool JvmtiSuspendControl::resume(JavaThread *java_thread) {
860 // external suspend should have caught resuming a thread twice
861 assert(java_thread->is_being_ext_suspended(), "thread should be suspended");
863 // resume thread
864 {
865 // must always grab Threads_lock, see JVM_SuspendThread
866 MutexLocker ml(Threads_lock);
867 java_thread->java_resume();
868 }
870 return true;
871 }
874 void JvmtiSuspendControl::print() {
875 #ifndef PRODUCT
876 MutexLocker mu(Threads_lock);
877 ResourceMark rm;
879 tty->print("Suspended Threads: [");
880 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) {
881 #ifdef JVMTI_TRACE
882 const char *name = JvmtiTrace::safe_get_thread_name(thread);
883 #else
884 const char *name = "";
885 #endif /*JVMTI_TRACE */
886 tty->print("%s(%c ", name, thread->is_being_ext_suspended() ? 'S' : '_');
887 if (!thread->has_last_Java_frame()) {
888 tty->print("no stack");
889 }
890 tty->print(") ");
891 }
892 tty->print_cr("]");
893 #endif
894 }
896 JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_load_event(
897 nmethod* nm) {
898 JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_LOAD);
899 event._event_data.compiled_method_load = nm;
900 // Keep the nmethod alive until the ServiceThread can process
901 // this deferred event.
902 nmethodLocker::lock_nmethod(nm);
903 return event;
904 }
906 JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_unload_event(
907 nmethod* nm, jmethodID id, const void* code) {
908 JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_UNLOAD);
909 event._event_data.compiled_method_unload.nm = nm;
910 event._event_data.compiled_method_unload.method_id = id;
911 event._event_data.compiled_method_unload.code_begin = code;
912 // Keep the nmethod alive until the ServiceThread can process
913 // this deferred event. This will keep the memory for the
914 // generated code from being reused too early. We pass
915 // zombie_ok == true here so that our nmethod that was just
916 // made into a zombie can be locked.
917 nmethodLocker::lock_nmethod(nm, true /* zombie_ok */);
918 return event;
919 }
921 JvmtiDeferredEvent JvmtiDeferredEvent::dynamic_code_generated_event(
922 const char* name, const void* code_begin, const void* code_end) {
923 JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_DYNAMIC_CODE_GENERATED);
924 // Need to make a copy of the name since we don't know how long
925 // the event poster will keep it around after we enqueue the
926 // deferred event and return. strdup() failure is handled in
927 // the post() routine below.
928 event._event_data.dynamic_code_generated.name = os::strdup(name);
929 event._event_data.dynamic_code_generated.code_begin = code_begin;
930 event._event_data.dynamic_code_generated.code_end = code_end;
931 return event;
932 }
934 void JvmtiDeferredEvent::post() {
935 assert(ServiceThread::is_service_thread(Thread::current()),
936 "Service thread must post enqueued events");
937 switch(_type) {
938 case TYPE_COMPILED_METHOD_LOAD: {
939 nmethod* nm = _event_data.compiled_method_load;
940 JvmtiExport::post_compiled_method_load(nm);
941 // done with the deferred event so unlock the nmethod
942 nmethodLocker::unlock_nmethod(nm);
943 break;
944 }
945 case TYPE_COMPILED_METHOD_UNLOAD: {
946 nmethod* nm = _event_data.compiled_method_unload.nm;
947 JvmtiExport::post_compiled_method_unload(
948 _event_data.compiled_method_unload.method_id,
949 _event_data.compiled_method_unload.code_begin);
950 // done with the deferred event so unlock the nmethod
951 nmethodLocker::unlock_nmethod(nm);
952 break;
953 }
954 case TYPE_DYNAMIC_CODE_GENERATED: {
955 JvmtiExport::post_dynamic_code_generated_internal(
956 // if strdup failed give the event a default name
957 (_event_data.dynamic_code_generated.name == NULL)
958 ? "unknown_code" : _event_data.dynamic_code_generated.name,
959 _event_data.dynamic_code_generated.code_begin,
960 _event_data.dynamic_code_generated.code_end);
961 if (_event_data.dynamic_code_generated.name != NULL) {
962 // release our copy
963 os::free((void *)_event_data.dynamic_code_generated.name);
964 }
965 break;
966 }
967 default:
968 ShouldNotReachHere();
969 }
970 }
972 JvmtiDeferredEventQueue::QueueNode* JvmtiDeferredEventQueue::_queue_tail = NULL;
973 JvmtiDeferredEventQueue::QueueNode* JvmtiDeferredEventQueue::_queue_head = NULL;
975 volatile JvmtiDeferredEventQueue::QueueNode*
976 JvmtiDeferredEventQueue::_pending_list = NULL;
978 bool JvmtiDeferredEventQueue::has_events() {
979 assert(Service_lock->owned_by_self(), "Must own Service_lock");
980 return _queue_head != NULL || _pending_list != NULL;
981 }
983 void JvmtiDeferredEventQueue::enqueue(const JvmtiDeferredEvent& event) {
984 assert(Service_lock->owned_by_self(), "Must own Service_lock");
986 process_pending_events();
988 // Events get added to the end of the queue (and are pulled off the front).
989 QueueNode* node = new QueueNode(event);
990 if (_queue_tail == NULL) {
991 _queue_tail = _queue_head = node;
992 } else {
993 assert(_queue_tail->next() == NULL, "Must be the last element in the list");
994 _queue_tail->set_next(node);
995 _queue_tail = node;
996 }
998 Service_lock->notify_all();
999 assert((_queue_head == NULL) == (_queue_tail == NULL),
1000 "Inconsistent queue markers");
1001 }
1003 JvmtiDeferredEvent JvmtiDeferredEventQueue::dequeue() {
1004 assert(Service_lock->owned_by_self(), "Must own Service_lock");
1006 process_pending_events();
1008 assert(_queue_head != NULL, "Nothing to dequeue");
1010 if (_queue_head == NULL) {
1011 // Just in case this happens in product; it shouldn't but let's not crash
1012 return JvmtiDeferredEvent();
1013 }
1015 QueueNode* node = _queue_head;
1016 _queue_head = _queue_head->next();
1017 if (_queue_head == NULL) {
1018 _queue_tail = NULL;
1019 }
1021 assert((_queue_head == NULL) == (_queue_tail == NULL),
1022 "Inconsistent queue markers");
1024 JvmtiDeferredEvent event = node->event();
1025 delete node;
1026 return event;
1027 }
1029 void JvmtiDeferredEventQueue::add_pending_event(
1030 const JvmtiDeferredEvent& event) {
1032 QueueNode* node = new QueueNode(event);
1034 bool success = false;
1035 QueueNode* prev_value = (QueueNode*)_pending_list;
1036 do {
1037 node->set_next(prev_value);
1038 prev_value = (QueueNode*)Atomic::cmpxchg_ptr(
1039 (void*)node, (volatile void*)&_pending_list, (void*)node->next());
1040 } while (prev_value != node->next());
1041 }
1043 // This method transfers any events that were added by someone NOT holding
1044 // the lock into the mainline queue.
1045 void JvmtiDeferredEventQueue::process_pending_events() {
1046 assert(Service_lock->owned_by_self(), "Must own Service_lock");
1048 if (_pending_list != NULL) {
1049 QueueNode* head =
1050 (QueueNode*)Atomic::xchg_ptr(NULL, (volatile void*)&_pending_list);
1052 assert((_queue_head == NULL) == (_queue_tail == NULL),
1053 "Inconsistent queue markers");
1055 if (head != NULL) {
1056 // Since we've treated the pending list as a stack (with newer
1057 // events at the beginning), we need to join the bottom of the stack
1058 // with the 'tail' of the queue in order to get the events in the
1059 // right order. We do this by reversing the pending list and appending
1060 // it to the queue.
1062 QueueNode* new_tail = head;
1063 QueueNode* new_head = NULL;
1065 // This reverses the list
1066 QueueNode* prev = new_tail;
1067 QueueNode* node = new_tail->next();
1068 new_tail->set_next(NULL);
1069 while (node != NULL) {
1070 QueueNode* next = node->next();
1071 node->set_next(prev);
1072 prev = node;
1073 node = next;
1074 }
1075 new_head = prev;
1077 // Now append the new list to the queue
1078 if (_queue_tail != NULL) {
1079 _queue_tail->set_next(new_head);
1080 } else { // _queue_head == NULL
1081 _queue_head = new_head;
1082 }
1083 _queue_tail = new_tail;
1084 }
1085 }
1086 }