rev 52084 : 8211980: Remove ThreadHeapSampler enable/disable/enabled methods
Summary:
Reviewed-by:

   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderExt.hpp"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/stringTable.hpp"
  29 #include "classfile/modules.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "interpreter/bytecodeStream.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvmtifiles/jvmtiEnv.hpp"
  35 #include "logging/log.hpp"
  36 #include "logging/logConfiguration.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "memory/universe.hpp"
  39 #include "oops/instanceKlass.hpp"
  40 #include "oops/objArrayOop.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "prims/jniCheck.hpp"
  43 #include "prims/jvm_misc.hpp"
  44 #include "prims/jvmtiAgentThread.hpp"
  45 #include "prims/jvmtiClassFileReconstituter.hpp"
  46 #include "prims/jvmtiCodeBlobEvents.hpp"
  47 #include "prims/jvmtiExtensions.hpp"
  48 #include "prims/jvmtiGetLoadedClasses.hpp"
  49 #include "prims/jvmtiImpl.hpp"
  50 #include "prims/jvmtiManageCapabilities.hpp"
  51 #include "prims/jvmtiRawMonitor.hpp"
  52 #include "prims/jvmtiRedefineClasses.hpp"
  53 #include "prims/jvmtiTagMap.hpp"
  54 #include "prims/jvmtiThreadState.inline.hpp"
  55 #include "prims/jvmtiUtil.hpp"
  56 #include "runtime/arguments.hpp"
  57 #include "runtime/deoptimization.hpp"
  58 #include "runtime/fieldDescriptor.inline.hpp"
  59 #include "runtime/interfaceSupport.inline.hpp"
  60 #include "runtime/javaCalls.hpp"
  61 #include "runtime/jfieldIDWorkaround.hpp"
  62 #include "runtime/jniHandles.inline.hpp"
  63 #include "runtime/objectMonitor.inline.hpp"
  64 #include "runtime/osThread.hpp"
  65 #include "runtime/reflectionUtils.hpp"
  66 #include "runtime/signature.hpp"
  67 #include "runtime/thread.inline.hpp"
  68 #include "runtime/threadHeapSampler.hpp"
  69 #include "runtime/threadSMR.hpp"
  70 #include "runtime/timerTrace.hpp"
  71 #include "runtime/vframe.inline.hpp"
  72 #include "runtime/vmThread.hpp"
  73 #include "services/threadService.hpp"
  74 #include "utilities/exceptions.hpp"
  75 #include "utilities/preserveException.hpp"
  76 
  77 
  78 #define FIXLATER 0 // REMOVE this when completed.
  79 
  80  // FIXLATER: hook into JvmtiTrace
  81 #define TraceJVMTICalls false
  82 
  83 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
  84 }
  85 
  86 JvmtiEnv::~JvmtiEnv() {
  87 }
  88 
  89 JvmtiEnv*
  90 JvmtiEnv::create_a_jvmti(jint version) {
  91   return new JvmtiEnv(version);
  92 }
  93 
  94 // VM operation class to copy jni function table at safepoint.
  95 // More than one java threads or jvmti agents may be reading/
  96 // modifying jni function tables. To reduce the risk of bad
  97 // interaction b/w these threads it is copied at safepoint.
  98 class VM_JNIFunctionTableCopier : public VM_Operation {
  99  private:
 100   const struct JNINativeInterface_ *_function_table;
 101  public:
 102   VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
 103     _function_table = func_tbl;
 104   };
 105 
 106   VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
 107   void doit() {
 108     copy_jni_function_table(_function_table);
 109   };
 110 };
 111 
 112 //
 113 // Do not change the "prefix" marker below, everything above it is copied
 114 // unchanged into the filled stub, everything below is controlled by the
 115 // stub filler (only method bodies are carried forward, and then only for
 116 // functionality still in the spec).
 117 //
 118 // end file prefix
 119 
 120   //
 121   // Memory Management functions
 122   //
 123 
 124 // mem_ptr - pre-checked for NULL
 125 jvmtiError
 126 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
 127   return allocate(size, mem_ptr);
 128 } /* end Allocate */
 129 
 130 
 131 // mem - NULL is a valid value, must be checked
 132 jvmtiError
 133 JvmtiEnv::Deallocate(unsigned char* mem) {
 134   return deallocate(mem);
 135 } /* end Deallocate */
 136 
 137 // Threads_lock NOT held, java_thread not protected by lock
 138 // java_thread - pre-checked
 139 // data - NULL is a valid value, must be checked
 140 jvmtiError
 141 JvmtiEnv::SetThreadLocalStorage(JavaThread* java_thread, const void* data) {
 142   JvmtiThreadState* state = java_thread->jvmti_thread_state();
 143   if (state == NULL) {
 144     if (data == NULL) {
 145       // leaving state unset same as data set to NULL
 146       return JVMTI_ERROR_NONE;
 147     }
 148     // otherwise, create the state
 149     state = JvmtiThreadState::state_for(java_thread);
 150     if (state == NULL) {
 151       return JVMTI_ERROR_THREAD_NOT_ALIVE;
 152     }
 153   }
 154   state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
 155   return JVMTI_ERROR_NONE;
 156 } /* end SetThreadLocalStorage */
 157 
 158 
 159 // Threads_lock NOT held
 160 // thread - NOT pre-checked
 161 // data_ptr - pre-checked for NULL
 162 jvmtiError
 163 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
 164   JavaThread* current_thread = JavaThread::current();
 165   if (thread == NULL) {
 166     JvmtiThreadState* state = current_thread->jvmti_thread_state();
 167     *data_ptr = (state == NULL) ? NULL :
 168       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 169   } else {
 170     // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
 171     // the thread to _thread_in_vm. However, when the TLS for a thread
 172     // other than the current thread is required we need to transition
 173     // from native so as to resolve the jthread.
 174 
 175     ThreadInVMfromNative __tiv(current_thread);
 176     VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
 177     debug_only(VMNativeEntryWrapper __vew;)
 178 
 179     JavaThread* java_thread = NULL;
 180     ThreadsListHandle tlh(current_thread);
 181     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);
 182     if (err != JVMTI_ERROR_NONE) {
 183       return err;
 184     }
 185 
 186     JvmtiThreadState* state = java_thread->jvmti_thread_state();
 187     *data_ptr = (state == NULL) ? NULL :
 188       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 189   }
 190   return JVMTI_ERROR_NONE;
 191 } /* end GetThreadLocalStorage */
 192 
 193   //
 194   // Module functions
 195   //
 196 
 197 // module_count_ptr - pre-checked for NULL
 198 // modules_ptr - pre-checked for NULL
 199 jvmtiError
 200 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
 201     JvmtiModuleClosure jmc;
 202 
 203     return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
 204 } /* end GetAllModules */
 205 
 206 
 207 // class_loader - NULL is a valid value, must be pre-checked
 208 // package_name - pre-checked for NULL
 209 // module_ptr - pre-checked for NULL
 210 jvmtiError
 211 JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
 212   JavaThread* THREAD = JavaThread::current(); // pass to macros
 213   ResourceMark rm(THREAD);
 214 
 215   Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
 216   // Check that loader is a subclass of java.lang.ClassLoader.
 217   if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 218     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 219   }
 220   jobject module = Modules::get_named_module(h_loader, package_name, THREAD);
 221   if (HAS_PENDING_EXCEPTION) {
 222     CLEAR_PENDING_EXCEPTION;
 223     return JVMTI_ERROR_INTERNAL; // unexpected exception
 224   }
 225   *module_ptr = module;
 226   return JVMTI_ERROR_NONE;
 227 } /* end GetNamedModule */
 228 
 229 
 230 // module - pre-checked for NULL
 231 // to_module - pre-checked for NULL
 232 jvmtiError
 233 JvmtiEnv::AddModuleReads(jobject module, jobject to_module) {
 234   JavaThread* THREAD = JavaThread::current();
 235 
 236   // check module
 237   Handle h_module(THREAD, JNIHandles::resolve(module));
 238   if (!java_lang_Module::is_instance(h_module())) {
 239     return JVMTI_ERROR_INVALID_MODULE;
 240   }
 241   // check to_module
 242   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 243   if (!java_lang_Module::is_instance(h_to_module())) {
 244     return JVMTI_ERROR_INVALID_MODULE;
 245   }
 246   return JvmtiExport::add_module_reads(h_module, h_to_module, THREAD);
 247 } /* end AddModuleReads */
 248 
 249 
 250 // module - pre-checked for NULL
 251 // pkg_name - pre-checked for NULL
 252 // to_module - pre-checked for NULL
 253 jvmtiError
 254 JvmtiEnv::AddModuleExports(jobject module, const char* pkg_name, jobject to_module) {
 255   JavaThread* THREAD = JavaThread::current();
 256   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 257 
 258   // check module
 259   Handle h_module(THREAD, JNIHandles::resolve(module));
 260   if (!java_lang_Module::is_instance(h_module())) {
 261     return JVMTI_ERROR_INVALID_MODULE;
 262   }
 263   // check to_module
 264   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 265   if (!java_lang_Module::is_instance(h_to_module())) {
 266     return JVMTI_ERROR_INVALID_MODULE;
 267   }
 268   return JvmtiExport::add_module_exports(h_module, h_pkg, h_to_module, THREAD);
 269 } /* end AddModuleExports */
 270 
 271 
 272 // module - pre-checked for NULL
 273 // pkg_name - pre-checked for NULL
 274 // to_module - pre-checked for NULL
 275 jvmtiError
 276 JvmtiEnv::AddModuleOpens(jobject module, const char* pkg_name, jobject to_module) {
 277   JavaThread* THREAD = JavaThread::current();
 278   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 279 
 280   // check module
 281   Handle h_module(THREAD, JNIHandles::resolve(module));
 282   if (!java_lang_Module::is_instance(h_module())) {
 283     return JVMTI_ERROR_INVALID_MODULE;
 284   }
 285   // check to_module
 286   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 287   if (!java_lang_Module::is_instance(h_to_module())) {
 288     return JVMTI_ERROR_INVALID_MODULE;
 289   }
 290   return JvmtiExport::add_module_opens(h_module, h_pkg, h_to_module, THREAD);
 291 } /* end AddModuleOpens */
 292 
 293 
 294 // module - pre-checked for NULL
 295 // service - pre-checked for NULL
 296 jvmtiError
 297 JvmtiEnv::AddModuleUses(jobject module, jclass service) {
 298   JavaThread* THREAD = JavaThread::current();
 299 
 300   // check module
 301   Handle h_module(THREAD, JNIHandles::resolve(module));
 302   if (!java_lang_Module::is_instance(h_module())) {
 303     return JVMTI_ERROR_INVALID_MODULE;
 304   }
 305   // check service
 306   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 307   if (!java_lang_Class::is_instance(h_service()) ||
 308       java_lang_Class::is_primitive(h_service())) {
 309     return JVMTI_ERROR_INVALID_CLASS;
 310   }
 311   return JvmtiExport::add_module_uses(h_module, h_service, THREAD);
 312 } /* end AddModuleUses */
 313 
 314 
 315 // module - pre-checked for NULL
 316 // service - pre-checked for NULL
 317 // impl_class - pre-checked for NULL
 318 jvmtiError
 319 JvmtiEnv::AddModuleProvides(jobject module, jclass service, jclass impl_class) {
 320   JavaThread* THREAD = JavaThread::current();
 321 
 322   // check module
 323   Handle h_module(THREAD, JNIHandles::resolve(module));
 324   if (!java_lang_Module::is_instance(h_module())) {
 325     return JVMTI_ERROR_INVALID_MODULE;
 326   }
 327   // check service
 328   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 329   if (!java_lang_Class::is_instance(h_service()) ||
 330       java_lang_Class::is_primitive(h_service())) {
 331     return JVMTI_ERROR_INVALID_CLASS;
 332   }
 333   // check impl_class
 334   Handle h_impl_class(THREAD, JNIHandles::resolve_external_guard(impl_class));
 335   if (!java_lang_Class::is_instance(h_impl_class()) ||
 336       java_lang_Class::is_primitive(h_impl_class())) {
 337     return JVMTI_ERROR_INVALID_CLASS;
 338   }
 339   return JvmtiExport::add_module_provides(h_module, h_service, h_impl_class, THREAD);
 340 } /* end AddModuleProvides */
 341 
 342 // module - pre-checked for NULL
 343 // is_modifiable_class_ptr - pre-checked for NULL
 344 jvmtiError
 345 JvmtiEnv::IsModifiableModule(jobject module, jboolean* is_modifiable_module_ptr) {
 346   JavaThread* THREAD = JavaThread::current();
 347 
 348   // check module
 349   Handle h_module(THREAD, JNIHandles::resolve(module));
 350   if (!java_lang_Module::is_instance(h_module())) {
 351     return JVMTI_ERROR_INVALID_MODULE;
 352   }
 353 
 354   *is_modifiable_module_ptr = JNI_TRUE;
 355   return JVMTI_ERROR_NONE;
 356 } /* end IsModifiableModule */
 357 
 358 
 359   //
 360   // Class functions
 361   //
 362 
 363 // class_count_ptr - pre-checked for NULL
 364 // classes_ptr - pre-checked for NULL
 365 jvmtiError
 366 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
 367   return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
 368 } /* end GetLoadedClasses */
 369 
 370 
 371 // initiating_loader - NULL is a valid value, must be checked
 372 // class_count_ptr - pre-checked for NULL
 373 // classes_ptr - pre-checked for NULL
 374 jvmtiError
 375 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
 376   return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
 377                                                   class_count_ptr, classes_ptr);
 378 } /* end GetClassLoaderClasses */
 379 
 380 // k_mirror - may be primitive, this must be checked
 381 // is_modifiable_class_ptr - pre-checked for NULL
 382 jvmtiError
 383 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
 384   *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
 385                                                        JNI_TRUE : JNI_FALSE;
 386   return JVMTI_ERROR_NONE;
 387 } /* end IsModifiableClass */
 388 
 389 // class_count - pre-checked to be greater than or equal to 0
 390 // classes - pre-checked for NULL
 391 jvmtiError
 392 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
 393 //TODO: add locking
 394 
 395   int index;
 396   JavaThread* current_thread = JavaThread::current();
 397   ResourceMark rm(current_thread);
 398 
 399   jvmtiClassDefinition* class_definitions =
 400                             NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
 401   NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
 402 
 403   for (index = 0; index < class_count; index++) {
 404     HandleMark hm(current_thread);
 405 
 406     jclass jcls = classes[index];
 407     oop k_mirror = JNIHandles::resolve_external_guard(jcls);
 408     if (k_mirror == NULL) {
 409       return JVMTI_ERROR_INVALID_CLASS;
 410     }
 411     if (!k_mirror->is_a(SystemDictionary::Class_klass())) {
 412       return JVMTI_ERROR_INVALID_CLASS;
 413     }
 414 
 415     if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
 416       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
 417     }
 418 
 419     Klass* klass = java_lang_Class::as_Klass(k_mirror);
 420 
 421     jint status = klass->jvmti_class_status();
 422     if (status & (JVMTI_CLASS_STATUS_ERROR)) {
 423       return JVMTI_ERROR_INVALID_CLASS;
 424     }
 425 
 426     InstanceKlass* ik = InstanceKlass::cast(klass);
 427     if (ik->get_cached_class_file_bytes() == NULL) {
 428       // Not cached, we need to reconstitute the class file from the
 429       // VM representation. We don't attach the reconstituted class
 430       // bytes to the InstanceKlass here because they have not been
 431       // validated and we're not at a safepoint.
 432       JvmtiClassFileReconstituter reconstituter(ik);
 433       if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
 434         return reconstituter.get_error();
 435       }
 436 
 437       class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
 438       class_definitions[index].class_bytes      = (unsigned char*)
 439                                                        reconstituter.class_file_bytes();
 440     } else {
 441       // it is cached, get it from the cache
 442       class_definitions[index].class_byte_count = ik->get_cached_class_file_len();
 443       class_definitions[index].class_bytes      = ik->get_cached_class_file_bytes();
 444     }
 445     class_definitions[index].klass              = jcls;
 446   }
 447   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
 448   VMThread::execute(&op);
 449   return (op.check_error());
 450 } /* end RetransformClasses */
 451 
 452 
 453 // class_count - pre-checked to be greater than or equal to 0
 454 // class_definitions - pre-checked for NULL
 455 jvmtiError
 456 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
 457 //TODO: add locking
 458   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
 459   VMThread::execute(&op);
 460   return (op.check_error());
 461 } /* end RedefineClasses */
 462 
 463 
 464   //
 465   // Object functions
 466   //
 467 
 468 // size_ptr - pre-checked for NULL
 469 jvmtiError
 470 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
 471   oop mirror = JNIHandles::resolve_external_guard(object);
 472   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
 473   *size_ptr = (jlong)Universe::heap()->obj_size(mirror) * wordSize;
 474   return JVMTI_ERROR_NONE;
 475 } /* end GetObjectSize */
 476 
 477   //
 478   // Method functions
 479   //
 480 
 481 // prefix - NULL is a valid value, must be checked
 482 jvmtiError
 483 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
 484   return prefix == NULL?
 485               SetNativeMethodPrefixes(0, NULL) :
 486               SetNativeMethodPrefixes(1, (char**)&prefix);
 487 } /* end SetNativeMethodPrefix */
 488 
 489 
 490 // prefix_count - pre-checked to be greater than or equal to 0
 491 // prefixes - pre-checked for NULL
 492 jvmtiError
 493 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
 494   // Have to grab JVMTI thread state lock to be sure that some thread
 495   // isn't accessing the prefixes at the same time we are setting them.
 496   // No locks during VM bring-up.
 497   if (Threads::number_of_threads() == 0) {
 498     return set_native_method_prefixes(prefix_count, prefixes);
 499   } else {
 500     MutexLocker mu(JvmtiThreadState_lock);
 501     return set_native_method_prefixes(prefix_count, prefixes);
 502   }
 503 } /* end SetNativeMethodPrefixes */
 504 
 505   //
 506   // Event Management functions
 507   //
 508 
 509 // callbacks - NULL is a valid value, must be checked
 510 // size_of_callbacks - pre-checked to be greater than or equal to 0
 511 jvmtiError
 512 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
 513   JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
 514   return JVMTI_ERROR_NONE;
 515 } /* end SetEventCallbacks */
 516 
 517 
 518 // event_thread - NULL is a valid value, must be checked
 519 jvmtiError
 520 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread,   ...) {
 521   if (event_thread == NULL) {
 522     // Can be called at Agent_OnLoad() time with event_thread == NULL
 523     // when Thread::current() does not work yet so we cannot create a
 524     // ThreadsListHandle that is common to both thread-specific and
 525     // global code paths.
 526 
 527     // event_type must be valid
 528     if (!JvmtiEventController::is_valid_event_type(event_type)) {
 529       return JVMTI_ERROR_INVALID_EVENT_TYPE;
 530     }
 531 
 532     bool enabled = (mode == JVMTI_ENABLE);
 533 
 534     // assure that needed capabilities are present
 535     if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 536       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 537     }
 538 
 539     if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 540       record_class_file_load_hook_enabled();
 541     }
 542 







 543     JvmtiEventController::set_user_enabled(this, (JavaThread*) NULL, event_type, enabled);
 544   } else {
 545     // We have a specified event_thread.
 546     JavaThread* java_thread = NULL;
 547     ThreadsListHandle tlh;
 548     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), event_thread, &java_thread, NULL);
 549     if (err != JVMTI_ERROR_NONE) {
 550       return err;
 551     }
 552 
 553     // event_type must be valid
 554     if (!JvmtiEventController::is_valid_event_type(event_type)) {
 555       return JVMTI_ERROR_INVALID_EVENT_TYPE;
 556     }
 557 
 558     // global events cannot be controlled at thread level.
 559     if (JvmtiEventController::is_global_event(event_type)) {
 560       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 561     }
 562 
 563     bool enabled = (mode == JVMTI_ENABLE);
 564 
 565     // assure that needed capabilities are present
 566     if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 567       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 568     }
 569 
 570     if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 571       record_class_file_load_hook_enabled();
 572     }
 573     JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);
 574   }
 575 
 576   return JVMTI_ERROR_NONE;
 577 } /* end SetEventNotificationMode */
 578 
 579   //
 580   // Capability functions
 581   //
 582 
 583 // capabilities_ptr - pre-checked for NULL
 584 jvmtiError
 585 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
 586   JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
 587                                                       get_prohibited_capabilities(),
 588                                                       capabilities_ptr);
 589   return JVMTI_ERROR_NONE;
 590 } /* end GetPotentialCapabilities */
 591 
 592 
 593 // capabilities_ptr - pre-checked for NULL
 594 jvmtiError
 595 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 596   return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
 597                                                    get_prohibited_capabilities(),
 598                                                    capabilities_ptr,
 599                                                    get_capabilities());
 600 } /* end AddCapabilities */
 601 
 602 
 603 // capabilities_ptr - pre-checked for NULL
 604 jvmtiError
 605 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 606   JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
 607   return JVMTI_ERROR_NONE;
 608 } /* end RelinquishCapabilities */
 609 
 610 
 611 // capabilities_ptr - pre-checked for NULL
 612 jvmtiError
 613 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
 614   JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
 615   return JVMTI_ERROR_NONE;
 616 } /* end GetCapabilities */
 617 
 618   //
 619   // Class Loader Search functions
 620   //
 621 
 622 // segment - pre-checked for NULL
 623 jvmtiError
 624 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
 625   jvmtiPhase phase = get_phase();
 626   if (phase == JVMTI_PHASE_ONLOAD) {
 627     Arguments::append_sysclasspath(segment);
 628     return JVMTI_ERROR_NONE;
 629   } else if (use_version_1_0_semantics()) {
 630     // This JvmtiEnv requested version 1.0 semantics and this function
 631     // is only allowed in the ONLOAD phase in version 1.0 so we need to
 632     // return an error here.
 633     return JVMTI_ERROR_WRONG_PHASE;
 634   } else if (phase == JVMTI_PHASE_LIVE) {
 635     // The phase is checked by the wrapper that called this function,
 636     // but this thread could be racing with the thread that is
 637     // terminating the VM so we check one more time.
 638 
 639     // create the zip entry
 640     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, true);
 641     if (zip_entry == NULL) {
 642       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 643     }
 644 
 645     // lock the loader
 646     Thread* thread = Thread::current();
 647     HandleMark hm;
 648     Handle loader_lock = Handle(thread, SystemDictionary::system_loader_lock());
 649 
 650     ObjectLocker ol(loader_lock, thread);
 651 
 652     // add the jar file to the bootclasspath
 653     log_info(class, load)("opened: %s", zip_entry->name());
 654 #if INCLUDE_CDS
 655     ClassLoaderExt::append_boot_classpath(zip_entry);
 656 #else
 657     ClassLoader::add_to_boot_append_entries(zip_entry);
 658 #endif
 659     return JVMTI_ERROR_NONE;
 660   } else {
 661     return JVMTI_ERROR_WRONG_PHASE;
 662   }
 663 
 664 } /* end AddToBootstrapClassLoaderSearch */
 665 
 666 
 667 // segment - pre-checked for NULL
 668 jvmtiError
 669 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
 670   jvmtiPhase phase = get_phase();
 671 
 672   if (phase == JVMTI_PHASE_ONLOAD) {
 673     for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
 674       if (strcmp("java.class.path", p->key()) == 0) {
 675         p->append_value(segment);
 676         break;
 677       }
 678     }
 679     return JVMTI_ERROR_NONE;
 680   } else if (phase == JVMTI_PHASE_LIVE) {
 681     // The phase is checked by the wrapper that called this function,
 682     // but this thread could be racing with the thread that is
 683     // terminating the VM so we check one more time.
 684     HandleMark hm;
 685 
 686     // create the zip entry (which will open the zip file and hence
 687     // check that the segment is indeed a zip file).
 688     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false);
 689     if (zip_entry == NULL) {
 690       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 691     }
 692     delete zip_entry;   // no longer needed
 693 
 694     // lock the loader
 695     Thread* THREAD = Thread::current();
 696     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 697 
 698     ObjectLocker ol(loader, THREAD);
 699 
 700     // need the path as java.lang.String
 701     Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
 702     if (HAS_PENDING_EXCEPTION) {
 703       CLEAR_PENDING_EXCEPTION;
 704       return JVMTI_ERROR_INTERNAL;
 705     }
 706 
 707     // Invoke the appendToClassPathForInstrumentation method - if the method
 708     // is not found it means the loader doesn't support adding to the class path
 709     // in the live phase.
 710     {
 711       JavaValue res(T_VOID);
 712       JavaCalls::call_special(&res,
 713                               loader,
 714                               loader->klass(),
 715                               vmSymbols::appendToClassPathForInstrumentation_name(),
 716                               vmSymbols::appendToClassPathForInstrumentation_signature(),
 717                               path,
 718                               THREAD);
 719       if (HAS_PENDING_EXCEPTION) {
 720         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 721         CLEAR_PENDING_EXCEPTION;
 722 
 723         if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
 724           return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
 725         } else {
 726           return JVMTI_ERROR_INTERNAL;
 727         }
 728       }
 729     }
 730 
 731     return JVMTI_ERROR_NONE;
 732   } else {
 733     return JVMTI_ERROR_WRONG_PHASE;
 734   }
 735 } /* end AddToSystemClassLoaderSearch */
 736 
 737   //
 738   // General functions
 739   //
 740 
 741 // phase_ptr - pre-checked for NULL
 742 jvmtiError
 743 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
 744   *phase_ptr = phase();
 745   return JVMTI_ERROR_NONE;
 746 } /* end GetPhase */
 747 
 748 
 749 jvmtiError
 750 JvmtiEnv::DisposeEnvironment() {
 751   dispose();
 752   return JVMTI_ERROR_NONE;
 753 } /* end DisposeEnvironment */
 754 
 755 
 756 // data - NULL is a valid value, must be checked
 757 jvmtiError
 758 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
 759   set_env_local_storage(data);
 760   return JVMTI_ERROR_NONE;
 761 } /* end SetEnvironmentLocalStorage */
 762 
 763 
 764 // data_ptr - pre-checked for NULL
 765 jvmtiError
 766 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
 767   *data_ptr = (void*)get_env_local_storage();
 768   return JVMTI_ERROR_NONE;
 769 } /* end GetEnvironmentLocalStorage */
 770 
 771 // version_ptr - pre-checked for NULL
 772 jvmtiError
 773 JvmtiEnv::GetVersionNumber(jint* version_ptr) {
 774   *version_ptr = JVMTI_VERSION;
 775   return JVMTI_ERROR_NONE;
 776 } /* end GetVersionNumber */
 777 
 778 
 779 // name_ptr - pre-checked for NULL
 780 jvmtiError
 781 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
 782   if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
 783     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 784   }
 785   const char *name = JvmtiUtil::error_name(error);
 786   if (name == NULL) {
 787     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 788   }
 789   size_t len = strlen(name) + 1;
 790   jvmtiError err = allocate(len, (unsigned char**)name_ptr);
 791   if (err == JVMTI_ERROR_NONE) {
 792     memcpy(*name_ptr, name, len);
 793   }
 794   return err;
 795 } /* end GetErrorName */
 796 
 797 
 798 jvmtiError
 799 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
 800   LogLevelType level = value == 0 ? LogLevel::Off : LogLevel::Info;
 801   switch (flag) {
 802   case JVMTI_VERBOSE_OTHER:
 803     // ignore
 804     break;
 805   case JVMTI_VERBOSE_CLASS:
 806     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));
 807     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));
 808     break;
 809   case JVMTI_VERBOSE_GC:
 810     if (value == 0) {
 811       LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(gc));
 812     } else {
 813       LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(gc));
 814     }
 815     break;
 816   case JVMTI_VERBOSE_JNI:
 817     PrintJNIResolving = value != 0;
 818     break;
 819   default:
 820     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 821   };
 822   return JVMTI_ERROR_NONE;
 823 } /* end SetVerboseFlag */
 824 
 825 
 826 // format_ptr - pre-checked for NULL
 827 jvmtiError
 828 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
 829   *format_ptr = JVMTI_JLOCATION_JVMBCI;
 830   return JVMTI_ERROR_NONE;
 831 } /* end GetJLocationFormat */
 832 
 833   //
 834   // Thread functions
 835   //
 836 
 837 // Threads_lock NOT held
 838 // thread - NOT pre-checked
 839 // thread_state_ptr - pre-checked for NULL
 840 jvmtiError
 841 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
 842   JavaThread* current_thread = JavaThread::current();
 843   JavaThread* java_thread = NULL;
 844   oop thread_oop = NULL;
 845   ThreadsListHandle tlh(current_thread);
 846 
 847   if (thread == NULL) {
 848     java_thread = current_thread;
 849     thread_oop = java_thread->threadObj();
 850 
 851     if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
 852       return JVMTI_ERROR_INVALID_THREAD;
 853     }
 854   } else {
 855     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
 856     if (err != JVMTI_ERROR_NONE) {
 857       // We got an error code so we don't have a JavaThread *, but
 858       // only return an error from here if we didn't get a valid
 859       // thread_oop.
 860       if (thread_oop == NULL) {
 861         return err;
 862       }
 863       // We have a valid thread_oop so we can return some thread state.
 864     }
 865   }
 866 
 867   // get most state bits
 868   jint state = (jint)java_lang_Thread::get_thread_status(thread_oop);
 869 
 870   if (java_thread != NULL) {
 871     // We have a JavaThread* so add more state bits.
 872     JavaThreadState jts = java_thread->thread_state();
 873 
 874     if (java_thread->is_being_ext_suspended()) {
 875       state |= JVMTI_THREAD_STATE_SUSPENDED;
 876     }
 877     if (jts == _thread_in_native) {
 878       state |= JVMTI_THREAD_STATE_IN_NATIVE;
 879     }
 880     OSThread* osThread = java_thread->osthread();
 881     if (osThread != NULL && osThread->interrupted()) {
 882       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 883     }
 884   }
 885 
 886   *thread_state_ptr = state;
 887   return JVMTI_ERROR_NONE;
 888 } /* end GetThreadState */
 889 
 890 
 891 // thread_ptr - pre-checked for NULL
 892 jvmtiError
 893 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
 894   JavaThread* current_thread  = JavaThread::current();
 895   *thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());
 896   return JVMTI_ERROR_NONE;
 897 } /* end GetCurrentThread */
 898 
 899 
 900 // threads_count_ptr - pre-checked for NULL
 901 // threads_ptr - pre-checked for NULL
 902 jvmtiError
 903 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
 904   int nthreads        = 0;
 905   Handle *thread_objs = NULL;
 906   ResourceMark rm;
 907   HandleMark hm;
 908 
 909   // enumerate threads (including agent threads)
 910   ThreadsListEnumerator tle(Thread::current(), true);
 911   nthreads = tle.num_threads();
 912   *threads_count_ptr = nthreads;
 913 
 914   if (nthreads == 0) {
 915     *threads_ptr = NULL;
 916     return JVMTI_ERROR_NONE;
 917   }
 918 
 919   thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
 920   NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 921 
 922   for (int i=0; i < nthreads; i++) {
 923     thread_objs[i] = Handle(tle.get_threadObj(i));
 924   }
 925 
 926   jthread *jthreads  = new_jthreadArray(nthreads, thread_objs);
 927   NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
 928 
 929   *threads_ptr = jthreads;
 930   return JVMTI_ERROR_NONE;
 931 } /* end GetAllThreads */
 932 
 933 
 934 // Threads_lock NOT held, java_thread not protected by lock
 935 // java_thread - pre-checked
 936 jvmtiError
 937 JvmtiEnv::SuspendThread(JavaThread* java_thread) {
 938   // don't allow hidden thread suspend request.
 939   if (java_thread->is_hidden_from_external_view()) {
 940     return (JVMTI_ERROR_NONE);
 941   }
 942 
 943   {
 944     MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
 945     if (java_thread->is_external_suspend()) {
 946       // don't allow nested external suspend requests.
 947       return (JVMTI_ERROR_THREAD_SUSPENDED);
 948     }
 949     if (java_thread->is_exiting()) { // thread is in the process of exiting
 950       return (JVMTI_ERROR_THREAD_NOT_ALIVE);
 951     }
 952     java_thread->set_external_suspend();
 953   }
 954 
 955   if (!JvmtiSuspendControl::suspend(java_thread)) {
 956     // the thread was in the process of exiting
 957     return (JVMTI_ERROR_THREAD_NOT_ALIVE);
 958   }
 959   return JVMTI_ERROR_NONE;
 960 } /* end SuspendThread */
 961 
 962 
 963 // request_count - pre-checked to be greater than or equal to 0
 964 // request_list - pre-checked for NULL
 965 // results - pre-checked for NULL
 966 jvmtiError
 967 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
 968   int needSafepoint = 0;  // > 0 if we need a safepoint
 969   ThreadsListHandle tlh;
 970   for (int i = 0; i < request_count; i++) {
 971     JavaThread *java_thread = NULL;
 972     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), request_list[i], &java_thread, NULL);
 973     if (err != JVMTI_ERROR_NONE) {
 974       results[i] = err;
 975       continue;
 976     }
 977     // don't allow hidden thread suspend request.
 978     if (java_thread->is_hidden_from_external_view()) {
 979       results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
 980       continue;
 981     }
 982 
 983     {
 984       MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
 985       if (java_thread->is_external_suspend()) {
 986         // don't allow nested external suspend requests.
 987         results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
 988         continue;
 989       }
 990       if (java_thread->is_exiting()) { // thread is in the process of exiting
 991         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 992         continue;
 993       }
 994       java_thread->set_external_suspend();
 995     }
 996     if (java_thread->thread_state() == _thread_in_native) {
 997       // We need to try and suspend native threads here. Threads in
 998       // other states will self-suspend on their next transition.
 999       if (!JvmtiSuspendControl::suspend(java_thread)) {
1000         // The thread was in the process of exiting. Force another
1001         // safepoint to make sure that this thread transitions.
1002         needSafepoint++;
1003         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
1004         continue;
1005       }
1006     } else {
1007       needSafepoint++;
1008     }
1009     results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
1010   }
1011   if (needSafepoint > 0) {
1012     VM_ThreadsSuspendJVMTI tsj;
1013     VMThread::execute(&tsj);
1014   }
1015   // per-thread suspend results returned via results parameter
1016   return JVMTI_ERROR_NONE;
1017 } /* end SuspendThreadList */
1018 
1019 
1020 // Threads_lock NOT held, java_thread not protected by lock
1021 // java_thread - pre-checked
1022 jvmtiError
1023 JvmtiEnv::ResumeThread(JavaThread* java_thread) {
1024   // don't allow hidden thread resume request.
1025   if (java_thread->is_hidden_from_external_view()) {
1026     return JVMTI_ERROR_NONE;
1027   }
1028 
1029   if (!java_thread->is_being_ext_suspended()) {
1030     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1031   }
1032 
1033   if (!JvmtiSuspendControl::resume(java_thread)) {
1034     return JVMTI_ERROR_INTERNAL;
1035   }
1036   return JVMTI_ERROR_NONE;
1037 } /* end ResumeThread */
1038 
1039 
1040 // request_count - pre-checked to be greater than or equal to 0
1041 // request_list - pre-checked for NULL
1042 // results - pre-checked for NULL
1043 jvmtiError
1044 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1045   ThreadsListHandle tlh;
1046   for (int i = 0; i < request_count; i++) {
1047     JavaThread* java_thread = NULL;
1048     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), request_list[i], &java_thread, NULL);
1049     if (err != JVMTI_ERROR_NONE) {
1050       results[i] = err;
1051       continue;
1052     }
1053     // don't allow hidden thread resume request.
1054     if (java_thread->is_hidden_from_external_view()) {
1055       results[i] = JVMTI_ERROR_NONE;  // indicate successful resume
1056       continue;
1057     }
1058     if (!java_thread->is_being_ext_suspended()) {
1059       results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1060       continue;
1061     }
1062 
1063     if (!JvmtiSuspendControl::resume(java_thread)) {
1064       results[i] = JVMTI_ERROR_INTERNAL;
1065       continue;
1066     }
1067 
1068     results[i] = JVMTI_ERROR_NONE;  // indicate successful resume
1069   }
1070   // per-thread resume results returned via results parameter
1071   return JVMTI_ERROR_NONE;
1072 } /* end ResumeThreadList */
1073 
1074 
1075 // Threads_lock NOT held, java_thread not protected by lock
1076 // java_thread - pre-checked
1077 jvmtiError
1078 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
1079   oop e = JNIHandles::resolve_external_guard(exception);
1080   NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1081 
1082   JavaThread::send_async_exception(java_thread->threadObj(), e);
1083 
1084   return JVMTI_ERROR_NONE;
1085 
1086 } /* end StopThread */
1087 
1088 
1089 // Threads_lock NOT held
1090 // thread - NOT pre-checked
1091 jvmtiError
1092 JvmtiEnv::InterruptThread(jthread thread) {
1093   // TODO: this is very similar to JVM_Interrupt(); share code in future
1094   JavaThread* current_thread  = JavaThread::current();
1095   JavaThread* java_thread = NULL;
1096   ThreadsListHandle tlh(current_thread);
1097   jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);
1098   if (err != JVMTI_ERROR_NONE) {
1099     return err;
1100   }
1101 
1102   Thread::interrupt(java_thread);
1103 
1104   return JVMTI_ERROR_NONE;
1105 } /* end InterruptThread */
1106 
1107 
1108 // Threads_lock NOT held
1109 // thread - NOT pre-checked
1110 // info_ptr - pre-checked for NULL
1111 jvmtiError
1112 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1113   ResourceMark rm;
1114   HandleMark hm;
1115 
1116   JavaThread* current_thread = JavaThread::current();
1117   ThreadsListHandle tlh(current_thread);
1118 
1119   // if thread is NULL the current thread is used
1120   oop thread_oop = NULL;
1121   if (thread == NULL) {
1122     thread_oop = current_thread->threadObj();
1123     if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
1124       return JVMTI_ERROR_INVALID_THREAD;
1125     }
1126   } else {
1127     JavaThread* java_thread = NULL;
1128     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1129     if (err != JVMTI_ERROR_NONE) {
1130       // We got an error code so we don't have a JavaThread *, but
1131       // only return an error from here if we didn't get a valid
1132       // thread_oop.
1133       if (thread_oop == NULL) {
1134         return err;
1135       }
1136       // We have a valid thread_oop so we can return some thread info.
1137     }
1138   }
1139 
1140   Handle thread_obj(current_thread, thread_oop);
1141   Handle name;
1142   ThreadPriority priority;
1143   Handle     thread_group;
1144   Handle context_class_loader;
1145   bool          is_daemon;
1146 
1147   { MutexLocker mu(Threads_lock);
1148 
1149     name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1150     priority = java_lang_Thread::priority(thread_obj());
1151     thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1152     is_daemon = java_lang_Thread::is_daemon(thread_obj());
1153 
1154     oop loader = java_lang_Thread::context_class_loader(thread_obj());
1155     context_class_loader = Handle(current_thread, loader);
1156   }
1157   { const char *n;
1158 
1159     if (name() != NULL) {
1160       n = java_lang_String::as_utf8_string(name());
1161     } else {
1162       int utf8_length = 0;
1163       n = UNICODE::as_utf8((jchar*) NULL, utf8_length);
1164     }
1165 
1166     info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1167     if (info_ptr->name == NULL)
1168       return JVMTI_ERROR_OUT_OF_MEMORY;
1169 
1170     strcpy(info_ptr->name, n);
1171   }
1172   info_ptr->is_daemon = is_daemon;
1173   info_ptr->priority  = priority;
1174 
1175   info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
1176                                      jni_reference(context_class_loader);
1177   info_ptr->thread_group = jni_reference(thread_group);
1178 
1179   return JVMTI_ERROR_NONE;
1180 } /* end GetThreadInfo */
1181 
1182 
1183 // Threads_lock NOT held, java_thread not protected by lock
1184 // java_thread - pre-checked
1185 // owned_monitor_count_ptr - pre-checked for NULL
1186 // owned_monitors_ptr - pre-checked for NULL
1187 jvmtiError
1188 JvmtiEnv::GetOwnedMonitorInfo(JavaThread* java_thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1189   jvmtiError err = JVMTI_ERROR_NONE;
1190   JavaThread* calling_thread = JavaThread::current();
1191 
1192   // growable array of jvmti monitors info on the C-heap
1193   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1194       new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1195 
1196   // It is only safe to perform the direct operation on the current
1197   // thread. All other usage needs to use a vm-safepoint-op for safety.
1198   if (java_thread == calling_thread) {
1199     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1200   } else {
1201     // JVMTI get monitors info at safepoint. Do not require target thread to
1202     // be suspended.
1203     VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1204     VMThread::execute(&op);
1205     err = op.result();
1206   }
1207   jint owned_monitor_count = owned_monitors_list->length();
1208   if (err == JVMTI_ERROR_NONE) {
1209     if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1210                       (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1211       // copy into the returned array
1212       for (int i = 0; i < owned_monitor_count; i++) {
1213         (*owned_monitors_ptr)[i] =
1214           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1215       }
1216       *owned_monitor_count_ptr = owned_monitor_count;
1217     }
1218   }
1219   // clean up.
1220   for (int i = 0; i < owned_monitor_count; i++) {
1221     deallocate((unsigned char*)owned_monitors_list->at(i));
1222   }
1223   delete owned_monitors_list;
1224 
1225   return err;
1226 } /* end GetOwnedMonitorInfo */
1227 
1228 
1229 // Threads_lock NOT held, java_thread not protected by lock
1230 // java_thread - pre-checked
1231 // monitor_info_count_ptr - pre-checked for NULL
1232 // monitor_info_ptr - pre-checked for NULL
1233 jvmtiError
1234 JvmtiEnv::GetOwnedMonitorStackDepthInfo(JavaThread* java_thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1235   jvmtiError err = JVMTI_ERROR_NONE;
1236   JavaThread* calling_thread  = JavaThread::current();
1237 
1238   // growable array of jvmti monitors info on the C-heap
1239   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1240          new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1241 
1242   // It is only safe to perform the direct operation on the current
1243   // thread. All other usage needs to use a vm-safepoint-op for safety.
1244   if (java_thread == calling_thread) {
1245     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1246   } else {
1247     // JVMTI get owned monitors info at safepoint. Do not require target thread to
1248     // be suspended.
1249     VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1250     VMThread::execute(&op);
1251     err = op.result();
1252   }
1253 
1254   jint owned_monitor_count = owned_monitors_list->length();
1255   if (err == JVMTI_ERROR_NONE) {
1256     if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1257                       (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1258       // copy to output array.
1259       for (int i = 0; i < owned_monitor_count; i++) {
1260         (*monitor_info_ptr)[i].monitor =
1261           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1262         (*monitor_info_ptr)[i].stack_depth =
1263           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1264       }
1265     }
1266     *monitor_info_count_ptr = owned_monitor_count;
1267   }
1268 
1269   // clean up.
1270   for (int i = 0; i < owned_monitor_count; i++) {
1271     deallocate((unsigned char*)owned_monitors_list->at(i));
1272   }
1273   delete owned_monitors_list;
1274 
1275   return err;
1276 } /* end GetOwnedMonitorStackDepthInfo */
1277 
1278 
1279 // Threads_lock NOT held, java_thread not protected by lock
1280 // java_thread - pre-checked
1281 // monitor_ptr - pre-checked for NULL
1282 jvmtiError
1283 JvmtiEnv::GetCurrentContendedMonitor(JavaThread* java_thread, jobject* monitor_ptr) {
1284   jvmtiError err = JVMTI_ERROR_NONE;
1285   JavaThread* calling_thread  = JavaThread::current();
1286 
1287   // It is only safe to perform the direct operation on the current
1288   // thread. All other usage needs to use a vm-safepoint-op for safety.
1289   if (java_thread == calling_thread) {
1290     err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
1291   } else {
1292     // get contended monitor information at safepoint.
1293     VM_GetCurrentContendedMonitor op(this, calling_thread, java_thread, monitor_ptr);
1294     VMThread::execute(&op);
1295     err = op.result();
1296   }
1297   return err;
1298 } /* end GetCurrentContendedMonitor */
1299 
1300 
1301 // Threads_lock NOT held
1302 // thread - NOT pre-checked
1303 // proc - pre-checked for NULL
1304 // arg - NULL is a valid value, must be checked
1305 jvmtiError
1306 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1307   JavaThread* current_thread = JavaThread::current();
1308 
1309   JavaThread* java_thread = NULL;
1310   oop thread_oop = NULL;
1311   ThreadsListHandle tlh(current_thread);
1312   jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1313   if (err != JVMTI_ERROR_NONE) {
1314     // We got an error code so we don't have a JavaThread *, but
1315     // only return an error from here if we didn't get a valid
1316     // thread_oop.
1317     if (thread_oop == NULL) {
1318       return err;
1319     }
1320     // We have a valid thread_oop.
1321   }
1322 
1323   if (java_thread != NULL) {
1324     // 'thread' refers to an existing JavaThread.
1325     return JVMTI_ERROR_INVALID_THREAD;
1326   }
1327 
1328   if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1329     return JVMTI_ERROR_INVALID_PRIORITY;
1330   }
1331 
1332   Handle thread_hndl(current_thread, thread_oop);
1333   {
1334     MutexLocker mu(Threads_lock); // grab Threads_lock
1335 
1336     JvmtiAgentThread *new_thread = new JvmtiAgentThread(this, proc, arg);
1337 
1338     // At this point it may be possible that no osthread was created for the
1339     // JavaThread due to lack of memory.
1340     if (new_thread == NULL || new_thread->osthread() == NULL) {
1341       if (new_thread != NULL) {
1342         new_thread->smr_delete();
1343       }
1344       return JVMTI_ERROR_OUT_OF_MEMORY;
1345     }
1346 
1347     java_lang_Thread::set_thread(thread_hndl(), new_thread);
1348     java_lang_Thread::set_priority(thread_hndl(), (ThreadPriority)priority);
1349     java_lang_Thread::set_daemon(thread_hndl());
1350 
1351     new_thread->set_threadObj(thread_hndl());
1352     Threads::add(new_thread);
1353     Thread::start(new_thread);
1354   } // unlock Threads_lock
1355 
1356   return JVMTI_ERROR_NONE;
1357 } /* end RunAgentThread */
1358 
1359   //
1360   // Thread Group functions
1361   //
1362 
1363 // group_count_ptr - pre-checked for NULL
1364 // groups_ptr - pre-checked for NULL
1365 jvmtiError
1366 JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
1367   JavaThread* current_thread = JavaThread::current();
1368 
1369   // Only one top level thread group now.
1370   *group_count_ptr = 1;
1371 
1372   // Allocate memory to store global-refs to the thread groups.
1373   // Assume this area is freed by caller.
1374   *groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
1375 
1376   NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
1377 
1378   // Convert oop to Handle, then convert Handle to global-ref.
1379   {
1380     HandleMark hm(current_thread);
1381     Handle system_thread_group(current_thread, Universe::system_thread_group());
1382     *groups_ptr[0] = jni_reference(system_thread_group);
1383   }
1384 
1385   return JVMTI_ERROR_NONE;
1386 } /* end GetTopThreadGroups */
1387 
1388 
1389 // info_ptr - pre-checked for NULL
1390 jvmtiError
1391 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1392   ResourceMark rm;
1393   HandleMark hm;
1394 
1395   JavaThread* current_thread = JavaThread::current();
1396 
1397   Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1398   NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1399 
1400   const char* name;
1401   Handle parent_group;
1402   bool is_daemon;
1403   ThreadPriority max_priority;
1404 
1405   { MutexLocker mu(Threads_lock);
1406 
1407     name         = java_lang_ThreadGroup::name(group_obj());
1408     parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
1409     is_daemon    = java_lang_ThreadGroup::is_daemon(group_obj());
1410     max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1411   }
1412 
1413   info_ptr->is_daemon    = is_daemon;
1414   info_ptr->max_priority = max_priority;
1415   info_ptr->parent       = jni_reference(parent_group);
1416 
1417   if (name != NULL) {
1418     info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1419     NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1420     strcpy(info_ptr->name, name);
1421   } else {
1422     info_ptr->name = NULL;
1423   }
1424 
1425   return JVMTI_ERROR_NONE;
1426 } /* end GetThreadGroupInfo */
1427 
1428 
1429 // thread_count_ptr - pre-checked for NULL
1430 // threads_ptr - pre-checked for NULL
1431 // group_count_ptr - pre-checked for NULL
1432 // groups_ptr - pre-checked for NULL
1433 jvmtiError
1434 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1435   JavaThread* current_thread = JavaThread::current();
1436   oop group_obj = (oop) JNIHandles::resolve_external_guard(group);
1437   NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1438 
1439   Handle *thread_objs = NULL;
1440   Handle *group_objs  = NULL;
1441   int nthreads = 0;
1442   int ngroups = 0;
1443   int hidden_threads = 0;
1444 
1445   ResourceMark rm(current_thread);
1446   HandleMark hm(current_thread);
1447 
1448   Handle group_hdl(current_thread, group_obj);
1449 
1450   { // Cannot allow thread or group counts to change.
1451     MutexLocker mu(Threads_lock);
1452 
1453     nthreads = java_lang_ThreadGroup::nthreads(group_hdl());
1454     ngroups  = java_lang_ThreadGroup::ngroups(group_hdl());
1455 
1456     if (nthreads > 0) {
1457       ThreadsListHandle tlh(current_thread);
1458       objArrayOop threads = java_lang_ThreadGroup::threads(group_hdl());
1459       assert(nthreads <= threads->length(), "too many threads");
1460       thread_objs = NEW_RESOURCE_ARRAY(Handle,nthreads);
1461       for (int i=0, j=0; i<nthreads; i++) {
1462         oop thread_obj = threads->obj_at(i);
1463         assert(thread_obj != NULL, "thread_obj is NULL");
1464         JavaThread *java_thread = NULL;
1465         jvmtiError err = JvmtiExport::cv_oop_to_JavaThread(tlh.list(), thread_obj, &java_thread);
1466         if (err == JVMTI_ERROR_NONE) {
1467           // Have a valid JavaThread*.
1468           if (java_thread->is_hidden_from_external_view()) {
1469             // Filter out hidden java threads.
1470             hidden_threads++;
1471             continue;
1472           }
1473         } else {
1474           // We couldn't convert thread_obj into a JavaThread*.
1475           if (err == JVMTI_ERROR_INVALID_THREAD) {
1476             // The thread_obj does not refer to a java.lang.Thread object
1477             // so skip it.
1478             hidden_threads++;
1479             continue;
1480           }
1481           // We have a valid thread_obj, but no JavaThread*; the caller
1482           // can still have limited use for the thread_obj.
1483         }
1484         thread_objs[j++] = Handle(current_thread, thread_obj);
1485       }
1486       nthreads -= hidden_threads;
1487     } // ThreadsListHandle is destroyed here.
1488 
1489     if (ngroups > 0) {
1490       objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
1491       assert(ngroups <= groups->length(), "too many groups");
1492       group_objs = NEW_RESOURCE_ARRAY(Handle,ngroups);
1493       for (int i=0; i<ngroups; i++) {
1494         oop group_obj = groups->obj_at(i);
1495         assert(group_obj != NULL, "group_obj != NULL");
1496         group_objs[i] = Handle(current_thread, group_obj);
1497       }
1498     }
1499   }
1500 
1501   // have to make global handles outside of Threads_lock
1502   *group_count_ptr  = ngroups;
1503   *thread_count_ptr = nthreads;
1504   *threads_ptr     = new_jthreadArray(nthreads, thread_objs);
1505   *groups_ptr      = new_jthreadGroupArray(ngroups, group_objs);
1506   if ((nthreads > 0) && (*threads_ptr == NULL)) {
1507     return JVMTI_ERROR_OUT_OF_MEMORY;
1508   }
1509   if ((ngroups > 0) && (*groups_ptr == NULL)) {
1510     return JVMTI_ERROR_OUT_OF_MEMORY;
1511   }
1512 
1513   return JVMTI_ERROR_NONE;
1514 } /* end GetThreadGroupChildren */
1515 
1516 
1517   //
1518   // Stack Frame functions
1519   //
1520 
1521 // Threads_lock NOT held, java_thread not protected by lock
1522 // java_thread - pre-checked
1523 // max_frame_count - pre-checked to be greater than or equal to 0
1524 // frame_buffer - pre-checked for NULL
1525 // count_ptr - pre-checked for NULL
1526 jvmtiError
1527 JvmtiEnv::GetStackTrace(JavaThread* java_thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1528   jvmtiError err = JVMTI_ERROR_NONE;
1529 
1530   // It is only safe to perform the direct operation on the current
1531   // thread. All other usage needs to use a vm-safepoint-op for safety.
1532   if (java_thread == JavaThread::current()) {
1533     err = get_stack_trace(java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1534   } else {
1535     // JVMTI get stack trace at safepoint. Do not require target thread to
1536     // be suspended.
1537     VM_GetStackTrace op(this, java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1538     VMThread::execute(&op);
1539     err = op.result();
1540   }
1541 
1542   return err;
1543 } /* end GetStackTrace */
1544 
1545 
1546 // max_frame_count - pre-checked to be greater than or equal to 0
1547 // stack_info_ptr - pre-checked for NULL
1548 // thread_count_ptr - pre-checked for NULL
1549 jvmtiError
1550 JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
1551   jvmtiError err = JVMTI_ERROR_NONE;
1552   JavaThread* calling_thread = JavaThread::current();
1553 
1554   // JVMTI get stack traces at safepoint.
1555   VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1556   VMThread::execute(&op);
1557   *thread_count_ptr = op.final_thread_count();
1558   *stack_info_ptr = op.stack_info();
1559   err = op.result();
1560   return err;
1561 } /* end GetAllStackTraces */
1562 
1563 
1564 // thread_count - pre-checked to be greater than or equal to 0
1565 // thread_list - pre-checked for NULL
1566 // max_frame_count - pre-checked to be greater than or equal to 0
1567 // stack_info_ptr - pre-checked for NULL
1568 jvmtiError
1569 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1570   jvmtiError err = JVMTI_ERROR_NONE;
1571   // JVMTI get stack traces at safepoint.
1572   VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1573   VMThread::execute(&op);
1574   err = op.result();
1575   if (err == JVMTI_ERROR_NONE) {
1576     *stack_info_ptr = op.stack_info();
1577   }
1578   return err;
1579 } /* end GetThreadListStackTraces */
1580 
1581 
1582 // Threads_lock NOT held, java_thread not protected by lock
1583 // java_thread - pre-checked
1584 // count_ptr - pre-checked for NULL
1585 jvmtiError
1586 JvmtiEnv::GetFrameCount(JavaThread* java_thread, jint* count_ptr) {
1587   jvmtiError err = JVMTI_ERROR_NONE;
1588 
1589   // retrieve or create JvmtiThreadState.
1590   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1591   if (state == NULL) {
1592     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1593   }
1594 
1595   // It is only safe to perform the direct operation on the current
1596   // thread. All other usage needs to use a vm-safepoint-op for safety.
1597   if (java_thread == JavaThread::current()) {
1598     err = get_frame_count(state, count_ptr);
1599   } else {
1600     // get java stack frame count at safepoint.
1601     VM_GetFrameCount op(this, state, count_ptr);
1602     VMThread::execute(&op);
1603     err = op.result();
1604   }
1605   return err;
1606 } /* end GetFrameCount */
1607 
1608 
1609 // Threads_lock NOT held, java_thread not protected by lock
1610 // java_thread - pre-checked
1611 jvmtiError
1612 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1613   JavaThread* current_thread  = JavaThread::current();
1614   HandleMark hm(current_thread);
1615   uint32_t debug_bits = 0;
1616 
1617   // retrieve or create the state
1618   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1619   if (state == NULL) {
1620     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1621   }
1622 
1623   // Check if java_thread is fully suspended
1624   if (!java_thread->is_thread_fully_suspended(true /* wait for suspend completion */, &debug_bits)) {
1625     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1626   }
1627   // Check to see if a PopFrame was already in progress
1628   if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
1629     // Probably possible for JVMTI clients to trigger this, but the
1630     // JPDA backend shouldn't allow this to happen
1631     return JVMTI_ERROR_INTERNAL;
1632   }
1633 
1634   {
1635     // Was workaround bug
1636     //    4812902: popFrame hangs if the method is waiting at a synchronize
1637     // Catch this condition and return an error to avoid hanging.
1638     // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
1639     OSThread* osThread = java_thread->osthread();
1640     if (osThread->get_state() == MONITOR_WAIT) {
1641       return JVMTI_ERROR_OPAQUE_FRAME;
1642     }
1643   }
1644 
1645   {
1646     ResourceMark rm(current_thread);
1647     // Check if there are more than one Java frame in this thread, that the top two frames
1648     // are Java (not native) frames, and that there is no intervening VM frame
1649     int frame_count = 0;
1650     bool is_interpreted[2];
1651     intptr_t *frame_sp[2];
1652     // The 2-nd arg of constructor is needed to stop iterating at java entry frame.
1653     for (vframeStream vfs(java_thread, true); !vfs.at_end(); vfs.next()) {
1654       methodHandle mh(current_thread, vfs.method());
1655       if (mh->is_native()) return(JVMTI_ERROR_OPAQUE_FRAME);
1656       is_interpreted[frame_count] = vfs.is_interpreted_frame();
1657       frame_sp[frame_count] = vfs.frame_id();
1658       if (++frame_count > 1) break;
1659     }
1660     if (frame_count < 2)  {
1661       // We haven't found two adjacent non-native Java frames on the top.
1662       // There can be two situations here:
1663       //  1. There are no more java frames
1664       //  2. Two top java frames are separated by non-java native frames
1665       if(vframeFor(java_thread, 1) == NULL) {
1666         return JVMTI_ERROR_NO_MORE_FRAMES;
1667       } else {
1668         // Intervening non-java native or VM frames separate java frames.
1669         // Current implementation does not support this. See bug #5031735.
1670         // In theory it is possible to pop frames in such cases.
1671         return JVMTI_ERROR_OPAQUE_FRAME;
1672       }
1673     }
1674 
1675     // If any of the top 2 frames is a compiled one, need to deoptimize it
1676     for (int i = 0; i < 2; i++) {
1677       if (!is_interpreted[i]) {
1678         Deoptimization::deoptimize_frame(java_thread, frame_sp[i]);
1679       }
1680     }
1681 
1682     // Update the thread state to reflect that the top frame is popped
1683     // so that cur_stack_depth is maintained properly and all frameIDs
1684     // are invalidated.
1685     // The current frame will be popped later when the suspended thread
1686     // is resumed and right before returning from VM to Java.
1687     // (see call_VM_base() in assembler_<cpu>.cpp).
1688 
1689     // It's fine to update the thread state here because no JVMTI events
1690     // shall be posted for this PopFrame.
1691 
1692     // It is only safe to perform the direct operation on the current
1693     // thread. All other usage needs to use a vm-safepoint-op for safety.
1694     if (java_thread == JavaThread::current()) {
1695       state->update_for_pop_top_frame();
1696     } else {
1697       VM_UpdateForPopTopFrame op(state);
1698       VMThread::execute(&op);
1699       jvmtiError err = op.result();
1700       if (err != JVMTI_ERROR_NONE) {
1701         return err;
1702       }
1703     }
1704 
1705     java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
1706     // Set pending step flag for this popframe and it is cleared when next
1707     // step event is posted.
1708     state->set_pending_step_for_popframe();
1709   }
1710 
1711   return JVMTI_ERROR_NONE;
1712 } /* end PopFrame */
1713 
1714 
1715 // Threads_lock NOT held, java_thread not protected by lock
1716 // java_thread - pre-checked
1717 // java_thread - unchecked
1718 // depth - pre-checked as non-negative
1719 // method_ptr - pre-checked for NULL
1720 // location_ptr - pre-checked for NULL
1721 jvmtiError
1722 JvmtiEnv::GetFrameLocation(JavaThread* java_thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1723   jvmtiError err = JVMTI_ERROR_NONE;
1724 
1725   // It is only safe to perform the direct operation on the current
1726   // thread. All other usage needs to use a vm-safepoint-op for safety.
1727   if (java_thread == JavaThread::current()) {
1728     err = get_frame_location(java_thread, depth, method_ptr, location_ptr);
1729   } else {
1730     // JVMTI get java stack frame location at safepoint.
1731     VM_GetFrameLocation op(this, java_thread, depth, method_ptr, location_ptr);
1732     VMThread::execute(&op);
1733     err = op.result();
1734   }
1735   return err;
1736 } /* end GetFrameLocation */
1737 
1738 
1739 // Threads_lock NOT held, java_thread not protected by lock
1740 // java_thread - pre-checked
1741 // java_thread - unchecked
1742 // depth - pre-checked as non-negative
1743 jvmtiError
1744 JvmtiEnv::NotifyFramePop(JavaThread* java_thread, jint depth) {
1745   jvmtiError err = JVMTI_ERROR_NONE;
1746   ResourceMark rm;
1747   uint32_t debug_bits = 0;
1748 
1749   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
1750   if (state == NULL) {
1751     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1752   }
1753 
1754   if (!java_thread->is_thread_fully_suspended(true, &debug_bits)) {
1755     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1756   }
1757 
1758   if (TraceJVMTICalls) {
1759     JvmtiSuspendControl::print();
1760   }
1761 
1762   vframe *vf = vframeFor(java_thread, depth);
1763   if (vf == NULL) {
1764     return JVMTI_ERROR_NO_MORE_FRAMES;
1765   }
1766 
1767   if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
1768     return JVMTI_ERROR_OPAQUE_FRAME;
1769   }
1770 
1771   assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
1772 
1773   // It is only safe to perform the direct operation on the current
1774   // thread. All other usage needs to use a vm-safepoint-op for safety.
1775   if (java_thread == JavaThread::current()) {
1776     int frame_number = state->count_frames() - depth;
1777     state->env_thread_state(this)->set_frame_pop(frame_number);
1778   } else {
1779     VM_SetFramePop op(this, state, depth);
1780     VMThread::execute(&op);
1781     err = op.result();
1782   }
1783   return err;
1784 } /* end NotifyFramePop */
1785 
1786 
1787   //
1788   // Force Early Return functions
1789   //
1790 
1791 // Threads_lock NOT held, java_thread not protected by lock
1792 // java_thread - pre-checked
1793 jvmtiError
1794 JvmtiEnv::ForceEarlyReturnObject(JavaThread* java_thread, jobject value) {
1795   jvalue val;
1796   val.l = value;
1797   return force_early_return(java_thread, val, atos);
1798 } /* end ForceEarlyReturnObject */
1799 
1800 
1801 // Threads_lock NOT held, java_thread not protected by lock
1802 // java_thread - pre-checked
1803 jvmtiError
1804 JvmtiEnv::ForceEarlyReturnInt(JavaThread* java_thread, jint value) {
1805   jvalue val;
1806   val.i = value;
1807   return force_early_return(java_thread, val, itos);
1808 } /* end ForceEarlyReturnInt */
1809 
1810 
1811 // Threads_lock NOT held, java_thread not protected by lock
1812 // java_thread - pre-checked
1813 jvmtiError
1814 JvmtiEnv::ForceEarlyReturnLong(JavaThread* java_thread, jlong value) {
1815   jvalue val;
1816   val.j = value;
1817   return force_early_return(java_thread, val, ltos);
1818 } /* end ForceEarlyReturnLong */
1819 
1820 
1821 // Threads_lock NOT held, java_thread not protected by lock
1822 // java_thread - pre-checked
1823 jvmtiError
1824 JvmtiEnv::ForceEarlyReturnFloat(JavaThread* java_thread, jfloat value) {
1825   jvalue val;
1826   val.f = value;
1827   return force_early_return(java_thread, val, ftos);
1828 } /* end ForceEarlyReturnFloat */
1829 
1830 
1831 // Threads_lock NOT held, java_thread not protected by lock
1832 // java_thread - pre-checked
1833 jvmtiError
1834 JvmtiEnv::ForceEarlyReturnDouble(JavaThread* java_thread, jdouble value) {
1835   jvalue val;
1836   val.d = value;
1837   return force_early_return(java_thread, val, dtos);
1838 } /* end ForceEarlyReturnDouble */
1839 
1840 
1841 // Threads_lock NOT held, java_thread not protected by lock
1842 // java_thread - pre-checked
1843 jvmtiError
1844 JvmtiEnv::ForceEarlyReturnVoid(JavaThread* java_thread) {
1845   jvalue val;
1846   val.j = 0L;
1847   return force_early_return(java_thread, val, vtos);
1848 } /* end ForceEarlyReturnVoid */
1849 
1850 
1851   //
1852   // Heap functions
1853   //
1854 
1855 // klass - NULL is a valid value, must be checked
1856 // initial_object - NULL is a valid value, must be checked
1857 // callbacks - pre-checked for NULL
1858 // user_data - NULL is a valid value, must be checked
1859 jvmtiError
1860 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1861   // check klass if provided
1862   Klass* k = NULL;
1863   if (klass != NULL) {
1864     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1865     if (k_mirror == NULL) {
1866       return JVMTI_ERROR_INVALID_CLASS;
1867     }
1868     if (java_lang_Class::is_primitive(k_mirror)) {
1869       return JVMTI_ERROR_NONE;
1870     }
1871     k = java_lang_Class::as_Klass(k_mirror);
1872     if (klass == NULL) {
1873       return JVMTI_ERROR_INVALID_CLASS;
1874     }
1875   }
1876 
1877   if (initial_object != NULL) {
1878     oop init_obj = JNIHandles::resolve_external_guard(initial_object);
1879     if (init_obj == NULL) {
1880       return JVMTI_ERROR_INVALID_OBJECT;
1881     }
1882   }
1883 
1884   Thread *thread = Thread::current();
1885   HandleMark hm(thread);
1886 
1887   TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
1888   JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, k, initial_object, callbacks, user_data);
1889   return JVMTI_ERROR_NONE;
1890 } /* end FollowReferences */
1891 
1892 
1893 // klass - NULL is a valid value, must be checked
1894 // callbacks - pre-checked for NULL
1895 // user_data - NULL is a valid value, must be checked
1896 jvmtiError
1897 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1898   // check klass if provided
1899   Klass* k = NULL;
1900   if (klass != NULL) {
1901     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1902     if (k_mirror == NULL) {
1903       return JVMTI_ERROR_INVALID_CLASS;
1904     }
1905     if (java_lang_Class::is_primitive(k_mirror)) {
1906       return JVMTI_ERROR_NONE;
1907     }
1908     k = java_lang_Class::as_Klass(k_mirror);
1909     if (k == NULL) {
1910       return JVMTI_ERROR_INVALID_CLASS;
1911     }
1912   }
1913 
1914   TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1915   JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, k, callbacks, user_data);
1916   return JVMTI_ERROR_NONE;
1917 } /* end IterateThroughHeap */
1918 
1919 
1920 // tag_ptr - pre-checked for NULL
1921 jvmtiError
1922 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1923   oop o = JNIHandles::resolve_external_guard(object);
1924   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1925   *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1926   return JVMTI_ERROR_NONE;
1927 } /* end GetTag */
1928 
1929 
1930 jvmtiError
1931 JvmtiEnv::SetTag(jobject object, jlong tag) {
1932   oop o = JNIHandles::resolve_external_guard(object);
1933   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1934   JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1935   return JVMTI_ERROR_NONE;
1936 } /* end SetTag */
1937 
1938 
1939 // tag_count - pre-checked to be greater than or equal to 0
1940 // tags - pre-checked for NULL
1941 // count_ptr - pre-checked for NULL
1942 // object_result_ptr - NULL is a valid value, must be checked
1943 // tag_result_ptr - NULL is a valid value, must be checked
1944 jvmtiError
1945 JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
1946   TraceTime t("GetObjectsWithTags", TRACETIME_LOG(Debug, jvmti, objecttagging));
1947   return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
1948 } /* end GetObjectsWithTags */
1949 
1950 
1951 jvmtiError
1952 JvmtiEnv::ForceGarbageCollection() {
1953   Universe::heap()->collect(GCCause::_jvmti_force_gc);
1954   return JVMTI_ERROR_NONE;
1955 } /* end ForceGarbageCollection */
1956 
1957 
1958   //
1959   // Heap (1.0) functions
1960   //
1961 
1962 // object_reference_callback - pre-checked for NULL
1963 // user_data - NULL is a valid value, must be checked
1964 jvmtiError
1965 JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
1966   oop o = JNIHandles::resolve_external_guard(object);
1967   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1968   JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
1969   return JVMTI_ERROR_NONE;
1970 } /* end IterateOverObjectsReachableFromObject */
1971 
1972 
1973 // heap_root_callback - NULL is a valid value, must be checked
1974 // stack_ref_callback - NULL is a valid value, must be checked
1975 // object_ref_callback - NULL is a valid value, must be checked
1976 // user_data - NULL is a valid value, must be checked
1977 jvmtiError
1978 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
1979   TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
1980   JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
1981   return JVMTI_ERROR_NONE;
1982 } /* end IterateOverReachableObjects */
1983 
1984 
1985 // heap_object_callback - pre-checked for NULL
1986 // user_data - NULL is a valid value, must be checked
1987 jvmtiError
1988 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1989   TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1990   Thread *thread = Thread::current();
1991   HandleMark hm(thread);
1992   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, NULL, heap_object_callback, user_data);
1993   return JVMTI_ERROR_NONE;
1994 } /* end IterateOverHeap */
1995 
1996 
1997 // k_mirror - may be primitive, this must be checked
1998 // heap_object_callback - pre-checked for NULL
1999 // user_data - NULL is a valid value, must be checked
2000 jvmtiError
2001 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2002   if (java_lang_Class::is_primitive(k_mirror)) {
2003     // DO PRIMITIVE CLASS PROCESSING
2004     return JVMTI_ERROR_NONE;
2005   }
2006   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2007   if (klass == NULL) {
2008     return JVMTI_ERROR_INVALID_CLASS;
2009   }
2010   TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
2011   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
2012   return JVMTI_ERROR_NONE;
2013 } /* end IterateOverInstancesOfClass */
2014 
2015 
2016   //
2017   // Local Variable functions
2018   //
2019 
2020 // Threads_lock NOT held, java_thread not protected by lock
2021 // java_thread - pre-checked
2022 // java_thread - unchecked
2023 // depth - pre-checked as non-negative
2024 // value_ptr - pre-checked for NULL
2025 jvmtiError
2026 JvmtiEnv::GetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject* value_ptr) {
2027   JavaThread* current_thread = JavaThread::current();
2028   // rm object is created to clean up the javaVFrame created in
2029   // doit_prologue(), but after doit() is finished with it.
2030   ResourceMark rm(current_thread);
2031 
2032   VM_GetOrSetLocal op(java_thread, current_thread, depth, slot);
2033   VMThread::execute(&op);
2034   jvmtiError err = op.result();
2035   if (err != JVMTI_ERROR_NONE) {
2036     return err;
2037   } else {
2038     *value_ptr = op.value().l;
2039     return JVMTI_ERROR_NONE;
2040   }
2041 } /* end GetLocalObject */
2042 
2043 // Threads_lock NOT held, java_thread not protected by lock
2044 // java_thread - pre-checked
2045 // java_thread - unchecked
2046 // depth - pre-checked as non-negative
2047 // value - pre-checked for NULL
2048 jvmtiError
2049 JvmtiEnv::GetLocalInstance(JavaThread* java_thread, jint depth, jobject* value_ptr){
2050   JavaThread* current_thread = JavaThread::current();
2051   // rm object is created to clean up the javaVFrame created in
2052   // doit_prologue(), but after doit() is finished with it.
2053   ResourceMark rm(current_thread);
2054 
2055   VM_GetReceiver op(java_thread, current_thread, depth);
2056   VMThread::execute(&op);
2057   jvmtiError err = op.result();
2058   if (err != JVMTI_ERROR_NONE) {
2059     return err;
2060   } else {
2061     *value_ptr = op.value().l;
2062     return JVMTI_ERROR_NONE;
2063   }
2064 } /* end GetLocalInstance */
2065 
2066 
2067 // Threads_lock NOT held, java_thread not protected by lock
2068 // java_thread - pre-checked
2069 // java_thread - unchecked
2070 // depth - pre-checked as non-negative
2071 // value_ptr - pre-checked for NULL
2072 jvmtiError
2073 JvmtiEnv::GetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint* value_ptr) {
2074   // rm object is created to clean up the javaVFrame created in
2075   // doit_prologue(), but after doit() is finished with it.
2076   ResourceMark rm;
2077 
2078   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT);
2079   VMThread::execute(&op);
2080   *value_ptr = op.value().i;
2081   return op.result();
2082 } /* end GetLocalInt */
2083 
2084 
2085 // Threads_lock NOT held, java_thread not protected by lock
2086 // java_thread - pre-checked
2087 // java_thread - unchecked
2088 // depth - pre-checked as non-negative
2089 // value_ptr - pre-checked for NULL
2090 jvmtiError
2091 JvmtiEnv::GetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong* value_ptr) {
2092   // rm object is created to clean up the javaVFrame created in
2093   // doit_prologue(), but after doit() is finished with it.
2094   ResourceMark rm;
2095 
2096   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG);
2097   VMThread::execute(&op);
2098   *value_ptr = op.value().j;
2099   return op.result();
2100 } /* end GetLocalLong */
2101 
2102 
2103 // Threads_lock NOT held, java_thread not protected by lock
2104 // java_thread - pre-checked
2105 // java_thread - unchecked
2106 // depth - pre-checked as non-negative
2107 // value_ptr - pre-checked for NULL
2108 jvmtiError
2109 JvmtiEnv::GetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat* value_ptr) {
2110   // rm object is created to clean up the javaVFrame created in
2111   // doit_prologue(), but after doit() is finished with it.
2112   ResourceMark rm;
2113 
2114   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT);
2115   VMThread::execute(&op);
2116   *value_ptr = op.value().f;
2117   return op.result();
2118 } /* end GetLocalFloat */
2119 
2120 
2121 // Threads_lock NOT held, java_thread not protected by lock
2122 // java_thread - pre-checked
2123 // java_thread - unchecked
2124 // depth - pre-checked as non-negative
2125 // value_ptr - pre-checked for NULL
2126 jvmtiError
2127 JvmtiEnv::GetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble* value_ptr) {
2128   // rm object is created to clean up the javaVFrame created in
2129   // doit_prologue(), but after doit() is finished with it.
2130   ResourceMark rm;
2131 
2132   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE);
2133   VMThread::execute(&op);
2134   *value_ptr = op.value().d;
2135   return op.result();
2136 } /* end GetLocalDouble */
2137 
2138 
2139 // Threads_lock NOT held, java_thread not protected by lock
2140 // java_thread - pre-checked
2141 // java_thread - unchecked
2142 // depth - pre-checked as non-negative
2143 jvmtiError
2144 JvmtiEnv::SetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject value) {
2145   // rm object is created to clean up the javaVFrame created in
2146   // doit_prologue(), but after doit() is finished with it.
2147   ResourceMark rm;
2148   jvalue val;
2149   val.l = value;
2150   VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val);
2151   VMThread::execute(&op);
2152   return op.result();
2153 } /* end SetLocalObject */
2154 
2155 
2156 // Threads_lock NOT held, java_thread not protected by lock
2157 // java_thread - pre-checked
2158 // java_thread - unchecked
2159 // depth - pre-checked as non-negative
2160 jvmtiError
2161 JvmtiEnv::SetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint value) {
2162   // rm object is created to clean up the javaVFrame created in
2163   // doit_prologue(), but after doit() is finished with it.
2164   ResourceMark rm;
2165   jvalue val;
2166   val.i = value;
2167   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val);
2168   VMThread::execute(&op);
2169   return op.result();
2170 } /* end SetLocalInt */
2171 
2172 
2173 // Threads_lock NOT held, java_thread not protected by lock
2174 // java_thread - pre-checked
2175 // java_thread - unchecked
2176 // depth - pre-checked as non-negative
2177 jvmtiError
2178 JvmtiEnv::SetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong value) {
2179   // rm object is created to clean up the javaVFrame created in
2180   // doit_prologue(), but after doit() is finished with it.
2181   ResourceMark rm;
2182   jvalue val;
2183   val.j = value;
2184   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val);
2185   VMThread::execute(&op);
2186   return op.result();
2187 } /* end SetLocalLong */
2188 
2189 
2190 // Threads_lock NOT held, java_thread not protected by lock
2191 // java_thread - pre-checked
2192 // java_thread - unchecked
2193 // depth - pre-checked as non-negative
2194 jvmtiError
2195 JvmtiEnv::SetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat value) {
2196   // rm object is created to clean up the javaVFrame created in
2197   // doit_prologue(), but after doit() is finished with it.
2198   ResourceMark rm;
2199   jvalue val;
2200   val.f = value;
2201   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val);
2202   VMThread::execute(&op);
2203   return op.result();
2204 } /* end SetLocalFloat */
2205 
2206 
2207 // Threads_lock NOT held, java_thread not protected by lock
2208 // java_thread - pre-checked
2209 // java_thread - unchecked
2210 // depth - pre-checked as non-negative
2211 jvmtiError
2212 JvmtiEnv::SetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble value) {
2213   // rm object is created to clean up the javaVFrame created in
2214   // doit_prologue(), but after doit() is finished with it.
2215   ResourceMark rm;
2216   jvalue val;
2217   val.d = value;
2218   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val);
2219   VMThread::execute(&op);
2220   return op.result();
2221 } /* end SetLocalDouble */
2222 
2223 
2224   //
2225   // Breakpoint functions
2226   //
2227 
2228 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2229 jvmtiError
2230 JvmtiEnv::SetBreakpoint(Method* method_oop, jlocation location) {
2231   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2232   if (location < 0) {   // simple invalid location check first
2233     return JVMTI_ERROR_INVALID_LOCATION;
2234   }
2235   // verify that the breakpoint is not past the end of the method
2236   if (location >= (jlocation) method_oop->code_size()) {
2237     return JVMTI_ERROR_INVALID_LOCATION;
2238   }
2239 
2240   ResourceMark rm;
2241   JvmtiBreakpoint bp(method_oop, location);
2242   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2243   if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
2244     return JVMTI_ERROR_DUPLICATE;
2245 
2246   if (TraceJVMTICalls) {
2247     jvmti_breakpoints.print();
2248   }
2249 
2250   return JVMTI_ERROR_NONE;
2251 } /* end SetBreakpoint */
2252 
2253 
2254 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2255 jvmtiError
2256 JvmtiEnv::ClearBreakpoint(Method* method_oop, jlocation location) {
2257   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2258 
2259   if (location < 0) {   // simple invalid location check first
2260     return JVMTI_ERROR_INVALID_LOCATION;
2261   }
2262 
2263   // verify that the breakpoint is not past the end of the method
2264   if (location >= (jlocation) method_oop->code_size()) {
2265     return JVMTI_ERROR_INVALID_LOCATION;
2266   }
2267 
2268   JvmtiBreakpoint bp(method_oop, location);
2269 
2270   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2271   if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
2272     return JVMTI_ERROR_NOT_FOUND;
2273 
2274   if (TraceJVMTICalls) {
2275     jvmti_breakpoints.print();
2276   }
2277 
2278   return JVMTI_ERROR_NONE;
2279 } /* end ClearBreakpoint */
2280 
2281 
2282   //
2283   // Watched Field functions
2284   //
2285 
2286 jvmtiError
2287 JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2288   // make sure we haven't set this watch before
2289   if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
2290   fdesc_ptr->set_is_field_access_watched(true);
2291 
2292   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
2293 
2294   return JVMTI_ERROR_NONE;
2295 } /* end SetFieldAccessWatch */
2296 
2297 
2298 jvmtiError
2299 JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2300   // make sure we have a watch to clear
2301   if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
2302   fdesc_ptr->set_is_field_access_watched(false);
2303 
2304   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
2305 
2306   return JVMTI_ERROR_NONE;
2307 } /* end ClearFieldAccessWatch */
2308 
2309 
2310 jvmtiError
2311 JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2312   // make sure we haven't set this watch before
2313   if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
2314   fdesc_ptr->set_is_field_modification_watched(true);
2315 
2316   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
2317 
2318   return JVMTI_ERROR_NONE;
2319 } /* end SetFieldModificationWatch */
2320 
2321 
2322 jvmtiError
2323 JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2324    // make sure we have a watch to clear
2325   if (!fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_NOT_FOUND;
2326   fdesc_ptr->set_is_field_modification_watched(false);
2327 
2328   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, false);
2329 
2330   return JVMTI_ERROR_NONE;
2331 } /* end ClearFieldModificationWatch */
2332 
2333   //
2334   // Class functions
2335   //
2336 
2337 
2338 // k_mirror - may be primitive, this must be checked
2339 // signature_ptr - NULL is a valid value, must be checked
2340 // generic_ptr - NULL is a valid value, must be checked
2341 jvmtiError
2342 JvmtiEnv::GetClassSignature(oop k_mirror, char** signature_ptr, char** generic_ptr) {
2343   ResourceMark rm;
2344   bool isPrimitive = java_lang_Class::is_primitive(k_mirror);
2345   Klass* k = NULL;
2346   if (!isPrimitive) {
2347     k = java_lang_Class::as_Klass(k_mirror);
2348     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2349   }
2350   if (signature_ptr != NULL) {
2351     char* result = NULL;
2352     if (isPrimitive) {
2353       char tchar = type2char(java_lang_Class::primitive_type(k_mirror));
2354       result = (char*) jvmtiMalloc(2);
2355       result[0] = tchar;
2356       result[1] = '\0';
2357     } else {
2358       const char* class_sig = k->signature_name();
2359       result = (char *) jvmtiMalloc(strlen(class_sig)+1);
2360       strcpy(result, class_sig);
2361     }
2362     *signature_ptr = result;
2363   }
2364   if (generic_ptr != NULL) {
2365     *generic_ptr = NULL;
2366     if (!isPrimitive && k->is_instance_klass()) {
2367       Symbol* soo = InstanceKlass::cast(k)->generic_signature();
2368       if (soo != NULL) {
2369         const char *gen_sig = soo->as_C_string();
2370         if (gen_sig != NULL) {
2371           char* gen_result;
2372           jvmtiError err = allocate(strlen(gen_sig) + 1,
2373                                     (unsigned char **)&gen_result);
2374           if (err != JVMTI_ERROR_NONE) {
2375             return err;
2376           }
2377           strcpy(gen_result, gen_sig);
2378           *generic_ptr = gen_result;
2379         }
2380       }
2381     }
2382   }
2383   return JVMTI_ERROR_NONE;
2384 } /* end GetClassSignature */
2385 
2386 
2387 // k_mirror - may be primitive, this must be checked
2388 // status_ptr - pre-checked for NULL
2389 jvmtiError
2390 JvmtiEnv::GetClassStatus(oop k_mirror, jint* status_ptr) {
2391   jint result = 0;
2392   if (java_lang_Class::is_primitive(k_mirror)) {
2393     result |= JVMTI_CLASS_STATUS_PRIMITIVE;
2394   } else {
2395     Klass* k = java_lang_Class::as_Klass(k_mirror);
2396     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2397     result = k->jvmti_class_status();
2398   }
2399   *status_ptr = result;
2400 
2401   return JVMTI_ERROR_NONE;
2402 } /* end GetClassStatus */
2403 
2404 
2405 // k_mirror - may be primitive, this must be checked
2406 // source_name_ptr - pre-checked for NULL
2407 jvmtiError
2408 JvmtiEnv::GetSourceFileName(oop k_mirror, char** source_name_ptr) {
2409   if (java_lang_Class::is_primitive(k_mirror)) {
2410      return JVMTI_ERROR_ABSENT_INFORMATION;
2411   }
2412   Klass* k_klass = java_lang_Class::as_Klass(k_mirror);
2413   NULL_CHECK(k_klass, JVMTI_ERROR_INVALID_CLASS);
2414 
2415   if (!k_klass->is_instance_klass()) {
2416     return JVMTI_ERROR_ABSENT_INFORMATION;
2417   }
2418 
2419   Symbol* sfnOop = InstanceKlass::cast(k_klass)->source_file_name();
2420   NULL_CHECK(sfnOop, JVMTI_ERROR_ABSENT_INFORMATION);
2421   {
2422     JavaThread* current_thread  = JavaThread::current();
2423     ResourceMark rm(current_thread);
2424     const char* sfncp = (const char*) sfnOop->as_C_string();
2425     *source_name_ptr = (char *) jvmtiMalloc(strlen(sfncp)+1);
2426     strcpy(*source_name_ptr, sfncp);
2427   }
2428 
2429   return JVMTI_ERROR_NONE;
2430 } /* end GetSourceFileName */
2431 
2432 
2433 // k_mirror - may be primitive, this must be checked
2434 // modifiers_ptr - pre-checked for NULL
2435 jvmtiError
2436 JvmtiEnv::GetClassModifiers(oop k_mirror, jint* modifiers_ptr) {
2437   JavaThread* current_thread  = JavaThread::current();
2438   jint result = 0;
2439   if (!java_lang_Class::is_primitive(k_mirror)) {
2440     Klass* k = java_lang_Class::as_Klass(k_mirror);
2441     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2442     result = k->compute_modifier_flags(current_thread);
2443     JavaThread* THREAD = current_thread; // pass to macros
2444     if (HAS_PENDING_EXCEPTION) {
2445       CLEAR_PENDING_EXCEPTION;
2446       return JVMTI_ERROR_INTERNAL;
2447     };
2448 
2449     // Reset the deleted  ACC_SUPER bit ( deleted in compute_modifier_flags()).
2450     if(k->is_super()) {
2451       result |= JVM_ACC_SUPER;
2452     }
2453   } else {
2454     result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
2455   }
2456   *modifiers_ptr = result;
2457 
2458   return JVMTI_ERROR_NONE;
2459 } /* end GetClassModifiers */
2460 
2461 
2462 // k_mirror - may be primitive, this must be checked
2463 // method_count_ptr - pre-checked for NULL
2464 // methods_ptr - pre-checked for NULL
2465 jvmtiError
2466 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
2467   JavaThread* current_thread  = JavaThread::current();
2468   HandleMark hm(current_thread);
2469 
2470   if (java_lang_Class::is_primitive(k_mirror)) {
2471     *method_count_ptr = 0;
2472     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2473     return JVMTI_ERROR_NONE;
2474   }
2475   Klass* k = java_lang_Class::as_Klass(k_mirror);
2476   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2477 
2478   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2479   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2480     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2481   }
2482 
2483   if (!k->is_instance_klass()) {
2484     *method_count_ptr = 0;
2485     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2486     return JVMTI_ERROR_NONE;
2487   }
2488   InstanceKlass* ik = InstanceKlass::cast(k);
2489   // Allocate the result and fill it in
2490   int result_length = ik->methods()->length();
2491   jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2492   int index;
2493   bool jmethodids_found = true;
2494 
2495   if (JvmtiExport::can_maintain_original_method_order()) {
2496     // Use the original method ordering indices stored in the class, so we can emit
2497     // jmethodIDs in the order they appeared in the class file
2498     for (index = 0; index < result_length; index++) {
2499       Method* m = ik->methods()->at(index);
2500       int original_index = ik->method_ordering()->at(index);
2501       assert(original_index >= 0 && original_index < result_length, "invalid original method index");
2502       jmethodID id;
2503       if (jmethodids_found) {
2504         id = m->find_jmethod_id_or_null();
2505         if (id == NULL) {
2506           // If we find an uninitialized value, make sure there is
2507           // enough space for all the uninitialized values we might
2508           // find.
2509           ik->ensure_space_for_methodids(index);
2510           jmethodids_found = false;
2511           id = m->jmethod_id();
2512         }
2513       } else {
2514         id = m->jmethod_id();
2515       }
2516       result_list[original_index] = id;
2517     }
2518   } else {
2519     // otherwise just copy in any order
2520     for (index = 0; index < result_length; index++) {
2521       Method* m = ik->methods()->at(index);
2522       jmethodID id;
2523       if (jmethodids_found) {
2524         id = m->find_jmethod_id_or_null();
2525         if (id == NULL) {
2526           // If we find an uninitialized value, make sure there is
2527           // enough space for all the uninitialized values we might
2528           // find.
2529           ik->ensure_space_for_methodids(index);
2530           jmethodids_found = false;
2531           id = m->jmethod_id();
2532         }
2533       } else {
2534         id = m->jmethod_id();
2535       }
2536       result_list[index] = id;
2537     }
2538   }
2539   // Fill in return value.
2540   *method_count_ptr = result_length;
2541   *methods_ptr = result_list;
2542 
2543   return JVMTI_ERROR_NONE;
2544 } /* end GetClassMethods */
2545 
2546 
2547 // k_mirror - may be primitive, this must be checked
2548 // field_count_ptr - pre-checked for NULL
2549 // fields_ptr - pre-checked for NULL
2550 jvmtiError
2551 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
2552   if (java_lang_Class::is_primitive(k_mirror)) {
2553     *field_count_ptr = 0;
2554     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2555     return JVMTI_ERROR_NONE;
2556   }
2557   JavaThread* current_thread = JavaThread::current();
2558   HandleMark hm(current_thread);
2559   Klass* k = java_lang_Class::as_Klass(k_mirror);
2560   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2561 
2562   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2563   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2564     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2565   }
2566 
2567   if (!k->is_instance_klass()) {
2568     *field_count_ptr = 0;
2569     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2570     return JVMTI_ERROR_NONE;
2571   }
2572 
2573 
2574   InstanceKlass* ik = InstanceKlass::cast(k);
2575 
2576   int result_count = 0;
2577   // First, count the fields.
2578   FilteredFieldStream flds(ik, true, true);
2579   result_count = flds.field_count();
2580 
2581   // Allocate the result and fill it in
2582   jfieldID* result_list = (jfieldID*) jvmtiMalloc(result_count * sizeof(jfieldID));
2583   // The JVMTI spec requires fields in the order they occur in the class file,
2584   // this is the reverse order of what FieldStream hands out.
2585   int id_index = (result_count - 1);
2586 
2587   for (FilteredFieldStream src_st(ik, true, true); !src_st.eos(); src_st.next()) {
2588     result_list[id_index--] = jfieldIDWorkaround::to_jfieldID(
2589                                             ik, src_st.offset(),
2590                                             src_st.access_flags().is_static());
2591   }
2592   assert(id_index == -1, "just checking");
2593   // Fill in the results
2594   *field_count_ptr = result_count;
2595   *fields_ptr = result_list;
2596 
2597   return JVMTI_ERROR_NONE;
2598 } /* end GetClassFields */
2599 
2600 
2601 // k_mirror - may be primitive, this must be checked
2602 // interface_count_ptr - pre-checked for NULL
2603 // interfaces_ptr - pre-checked for NULL
2604 jvmtiError
2605 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2606   {
2607     if (java_lang_Class::is_primitive(k_mirror)) {
2608       *interface_count_ptr = 0;
2609       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2610       return JVMTI_ERROR_NONE;
2611     }
2612     JavaThread* current_thread = JavaThread::current();
2613     HandleMark hm(current_thread);
2614     Klass* k = java_lang_Class::as_Klass(k_mirror);
2615     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2616 
2617     // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2618     if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2619       return JVMTI_ERROR_CLASS_NOT_PREPARED;
2620 
2621     if (!k->is_instance_klass()) {
2622       *interface_count_ptr = 0;
2623       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2624       return JVMTI_ERROR_NONE;
2625     }
2626 
2627     Array<InstanceKlass*>* interface_list = InstanceKlass::cast(k)->local_interfaces();
2628     const int result_length = (interface_list == NULL ? 0 : interface_list->length());
2629     jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2630     for (int i_index = 0; i_index < result_length; i_index += 1) {
2631       InstanceKlass* klass_at = interface_list->at(i_index);
2632       assert(klass_at->is_klass(), "interfaces must be Klass*s");
2633       assert(klass_at->is_interface(), "interfaces must be interfaces");
2634       oop mirror_at = klass_at->java_mirror();
2635       Handle handle_at = Handle(current_thread, mirror_at);
2636       result_list[i_index] = (jclass) jni_reference(handle_at);
2637     }
2638     *interface_count_ptr = result_length;
2639     *interfaces_ptr = result_list;
2640   }
2641 
2642   return JVMTI_ERROR_NONE;
2643 } /* end GetImplementedInterfaces */
2644 
2645 
2646 // k_mirror - may be primitive, this must be checked
2647 // minor_version_ptr - pre-checked for NULL
2648 // major_version_ptr - pre-checked for NULL
2649 jvmtiError
2650 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2651   if (java_lang_Class::is_primitive(k_mirror)) {
2652     return JVMTI_ERROR_ABSENT_INFORMATION;
2653   }
2654   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2655 
2656   jint status = klass->jvmti_class_status();
2657   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2658     return JVMTI_ERROR_INVALID_CLASS;
2659   }
2660   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2661     return JVMTI_ERROR_ABSENT_INFORMATION;
2662   }
2663 
2664   InstanceKlass* ik = InstanceKlass::cast(klass);
2665   *minor_version_ptr = ik->minor_version();
2666   *major_version_ptr = ik->major_version();
2667 
2668   return JVMTI_ERROR_NONE;
2669 } /* end GetClassVersionNumbers */
2670 
2671 
2672 // k_mirror - may be primitive, this must be checked
2673 // constant_pool_count_ptr - pre-checked for NULL
2674 // constant_pool_byte_count_ptr - pre-checked for NULL
2675 // constant_pool_bytes_ptr - pre-checked for NULL
2676 jvmtiError
2677 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2678   if (java_lang_Class::is_primitive(k_mirror)) {
2679     return JVMTI_ERROR_ABSENT_INFORMATION;
2680   }
2681 
2682   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2683   Thread *thread = Thread::current();
2684   ResourceMark rm(thread);
2685 
2686   jint status = klass->jvmti_class_status();
2687   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2688     return JVMTI_ERROR_INVALID_CLASS;
2689   }
2690   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2691     return JVMTI_ERROR_ABSENT_INFORMATION;
2692   }
2693 
2694   InstanceKlass* ik = InstanceKlass::cast(klass);
2695   JvmtiConstantPoolReconstituter reconstituter(ik);
2696   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2697     return reconstituter.get_error();
2698   }
2699 
2700   unsigned char *cpool_bytes;
2701   int cpool_size = reconstituter.cpool_size();
2702   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2703     return reconstituter.get_error();
2704   }
2705   jvmtiError res = allocate(cpool_size, &cpool_bytes);
2706   if (res != JVMTI_ERROR_NONE) {
2707     return res;
2708   }
2709   reconstituter.copy_cpool_bytes(cpool_bytes);
2710   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2711     return reconstituter.get_error();
2712   }
2713 
2714   constantPoolHandle  constants(thread, ik->constants());
2715   *constant_pool_count_ptr      = constants->length();
2716   *constant_pool_byte_count_ptr = cpool_size;
2717   *constant_pool_bytes_ptr      = cpool_bytes;
2718 
2719   return JVMTI_ERROR_NONE;
2720 } /* end GetConstantPool */
2721 
2722 
2723 // k_mirror - may be primitive, this must be checked
2724 // is_interface_ptr - pre-checked for NULL
2725 jvmtiError
2726 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2727   {
2728     bool result = false;
2729     if (!java_lang_Class::is_primitive(k_mirror)) {
2730       Klass* k = java_lang_Class::as_Klass(k_mirror);
2731       if (k != NULL && k->is_interface()) {
2732         result = true;
2733       }
2734     }
2735     *is_interface_ptr = result;
2736   }
2737 
2738   return JVMTI_ERROR_NONE;
2739 } /* end IsInterface */
2740 
2741 
2742 // k_mirror - may be primitive, this must be checked
2743 // is_array_class_ptr - pre-checked for NULL
2744 jvmtiError
2745 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
2746   {
2747     bool result = false;
2748     if (!java_lang_Class::is_primitive(k_mirror)) {
2749       Klass* k = java_lang_Class::as_Klass(k_mirror);
2750       if (k != NULL && k->is_array_klass()) {
2751         result = true;
2752       }
2753     }
2754     *is_array_class_ptr = result;
2755   }
2756 
2757   return JVMTI_ERROR_NONE;
2758 } /* end IsArrayClass */
2759 
2760 
2761 // k_mirror - may be primitive, this must be checked
2762 // classloader_ptr - pre-checked for NULL
2763 jvmtiError
2764 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
2765   {
2766     if (java_lang_Class::is_primitive(k_mirror)) {
2767       *classloader_ptr = (jclass) jni_reference(Handle());
2768       return JVMTI_ERROR_NONE;
2769     }
2770     JavaThread* current_thread = JavaThread::current();
2771     HandleMark hm(current_thread);
2772     Klass* k = java_lang_Class::as_Klass(k_mirror);
2773     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2774 
2775     oop result_oop = k->class_loader();
2776     if (result_oop == NULL) {
2777       *classloader_ptr = (jclass) jni_reference(Handle());
2778       return JVMTI_ERROR_NONE;
2779     }
2780     Handle result_handle = Handle(current_thread, result_oop);
2781     jclass result_jnihandle = (jclass) jni_reference(result_handle);
2782     *classloader_ptr = result_jnihandle;
2783   }
2784   return JVMTI_ERROR_NONE;
2785 } /* end GetClassLoader */
2786 
2787 
2788 // k_mirror - may be primitive, this must be checked
2789 // source_debug_extension_ptr - pre-checked for NULL
2790 jvmtiError
2791 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
2792   {
2793     if (java_lang_Class::is_primitive(k_mirror)) {
2794       return JVMTI_ERROR_ABSENT_INFORMATION;
2795     }
2796     Klass* k = java_lang_Class::as_Klass(k_mirror);
2797     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2798     if (!k->is_instance_klass()) {
2799       return JVMTI_ERROR_ABSENT_INFORMATION;
2800     }
2801     const char* sde = InstanceKlass::cast(k)->source_debug_extension();
2802     NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION);
2803 
2804     {
2805       *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1);
2806       strcpy(*source_debug_extension_ptr, sde);
2807     }
2808   }
2809 
2810   return JVMTI_ERROR_NONE;
2811 } /* end GetSourceDebugExtension */
2812 
2813   //
2814   // Object functions
2815   //
2816 
2817 // hash_code_ptr - pre-checked for NULL
2818 jvmtiError
2819 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
2820   oop mirror = JNIHandles::resolve_external_guard(object);
2821   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
2822   NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
2823 
2824   {
2825     jint result = (jint) mirror->identity_hash();
2826     *hash_code_ptr = result;
2827   }
2828   return JVMTI_ERROR_NONE;
2829 } /* end GetObjectHashCode */
2830 
2831 
2832 // info_ptr - pre-checked for NULL
2833 jvmtiError
2834 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
2835   JavaThread* calling_thread = JavaThread::current();
2836   jvmtiError err = get_object_monitor_usage(calling_thread, object, info_ptr);
2837   if (err == JVMTI_ERROR_THREAD_NOT_SUSPENDED) {
2838     // Some of the critical threads were not suspended. go to a safepoint and try again
2839     VM_GetObjectMonitorUsage op(this, calling_thread, object, info_ptr);
2840     VMThread::execute(&op);
2841     err = op.result();
2842   }
2843   return err;
2844 } /* end GetObjectMonitorUsage */
2845 
2846 
2847   //
2848   // Field functions
2849   //
2850 
2851 // name_ptr - NULL is a valid value, must be checked
2852 // signature_ptr - NULL is a valid value, must be checked
2853 // generic_ptr - NULL is a valid value, must be checked
2854 jvmtiError
2855 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2856   JavaThread* current_thread  = JavaThread::current();
2857   ResourceMark rm(current_thread);
2858   if (name_ptr == NULL) {
2859     // just don't return the name
2860   } else {
2861     const char* fieldName = fdesc_ptr->name()->as_C_string();
2862     *name_ptr =  (char*) jvmtiMalloc(strlen(fieldName) + 1);
2863     if (*name_ptr == NULL)
2864       return JVMTI_ERROR_OUT_OF_MEMORY;
2865     strcpy(*name_ptr, fieldName);
2866   }
2867   if (signature_ptr== NULL) {
2868     // just don't return the signature
2869   } else {
2870     const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
2871     *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
2872     if (*signature_ptr == NULL)
2873       return JVMTI_ERROR_OUT_OF_MEMORY;
2874     strcpy(*signature_ptr, fieldSignature);
2875   }
2876   if (generic_ptr != NULL) {
2877     *generic_ptr = NULL;
2878     Symbol* soop = fdesc_ptr->generic_signature();
2879     if (soop != NULL) {
2880       const char* gen_sig = soop->as_C_string();
2881       if (gen_sig != NULL) {
2882         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2883         if (err != JVMTI_ERROR_NONE) {
2884           return err;
2885         }
2886         strcpy(*generic_ptr, gen_sig);
2887       }
2888     }
2889   }
2890   return JVMTI_ERROR_NONE;
2891 } /* end GetFieldName */
2892 
2893 
2894 // declaring_class_ptr - pre-checked for NULL
2895 jvmtiError
2896 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
2897 
2898   *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
2899   return JVMTI_ERROR_NONE;
2900 } /* end GetFieldDeclaringClass */
2901 
2902 
2903 // modifiers_ptr - pre-checked for NULL
2904 jvmtiError
2905 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
2906 
2907   AccessFlags resultFlags = fdesc_ptr->access_flags();
2908   jint result = resultFlags.as_int();
2909   *modifiers_ptr = result;
2910 
2911   return JVMTI_ERROR_NONE;
2912 } /* end GetFieldModifiers */
2913 
2914 
2915 // is_synthetic_ptr - pre-checked for NULL
2916 jvmtiError
2917 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
2918   *is_synthetic_ptr = fdesc_ptr->is_synthetic();
2919   return JVMTI_ERROR_NONE;
2920 } /* end IsFieldSynthetic */
2921 
2922 
2923   //
2924   // Method functions
2925   //
2926 
2927 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2928 // name_ptr - NULL is a valid value, must be checked
2929 // signature_ptr - NULL is a valid value, must be checked
2930 // generic_ptr - NULL is a valid value, must be checked
2931 jvmtiError
2932 JvmtiEnv::GetMethodName(Method* method_oop, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2933   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2934   JavaThread* current_thread  = JavaThread::current();
2935 
2936   ResourceMark rm(current_thread); // get the utf8 name and signature
2937   if (name_ptr == NULL) {
2938     // just don't return the name
2939   } else {
2940     const char* utf8_name = (const char *) method_oop->name()->as_utf8();
2941     *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
2942     strcpy(*name_ptr, utf8_name);
2943   }
2944   if (signature_ptr == NULL) {
2945     // just don't return the signature
2946   } else {
2947     const char* utf8_signature = (const char *) method_oop->signature()->as_utf8();
2948     *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
2949     strcpy(*signature_ptr, utf8_signature);
2950   }
2951 
2952   if (generic_ptr != NULL) {
2953     *generic_ptr = NULL;
2954     Symbol* soop = method_oop->generic_signature();
2955     if (soop != NULL) {
2956       const char* gen_sig = soop->as_C_string();
2957       if (gen_sig != NULL) {
2958         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2959         if (err != JVMTI_ERROR_NONE) {
2960           return err;
2961         }
2962         strcpy(*generic_ptr, gen_sig);
2963       }
2964     }
2965   }
2966   return JVMTI_ERROR_NONE;
2967 } /* end GetMethodName */
2968 
2969 
2970 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2971 // declaring_class_ptr - pre-checked for NULL
2972 jvmtiError
2973 JvmtiEnv::GetMethodDeclaringClass(Method* method_oop, jclass* declaring_class_ptr) {
2974   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2975   (*declaring_class_ptr) = get_jni_class_non_null(method_oop->method_holder());
2976   return JVMTI_ERROR_NONE;
2977 } /* end GetMethodDeclaringClass */
2978 
2979 
2980 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2981 // modifiers_ptr - pre-checked for NULL
2982 jvmtiError
2983 JvmtiEnv::GetMethodModifiers(Method* method_oop, jint* modifiers_ptr) {
2984   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2985   (*modifiers_ptr) = method_oop->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2986   return JVMTI_ERROR_NONE;
2987 } /* end GetMethodModifiers */
2988 
2989 
2990 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2991 // max_ptr - pre-checked for NULL
2992 jvmtiError
2993 JvmtiEnv::GetMaxLocals(Method* method_oop, jint* max_ptr) {
2994   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2995   // get max stack
2996   (*max_ptr) = method_oop->max_locals();
2997   return JVMTI_ERROR_NONE;
2998 } /* end GetMaxLocals */
2999 
3000 
3001 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3002 // size_ptr - pre-checked for NULL
3003 jvmtiError
3004 JvmtiEnv::GetArgumentsSize(Method* method_oop, jint* size_ptr) {
3005   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3006   // get size of arguments
3007 
3008   (*size_ptr) = method_oop->size_of_parameters();
3009   return JVMTI_ERROR_NONE;
3010 } /* end GetArgumentsSize */
3011 
3012 
3013 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3014 // entry_count_ptr - pre-checked for NULL
3015 // table_ptr - pre-checked for NULL
3016 jvmtiError
3017 JvmtiEnv::GetLineNumberTable(Method* method_oop, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
3018   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3019   if (!method_oop->has_linenumber_table()) {
3020     return (JVMTI_ERROR_ABSENT_INFORMATION);
3021   }
3022 
3023   // The line number table is compressed so we don't know how big it is until decompressed.
3024   // Decompression is really fast so we just do it twice.
3025 
3026   // Compute size of table
3027   jint num_entries = 0;
3028   CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
3029   while (stream.read_pair()) {
3030     num_entries++;
3031   }
3032   jvmtiLineNumberEntry *jvmti_table =
3033             (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
3034 
3035   // Fill jvmti table
3036   if (num_entries > 0) {
3037     int index = 0;
3038     CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
3039     while (stream.read_pair()) {
3040       jvmti_table[index].start_location = (jlocation) stream.bci();
3041       jvmti_table[index].line_number = (jint) stream.line();
3042       index++;
3043     }
3044     assert(index == num_entries, "sanity check");
3045   }
3046 
3047   // Set up results
3048   (*entry_count_ptr) = num_entries;
3049   (*table_ptr) = jvmti_table;
3050 
3051   return JVMTI_ERROR_NONE;
3052 } /* end GetLineNumberTable */
3053 
3054 
3055 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3056 // start_location_ptr - pre-checked for NULL
3057 // end_location_ptr - pre-checked for NULL
3058 jvmtiError
3059 JvmtiEnv::GetMethodLocation(Method* method_oop, jlocation* start_location_ptr, jlocation* end_location_ptr) {
3060 
3061   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3062   // get start and end location
3063   (*end_location_ptr) = (jlocation) (method_oop->code_size() - 1);
3064   if (method_oop->code_size() == 0) {
3065     // there is no code so there is no start location
3066     (*start_location_ptr) = (jlocation)(-1);
3067   } else {
3068     (*start_location_ptr) = (jlocation)(0);
3069   }
3070 
3071   return JVMTI_ERROR_NONE;
3072 } /* end GetMethodLocation */
3073 
3074 
3075 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3076 // entry_count_ptr - pre-checked for NULL
3077 // table_ptr - pre-checked for NULL
3078 jvmtiError
3079 JvmtiEnv::GetLocalVariableTable(Method* method_oop, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
3080 
3081   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3082   JavaThread* current_thread  = JavaThread::current();
3083 
3084   // does the klass have any local variable information?
3085   InstanceKlass* ik = method_oop->method_holder();
3086   if (!ik->access_flags().has_localvariable_table()) {
3087     return (JVMTI_ERROR_ABSENT_INFORMATION);
3088   }
3089 
3090   ConstantPool* constants = method_oop->constants();
3091   NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
3092 
3093   // in the vm localvariable table representation, 6 consecutive elements in the table
3094   // represent a 6-tuple of shorts
3095   // [start_pc, length, name_index, descriptor_index, signature_index, index]
3096   jint num_entries = method_oop->localvariable_table_length();
3097   jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
3098                 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
3099 
3100   if (num_entries > 0) {
3101     LocalVariableTableElement* table = method_oop->localvariable_table_start();
3102     for (int i = 0; i < num_entries; i++) {
3103       // get the 5 tuple information from the vm table
3104       jlocation start_location = (jlocation) table[i].start_bci;
3105       jint length = (jint) table[i].length;
3106       int name_index = (int) table[i].name_cp_index;
3107       int signature_index = (int) table[i].descriptor_cp_index;
3108       int generic_signature_index = (int) table[i].signature_cp_index;
3109       jint slot = (jint) table[i].slot;
3110 
3111       // get utf8 name and signature
3112       char *name_buf = NULL;
3113       char *sig_buf = NULL;
3114       char *gen_sig_buf = NULL;
3115       {
3116         ResourceMark rm(current_thread);
3117 
3118         const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
3119         name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3120         strcpy(name_buf, utf8_name);
3121 
3122         const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
3123         sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
3124         strcpy(sig_buf, utf8_signature);
3125 
3126         if (generic_signature_index > 0) {
3127           const char *utf8_gen_sign = (const char *)
3128                                        constants->symbol_at(generic_signature_index)->as_utf8();
3129           gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
3130           strcpy(gen_sig_buf, utf8_gen_sign);
3131         }
3132       }
3133 
3134       // fill in the jvmti local variable table
3135       jvmti_table[i].start_location = start_location;
3136       jvmti_table[i].length = length;
3137       jvmti_table[i].name = name_buf;
3138       jvmti_table[i].signature = sig_buf;
3139       jvmti_table[i].generic_signature = gen_sig_buf;
3140       jvmti_table[i].slot = slot;
3141     }
3142   }
3143 
3144   // set results
3145   (*entry_count_ptr) = num_entries;
3146   (*table_ptr) = jvmti_table;
3147 
3148   return JVMTI_ERROR_NONE;
3149 } /* end GetLocalVariableTable */
3150 
3151 
3152 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3153 // bytecode_count_ptr - pre-checked for NULL
3154 // bytecodes_ptr - pre-checked for NULL
3155 jvmtiError
3156 JvmtiEnv::GetBytecodes(Method* method_oop, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
3157   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3158 
3159   HandleMark hm;
3160   methodHandle method(method_oop);
3161   jint size = (jint)method->code_size();
3162   jvmtiError err = allocate(size, bytecodes_ptr);
3163   if (err != JVMTI_ERROR_NONE) {
3164     return err;
3165   }
3166 
3167   (*bytecode_count_ptr) = size;
3168   // get byte codes
3169   JvmtiClassFileReconstituter::copy_bytecodes(method, *bytecodes_ptr);
3170 
3171   return JVMTI_ERROR_NONE;
3172 } /* end GetBytecodes */
3173 
3174 
3175 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3176 // is_native_ptr - pre-checked for NULL
3177 jvmtiError
3178 JvmtiEnv::IsMethodNative(Method* method_oop, jboolean* is_native_ptr) {
3179   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3180   (*is_native_ptr) = method_oop->is_native();
3181   return JVMTI_ERROR_NONE;
3182 } /* end IsMethodNative */
3183 
3184 
3185 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3186 // is_synthetic_ptr - pre-checked for NULL
3187 jvmtiError
3188 JvmtiEnv::IsMethodSynthetic(Method* method_oop, jboolean* is_synthetic_ptr) {
3189   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3190   (*is_synthetic_ptr) = method_oop->is_synthetic();
3191   return JVMTI_ERROR_NONE;
3192 } /* end IsMethodSynthetic */
3193 
3194 
3195 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3196 // is_obsolete_ptr - pre-checked for NULL
3197 jvmtiError
3198 JvmtiEnv::IsMethodObsolete(Method* method_oop, jboolean* is_obsolete_ptr) {
3199   if (use_version_1_0_semantics() &&
3200       get_capabilities()->can_redefine_classes == 0) {
3201     // This JvmtiEnv requested version 1.0 semantics and this function
3202     // requires the can_redefine_classes capability in version 1.0 so
3203     // we need to return an error here.
3204     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3205   }
3206 
3207   if (method_oop == NULL || method_oop->is_obsolete()) {
3208     *is_obsolete_ptr = true;
3209   } else {
3210     *is_obsolete_ptr = false;
3211   }
3212   return JVMTI_ERROR_NONE;
3213 } /* end IsMethodObsolete */
3214 
3215   //
3216   // Raw Monitor functions
3217   //
3218 
3219 // name - pre-checked for NULL
3220 // monitor_ptr - pre-checked for NULL
3221 jvmtiError
3222 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
3223   JvmtiRawMonitor* rmonitor = new JvmtiRawMonitor(name);
3224   NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
3225 
3226   *monitor_ptr = (jrawMonitorID)rmonitor;
3227 
3228   return JVMTI_ERROR_NONE;
3229 } /* end CreateRawMonitor */
3230 
3231 
3232 // rmonitor - pre-checked for validity
3233 jvmtiError
3234 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
3235   if (Threads::number_of_threads() == 0) {
3236     // Remove this  monitor from pending raw monitors list
3237     // if it has entered in onload or start phase.
3238     JvmtiPendingMonitors::destroy(rmonitor);
3239   } else {
3240     Thread* thread  = Thread::current();
3241     if (rmonitor->is_entered(thread)) {
3242       // The caller owns this monitor which we are about to destroy.
3243       // We exit the underlying synchronization object so that the
3244       // "delete monitor" call below can work without an assertion
3245       // failure on systems that don't like destroying synchronization
3246       // objects that are locked.
3247       int r;
3248       intptr_t recursion = rmonitor->recursions();
3249       for (intptr_t i=0; i <= recursion; i++) {
3250         r = rmonitor->raw_exit(thread);
3251         assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
3252         if (r != ObjectMonitor::OM_OK) {  // robustness
3253           return JVMTI_ERROR_INTERNAL;
3254         }
3255       }
3256     }
3257     if (rmonitor->owner() != NULL) {
3258       // The caller is trying to destroy a monitor that is locked by
3259       // someone else. While this is not forbidden by the JVMTI
3260       // spec, it will cause an assertion failure on systems that don't
3261       // like destroying synchronization objects that are locked.
3262       // We indicate a problem with the error return (and leak the
3263       // monitor's memory).
3264       return JVMTI_ERROR_NOT_MONITOR_OWNER;
3265     }
3266   }
3267 
3268   delete rmonitor;
3269 
3270   return JVMTI_ERROR_NONE;
3271 } /* end DestroyRawMonitor */
3272 
3273 
3274 // rmonitor - pre-checked for validity
3275 jvmtiError
3276 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
3277   if (Threads::number_of_threads() == 0) {
3278     // No JavaThreads exist so ObjectMonitor enter cannot be
3279     // used, add this raw monitor to the pending list.
3280     // The pending monitors will be actually entered when
3281     // the VM is setup.
3282     // See transition_pending_raw_monitors in create_vm()
3283     // in thread.cpp.
3284     JvmtiPendingMonitors::enter(rmonitor);
3285   } else {
3286     int r = 0;
3287     Thread* thread = Thread::current();
3288 
3289     if (thread->is_Java_thread()) {
3290       JavaThread* current_thread = (JavaThread*)thread;
3291 
3292 #ifdef PROPER_TRANSITIONS
3293       // Not really unknown but ThreadInVMfromNative does more than we want
3294       ThreadInVMfromUnknown __tiv;
3295       {
3296         ThreadBlockInVM __tbivm(current_thread);
3297         r = rmonitor->raw_enter(current_thread);
3298       }
3299 #else
3300       /* Transition to thread_blocked without entering vm state          */
3301       /* This is really evil. Normally you can't undo _thread_blocked    */
3302       /* transitions like this because it would cause us to miss a       */
3303       /* safepoint but since the thread was already in _thread_in_native */
3304       /* the thread is not leaving a safepoint safe state and it will    */
3305       /* block when it tries to return from native. We can't safepoint   */
3306       /* block in here because we could deadlock the vmthread. Blech.    */
3307 
3308       JavaThreadState state = current_thread->thread_state();
3309       assert(state == _thread_in_native, "Must be _thread_in_native");
3310       // frame should already be walkable since we are in native
3311       assert(!current_thread->has_last_Java_frame() ||
3312              current_thread->frame_anchor()->walkable(), "Must be walkable");
3313       current_thread->set_thread_state(_thread_blocked);
3314 
3315       r = rmonitor->raw_enter(current_thread);
3316       // restore state, still at a safepoint safe state
3317       current_thread->set_thread_state(state);
3318 
3319 #endif /* PROPER_TRANSITIONS */
3320       assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked");
3321     } else {
3322       if (thread->is_Named_thread()) {
3323         r = rmonitor->raw_enter(thread);
3324       } else {
3325         ShouldNotReachHere();
3326       }
3327     }
3328 
3329     if (r != ObjectMonitor::OM_OK) {  // robustness
3330       return JVMTI_ERROR_INTERNAL;
3331     }
3332   }
3333   return JVMTI_ERROR_NONE;
3334 } /* end RawMonitorEnter */
3335 
3336 
3337 // rmonitor - pre-checked for validity
3338 jvmtiError
3339 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3340   jvmtiError err = JVMTI_ERROR_NONE;
3341 
3342   if (Threads::number_of_threads() == 0) {
3343     // No JavaThreads exist so just remove this monitor from the pending list.
3344     // Bool value from exit is false if rmonitor is not in the list.
3345     if (!JvmtiPendingMonitors::exit(rmonitor)) {
3346       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3347     }
3348   } else {
3349     int r = 0;
3350     Thread* thread = Thread::current();
3351 
3352     if (thread->is_Java_thread()) {
3353       JavaThread* current_thread = (JavaThread*)thread;
3354 #ifdef PROPER_TRANSITIONS
3355       // Not really unknown but ThreadInVMfromNative does more than we want
3356       ThreadInVMfromUnknown __tiv;
3357 #endif /* PROPER_TRANSITIONS */
3358       r = rmonitor->raw_exit(current_thread);
3359     } else {
3360       if (thread->is_Named_thread()) {
3361         r = rmonitor->raw_exit(thread);
3362       } else {
3363         ShouldNotReachHere();
3364       }
3365     }
3366 
3367     if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3368       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3369     } else {
3370       assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
3371       if (r != ObjectMonitor::OM_OK) {  // robustness
3372         err = JVMTI_ERROR_INTERNAL;
3373       }
3374     }
3375   }
3376   return err;
3377 } /* end RawMonitorExit */
3378 
3379 
3380 // rmonitor - pre-checked for validity
3381 jvmtiError
3382 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3383   int r = 0;
3384   Thread* thread = Thread::current();
3385 
3386   if (thread->is_Java_thread()) {
3387     JavaThread* current_thread = (JavaThread*)thread;
3388 #ifdef PROPER_TRANSITIONS
3389     // Not really unknown but ThreadInVMfromNative does more than we want
3390     ThreadInVMfromUnknown __tiv;
3391     {
3392       ThreadBlockInVM __tbivm(current_thread);
3393       r = rmonitor->raw_wait(millis, true, current_thread);
3394     }
3395 #else
3396     /* Transition to thread_blocked without entering vm state          */
3397     /* This is really evil. Normally you can't undo _thread_blocked    */
3398     /* transitions like this because it would cause us to miss a       */
3399     /* safepoint but since the thread was already in _thread_in_native */
3400     /* the thread is not leaving a safepoint safe state and it will    */
3401     /* block when it tries to return from native. We can't safepoint   */
3402     /* block in here because we could deadlock the vmthread. Blech.    */
3403 
3404     JavaThreadState state = current_thread->thread_state();
3405     assert(state == _thread_in_native, "Must be _thread_in_native");
3406     // frame should already be walkable since we are in native
3407     assert(!current_thread->has_last_Java_frame() ||
3408            current_thread->frame_anchor()->walkable(), "Must be walkable");
3409     current_thread->set_thread_state(_thread_blocked);
3410 
3411     r = rmonitor->raw_wait(millis, true, current_thread);
3412     // restore state, still at a safepoint safe state
3413     current_thread->set_thread_state(state);
3414 
3415 #endif /* PROPER_TRANSITIONS */
3416   } else {
3417     if (thread->is_Named_thread()) {
3418       r = rmonitor->raw_wait(millis, true, thread);
3419     } else {
3420       ShouldNotReachHere();
3421     }
3422   }
3423 
3424   switch (r) {
3425   case ObjectMonitor::OM_INTERRUPTED:
3426     return JVMTI_ERROR_INTERRUPT;
3427   case ObjectMonitor::OM_ILLEGAL_MONITOR_STATE:
3428     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3429   }
3430   assert(r == ObjectMonitor::OM_OK, "raw_wait should have worked");
3431   if (r != ObjectMonitor::OM_OK) {  // robustness
3432     return JVMTI_ERROR_INTERNAL;
3433   }
3434 
3435   return JVMTI_ERROR_NONE;
3436 } /* end RawMonitorWait */
3437 
3438 
3439 // rmonitor - pre-checked for validity
3440 jvmtiError
3441 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3442   int r = 0;
3443   Thread* thread = Thread::current();
3444 
3445   if (thread->is_Java_thread()) {
3446     JavaThread* current_thread = (JavaThread*)thread;
3447     // Not really unknown but ThreadInVMfromNative does more than we want
3448     ThreadInVMfromUnknown __tiv;
3449     r = rmonitor->raw_notify(current_thread);
3450   } else {
3451     if (thread->is_Named_thread()) {
3452       r = rmonitor->raw_notify(thread);
3453     } else {
3454       ShouldNotReachHere();
3455     }
3456   }
3457 
3458   if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3459     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3460   }
3461   assert(r == ObjectMonitor::OM_OK, "raw_notify should have worked");
3462   if (r != ObjectMonitor::OM_OK) {  // robustness
3463     return JVMTI_ERROR_INTERNAL;
3464   }
3465 
3466   return JVMTI_ERROR_NONE;
3467 } /* end RawMonitorNotify */
3468 
3469 
3470 // rmonitor - pre-checked for validity
3471 jvmtiError
3472 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3473   int r = 0;
3474   Thread* thread = Thread::current();
3475 
3476   if (thread->is_Java_thread()) {
3477     JavaThread* current_thread = (JavaThread*)thread;
3478     ThreadInVMfromUnknown __tiv;
3479     r = rmonitor->raw_notifyAll(current_thread);
3480   } else {
3481     if (thread->is_Named_thread()) {
3482       r = rmonitor->raw_notifyAll(thread);
3483     } else {
3484       ShouldNotReachHere();
3485     }
3486   }
3487 
3488   if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3489     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3490   }
3491   assert(r == ObjectMonitor::OM_OK, "raw_notifyAll should have worked");
3492   if (r != ObjectMonitor::OM_OK) {  // robustness
3493     return JVMTI_ERROR_INTERNAL;
3494   }
3495 
3496   return JVMTI_ERROR_NONE;
3497 } /* end RawMonitorNotifyAll */
3498 
3499 
3500   //
3501   // JNI Function Interception functions
3502   //
3503 
3504 
3505 // function_table - pre-checked for NULL
3506 jvmtiError
3507 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3508   // Copy jni function table at safepoint.
3509   VM_JNIFunctionTableCopier copier(function_table);
3510   VMThread::execute(&copier);
3511 
3512   return JVMTI_ERROR_NONE;
3513 } /* end SetJNIFunctionTable */
3514 
3515 
3516 // function_table - pre-checked for NULL
3517 jvmtiError
3518 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3519   *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3520   if (*function_table == NULL)
3521     return JVMTI_ERROR_OUT_OF_MEMORY;
3522   memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3523   return JVMTI_ERROR_NONE;
3524 } /* end GetJNIFunctionTable */
3525 
3526 
3527   //
3528   // Event Management functions
3529   //
3530 
3531 jvmtiError
3532 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3533   // can only generate two event types
3534   if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3535       event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3536     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3537   }
3538 
3539   // for compiled_method_load events we must check that the environment
3540   // has the can_generate_compiled_method_load_events capability.
3541   if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3542     if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3543       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3544     }
3545     return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3546   } else {
3547     return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3548   }
3549 
3550 } /* end GenerateEvents */
3551 
3552 
3553   //
3554   // Extension Mechanism functions
3555   //
3556 
3557 // extension_count_ptr - pre-checked for NULL
3558 // extensions - pre-checked for NULL
3559 jvmtiError
3560 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3561   return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3562 } /* end GetExtensionFunctions */
3563 
3564 
3565 // extension_count_ptr - pre-checked for NULL
3566 // extensions - pre-checked for NULL
3567 jvmtiError
3568 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3569   return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3570 } /* end GetExtensionEvents */
3571 
3572 
3573 // callback - NULL is a valid value, must be checked
3574 jvmtiError
3575 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3576   return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3577 } /* end SetExtensionEventCallback */
3578 
3579   //
3580   // Timers functions
3581   //
3582 
3583 // info_ptr - pre-checked for NULL
3584 jvmtiError
3585 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3586   os::current_thread_cpu_time_info(info_ptr);
3587   return JVMTI_ERROR_NONE;
3588 } /* end GetCurrentThreadCpuTimerInfo */
3589 
3590 
3591 // nanos_ptr - pre-checked for NULL
3592 jvmtiError
3593 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3594   *nanos_ptr = os::current_thread_cpu_time();
3595   return JVMTI_ERROR_NONE;
3596 } /* end GetCurrentThreadCpuTime */
3597 
3598 
3599 // info_ptr - pre-checked for NULL
3600 jvmtiError
3601 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3602   os::thread_cpu_time_info(info_ptr);
3603   return JVMTI_ERROR_NONE;
3604 } /* end GetThreadCpuTimerInfo */
3605 
3606 
3607 // Threads_lock NOT held, java_thread not protected by lock
3608 // java_thread - pre-checked
3609 // nanos_ptr - pre-checked for NULL
3610 jvmtiError
3611 JvmtiEnv::GetThreadCpuTime(JavaThread* java_thread, jlong* nanos_ptr) {
3612   *nanos_ptr = os::thread_cpu_time(java_thread);
3613   return JVMTI_ERROR_NONE;
3614 } /* end GetThreadCpuTime */
3615 
3616 
3617 // info_ptr - pre-checked for NULL
3618 jvmtiError
3619 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3620   os::javaTimeNanos_info(info_ptr);
3621   return JVMTI_ERROR_NONE;
3622 } /* end GetTimerInfo */
3623 
3624 
3625 // nanos_ptr - pre-checked for NULL
3626 jvmtiError
3627 JvmtiEnv::GetTime(jlong* nanos_ptr) {
3628   *nanos_ptr = os::javaTimeNanos();
3629   return JVMTI_ERROR_NONE;
3630 } /* end GetTime */
3631 
3632 
3633 // processor_count_ptr - pre-checked for NULL
3634 jvmtiError
3635 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3636   *processor_count_ptr = os::active_processor_count();
3637   return JVMTI_ERROR_NONE;
3638 } /* end GetAvailableProcessors */
3639 
3640 jvmtiError
3641 JvmtiEnv::SetHeapSamplingInterval(jint sampling_interval) {
3642   if (sampling_interval < 0) {
3643     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3644   }
3645   ThreadHeapSampler::set_sampling_interval(sampling_interval);
3646   return JVMTI_ERROR_NONE;
3647 } /* end SetHeapSamplingInterval */
3648 
3649   //
3650   // System Properties functions
3651   //
3652 
3653 // count_ptr - pre-checked for NULL
3654 // property_ptr - pre-checked for NULL
3655 jvmtiError
3656 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3657   jvmtiError err = JVMTI_ERROR_NONE;
3658 
3659   // Get the number of readable properties.
3660   *count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties());
3661 
3662   // Allocate memory to hold the exact number of readable properties.
3663   err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3664   if (err != JVMTI_ERROR_NONE) {
3665     return err;
3666   }
3667   int readable_count = 0;
3668   // Loop through the system properties until all the readable properties are found.
3669   for (SystemProperty* p = Arguments::system_properties(); p != NULL && readable_count < *count_ptr; p = p->next()) {
3670     if (p->is_readable()) {
3671       const char *key = p->key();
3672       char **tmp_value = *property_ptr+readable_count;
3673       readable_count++;
3674       err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3675       if (err == JVMTI_ERROR_NONE) {
3676         strcpy(*tmp_value, key);
3677       } else {
3678         // clean up previously allocated memory.
3679         for (int j=0; j<readable_count; j++) {
3680           Deallocate((unsigned char*)*property_ptr+j);
3681         }
3682         Deallocate((unsigned char*)property_ptr);
3683         break;
3684       }
3685     }
3686   }
3687   assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count");
3688   return err;
3689 } /* end GetSystemProperties */
3690 
3691 
3692 // property - pre-checked for NULL
3693 // value_ptr - pre-checked for NULL
3694 jvmtiError
3695 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3696   jvmtiError err = JVMTI_ERROR_NONE;
3697   const char *value;
3698 
3699   // Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist.
3700   value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property);
3701   if (value == NULL) {
3702     err =  JVMTI_ERROR_NOT_AVAILABLE;
3703   } else {
3704     err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3705     if (err == JVMTI_ERROR_NONE) {
3706       strcpy(*value_ptr, value);
3707     }
3708   }
3709   return err;
3710 } /* end GetSystemProperty */
3711 
3712 
3713 // property - pre-checked for NULL
3714 // value - NULL is a valid value, must be checked
3715 jvmtiError
3716 JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) {
3717   jvmtiError err =JVMTI_ERROR_NOT_AVAILABLE;
3718 
3719   for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
3720     if (strcmp(property, p->key()) == 0) {
3721       if (p->set_writeable_value(value_ptr)) {
3722         err =  JVMTI_ERROR_NONE;
3723       }
3724     }
3725   }
3726   return err;
3727 } /* end SetSystemProperty */
--- EOF ---