src/share/vm/prims/jvmtiEnv.cpp

Mon, 02 Mar 2009 14:05:07 -0700

author
dcubed
date
Mon, 02 Mar 2009 14:05:07 -0700
changeset 1046
2f716c0acb64
parent 1044
ea20d7ce26b0
child 1557
dcb15a6f342d
child 1577
4ce7240d622c
permissions
-rw-r--r--

6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
Summary: Change JvmtiEnvBase::is_valid() and JvmtiRawMonitor::is_valid() to fetch the _magic fields via Bytes::get_native_u[248]().
Reviewed-by: coleenp, swamyv

     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  */
    26 # include "incls/_precompiled.incl"
    27 # include "incls/_jvmtiEnv.cpp.incl"
    30 #define FIXLATER 0 // REMOVE this when completed.
    32  // FIXLATER: hook into JvmtiTrace
    33 #define TraceJVMTICalls false
    35 JvmtiEnv::JvmtiEnv() : JvmtiEnvBase() {
    36 }
    38 JvmtiEnv::~JvmtiEnv() {
    39 }
    41 JvmtiEnv*
    42 JvmtiEnv::create_a_jvmti() {
    43   return new JvmtiEnv();
    44 }
    46 // VM operation class to copy jni function table at safepoint.
    47 // More than one java threads or jvmti agents may be reading/
    48 // modifying jni function tables. To reduce the risk of bad
    49 // interaction b/w these threads it is copied at safepoint.
    50 class VM_JNIFunctionTableCopier : public VM_Operation {
    51  private:
    52   const struct JNINativeInterface_ *_function_table;
    53  public:
    54   VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
    55     _function_table = func_tbl;
    56   };
    58   VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
    59   void doit() {
    60     copy_jni_function_table(_function_table);
    61   };
    62 };
    64 //
    65 // Do not change the "prefix" marker below, everything above it is copied
    66 // unchanged into the filled stub, everything below is controlled by the
    67 // stub filler (only method bodies are carried forward, and then only for
    68 // functionality still in the spec).
    69 //
    70 // end file prefix
    72   //
    73   // Memory Management functions
    74   //
    76 // mem_ptr - pre-checked for NULL
    77 jvmtiError
    78 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
    79   return allocate(size, mem_ptr);
    80 } /* end Allocate */
    83 // mem - NULL is a valid value, must be checked
    84 jvmtiError
    85 JvmtiEnv::Deallocate(unsigned char* mem) {
    86   return deallocate(mem);
    87 } /* end Deallocate */
    89 // Threads_lock NOT held, java_thread not protected by lock
    90 // java_thread - pre-checked
    91 // data - NULL is a valid value, must be checked
    92 jvmtiError
    93 JvmtiEnv::SetThreadLocalStorage(JavaThread* java_thread, const void* data) {
    94   JvmtiThreadState* state = java_thread->jvmti_thread_state();
    95   if (state == NULL) {
    96     if (data == NULL) {
    97       // leaving state unset same as data set to NULL
    98       return JVMTI_ERROR_NONE;
    99     }
   100     // otherwise, create the state
   101     state = JvmtiThreadState::state_for(java_thread);
   102     if (state == NULL) {
   103       return JVMTI_ERROR_THREAD_NOT_ALIVE;
   104     }
   105   }
   106   state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
   107   return JVMTI_ERROR_NONE;
   108 } /* end SetThreadLocalStorage */
   111 // Threads_lock NOT held
   112 // thread - NOT pre-checked
   113 // data_ptr - pre-checked for NULL
   114 jvmtiError
   115 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
   116   JavaThread* current_thread = JavaThread::current();
   117   if (thread == NULL) {
   118     JvmtiThreadState* state = current_thread->jvmti_thread_state();
   119     *data_ptr = (state == NULL) ? NULL :
   120       state->env_thread_state(this)->get_agent_thread_local_storage_data();
   121   } else {
   123     // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
   124     // the thread to _thread_in_vm. However, when the TLS for a thread
   125     // other than the current thread is required we need to transition
   126     // from native so as to resolve the jthread.
   128     ThreadInVMfromNative __tiv(current_thread);
   129     __ENTRY(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
   130     debug_only(VMNativeEntryWrapper __vew;)
   132     oop thread_oop = JNIHandles::resolve_external_guard(thread);
   133     if (thread_oop == NULL) {
   134       return JVMTI_ERROR_INVALID_THREAD;
   135     }
   136     if (!thread_oop->is_a(SystemDictionary::thread_klass())) {
   137       return JVMTI_ERROR_INVALID_THREAD;
   138     }
   139     JavaThread* java_thread = java_lang_Thread::thread(thread_oop);
   140     if (java_thread == NULL) {
   141       return JVMTI_ERROR_THREAD_NOT_ALIVE;
   142     }
   143     JvmtiThreadState* state = java_thread->jvmti_thread_state();
   144     *data_ptr = (state == NULL) ? NULL :
   145       state->env_thread_state(this)->get_agent_thread_local_storage_data();
   146   }
   147   return JVMTI_ERROR_NONE;
   148 } /* end GetThreadLocalStorage */
   150   //
   151   // Class functions
   152   //
   154 // class_count_ptr - pre-checked for NULL
   155 // classes_ptr - pre-checked for NULL
   156 jvmtiError
   157 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
   158   return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
   159 } /* end GetLoadedClasses */
   162 // initiating_loader - NULL is a valid value, must be checked
   163 // class_count_ptr - pre-checked for NULL
   164 // classes_ptr - pre-checked for NULL
   165 jvmtiError
   166 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
   167   return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
   168                                                   class_count_ptr, classes_ptr);
   169 } /* end GetClassLoaderClasses */
   171 // k_mirror - may be primitive, this must be checked
   172 // is_modifiable_class_ptr - pre-checked for NULL
   173 jvmtiError
   174 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
   175   *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
   176                                                        JNI_TRUE : JNI_FALSE;
   177   return JVMTI_ERROR_NONE;
   178 } /* end IsModifiableClass */
   180 // class_count - pre-checked to be greater than or equal to 0
   181 // classes - pre-checked for NULL
   182 jvmtiError
   183 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
   184 //TODO: add locking
   186   int index;
   187   JavaThread* current_thread = JavaThread::current();
   188   ResourceMark rm(current_thread);
   190   jvmtiClassDefinition* class_definitions =
   191                             NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
   192   NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
   194   for (index = 0; index < class_count; index++) {
   195     HandleMark hm(current_thread);
   197     jclass jcls = classes[index];
   198     oop k_mirror = JNIHandles::resolve_external_guard(jcls);
   199     if (k_mirror == NULL) {
   200       return JVMTI_ERROR_INVALID_CLASS;
   201     }
   202     if (!k_mirror->is_a(SystemDictionary::class_klass())) {
   203       return JVMTI_ERROR_INVALID_CLASS;
   204     }
   206     if (java_lang_Class::is_primitive(k_mirror)) {
   207       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
   208     }
   210     klassOop k_oop = java_lang_Class::as_klassOop(k_mirror);
   211     KlassHandle klass(current_thread, k_oop);
   213     jint status = klass->jvmti_class_status();
   214     if (status & (JVMTI_CLASS_STATUS_ERROR)) {
   215       return JVMTI_ERROR_INVALID_CLASS;
   216     }
   217     if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
   218       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
   219     }
   221     instanceKlassHandle ikh(current_thread, k_oop);
   222     if (ikh->get_cached_class_file_bytes() == NULL) {
   223       // not cached, we need to reconstitute the class file from VM representation
   224       constantPoolHandle  constants(current_thread, ikh->constants());
   225       ObjectLocker ol(constants, current_thread);    // lock constant pool while we query it
   227       JvmtiClassFileReconstituter reconstituter(ikh);
   228       if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
   229         return reconstituter.get_error();
   230       }
   232       class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
   233       class_definitions[index].class_bytes      = (unsigned char*)
   234                                                        reconstituter.class_file_bytes();
   235     } else {
   236       // it is cached, get it from the cache
   237       class_definitions[index].class_byte_count = ikh->get_cached_class_file_len();
   238       class_definitions[index].class_bytes      = ikh->get_cached_class_file_bytes();
   239     }
   240     class_definitions[index].klass              = jcls;
   241   }
   242   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
   243   VMThread::execute(&op);
   244   return (op.check_error());
   245 } /* end RetransformClasses */
   248 // class_count - pre-checked to be greater than or equal to 0
   249 // class_definitions - pre-checked for NULL
   250 jvmtiError
   251 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
   252 //TODO: add locking
   253   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
   254   VMThread::execute(&op);
   255   return (op.check_error());
   256 } /* end RedefineClasses */
   259   //
   260   // Object functions
   261   //
   263 // size_ptr - pre-checked for NULL
   264 jvmtiError
   265 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
   266   oop mirror = JNIHandles::resolve_external_guard(object);
   267   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
   269   if (mirror->klass() == SystemDictionary::class_klass()) {
   270     if (!java_lang_Class::is_primitive(mirror)) {
   271         mirror = java_lang_Class::as_klassOop(mirror);
   272         assert(mirror != NULL, "class for non-primitive mirror must exist");
   273     }
   274   }
   276   *size_ptr = mirror->size() * wordSize;
   277   return JVMTI_ERROR_NONE;
   278 } /* end GetObjectSize */
   280   //
   281   // Method functions
   282   //
   284 // prefix - NULL is a valid value, must be checked
   285 jvmtiError
   286 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
   287   return prefix == NULL?
   288               SetNativeMethodPrefixes(0, NULL) :
   289               SetNativeMethodPrefixes(1, (char**)&prefix);
   290 } /* end SetNativeMethodPrefix */
   293 // prefix_count - pre-checked to be greater than or equal to 0
   294 // prefixes - pre-checked for NULL
   295 jvmtiError
   296 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
   297   // Have to grab JVMTI thread state lock to be sure that some thread
   298   // isn't accessing the prefixes at the same time we are setting them.
   299   // No locks during VM bring-up.
   300   if (Threads::number_of_threads() == 0) {
   301     return set_native_method_prefixes(prefix_count, prefixes);
   302   } else {
   303     MutexLocker mu(JvmtiThreadState_lock);
   304     return set_native_method_prefixes(prefix_count, prefixes);
   305   }
   306 } /* end SetNativeMethodPrefixes */
   308   //
   309   // Event Management functions
   310   //
   312 // callbacks - NULL is a valid value, must be checked
   313 // size_of_callbacks - pre-checked to be greater than or equal to 0
   314 jvmtiError
   315 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
   316   JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
   317   return JVMTI_ERROR_NONE;
   318 } /* end SetEventCallbacks */
   321 // event_thread - NULL is a valid value, must be checked
   322 jvmtiError
   323 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread,   ...) {
   324   JavaThread* java_thread = NULL;
   325   if (event_thread != NULL) {
   326     oop thread_oop = JNIHandles::resolve_external_guard(event_thread);
   327     if (thread_oop == NULL) {
   328       return JVMTI_ERROR_INVALID_THREAD;
   329     }
   330     if (!thread_oop->is_a(SystemDictionary::thread_klass())) {
   331       return JVMTI_ERROR_INVALID_THREAD;
   332     }
   333     java_thread = java_lang_Thread::thread(thread_oop);
   334     if (java_thread == NULL) {
   335       return JVMTI_ERROR_THREAD_NOT_ALIVE;
   336     }
   337   }
   339   // event_type must be valid
   340   if (!JvmtiEventController::is_valid_event_type(event_type)) {
   341     return JVMTI_ERROR_INVALID_EVENT_TYPE;
   342   }
   344   // global events cannot be controlled at thread level.
   345   if (java_thread != NULL && JvmtiEventController::is_global_event(event_type)) {
   346     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
   347   }
   349   bool enabled = (mode == JVMTI_ENABLE);
   351   // assure that needed capabilities are present
   352   if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
   353     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
   354   }
   356   if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
   357     record_class_file_load_hook_enabled();
   358   }
   359   JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);
   361   return JVMTI_ERROR_NONE;
   362 } /* end SetEventNotificationMode */
   364   //
   365   // Capability functions
   366   //
   368 // capabilities_ptr - pre-checked for NULL
   369 jvmtiError
   370 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
   371   JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
   372                                                       get_prohibited_capabilities(),
   373                                                       capabilities_ptr);
   374   return JVMTI_ERROR_NONE;
   375 } /* end GetPotentialCapabilities */
   378 // capabilities_ptr - pre-checked for NULL
   379 jvmtiError
   380 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
   381   return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
   382                                                    get_prohibited_capabilities(),
   383                                                    capabilities_ptr,
   384                                                    get_capabilities());
   385 } /* end AddCapabilities */
   388 // capabilities_ptr - pre-checked for NULL
   389 jvmtiError
   390 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
   391   JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
   392   return JVMTI_ERROR_NONE;
   393 } /* end RelinquishCapabilities */
   396 // capabilities_ptr - pre-checked for NULL
   397 jvmtiError
   398 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
   399   JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
   400   return JVMTI_ERROR_NONE;
   401 } /* end GetCapabilities */
   403   //
   404   // Class Loader Search functions
   405   //
   407 // segment - pre-checked for NULL
   408 jvmtiError
   409 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
   410   jvmtiPhase phase = get_phase();
   411   if (phase == JVMTI_PHASE_ONLOAD) {
   412     Arguments::append_sysclasspath(segment);
   413     return JVMTI_ERROR_NONE;
   414   } else {
   415     assert(phase == JVMTI_PHASE_LIVE, "sanity check");
   417     // create the zip entry
   418     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment);
   419     if (zip_entry == NULL) {
   420       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
   421     }
   423     // lock the loader
   424     Thread* thread = Thread::current();
   425     HandleMark hm;
   426     Handle loader_lock = Handle(thread, SystemDictionary::system_loader_lock());
   428     ObjectLocker ol(loader_lock, thread);
   430     // add the jar file to the bootclasspath
   431     if (TraceClassLoading) {
   432       tty->print_cr("[Opened %s]", zip_entry->name());
   433     }
   434     ClassLoader::add_to_list(zip_entry);
   435     return JVMTI_ERROR_NONE;
   436   }
   438 } /* end AddToBootstrapClassLoaderSearch */
   441 // segment - pre-checked for NULL
   442 jvmtiError
   443 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
   444   jvmtiPhase phase = get_phase();
   446   if (phase == JVMTI_PHASE_ONLOAD) {
   447     for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
   448       if (strcmp("java.class.path", p->key()) == 0) {
   449         p->append_value(segment);
   450         break;
   451       }
   452     }
   453     return JVMTI_ERROR_NONE;
   454   } else {
   455     HandleMark hm;
   457     assert(phase == JVMTI_PHASE_LIVE, "sanity check");
   459     // create the zip entry (which will open the zip file and hence
   460     // check that the segment is indeed a zip file).
   461     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment);
   462     if (zip_entry == NULL) {
   463       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
   464     }
   465     delete zip_entry;   // no longer needed
   467     // lock the loader
   468     Thread* THREAD = Thread::current();
   469     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
   471     ObjectLocker ol(loader, THREAD);
   473     // need the path as java.lang.String
   474     Handle path = java_lang_String::create_from_str(segment, THREAD);
   475     if (HAS_PENDING_EXCEPTION) {
   476       CLEAR_PENDING_EXCEPTION;
   477       return JVMTI_ERROR_INTERNAL;
   478     }
   480     instanceKlassHandle loader_ik(THREAD, loader->klass());
   482     // Invoke the appendToClassPathForInstrumentation method - if the method
   483     // is not found it means the loader doesn't support adding to the class path
   484     // in the live phase.
   485     {
   486       JavaValue res(T_VOID);
   487       JavaCalls::call_special(&res,
   488                               loader,
   489                               loader_ik,
   490                               vmSymbolHandles::appendToClassPathForInstrumentation_name(),
   491                               vmSymbolHandles::appendToClassPathForInstrumentation_signature(),
   492                               path,
   493                               THREAD);
   494       if (HAS_PENDING_EXCEPTION) {
   495         symbolOop ex_name = PENDING_EXCEPTION->klass()->klass_part()->name();
   496         CLEAR_PENDING_EXCEPTION;
   498         if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
   499           return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
   500         } else {
   501           return JVMTI_ERROR_INTERNAL;
   502         }
   503       }
   504     }
   506     return JVMTI_ERROR_NONE;
   507   }
   508 } /* end AddToSystemClassLoaderSearch */
   510   //
   511   // General functions
   512   //
   514 // phase_ptr - pre-checked for NULL
   515 jvmtiError
   516 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
   517   *phase_ptr = get_phase();
   518   return JVMTI_ERROR_NONE;
   519 } /* end GetPhase */
   522 jvmtiError
   523 JvmtiEnv::DisposeEnvironment() {
   524   dispose();
   525   return JVMTI_ERROR_NONE;
   526 } /* end DisposeEnvironment */
   529 // data - NULL is a valid value, must be checked
   530 jvmtiError
   531 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
   532   set_env_local_storage(data);
   533   return JVMTI_ERROR_NONE;
   534 } /* end SetEnvironmentLocalStorage */
   537 // data_ptr - pre-checked for NULL
   538 jvmtiError
   539 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
   540   *data_ptr = (void*)get_env_local_storage();
   541   return JVMTI_ERROR_NONE;
   542 } /* end GetEnvironmentLocalStorage */
   544 // version_ptr - pre-checked for NULL
   545 jvmtiError
   546 JvmtiEnv::GetVersionNumber(jint* version_ptr) {
   547   *version_ptr = JVMTI_VERSION;
   548   return JVMTI_ERROR_NONE;
   549 } /* end GetVersionNumber */
   552 // name_ptr - pre-checked for NULL
   553 jvmtiError
   554 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
   555   if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
   556     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
   557   }
   558   const char *name = JvmtiUtil::error_name(error);
   559   if (name == NULL) {
   560     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
   561   }
   562   size_t len = strlen(name) + 1;
   563   jvmtiError err = allocate(len, (unsigned char**)name_ptr);
   564   if (err == JVMTI_ERROR_NONE) {
   565     memcpy(*name_ptr, name, len);
   566   }
   567   return err;
   568 } /* end GetErrorName */
   571 jvmtiError
   572 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
   573   switch (flag) {
   574   case JVMTI_VERBOSE_OTHER:
   575     // ignore
   576     break;
   577   case JVMTI_VERBOSE_CLASS:
   578     TraceClassLoading = value != 0;
   579     TraceClassUnloading = value != 0;
   580     break;
   581   case JVMTI_VERBOSE_GC:
   582     PrintGC = value != 0;
   583     TraceClassUnloading = value != 0;
   584     break;
   585   case JVMTI_VERBOSE_JNI:
   586     PrintJNIResolving = value != 0;
   587     break;
   588   default:
   589     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
   590   };
   591   return JVMTI_ERROR_NONE;
   592 } /* end SetVerboseFlag */
   595 // format_ptr - pre-checked for NULL
   596 jvmtiError
   597 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
   598   *format_ptr = JVMTI_JLOCATION_JVMBCI;
   599   return JVMTI_ERROR_NONE;
   600 } /* end GetJLocationFormat */
   602 #ifndef JVMTI_KERNEL
   604   //
   605   // Thread functions
   606   //
   608 // Threads_lock NOT held
   609 // thread - NOT pre-checked
   610 // thread_state_ptr - pre-checked for NULL
   611 jvmtiError
   612 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
   613   jint state;
   614   oop thread_oop;
   615   JavaThread* thr;
   617   if (thread == NULL) {
   618     thread_oop = JavaThread::current()->threadObj();
   619   } else {
   620     thread_oop = JNIHandles::resolve_external_guard(thread);
   621   }
   623   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::thread_klass())) {
   624     return JVMTI_ERROR_INVALID_THREAD;
   625   }
   627   // get most state bits
   628   state = (jint)java_lang_Thread::get_thread_status(thread_oop);
   630   // add more state bits
   631   thr = java_lang_Thread::thread(thread_oop);
   632   if (thr != NULL) {
   633     JavaThreadState jts = thr->thread_state();
   635     if (thr->is_being_ext_suspended()) {
   636       state |= JVMTI_THREAD_STATE_SUSPENDED;
   637     }
   638     if (jts == _thread_in_native) {
   639       state |= JVMTI_THREAD_STATE_IN_NATIVE;
   640     }
   641     OSThread* osThread = thr->osthread();
   642     if (osThread != NULL && osThread->interrupted()) {
   643       state |= JVMTI_THREAD_STATE_INTERRUPTED;
   644     }
   645   }
   647   *thread_state_ptr = state;
   648   return JVMTI_ERROR_NONE;
   649 } /* end GetThreadState */
   652 // thread_ptr - pre-checked for NULL
   653 jvmtiError
   654 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
   655   JavaThread* current_thread  = JavaThread::current();
   656   *thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());
   657   return JVMTI_ERROR_NONE;
   658 } /* end GetCurrentThread */
   661 // threads_count_ptr - pre-checked for NULL
   662 // threads_ptr - pre-checked for NULL
   663 jvmtiError
   664 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
   665   int nthreads        = 0;
   666   Handle *thread_objs = NULL;
   667   ResourceMark rm;
   668   HandleMark hm;
   670   // enumerate threads (including agent threads)
   671   ThreadsListEnumerator tle(Thread::current(), true);
   672   nthreads = tle.num_threads();
   673   *threads_count_ptr = nthreads;
   675   if (nthreads == 0) {
   676     *threads_ptr = NULL;
   677     return JVMTI_ERROR_NONE;
   678   }
   680   thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
   681   NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
   683   for (int i=0; i < nthreads; i++) {
   684     thread_objs[i] = Handle(tle.get_threadObj(i));
   685   }
   687   // have to make global handles outside of Threads_lock
   688   jthread *jthreads  = new_jthreadArray(nthreads, thread_objs);
   689   NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
   691   *threads_ptr = jthreads;
   692   return JVMTI_ERROR_NONE;
   693 } /* end GetAllThreads */
   696 // Threads_lock NOT held, java_thread not protected by lock
   697 // java_thread - pre-checked
   698 jvmtiError
   699 JvmtiEnv::SuspendThread(JavaThread* java_thread) {
   700   // don't allow hidden thread suspend request.
   701   if (java_thread->is_hidden_from_external_view()) {
   702     return (JVMTI_ERROR_NONE);
   703   }
   705   {
   706     MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
   707     if (java_thread->is_external_suspend()) {
   708       // don't allow nested external suspend requests.
   709       return (JVMTI_ERROR_THREAD_SUSPENDED);
   710     }
   711     if (java_thread->is_exiting()) { // thread is in the process of exiting
   712       return (JVMTI_ERROR_THREAD_NOT_ALIVE);
   713     }
   714     java_thread->set_external_suspend();
   715   }
   717   if (!JvmtiSuspendControl::suspend(java_thread)) {
   718     // the thread was in the process of exiting
   719     return (JVMTI_ERROR_THREAD_NOT_ALIVE);
   720   }
   721   return JVMTI_ERROR_NONE;
   722 } /* end SuspendThread */
   725 // request_count - pre-checked to be greater than or equal to 0
   726 // request_list - pre-checked for NULL
   727 // results - pre-checked for NULL
   728 jvmtiError
   729 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
   730   int needSafepoint = 0;  // > 0 if we need a safepoint
   731   for (int i = 0; i < request_count; i++) {
   732     JavaThread *java_thread = get_JavaThread(request_list[i]);
   733     if (java_thread == NULL) {
   734       results[i] = JVMTI_ERROR_INVALID_THREAD;
   735       continue;
   736     }
   737     // the thread has not yet run or has exited (not on threads list)
   738     if (java_thread->threadObj() == NULL) {
   739       results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
   740       continue;
   741     }
   742     if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) {
   743       results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
   744       continue;
   745     }
   746     // don't allow hidden thread suspend request.
   747     if (java_thread->is_hidden_from_external_view()) {
   748       results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
   749       continue;
   750     }
   752     {
   753       MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
   754       if (java_thread->is_external_suspend()) {
   755         // don't allow nested external suspend requests.
   756         results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
   757         continue;
   758       }
   759       if (java_thread->is_exiting()) { // thread is in the process of exiting
   760         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
   761         continue;
   762       }
   763       java_thread->set_external_suspend();
   764     }
   765     if (java_thread->thread_state() == _thread_in_native) {
   766       // We need to try and suspend native threads here. Threads in
   767       // other states will self-suspend on their next transition.
   768       if (!JvmtiSuspendControl::suspend(java_thread)) {
   769         // The thread was in the process of exiting. Force another
   770         // safepoint to make sure that this thread transitions.
   771         needSafepoint++;
   772         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
   773         continue;
   774       }
   775     } else {
   776       needSafepoint++;
   777     }
   778     results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
   779   }
   780   if (needSafepoint > 0) {
   781     VM_ForceSafepoint vfs;
   782     VMThread::execute(&vfs);
   783   }
   784   // per-thread suspend results returned via results parameter
   785   return JVMTI_ERROR_NONE;
   786 } /* end SuspendThreadList */
   789 // Threads_lock NOT held, java_thread not protected by lock
   790 // java_thread - pre-checked
   791 jvmtiError
   792 JvmtiEnv::ResumeThread(JavaThread* java_thread) {
   793   // don't allow hidden thread resume request.
   794   if (java_thread->is_hidden_from_external_view()) {
   795     return JVMTI_ERROR_NONE;
   796   }
   798   if (!java_thread->is_being_ext_suspended()) {
   799     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
   800   }
   802   if (!JvmtiSuspendControl::resume(java_thread)) {
   803     return JVMTI_ERROR_INTERNAL;
   804   }
   805   return JVMTI_ERROR_NONE;
   806 } /* end ResumeThread */
   809 // request_count - pre-checked to be greater than or equal to 0
   810 // request_list - pre-checked for NULL
   811 // results - pre-checked for NULL
   812 jvmtiError
   813 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
   814   for (int i = 0; i < request_count; i++) {
   815     JavaThread *java_thread = get_JavaThread(request_list[i]);
   816     if (java_thread == NULL) {
   817       results[i] = JVMTI_ERROR_INVALID_THREAD;
   818       continue;
   819     }
   820     // don't allow hidden thread resume request.
   821     if (java_thread->is_hidden_from_external_view()) {
   822       results[i] = JVMTI_ERROR_NONE;  // indicate successful resume
   823       continue;
   824     }
   825     if (!java_thread->is_being_ext_suspended()) {
   826       results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
   827       continue;
   828     }
   830     if (!JvmtiSuspendControl::resume(java_thread)) {
   831       results[i] = JVMTI_ERROR_INTERNAL;
   832       continue;
   833     }
   835     results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
   836   }
   837   // per-thread resume results returned via results parameter
   838   return JVMTI_ERROR_NONE;
   839 } /* end ResumeThreadList */
   842 // Threads_lock NOT held, java_thread not protected by lock
   843 // java_thread - pre-checked
   844 jvmtiError
   845 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
   846   oop e = JNIHandles::resolve_external_guard(exception);
   847   NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
   849   JavaThread::send_async_exception(java_thread->threadObj(), e);
   851   return JVMTI_ERROR_NONE;
   853 } /* end StopThread */
   856 // Threads_lock NOT held
   857 // thread - NOT pre-checked
   858 jvmtiError
   859 JvmtiEnv::InterruptThread(jthread thread) {
   860   oop thread_oop = JNIHandles::resolve_external_guard(thread);
   861   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::thread_klass()))
   862     return JVMTI_ERROR_INVALID_THREAD;
   864   JavaThread* current_thread  = JavaThread::current();
   866   // Todo: this is a duplicate of JVM_Interrupt; share code in future
   867   // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
   868   MutexLockerEx ml(current_thread->threadObj() == thread_oop ? NULL : Threads_lock);
   869   // We need to re-resolve the java_thread, since a GC might have happened during the
   870   // acquire of the lock
   872   JavaThread* java_thread = java_lang_Thread::thread(JNIHandles::resolve_external_guard(thread));
   873   NULL_CHECK(java_thread, JVMTI_ERROR_THREAD_NOT_ALIVE);
   875   Thread::interrupt(java_thread);
   877   return JVMTI_ERROR_NONE;
   878 } /* end InterruptThread */
   881 // Threads_lock NOT held
   882 // thread - NOT pre-checked
   883 // info_ptr - pre-checked for NULL
   884 jvmtiError
   885 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
   886   ResourceMark rm;
   887   HandleMark hm;
   889   JavaThread* current_thread = JavaThread::current();
   891   // if thread is NULL the current thread is used
   892   oop thread_oop;
   893   if (thread == NULL) {
   894     thread_oop = current_thread->threadObj();
   895   } else {
   896     thread_oop = JNIHandles::resolve_external_guard(thread);
   897   }
   898   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::thread_klass()))
   899     return JVMTI_ERROR_INVALID_THREAD;
   901   Handle thread_obj(current_thread, thread_oop);
   902   typeArrayHandle    name;
   903   ThreadPriority priority;
   904   Handle     thread_group;
   905   Handle context_class_loader;
   906   bool          is_daemon;
   908   { MutexLocker mu(Threads_lock);
   910     name = typeArrayHandle(current_thread, java_lang_Thread::name(thread_obj()));
   911     priority = java_lang_Thread::priority(thread_obj());
   912     thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
   913     is_daemon = java_lang_Thread::is_daemon(thread_obj());
   915     oop loader = java_lang_Thread::context_class_loader(thread_obj());
   916     context_class_loader = Handle(current_thread, loader);
   917   }
   918   { const char *n;
   920     if (name() != NULL) {
   921       n = UNICODE::as_utf8((jchar*) name->base(T_CHAR), name->length());
   922     } else {
   923       n = UNICODE::as_utf8(NULL, 0);
   924     }
   926     info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
   927     if (info_ptr->name == NULL)
   928       return JVMTI_ERROR_OUT_OF_MEMORY;
   930     strcpy(info_ptr->name, n);
   931   }
   932   info_ptr->is_daemon = is_daemon;
   933   info_ptr->priority  = priority;
   935   info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
   936                                      jni_reference(context_class_loader);
   937   info_ptr->thread_group = jni_reference(thread_group);
   939   return JVMTI_ERROR_NONE;
   940 } /* end GetThreadInfo */
   943 // Threads_lock NOT held, java_thread not protected by lock
   944 // java_thread - pre-checked
   945 // owned_monitor_count_ptr - pre-checked for NULL
   946 // owned_monitors_ptr - pre-checked for NULL
   947 jvmtiError
   948 JvmtiEnv::GetOwnedMonitorInfo(JavaThread* java_thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
   949   jvmtiError err = JVMTI_ERROR_NONE;
   950   JavaThread* calling_thread = JavaThread::current();
   952   // growable array of jvmti monitors info on the C-heap
   953   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
   954       new (ResourceObj::C_HEAP) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
   956   uint32_t debug_bits = 0;
   957   if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
   958     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
   959   } else {
   960     // JVMTI get monitors info at safepoint. Do not require target thread to
   961     // be suspended.
   962     VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
   963     VMThread::execute(&op);
   964     err = op.result();
   965   }
   966   jint owned_monitor_count = owned_monitors_list->length();
   967   if (err == JVMTI_ERROR_NONE) {
   968     if ((err = allocate(owned_monitor_count * sizeof(jobject *),
   969                       (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
   970       // copy into the returned array
   971       for (int i = 0; i < owned_monitor_count; i++) {
   972         (*owned_monitors_ptr)[i] =
   973           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
   974       }
   975       *owned_monitor_count_ptr = owned_monitor_count;
   976     }
   977   }
   978   // clean up.
   979   for (int i = 0; i < owned_monitor_count; i++) {
   980     deallocate((unsigned char*)owned_monitors_list->at(i));
   981   }
   982   delete owned_monitors_list;
   984   return err;
   985 } /* end GetOwnedMonitorInfo */
   988 // Threads_lock NOT held, java_thread not protected by lock
   989 // java_thread - pre-checked
   990 // monitor_info_count_ptr - pre-checked for NULL
   991 // monitor_info_ptr - pre-checked for NULL
   992 jvmtiError
   993 JvmtiEnv::GetOwnedMonitorStackDepthInfo(JavaThread* java_thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
   994   jvmtiError err = JVMTI_ERROR_NONE;
   995   JavaThread* calling_thread  = JavaThread::current();
   997   // growable array of jvmti monitors info on the C-heap
   998   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
   999          new (ResourceObj::C_HEAP) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
  1001   uint32_t debug_bits = 0;
  1002   if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
  1003     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
  1004   } else {
  1005     // JVMTI get owned monitors info at safepoint. Do not require target thread to
  1006     // be suspended.
  1007     VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
  1008     VMThread::execute(&op);
  1009     err = op.result();
  1012   jint owned_monitor_count = owned_monitors_list->length();
  1013   if (err == JVMTI_ERROR_NONE) {
  1014     if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
  1015                       (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
  1016       // copy to output array.
  1017       for (int i = 0; i < owned_monitor_count; i++) {
  1018         (*monitor_info_ptr)[i].monitor =
  1019           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
  1020         (*monitor_info_ptr)[i].stack_depth =
  1021           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
  1024     *monitor_info_count_ptr = owned_monitor_count;
  1027   // clean up.
  1028   for (int i = 0; i < owned_monitor_count; i++) {
  1029     deallocate((unsigned char*)owned_monitors_list->at(i));
  1031   delete owned_monitors_list;
  1033   return err;
  1034 } /* end GetOwnedMonitorStackDepthInfo */
  1037 // Threads_lock NOT held, java_thread not protected by lock
  1038 // java_thread - pre-checked
  1039 // monitor_ptr - pre-checked for NULL
  1040 jvmtiError
  1041 JvmtiEnv::GetCurrentContendedMonitor(JavaThread* java_thread, jobject* monitor_ptr) {
  1042   jvmtiError err = JVMTI_ERROR_NONE;
  1043   uint32_t debug_bits = 0;
  1044   JavaThread* calling_thread  = JavaThread::current();
  1045   if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
  1046     err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
  1047   } else {
  1048     // get contended monitor information at safepoint.
  1049     VM_GetCurrentContendedMonitor op(this, calling_thread, java_thread, monitor_ptr);
  1050     VMThread::execute(&op);
  1051     err = op.result();
  1053   return err;
  1054 } /* end GetCurrentContendedMonitor */
  1057 // Threads_lock NOT held
  1058 // thread - NOT pre-checked
  1059 // proc - pre-checked for NULL
  1060 // arg - NULL is a valid value, must be checked
  1061 jvmtiError
  1062 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
  1063   oop thread_oop = JNIHandles::resolve_external_guard(thread);
  1064   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::thread_klass())) {
  1065     return JVMTI_ERROR_INVALID_THREAD;
  1067   if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
  1068     return JVMTI_ERROR_INVALID_PRIORITY;
  1071   //Thread-self
  1072   JavaThread* current_thread = JavaThread::current();
  1074   Handle thread_hndl(current_thread, thread_oop);
  1076     MutexLocker mu(Threads_lock); // grab Threads_lock
  1078     JvmtiAgentThread *new_thread = new JvmtiAgentThread(this, proc, arg);
  1080     // At this point it may be possible that no osthread was created for the
  1081     // JavaThread due to lack of memory.
  1082     if (new_thread == NULL || new_thread->osthread() == NULL) {
  1083       if (new_thread) delete new_thread;
  1084       return JVMTI_ERROR_OUT_OF_MEMORY;
  1087     java_lang_Thread::set_thread(thread_hndl(), new_thread);
  1088     java_lang_Thread::set_priority(thread_hndl(), (ThreadPriority)priority);
  1089     java_lang_Thread::set_daemon(thread_hndl());
  1091     new_thread->set_threadObj(thread_hndl());
  1092     Threads::add(new_thread);
  1093     Thread::start(new_thread);
  1094   } // unlock Threads_lock
  1096   return JVMTI_ERROR_NONE;
  1097 } /* end RunAgentThread */
  1099   //
  1100   // Thread Group functions
  1101   //
  1103 // group_count_ptr - pre-checked for NULL
  1104 // groups_ptr - pre-checked for NULL
  1105 jvmtiError
  1106 JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
  1107   JavaThread* current_thread = JavaThread::current();
  1109   // Only one top level thread group now.
  1110   *group_count_ptr = 1;
  1112   // Allocate memory to store global-refs to the thread groups.
  1113   // Assume this area is freed by caller.
  1114   *groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
  1116   NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
  1118   // Convert oop to Handle, then convert Handle to global-ref.
  1120     HandleMark hm(current_thread);
  1121     Handle system_thread_group(current_thread, Universe::system_thread_group());
  1122     *groups_ptr[0] = jni_reference(system_thread_group);
  1125   return JVMTI_ERROR_NONE;
  1126 } /* end GetTopThreadGroups */
  1129 // info_ptr - pre-checked for NULL
  1130 jvmtiError
  1131 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
  1132   ResourceMark rm;
  1133   HandleMark hm;
  1135   JavaThread* current_thread = JavaThread::current();
  1137   Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
  1138   NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
  1140   typeArrayHandle name;
  1141   Handle parent_group;
  1142   bool is_daemon;
  1143   ThreadPriority max_priority;
  1145   { MutexLocker mu(Threads_lock);
  1147     name         = typeArrayHandle(current_thread,
  1148                                    java_lang_ThreadGroup::name(group_obj()));
  1149     parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
  1150     is_daemon    = java_lang_ThreadGroup::is_daemon(group_obj());
  1151     max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
  1154   info_ptr->is_daemon    = is_daemon;
  1155   info_ptr->max_priority = max_priority;
  1156   info_ptr->parent       = jni_reference(parent_group);
  1158   if (name() != NULL) {
  1159     const char* n = UNICODE::as_utf8((jchar*) name->base(T_CHAR), name->length());
  1160     info_ptr->name = (char *)jvmtiMalloc(strlen(n)+1);
  1161     NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
  1162     strcpy(info_ptr->name, n);
  1163   } else {
  1164     info_ptr->name = NULL;
  1167   return JVMTI_ERROR_NONE;
  1168 } /* end GetThreadGroupInfo */
  1171 // thread_count_ptr - pre-checked for NULL
  1172 // threads_ptr - pre-checked for NULL
  1173 // group_count_ptr - pre-checked for NULL
  1174 // groups_ptr - pre-checked for NULL
  1175 jvmtiError
  1176 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
  1177   JavaThread* current_thread = JavaThread::current();
  1178   oop group_obj = (oop) JNIHandles::resolve_external_guard(group);
  1179   NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
  1181   Handle *thread_objs = NULL;
  1182   Handle *group_objs  = NULL;
  1183   int nthreads = 0;
  1184   int ngroups = 0;
  1185   int hidden_threads = 0;
  1187   ResourceMark rm;
  1188   HandleMark hm;
  1190   Handle group_hdl(current_thread, group_obj);
  1192   { MutexLocker mu(Threads_lock);
  1194     nthreads = java_lang_ThreadGroup::nthreads(group_hdl());
  1195     ngroups  = java_lang_ThreadGroup::ngroups(group_hdl());
  1197     if (nthreads > 0) {
  1198       objArrayOop threads = java_lang_ThreadGroup::threads(group_hdl());
  1199       assert(nthreads <= threads->length(), "too many threads");
  1200       thread_objs = NEW_RESOURCE_ARRAY(Handle,nthreads);
  1201       for (int i=0, j=0; i<nthreads; i++) {
  1202         oop thread_obj = threads->obj_at(i);
  1203         assert(thread_obj != NULL, "thread_obj is NULL");
  1204         JavaThread *javathread = java_lang_Thread::thread(thread_obj);
  1205         // Filter out hidden java threads.
  1206         if (javathread != NULL && javathread->is_hidden_from_external_view()) {
  1207           hidden_threads++;
  1208           continue;
  1210         thread_objs[j++] = Handle(current_thread, thread_obj);
  1212       nthreads -= hidden_threads;
  1214     if (ngroups > 0) {
  1215       objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
  1216       assert(ngroups <= groups->length(), "too many threads");
  1217       group_objs = NEW_RESOURCE_ARRAY(Handle,ngroups);
  1218       for (int i=0; i<ngroups; i++) {
  1219         oop group_obj = groups->obj_at(i);
  1220         assert(group_obj != NULL, "group_obj != NULL");
  1221         group_objs[i] = Handle(current_thread, group_obj);
  1226   // have to make global handles outside of Threads_lock
  1227   *group_count_ptr  = ngroups;
  1228   *thread_count_ptr = nthreads;
  1229   *threads_ptr     = new_jthreadArray(nthreads, thread_objs);
  1230   *groups_ptr      = new_jthreadGroupArray(ngroups, group_objs);
  1231   if ((nthreads > 0) && (*threads_ptr == NULL)) {
  1232     return JVMTI_ERROR_OUT_OF_MEMORY;
  1234   if ((ngroups > 0) && (*groups_ptr == NULL)) {
  1235     return JVMTI_ERROR_OUT_OF_MEMORY;
  1238   return JVMTI_ERROR_NONE;
  1239 } /* end GetThreadGroupChildren */
  1242   //
  1243   // Stack Frame functions
  1244   //
  1246 // Threads_lock NOT held, java_thread not protected by lock
  1247 // java_thread - pre-checked
  1248 // max_frame_count - pre-checked to be greater than or equal to 0
  1249 // frame_buffer - pre-checked for NULL
  1250 // count_ptr - pre-checked for NULL
  1251 jvmtiError
  1252 JvmtiEnv::GetStackTrace(JavaThread* java_thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
  1253   jvmtiError err = JVMTI_ERROR_NONE;
  1254   uint32_t debug_bits = 0;
  1255   if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
  1256     err = get_stack_trace(java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
  1257   } else {
  1258     // JVMTI get stack trace at safepoint. Do not require target thread to
  1259     // be suspended.
  1260     VM_GetStackTrace op(this, java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
  1261     VMThread::execute(&op);
  1262     err = op.result();
  1265   return err;
  1266 } /* end GetStackTrace */
  1269 // max_frame_count - pre-checked to be greater than or equal to 0
  1270 // stack_info_ptr - pre-checked for NULL
  1271 // thread_count_ptr - pre-checked for NULL
  1272 jvmtiError
  1273 JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
  1274   jvmtiError err = JVMTI_ERROR_NONE;
  1275   JavaThread* calling_thread = JavaThread::current();
  1277   // JVMTI get stack traces at safepoint.
  1278   VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
  1279   VMThread::execute(&op);
  1280   *thread_count_ptr = op.final_thread_count();
  1281   *stack_info_ptr = op.stack_info();
  1282   err = op.result();
  1283   return err;
  1284 } /* end GetAllStackTraces */
  1287 // thread_count - pre-checked to be greater than or equal to 0
  1288 // thread_list - pre-checked for NULL
  1289 // max_frame_count - pre-checked to be greater than or equal to 0
  1290 // stack_info_ptr - pre-checked for NULL
  1291 jvmtiError
  1292 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
  1293   jvmtiError err = JVMTI_ERROR_NONE;
  1294   // JVMTI get stack traces at safepoint.
  1295   VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
  1296   VMThread::execute(&op);
  1297   err = op.result();
  1298   if (err == JVMTI_ERROR_NONE) {
  1299     *stack_info_ptr = op.stack_info();
  1301   return err;
  1302 } /* end GetThreadListStackTraces */
  1305 // Threads_lock NOT held, java_thread not protected by lock
  1306 // java_thread - pre-checked
  1307 // count_ptr - pre-checked for NULL
  1308 jvmtiError
  1309 JvmtiEnv::GetFrameCount(JavaThread* java_thread, jint* count_ptr) {
  1310   jvmtiError err = JVMTI_ERROR_NONE;
  1312   // retrieve or create JvmtiThreadState.
  1313   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
  1314   if (state == NULL) {
  1315     return JVMTI_ERROR_THREAD_NOT_ALIVE;
  1317   uint32_t debug_bits = 0;
  1318   if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
  1319     err = get_frame_count(state, count_ptr);
  1320   } else {
  1321     // get java stack frame count at safepoint.
  1322     VM_GetFrameCount op(this, state, count_ptr);
  1323     VMThread::execute(&op);
  1324     err = op.result();
  1326   return err;
  1327 } /* end GetFrameCount */
  1330 // Threads_lock NOT held, java_thread not protected by lock
  1331 // java_thread - pre-checked
  1332 jvmtiError
  1333 JvmtiEnv::PopFrame(JavaThread* java_thread) {
  1334   JavaThread* current_thread  = JavaThread::current();
  1335   HandleMark hm(current_thread);
  1336   uint32_t debug_bits = 0;
  1338   // retrieve or create the state
  1339   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
  1340   if (state == NULL) {
  1341     return JVMTI_ERROR_THREAD_NOT_ALIVE;
  1344   // Check if java_thread is fully suspended
  1345   if (!is_thread_fully_suspended(java_thread, true /* wait for suspend completion */, &debug_bits)) {
  1346     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
  1348   // Check to see if a PopFrame was already in progress
  1349   if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
  1350     // Probably possible for JVMTI clients to trigger this, but the
  1351     // JPDA backend shouldn't allow this to happen
  1352     return JVMTI_ERROR_INTERNAL;
  1356     // Was workaround bug
  1357     //    4812902: popFrame hangs if the method is waiting at a synchronize
  1358     // Catch this condition and return an error to avoid hanging.
  1359     // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
  1360     OSThread* osThread = java_thread->osthread();
  1361     if (osThread->get_state() == MONITOR_WAIT) {
  1362       return JVMTI_ERROR_OPAQUE_FRAME;
  1367     ResourceMark rm(current_thread);
  1368     // Check if there are more than one Java frame in this thread, that the top two frames
  1369     // are Java (not native) frames, and that there is no intervening VM frame
  1370     int frame_count = 0;
  1371     bool is_interpreted[2];
  1372     intptr_t *frame_sp[2];
  1373     // The 2-nd arg of constructor is needed to stop iterating at java entry frame.
  1374     for (vframeStream vfs(java_thread, true); !vfs.at_end(); vfs.next()) {
  1375       methodHandle mh(current_thread, vfs.method());
  1376       if (mh->is_native()) return(JVMTI_ERROR_OPAQUE_FRAME);
  1377       is_interpreted[frame_count] = vfs.is_interpreted_frame();
  1378       frame_sp[frame_count] = vfs.frame_id();
  1379       if (++frame_count > 1) break;
  1381     if (frame_count < 2)  {
  1382       // We haven't found two adjacent non-native Java frames on the top.
  1383       // There can be two situations here:
  1384       //  1. There are no more java frames
  1385       //  2. Two top java frames are separated by non-java native frames
  1386       if(vframeFor(java_thread, 1) == NULL) {
  1387         return JVMTI_ERROR_NO_MORE_FRAMES;
  1388       } else {
  1389         // Intervening non-java native or VM frames separate java frames.
  1390         // Current implementation does not support this. See bug #5031735.
  1391         // In theory it is possible to pop frames in such cases.
  1392         return JVMTI_ERROR_OPAQUE_FRAME;
  1396     // If any of the top 2 frames is a compiled one, need to deoptimize it
  1397     for (int i = 0; i < 2; i++) {
  1398       if (!is_interpreted[i]) {
  1399         VM_DeoptimizeFrame op(java_thread, frame_sp[i]);
  1400         VMThread::execute(&op);
  1404     // Update the thread state to reflect that the top frame is popped
  1405     // so that cur_stack_depth is maintained properly and all frameIDs
  1406     // are invalidated.
  1407     // The current frame will be popped later when the suspended thread
  1408     // is resumed and right before returning from VM to Java.
  1409     // (see call_VM_base() in assembler_<cpu>.cpp).
  1411     // It's fine to update the thread state here because no JVMTI events
  1412     // shall be posted for this PopFrame.
  1414     state->update_for_pop_top_frame();
  1415     java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
  1416     // Set pending step flag for this popframe and it is cleared when next
  1417     // step event is posted.
  1418     state->set_pending_step_for_popframe();
  1421   return JVMTI_ERROR_NONE;
  1422 } /* end PopFrame */
  1425 // Threads_lock NOT held, java_thread not protected by lock
  1426 // java_thread - pre-checked
  1427 // java_thread - unchecked
  1428 // depth - pre-checked as non-negative
  1429 // method_ptr - pre-checked for NULL
  1430 // location_ptr - pre-checked for NULL
  1431 jvmtiError
  1432 JvmtiEnv::GetFrameLocation(JavaThread* java_thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
  1433   jvmtiError err = JVMTI_ERROR_NONE;
  1434   uint32_t debug_bits = 0;
  1436   if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
  1437     err = get_frame_location(java_thread, depth, method_ptr, location_ptr);
  1438   } else {
  1439     // JVMTI get java stack frame location at safepoint.
  1440     VM_GetFrameLocation op(this, java_thread, depth, method_ptr, location_ptr);
  1441     VMThread::execute(&op);
  1442     err = op.result();
  1444   return err;
  1445 } /* end GetFrameLocation */
  1448 // Threads_lock NOT held, java_thread not protected by lock
  1449 // java_thread - pre-checked
  1450 // java_thread - unchecked
  1451 // depth - pre-checked as non-negative
  1452 jvmtiError
  1453 JvmtiEnv::NotifyFramePop(JavaThread* java_thread, jint depth) {
  1454   ResourceMark rm;
  1455   uint32_t debug_bits = 0;
  1457   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
  1458   if (state == NULL) {
  1459     return JVMTI_ERROR_THREAD_NOT_ALIVE;
  1462   if (!JvmtiEnv::is_thread_fully_suspended(java_thread, true, &debug_bits)) {
  1463       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
  1466   if (TraceJVMTICalls) {
  1467     JvmtiSuspendControl::print();
  1470   vframe *vf = vframeFor(java_thread, depth);
  1471   if (vf == NULL) {
  1472     return JVMTI_ERROR_NO_MORE_FRAMES;
  1475   if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
  1476     return JVMTI_ERROR_OPAQUE_FRAME;
  1479   assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
  1481   int frame_number = state->count_frames() - depth;
  1482   state->env_thread_state(this)->set_frame_pop(frame_number);
  1484   return JVMTI_ERROR_NONE;
  1485 } /* end NotifyFramePop */
  1488   //
  1489   // Force Early Return functions
  1490   //
  1492 // Threads_lock NOT held, java_thread not protected by lock
  1493 // java_thread - pre-checked
  1494 jvmtiError
  1495 JvmtiEnv::ForceEarlyReturnObject(JavaThread* java_thread, jobject value) {
  1496   jvalue val;
  1497   val.l = value;
  1498   return force_early_return(java_thread, val, atos);
  1499 } /* end ForceEarlyReturnObject */
  1502 // Threads_lock NOT held, java_thread not protected by lock
  1503 // java_thread - pre-checked
  1504 jvmtiError
  1505 JvmtiEnv::ForceEarlyReturnInt(JavaThread* java_thread, jint value) {
  1506   jvalue val;
  1507   val.i = value;
  1508   return force_early_return(java_thread, val, itos);
  1509 } /* end ForceEarlyReturnInt */
  1512 // Threads_lock NOT held, java_thread not protected by lock
  1513 // java_thread - pre-checked
  1514 jvmtiError
  1515 JvmtiEnv::ForceEarlyReturnLong(JavaThread* java_thread, jlong value) {
  1516   jvalue val;
  1517   val.j = value;
  1518   return force_early_return(java_thread, val, ltos);
  1519 } /* end ForceEarlyReturnLong */
  1522 // Threads_lock NOT held, java_thread not protected by lock
  1523 // java_thread - pre-checked
  1524 jvmtiError
  1525 JvmtiEnv::ForceEarlyReturnFloat(JavaThread* java_thread, jfloat value) {
  1526   jvalue val;
  1527   val.f = value;
  1528   return force_early_return(java_thread, val, ftos);
  1529 } /* end ForceEarlyReturnFloat */
  1532 // Threads_lock NOT held, java_thread not protected by lock
  1533 // java_thread - pre-checked
  1534 jvmtiError
  1535 JvmtiEnv::ForceEarlyReturnDouble(JavaThread* java_thread, jdouble value) {
  1536   jvalue val;
  1537   val.d = value;
  1538   return force_early_return(java_thread, val, dtos);
  1539 } /* end ForceEarlyReturnDouble */
  1542 // Threads_lock NOT held, java_thread not protected by lock
  1543 // java_thread - pre-checked
  1544 jvmtiError
  1545 JvmtiEnv::ForceEarlyReturnVoid(JavaThread* java_thread) {
  1546   jvalue val;
  1547   val.j = 0L;
  1548   return force_early_return(java_thread, val, vtos);
  1549 } /* end ForceEarlyReturnVoid */
  1552   //
  1553   // Heap functions
  1554   //
  1556 // klass - NULL is a valid value, must be checked
  1557 // initial_object - NULL is a valid value, must be checked
  1558 // callbacks - pre-checked for NULL
  1559 // user_data - NULL is a valid value, must be checked
  1560 jvmtiError
  1561 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
  1562   // check klass if provided
  1563   klassOop k_oop = NULL;
  1564   if (klass != NULL) {
  1565     oop k_mirror = JNIHandles::resolve_external_guard(klass);
  1566     if (k_mirror == NULL) {
  1567       return JVMTI_ERROR_INVALID_CLASS;
  1569     if (java_lang_Class::is_primitive(k_mirror)) {
  1570       return JVMTI_ERROR_NONE;
  1572     k_oop = java_lang_Class::as_klassOop(k_mirror);
  1573     if (k_oop == NULL) {
  1574       return JVMTI_ERROR_INVALID_CLASS;
  1578   Thread *thread = Thread::current();
  1579   HandleMark hm(thread);
  1580   KlassHandle kh (thread, k_oop);
  1582   TraceTime t("FollowReferences", TraceJVMTIObjectTagging);
  1583   JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, kh, initial_object, callbacks, user_data);
  1584   return JVMTI_ERROR_NONE;
  1585 } /* end FollowReferences */
  1588 // klass - NULL is a valid value, must be checked
  1589 // callbacks - pre-checked for NULL
  1590 // user_data - NULL is a valid value, must be checked
  1591 jvmtiError
  1592 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
  1593   // check klass if provided
  1594   klassOop k_oop = NULL;
  1595   if (klass != NULL) {
  1596     oop k_mirror = JNIHandles::resolve_external_guard(klass);
  1597     if (k_mirror == NULL) {
  1598       return JVMTI_ERROR_INVALID_CLASS;
  1600     if (java_lang_Class::is_primitive(k_mirror)) {
  1601       return JVMTI_ERROR_NONE;
  1603     k_oop = java_lang_Class::as_klassOop(k_mirror);
  1604     if (k_oop == NULL) {
  1605       return JVMTI_ERROR_INVALID_CLASS;
  1609   Thread *thread = Thread::current();
  1610   HandleMark hm(thread);
  1611   KlassHandle kh (thread, k_oop);
  1613   TraceTime t("IterateThroughHeap", TraceJVMTIObjectTagging);
  1614   JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, kh, callbacks, user_data);
  1615   return JVMTI_ERROR_NONE;
  1616 } /* end IterateThroughHeap */
  1619 // tag_ptr - pre-checked for NULL
  1620 jvmtiError
  1621 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
  1622   oop o = JNIHandles::resolve_external_guard(object);
  1623   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
  1624   *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
  1625   return JVMTI_ERROR_NONE;
  1626 } /* end GetTag */
  1629 jvmtiError
  1630 JvmtiEnv::SetTag(jobject object, jlong tag) {
  1631   oop o = JNIHandles::resolve_external_guard(object);
  1632   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
  1633   JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
  1634   return JVMTI_ERROR_NONE;
  1635 } /* end SetTag */
  1638 // tag_count - pre-checked to be greater than or equal to 0
  1639 // tags - pre-checked for NULL
  1640 // count_ptr - pre-checked for NULL
  1641 // object_result_ptr - NULL is a valid value, must be checked
  1642 // tag_result_ptr - NULL is a valid value, must be checked
  1643 jvmtiError
  1644 JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
  1645   TraceTime t("GetObjectsWithTags", TraceJVMTIObjectTagging);
  1646   return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
  1647 } /* end GetObjectsWithTags */
  1650 jvmtiError
  1651 JvmtiEnv::ForceGarbageCollection() {
  1652   Universe::heap()->collect(GCCause::_jvmti_force_gc);
  1653   return JVMTI_ERROR_NONE;
  1654 } /* end ForceGarbageCollection */
  1657   //
  1658   // Heap (1.0) functions
  1659   //
  1661 // object_reference_callback - pre-checked for NULL
  1662 // user_data - NULL is a valid value, must be checked
  1663 jvmtiError
  1664 JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
  1665   oop o = JNIHandles::resolve_external_guard(object);
  1666   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
  1667   JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
  1668   return JVMTI_ERROR_NONE;
  1669 } /* end IterateOverObjectsReachableFromObject */
  1672 // heap_root_callback - NULL is a valid value, must be checked
  1673 // stack_ref_callback - NULL is a valid value, must be checked
  1674 // object_ref_callback - NULL is a valid value, must be checked
  1675 // user_data - NULL is a valid value, must be checked
  1676 jvmtiError
  1677 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
  1678   TraceTime t("IterateOverReachableObjects", TraceJVMTIObjectTagging);
  1679   JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
  1680   return JVMTI_ERROR_NONE;
  1681 } /* end IterateOverReachableObjects */
  1684 // heap_object_callback - pre-checked for NULL
  1685 // user_data - NULL is a valid value, must be checked
  1686 jvmtiError
  1687 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
  1688   TraceTime t("IterateOverHeap", TraceJVMTIObjectTagging);
  1689   Thread *thread = Thread::current();
  1690   HandleMark hm(thread);
  1691   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, KlassHandle(), heap_object_callback, user_data);
  1692   return JVMTI_ERROR_NONE;
  1693 } /* end IterateOverHeap */
  1696 // k_mirror - may be primitive, this must be checked
  1697 // heap_object_callback - pre-checked for NULL
  1698 // user_data - NULL is a valid value, must be checked
  1699 jvmtiError
  1700 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
  1701   if (java_lang_Class::is_primitive(k_mirror)) {
  1702     // DO PRIMITIVE CLASS PROCESSING
  1703     return JVMTI_ERROR_NONE;
  1705   klassOop k_oop = java_lang_Class::as_klassOop(k_mirror);
  1706   if (k_oop == NULL) {
  1707     return JVMTI_ERROR_INVALID_CLASS;
  1709   Thread *thread = Thread::current();
  1710   HandleMark hm(thread);
  1711   KlassHandle klass (thread, k_oop);
  1712   TraceTime t("IterateOverInstancesOfClass", TraceJVMTIObjectTagging);
  1713   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
  1714   return JVMTI_ERROR_NONE;
  1715 } /* end IterateOverInstancesOfClass */
  1718   //
  1719   // Local Variable functions
  1720   //
  1722 // Threads_lock NOT held, java_thread not protected by lock
  1723 // java_thread - pre-checked
  1724 // java_thread - unchecked
  1725 // depth - pre-checked as non-negative
  1726 // value_ptr - pre-checked for NULL
  1727 jvmtiError
  1728 JvmtiEnv::GetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject* value_ptr) {
  1729   JavaThread* current_thread = JavaThread::current();
  1730   // rm object is created to clean up the javaVFrame created in
  1731   // doit_prologue(), but after doit() is finished with it.
  1732   ResourceMark rm(current_thread);
  1734   VM_GetOrSetLocal op(java_thread, current_thread, depth, slot);
  1735   VMThread::execute(&op);
  1736   jvmtiError err = op.result();
  1737   if (err != JVMTI_ERROR_NONE) {
  1738     return err;
  1739   } else {
  1740     *value_ptr = op.value().l;
  1741     return JVMTI_ERROR_NONE;
  1743 } /* end GetLocalObject */
  1746 // Threads_lock NOT held, java_thread not protected by lock
  1747 // java_thread - pre-checked
  1748 // java_thread - unchecked
  1749 // depth - pre-checked as non-negative
  1750 // value_ptr - pre-checked for NULL
  1751 jvmtiError
  1752 JvmtiEnv::GetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint* value_ptr) {
  1753   // rm object is created to clean up the javaVFrame created in
  1754   // doit_prologue(), but after doit() is finished with it.
  1755   ResourceMark rm;
  1757   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT);
  1758   VMThread::execute(&op);
  1759   *value_ptr = op.value().i;
  1760   return op.result();
  1761 } /* end GetLocalInt */
  1764 // Threads_lock NOT held, java_thread not protected by lock
  1765 // java_thread - pre-checked
  1766 // java_thread - unchecked
  1767 // depth - pre-checked as non-negative
  1768 // value_ptr - pre-checked for NULL
  1769 jvmtiError
  1770 JvmtiEnv::GetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong* value_ptr) {
  1771   // rm object is created to clean up the javaVFrame created in
  1772   // doit_prologue(), but after doit() is finished with it.
  1773   ResourceMark rm;
  1775   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG);
  1776   VMThread::execute(&op);
  1777   *value_ptr = op.value().j;
  1778   return op.result();
  1779 } /* end GetLocalLong */
  1782 // Threads_lock NOT held, java_thread not protected by lock
  1783 // java_thread - pre-checked
  1784 // java_thread - unchecked
  1785 // depth - pre-checked as non-negative
  1786 // value_ptr - pre-checked for NULL
  1787 jvmtiError
  1788 JvmtiEnv::GetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat* value_ptr) {
  1789   // rm object is created to clean up the javaVFrame created in
  1790   // doit_prologue(), but after doit() is finished with it.
  1791   ResourceMark rm;
  1793   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT);
  1794   VMThread::execute(&op);
  1795   *value_ptr = op.value().f;
  1796   return op.result();
  1797 } /* end GetLocalFloat */
  1800 // Threads_lock NOT held, java_thread not protected by lock
  1801 // java_thread - pre-checked
  1802 // java_thread - unchecked
  1803 // depth - pre-checked as non-negative
  1804 // value_ptr - pre-checked for NULL
  1805 jvmtiError
  1806 JvmtiEnv::GetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble* value_ptr) {
  1807   // rm object is created to clean up the javaVFrame created in
  1808   // doit_prologue(), but after doit() is finished with it.
  1809   ResourceMark rm;
  1811   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE);
  1812   VMThread::execute(&op);
  1813   *value_ptr = op.value().d;
  1814   return op.result();
  1815 } /* end GetLocalDouble */
  1818 // Threads_lock NOT held, java_thread not protected by lock
  1819 // java_thread - pre-checked
  1820 // java_thread - unchecked
  1821 // depth - pre-checked as non-negative
  1822 jvmtiError
  1823 JvmtiEnv::SetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject value) {
  1824   // rm object is created to clean up the javaVFrame created in
  1825   // doit_prologue(), but after doit() is finished with it.
  1826   ResourceMark rm;
  1827   jvalue val;
  1828   val.l = value;
  1829   VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val);
  1830   VMThread::execute(&op);
  1831   return op.result();
  1832 } /* end SetLocalObject */
  1835 // Threads_lock NOT held, java_thread not protected by lock
  1836 // java_thread - pre-checked
  1837 // java_thread - unchecked
  1838 // depth - pre-checked as non-negative
  1839 jvmtiError
  1840 JvmtiEnv::SetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint value) {
  1841   // rm object is created to clean up the javaVFrame created in
  1842   // doit_prologue(), but after doit() is finished with it.
  1843   ResourceMark rm;
  1844   jvalue val;
  1845   val.i = value;
  1846   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val);
  1847   VMThread::execute(&op);
  1848   return op.result();
  1849 } /* end SetLocalInt */
  1852 // Threads_lock NOT held, java_thread not protected by lock
  1853 // java_thread - pre-checked
  1854 // java_thread - unchecked
  1855 // depth - pre-checked as non-negative
  1856 jvmtiError
  1857 JvmtiEnv::SetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong value) {
  1858   // rm object is created to clean up the javaVFrame created in
  1859   // doit_prologue(), but after doit() is finished with it.
  1860   ResourceMark rm;
  1861   jvalue val;
  1862   val.j = value;
  1863   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val);
  1864   VMThread::execute(&op);
  1865   return op.result();
  1866 } /* end SetLocalLong */
  1869 // Threads_lock NOT held, java_thread not protected by lock
  1870 // java_thread - pre-checked
  1871 // java_thread - unchecked
  1872 // depth - pre-checked as non-negative
  1873 jvmtiError
  1874 JvmtiEnv::SetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat value) {
  1875   // rm object is created to clean up the javaVFrame created in
  1876   // doit_prologue(), but after doit() is finished with it.
  1877   ResourceMark rm;
  1878   jvalue val;
  1879   val.f = value;
  1880   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val);
  1881   VMThread::execute(&op);
  1882   return op.result();
  1883 } /* end SetLocalFloat */
  1886 // Threads_lock NOT held, java_thread not protected by lock
  1887 // java_thread - pre-checked
  1888 // java_thread - unchecked
  1889 // depth - pre-checked as non-negative
  1890 jvmtiError
  1891 JvmtiEnv::SetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble value) {
  1892   // rm object is created to clean up the javaVFrame created in
  1893   // doit_prologue(), but after doit() is finished with it.
  1894   ResourceMark rm;
  1895   jvalue val;
  1896   val.d = value;
  1897   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val);
  1898   VMThread::execute(&op);
  1899   return op.result();
  1900 } /* end SetLocalDouble */
  1903   //
  1904   // Breakpoint functions
  1905   //
  1907 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  1908 jvmtiError
  1909 JvmtiEnv::SetBreakpoint(methodOop method_oop, jlocation location) {
  1910   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  1911   if (location < 0) {   // simple invalid location check first
  1912     return JVMTI_ERROR_INVALID_LOCATION;
  1914   // verify that the breakpoint is not past the end of the method
  1915   if (location >= (jlocation) method_oop->code_size()) {
  1916     return JVMTI_ERROR_INVALID_LOCATION;
  1919   ResourceMark rm;
  1920   JvmtiBreakpoint bp(method_oop, location);
  1921   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
  1922   if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
  1923     return JVMTI_ERROR_DUPLICATE;
  1925   if (TraceJVMTICalls) {
  1926     jvmti_breakpoints.print();
  1929   return JVMTI_ERROR_NONE;
  1930 } /* end SetBreakpoint */
  1933 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  1934 jvmtiError
  1935 JvmtiEnv::ClearBreakpoint(methodOop method_oop, jlocation location) {
  1936   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  1938   if (location < 0) {   // simple invalid location check first
  1939     return JVMTI_ERROR_INVALID_LOCATION;
  1942   // verify that the breakpoint is not past the end of the method
  1943   if (location >= (jlocation) method_oop->code_size()) {
  1944     return JVMTI_ERROR_INVALID_LOCATION;
  1947   JvmtiBreakpoint bp(method_oop, location);
  1949   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
  1950   if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
  1951     return JVMTI_ERROR_NOT_FOUND;
  1953   if (TraceJVMTICalls) {
  1954     jvmti_breakpoints.print();
  1957   return JVMTI_ERROR_NONE;
  1958 } /* end ClearBreakpoint */
  1961   //
  1962   // Watched Field functions
  1963   //
  1965 jvmtiError
  1966 JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
  1967   // make sure we haven't set this watch before
  1968   if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
  1969   fdesc_ptr->set_is_field_access_watched(true);
  1970   update_klass_field_access_flag(fdesc_ptr);
  1972   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
  1974   return JVMTI_ERROR_NONE;
  1975 } /* end SetFieldAccessWatch */
  1978 jvmtiError
  1979 JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
  1980   // make sure we have a watch to clear
  1981   if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
  1982   fdesc_ptr->set_is_field_access_watched(false);
  1983   update_klass_field_access_flag(fdesc_ptr);
  1985   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
  1987   return JVMTI_ERROR_NONE;
  1988 } /* end ClearFieldAccessWatch */
  1991 jvmtiError
  1992 JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
  1993   // make sure we haven't set this watch before
  1994   if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
  1995   fdesc_ptr->set_is_field_modification_watched(true);
  1996   update_klass_field_access_flag(fdesc_ptr);
  1998   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
  2000   return JVMTI_ERROR_NONE;
  2001 } /* end SetFieldModificationWatch */
  2004 jvmtiError
  2005 JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
  2006    // make sure we have a watch to clear
  2007   if (!fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_NOT_FOUND;
  2008   fdesc_ptr->set_is_field_modification_watched(false);
  2009   update_klass_field_access_flag(fdesc_ptr);
  2011   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, false);
  2013   return JVMTI_ERROR_NONE;
  2014 } /* end ClearFieldModificationWatch */
  2016   //
  2017   // Class functions
  2018   //
  2021 // k_mirror - may be primitive, this must be checked
  2022 // signature_ptr - NULL is a valid value, must be checked
  2023 // generic_ptr - NULL is a valid value, must be checked
  2024 jvmtiError
  2025 JvmtiEnv::GetClassSignature(oop k_mirror, char** signature_ptr, char** generic_ptr) {
  2026   ResourceMark rm;
  2027   bool isPrimitive = java_lang_Class::is_primitive(k_mirror);
  2028   klassOop k = NULL;
  2029   if (!isPrimitive) {
  2030     k = java_lang_Class::as_klassOop(k_mirror);
  2031     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
  2033   if (signature_ptr != NULL) {
  2034     char* result = NULL;
  2035     if (isPrimitive) {
  2036       char tchar = type2char(java_lang_Class::primitive_type(k_mirror));
  2037       result = (char*) jvmtiMalloc(2);
  2038       result[0] = tchar;
  2039       result[1] = '\0';
  2040     } else {
  2041       const char* class_sig = Klass::cast(k)->signature_name();
  2042       result = (char *) jvmtiMalloc(strlen(class_sig)+1);
  2043       strcpy(result, class_sig);
  2045     *signature_ptr = result;
  2047   if (generic_ptr != NULL) {
  2048     *generic_ptr = NULL;
  2049     if (!isPrimitive && Klass::cast(k)->oop_is_instance()) {
  2050       symbolOop soo = instanceKlass::cast(k)->generic_signature();
  2051       if (soo != NULL) {
  2052         const char *gen_sig = soo->as_C_string();
  2053         if (gen_sig != NULL) {
  2054           char* gen_result;
  2055           jvmtiError err = allocate(strlen(gen_sig) + 1,
  2056                                     (unsigned char **)&gen_result);
  2057           if (err != JVMTI_ERROR_NONE) {
  2058             return err;
  2060           strcpy(gen_result, gen_sig);
  2061           *generic_ptr = gen_result;
  2066   return JVMTI_ERROR_NONE;
  2067 } /* end GetClassSignature */
  2070 // k_mirror - may be primitive, this must be checked
  2071 // status_ptr - pre-checked for NULL
  2072 jvmtiError
  2073 JvmtiEnv::GetClassStatus(oop k_mirror, jint* status_ptr) {
  2074   jint result = 0;
  2075   if (java_lang_Class::is_primitive(k_mirror)) {
  2076     result |= JVMTI_CLASS_STATUS_PRIMITIVE;
  2077   } else {
  2078     klassOop k = java_lang_Class::as_klassOop(k_mirror);
  2079     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
  2080     result = Klass::cast(k)->jvmti_class_status();
  2082   *status_ptr = result;
  2084   return JVMTI_ERROR_NONE;
  2085 } /* end GetClassStatus */
  2088 // k_mirror - may be primitive, this must be checked
  2089 // source_name_ptr - pre-checked for NULL
  2090 jvmtiError
  2091 JvmtiEnv::GetSourceFileName(oop k_mirror, char** source_name_ptr) {
  2092   if (java_lang_Class::is_primitive(k_mirror)) {
  2093      return JVMTI_ERROR_ABSENT_INFORMATION;
  2095   klassOop k_klass = java_lang_Class::as_klassOop(k_mirror);
  2096   NULL_CHECK(k_klass, JVMTI_ERROR_INVALID_CLASS);
  2098   if (!Klass::cast(k_klass)->oop_is_instance()) {
  2099     return JVMTI_ERROR_ABSENT_INFORMATION;
  2102   symbolOop sfnOop = instanceKlass::cast(k_klass)->source_file_name();
  2103   NULL_CHECK(sfnOop, JVMTI_ERROR_ABSENT_INFORMATION);
  2105     JavaThread* current_thread  = JavaThread::current();
  2106     ResourceMark rm(current_thread);
  2107     const char* sfncp = (const char*) sfnOop->as_C_string();
  2108     *source_name_ptr = (char *) jvmtiMalloc(strlen(sfncp)+1);
  2109     strcpy(*source_name_ptr, sfncp);
  2112   return JVMTI_ERROR_NONE;
  2113 } /* end GetSourceFileName */
  2116 // k_mirror - may be primitive, this must be checked
  2117 // modifiers_ptr - pre-checked for NULL
  2118 jvmtiError
  2119 JvmtiEnv::GetClassModifiers(oop k_mirror, jint* modifiers_ptr) {
  2120   JavaThread* current_thread  = JavaThread::current();
  2121   jint result = 0;
  2122   if (!java_lang_Class::is_primitive(k_mirror)) {
  2123     klassOop k = java_lang_Class::as_klassOop(k_mirror);
  2124     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
  2125     assert((Klass::cast(k)->oop_is_instance() || Klass::cast(k)->oop_is_array()), "should be an instance or an array klass");
  2126     result = Klass::cast(k)->compute_modifier_flags(current_thread);
  2127     JavaThread* THREAD = current_thread; // pass to macros
  2128     if (HAS_PENDING_EXCEPTION) {
  2129       CLEAR_PENDING_EXCEPTION;
  2130       return JVMTI_ERROR_INTERNAL;
  2131     };
  2133     // Reset the deleted  ACC_SUPER bit ( deleted in compute_modifier_flags()).
  2134     if(Klass::cast(k)->is_super()) {
  2135       result |= JVM_ACC_SUPER;
  2137   } else {
  2138     result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
  2140   *modifiers_ptr = result;
  2142   return JVMTI_ERROR_NONE;
  2143 } /* end GetClassModifiers */
  2146 // k_mirror - may be primitive, this must be checked
  2147 // method_count_ptr - pre-checked for NULL
  2148 // methods_ptr - pre-checked for NULL
  2149 jvmtiError
  2150 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
  2151   JavaThread* current_thread  = JavaThread::current();
  2152   HandleMark hm(current_thread);
  2154   if (java_lang_Class::is_primitive(k_mirror)) {
  2155     *method_count_ptr = 0;
  2156     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
  2157     return JVMTI_ERROR_NONE;
  2159   klassOop k = java_lang_Class::as_klassOop(k_mirror);
  2160   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
  2162   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
  2163   if (!(Klass::cast(k)->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
  2164     return JVMTI_ERROR_CLASS_NOT_PREPARED;
  2167   if (!Klass::cast(k)->oop_is_instance()) {
  2168     *method_count_ptr = 0;
  2169     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
  2170     return JVMTI_ERROR_NONE;
  2172   instanceKlassHandle instanceK_h(current_thread, k);
  2173   // Allocate the result and fill it in
  2174   int result_length = instanceK_h->methods()->length();
  2175   jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
  2176   int index;
  2177   if (JvmtiExport::can_maintain_original_method_order()) {
  2178     // Use the original method ordering indices stored in the class, so we can emit
  2179     // jmethodIDs in the order they appeared in the class file
  2180     for (index = 0; index < result_length; index++) {
  2181       methodOop m = methodOop(instanceK_h->methods()->obj_at(index));
  2182       int original_index = instanceK_h->method_ordering()->int_at(index);
  2183       assert(original_index >= 0 && original_index < result_length, "invalid original method index");
  2184       jmethodID id = m->jmethod_id();
  2185       result_list[original_index] = id;
  2187   } else {
  2188     // otherwise just copy in any order
  2189     for (index = 0; index < result_length; index++) {
  2190       methodOop m = methodOop(instanceK_h->methods()->obj_at(index));
  2191       jmethodID id = m->jmethod_id();
  2192       result_list[index] = id;
  2195   // Fill in return value.
  2196   *method_count_ptr = result_length;
  2197   *methods_ptr = result_list;
  2199   return JVMTI_ERROR_NONE;
  2200 } /* end GetClassMethods */
  2203 // k_mirror - may be primitive, this must be checked
  2204 // field_count_ptr - pre-checked for NULL
  2205 // fields_ptr - pre-checked for NULL
  2206 jvmtiError
  2207 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
  2208   if (java_lang_Class::is_primitive(k_mirror)) {
  2209     *field_count_ptr = 0;
  2210     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
  2211     return JVMTI_ERROR_NONE;
  2213   JavaThread* current_thread = JavaThread::current();
  2214   HandleMark hm(current_thread);
  2215   klassOop k = java_lang_Class::as_klassOop(k_mirror);
  2216   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
  2218   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
  2219   if (!(Klass::cast(k)->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
  2220     return JVMTI_ERROR_CLASS_NOT_PREPARED;
  2223   if (!Klass::cast(k)->oop_is_instance()) {
  2224     *field_count_ptr = 0;
  2225     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
  2226     return JVMTI_ERROR_NONE;
  2230   instanceKlassHandle instanceK_h(current_thread, k);
  2232   int result_count = 0;
  2233   // First, count the fields.
  2234   FilteredFieldStream flds(instanceK_h, true, true);
  2235   result_count = flds.field_count();
  2237   // Allocate the result and fill it in
  2238   jfieldID* result_list = (jfieldID*) jvmtiMalloc(result_count * sizeof(jfieldID));
  2239   // The JVMTI spec requires fields in the order they occur in the class file,
  2240   // this is the reverse order of what FieldStream hands out.
  2241   int id_index = (result_count - 1);
  2243   for (FilteredFieldStream src_st(instanceK_h, true, true); !src_st.eos(); src_st.next()) {
  2244     result_list[id_index--] = jfieldIDWorkaround::to_jfieldID(
  2245                                             instanceK_h, src_st.offset(),
  2246                                             src_st.access_flags().is_static());
  2248   assert(id_index == -1, "just checking");
  2249   // Fill in the results
  2250   *field_count_ptr = result_count;
  2251   *fields_ptr = result_list;
  2253   return JVMTI_ERROR_NONE;
  2254 } /* end GetClassFields */
  2257 // k_mirror - may be primitive, this must be checked
  2258 // interface_count_ptr - pre-checked for NULL
  2259 // interfaces_ptr - pre-checked for NULL
  2260 jvmtiError
  2261 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
  2263     if (java_lang_Class::is_primitive(k_mirror)) {
  2264       *interface_count_ptr = 0;
  2265       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
  2266       return JVMTI_ERROR_NONE;
  2268     JavaThread* current_thread = JavaThread::current();
  2269     HandleMark hm(current_thread);
  2270     klassOop k = java_lang_Class::as_klassOop(k_mirror);
  2271     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
  2273     // Return CLASS_NOT_PREPARED error as per JVMTI spec.
  2274     if (!(Klass::cast(k)->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
  2275       return JVMTI_ERROR_CLASS_NOT_PREPARED;
  2277     if (!Klass::cast(k)->oop_is_instance()) {
  2278       *interface_count_ptr = 0;
  2279       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
  2280       return JVMTI_ERROR_NONE;
  2283     objArrayHandle interface_list(current_thread, instanceKlass::cast(k)->local_interfaces());
  2284     const int result_length = (interface_list.is_null() ? 0 : interface_list->length());
  2285     jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
  2286     for (int i_index = 0; i_index < result_length; i_index += 1) {
  2287       oop oop_at = interface_list->obj_at(i_index);
  2288       assert(oop_at->is_klass(), "interfaces must be klassOops");
  2289       klassOop klassOop_at = klassOop(oop_at);      // ???: is there a better way?
  2290       assert(Klass::cast(klassOop_at)->is_interface(), "interfaces must be interfaces");
  2291       oop mirror_at = Klass::cast(klassOop_at)->java_mirror();
  2292       Handle handle_at = Handle(current_thread, mirror_at);
  2293       result_list[i_index] = (jclass) jni_reference(handle_at);
  2295     *interface_count_ptr = result_length;
  2296     *interfaces_ptr = result_list;
  2299   return JVMTI_ERROR_NONE;
  2300 } /* end GetImplementedInterfaces */
  2303 // k_mirror - may be primitive, this must be checked
  2304 // minor_version_ptr - pre-checked for NULL
  2305 // major_version_ptr - pre-checked for NULL
  2306 jvmtiError
  2307 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
  2308   if (java_lang_Class::is_primitive(k_mirror)) {
  2309     return JVMTI_ERROR_ABSENT_INFORMATION;
  2311   klassOop k_oop = java_lang_Class::as_klassOop(k_mirror);
  2312   Thread *thread = Thread::current();
  2313   HandleMark hm(thread);
  2314   KlassHandle klass(thread, k_oop);
  2316   jint status = klass->jvmti_class_status();
  2317   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
  2318     return JVMTI_ERROR_INVALID_CLASS;
  2320   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
  2321     return JVMTI_ERROR_ABSENT_INFORMATION;
  2324   instanceKlassHandle ik(thread, k_oop);
  2325   *minor_version_ptr = ik->minor_version();
  2326   *major_version_ptr = ik->major_version();
  2328   return JVMTI_ERROR_NONE;
  2329 } /* end GetClassVersionNumbers */
  2332 // k_mirror - may be primitive, this must be checked
  2333 // constant_pool_count_ptr - pre-checked for NULL
  2334 // constant_pool_byte_count_ptr - pre-checked for NULL
  2335 // constant_pool_bytes_ptr - pre-checked for NULL
  2336 jvmtiError
  2337 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
  2338   if (java_lang_Class::is_primitive(k_mirror)) {
  2339     return JVMTI_ERROR_ABSENT_INFORMATION;
  2342   klassOop k_oop = java_lang_Class::as_klassOop(k_mirror);
  2343   Thread *thread = Thread::current();
  2344   HandleMark hm(thread);
  2345   ResourceMark rm(thread);
  2346   KlassHandle klass(thread, k_oop);
  2348   jint status = klass->jvmti_class_status();
  2349   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
  2350     return JVMTI_ERROR_INVALID_CLASS;
  2352   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
  2353     return JVMTI_ERROR_ABSENT_INFORMATION;
  2356   instanceKlassHandle ikh(thread, k_oop);
  2357   constantPoolHandle  constants(thread, ikh->constants());
  2358   ObjectLocker ol(constants, thread);    // lock constant pool while we query it
  2360   JvmtiConstantPoolReconstituter reconstituter(ikh);
  2361   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
  2362     return reconstituter.get_error();
  2365   unsigned char *cpool_bytes;
  2366   int cpool_size = reconstituter.cpool_size();
  2367   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
  2368     return reconstituter.get_error();
  2370   jvmtiError res = allocate(cpool_size, &cpool_bytes);
  2371   if (res != JVMTI_ERROR_NONE) {
  2372     return res;
  2374   reconstituter.copy_cpool_bytes(cpool_bytes);
  2375   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
  2376     return reconstituter.get_error();
  2379   *constant_pool_count_ptr      = constants->length();
  2380   *constant_pool_byte_count_ptr = cpool_size;
  2381   *constant_pool_bytes_ptr      = cpool_bytes;
  2383   return JVMTI_ERROR_NONE;
  2384 } /* end GetConstantPool */
  2387 // k_mirror - may be primitive, this must be checked
  2388 // is_interface_ptr - pre-checked for NULL
  2389 jvmtiError
  2390 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
  2392     bool result = false;
  2393     if (!java_lang_Class::is_primitive(k_mirror)) {
  2394       klassOop k = java_lang_Class::as_klassOop(k_mirror);
  2395       if (k != NULL && Klass::cast(k)->is_interface()) {
  2396         result = true;
  2399     *is_interface_ptr = result;
  2402   return JVMTI_ERROR_NONE;
  2403 } /* end IsInterface */
  2406 // k_mirror - may be primitive, this must be checked
  2407 // is_array_class_ptr - pre-checked for NULL
  2408 jvmtiError
  2409 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
  2411     bool result = false;
  2412     if (!java_lang_Class::is_primitive(k_mirror)) {
  2413       klassOop k = java_lang_Class::as_klassOop(k_mirror);
  2414       if (k != NULL && Klass::cast(k)->oop_is_array()) {
  2415         result = true;
  2418     *is_array_class_ptr = result;
  2421   return JVMTI_ERROR_NONE;
  2422 } /* end IsArrayClass */
  2425 // k_mirror - may be primitive, this must be checked
  2426 // classloader_ptr - pre-checked for NULL
  2427 jvmtiError
  2428 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
  2430     if (java_lang_Class::is_primitive(k_mirror)) {
  2431       *classloader_ptr = (jclass) jni_reference(Handle());
  2432       return JVMTI_ERROR_NONE;
  2434     JavaThread* current_thread = JavaThread::current();
  2435     HandleMark hm(current_thread);
  2436     klassOop k = java_lang_Class::as_klassOop(k_mirror);
  2437     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
  2439     oop result_oop = Klass::cast(k)->class_loader();
  2440     if (result_oop == NULL) {
  2441       *classloader_ptr = (jclass) jni_reference(Handle());
  2442       return JVMTI_ERROR_NONE;
  2444     Handle result_handle = Handle(current_thread, result_oop);
  2445     jclass result_jnihandle = (jclass) jni_reference(result_handle);
  2446     *classloader_ptr = result_jnihandle;
  2448   return JVMTI_ERROR_NONE;
  2449 } /* end GetClassLoader */
  2452 // k_mirror - may be primitive, this must be checked
  2453 // source_debug_extension_ptr - pre-checked for NULL
  2454 jvmtiError
  2455 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
  2457     if (java_lang_Class::is_primitive(k_mirror)) {
  2458       return JVMTI_ERROR_ABSENT_INFORMATION;
  2460     klassOop k = java_lang_Class::as_klassOop(k_mirror);
  2461     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
  2462     if (!Klass::cast(k)->oop_is_instance()) {
  2463       return JVMTI_ERROR_ABSENT_INFORMATION;
  2465     symbolOop sdeOop = instanceKlass::cast(k)->source_debug_extension();
  2466     NULL_CHECK(sdeOop, JVMTI_ERROR_ABSENT_INFORMATION);
  2469       JavaThread* current_thread  = JavaThread::current();
  2470       ResourceMark rm(current_thread);
  2471       const char* sdecp = (const char*) sdeOop->as_C_string();
  2472       *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sdecp)+1);
  2473       strcpy(*source_debug_extension_ptr, sdecp);
  2477   return JVMTI_ERROR_NONE;
  2478 } /* end GetSourceDebugExtension */
  2480   //
  2481   // Object functions
  2482   //
  2484 // hash_code_ptr - pre-checked for NULL
  2485 jvmtiError
  2486 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
  2487   oop mirror = JNIHandles::resolve_external_guard(object);
  2488   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
  2489   NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
  2492     jint result = (jint) mirror->identity_hash();
  2493     *hash_code_ptr = result;
  2495   return JVMTI_ERROR_NONE;
  2496 } /* end GetObjectHashCode */
  2499 // info_ptr - pre-checked for NULL
  2500 jvmtiError
  2501 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
  2502   JavaThread* calling_thread = JavaThread::current();
  2503   jvmtiError err = get_object_monitor_usage(calling_thread, object, info_ptr);
  2504   if (err == JVMTI_ERROR_THREAD_NOT_SUSPENDED) {
  2505     // Some of the critical threads were not suspended. go to a safepoint and try again
  2506     VM_GetObjectMonitorUsage op(this, calling_thread, object, info_ptr);
  2507     VMThread::execute(&op);
  2508     err = op.result();
  2510   return err;
  2511 } /* end GetObjectMonitorUsage */
  2514   //
  2515   // Field functions
  2516   //
  2518 // name_ptr - NULL is a valid value, must be checked
  2519 // signature_ptr - NULL is a valid value, must be checked
  2520 // generic_ptr - NULL is a valid value, must be checked
  2521 jvmtiError
  2522 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
  2523   JavaThread* current_thread  = JavaThread::current();
  2524   ResourceMark rm(current_thread);
  2525   if (name_ptr == NULL) {
  2526     // just don't return the name
  2527   } else {
  2528     const char* fieldName = fdesc_ptr->name()->as_C_string();
  2529     *name_ptr =  (char*) jvmtiMalloc(strlen(fieldName) + 1);
  2530     if (*name_ptr == NULL)
  2531       return JVMTI_ERROR_OUT_OF_MEMORY;
  2532     strcpy(*name_ptr, fieldName);
  2534   if (signature_ptr== NULL) {
  2535     // just don't return the signature
  2536   } else {
  2537     const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
  2538     *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
  2539     if (*signature_ptr == NULL)
  2540       return JVMTI_ERROR_OUT_OF_MEMORY;
  2541     strcpy(*signature_ptr, fieldSignature);
  2543   if (generic_ptr != NULL) {
  2544     *generic_ptr = NULL;
  2545     symbolOop soop = fdesc_ptr->generic_signature();
  2546     if (soop != NULL) {
  2547       const char* gen_sig = soop->as_C_string();
  2548       if (gen_sig != NULL) {
  2549         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
  2550         if (err != JVMTI_ERROR_NONE) {
  2551           return err;
  2553         strcpy(*generic_ptr, gen_sig);
  2557   return JVMTI_ERROR_NONE;
  2558 } /* end GetFieldName */
  2561 // declaring_class_ptr - pre-checked for NULL
  2562 jvmtiError
  2563 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
  2565   *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
  2566   return JVMTI_ERROR_NONE;
  2567 } /* end GetFieldDeclaringClass */
  2570 // modifiers_ptr - pre-checked for NULL
  2571 jvmtiError
  2572 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
  2574   AccessFlags resultFlags = fdesc_ptr->access_flags();
  2575   jint result = resultFlags.as_int();
  2576   *modifiers_ptr = result;
  2578   return JVMTI_ERROR_NONE;
  2579 } /* end GetFieldModifiers */
  2582 // is_synthetic_ptr - pre-checked for NULL
  2583 jvmtiError
  2584 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
  2585   *is_synthetic_ptr = fdesc_ptr->is_synthetic();
  2586   return JVMTI_ERROR_NONE;
  2587 } /* end IsFieldSynthetic */
  2590   //
  2591   // Method functions
  2592   //
  2594 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2595 // name_ptr - NULL is a valid value, must be checked
  2596 // signature_ptr - NULL is a valid value, must be checked
  2597 // generic_ptr - NULL is a valid value, must be checked
  2598 jvmtiError
  2599 JvmtiEnv::GetMethodName(methodOop method_oop, char** name_ptr, char** signature_ptr, char** generic_ptr) {
  2600   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2601   JavaThread* current_thread  = JavaThread::current();
  2603   ResourceMark rm(current_thread); // get the utf8 name and signature
  2604   if (name_ptr == NULL) {
  2605     // just don't return the name
  2606   } else {
  2607     const char* utf8_name = (const char *) method_oop->name()->as_utf8();
  2608     *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
  2609     strcpy(*name_ptr, utf8_name);
  2611   if (signature_ptr == NULL) {
  2612     // just don't return the signature
  2613   } else {
  2614     const char* utf8_signature = (const char *) method_oop->signature()->as_utf8();
  2615     *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
  2616     strcpy(*signature_ptr, utf8_signature);
  2619   if (generic_ptr != NULL) {
  2620     *generic_ptr = NULL;
  2621     symbolOop soop = method_oop->generic_signature();
  2622     if (soop != NULL) {
  2623       const char* gen_sig = soop->as_C_string();
  2624       if (gen_sig != NULL) {
  2625         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
  2626         if (err != JVMTI_ERROR_NONE) {
  2627           return err;
  2629         strcpy(*generic_ptr, gen_sig);
  2633   return JVMTI_ERROR_NONE;
  2634 } /* end GetMethodName */
  2637 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2638 // declaring_class_ptr - pre-checked for NULL
  2639 jvmtiError
  2640 JvmtiEnv::GetMethodDeclaringClass(methodOop method_oop, jclass* declaring_class_ptr) {
  2641   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2642   (*declaring_class_ptr) = get_jni_class_non_null(method_oop->method_holder());
  2643   return JVMTI_ERROR_NONE;
  2644 } /* end GetMethodDeclaringClass */
  2647 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2648 // modifiers_ptr - pre-checked for NULL
  2649 jvmtiError
  2650 JvmtiEnv::GetMethodModifiers(methodOop method_oop, jint* modifiers_ptr) {
  2651   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2652   (*modifiers_ptr) = method_oop->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
  2653   return JVMTI_ERROR_NONE;
  2654 } /* end GetMethodModifiers */
  2657 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2658 // max_ptr - pre-checked for NULL
  2659 jvmtiError
  2660 JvmtiEnv::GetMaxLocals(methodOop method_oop, jint* max_ptr) {
  2661   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2662   // get max stack
  2663   (*max_ptr) = method_oop->max_locals();
  2664   return JVMTI_ERROR_NONE;
  2665 } /* end GetMaxLocals */
  2668 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2669 // size_ptr - pre-checked for NULL
  2670 jvmtiError
  2671 JvmtiEnv::GetArgumentsSize(methodOop method_oop, jint* size_ptr) {
  2672   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2673   // get size of arguments
  2675   (*size_ptr) = method_oop->size_of_parameters();
  2676   return JVMTI_ERROR_NONE;
  2677 } /* end GetArgumentsSize */
  2680 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2681 // entry_count_ptr - pre-checked for NULL
  2682 // table_ptr - pre-checked for NULL
  2683 jvmtiError
  2684 JvmtiEnv::GetLineNumberTable(methodOop method_oop, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
  2685   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2686   if (!method_oop->has_linenumber_table()) {
  2687     return (JVMTI_ERROR_ABSENT_INFORMATION);
  2690   // The line number table is compressed so we don't know how big it is until decompressed.
  2691   // Decompression is really fast so we just do it twice.
  2693   // Compute size of table
  2694   jint num_entries = 0;
  2695   CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
  2696   while (stream.read_pair()) {
  2697     num_entries++;
  2699   jvmtiLineNumberEntry *jvmti_table =
  2700             (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
  2702   // Fill jvmti table
  2703   if (num_entries > 0) {
  2704     int index = 0;
  2705     CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
  2706     while (stream.read_pair()) {
  2707       jvmti_table[index].start_location = (jlocation) stream.bci();
  2708       jvmti_table[index].line_number = (jint) stream.line();
  2709       index++;
  2711     assert(index == num_entries, "sanity check");
  2714   // Set up results
  2715   (*entry_count_ptr) = num_entries;
  2716   (*table_ptr) = jvmti_table;
  2718   return JVMTI_ERROR_NONE;
  2719 } /* end GetLineNumberTable */
  2722 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2723 // start_location_ptr - pre-checked for NULL
  2724 // end_location_ptr - pre-checked for NULL
  2725 jvmtiError
  2726 JvmtiEnv::GetMethodLocation(methodOop method_oop, jlocation* start_location_ptr, jlocation* end_location_ptr) {
  2728   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2729   // get start and end location
  2730   (*end_location_ptr) = (jlocation) (method_oop->code_size() - 1);
  2731   if (method_oop->code_size() == 0) {
  2732     // there is no code so there is no start location
  2733     (*start_location_ptr) = (jlocation)(-1);
  2734   } else {
  2735     (*start_location_ptr) = (jlocation)(0);
  2738   return JVMTI_ERROR_NONE;
  2739 } /* end GetMethodLocation */
  2742 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2743 // entry_count_ptr - pre-checked for NULL
  2744 // table_ptr - pre-checked for NULL
  2745 jvmtiError
  2746 JvmtiEnv::GetLocalVariableTable(methodOop method_oop, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
  2748   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2749   JavaThread* current_thread  = JavaThread::current();
  2751   // does the klass have any local variable information?
  2752   instanceKlass* ik = instanceKlass::cast(method_oop->method_holder());
  2753   if (!ik->access_flags().has_localvariable_table()) {
  2754     return (JVMTI_ERROR_ABSENT_INFORMATION);
  2757   constantPoolOop constants = method_oop->constants();
  2758   NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
  2760   // in the vm localvariable table representation, 6 consecutive elements in the table
  2761   // represent a 6-tuple of shorts
  2762   // [start_pc, length, name_index, descriptor_index, signature_index, index]
  2763   jint num_entries = method_oop->localvariable_table_length();
  2764   jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
  2765                 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
  2767   if (num_entries > 0) {
  2768     LocalVariableTableElement* table = method_oop->localvariable_table_start();
  2769     for (int i = 0; i < num_entries; i++) {
  2770       // get the 5 tuple information from the vm table
  2771       jlocation start_location = (jlocation) table[i].start_bci;
  2772       jint length = (jint) table[i].length;
  2773       int name_index = (int) table[i].name_cp_index;
  2774       int signature_index = (int) table[i].descriptor_cp_index;
  2775       int generic_signature_index = (int) table[i].signature_cp_index;
  2776       jint slot = (jint) table[i].slot;
  2778       // get utf8 name and signature
  2779       char *name_buf = NULL;
  2780       char *sig_buf = NULL;
  2781       char *gen_sig_buf = NULL;
  2783         ResourceMark rm(current_thread);
  2785         const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
  2786         name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
  2787         strcpy(name_buf, utf8_name);
  2789         const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
  2790         sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
  2791         strcpy(sig_buf, utf8_signature);
  2793         if (generic_signature_index > 0) {
  2794           const char *utf8_gen_sign = (const char *)
  2795                                        constants->symbol_at(generic_signature_index)->as_utf8();
  2796           gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
  2797           strcpy(gen_sig_buf, utf8_gen_sign);
  2801       // fill in the jvmti local variable table
  2802       jvmti_table[i].start_location = start_location;
  2803       jvmti_table[i].length = length;
  2804       jvmti_table[i].name = name_buf;
  2805       jvmti_table[i].signature = sig_buf;
  2806       jvmti_table[i].generic_signature = gen_sig_buf;
  2807       jvmti_table[i].slot = slot;
  2811   // set results
  2812   (*entry_count_ptr) = num_entries;
  2813   (*table_ptr) = jvmti_table;
  2815   return JVMTI_ERROR_NONE;
  2816 } /* end GetLocalVariableTable */
  2819 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2820 // bytecode_count_ptr - pre-checked for NULL
  2821 // bytecodes_ptr - pre-checked for NULL
  2822 jvmtiError
  2823 JvmtiEnv::GetBytecodes(methodOop method_oop, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
  2824   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2826   HandleMark hm;
  2827   methodHandle method(method_oop);
  2828   jint size = (jint)method->code_size();
  2829   jvmtiError err = allocate(size, bytecodes_ptr);
  2830   if (err != JVMTI_ERROR_NONE) {
  2831     return err;
  2834   (*bytecode_count_ptr) = size;
  2835   // get byte codes
  2836   JvmtiClassFileReconstituter::copy_bytecodes(method, *bytecodes_ptr);
  2838   return JVMTI_ERROR_NONE;
  2839 } /* end GetBytecodes */
  2842 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2843 // is_native_ptr - pre-checked for NULL
  2844 jvmtiError
  2845 JvmtiEnv::IsMethodNative(methodOop method_oop, jboolean* is_native_ptr) {
  2846   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2847   (*is_native_ptr) = method_oop->is_native();
  2848   return JVMTI_ERROR_NONE;
  2849 } /* end IsMethodNative */
  2852 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2853 // is_synthetic_ptr - pre-checked for NULL
  2854 jvmtiError
  2855 JvmtiEnv::IsMethodSynthetic(methodOop method_oop, jboolean* is_synthetic_ptr) {
  2856   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
  2857   (*is_synthetic_ptr) = method_oop->is_synthetic();
  2858   return JVMTI_ERROR_NONE;
  2859 } /* end IsMethodSynthetic */
  2862 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
  2863 // is_obsolete_ptr - pre-checked for NULL
  2864 jvmtiError
  2865 JvmtiEnv::IsMethodObsolete(methodOop method_oop, jboolean* is_obsolete_ptr) {
  2866   if (method_oop == NULL || method_oop->is_obsolete()) {
  2867     *is_obsolete_ptr = true;
  2868   } else {
  2869     *is_obsolete_ptr = false;
  2871   return JVMTI_ERROR_NONE;
  2872 } /* end IsMethodObsolete */
  2874   //
  2875   // Raw Monitor functions
  2876   //
  2878 // name - pre-checked for NULL
  2879 // monitor_ptr - pre-checked for NULL
  2880 jvmtiError
  2881 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
  2882   JvmtiRawMonitor* rmonitor = new JvmtiRawMonitor(name);
  2883   NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
  2885   *monitor_ptr = (jrawMonitorID)rmonitor;
  2887   return JVMTI_ERROR_NONE;
  2888 } /* end CreateRawMonitor */
  2891 // rmonitor - pre-checked for validity
  2892 jvmtiError
  2893 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
  2894   if (Threads::number_of_threads() == 0) {
  2895     // Remove this  monitor from pending raw monitors list
  2896     // if it has entered in onload or start phase.
  2897     JvmtiPendingMonitors::destroy(rmonitor);
  2898   } else {
  2899     Thread* thread  = Thread::current();
  2900     if (rmonitor->is_entered(thread)) {
  2901       // The caller owns this monitor which we are about to destroy.
  2902       // We exit the underlying synchronization object so that the
  2903       // "delete monitor" call below can work without an assertion
  2904       // failure on systems that don't like destroying synchronization
  2905       // objects that are locked.
  2906       int r;
  2907       intptr_t recursion = rmonitor->recursions();
  2908       for (intptr_t i=0; i <= recursion; i++) {
  2909         r = rmonitor->raw_exit(thread);
  2910         assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
  2911         if (r != ObjectMonitor::OM_OK) {  // robustness
  2912           return JVMTI_ERROR_INTERNAL;
  2916     if (rmonitor->owner() != NULL) {
  2917       // The caller is trying to destroy a monitor that is locked by
  2918       // someone else. While this is not forbidden by the JVMTI
  2919       // spec, it will cause an assertion failure on systems that don't
  2920       // like destroying synchronization objects that are locked.
  2921       // We indicate a problem with the error return (and leak the
  2922       // monitor's memory).
  2923       return JVMTI_ERROR_NOT_MONITOR_OWNER;
  2927   delete rmonitor;
  2929   return JVMTI_ERROR_NONE;
  2930 } /* end DestroyRawMonitor */
  2933 // rmonitor - pre-checked for validity
  2934 jvmtiError
  2935 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
  2936   if (Threads::number_of_threads() == 0) {
  2937     // No JavaThreads exist so ObjectMonitor enter cannot be
  2938     // used, add this raw monitor to the pending list.
  2939     // The pending monitors will be actually entered when
  2940     // the VM is setup.
  2941     // See transition_pending_raw_monitors in create_vm()
  2942     // in thread.cpp.
  2943     JvmtiPendingMonitors::enter(rmonitor);
  2944   } else {
  2945     int r;
  2946     Thread* thread = Thread::current();
  2948     if (thread->is_Java_thread()) {
  2949       JavaThread* current_thread = (JavaThread*)thread;
  2951 #ifdef PROPER_TRANSITIONS
  2952       // Not really unknown but ThreadInVMfromNative does more than we want
  2953       ThreadInVMfromUnknown __tiv;
  2955         ThreadBlockInVM __tbivm(current_thread);
  2956         r = rmonitor->raw_enter(current_thread);
  2958 #else
  2959       /* Transition to thread_blocked without entering vm state          */
  2960       /* This is really evil. Normally you can't undo _thread_blocked    */
  2961       /* transitions like this because it would cause us to miss a       */
  2962       /* safepoint but since the thread was already in _thread_in_native */
  2963       /* the thread is not leaving a safepoint safe state and it will    */
  2964       /* block when it tries to return from native. We can't safepoint   */
  2965       /* block in here because we could deadlock the vmthread. Blech.    */
  2967       JavaThreadState state = current_thread->thread_state();
  2968       assert(state == _thread_in_native, "Must be _thread_in_native");
  2969       // frame should already be walkable since we are in native
  2970       assert(!current_thread->has_last_Java_frame() ||
  2971              current_thread->frame_anchor()->walkable(), "Must be walkable");
  2972       current_thread->set_thread_state(_thread_blocked);
  2974       r = rmonitor->raw_enter(current_thread);
  2975       // restore state, still at a safepoint safe state
  2976       current_thread->set_thread_state(state);
  2978 #endif /* PROPER_TRANSITIONS */
  2979       assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked");
  2980     } else {
  2981       if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
  2982         r = rmonitor->raw_enter(thread);
  2983       } else {
  2984         ShouldNotReachHere();
  2988     if (r != ObjectMonitor::OM_OK) {  // robustness
  2989       return JVMTI_ERROR_INTERNAL;
  2992   return JVMTI_ERROR_NONE;
  2993 } /* end RawMonitorEnter */
  2996 // rmonitor - pre-checked for validity
  2997 jvmtiError
  2998 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
  2999   jvmtiError err = JVMTI_ERROR_NONE;
  3001   if (Threads::number_of_threads() == 0) {
  3002     // No JavaThreads exist so just remove this monitor from the pending list.
  3003     // Bool value from exit is false if rmonitor is not in the list.
  3004     if (!JvmtiPendingMonitors::exit(rmonitor)) {
  3005       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
  3007   } else {
  3008     int r;
  3009     Thread* thread = Thread::current();
  3011     if (thread->is_Java_thread()) {
  3012       JavaThread* current_thread = (JavaThread*)thread;
  3013 #ifdef PROPER_TRANSITIONS
  3014       // Not really unknown but ThreadInVMfromNative does more than we want
  3015       ThreadInVMfromUnknown __tiv;
  3016 #endif /* PROPER_TRANSITIONS */
  3017       r = rmonitor->raw_exit(current_thread);
  3018     } else {
  3019       if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
  3020         r = rmonitor->raw_exit(thread);
  3021       } else {
  3022         ShouldNotReachHere();
  3026     if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
  3027       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
  3028     } else {
  3029       assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
  3030       if (r != ObjectMonitor::OM_OK) {  // robustness
  3031         err = JVMTI_ERROR_INTERNAL;
  3035   return err;
  3036 } /* end RawMonitorExit */
  3039 // rmonitor - pre-checked for validity
  3040 jvmtiError
  3041 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
  3042   int r;
  3043   Thread* thread = Thread::current();
  3045   if (thread->is_Java_thread()) {
  3046     JavaThread* current_thread = (JavaThread*)thread;
  3047 #ifdef PROPER_TRANSITIONS
  3048     // Not really unknown but ThreadInVMfromNative does more than we want
  3049     ThreadInVMfromUnknown __tiv;
  3051       ThreadBlockInVM __tbivm(current_thread);
  3052       r = rmonitor->raw_wait(millis, true, current_thread);
  3054 #else
  3055     /* Transition to thread_blocked without entering vm state          */
  3056     /* This is really evil. Normally you can't undo _thread_blocked    */
  3057     /* transitions like this because it would cause us to miss a       */
  3058     /* safepoint but since the thread was already in _thread_in_native */
  3059     /* the thread is not leaving a safepoint safe state and it will    */
  3060     /* block when it tries to return from native. We can't safepoint   */
  3061     /* block in here because we could deadlock the vmthread. Blech.    */
  3063     JavaThreadState state = current_thread->thread_state();
  3064     assert(state == _thread_in_native, "Must be _thread_in_native");
  3065     // frame should already be walkable since we are in native
  3066     assert(!current_thread->has_last_Java_frame() ||
  3067            current_thread->frame_anchor()->walkable(), "Must be walkable");
  3068     current_thread->set_thread_state(_thread_blocked);
  3070     r = rmonitor->raw_wait(millis, true, current_thread);
  3071     // restore state, still at a safepoint safe state
  3072     current_thread->set_thread_state(state);
  3074 #endif /* PROPER_TRANSITIONS */
  3075   } else {
  3076     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
  3077       r = rmonitor->raw_wait(millis, true, thread);
  3078     } else {
  3079       ShouldNotReachHere();
  3083   switch (r) {
  3084   case ObjectMonitor::OM_INTERRUPTED:
  3085     return JVMTI_ERROR_INTERRUPT;
  3086   case ObjectMonitor::OM_ILLEGAL_MONITOR_STATE:
  3087     return JVMTI_ERROR_NOT_MONITOR_OWNER;
  3089   assert(r == ObjectMonitor::OM_OK, "raw_wait should have worked");
  3090   if (r != ObjectMonitor::OM_OK) {  // robustness
  3091     return JVMTI_ERROR_INTERNAL;
  3094   return JVMTI_ERROR_NONE;
  3095 } /* end RawMonitorWait */
  3098 // rmonitor - pre-checked for validity
  3099 jvmtiError
  3100 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
  3101   int r;
  3102   Thread* thread = Thread::current();
  3104   if (thread->is_Java_thread()) {
  3105     JavaThread* current_thread = (JavaThread*)thread;
  3106     // Not really unknown but ThreadInVMfromNative does more than we want
  3107     ThreadInVMfromUnknown __tiv;
  3108     r = rmonitor->raw_notify(current_thread);
  3109   } else {
  3110     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
  3111       r = rmonitor->raw_notify(thread);
  3112     } else {
  3113       ShouldNotReachHere();
  3117   if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
  3118     return JVMTI_ERROR_NOT_MONITOR_OWNER;
  3120   assert(r == ObjectMonitor::OM_OK, "raw_notify should have worked");
  3121   if (r != ObjectMonitor::OM_OK) {  // robustness
  3122     return JVMTI_ERROR_INTERNAL;
  3125   return JVMTI_ERROR_NONE;
  3126 } /* end RawMonitorNotify */
  3129 // rmonitor - pre-checked for validity
  3130 jvmtiError
  3131 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
  3132   int r;
  3133   Thread* thread = Thread::current();
  3135   if (thread->is_Java_thread()) {
  3136     JavaThread* current_thread = (JavaThread*)thread;
  3137     ThreadInVMfromUnknown __tiv;
  3138     r = rmonitor->raw_notifyAll(current_thread);
  3139   } else {
  3140     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
  3141       r = rmonitor->raw_notifyAll(thread);
  3142     } else {
  3143       ShouldNotReachHere();
  3147   if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
  3148     return JVMTI_ERROR_NOT_MONITOR_OWNER;
  3150   assert(r == ObjectMonitor::OM_OK, "raw_notifyAll should have worked");
  3151   if (r != ObjectMonitor::OM_OK) {  // robustness
  3152     return JVMTI_ERROR_INTERNAL;
  3155   return JVMTI_ERROR_NONE;
  3156 } /* end RawMonitorNotifyAll */
  3159   //
  3160   // JNI Function Interception functions
  3161   //
  3164 // function_table - pre-checked for NULL
  3165 jvmtiError
  3166 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
  3167   // Copy jni function table at safepoint.
  3168   VM_JNIFunctionTableCopier copier(function_table);
  3169   VMThread::execute(&copier);
  3171   return JVMTI_ERROR_NONE;
  3172 } /* end SetJNIFunctionTable */
  3175 // function_table - pre-checked for NULL
  3176 jvmtiError
  3177 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
  3178   *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
  3179   if (*function_table == NULL)
  3180     return JVMTI_ERROR_OUT_OF_MEMORY;
  3181   memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
  3182   return JVMTI_ERROR_NONE;
  3183 } /* end GetJNIFunctionTable */
  3186   //
  3187   // Event Management functions
  3188   //
  3190 jvmtiError
  3191 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
  3192   // can only generate two event types
  3193   if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
  3194       event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
  3195     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
  3198   // for compiled_method_load events we must check that the environment
  3199   // has the can_generate_compiled_method_load_events capability.
  3200   if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
  3201     if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
  3202       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
  3204     return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
  3205   } else {
  3206     return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
  3209 } /* end GenerateEvents */
  3212   //
  3213   // Extension Mechanism functions
  3214   //
  3216 // extension_count_ptr - pre-checked for NULL
  3217 // extensions - pre-checked for NULL
  3218 jvmtiError
  3219 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
  3220   return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
  3221 } /* end GetExtensionFunctions */
  3224 // extension_count_ptr - pre-checked for NULL
  3225 // extensions - pre-checked for NULL
  3226 jvmtiError
  3227 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
  3228   return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
  3229 } /* end GetExtensionEvents */
  3232 // callback - NULL is a valid value, must be checked
  3233 jvmtiError
  3234 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
  3235   return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
  3236 } /* end SetExtensionEventCallback */
  3238   //
  3239   // Timers functions
  3240   //
  3242 // info_ptr - pre-checked for NULL
  3243 jvmtiError
  3244 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
  3245   os::current_thread_cpu_time_info(info_ptr);
  3246   return JVMTI_ERROR_NONE;
  3247 } /* end GetCurrentThreadCpuTimerInfo */
  3250 // nanos_ptr - pre-checked for NULL
  3251 jvmtiError
  3252 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
  3253   *nanos_ptr = os::current_thread_cpu_time();
  3254   return JVMTI_ERROR_NONE;
  3255 } /* end GetCurrentThreadCpuTime */
  3258 // info_ptr - pre-checked for NULL
  3259 jvmtiError
  3260 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
  3261   os::thread_cpu_time_info(info_ptr);
  3262   return JVMTI_ERROR_NONE;
  3263 } /* end GetThreadCpuTimerInfo */
  3266 // Threads_lock NOT held, java_thread not protected by lock
  3267 // java_thread - pre-checked
  3268 // nanos_ptr - pre-checked for NULL
  3269 jvmtiError
  3270 JvmtiEnv::GetThreadCpuTime(JavaThread* java_thread, jlong* nanos_ptr) {
  3271   *nanos_ptr = os::thread_cpu_time(java_thread);
  3272   return JVMTI_ERROR_NONE;
  3273 } /* end GetThreadCpuTime */
  3276 // info_ptr - pre-checked for NULL
  3277 jvmtiError
  3278 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
  3279   os::javaTimeNanos_info(info_ptr);
  3280   return JVMTI_ERROR_NONE;
  3281 } /* end GetTimerInfo */
  3284 // nanos_ptr - pre-checked for NULL
  3285 jvmtiError
  3286 JvmtiEnv::GetTime(jlong* nanos_ptr) {
  3287   *nanos_ptr = os::javaTimeNanos();
  3288   return JVMTI_ERROR_NONE;
  3289 } /* end GetTime */
  3292 // processor_count_ptr - pre-checked for NULL
  3293 jvmtiError
  3294 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
  3295   *processor_count_ptr = os::active_processor_count();
  3296   return JVMTI_ERROR_NONE;
  3297 } /* end GetAvailableProcessors */
  3299   //
  3300   // System Properties functions
  3301   //
  3303 // count_ptr - pre-checked for NULL
  3304 // property_ptr - pre-checked for NULL
  3305 jvmtiError
  3306 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
  3307   jvmtiError err = JVMTI_ERROR_NONE;
  3309   *count_ptr = Arguments::PropertyList_count(Arguments::system_properties());
  3311   err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
  3312   if (err != JVMTI_ERROR_NONE) {
  3313     return err;
  3315   int i = 0 ;
  3316   for (SystemProperty* p = Arguments::system_properties(); p != NULL && i < *count_ptr; p = p->next(), i++) {
  3317     const char *key = p->key();
  3318     char **tmp_value = *property_ptr+i;
  3319     err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
  3320     if (err == JVMTI_ERROR_NONE) {
  3321       strcpy(*tmp_value, key);
  3322     } else {
  3323       // clean up previously allocated memory.
  3324       for (int j=0; j<i; j++) {
  3325         Deallocate((unsigned char*)*property_ptr+j);
  3327       Deallocate((unsigned char*)property_ptr);
  3328       break;
  3331   return err;
  3332 } /* end GetSystemProperties */
  3335 // property - pre-checked for NULL
  3336 // value_ptr - pre-checked for NULL
  3337 jvmtiError
  3338 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
  3339   jvmtiError err = JVMTI_ERROR_NONE;
  3340   const char *value;
  3342   value = Arguments::PropertyList_get_value(Arguments::system_properties(), property);
  3343   if (value == NULL) {
  3344     err =  JVMTI_ERROR_NOT_AVAILABLE;
  3345   } else {
  3346     err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
  3347     if (err == JVMTI_ERROR_NONE) {
  3348       strcpy(*value_ptr, value);
  3351   return err;
  3352 } /* end GetSystemProperty */
  3355 // property - pre-checked for NULL
  3356 // value - NULL is a valid value, must be checked
  3357 jvmtiError
  3358 JvmtiEnv::SetSystemProperty(const char* property, const char* value) {
  3359   jvmtiError err =JVMTI_ERROR_NOT_AVAILABLE;
  3361   for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
  3362     if (strcmp(property, p->key()) == 0) {
  3363       if (p->set_value((char *)value)) {
  3364         err =  JVMTI_ERROR_NONE;
  3368   return err;
  3369 } /* end SetSystemProperty */
  3371 #endif // !JVMTI_KERNEL

mercurial