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