jvmtiImpl.cpp revision 1798:fa83ab460c54
1/*
2 * Copyright (c) 2003, 2007, 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 "incls/_precompiled.incl"
26# include "incls/_jvmtiImpl.cpp.incl"
27
28//
29// class JvmtiAgentThread
30//
31// JavaThread used to wrap a thread started by an agent
32// using the JVMTI method RunAgentThread.
33//
34
35JvmtiAgentThread::JvmtiAgentThread(JvmtiEnv* env, jvmtiStartFunction start_fn, const void *start_arg)
36    : JavaThread(start_function_wrapper) {
37    _env = env;
38    _start_fn = start_fn;
39    _start_arg = start_arg;
40}
41
42void
43JvmtiAgentThread::start_function_wrapper(JavaThread *thread, TRAPS) {
44    // It is expected that any Agent threads will be created as
45    // Java Threads.  If this is the case, notification of the creation
46    // of the thread is given in JavaThread::thread_main().
47    assert(thread->is_Java_thread(), "debugger thread should be a Java Thread");
48    assert(thread == JavaThread::current(), "sanity check");
49
50    JvmtiAgentThread *dthread = (JvmtiAgentThread *)thread;
51    dthread->call_start_function();
52}
53
54void
55JvmtiAgentThread::call_start_function() {
56    ThreadToNativeFromVM transition(this);
57    _start_fn(_env->jvmti_external(), jni_environment(), (void*)_start_arg);
58}
59
60
61//
62// class GrowableCache - private methods
63//
64
65void GrowableCache::recache() {
66  int len = _elements->length();
67
68  FREE_C_HEAP_ARRAY(address, _cache);
69  _cache = NEW_C_HEAP_ARRAY(address,len+1);
70
71  for (int i=0; i<len; i++) {
72    _cache[i] = _elements->at(i)->getCacheValue();
73    //
74    // The cache entry has gone bad. Without a valid frame pointer
75    // value, the entry is useless so we simply delete it in product
76    // mode. The call to remove() will rebuild the cache again
77    // without the bad entry.
78    //
79    if (_cache[i] == NULL) {
80      assert(false, "cannot recache NULL elements");
81      remove(i);
82      return;
83    }
84  }
85  _cache[len] = NULL;
86
87  _listener_fun(_this_obj,_cache);
88}
89
90bool GrowableCache::equals(void* v, GrowableElement *e2) {
91  GrowableElement *e1 = (GrowableElement *) v;
92  assert(e1 != NULL, "e1 != NULL");
93  assert(e2 != NULL, "e2 != NULL");
94
95  return e1->equals(e2);
96}
97
98//
99// class GrowableCache - public methods
100//
101
102GrowableCache::GrowableCache() {
103  _this_obj       = NULL;
104  _listener_fun   = NULL;
105  _elements       = NULL;
106  _cache          = NULL;
107}
108
109GrowableCache::~GrowableCache() {
110  clear();
111  delete _elements;
112  FREE_C_HEAP_ARRAY(address, _cache);
113}
114
115void GrowableCache::initialize(void *this_obj, void listener_fun(void *, address*) ) {
116  _this_obj       = this_obj;
117  _listener_fun   = listener_fun;
118  _elements       = new (ResourceObj::C_HEAP) GrowableArray<GrowableElement*>(5,true);
119  recache();
120}
121
122// number of elements in the collection
123int GrowableCache::length() {
124  return _elements->length();
125}
126
127// get the value of the index element in the collection
128GrowableElement* GrowableCache::at(int index) {
129  GrowableElement *e = (GrowableElement *) _elements->at(index);
130  assert(e != NULL, "e != NULL");
131  return e;
132}
133
134int GrowableCache::find(GrowableElement* e) {
135  return _elements->find(e, GrowableCache::equals);
136}
137
138// append a copy of the element to the end of the collection
139void GrowableCache::append(GrowableElement* e) {
140  GrowableElement *new_e = e->clone();
141  _elements->append(new_e);
142  recache();
143}
144
145// insert a copy of the element using lessthan()
146void GrowableCache::insert(GrowableElement* e) {
147  GrowableElement *new_e = e->clone();
148  _elements->append(new_e);
149
150  int n = length()-2;
151  for (int i=n; i>=0; i--) {
152    GrowableElement *e1 = _elements->at(i);
153    GrowableElement *e2 = _elements->at(i+1);
154    if (e2->lessThan(e1)) {
155      _elements->at_put(i+1, e1);
156      _elements->at_put(i,   e2);
157    }
158  }
159
160  recache();
161}
162
163// remove the element at index
164void GrowableCache::remove (int index) {
165  GrowableElement *e = _elements->at(index);
166  assert(e != NULL, "e != NULL");
167  _elements->remove(e);
168  delete e;
169  recache();
170}
171
172// clear out all elements, release all heap space and
173// let our listener know that things have changed.
174void GrowableCache::clear() {
175  int len = _elements->length();
176  for (int i=0; i<len; i++) {
177    delete _elements->at(i);
178  }
179  _elements->clear();
180  recache();
181}
182
183void GrowableCache::oops_do(OopClosure* f) {
184  int len = _elements->length();
185  for (int i=0; i<len; i++) {
186    GrowableElement *e = _elements->at(i);
187    e->oops_do(f);
188  }
189}
190
191void GrowableCache::gc_epilogue() {
192  int len = _elements->length();
193  // recompute the new cache value after GC
194  for (int i=0; i<len; i++) {
195    _cache[i] = _elements->at(i)->getCacheValue();
196  }
197}
198
199//
200// class JvmtiBreakpoint
201//
202
203JvmtiBreakpoint::JvmtiBreakpoint() {
204  _method = NULL;
205  _bci    = 0;
206#ifdef CHECK_UNHANDLED_OOPS
207  // This one is always allocated with new, but check it just in case.
208  Thread *thread = Thread::current();
209  if (thread->is_in_stack((address)&_method)) {
210    thread->allow_unhandled_oop((oop*)&_method);
211  }
212#endif // CHECK_UNHANDLED_OOPS
213}
214
215JvmtiBreakpoint::JvmtiBreakpoint(methodOop m_method, jlocation location) {
216  _method        = m_method;
217  assert(_method != NULL, "_method != NULL");
218  _bci           = (int) location;
219#ifdef CHECK_UNHANDLED_OOPS
220  // Could be allocated with new and wouldn't be on the unhandled oop list.
221  Thread *thread = Thread::current();
222  if (thread->is_in_stack((address)&_method)) {
223    thread->allow_unhandled_oop(&_method);
224  }
225#endif // CHECK_UNHANDLED_OOPS
226
227  assert(_bci >= 0, "_bci >= 0");
228}
229
230void JvmtiBreakpoint::copy(JvmtiBreakpoint& bp) {
231  _method   = bp._method;
232  _bci      = bp._bci;
233}
234
235bool JvmtiBreakpoint::lessThan(JvmtiBreakpoint& bp) {
236  Unimplemented();
237  return false;
238}
239
240bool JvmtiBreakpoint::equals(JvmtiBreakpoint& bp) {
241  return _method   == bp._method
242    &&   _bci      == bp._bci;
243}
244
245bool JvmtiBreakpoint::is_valid() {
246  return _method != NULL &&
247         _bci >= 0;
248}
249
250address JvmtiBreakpoint::getBcp() {
251  return _method->bcp_from(_bci);
252}
253
254void JvmtiBreakpoint::each_method_version_do(method_action meth_act) {
255  ((methodOopDesc*)_method->*meth_act)(_bci);
256
257  // add/remove breakpoint to/from versions of the method that
258  // are EMCP. Directly or transitively obsolete methods are
259  // not saved in the PreviousVersionInfo.
260  Thread *thread = Thread::current();
261  instanceKlassHandle ikh = instanceKlassHandle(thread, _method->method_holder());
262  symbolOop m_name = _method->name();
263  symbolOop m_signature = _method->signature();
264
265  {
266    ResourceMark rm(thread);
267    // PreviousVersionInfo objects returned via PreviousVersionWalker
268    // contain a GrowableArray of handles. We have to clean up the
269    // GrowableArray _after_ the PreviousVersionWalker destructor
270    // has destroyed the handles.
271    {
272      // search previous versions if they exist
273      PreviousVersionWalker pvw((instanceKlass *)ikh()->klass_part());
274      for (PreviousVersionInfo * pv_info = pvw.next_previous_version();
275           pv_info != NULL; pv_info = pvw.next_previous_version()) {
276        GrowableArray<methodHandle>* methods =
277          pv_info->prev_EMCP_method_handles();
278
279        if (methods == NULL) {
280          // We have run into a PreviousVersion generation where
281          // all methods were made obsolete during that generation's
282          // RedefineClasses() operation. At the time of that
283          // operation, all EMCP methods were flushed so we don't
284          // have to go back any further.
285          //
286          // A NULL methods array is different than an empty methods
287          // array. We cannot infer any optimizations about older
288          // generations from an empty methods array for the current
289          // generation.
290          break;
291        }
292
293        for (int i = methods->length() - 1; i >= 0; i--) {
294          methodHandle method = methods->at(i);
295          if (method->name() == m_name && method->signature() == m_signature) {
296            RC_TRACE(0x00000800, ("%sing breakpoint in %s(%s)",
297              meth_act == &methodOopDesc::set_breakpoint ? "sett" : "clear",
298              method->name()->as_C_string(),
299              method->signature()->as_C_string()));
300            assert(!method->is_obsolete(), "only EMCP methods here");
301
302            ((methodOopDesc*)method()->*meth_act)(_bci);
303            break;
304          }
305        }
306      }
307    } // pvw is cleaned up
308  } // rm is cleaned up
309}
310
311void JvmtiBreakpoint::set() {
312  each_method_version_do(&methodOopDesc::set_breakpoint);
313}
314
315void JvmtiBreakpoint::clear() {
316  each_method_version_do(&methodOopDesc::clear_breakpoint);
317}
318
319void JvmtiBreakpoint::print() {
320#ifndef PRODUCT
321  const char *class_name  = (_method == NULL) ? "NULL" : _method->klass_name()->as_C_string();
322  const char *method_name = (_method == NULL) ? "NULL" : _method->name()->as_C_string();
323
324  tty->print("Breakpoint(%s,%s,%d,%p)",class_name, method_name, _bci, getBcp());
325#endif
326}
327
328
329//
330// class VM_ChangeBreakpoints
331//
332// Modify the Breakpoints data structure at a safepoint
333//
334
335void VM_ChangeBreakpoints::doit() {
336  switch (_operation) {
337  case SET_BREAKPOINT:
338    _breakpoints->set_at_safepoint(*_bp);
339    break;
340  case CLEAR_BREAKPOINT:
341    _breakpoints->clear_at_safepoint(*_bp);
342    break;
343  case CLEAR_ALL_BREAKPOINT:
344    _breakpoints->clearall_at_safepoint();
345    break;
346  default:
347    assert(false, "Unknown operation");
348  }
349}
350
351void VM_ChangeBreakpoints::oops_do(OopClosure* f) {
352  // This operation keeps breakpoints alive
353  if (_breakpoints != NULL) {
354    _breakpoints->oops_do(f);
355  }
356  if (_bp != NULL) {
357    _bp->oops_do(f);
358  }
359}
360
361//
362// class JvmtiBreakpoints
363//
364// a JVMTI internal collection of JvmtiBreakpoint
365//
366
367JvmtiBreakpoints::JvmtiBreakpoints(void listener_fun(void *,address *)) {
368  _bps.initialize(this,listener_fun);
369}
370
371JvmtiBreakpoints:: ~JvmtiBreakpoints() {}
372
373void  JvmtiBreakpoints::oops_do(OopClosure* f) {
374  _bps.oops_do(f);
375}
376
377void  JvmtiBreakpoints::gc_epilogue() {
378  _bps.gc_epilogue();
379}
380
381void  JvmtiBreakpoints::print() {
382#ifndef PRODUCT
383  ResourceMark rm;
384
385  int n = _bps.length();
386  for (int i=0; i<n; i++) {
387    JvmtiBreakpoint& bp = _bps.at(i);
388    tty->print("%d: ", i);
389    bp.print();
390    tty->print_cr("");
391  }
392#endif
393}
394
395
396void JvmtiBreakpoints::set_at_safepoint(JvmtiBreakpoint& bp) {
397  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
398
399  int i = _bps.find(bp);
400  if (i == -1) {
401    _bps.append(bp);
402    bp.set();
403  }
404}
405
406void JvmtiBreakpoints::clear_at_safepoint(JvmtiBreakpoint& bp) {
407  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
408
409  int i = _bps.find(bp);
410  if (i != -1) {
411    _bps.remove(i);
412    bp.clear();
413  }
414}
415
416void JvmtiBreakpoints::clearall_at_safepoint() {
417  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
418
419  int len = _bps.length();
420  for (int i=0; i<len; i++) {
421    _bps.at(i).clear();
422  }
423  _bps.clear();
424}
425
426int JvmtiBreakpoints::length() { return _bps.length(); }
427
428int JvmtiBreakpoints::set(JvmtiBreakpoint& bp) {
429  if ( _bps.find(bp) != -1) {
430     return JVMTI_ERROR_DUPLICATE;
431  }
432  VM_ChangeBreakpoints set_breakpoint(this,VM_ChangeBreakpoints::SET_BREAKPOINT, &bp);
433  VMThread::execute(&set_breakpoint);
434  return JVMTI_ERROR_NONE;
435}
436
437int JvmtiBreakpoints::clear(JvmtiBreakpoint& bp) {
438  if ( _bps.find(bp) == -1) {
439     return JVMTI_ERROR_NOT_FOUND;
440  }
441
442  VM_ChangeBreakpoints clear_breakpoint(this,VM_ChangeBreakpoints::CLEAR_BREAKPOINT, &bp);
443  VMThread::execute(&clear_breakpoint);
444  return JVMTI_ERROR_NONE;
445}
446
447void JvmtiBreakpoints::clearall_in_class_at_safepoint(klassOop 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}
472
473void JvmtiBreakpoints::clearall() {
474  VM_ChangeBreakpoints clearall_breakpoint(this,VM_ChangeBreakpoints::CLEAR_ALL_BREAKPOINT);
475  VMThread::execute(&clearall_breakpoint);
476}
477
478//
479// class JvmtiCurrentBreakpoints
480//
481
482JvmtiBreakpoints *JvmtiCurrentBreakpoints::_jvmti_breakpoints  = NULL;
483address *         JvmtiCurrentBreakpoints::_breakpoint_list    = NULL;
484
485
486JvmtiBreakpoints& JvmtiCurrentBreakpoints::get_jvmti_breakpoints() {
487  if (_jvmti_breakpoints != NULL) return (*_jvmti_breakpoints);
488  _jvmti_breakpoints = new JvmtiBreakpoints(listener_fun);
489  assert(_jvmti_breakpoints != NULL, "_jvmti_breakpoints != NULL");
490  return (*_jvmti_breakpoints);
491}
492
493void  JvmtiCurrentBreakpoints::listener_fun(void *this_obj, address *cache) {
494  JvmtiBreakpoints *this_jvmti = (JvmtiBreakpoints *) this_obj;
495  assert(this_jvmti != NULL, "this_jvmti != NULL");
496
497  debug_only(int n = this_jvmti->length(););
498  assert(cache[n] == NULL, "cache must be NULL terminated");
499
500  set_breakpoint_list(cache);
501}
502
503
504void JvmtiCurrentBreakpoints::oops_do(OopClosure* f) {
505  if (_jvmti_breakpoints != NULL) {
506    _jvmti_breakpoints->oops_do(f);
507  }
508}
509
510void JvmtiCurrentBreakpoints::gc_epilogue() {
511  if (_jvmti_breakpoints != NULL) {
512    _jvmti_breakpoints->gc_epilogue();
513  }
514}
515
516
517///////////////////////////////////////////////////////////////
518//
519// class VM_GetOrSetLocal
520//
521
522// Constructor for non-object getter
523VM_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}
534
535// Constructor for object or non-object setter
536VM_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}
548
549// Constructor for object getter
550VM_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}
561
562
563vframe *VM_GetOrSetLocal::get_vframe() {
564  if (!_thread->has_last_Java_frame()) {
565    return NULL;
566  }
567  RegisterMap reg_map(_thread);
568  vframe *vf = _thread->last_java_vframe(&reg_map);
569  int d = 0;
570  while ((vf != NULL) && (d < _depth)) {
571    vf = vf->java_sender();
572    d++;
573  }
574  return vf;
575}
576
577javaVFrame *VM_GetOrSetLocal::get_java_vframe() {
578  vframe* vf = get_vframe();
579  if (vf == NULL) {
580    _result = JVMTI_ERROR_NO_MORE_FRAMES;
581    return NULL;
582  }
583  javaVFrame *jvf = (javaVFrame*)vf;
584
585  if (!vf->is_java_frame() || jvf->method()->is_native()) {
586    _result = JVMTI_ERROR_OPAQUE_FRAME;
587    return NULL;
588  }
589  return jvf;
590}
591
592// Check that the klass is assignable to a type with the given signature.
593// Another solution could be to use the function Klass::is_subtype_of(type).
594// But the type class can be forced to load/initialize eagerly in such a case.
595// This may cause unexpected consequences like CFLH or class-init JVMTI events.
596// It is better to avoid such a behavior.
597bool VM_GetOrSetLocal::is_assignable(const char* ty_sign, Klass* klass, Thread* thread) {
598  assert(ty_sign != NULL, "type signature must not be NULL");
599  assert(thread != NULL, "thread must not be NULL");
600  assert(klass != NULL, "klass must not be NULL");
601
602  int len = (int) strlen(ty_sign);
603  if (ty_sign[0] == 'L' && ty_sign[len-1] == ';') { // Need pure class/interface name
604    ty_sign++;
605    len -= 2;
606  }
607  symbolHandle ty_sym = oopFactory::new_symbol_handle(ty_sign, len, thread);
608  if (klass->name() == ty_sym()) {
609    return true;
610  }
611  // Compare primary supers
612  int super_depth = klass->super_depth();
613  int idx;
614  for (idx = 0; idx < super_depth; idx++) {
615    if (Klass::cast(klass->primary_super_of_depth(idx))->name() == ty_sym()) {
616      return true;
617    }
618  }
619  // Compare secondary supers
620  objArrayOop sec_supers = klass->secondary_supers();
621  for (idx = 0; idx < sec_supers->length(); idx++) {
622    if (Klass::cast((klassOop) sec_supers->obj_at(idx))->name() == ty_sym()) {
623      return true;
624    }
625  }
626  return false;
627}
628
629// Checks error conditions:
630//   JVMTI_ERROR_INVALID_SLOT
631//   JVMTI_ERROR_TYPE_MISMATCH
632// Returns: 'true' - everything is Ok, 'false' - error code
633
634bool VM_GetOrSetLocal::check_slot_type(javaVFrame* jvf) {
635  methodOop method_oop = jvf->method();
636  if (!method_oop->has_localvariable_table()) {
637    // Just to check index boundaries
638    jint extra_slot = (_type == T_LONG || _type == T_DOUBLE) ? 1 : 0;
639    if (_index < 0 || _index + extra_slot >= method_oop->max_locals()) {
640      _result = JVMTI_ERROR_INVALID_SLOT;
641      return false;
642    }
643    return true;
644  }
645
646  jint num_entries = method_oop->localvariable_table_length();
647  if (num_entries == 0) {
648    _result = JVMTI_ERROR_INVALID_SLOT;
649    return false;       // There are no slots
650  }
651  int signature_idx = -1;
652  int vf_bci = jvf->bci();
653  LocalVariableTableElement* table = method_oop->localvariable_table_start();
654  for (int i = 0; i < num_entries; i++) {
655    int start_bci = table[i].start_bci;
656    int end_bci = start_bci + table[i].length;
657
658    // Here we assume that locations of LVT entries
659    // with the same slot number cannot be overlapped
660    if (_index == (jint) table[i].slot && start_bci <= vf_bci && vf_bci <= end_bci) {
661      signature_idx = (int) table[i].descriptor_cp_index;
662      break;
663    }
664  }
665  if (signature_idx == -1) {
666    _result = JVMTI_ERROR_INVALID_SLOT;
667    return false;       // Incorrect slot index
668  }
669  symbolOop   sign_sym  = method_oop->constants()->symbol_at(signature_idx);
670  const char* signature = (const char *) sign_sym->as_utf8();
671  BasicType slot_type = char2type(signature[0]);
672
673  switch (slot_type) {
674  case T_BYTE:
675  case T_SHORT:
676  case T_CHAR:
677  case T_BOOLEAN:
678    slot_type = T_INT;
679    break;
680  case T_ARRAY:
681    slot_type = T_OBJECT;
682    break;
683  };
684  if (_type != slot_type) {
685    _result = JVMTI_ERROR_TYPE_MISMATCH;
686    return false;
687  }
688
689  jobject jobj = _value.l;
690  if (_set && slot_type == T_OBJECT && jobj != NULL) { // NULL reference is allowed
691    // Check that the jobject class matches the return type signature.
692    JavaThread* cur_thread = JavaThread::current();
693    HandleMark hm(cur_thread);
694
695    Handle obj = Handle(cur_thread, JNIHandles::resolve_external_guard(jobj));
696    NULL_CHECK(obj, (_result = JVMTI_ERROR_INVALID_OBJECT, false));
697    KlassHandle ob_kh = KlassHandle(cur_thread, obj->klass());
698    NULL_CHECK(ob_kh, (_result = JVMTI_ERROR_INVALID_OBJECT, false));
699
700    if (!is_assignable(signature, Klass::cast(ob_kh()), cur_thread)) {
701      _result = JVMTI_ERROR_TYPE_MISMATCH;
702      return false;
703    }
704  }
705  return true;
706}
707
708static bool can_be_deoptimized(vframe* vf) {
709  return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());
710}
711
712bool VM_GetOrSetLocal::doit_prologue() {
713  _jvf = get_java_vframe();
714  NULL_CHECK(_jvf, false);
715
716  if (!check_slot_type(_jvf)) {
717    return false;
718  }
719  return true;
720}
721
722void VM_GetOrSetLocal::doit() {
723  if (_set) {
724    // Force deoptimization of frame if compiled because it's
725    // possible the compiler emitted some locals as constant values,
726    // meaning they are not mutable.
727    if (can_be_deoptimized(_jvf)) {
728
729      // Schedule deoptimization so that eventually the local
730      // update will be written to an interpreter frame.
731      VM_DeoptimizeFrame deopt(_jvf->thread(), _jvf->fr().id());
732      VMThread::execute(&deopt);
733
734      // Now store a new value for the local which will be applied
735      // once deoptimization occurs. Note however that while this
736      // write is deferred until deoptimization actually happens
737      // can vframe created after this point will have its locals
738      // reflecting this update so as far as anyone can see the
739      // write has already taken place.
740
741      // If we are updating an oop then get the oop from the handle
742      // since the handle will be long gone by the time the deopt
743      // happens. The oop stored in the deferred local will be
744      // gc'd on its own.
745      if (_type == T_OBJECT) {
746        _value.l = (jobject) (JNIHandles::resolve_external_guard(_value.l));
747      }
748      // Re-read the vframe so we can see that it is deoptimized
749      // [ Only need because of assert in update_local() ]
750      _jvf = get_java_vframe();
751      ((compiledVFrame*)_jvf)->update_local(_type, _index, _value);
752      return;
753    }
754    StackValueCollection *locals = _jvf->locals();
755    HandleMark hm;
756
757    switch (_type) {
758    case T_INT:    locals->set_int_at   (_index, _value.i); break;
759    case T_LONG:   locals->set_long_at  (_index, _value.j); break;
760    case T_FLOAT:  locals->set_float_at (_index, _value.f); break;
761    case T_DOUBLE: locals->set_double_at(_index, _value.d); break;
762    case T_OBJECT: {
763      Handle ob_h(JNIHandles::resolve_external_guard(_value.l));
764      locals->set_obj_at (_index, ob_h);
765      break;
766    }
767    default: ShouldNotReachHere();
768    }
769    _jvf->set_locals(locals);
770  } else {
771    StackValueCollection *locals = _jvf->locals();
772
773    if (locals->at(_index)->type() == T_CONFLICT) {
774      memset(&_value, 0, sizeof(_value));
775      _value.l = NULL;
776      return;
777    }
778
779    switch (_type) {
780    case T_INT:    _value.i = locals->int_at   (_index);   break;
781    case T_LONG:   _value.j = locals->long_at  (_index);   break;
782    case T_FLOAT:  _value.f = locals->float_at (_index);   break;
783    case T_DOUBLE: _value.d = locals->double_at(_index);   break;
784    case T_OBJECT: {
785      // Wrap the oop to be returned in a local JNI handle since
786      // oops_do() no longer applies after doit() is finished.
787      oop obj = locals->obj_at(_index)();
788      _value.l = JNIHandles::make_local(_calling_thread, obj);
789      break;
790    }
791    default: ShouldNotReachHere();
792    }
793  }
794}
795
796
797bool VM_GetOrSetLocal::allow_nested_vm_operations() const {
798  return true; // May need to deoptimize
799}
800
801
802/////////////////////////////////////////////////////////////////////////////////////////
803
804//
805// class JvmtiSuspendControl - see comments in jvmtiImpl.hpp
806//
807
808bool JvmtiSuspendControl::suspend(JavaThread *java_thread) {
809  // external suspend should have caught suspending a thread twice
810
811  // Immediate suspension required for JPDA back-end so JVMTI agent threads do
812  // not deadlock due to later suspension on transitions while holding
813  // raw monitors.  Passing true causes the immediate suspension.
814  // java_suspend() will catch threads in the process of exiting
815  // and will ignore them.
816  java_thread->java_suspend();
817
818  // It would be nice to have the following assertion in all the time,
819  // but it is possible for a racing resume request to have resumed
820  // this thread right after we suspended it. Temporarily enable this
821  // assertion if you are chasing a different kind of bug.
822  //
823  // assert(java_lang_Thread::thread(java_thread->threadObj()) == NULL ||
824  //   java_thread->is_being_ext_suspended(), "thread is not suspended");
825
826  if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) {
827    // check again because we can get delayed in java_suspend():
828    // the thread is in process of exiting.
829    return false;
830  }
831
832  return true;
833}
834
835bool JvmtiSuspendControl::resume(JavaThread *java_thread) {
836  // external suspend should have caught resuming a thread twice
837  assert(java_thread->is_being_ext_suspended(), "thread should be suspended");
838
839  // resume thread
840  {
841    // must always grab Threads_lock, see JVM_SuspendThread
842    MutexLocker ml(Threads_lock);
843    java_thread->java_resume();
844  }
845
846  return true;
847}
848
849
850void JvmtiSuspendControl::print() {
851#ifndef PRODUCT
852  MutexLocker mu(Threads_lock);
853  ResourceMark rm;
854
855  tty->print("Suspended Threads: [");
856  for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) {
857#if JVMTI_TRACE
858    const char *name   = JvmtiTrace::safe_get_thread_name(thread);
859#else
860    const char *name   = "";
861#endif /*JVMTI_TRACE */
862    tty->print("%s(%c ", name, thread->is_being_ext_suspended() ? 'S' : '_');
863    if (!thread->has_last_Java_frame()) {
864      tty->print("no stack");
865    }
866    tty->print(") ");
867  }
868  tty->print_cr("]");
869#endif
870}
871