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