src/share/vm/prims/jvmtiImpl.cpp

Wed, 08 Oct 2008 08:10:51 -0700

author
ksrini
date
Wed, 08 Oct 2008 08:10:51 -0700
changeset 823
f008d3631bd1
parent 435
a61af66fc99e
child 1046
2f716c0acb64
permissions
-rw-r--r--

6755845: JVM_FindClassFromBoot triggers assertions
Summary: Fixes assertions caused by one jvm_entry calling another, solved by refactoring code and modified gamma test.
Reviewed-by: dholmes, xlu

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

mercurial