rev 47862 : imported patch 10.07.open.rebase_20171110.dcubed

   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_Module::is_instance(h_module())) {
 239     return JVMTI_ERROR_INVALID_MODULE;
 240   }
 241   // check to_module
 242   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 243   if (!java_lang_Module::is_instance(h_to_module())) {
 244     return JVMTI_ERROR_INVALID_MODULE;
 245   }
 246   return JvmtiExport::add_module_reads(h_module, h_to_module, THREAD);
 247 } /* end AddModuleReads */
 248 
 249 
 250 // module - pre-checked for NULL
 251 // pkg_name - pre-checked for NULL
 252 // to_module - pre-checked for NULL
 253 jvmtiError
 254 JvmtiEnv::AddModuleExports(jobject module, const char* pkg_name, jobject to_module) {
 255   JavaThread* THREAD = JavaThread::current();
 256   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 257 
 258   // check module
 259   Handle h_module(THREAD, JNIHandles::resolve(module));
 260   if (!java_lang_Module::is_instance(h_module())) {
 261     return JVMTI_ERROR_INVALID_MODULE;
 262   }
 263   // check to_module
 264   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 265   if (!java_lang_Module::is_instance(h_to_module())) {
 266     return JVMTI_ERROR_INVALID_MODULE;
 267   }
 268   return JvmtiExport::add_module_exports(h_module, h_pkg, h_to_module, THREAD);
 269 } /* end AddModuleExports */
 270 
 271 
 272 // module - pre-checked for NULL
 273 // pkg_name - pre-checked for NULL
 274 // to_module - pre-checked for NULL
 275 jvmtiError
 276 JvmtiEnv::AddModuleOpens(jobject module, const char* pkg_name, jobject to_module) {
 277   JavaThread* THREAD = JavaThread::current();
 278   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 279 
 280   // check module
 281   Handle h_module(THREAD, JNIHandles::resolve(module));
 282   if (!java_lang_Module::is_instance(h_module())) {
 283     return JVMTI_ERROR_INVALID_MODULE;
 284   }
 285   // check to_module
 286   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 287   if (!java_lang_Module::is_instance(h_to_module())) {
 288     return JVMTI_ERROR_INVALID_MODULE;
 289   }
 290   return JvmtiExport::add_module_opens(h_module, h_pkg, h_to_module, THREAD);
 291 } /* end AddModuleOpens */
 292 
 293 
 294 // module - pre-checked for NULL
 295 // service - pre-checked for NULL
 296 jvmtiError
 297 JvmtiEnv::AddModuleUses(jobject module, jclass service) {
 298   JavaThread* THREAD = JavaThread::current();
 299 
 300   // check module
 301   Handle h_module(THREAD, JNIHandles::resolve(module));
 302   if (!java_lang_Module::is_instance(h_module())) {
 303     return JVMTI_ERROR_INVALID_MODULE;
 304   }
 305   // check service
 306   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 307   if (!java_lang_Class::is_instance(h_service()) ||
 308       java_lang_Class::is_primitive(h_service())) {
 309     return JVMTI_ERROR_INVALID_CLASS;
 310   }
 311   return JvmtiExport::add_module_uses(h_module, h_service, THREAD);
 312 } /* end AddModuleUses */
 313 
 314 
 315 // module - pre-checked for NULL
 316 // service - pre-checked for NULL
 317 // impl_class - pre-checked for NULL
 318 jvmtiError
 319 JvmtiEnv::AddModuleProvides(jobject module, jclass service, jclass impl_class) {
 320   JavaThread* THREAD = JavaThread::current();
 321 
 322   // check module
 323   Handle h_module(THREAD, JNIHandles::resolve(module));
 324   if (!java_lang_Module::is_instance(h_module())) {
 325     return JVMTI_ERROR_INVALID_MODULE;
 326   }
 327   // check service
 328   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 329   if (!java_lang_Class::is_instance(h_service()) ||
 330       java_lang_Class::is_primitive(h_service())) {
 331     return JVMTI_ERROR_INVALID_CLASS;
 332   }
 333   // check impl_class
 334   Handle h_impl_class(THREAD, JNIHandles::resolve_external_guard(impl_class));
 335   if (!java_lang_Class::is_instance(h_impl_class()) ||
 336       java_lang_Class::is_primitive(h_impl_class())) {
 337     return JVMTI_ERROR_INVALID_CLASS;
 338   }
 339   return JvmtiExport::add_module_provides(h_module, h_service, h_impl_class, THREAD);
 340 } /* end AddModuleProvides */
 341 
 342 // module - pre-checked for NULL
 343 // is_modifiable_class_ptr - pre-checked for NULL
 344 jvmtiError
 345 JvmtiEnv::IsModifiableModule(jobject module, jboolean* is_modifiable_module_ptr) {
 346   JavaThread* THREAD = JavaThread::current();
 347 
 348   // check module
 349   Handle h_module(THREAD, JNIHandles::resolve(module));
 350   if (!java_lang_Module::is_instance(h_module())) {
 351     return JVMTI_ERROR_INVALID_MODULE;
 352   }
 353 
 354   *is_modifiable_module_ptr = JNI_TRUE;
 355   return JVMTI_ERROR_NONE;
 356 } /* end IsModifiableModule */
 357 
 358 
 359   //
 360   // Class functions
 361   //
 362 
 363 // class_count_ptr - pre-checked for NULL
 364 // classes_ptr - pre-checked for NULL
 365 jvmtiError
 366 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
 367   return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
 368 } /* end GetLoadedClasses */
 369 
 370 
 371 // initiating_loader - NULL is a valid value, must be checked
 372 // class_count_ptr - pre-checked for NULL
 373 // classes_ptr - pre-checked for NULL
 374 jvmtiError
 375 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
 376   return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
 377                                                   class_count_ptr, classes_ptr);
 378 } /* end GetClassLoaderClasses */
 379 
 380 // k_mirror - may be primitive, this must be checked
 381 // is_modifiable_class_ptr - pre-checked for NULL
 382 jvmtiError
 383 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
 384   *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
 385                                                        JNI_TRUE : JNI_FALSE;
 386   return JVMTI_ERROR_NONE;
 387 } /* end IsModifiableClass */
 388 
 389 // class_count - pre-checked to be greater than or equal to 0
 390 // classes - pre-checked for NULL
 391 jvmtiError
 392 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
 393 //TODO: add locking
 394 
 395   int index;
 396   JavaThread* current_thread = JavaThread::current();
 397   ResourceMark rm(current_thread);
 398 
 399   jvmtiClassDefinition* class_definitions =
 400                             NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
 401   NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
 402 
 403   for (index = 0; index < class_count; index++) {
 404     HandleMark hm(current_thread);
 405 
 406     jclass jcls = classes[index];
 407     oop k_mirror = JNIHandles::resolve_external_guard(jcls);
 408     if (k_mirror == NULL) {
 409       return JVMTI_ERROR_INVALID_CLASS;
 410     }
 411     if (!k_mirror->is_a(SystemDictionary::Class_klass())) {
 412       return JVMTI_ERROR_INVALID_CLASS;
 413     }
 414 
 415     if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
 416       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
 417     }
 418 
 419     Klass* klass = java_lang_Class::as_Klass(k_mirror);
 420 
 421     jint status = klass->jvmti_class_status();
 422     if (status & (JVMTI_CLASS_STATUS_ERROR)) {
 423       return JVMTI_ERROR_INVALID_CLASS;
 424     }
 425 
 426     InstanceKlass* ik = InstanceKlass::cast(klass);
 427     if (ik->get_cached_class_file_bytes() == NULL) {
 428       // Not cached, we need to reconstitute the class file from the
 429       // VM representation. We don't attach the reconstituted class
 430       // bytes to the InstanceKlass here because they have not been
 431       // validated and we're not at a safepoint.
 432       JvmtiClassFileReconstituter reconstituter(ik);
 433       if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
 434         return reconstituter.get_error();
 435       }
 436 
 437       class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
 438       class_definitions[index].class_bytes      = (unsigned char*)
 439                                                        reconstituter.class_file_bytes();
 440     } else {
 441       // it is cached, get it from the cache
 442       class_definitions[index].class_byte_count = ik->get_cached_class_file_len();
 443       class_definitions[index].class_bytes      = ik->get_cached_class_file_bytes();
 444     }
 445     class_definitions[index].klass              = jcls;
 446   }
 447   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
 448   VMThread::execute(&op);
 449   return (op.check_error());
 450 } /* end RetransformClasses */
 451 
 452 
 453 // class_count - pre-checked to be greater than or equal to 0
 454 // class_definitions - pre-checked for NULL
 455 jvmtiError
 456 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
 457 //TODO: add locking
 458   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
 459   VMThread::execute(&op);
 460   return (op.check_error());
 461 } /* end RedefineClasses */
 462 
 463 
 464   //
 465   // Object functions
 466   //
 467 
 468 // size_ptr - pre-checked for NULL
 469 jvmtiError
 470 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
 471   oop mirror = JNIHandles::resolve_external_guard(object);
 472   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
 473   *size_ptr = (jlong)mirror->size() * wordSize;
 474   return JVMTI_ERROR_NONE;
 475 } /* end GetObjectSize */
 476 
 477   //
 478   // Method functions
 479   //
 480 
 481 // prefix - NULL is a valid value, must be checked
 482 jvmtiError
 483 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
 484   return prefix == NULL?
 485               SetNativeMethodPrefixes(0, NULL) :
 486               SetNativeMethodPrefixes(1, (char**)&prefix);
 487 } /* end SetNativeMethodPrefix */
 488 
 489 
 490 // prefix_count - pre-checked to be greater than or equal to 0
 491 // prefixes - pre-checked for NULL
 492 jvmtiError
 493 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
 494   // Have to grab JVMTI thread state lock to be sure that some thread
 495   // isn't accessing the prefixes at the same time we are setting them.
 496   // No locks during VM bring-up.
 497   if (Threads::number_of_threads() == 0) {
 498     return set_native_method_prefixes(prefix_count, prefixes);
 499   } else {
 500     MutexLocker mu(JvmtiThreadState_lock);
 501     return set_native_method_prefixes(prefix_count, prefixes);
 502   }
 503 } /* end SetNativeMethodPrefixes */
 504 
 505   //
 506   // Event Management functions
 507   //
 508 
 509 // callbacks - NULL is a valid value, must be checked
 510 // size_of_callbacks - pre-checked to be greater than or equal to 0
 511 jvmtiError
 512 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
 513   JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
 514   return JVMTI_ERROR_NONE;
 515 } /* end SetEventCallbacks */
 516 
 517 
 518 // event_thread - NULL is a valid value, must be checked
 519 jvmtiError
 520 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread,   ...) {
 521   JavaThread* java_thread = NULL;
 522   if (event_thread != NULL) {
 523     oop thread_oop = JNIHandles::resolve_external_guard(event_thread);
 524     if (thread_oop == NULL) {
 525       return JVMTI_ERROR_INVALID_THREAD;




 526     }
 527     if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
 528       return JVMTI_ERROR_INVALID_THREAD;




 529     }
 530     java_thread = java_lang_Thread::thread(thread_oop);
 531     if (java_thread == NULL) {
 532       return JVMTI_ERROR_THREAD_NOT_ALIVE;
 533     }









 534   }
 535 
 536   // event_type must be valid
 537   if (!JvmtiEventController::is_valid_event_type(event_type)) {
 538     return JVMTI_ERROR_INVALID_EVENT_TYPE;
 539   }
 540 
 541   // global events cannot be controlled at thread level.
 542   if (java_thread != NULL && JvmtiEventController::is_global_event(event_type)) {
 543     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 544   }
 545 
 546   bool enabled = (mode == JVMTI_ENABLE);
 547 
 548   // assure that needed capabilities are present
 549   if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 550     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 551   }
 552 
 553   if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 554     record_class_file_load_hook_enabled();
 555   }
 556   JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);

 557 
 558   return JVMTI_ERROR_NONE;
 559 } /* end SetEventNotificationMode */
 560 
 561   //
 562   // Capability functions
 563   //
 564 
 565 // capabilities_ptr - pre-checked for NULL
 566 jvmtiError
 567 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
 568   JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
 569                                                       get_prohibited_capabilities(),
 570                                                       capabilities_ptr);
 571   return JVMTI_ERROR_NONE;
 572 } /* end GetPotentialCapabilities */
 573 
 574 
 575 // capabilities_ptr - pre-checked for NULL
 576 jvmtiError
 577 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 578   return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
 579                                                    get_prohibited_capabilities(),
 580                                                    capabilities_ptr,
 581                                                    get_capabilities());
 582 } /* end AddCapabilities */
 583 
 584 
 585 // capabilities_ptr - pre-checked for NULL
 586 jvmtiError
 587 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 588   JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
 589   return JVMTI_ERROR_NONE;
 590 } /* end RelinquishCapabilities */
 591 
 592 
 593 // capabilities_ptr - pre-checked for NULL
 594 jvmtiError
 595 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
 596   JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
 597   return JVMTI_ERROR_NONE;
 598 } /* end GetCapabilities */
 599 
 600   //
 601   // Class Loader Search functions
 602   //
 603 
 604 // segment - pre-checked for NULL
 605 jvmtiError
 606 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
 607   jvmtiPhase phase = get_phase();
 608   if (phase == JVMTI_PHASE_ONLOAD) {
 609     Arguments::append_sysclasspath(segment);
 610     return JVMTI_ERROR_NONE;
 611   } else if (use_version_1_0_semantics()) {
 612     // This JvmtiEnv requested version 1.0 semantics and this function
 613     // is only allowed in the ONLOAD phase in version 1.0 so we need to
 614     // return an error here.
 615     return JVMTI_ERROR_WRONG_PHASE;
 616   } else if (phase == JVMTI_PHASE_LIVE) {
 617     // The phase is checked by the wrapper that called this function,
 618     // but this thread could be racing with the thread that is
 619     // terminating the VM so we check one more time.
 620 
 621     // create the zip entry
 622     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, true);
 623     if (zip_entry == NULL) {
 624       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 625     }
 626 
 627     // lock the loader
 628     Thread* thread = Thread::current();
 629     HandleMark hm;
 630     Handle loader_lock = Handle(thread, SystemDictionary::system_loader_lock());
 631 
 632     ObjectLocker ol(loader_lock, thread);
 633 
 634     // add the jar file to the bootclasspath
 635     log_info(class, load)("opened: %s", zip_entry->name());
 636     ClassLoaderExt::append_boot_classpath(zip_entry);
 637     return JVMTI_ERROR_NONE;
 638   } else {
 639     return JVMTI_ERROR_WRONG_PHASE;
 640   }
 641 
 642 } /* end AddToBootstrapClassLoaderSearch */
 643 
 644 
 645 // segment - pre-checked for NULL
 646 jvmtiError
 647 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
 648   jvmtiPhase phase = get_phase();
 649 
 650   if (phase == JVMTI_PHASE_ONLOAD) {
 651     for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
 652       if (strcmp("java.class.path", p->key()) == 0) {
 653         p->append_value(segment);
 654         break;
 655       }
 656     }
 657     return JVMTI_ERROR_NONE;
 658   } else if (phase == JVMTI_PHASE_LIVE) {
 659     // The phase is checked by the wrapper that called this function,
 660     // but this thread could be racing with the thread that is
 661     // terminating the VM so we check one more time.
 662     HandleMark hm;
 663 
 664     // create the zip entry (which will open the zip file and hence
 665     // check that the segment is indeed a zip file).
 666     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false);
 667     if (zip_entry == NULL) {
 668       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 669     }
 670     delete zip_entry;   // no longer needed
 671 
 672     // lock the loader
 673     Thread* THREAD = Thread::current();
 674     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 675 
 676     ObjectLocker ol(loader, THREAD);
 677 
 678     // need the path as java.lang.String
 679     Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
 680     if (HAS_PENDING_EXCEPTION) {
 681       CLEAR_PENDING_EXCEPTION;
 682       return JVMTI_ERROR_INTERNAL;
 683     }
 684 
 685     // Invoke the appendToClassPathForInstrumentation method - if the method
 686     // is not found it means the loader doesn't support adding to the class path
 687     // in the live phase.
 688     {
 689       JavaValue res(T_VOID);
 690       JavaCalls::call_special(&res,
 691                               loader,
 692                               loader->klass(),
 693                               vmSymbols::appendToClassPathForInstrumentation_name(),
 694                               vmSymbols::appendToClassPathForInstrumentation_signature(),
 695                               path,
 696                               THREAD);
 697       if (HAS_PENDING_EXCEPTION) {
 698         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 699         CLEAR_PENDING_EXCEPTION;
 700 
 701         if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
 702           return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
 703         } else {
 704           return JVMTI_ERROR_INTERNAL;
 705         }
 706       }
 707     }
 708 
 709     return JVMTI_ERROR_NONE;
 710   } else {
 711     return JVMTI_ERROR_WRONG_PHASE;
 712   }
 713 } /* end AddToSystemClassLoaderSearch */
 714 
 715   //
 716   // General functions
 717   //
 718 
 719 // phase_ptr - pre-checked for NULL
 720 jvmtiError
 721 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
 722   *phase_ptr = phase();
 723   return JVMTI_ERROR_NONE;
 724 } /* end GetPhase */
 725 
 726 
 727 jvmtiError
 728 JvmtiEnv::DisposeEnvironment() {
 729   dispose();
 730   return JVMTI_ERROR_NONE;
 731 } /* end DisposeEnvironment */
 732 
 733 
 734 // data - NULL is a valid value, must be checked
 735 jvmtiError
 736 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
 737   set_env_local_storage(data);
 738   return JVMTI_ERROR_NONE;
 739 } /* end SetEnvironmentLocalStorage */
 740 
 741 
 742 // data_ptr - pre-checked for NULL
 743 jvmtiError
 744 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
 745   *data_ptr = (void*)get_env_local_storage();
 746   return JVMTI_ERROR_NONE;
 747 } /* end GetEnvironmentLocalStorage */
 748 
 749 // version_ptr - pre-checked for NULL
 750 jvmtiError
 751 JvmtiEnv::GetVersionNumber(jint* version_ptr) {
 752   *version_ptr = JVMTI_VERSION;
 753   return JVMTI_ERROR_NONE;
 754 } /* end GetVersionNumber */
 755 
 756 
 757 // name_ptr - pre-checked for NULL
 758 jvmtiError
 759 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
 760   if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
 761     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 762   }
 763   const char *name = JvmtiUtil::error_name(error);
 764   if (name == NULL) {
 765     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 766   }
 767   size_t len = strlen(name) + 1;
 768   jvmtiError err = allocate(len, (unsigned char**)name_ptr);
 769   if (err == JVMTI_ERROR_NONE) {
 770     memcpy(*name_ptr, name, len);
 771   }
 772   return err;
 773 } /* end GetErrorName */
 774 
 775 
 776 jvmtiError
 777 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
 778   LogLevelType level = value == 0 ? LogLevel::Off : LogLevel::Info;
 779   switch (flag) {
 780   case JVMTI_VERBOSE_OTHER:
 781     // ignore
 782     break;
 783   case JVMTI_VERBOSE_CLASS:
 784     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));
 785     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));
 786     break;
 787   case JVMTI_VERBOSE_GC:
 788     if (value == 0) {
 789       LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(gc));
 790     } else {
 791       LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(gc));
 792     }
 793     break;
 794   case JVMTI_VERBOSE_JNI:
 795     PrintJNIResolving = value != 0;
 796     break;
 797   default:
 798     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 799   };
 800   return JVMTI_ERROR_NONE;
 801 } /* end SetVerboseFlag */
 802 
 803 
 804 // format_ptr - pre-checked for NULL
 805 jvmtiError
 806 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
 807   *format_ptr = JVMTI_JLOCATION_JVMBCI;
 808   return JVMTI_ERROR_NONE;
 809 } /* end GetJLocationFormat */
 810 
 811   //
 812   // Thread functions
 813   //
 814 
 815 // Threads_lock NOT held
 816 // thread - NOT pre-checked
 817 // thread_state_ptr - pre-checked for NULL
 818 jvmtiError
 819 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
 820   jint state;
 821   oop thread_oop;
 822   JavaThread* thr;

 823 
 824   if (thread == NULL) {
 825     thread_oop = JavaThread::current()->threadObj();
 826   } else {
 827     thread_oop = JNIHandles::resolve_external_guard(thread);
 828   }
 829 
 830   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
 831     return JVMTI_ERROR_INVALID_THREAD;
 832   }












 833 
 834   // get most state bits
 835   state = (jint)java_lang_Thread::get_thread_status(thread_oop);
 836 
 837   // add more state bits
 838   thr = java_lang_Thread::thread(thread_oop);
 839   if (thr != NULL) {
 840     JavaThreadState jts = thr->thread_state();
 841 
 842     if (thr->is_being_ext_suspended()) {
 843       state |= JVMTI_THREAD_STATE_SUSPENDED;
 844     }
 845     if (jts == _thread_in_native) {
 846       state |= JVMTI_THREAD_STATE_IN_NATIVE;
 847     }
 848     OSThread* osThread = thr->osthread();
 849     if (osThread != NULL && osThread->interrupted()) {
 850       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 851     }
 852   }
 853 
 854   *thread_state_ptr = state;
 855   return JVMTI_ERROR_NONE;
 856 } /* end GetThreadState */
 857 
 858 
 859 // thread_ptr - pre-checked for NULL
 860 jvmtiError
 861 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
 862   JavaThread* current_thread  = JavaThread::current();
 863   *thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());
 864   return JVMTI_ERROR_NONE;
 865 } /* end GetCurrentThread */
 866 
 867 
 868 // threads_count_ptr - pre-checked for NULL
 869 // threads_ptr - pre-checked for NULL
 870 jvmtiError
 871 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
 872   int nthreads        = 0;
 873   Handle *thread_objs = NULL;
 874   ResourceMark rm;
 875   HandleMark hm;
 876 
 877   // enumerate threads (including agent threads)
 878   ThreadsListEnumerator tle(Thread::current(), true);
 879   nthreads = tle.num_threads();
 880   *threads_count_ptr = nthreads;
 881 
 882   if (nthreads == 0) {
 883     *threads_ptr = NULL;
 884     return JVMTI_ERROR_NONE;
 885   }
 886 
 887   thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
 888   NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 889 
 890   for (int i=0; i < nthreads; i++) {
 891     thread_objs[i] = Handle(tle.get_threadObj(i));
 892   }
 893 
 894   // have to make global handles outside of Threads_lock
 895   jthread *jthreads  = new_jthreadArray(nthreads, thread_objs);
 896   NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
 897 
 898   *threads_ptr = jthreads;
 899   return JVMTI_ERROR_NONE;
 900 } /* end GetAllThreads */
 901 
 902 
 903 // Threads_lock NOT held, java_thread not protected by lock
 904 // java_thread - pre-checked
 905 jvmtiError
 906 JvmtiEnv::SuspendThread(JavaThread* java_thread) {
 907   // don't allow hidden thread suspend request.
 908   if (java_thread->is_hidden_from_external_view()) {
 909     return (JVMTI_ERROR_NONE);
 910   }
 911 
 912   {
 913     MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
 914     if (java_thread->is_external_suspend()) {
 915       // don't allow nested external suspend requests.
 916       return (JVMTI_ERROR_THREAD_SUSPENDED);
 917     }
 918     if (java_thread->is_exiting()) { // thread is in the process of exiting
 919       return (JVMTI_ERROR_THREAD_NOT_ALIVE);
 920     }
 921     java_thread->set_external_suspend();
 922   }
 923 
 924   if (!JvmtiSuspendControl::suspend(java_thread)) {
 925     // the thread was in the process of exiting
 926     return (JVMTI_ERROR_THREAD_NOT_ALIVE);
 927   }
 928   return JVMTI_ERROR_NONE;
 929 } /* end SuspendThread */
 930 
 931 
 932 // request_count - pre-checked to be greater than or equal to 0
 933 // request_list - pre-checked for NULL
 934 // results - pre-checked for NULL
 935 jvmtiError
 936 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
 937   int needSafepoint = 0;  // > 0 if we need a safepoint

 938   for (int i = 0; i < request_count; i++) {
 939     JavaThread *java_thread = get_JavaThread(request_list[i]);
 940     if (java_thread == NULL) {
 941       results[i] = JVMTI_ERROR_INVALID_THREAD;

 942       continue;
 943     }
 944     // the thread has not yet run or has exited (not on threads list)
 945     if (java_thread->threadObj() == NULL) {
 946       results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 947       continue;
 948     }
 949     if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) {
 950       results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 951       continue;
 952     }
 953     // don't allow hidden thread suspend request.
 954     if (java_thread->is_hidden_from_external_view()) {
 955       results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
 956       continue;
 957     }
 958 
 959     {
 960       MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
 961       if (java_thread->is_external_suspend()) {
 962         // don't allow nested external suspend requests.
 963         results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
 964         continue;
 965       }
 966       if (java_thread->is_exiting()) { // thread is in the process of exiting
 967         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 968         continue;
 969       }
 970       java_thread->set_external_suspend();
 971     }
 972     if (java_thread->thread_state() == _thread_in_native) {
 973       // We need to try and suspend native threads here. Threads in
 974       // other states will self-suspend on their next transition.
 975       if (!JvmtiSuspendControl::suspend(java_thread)) {
 976         // The thread was in the process of exiting. Force another
 977         // safepoint to make sure that this thread transitions.
 978         needSafepoint++;
 979         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 980         continue;
 981       }
 982     } else {
 983       needSafepoint++;
 984     }
 985     results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
 986   }
 987   if (needSafepoint > 0) {
 988     VM_ThreadsSuspendJVMTI tsj;
 989     VMThread::execute(&tsj);
 990   }
 991   // per-thread suspend results returned via results parameter
 992   return JVMTI_ERROR_NONE;
 993 } /* end SuspendThreadList */
 994 
 995 
 996 // Threads_lock NOT held, java_thread not protected by lock
 997 // java_thread - pre-checked
 998 jvmtiError
 999 JvmtiEnv::ResumeThread(JavaThread* java_thread) {
1000   // don't allow hidden thread resume request.
1001   if (java_thread->is_hidden_from_external_view()) {
1002     return JVMTI_ERROR_NONE;
1003   }
1004 
1005   if (!java_thread->is_being_ext_suspended()) {
1006     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1007   }
1008 
1009   if (!JvmtiSuspendControl::resume(java_thread)) {
1010     return JVMTI_ERROR_INTERNAL;
1011   }
1012   return JVMTI_ERROR_NONE;
1013 } /* end ResumeThread */
1014 
1015 
1016 // request_count - pre-checked to be greater than or equal to 0
1017 // request_list - pre-checked for NULL
1018 // results - pre-checked for NULL
1019 jvmtiError
1020 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {

1021   for (int i = 0; i < request_count; i++) {
1022     JavaThread *java_thread = get_JavaThread(request_list[i]);
1023     if (java_thread == NULL) {
1024       results[i] = JVMTI_ERROR_INVALID_THREAD;

1025       continue;
1026     }
1027     // don't allow hidden thread resume request.
1028     if (java_thread->is_hidden_from_external_view()) {
1029       results[i] = JVMTI_ERROR_NONE;  // indicate successful resume
1030       continue;
1031     }
1032     if (!java_thread->is_being_ext_suspended()) {
1033       results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1034       continue;
1035     }
1036 
1037     if (!JvmtiSuspendControl::resume(java_thread)) {
1038       results[i] = JVMTI_ERROR_INTERNAL;
1039       continue;
1040     }
1041 
1042     results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
1043   }
1044   // per-thread resume results returned via results parameter
1045   return JVMTI_ERROR_NONE;
1046 } /* end ResumeThreadList */
1047 
1048 
1049 // Threads_lock NOT held, java_thread not protected by lock
1050 // java_thread - pre-checked
1051 jvmtiError
1052 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
1053   oop e = JNIHandles::resolve_external_guard(exception);
1054   NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1055 
1056   JavaThread::send_async_exception(java_thread->threadObj(), e);
1057 
1058   return JVMTI_ERROR_NONE;
1059 
1060 } /* end StopThread */
1061 
1062 
1063 // Threads_lock NOT held
1064 // thread - NOT pre-checked
1065 jvmtiError
1066 JvmtiEnv::InterruptThread(jthread thread) {
1067   oop thread_oop = JNIHandles::resolve_external_guard(thread);
1068   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass()))
1069     return JVMTI_ERROR_INVALID_THREAD;
1070 
1071   JavaThread* current_thread  = JavaThread::current();






1072 
1073   // Todo: this is a duplicate of JVM_Interrupt; share code in future
1074   // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
1075   MutexLockerEx ml(current_thread->threadObj() == thread_oop ? NULL : Threads_lock);
1076   // We need to re-resolve the java_thread, since a GC might have happened during the
1077   // acquire of the lock
1078 
1079   JavaThread* java_thread = java_lang_Thread::thread(JNIHandles::resolve_external_guard(thread));
1080   NULL_CHECK(java_thread, JVMTI_ERROR_THREAD_NOT_ALIVE);
1081 
1082   Thread::interrupt(java_thread);
1083 
1084   return JVMTI_ERROR_NONE;
1085 } /* end InterruptThread */
1086 
1087 
1088 // Threads_lock NOT held
1089 // thread - NOT pre-checked
1090 // info_ptr - pre-checked for NULL
1091 jvmtiError
1092 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1093   ResourceMark rm;
1094   HandleMark hm;
1095 
1096   JavaThread* current_thread = JavaThread::current();

1097 
1098   // if thread is NULL the current thread is used
1099   oop thread_oop;
1100   if (thread == NULL) {
1101     thread_oop = current_thread->threadObj();



1102   } else {
1103     thread_oop = JNIHandles::resolve_external_guard(thread);







1104   }
1105   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass()))
1106     return JVMTI_ERROR_INVALID_THREAD;

1107 
1108   Handle thread_obj(current_thread, thread_oop);
1109   Handle name;
1110   ThreadPriority priority;
1111   Handle     thread_group;
1112   Handle context_class_loader;
1113   bool          is_daemon;
1114 
1115   { MutexLocker mu(Threads_lock);
1116 
1117     name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1118     priority = java_lang_Thread::priority(thread_obj());
1119     thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1120     is_daemon = java_lang_Thread::is_daemon(thread_obj());
1121 
1122     oop loader = java_lang_Thread::context_class_loader(thread_obj());
1123     context_class_loader = Handle(current_thread, loader);
1124   }
1125   { const char *n;
1126 
1127     if (name() != NULL) {
1128       n = java_lang_String::as_utf8_string(name());
1129     } else {
1130       int utf8_length = 0;
1131       n = UNICODE::as_utf8((jchar*) NULL, utf8_length);
1132     }
1133 
1134     info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1135     if (info_ptr->name == NULL)
1136       return JVMTI_ERROR_OUT_OF_MEMORY;
1137 
1138     strcpy(info_ptr->name, n);
1139   }
1140   info_ptr->is_daemon = is_daemon;
1141   info_ptr->priority  = priority;
1142 
1143   info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
1144                                      jni_reference(context_class_loader);
1145   info_ptr->thread_group = jni_reference(thread_group);
1146 
1147   return JVMTI_ERROR_NONE;
1148 } /* end GetThreadInfo */
1149 
1150 
1151 // Threads_lock NOT held, java_thread not protected by lock
1152 // java_thread - pre-checked
1153 // owned_monitor_count_ptr - pre-checked for NULL
1154 // owned_monitors_ptr - pre-checked for NULL
1155 jvmtiError
1156 JvmtiEnv::GetOwnedMonitorInfo(JavaThread* java_thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1157   jvmtiError err = JVMTI_ERROR_NONE;
1158   JavaThread* calling_thread = JavaThread::current();
1159 
1160   // growable array of jvmti monitors info on the C-heap
1161   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1162       new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1163 
1164   // It is only safe to perform the direct operation on the current
1165   // thread. All other usage needs to use a vm-safepoint-op for safety.
1166   if (java_thread == calling_thread) {
1167     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1168   } else {
1169     // JVMTI get monitors info at safepoint. Do not require target thread to
1170     // be suspended.
1171     VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1172     VMThread::execute(&op);
1173     err = op.result();
1174   }
1175   jint owned_monitor_count = owned_monitors_list->length();
1176   if (err == JVMTI_ERROR_NONE) {
1177     if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1178                       (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1179       // copy into the returned array
1180       for (int i = 0; i < owned_monitor_count; i++) {
1181         (*owned_monitors_ptr)[i] =
1182           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1183       }
1184       *owned_monitor_count_ptr = owned_monitor_count;
1185     }
1186   }
1187   // clean up.
1188   for (int i = 0; i < owned_monitor_count; i++) {
1189     deallocate((unsigned char*)owned_monitors_list->at(i));
1190   }
1191   delete owned_monitors_list;
1192 
1193   return err;
1194 } /* end GetOwnedMonitorInfo */
1195 
1196 
1197 // Threads_lock NOT held, java_thread not protected by lock
1198 // java_thread - pre-checked
1199 // monitor_info_count_ptr - pre-checked for NULL
1200 // monitor_info_ptr - pre-checked for NULL
1201 jvmtiError
1202 JvmtiEnv::GetOwnedMonitorStackDepthInfo(JavaThread* java_thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1203   jvmtiError err = JVMTI_ERROR_NONE;
1204   JavaThread* calling_thread  = JavaThread::current();
1205 
1206   // growable array of jvmti monitors info on the C-heap
1207   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1208          new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1209 
1210   // It is only safe to perform the direct operation on the current
1211   // thread. All other usage needs to use a vm-safepoint-op for safety.
1212   if (java_thread == calling_thread) {
1213     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1214   } else {
1215     // JVMTI get owned monitors info at safepoint. Do not require target thread to
1216     // be suspended.
1217     VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1218     VMThread::execute(&op);
1219     err = op.result();
1220   }
1221 
1222   jint owned_monitor_count = owned_monitors_list->length();
1223   if (err == JVMTI_ERROR_NONE) {
1224     if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1225                       (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1226       // copy to output array.
1227       for (int i = 0; i < owned_monitor_count; i++) {
1228         (*monitor_info_ptr)[i].monitor =
1229           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1230         (*monitor_info_ptr)[i].stack_depth =
1231           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1232       }
1233     }
1234     *monitor_info_count_ptr = owned_monitor_count;
1235   }
1236 
1237   // clean up.
1238   for (int i = 0; i < owned_monitor_count; i++) {
1239     deallocate((unsigned char*)owned_monitors_list->at(i));
1240   }
1241   delete owned_monitors_list;
1242 
1243   return err;
1244 } /* end GetOwnedMonitorStackDepthInfo */
1245 
1246 
1247 // Threads_lock NOT held, java_thread not protected by lock
1248 // java_thread - pre-checked
1249 // monitor_ptr - pre-checked for NULL
1250 jvmtiError
1251 JvmtiEnv::GetCurrentContendedMonitor(JavaThread* java_thread, jobject* monitor_ptr) {
1252   jvmtiError err = JVMTI_ERROR_NONE;
1253   JavaThread* calling_thread  = JavaThread::current();
1254 
1255   // It is only safe to perform the direct operation on the current
1256   // thread. All other usage needs to use a vm-safepoint-op for safety.
1257   if (java_thread == calling_thread) {
1258     err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
1259   } else {
1260     // get contended monitor information at safepoint.
1261     VM_GetCurrentContendedMonitor op(this, calling_thread, java_thread, monitor_ptr);
1262     VMThread::execute(&op);
1263     err = op.result();
1264   }
1265   return err;
1266 } /* end GetCurrentContendedMonitor */
1267 
1268 
1269 // Threads_lock NOT held
1270 // thread - NOT pre-checked
1271 // proc - pre-checked for NULL
1272 // arg - NULL is a valid value, must be checked
1273 jvmtiError
1274 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1275   oop thread_oop = JNIHandles::resolve_external_guard(thread);
1276   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
















1277     return JVMTI_ERROR_INVALID_THREAD;
1278   }

1279   if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1280     return JVMTI_ERROR_INVALID_PRIORITY;
1281   }
1282 
1283   //Thread-self
1284   JavaThread* current_thread = JavaThread::current();
1285 
1286   Handle thread_hndl(current_thread, thread_oop);
1287   {
1288     MutexLocker mu(Threads_lock); // grab Threads_lock
1289 
1290     JvmtiAgentThread *new_thread = new JvmtiAgentThread(this, proc, arg);
1291 
1292     // At this point it may be possible that no osthread was created for the
1293     // JavaThread due to lack of memory.
1294     if (new_thread == NULL || new_thread->osthread() == NULL) {
1295       if (new_thread) delete new_thread;


1296       return JVMTI_ERROR_OUT_OF_MEMORY;
1297     }
1298 
1299     java_lang_Thread::set_thread(thread_hndl(), new_thread);
1300     java_lang_Thread::set_priority(thread_hndl(), (ThreadPriority)priority);
1301     java_lang_Thread::set_daemon(thread_hndl());
1302 
1303     new_thread->set_threadObj(thread_hndl());
1304     Threads::add(new_thread);
1305     Thread::start(new_thread);
1306   } // unlock Threads_lock
1307 
1308   return JVMTI_ERROR_NONE;
1309 } /* end RunAgentThread */
1310 
1311   //
1312   // Thread Group functions
1313   //
1314 
1315 // group_count_ptr - pre-checked for NULL
1316 // groups_ptr - pre-checked for NULL
1317 jvmtiError
1318 JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
1319   JavaThread* current_thread = JavaThread::current();
1320 
1321   // Only one top level thread group now.
1322   *group_count_ptr = 1;
1323 
1324   // Allocate memory to store global-refs to the thread groups.
1325   // Assume this area is freed by caller.
1326   *groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
1327 
1328   NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
1329 
1330   // Convert oop to Handle, then convert Handle to global-ref.
1331   {
1332     HandleMark hm(current_thread);
1333     Handle system_thread_group(current_thread, Universe::system_thread_group());
1334     *groups_ptr[0] = jni_reference(system_thread_group);
1335   }
1336 
1337   return JVMTI_ERROR_NONE;
1338 } /* end GetTopThreadGroups */
1339 
1340 
1341 // info_ptr - pre-checked for NULL
1342 jvmtiError
1343 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1344   ResourceMark rm;
1345   HandleMark hm;
1346 
1347   JavaThread* current_thread = JavaThread::current();
1348 
1349   Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1350   NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1351 
1352   const char* name;
1353   Handle parent_group;
1354   bool is_daemon;
1355   ThreadPriority max_priority;
1356 
1357   { MutexLocker mu(Threads_lock);
1358 
1359     name         = java_lang_ThreadGroup::name(group_obj());
1360     parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
1361     is_daemon    = java_lang_ThreadGroup::is_daemon(group_obj());
1362     max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1363   }
1364 
1365   info_ptr->is_daemon    = is_daemon;
1366   info_ptr->max_priority = max_priority;
1367   info_ptr->parent       = jni_reference(parent_group);
1368 
1369   if (name != NULL) {
1370     info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1371     NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1372     strcpy(info_ptr->name, name);
1373   } else {
1374     info_ptr->name = NULL;
1375   }
1376 
1377   return JVMTI_ERROR_NONE;
1378 } /* end GetThreadGroupInfo */
1379 
1380 
1381 // thread_count_ptr - pre-checked for NULL
1382 // threads_ptr - pre-checked for NULL
1383 // group_count_ptr - pre-checked for NULL
1384 // groups_ptr - pre-checked for NULL
1385 jvmtiError
1386 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1387   JavaThread* current_thread = JavaThread::current();
1388   oop group_obj = (oop) JNIHandles::resolve_external_guard(group);
1389   NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1390 
1391   Handle *thread_objs = NULL;
1392   Handle *group_objs  = NULL;
1393   int nthreads = 0;
1394   int ngroups = 0;
1395   int hidden_threads = 0;
1396 
1397   ResourceMark rm;
1398   HandleMark hm;
1399 
1400   Handle group_hdl(current_thread, group_obj);
1401 
1402   { MutexLocker mu(Threads_lock);

1403 
1404     nthreads = java_lang_ThreadGroup::nthreads(group_hdl());
1405     ngroups  = java_lang_ThreadGroup::ngroups(group_hdl());
1406 
1407     if (nthreads > 0) {

1408       objArrayOop threads = java_lang_ThreadGroup::threads(group_hdl());
1409       assert(nthreads <= threads->length(), "too many threads");
1410       thread_objs = NEW_RESOURCE_ARRAY(Handle,nthreads);
1411       for (int i=0, j=0; i<nthreads; i++) {
1412         oop thread_obj = threads->obj_at(i);
1413         assert(thread_obj != NULL, "thread_obj is NULL");
1414         JavaThread *javathread = java_lang_Thread::thread(thread_obj);




1415         // Filter out hidden java threads.
1416         if (javathread != NULL && javathread->is_hidden_from_external_view()) {
1417           hidden_threads++;
1418           continue;
1419         }











1420         thread_objs[j++] = Handle(current_thread, thread_obj);
1421       }
1422       nthreads -= hidden_threads;
1423     }

1424     if (ngroups > 0) {
1425       objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
1426       assert(ngroups <= groups->length(), "too many threads");
1427       group_objs = NEW_RESOURCE_ARRAY(Handle,ngroups);
1428       for (int i=0; i<ngroups; i++) {
1429         oop group_obj = groups->obj_at(i);
1430         assert(group_obj != NULL, "group_obj != NULL");
1431         group_objs[i] = Handle(current_thread, group_obj);
1432       }
1433     }
1434   }
1435 
1436   // have to make global handles outside of Threads_lock
1437   *group_count_ptr  = ngroups;
1438   *thread_count_ptr = nthreads;
1439   *threads_ptr     = new_jthreadArray(nthreads, thread_objs);
1440   *groups_ptr      = new_jthreadGroupArray(ngroups, group_objs);
1441   if ((nthreads > 0) && (*threads_ptr == NULL)) {
1442     return JVMTI_ERROR_OUT_OF_MEMORY;
1443   }
1444   if ((ngroups > 0) && (*groups_ptr == NULL)) {
1445     return JVMTI_ERROR_OUT_OF_MEMORY;
1446   }
1447 
1448   return JVMTI_ERROR_NONE;
1449 } /* end GetThreadGroupChildren */
1450 
1451 
1452   //
1453   // Stack Frame functions
1454   //
1455 
1456 // Threads_lock NOT held, java_thread not protected by lock
1457 // java_thread - pre-checked
1458 // max_frame_count - pre-checked to be greater than or equal to 0
1459 // frame_buffer - pre-checked for NULL
1460 // count_ptr - pre-checked for NULL
1461 jvmtiError
1462 JvmtiEnv::GetStackTrace(JavaThread* java_thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1463   jvmtiError err = JVMTI_ERROR_NONE;
1464 
1465   // It is only safe to perform the direct operation on the current
1466   // thread. All other usage needs to use a vm-safepoint-op for safety.
1467   if (java_thread == JavaThread::current()) {
1468     err = get_stack_trace(java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1469   } else {
1470     // JVMTI get stack trace at safepoint. Do not require target thread to
1471     // be suspended.
1472     VM_GetStackTrace op(this, java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1473     VMThread::execute(&op);
1474     err = op.result();
1475   }
1476 
1477   return err;
1478 } /* end GetStackTrace */
1479 
1480 
1481 // max_frame_count - pre-checked to be greater than or equal to 0
1482 // stack_info_ptr - pre-checked for NULL
1483 // thread_count_ptr - pre-checked for NULL
1484 jvmtiError
1485 JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
1486   jvmtiError err = JVMTI_ERROR_NONE;
1487   JavaThread* calling_thread = JavaThread::current();
1488 
1489   // JVMTI get stack traces at safepoint.
1490   VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1491   VMThread::execute(&op);
1492   *thread_count_ptr = op.final_thread_count();
1493   *stack_info_ptr = op.stack_info();
1494   err = op.result();
1495   return err;
1496 } /* end GetAllStackTraces */
1497 
1498 
1499 // thread_count - pre-checked to be greater than or equal to 0
1500 // thread_list - pre-checked for NULL
1501 // max_frame_count - pre-checked to be greater than or equal to 0
1502 // stack_info_ptr - pre-checked for NULL
1503 jvmtiError
1504 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1505   jvmtiError err = JVMTI_ERROR_NONE;
1506   // JVMTI get stack traces at safepoint.
1507   VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1508   VMThread::execute(&op);
1509   err = op.result();
1510   if (err == JVMTI_ERROR_NONE) {
1511     *stack_info_ptr = op.stack_info();
1512   }
1513   return err;
1514 } /* end GetThreadListStackTraces */
1515 
1516 
1517 // Threads_lock NOT held, java_thread not protected by lock
1518 // java_thread - pre-checked
1519 // count_ptr - pre-checked for NULL
1520 jvmtiError
1521 JvmtiEnv::GetFrameCount(JavaThread* java_thread, jint* count_ptr) {
1522   jvmtiError err = JVMTI_ERROR_NONE;
1523 
1524   // retrieve or create JvmtiThreadState.
1525   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1526   if (state == NULL) {
1527     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1528   }
1529 
1530   // It is only safe to perform the direct operation on the current
1531   // thread. All other usage needs to use a vm-safepoint-op for safety.
1532   if (java_thread == JavaThread::current()) {
1533     err = get_frame_count(state, count_ptr);
1534   } else {
1535     // get java stack frame count at safepoint.
1536     VM_GetFrameCount op(this, state, count_ptr);
1537     VMThread::execute(&op);
1538     err = op.result();
1539   }
1540   return err;
1541 } /* end GetFrameCount */
1542 
1543 
1544 // Threads_lock NOT held, java_thread not protected by lock
1545 // java_thread - pre-checked
1546 jvmtiError
1547 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1548   JavaThread* current_thread  = JavaThread::current();
1549   HandleMark hm(current_thread);
1550   uint32_t debug_bits = 0;
1551 
1552   // retrieve or create the state
1553   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1554   if (state == NULL) {
1555     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1556   }
1557 
1558   // Check if java_thread is fully suspended
1559   if (!is_thread_fully_suspended(java_thread, true /* wait for suspend completion */, &debug_bits)) {
1560     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1561   }
1562   // Check to see if a PopFrame was already in progress
1563   if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
1564     // Probably possible for JVMTI clients to trigger this, but the
1565     // JPDA backend shouldn't allow this to happen
1566     return JVMTI_ERROR_INTERNAL;
1567   }
1568 
1569   {
1570     // Was workaround bug
1571     //    4812902: popFrame hangs if the method is waiting at a synchronize
1572     // Catch this condition and return an error to avoid hanging.
1573     // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
1574     OSThread* osThread = java_thread->osthread();
1575     if (osThread->get_state() == MONITOR_WAIT) {
1576       return JVMTI_ERROR_OPAQUE_FRAME;
1577     }
1578   }
1579 
1580   {
1581     ResourceMark rm(current_thread);
1582     // Check if there are more than one Java frame in this thread, that the top two frames
1583     // are Java (not native) frames, and that there is no intervening VM frame
1584     int frame_count = 0;
1585     bool is_interpreted[2];
1586     intptr_t *frame_sp[2];
1587     // The 2-nd arg of constructor is needed to stop iterating at java entry frame.
1588     for (vframeStream vfs(java_thread, true); !vfs.at_end(); vfs.next()) {
1589       methodHandle mh(current_thread, vfs.method());
1590       if (mh->is_native()) return(JVMTI_ERROR_OPAQUE_FRAME);
1591       is_interpreted[frame_count] = vfs.is_interpreted_frame();
1592       frame_sp[frame_count] = vfs.frame_id();
1593       if (++frame_count > 1) break;
1594     }
1595     if (frame_count < 2)  {
1596       // We haven't found two adjacent non-native Java frames on the top.
1597       // There can be two situations here:
1598       //  1. There are no more java frames
1599       //  2. Two top java frames are separated by non-java native frames
1600       if(vframeFor(java_thread, 1) == NULL) {
1601         return JVMTI_ERROR_NO_MORE_FRAMES;
1602       } else {
1603         // Intervening non-java native or VM frames separate java frames.
1604         // Current implementation does not support this. See bug #5031735.
1605         // In theory it is possible to pop frames in such cases.
1606         return JVMTI_ERROR_OPAQUE_FRAME;
1607       }
1608     }
1609 
1610     // If any of the top 2 frames is a compiled one, need to deoptimize it
1611     for (int i = 0; i < 2; i++) {
1612       if (!is_interpreted[i]) {
1613         Deoptimization::deoptimize_frame(java_thread, frame_sp[i]);
1614       }
1615     }
1616 
1617     // Update the thread state to reflect that the top frame is popped
1618     // so that cur_stack_depth is maintained properly and all frameIDs
1619     // are invalidated.
1620     // The current frame will be popped later when the suspended thread
1621     // is resumed and right before returning from VM to Java.
1622     // (see call_VM_base() in assembler_<cpu>.cpp).
1623 
1624     // It's fine to update the thread state here because no JVMTI events
1625     // shall be posted for this PopFrame.
1626 
1627     // It is only safe to perform the direct operation on the current
1628     // thread. All other usage needs to use a vm-safepoint-op for safety.
1629     if (java_thread == JavaThread::current()) {
1630       state->update_for_pop_top_frame();
1631     } else {
1632       VM_UpdateForPopTopFrame op(state);
1633       VMThread::execute(&op);
1634       jvmtiError err = op.result();
1635       if (err != JVMTI_ERROR_NONE) {
1636         return err;
1637       }
1638     }
1639 
1640     java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
1641     // Set pending step flag for this popframe and it is cleared when next
1642     // step event is posted.
1643     state->set_pending_step_for_popframe();
1644   }
1645 
1646   return JVMTI_ERROR_NONE;
1647 } /* end PopFrame */
1648 
1649 
1650 // Threads_lock NOT held, java_thread not protected by lock
1651 // java_thread - pre-checked
1652 // java_thread - unchecked
1653 // depth - pre-checked as non-negative
1654 // method_ptr - pre-checked for NULL
1655 // location_ptr - pre-checked for NULL
1656 jvmtiError
1657 JvmtiEnv::GetFrameLocation(JavaThread* java_thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1658   jvmtiError err = JVMTI_ERROR_NONE;
1659 
1660   // It is only safe to perform the direct operation on the current
1661   // thread. All other usage needs to use a vm-safepoint-op for safety.
1662   if (java_thread == JavaThread::current()) {
1663     err = get_frame_location(java_thread, depth, method_ptr, location_ptr);
1664   } else {
1665     // JVMTI get java stack frame location at safepoint.
1666     VM_GetFrameLocation op(this, java_thread, depth, method_ptr, location_ptr);
1667     VMThread::execute(&op);
1668     err = op.result();
1669   }
1670   return err;
1671 } /* end GetFrameLocation */
1672 
1673 
1674 // Threads_lock NOT held, java_thread not protected by lock
1675 // java_thread - pre-checked
1676 // java_thread - unchecked
1677 // depth - pre-checked as non-negative
1678 jvmtiError
1679 JvmtiEnv::NotifyFramePop(JavaThread* java_thread, jint depth) {
1680   jvmtiError err = JVMTI_ERROR_NONE;
1681   ResourceMark rm;
1682   uint32_t debug_bits = 0;
1683 
1684   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
1685   if (state == NULL) {
1686     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1687   }
1688 
1689   if (!JvmtiEnv::is_thread_fully_suspended(java_thread, true, &debug_bits)) {
1690       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1691   }
1692 
1693   if (TraceJVMTICalls) {
1694     JvmtiSuspendControl::print();
1695   }
1696 
1697   vframe *vf = vframeFor(java_thread, depth);
1698   if (vf == NULL) {
1699     return JVMTI_ERROR_NO_MORE_FRAMES;
1700   }
1701 
1702   if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
1703     return JVMTI_ERROR_OPAQUE_FRAME;
1704   }
1705 
1706   assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
1707 
1708   // It is only safe to perform the direct operation on the current
1709   // thread. All other usage needs to use a vm-safepoint-op for safety.
1710   if (java_thread == JavaThread::current()) {
1711     int frame_number = state->count_frames() - depth;
1712     state->env_thread_state(this)->set_frame_pop(frame_number);
1713   } else {
1714     VM_SetFramePop op(this, state, depth);
1715     VMThread::execute(&op);
1716     err = op.result();
1717   }
1718   return err;
1719 } /* end NotifyFramePop */
1720 
1721 
1722   //
1723   // Force Early Return functions
1724   //
1725 
1726 // Threads_lock NOT held, java_thread not protected by lock
1727 // java_thread - pre-checked
1728 jvmtiError
1729 JvmtiEnv::ForceEarlyReturnObject(JavaThread* java_thread, jobject value) {
1730   jvalue val;
1731   val.l = value;
1732   return force_early_return(java_thread, val, atos);
1733 } /* end ForceEarlyReturnObject */
1734 
1735 
1736 // Threads_lock NOT held, java_thread not protected by lock
1737 // java_thread - pre-checked
1738 jvmtiError
1739 JvmtiEnv::ForceEarlyReturnInt(JavaThread* java_thread, jint value) {
1740   jvalue val;
1741   val.i = value;
1742   return force_early_return(java_thread, val, itos);
1743 } /* end ForceEarlyReturnInt */
1744 
1745 
1746 // Threads_lock NOT held, java_thread not protected by lock
1747 // java_thread - pre-checked
1748 jvmtiError
1749 JvmtiEnv::ForceEarlyReturnLong(JavaThread* java_thread, jlong value) {
1750   jvalue val;
1751   val.j = value;
1752   return force_early_return(java_thread, val, ltos);
1753 } /* end ForceEarlyReturnLong */
1754 
1755 
1756 // Threads_lock NOT held, java_thread not protected by lock
1757 // java_thread - pre-checked
1758 jvmtiError
1759 JvmtiEnv::ForceEarlyReturnFloat(JavaThread* java_thread, jfloat value) {
1760   jvalue val;
1761   val.f = value;
1762   return force_early_return(java_thread, val, ftos);
1763 } /* end ForceEarlyReturnFloat */
1764 
1765 
1766 // Threads_lock NOT held, java_thread not protected by lock
1767 // java_thread - pre-checked
1768 jvmtiError
1769 JvmtiEnv::ForceEarlyReturnDouble(JavaThread* java_thread, jdouble value) {
1770   jvalue val;
1771   val.d = value;
1772   return force_early_return(java_thread, val, dtos);
1773 } /* end ForceEarlyReturnDouble */
1774 
1775 
1776 // Threads_lock NOT held, java_thread not protected by lock
1777 // java_thread - pre-checked
1778 jvmtiError
1779 JvmtiEnv::ForceEarlyReturnVoid(JavaThread* java_thread) {
1780   jvalue val;
1781   val.j = 0L;
1782   return force_early_return(java_thread, val, vtos);
1783 } /* end ForceEarlyReturnVoid */
1784 
1785 
1786   //
1787   // Heap functions
1788   //
1789 
1790 // klass - NULL is a valid value, must be checked
1791 // initial_object - NULL is a valid value, must be checked
1792 // callbacks - pre-checked for NULL
1793 // user_data - NULL is a valid value, must be checked
1794 jvmtiError
1795 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1796   // check klass if provided
1797   Klass* k = NULL;
1798   if (klass != NULL) {
1799     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1800     if (k_mirror == NULL) {
1801       return JVMTI_ERROR_INVALID_CLASS;
1802     }
1803     if (java_lang_Class::is_primitive(k_mirror)) {
1804       return JVMTI_ERROR_NONE;
1805     }
1806     k = java_lang_Class::as_Klass(k_mirror);
1807     if (klass == NULL) {
1808       return JVMTI_ERROR_INVALID_CLASS;
1809     }
1810   }
1811 
1812   if (initial_object != NULL) {
1813     oop init_obj = JNIHandles::resolve_external_guard(initial_object);
1814     if (init_obj == NULL) {
1815       return JVMTI_ERROR_INVALID_OBJECT;
1816     }
1817   }
1818 
1819   Thread *thread = Thread::current();
1820   HandleMark hm(thread);
1821 
1822   TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
1823   JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, k, initial_object, callbacks, user_data);
1824   return JVMTI_ERROR_NONE;
1825 } /* end FollowReferences */
1826 
1827 
1828 // klass - NULL is a valid value, must be checked
1829 // callbacks - pre-checked for NULL
1830 // user_data - NULL is a valid value, must be checked
1831 jvmtiError
1832 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1833   // check klass if provided
1834   Klass* k = NULL;
1835   if (klass != NULL) {
1836     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1837     if (k_mirror == NULL) {
1838       return JVMTI_ERROR_INVALID_CLASS;
1839     }
1840     if (java_lang_Class::is_primitive(k_mirror)) {
1841       return JVMTI_ERROR_NONE;
1842     }
1843     k = java_lang_Class::as_Klass(k_mirror);
1844     if (k == NULL) {
1845       return JVMTI_ERROR_INVALID_CLASS;
1846     }
1847   }
1848 
1849   TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1850   JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, k, callbacks, user_data);
1851   return JVMTI_ERROR_NONE;
1852 } /* end IterateThroughHeap */
1853 
1854 
1855 // tag_ptr - pre-checked for NULL
1856 jvmtiError
1857 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1858   oop o = JNIHandles::resolve_external_guard(object);
1859   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1860   *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1861   return JVMTI_ERROR_NONE;
1862 } /* end GetTag */
1863 
1864 
1865 jvmtiError
1866 JvmtiEnv::SetTag(jobject object, jlong tag) {
1867   oop o = JNIHandles::resolve_external_guard(object);
1868   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1869   JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1870   return JVMTI_ERROR_NONE;
1871 } /* end SetTag */
1872 
1873 
1874 // tag_count - pre-checked to be greater than or equal to 0
1875 // tags - pre-checked for NULL
1876 // count_ptr - pre-checked for NULL
1877 // object_result_ptr - NULL is a valid value, must be checked
1878 // tag_result_ptr - NULL is a valid value, must be checked
1879 jvmtiError
1880 JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
1881   TraceTime t("GetObjectsWithTags", TRACETIME_LOG(Debug, jvmti, objecttagging));
1882   return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
1883 } /* end GetObjectsWithTags */
1884 
1885 
1886 jvmtiError
1887 JvmtiEnv::ForceGarbageCollection() {
1888   Universe::heap()->collect(GCCause::_jvmti_force_gc);
1889   return JVMTI_ERROR_NONE;
1890 } /* end ForceGarbageCollection */
1891 
1892 
1893   //
1894   // Heap (1.0) functions
1895   //
1896 
1897 // object_reference_callback - pre-checked for NULL
1898 // user_data - NULL is a valid value, must be checked
1899 jvmtiError
1900 JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
1901   oop o = JNIHandles::resolve_external_guard(object);
1902   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1903   JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
1904   return JVMTI_ERROR_NONE;
1905 } /* end IterateOverObjectsReachableFromObject */
1906 
1907 
1908 // heap_root_callback - NULL is a valid value, must be checked
1909 // stack_ref_callback - NULL is a valid value, must be checked
1910 // object_ref_callback - NULL is a valid value, must be checked
1911 // user_data - NULL is a valid value, must be checked
1912 jvmtiError
1913 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
1914   TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
1915   JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
1916   return JVMTI_ERROR_NONE;
1917 } /* end IterateOverReachableObjects */
1918 
1919 
1920 // heap_object_callback - pre-checked for NULL
1921 // user_data - NULL is a valid value, must be checked
1922 jvmtiError
1923 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1924   TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1925   Thread *thread = Thread::current();
1926   HandleMark hm(thread);
1927   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, NULL, heap_object_callback, user_data);
1928   return JVMTI_ERROR_NONE;
1929 } /* end IterateOverHeap */
1930 
1931 
1932 // k_mirror - may be primitive, this must be checked
1933 // heap_object_callback - pre-checked for NULL
1934 // user_data - NULL is a valid value, must be checked
1935 jvmtiError
1936 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1937   if (java_lang_Class::is_primitive(k_mirror)) {
1938     // DO PRIMITIVE CLASS PROCESSING
1939     return JVMTI_ERROR_NONE;
1940   }
1941   Klass* klass = java_lang_Class::as_Klass(k_mirror);
1942   if (klass == NULL) {
1943     return JVMTI_ERROR_INVALID_CLASS;
1944   }
1945   TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
1946   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
1947   return JVMTI_ERROR_NONE;
1948 } /* end IterateOverInstancesOfClass */
1949 
1950 
1951   //
1952   // Local Variable functions
1953   //
1954 
1955 // Threads_lock NOT held, java_thread not protected by lock
1956 // java_thread - pre-checked
1957 // java_thread - unchecked
1958 // depth - pre-checked as non-negative
1959 // value_ptr - pre-checked for NULL
1960 jvmtiError
1961 JvmtiEnv::GetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject* value_ptr) {
1962   JavaThread* current_thread = JavaThread::current();
1963   // rm object is created to clean up the javaVFrame created in
1964   // doit_prologue(), but after doit() is finished with it.
1965   ResourceMark rm(current_thread);
1966 
1967   VM_GetOrSetLocal op(java_thread, current_thread, depth, slot);
1968   VMThread::execute(&op);
1969   jvmtiError err = op.result();
1970   if (err != JVMTI_ERROR_NONE) {
1971     return err;
1972   } else {
1973     *value_ptr = op.value().l;
1974     return JVMTI_ERROR_NONE;
1975   }
1976 } /* end GetLocalObject */
1977 
1978 // Threads_lock NOT held, java_thread not protected by lock
1979 // java_thread - pre-checked
1980 // java_thread - unchecked
1981 // depth - pre-checked as non-negative
1982 // value - pre-checked for NULL
1983 jvmtiError
1984 JvmtiEnv::GetLocalInstance(JavaThread* java_thread, jint depth, jobject* value_ptr){
1985   JavaThread* current_thread = JavaThread::current();
1986   // rm object is created to clean up the javaVFrame created in
1987   // doit_prologue(), but after doit() is finished with it.
1988   ResourceMark rm(current_thread);
1989 
1990   VM_GetReceiver op(java_thread, current_thread, depth);
1991   VMThread::execute(&op);
1992   jvmtiError err = op.result();
1993   if (err != JVMTI_ERROR_NONE) {
1994     return err;
1995   } else {
1996     *value_ptr = op.value().l;
1997     return JVMTI_ERROR_NONE;
1998   }
1999 } /* end GetLocalInstance */
2000 
2001 
2002 // Threads_lock NOT held, java_thread not protected by lock
2003 // java_thread - pre-checked
2004 // java_thread - unchecked
2005 // depth - pre-checked as non-negative
2006 // value_ptr - pre-checked for NULL
2007 jvmtiError
2008 JvmtiEnv::GetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint* value_ptr) {
2009   // rm object is created to clean up the javaVFrame created in
2010   // doit_prologue(), but after doit() is finished with it.
2011   ResourceMark rm;
2012 
2013   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT);
2014   VMThread::execute(&op);
2015   *value_ptr = op.value().i;
2016   return op.result();
2017 } /* end GetLocalInt */
2018 
2019 
2020 // Threads_lock NOT held, java_thread not protected by lock
2021 // java_thread - pre-checked
2022 // java_thread - unchecked
2023 // depth - pre-checked as non-negative
2024 // value_ptr - pre-checked for NULL
2025 jvmtiError
2026 JvmtiEnv::GetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong* value_ptr) {
2027   // rm object is created to clean up the javaVFrame created in
2028   // doit_prologue(), but after doit() is finished with it.
2029   ResourceMark rm;
2030 
2031   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG);
2032   VMThread::execute(&op);
2033   *value_ptr = op.value().j;
2034   return op.result();
2035 } /* end GetLocalLong */
2036 
2037 
2038 // Threads_lock NOT held, java_thread not protected by lock
2039 // java_thread - pre-checked
2040 // java_thread - unchecked
2041 // depth - pre-checked as non-negative
2042 // value_ptr - pre-checked for NULL
2043 jvmtiError
2044 JvmtiEnv::GetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat* value_ptr) {
2045   // rm object is created to clean up the javaVFrame created in
2046   // doit_prologue(), but after doit() is finished with it.
2047   ResourceMark rm;
2048 
2049   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT);
2050   VMThread::execute(&op);
2051   *value_ptr = op.value().f;
2052   return op.result();
2053 } /* end GetLocalFloat */
2054 
2055 
2056 // Threads_lock NOT held, java_thread not protected by lock
2057 // java_thread - pre-checked
2058 // java_thread - unchecked
2059 // depth - pre-checked as non-negative
2060 // value_ptr - pre-checked for NULL
2061 jvmtiError
2062 JvmtiEnv::GetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble* value_ptr) {
2063   // rm object is created to clean up the javaVFrame created in
2064   // doit_prologue(), but after doit() is finished with it.
2065   ResourceMark rm;
2066 
2067   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE);
2068   VMThread::execute(&op);
2069   *value_ptr = op.value().d;
2070   return op.result();
2071 } /* end GetLocalDouble */
2072 
2073 
2074 // Threads_lock NOT held, java_thread not protected by lock
2075 // java_thread - pre-checked
2076 // java_thread - unchecked
2077 // depth - pre-checked as non-negative
2078 jvmtiError
2079 JvmtiEnv::SetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject value) {
2080   // rm object is created to clean up the javaVFrame created in
2081   // doit_prologue(), but after doit() is finished with it.
2082   ResourceMark rm;
2083   jvalue val;
2084   val.l = value;
2085   VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val);
2086   VMThread::execute(&op);
2087   return op.result();
2088 } /* end SetLocalObject */
2089 
2090 
2091 // Threads_lock NOT held, java_thread not protected by lock
2092 // java_thread - pre-checked
2093 // java_thread - unchecked
2094 // depth - pre-checked as non-negative
2095 jvmtiError
2096 JvmtiEnv::SetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint value) {
2097   // rm object is created to clean up the javaVFrame created in
2098   // doit_prologue(), but after doit() is finished with it.
2099   ResourceMark rm;
2100   jvalue val;
2101   val.i = value;
2102   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val);
2103   VMThread::execute(&op);
2104   return op.result();
2105 } /* end SetLocalInt */
2106 
2107 
2108 // Threads_lock NOT held, java_thread not protected by lock
2109 // java_thread - pre-checked
2110 // java_thread - unchecked
2111 // depth - pre-checked as non-negative
2112 jvmtiError
2113 JvmtiEnv::SetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong value) {
2114   // rm object is created to clean up the javaVFrame created in
2115   // doit_prologue(), but after doit() is finished with it.
2116   ResourceMark rm;
2117   jvalue val;
2118   val.j = value;
2119   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val);
2120   VMThread::execute(&op);
2121   return op.result();
2122 } /* end SetLocalLong */
2123 
2124 
2125 // Threads_lock NOT held, java_thread not protected by lock
2126 // java_thread - pre-checked
2127 // java_thread - unchecked
2128 // depth - pre-checked as non-negative
2129 jvmtiError
2130 JvmtiEnv::SetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat value) {
2131   // rm object is created to clean up the javaVFrame created in
2132   // doit_prologue(), but after doit() is finished with it.
2133   ResourceMark rm;
2134   jvalue val;
2135   val.f = value;
2136   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val);
2137   VMThread::execute(&op);
2138   return op.result();
2139 } /* end SetLocalFloat */
2140 
2141 
2142 // Threads_lock NOT held, java_thread not protected by lock
2143 // java_thread - pre-checked
2144 // java_thread - unchecked
2145 // depth - pre-checked as non-negative
2146 jvmtiError
2147 JvmtiEnv::SetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble value) {
2148   // rm object is created to clean up the javaVFrame created in
2149   // doit_prologue(), but after doit() is finished with it.
2150   ResourceMark rm;
2151   jvalue val;
2152   val.d = value;
2153   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val);
2154   VMThread::execute(&op);
2155   return op.result();
2156 } /* end SetLocalDouble */
2157 
2158 
2159   //
2160   // Breakpoint functions
2161   //
2162 
2163 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2164 jvmtiError
2165 JvmtiEnv::SetBreakpoint(Method* method_oop, jlocation location) {
2166   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2167   if (location < 0) {   // simple invalid location check first
2168     return JVMTI_ERROR_INVALID_LOCATION;
2169   }
2170   // verify that the breakpoint is not past the end of the method
2171   if (location >= (jlocation) method_oop->code_size()) {
2172     return JVMTI_ERROR_INVALID_LOCATION;
2173   }
2174 
2175   ResourceMark rm;
2176   JvmtiBreakpoint bp(method_oop, location);
2177   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2178   if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
2179     return JVMTI_ERROR_DUPLICATE;
2180 
2181   if (TraceJVMTICalls) {
2182     jvmti_breakpoints.print();
2183   }
2184 
2185   return JVMTI_ERROR_NONE;
2186 } /* end SetBreakpoint */
2187 
2188 
2189 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2190 jvmtiError
2191 JvmtiEnv::ClearBreakpoint(Method* method_oop, jlocation location) {
2192   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2193 
2194   if (location < 0) {   // simple invalid location check first
2195     return JVMTI_ERROR_INVALID_LOCATION;
2196   }
2197 
2198   // verify that the breakpoint is not past the end of the method
2199   if (location >= (jlocation) method_oop->code_size()) {
2200     return JVMTI_ERROR_INVALID_LOCATION;
2201   }
2202 
2203   JvmtiBreakpoint bp(method_oop, location);
2204 
2205   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2206   if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
2207     return JVMTI_ERROR_NOT_FOUND;
2208 
2209   if (TraceJVMTICalls) {
2210     jvmti_breakpoints.print();
2211   }
2212 
2213   return JVMTI_ERROR_NONE;
2214 } /* end ClearBreakpoint */
2215 
2216 
2217   //
2218   // Watched Field functions
2219   //
2220 
2221 jvmtiError
2222 JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2223   // make sure we haven't set this watch before
2224   if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
2225   fdesc_ptr->set_is_field_access_watched(true);
2226 
2227   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
2228 
2229   return JVMTI_ERROR_NONE;
2230 } /* end SetFieldAccessWatch */
2231 
2232 
2233 jvmtiError
2234 JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2235   // make sure we have a watch to clear
2236   if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
2237   fdesc_ptr->set_is_field_access_watched(false);
2238 
2239   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
2240 
2241   return JVMTI_ERROR_NONE;
2242 } /* end ClearFieldAccessWatch */
2243 
2244 
2245 jvmtiError
2246 JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2247   // make sure we haven't set this watch before
2248   if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
2249   fdesc_ptr->set_is_field_modification_watched(true);
2250 
2251   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
2252 
2253   return JVMTI_ERROR_NONE;
2254 } /* end SetFieldModificationWatch */
2255 
2256 
2257 jvmtiError
2258 JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2259    // make sure we have a watch to clear
2260   if (!fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_NOT_FOUND;
2261   fdesc_ptr->set_is_field_modification_watched(false);
2262 
2263   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, false);
2264 
2265   return JVMTI_ERROR_NONE;
2266 } /* end ClearFieldModificationWatch */
2267 
2268   //
2269   // Class functions
2270   //
2271 
2272 
2273 // k_mirror - may be primitive, this must be checked
2274 // signature_ptr - NULL is a valid value, must be checked
2275 // generic_ptr - NULL is a valid value, must be checked
2276 jvmtiError
2277 JvmtiEnv::GetClassSignature(oop k_mirror, char** signature_ptr, char** generic_ptr) {
2278   ResourceMark rm;
2279   bool isPrimitive = java_lang_Class::is_primitive(k_mirror);
2280   Klass* k = NULL;
2281   if (!isPrimitive) {
2282     k = java_lang_Class::as_Klass(k_mirror);
2283     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2284   }
2285   if (signature_ptr != NULL) {
2286     char* result = NULL;
2287     if (isPrimitive) {
2288       char tchar = type2char(java_lang_Class::primitive_type(k_mirror));
2289       result = (char*) jvmtiMalloc(2);
2290       result[0] = tchar;
2291       result[1] = '\0';
2292     } else {
2293       const char* class_sig = k->signature_name();
2294       result = (char *) jvmtiMalloc(strlen(class_sig)+1);
2295       strcpy(result, class_sig);
2296     }
2297     *signature_ptr = result;
2298   }
2299   if (generic_ptr != NULL) {
2300     *generic_ptr = NULL;
2301     if (!isPrimitive && k->is_instance_klass()) {
2302       Symbol* soo = InstanceKlass::cast(k)->generic_signature();
2303       if (soo != NULL) {
2304         const char *gen_sig = soo->as_C_string();
2305         if (gen_sig != NULL) {
2306           char* gen_result;
2307           jvmtiError err = allocate(strlen(gen_sig) + 1,
2308                                     (unsigned char **)&gen_result);
2309           if (err != JVMTI_ERROR_NONE) {
2310             return err;
2311           }
2312           strcpy(gen_result, gen_sig);
2313           *generic_ptr = gen_result;
2314         }
2315       }
2316     }
2317   }
2318   return JVMTI_ERROR_NONE;
2319 } /* end GetClassSignature */
2320 
2321 
2322 // k_mirror - may be primitive, this must be checked
2323 // status_ptr - pre-checked for NULL
2324 jvmtiError
2325 JvmtiEnv::GetClassStatus(oop k_mirror, jint* status_ptr) {
2326   jint result = 0;
2327   if (java_lang_Class::is_primitive(k_mirror)) {
2328     result |= JVMTI_CLASS_STATUS_PRIMITIVE;
2329   } else {
2330     Klass* k = java_lang_Class::as_Klass(k_mirror);
2331     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2332     result = k->jvmti_class_status();
2333   }
2334   *status_ptr = result;
2335 
2336   return JVMTI_ERROR_NONE;
2337 } /* end GetClassStatus */
2338 
2339 
2340 // k_mirror - may be primitive, this must be checked
2341 // source_name_ptr - pre-checked for NULL
2342 jvmtiError
2343 JvmtiEnv::GetSourceFileName(oop k_mirror, char** source_name_ptr) {
2344   if (java_lang_Class::is_primitive(k_mirror)) {
2345      return JVMTI_ERROR_ABSENT_INFORMATION;
2346   }
2347   Klass* k_klass = java_lang_Class::as_Klass(k_mirror);
2348   NULL_CHECK(k_klass, JVMTI_ERROR_INVALID_CLASS);
2349 
2350   if (!k_klass->is_instance_klass()) {
2351     return JVMTI_ERROR_ABSENT_INFORMATION;
2352   }
2353 
2354   Symbol* sfnOop = InstanceKlass::cast(k_klass)->source_file_name();
2355   NULL_CHECK(sfnOop, JVMTI_ERROR_ABSENT_INFORMATION);
2356   {
2357     JavaThread* current_thread  = JavaThread::current();
2358     ResourceMark rm(current_thread);
2359     const char* sfncp = (const char*) sfnOop->as_C_string();
2360     *source_name_ptr = (char *) jvmtiMalloc(strlen(sfncp)+1);
2361     strcpy(*source_name_ptr, sfncp);
2362   }
2363 
2364   return JVMTI_ERROR_NONE;
2365 } /* end GetSourceFileName */
2366 
2367 
2368 // k_mirror - may be primitive, this must be checked
2369 // modifiers_ptr - pre-checked for NULL
2370 jvmtiError
2371 JvmtiEnv::GetClassModifiers(oop k_mirror, jint* modifiers_ptr) {
2372   JavaThread* current_thread  = JavaThread::current();
2373   jint result = 0;
2374   if (!java_lang_Class::is_primitive(k_mirror)) {
2375     Klass* k = java_lang_Class::as_Klass(k_mirror);
2376     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2377     result = k->compute_modifier_flags(current_thread);
2378     JavaThread* THREAD = current_thread; // pass to macros
2379     if (HAS_PENDING_EXCEPTION) {
2380       CLEAR_PENDING_EXCEPTION;
2381       return JVMTI_ERROR_INTERNAL;
2382     };
2383 
2384     // Reset the deleted  ACC_SUPER bit ( deleted in compute_modifier_flags()).
2385     if(k->is_super()) {
2386       result |= JVM_ACC_SUPER;
2387     }
2388   } else {
2389     result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
2390   }
2391   *modifiers_ptr = result;
2392 
2393   return JVMTI_ERROR_NONE;
2394 } /* end GetClassModifiers */
2395 
2396 
2397 // k_mirror - may be primitive, this must be checked
2398 // method_count_ptr - pre-checked for NULL
2399 // methods_ptr - pre-checked for NULL
2400 jvmtiError
2401 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
2402   JavaThread* current_thread  = JavaThread::current();
2403   HandleMark hm(current_thread);
2404 
2405   if (java_lang_Class::is_primitive(k_mirror)) {
2406     *method_count_ptr = 0;
2407     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2408     return JVMTI_ERROR_NONE;
2409   }
2410   Klass* k = java_lang_Class::as_Klass(k_mirror);
2411   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2412 
2413   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2414   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2415     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2416   }
2417 
2418   if (!k->is_instance_klass()) {
2419     *method_count_ptr = 0;
2420     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2421     return JVMTI_ERROR_NONE;
2422   }
2423   InstanceKlass* ik = InstanceKlass::cast(k);
2424   // Allocate the result and fill it in
2425   int result_length = ik->methods()->length();
2426   jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2427   int index;
2428   bool jmethodids_found = true;
2429 
2430   if (JvmtiExport::can_maintain_original_method_order()) {
2431     // Use the original method ordering indices stored in the class, so we can emit
2432     // jmethodIDs in the order they appeared in the class file
2433     for (index = 0; index < result_length; index++) {
2434       Method* m = ik->methods()->at(index);
2435       int original_index = ik->method_ordering()->at(index);
2436       assert(original_index >= 0 && original_index < result_length, "invalid original method index");
2437       jmethodID id;
2438       if (jmethodids_found) {
2439         id = m->find_jmethod_id_or_null();
2440         if (id == NULL) {
2441           // If we find an uninitialized value, make sure there is
2442           // enough space for all the uninitialized values we might
2443           // find.
2444           ik->ensure_space_for_methodids(index);
2445           jmethodids_found = false;
2446           id = m->jmethod_id();
2447         }
2448       } else {
2449         id = m->jmethod_id();
2450       }
2451       result_list[original_index] = id;
2452     }
2453   } else {
2454     // otherwise just copy in any order
2455     for (index = 0; index < result_length; index++) {
2456       Method* m = ik->methods()->at(index);
2457       jmethodID id;
2458       if (jmethodids_found) {
2459         id = m->find_jmethod_id_or_null();
2460         if (id == NULL) {
2461           // If we find an uninitialized value, make sure there is
2462           // enough space for all the uninitialized values we might
2463           // find.
2464           ik->ensure_space_for_methodids(index);
2465           jmethodids_found = false;
2466           id = m->jmethod_id();
2467         }
2468       } else {
2469         id = m->jmethod_id();
2470       }
2471       result_list[index] = id;
2472     }
2473   }
2474   // Fill in return value.
2475   *method_count_ptr = result_length;
2476   *methods_ptr = result_list;
2477 
2478   return JVMTI_ERROR_NONE;
2479 } /* end GetClassMethods */
2480 
2481 
2482 // k_mirror - may be primitive, this must be checked
2483 // field_count_ptr - pre-checked for NULL
2484 // fields_ptr - pre-checked for NULL
2485 jvmtiError
2486 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
2487   if (java_lang_Class::is_primitive(k_mirror)) {
2488     *field_count_ptr = 0;
2489     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2490     return JVMTI_ERROR_NONE;
2491   }
2492   JavaThread* current_thread = JavaThread::current();
2493   HandleMark hm(current_thread);
2494   Klass* k = java_lang_Class::as_Klass(k_mirror);
2495   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2496 
2497   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2498   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2499     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2500   }
2501 
2502   if (!k->is_instance_klass()) {
2503     *field_count_ptr = 0;
2504     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2505     return JVMTI_ERROR_NONE;
2506   }
2507 
2508 
2509   InstanceKlass* ik = InstanceKlass::cast(k);
2510 
2511   int result_count = 0;
2512   // First, count the fields.
2513   FilteredFieldStream flds(ik, true, true);
2514   result_count = flds.field_count();
2515 
2516   // Allocate the result and fill it in
2517   jfieldID* result_list = (jfieldID*) jvmtiMalloc(result_count * sizeof(jfieldID));
2518   // The JVMTI spec requires fields in the order they occur in the class file,
2519   // this is the reverse order of what FieldStream hands out.
2520   int id_index = (result_count - 1);
2521 
2522   for (FilteredFieldStream src_st(ik, true, true); !src_st.eos(); src_st.next()) {
2523     result_list[id_index--] = jfieldIDWorkaround::to_jfieldID(
2524                                             ik, src_st.offset(),
2525                                             src_st.access_flags().is_static());
2526   }
2527   assert(id_index == -1, "just checking");
2528   // Fill in the results
2529   *field_count_ptr = result_count;
2530   *fields_ptr = result_list;
2531 
2532   return JVMTI_ERROR_NONE;
2533 } /* end GetClassFields */
2534 
2535 
2536 // k_mirror - may be primitive, this must be checked
2537 // interface_count_ptr - pre-checked for NULL
2538 // interfaces_ptr - pre-checked for NULL
2539 jvmtiError
2540 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2541   {
2542     if (java_lang_Class::is_primitive(k_mirror)) {
2543       *interface_count_ptr = 0;
2544       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2545       return JVMTI_ERROR_NONE;
2546     }
2547     JavaThread* current_thread = JavaThread::current();
2548     HandleMark hm(current_thread);
2549     Klass* k = java_lang_Class::as_Klass(k_mirror);
2550     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2551 
2552     // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2553     if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2554       return JVMTI_ERROR_CLASS_NOT_PREPARED;
2555 
2556     if (!k->is_instance_klass()) {
2557       *interface_count_ptr = 0;
2558       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2559       return JVMTI_ERROR_NONE;
2560     }
2561 
2562     Array<Klass*>* interface_list = InstanceKlass::cast(k)->local_interfaces();
2563     const int result_length = (interface_list == NULL ? 0 : interface_list->length());
2564     jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2565     for (int i_index = 0; i_index < result_length; i_index += 1) {
2566       Klass* klass_at = interface_list->at(i_index);
2567       assert(klass_at->is_klass(), "interfaces must be Klass*s");
2568       assert(klass_at->is_interface(), "interfaces must be interfaces");
2569       oop mirror_at = klass_at->java_mirror();
2570       Handle handle_at = Handle(current_thread, mirror_at);
2571       result_list[i_index] = (jclass) jni_reference(handle_at);
2572     }
2573     *interface_count_ptr = result_length;
2574     *interfaces_ptr = result_list;
2575   }
2576 
2577   return JVMTI_ERROR_NONE;
2578 } /* end GetImplementedInterfaces */
2579 
2580 
2581 // k_mirror - may be primitive, this must be checked
2582 // minor_version_ptr - pre-checked for NULL
2583 // major_version_ptr - pre-checked for NULL
2584 jvmtiError
2585 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2586   if (java_lang_Class::is_primitive(k_mirror)) {
2587     return JVMTI_ERROR_ABSENT_INFORMATION;
2588   }
2589   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2590 
2591   jint status = klass->jvmti_class_status();
2592   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2593     return JVMTI_ERROR_INVALID_CLASS;
2594   }
2595   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2596     return JVMTI_ERROR_ABSENT_INFORMATION;
2597   }
2598 
2599   InstanceKlass* ik = InstanceKlass::cast(klass);
2600   *minor_version_ptr = ik->minor_version();
2601   *major_version_ptr = ik->major_version();
2602 
2603   return JVMTI_ERROR_NONE;
2604 } /* end GetClassVersionNumbers */
2605 
2606 
2607 // k_mirror - may be primitive, this must be checked
2608 // constant_pool_count_ptr - pre-checked for NULL
2609 // constant_pool_byte_count_ptr - pre-checked for NULL
2610 // constant_pool_bytes_ptr - pre-checked for NULL
2611 jvmtiError
2612 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2613   if (java_lang_Class::is_primitive(k_mirror)) {
2614     return JVMTI_ERROR_ABSENT_INFORMATION;
2615   }
2616 
2617   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2618   Thread *thread = Thread::current();
2619   ResourceMark rm(thread);
2620 
2621   jint status = klass->jvmti_class_status();
2622   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2623     return JVMTI_ERROR_INVALID_CLASS;
2624   }
2625   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2626     return JVMTI_ERROR_ABSENT_INFORMATION;
2627   }
2628 
2629   InstanceKlass* ik = InstanceKlass::cast(klass);
2630   JvmtiConstantPoolReconstituter reconstituter(ik);
2631   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2632     return reconstituter.get_error();
2633   }
2634 
2635   unsigned char *cpool_bytes;
2636   int cpool_size = reconstituter.cpool_size();
2637   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2638     return reconstituter.get_error();
2639   }
2640   jvmtiError res = allocate(cpool_size, &cpool_bytes);
2641   if (res != JVMTI_ERROR_NONE) {
2642     return res;
2643   }
2644   reconstituter.copy_cpool_bytes(cpool_bytes);
2645   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2646     return reconstituter.get_error();
2647   }
2648 
2649   constantPoolHandle  constants(thread, ik->constants());
2650   *constant_pool_count_ptr      = constants->length();
2651   *constant_pool_byte_count_ptr = cpool_size;
2652   *constant_pool_bytes_ptr      = cpool_bytes;
2653 
2654   return JVMTI_ERROR_NONE;
2655 } /* end GetConstantPool */
2656 
2657 
2658 // k_mirror - may be primitive, this must be checked
2659 // is_interface_ptr - pre-checked for NULL
2660 jvmtiError
2661 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2662   {
2663     bool result = false;
2664     if (!java_lang_Class::is_primitive(k_mirror)) {
2665       Klass* k = java_lang_Class::as_Klass(k_mirror);
2666       if (k != NULL && k->is_interface()) {
2667         result = true;
2668       }
2669     }
2670     *is_interface_ptr = result;
2671   }
2672 
2673   return JVMTI_ERROR_NONE;
2674 } /* end IsInterface */
2675 
2676 
2677 // k_mirror - may be primitive, this must be checked
2678 // is_array_class_ptr - pre-checked for NULL
2679 jvmtiError
2680 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
2681   {
2682     bool result = false;
2683     if (!java_lang_Class::is_primitive(k_mirror)) {
2684       Klass* k = java_lang_Class::as_Klass(k_mirror);
2685       if (k != NULL && k->is_array_klass()) {
2686         result = true;
2687       }
2688     }
2689     *is_array_class_ptr = result;
2690   }
2691 
2692   return JVMTI_ERROR_NONE;
2693 } /* end IsArrayClass */
2694 
2695 
2696 // k_mirror - may be primitive, this must be checked
2697 // classloader_ptr - pre-checked for NULL
2698 jvmtiError
2699 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
2700   {
2701     if (java_lang_Class::is_primitive(k_mirror)) {
2702       *classloader_ptr = (jclass) jni_reference(Handle());
2703       return JVMTI_ERROR_NONE;
2704     }
2705     JavaThread* current_thread = JavaThread::current();
2706     HandleMark hm(current_thread);
2707     Klass* k = java_lang_Class::as_Klass(k_mirror);
2708     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2709 
2710     oop result_oop = k->class_loader();
2711     if (result_oop == NULL) {
2712       *classloader_ptr = (jclass) jni_reference(Handle());
2713       return JVMTI_ERROR_NONE;
2714     }
2715     Handle result_handle = Handle(current_thread, result_oop);
2716     jclass result_jnihandle = (jclass) jni_reference(result_handle);
2717     *classloader_ptr = result_jnihandle;
2718   }
2719   return JVMTI_ERROR_NONE;
2720 } /* end GetClassLoader */
2721 
2722 
2723 // k_mirror - may be primitive, this must be checked
2724 // source_debug_extension_ptr - pre-checked for NULL
2725 jvmtiError
2726 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
2727   {
2728     if (java_lang_Class::is_primitive(k_mirror)) {
2729       return JVMTI_ERROR_ABSENT_INFORMATION;
2730     }
2731     Klass* k = java_lang_Class::as_Klass(k_mirror);
2732     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2733     if (!k->is_instance_klass()) {
2734       return JVMTI_ERROR_ABSENT_INFORMATION;
2735     }
2736     const char* sde = InstanceKlass::cast(k)->source_debug_extension();
2737     NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION);
2738 
2739     {
2740       *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1);
2741       strcpy(*source_debug_extension_ptr, sde);
2742     }
2743   }
2744 
2745   return JVMTI_ERROR_NONE;
2746 } /* end GetSourceDebugExtension */
2747 
2748   //
2749   // Object functions
2750   //
2751 
2752 // hash_code_ptr - pre-checked for NULL
2753 jvmtiError
2754 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
2755   oop mirror = JNIHandles::resolve_external_guard(object);
2756   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
2757   NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
2758 
2759   {
2760     jint result = (jint) mirror->identity_hash();
2761     *hash_code_ptr = result;
2762   }
2763   return JVMTI_ERROR_NONE;
2764 } /* end GetObjectHashCode */
2765 
2766 
2767 // info_ptr - pre-checked for NULL
2768 jvmtiError
2769 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
2770   JavaThread* calling_thread = JavaThread::current();
2771   jvmtiError err = get_object_monitor_usage(calling_thread, object, info_ptr);
2772   if (err == JVMTI_ERROR_THREAD_NOT_SUSPENDED) {
2773     // Some of the critical threads were not suspended. go to a safepoint and try again
2774     VM_GetObjectMonitorUsage op(this, calling_thread, object, info_ptr);
2775     VMThread::execute(&op);
2776     err = op.result();
2777   }
2778   return err;
2779 } /* end GetObjectMonitorUsage */
2780 
2781 
2782   //
2783   // Field functions
2784   //
2785 
2786 // name_ptr - NULL is a valid value, must be checked
2787 // signature_ptr - NULL is a valid value, must be checked
2788 // generic_ptr - NULL is a valid value, must be checked
2789 jvmtiError
2790 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2791   JavaThread* current_thread  = JavaThread::current();
2792   ResourceMark rm(current_thread);
2793   if (name_ptr == NULL) {
2794     // just don't return the name
2795   } else {
2796     const char* fieldName = fdesc_ptr->name()->as_C_string();
2797     *name_ptr =  (char*) jvmtiMalloc(strlen(fieldName) + 1);
2798     if (*name_ptr == NULL)
2799       return JVMTI_ERROR_OUT_OF_MEMORY;
2800     strcpy(*name_ptr, fieldName);
2801   }
2802   if (signature_ptr== NULL) {
2803     // just don't return the signature
2804   } else {
2805     const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
2806     *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
2807     if (*signature_ptr == NULL)
2808       return JVMTI_ERROR_OUT_OF_MEMORY;
2809     strcpy(*signature_ptr, fieldSignature);
2810   }
2811   if (generic_ptr != NULL) {
2812     *generic_ptr = NULL;
2813     Symbol* soop = fdesc_ptr->generic_signature();
2814     if (soop != NULL) {
2815       const char* gen_sig = soop->as_C_string();
2816       if (gen_sig != NULL) {
2817         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2818         if (err != JVMTI_ERROR_NONE) {
2819           return err;
2820         }
2821         strcpy(*generic_ptr, gen_sig);
2822       }
2823     }
2824   }
2825   return JVMTI_ERROR_NONE;
2826 } /* end GetFieldName */
2827 
2828 
2829 // declaring_class_ptr - pre-checked for NULL
2830 jvmtiError
2831 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
2832 
2833   *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
2834   return JVMTI_ERROR_NONE;
2835 } /* end GetFieldDeclaringClass */
2836 
2837 
2838 // modifiers_ptr - pre-checked for NULL
2839 jvmtiError
2840 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
2841 
2842   AccessFlags resultFlags = fdesc_ptr->access_flags();
2843   jint result = resultFlags.as_int();
2844   *modifiers_ptr = result;
2845 
2846   return JVMTI_ERROR_NONE;
2847 } /* end GetFieldModifiers */
2848 
2849 
2850 // is_synthetic_ptr - pre-checked for NULL
2851 jvmtiError
2852 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
2853   *is_synthetic_ptr = fdesc_ptr->is_synthetic();
2854   return JVMTI_ERROR_NONE;
2855 } /* end IsFieldSynthetic */
2856 
2857 
2858   //
2859   // Method functions
2860   //
2861 
2862 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2863 // name_ptr - NULL is a valid value, must be checked
2864 // signature_ptr - NULL is a valid value, must be checked
2865 // generic_ptr - NULL is a valid value, must be checked
2866 jvmtiError
2867 JvmtiEnv::GetMethodName(Method* method_oop, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2868   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2869   JavaThread* current_thread  = JavaThread::current();
2870 
2871   ResourceMark rm(current_thread); // get the utf8 name and signature
2872   if (name_ptr == NULL) {
2873     // just don't return the name
2874   } else {
2875     const char* utf8_name = (const char *) method_oop->name()->as_utf8();
2876     *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
2877     strcpy(*name_ptr, utf8_name);
2878   }
2879   if (signature_ptr == NULL) {
2880     // just don't return the signature
2881   } else {
2882     const char* utf8_signature = (const char *) method_oop->signature()->as_utf8();
2883     *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
2884     strcpy(*signature_ptr, utf8_signature);
2885   }
2886 
2887   if (generic_ptr != NULL) {
2888     *generic_ptr = NULL;
2889     Symbol* soop = method_oop->generic_signature();
2890     if (soop != NULL) {
2891       const char* gen_sig = soop->as_C_string();
2892       if (gen_sig != NULL) {
2893         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2894         if (err != JVMTI_ERROR_NONE) {
2895           return err;
2896         }
2897         strcpy(*generic_ptr, gen_sig);
2898       }
2899     }
2900   }
2901   return JVMTI_ERROR_NONE;
2902 } /* end GetMethodName */
2903 
2904 
2905 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2906 // declaring_class_ptr - pre-checked for NULL
2907 jvmtiError
2908 JvmtiEnv::GetMethodDeclaringClass(Method* method_oop, jclass* declaring_class_ptr) {
2909   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2910   (*declaring_class_ptr) = get_jni_class_non_null(method_oop->method_holder());
2911   return JVMTI_ERROR_NONE;
2912 } /* end GetMethodDeclaringClass */
2913 
2914 
2915 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2916 // modifiers_ptr - pre-checked for NULL
2917 jvmtiError
2918 JvmtiEnv::GetMethodModifiers(Method* method_oop, jint* modifiers_ptr) {
2919   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2920   (*modifiers_ptr) = method_oop->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2921   return JVMTI_ERROR_NONE;
2922 } /* end GetMethodModifiers */
2923 
2924 
2925 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2926 // max_ptr - pre-checked for NULL
2927 jvmtiError
2928 JvmtiEnv::GetMaxLocals(Method* method_oop, jint* max_ptr) {
2929   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2930   // get max stack
2931   (*max_ptr) = method_oop->max_locals();
2932   return JVMTI_ERROR_NONE;
2933 } /* end GetMaxLocals */
2934 
2935 
2936 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2937 // size_ptr - pre-checked for NULL
2938 jvmtiError
2939 JvmtiEnv::GetArgumentsSize(Method* method_oop, jint* size_ptr) {
2940   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2941   // get size of arguments
2942 
2943   (*size_ptr) = method_oop->size_of_parameters();
2944   return JVMTI_ERROR_NONE;
2945 } /* end GetArgumentsSize */
2946 
2947 
2948 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2949 // entry_count_ptr - pre-checked for NULL
2950 // table_ptr - pre-checked for NULL
2951 jvmtiError
2952 JvmtiEnv::GetLineNumberTable(Method* method_oop, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
2953   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2954   if (!method_oop->has_linenumber_table()) {
2955     return (JVMTI_ERROR_ABSENT_INFORMATION);
2956   }
2957 
2958   // The line number table is compressed so we don't know how big it is until decompressed.
2959   // Decompression is really fast so we just do it twice.
2960 
2961   // Compute size of table
2962   jint num_entries = 0;
2963   CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
2964   while (stream.read_pair()) {
2965     num_entries++;
2966   }
2967   jvmtiLineNumberEntry *jvmti_table =
2968             (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
2969 
2970   // Fill jvmti table
2971   if (num_entries > 0) {
2972     int index = 0;
2973     CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
2974     while (stream.read_pair()) {
2975       jvmti_table[index].start_location = (jlocation) stream.bci();
2976       jvmti_table[index].line_number = (jint) stream.line();
2977       index++;
2978     }
2979     assert(index == num_entries, "sanity check");
2980   }
2981 
2982   // Set up results
2983   (*entry_count_ptr) = num_entries;
2984   (*table_ptr) = jvmti_table;
2985 
2986   return JVMTI_ERROR_NONE;
2987 } /* end GetLineNumberTable */
2988 
2989 
2990 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2991 // start_location_ptr - pre-checked for NULL
2992 // end_location_ptr - pre-checked for NULL
2993 jvmtiError
2994 JvmtiEnv::GetMethodLocation(Method* method_oop, jlocation* start_location_ptr, jlocation* end_location_ptr) {
2995 
2996   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2997   // get start and end location
2998   (*end_location_ptr) = (jlocation) (method_oop->code_size() - 1);
2999   if (method_oop->code_size() == 0) {
3000     // there is no code so there is no start location
3001     (*start_location_ptr) = (jlocation)(-1);
3002   } else {
3003     (*start_location_ptr) = (jlocation)(0);
3004   }
3005 
3006   return JVMTI_ERROR_NONE;
3007 } /* end GetMethodLocation */
3008 
3009 
3010 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3011 // entry_count_ptr - pre-checked for NULL
3012 // table_ptr - pre-checked for NULL
3013 jvmtiError
3014 JvmtiEnv::GetLocalVariableTable(Method* method_oop, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
3015 
3016   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3017   JavaThread* current_thread  = JavaThread::current();
3018 
3019   // does the klass have any local variable information?
3020   InstanceKlass* ik = method_oop->method_holder();
3021   if (!ik->access_flags().has_localvariable_table()) {
3022     return (JVMTI_ERROR_ABSENT_INFORMATION);
3023   }
3024 
3025   ConstantPool* constants = method_oop->constants();
3026   NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
3027 
3028   // in the vm localvariable table representation, 6 consecutive elements in the table
3029   // represent a 6-tuple of shorts
3030   // [start_pc, length, name_index, descriptor_index, signature_index, index]
3031   jint num_entries = method_oop->localvariable_table_length();
3032   jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
3033                 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
3034 
3035   if (num_entries > 0) {
3036     LocalVariableTableElement* table = method_oop->localvariable_table_start();
3037     for (int i = 0; i < num_entries; i++) {
3038       // get the 5 tuple information from the vm table
3039       jlocation start_location = (jlocation) table[i].start_bci;
3040       jint length = (jint) table[i].length;
3041       int name_index = (int) table[i].name_cp_index;
3042       int signature_index = (int) table[i].descriptor_cp_index;
3043       int generic_signature_index = (int) table[i].signature_cp_index;
3044       jint slot = (jint) table[i].slot;
3045 
3046       // get utf8 name and signature
3047       char *name_buf = NULL;
3048       char *sig_buf = NULL;
3049       char *gen_sig_buf = NULL;
3050       {
3051         ResourceMark rm(current_thread);
3052 
3053         const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
3054         name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3055         strcpy(name_buf, utf8_name);
3056 
3057         const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
3058         sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
3059         strcpy(sig_buf, utf8_signature);
3060 
3061         if (generic_signature_index > 0) {
3062           const char *utf8_gen_sign = (const char *)
3063                                        constants->symbol_at(generic_signature_index)->as_utf8();
3064           gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
3065           strcpy(gen_sig_buf, utf8_gen_sign);
3066         }
3067       }
3068 
3069       // fill in the jvmti local variable table
3070       jvmti_table[i].start_location = start_location;
3071       jvmti_table[i].length = length;
3072       jvmti_table[i].name = name_buf;
3073       jvmti_table[i].signature = sig_buf;
3074       jvmti_table[i].generic_signature = gen_sig_buf;
3075       jvmti_table[i].slot = slot;
3076     }
3077   }
3078 
3079   // set results
3080   (*entry_count_ptr) = num_entries;
3081   (*table_ptr) = jvmti_table;
3082 
3083   return JVMTI_ERROR_NONE;
3084 } /* end GetLocalVariableTable */
3085 
3086 
3087 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3088 // bytecode_count_ptr - pre-checked for NULL
3089 // bytecodes_ptr - pre-checked for NULL
3090 jvmtiError
3091 JvmtiEnv::GetBytecodes(Method* method_oop, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
3092   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3093 
3094   HandleMark hm;
3095   methodHandle method(method_oop);
3096   jint size = (jint)method->code_size();
3097   jvmtiError err = allocate(size, bytecodes_ptr);
3098   if (err != JVMTI_ERROR_NONE) {
3099     return err;
3100   }
3101 
3102   (*bytecode_count_ptr) = size;
3103   // get byte codes
3104   JvmtiClassFileReconstituter::copy_bytecodes(method, *bytecodes_ptr);
3105 
3106   return JVMTI_ERROR_NONE;
3107 } /* end GetBytecodes */
3108 
3109 
3110 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3111 // is_native_ptr - pre-checked for NULL
3112 jvmtiError
3113 JvmtiEnv::IsMethodNative(Method* method_oop, jboolean* is_native_ptr) {
3114   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3115   (*is_native_ptr) = method_oop->is_native();
3116   return JVMTI_ERROR_NONE;
3117 } /* end IsMethodNative */
3118 
3119 
3120 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3121 // is_synthetic_ptr - pre-checked for NULL
3122 jvmtiError
3123 JvmtiEnv::IsMethodSynthetic(Method* method_oop, jboolean* is_synthetic_ptr) {
3124   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3125   (*is_synthetic_ptr) = method_oop->is_synthetic();
3126   return JVMTI_ERROR_NONE;
3127 } /* end IsMethodSynthetic */
3128 
3129 
3130 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3131 // is_obsolete_ptr - pre-checked for NULL
3132 jvmtiError
3133 JvmtiEnv::IsMethodObsolete(Method* method_oop, jboolean* is_obsolete_ptr) {
3134   if (use_version_1_0_semantics() &&
3135       get_capabilities()->can_redefine_classes == 0) {
3136     // This JvmtiEnv requested version 1.0 semantics and this function
3137     // requires the can_redefine_classes capability in version 1.0 so
3138     // we need to return an error here.
3139     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3140   }
3141 
3142   if (method_oop == NULL || method_oop->is_obsolete()) {
3143     *is_obsolete_ptr = true;
3144   } else {
3145     *is_obsolete_ptr = false;
3146   }
3147   return JVMTI_ERROR_NONE;
3148 } /* end IsMethodObsolete */
3149 
3150   //
3151   // Raw Monitor functions
3152   //
3153 
3154 // name - pre-checked for NULL
3155 // monitor_ptr - pre-checked for NULL
3156 jvmtiError
3157 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
3158   JvmtiRawMonitor* rmonitor = new JvmtiRawMonitor(name);
3159   NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
3160 
3161   *monitor_ptr = (jrawMonitorID)rmonitor;
3162 
3163   return JVMTI_ERROR_NONE;
3164 } /* end CreateRawMonitor */
3165 
3166 
3167 // rmonitor - pre-checked for validity
3168 jvmtiError
3169 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
3170   if (Threads::number_of_threads() == 0) {
3171     // Remove this  monitor from pending raw monitors list
3172     // if it has entered in onload or start phase.
3173     JvmtiPendingMonitors::destroy(rmonitor);
3174   } else {
3175     Thread* thread  = Thread::current();
3176     if (rmonitor->is_entered(thread)) {
3177       // The caller owns this monitor which we are about to destroy.
3178       // We exit the underlying synchronization object so that the
3179       // "delete monitor" call below can work without an assertion
3180       // failure on systems that don't like destroying synchronization
3181       // objects that are locked.
3182       int r;
3183       intptr_t recursion = rmonitor->recursions();
3184       for (intptr_t i=0; i <= recursion; i++) {
3185         r = rmonitor->raw_exit(thread);
3186         assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
3187         if (r != ObjectMonitor::OM_OK) {  // robustness
3188           return JVMTI_ERROR_INTERNAL;
3189         }
3190       }
3191     }
3192     if (rmonitor->owner() != NULL) {
3193       // The caller is trying to destroy a monitor that is locked by
3194       // someone else. While this is not forbidden by the JVMTI
3195       // spec, it will cause an assertion failure on systems that don't
3196       // like destroying synchronization objects that are locked.
3197       // We indicate a problem with the error return (and leak the
3198       // monitor's memory).
3199       return JVMTI_ERROR_NOT_MONITOR_OWNER;
3200     }
3201   }
3202 
3203   delete rmonitor;
3204 
3205   return JVMTI_ERROR_NONE;
3206 } /* end DestroyRawMonitor */
3207 
3208 
3209 // rmonitor - pre-checked for validity
3210 jvmtiError
3211 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
3212   if (Threads::number_of_threads() == 0) {
3213     // No JavaThreads exist so ObjectMonitor enter cannot be
3214     // used, add this raw monitor to the pending list.
3215     // The pending monitors will be actually entered when
3216     // the VM is setup.
3217     // See transition_pending_raw_monitors in create_vm()
3218     // in thread.cpp.
3219     JvmtiPendingMonitors::enter(rmonitor);
3220   } else {
3221     int r = 0;
3222     Thread* thread = Thread::current();
3223 
3224     if (thread->is_Java_thread()) {
3225       JavaThread* current_thread = (JavaThread*)thread;
3226 
3227 #ifdef PROPER_TRANSITIONS
3228       // Not really unknown but ThreadInVMfromNative does more than we want
3229       ThreadInVMfromUnknown __tiv;
3230       {
3231         ThreadBlockInVM __tbivm(current_thread);
3232         r = rmonitor->raw_enter(current_thread);
3233       }
3234 #else
3235       /* Transition to thread_blocked without entering vm state          */
3236       /* This is really evil. Normally you can't undo _thread_blocked    */
3237       /* transitions like this because it would cause us to miss a       */
3238       /* safepoint but since the thread was already in _thread_in_native */
3239       /* the thread is not leaving a safepoint safe state and it will    */
3240       /* block when it tries to return from native. We can't safepoint   */
3241       /* block in here because we could deadlock the vmthread. Blech.    */
3242 
3243       JavaThreadState state = current_thread->thread_state();
3244       assert(state == _thread_in_native, "Must be _thread_in_native");
3245       // frame should already be walkable since we are in native
3246       assert(!current_thread->has_last_Java_frame() ||
3247              current_thread->frame_anchor()->walkable(), "Must be walkable");
3248       current_thread->set_thread_state(_thread_blocked);
3249 
3250       r = rmonitor->raw_enter(current_thread);
3251       // restore state, still at a safepoint safe state
3252       current_thread->set_thread_state(state);
3253 
3254 #endif /* PROPER_TRANSITIONS */
3255       assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked");
3256     } else {
3257       if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3258         r = rmonitor->raw_enter(thread);
3259       } else {
3260         ShouldNotReachHere();
3261       }
3262     }
3263 
3264     if (r != ObjectMonitor::OM_OK) {  // robustness
3265       return JVMTI_ERROR_INTERNAL;
3266     }
3267   }
3268   return JVMTI_ERROR_NONE;
3269 } /* end RawMonitorEnter */
3270 
3271 
3272 // rmonitor - pre-checked for validity
3273 jvmtiError
3274 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3275   jvmtiError err = JVMTI_ERROR_NONE;
3276 
3277   if (Threads::number_of_threads() == 0) {
3278     // No JavaThreads exist so just remove this monitor from the pending list.
3279     // Bool value from exit is false if rmonitor is not in the list.
3280     if (!JvmtiPendingMonitors::exit(rmonitor)) {
3281       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3282     }
3283   } else {
3284     int r = 0;
3285     Thread* thread = Thread::current();
3286 
3287     if (thread->is_Java_thread()) {
3288       JavaThread* current_thread = (JavaThread*)thread;
3289 #ifdef PROPER_TRANSITIONS
3290       // Not really unknown but ThreadInVMfromNative does more than we want
3291       ThreadInVMfromUnknown __tiv;
3292 #endif /* PROPER_TRANSITIONS */
3293       r = rmonitor->raw_exit(current_thread);
3294     } else {
3295       if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3296         r = rmonitor->raw_exit(thread);
3297       } else {
3298         ShouldNotReachHere();
3299       }
3300     }
3301 
3302     if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3303       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3304     } else {
3305       assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
3306       if (r != ObjectMonitor::OM_OK) {  // robustness
3307         err = JVMTI_ERROR_INTERNAL;
3308       }
3309     }
3310   }
3311   return err;
3312 } /* end RawMonitorExit */
3313 
3314 
3315 // rmonitor - pre-checked for validity
3316 jvmtiError
3317 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3318   int r = 0;
3319   Thread* thread = Thread::current();
3320 
3321   if (thread->is_Java_thread()) {
3322     JavaThread* current_thread = (JavaThread*)thread;
3323 #ifdef PROPER_TRANSITIONS
3324     // Not really unknown but ThreadInVMfromNative does more than we want
3325     ThreadInVMfromUnknown __tiv;
3326     {
3327       ThreadBlockInVM __tbivm(current_thread);
3328       r = rmonitor->raw_wait(millis, true, current_thread);
3329     }
3330 #else
3331     /* Transition to thread_blocked without entering vm state          */
3332     /* This is really evil. Normally you can't undo _thread_blocked    */
3333     /* transitions like this because it would cause us to miss a       */
3334     /* safepoint but since the thread was already in _thread_in_native */
3335     /* the thread is not leaving a safepoint safe state and it will    */
3336     /* block when it tries to return from native. We can't safepoint   */
3337     /* block in here because we could deadlock the vmthread. Blech.    */
3338 
3339     JavaThreadState state = current_thread->thread_state();
3340     assert(state == _thread_in_native, "Must be _thread_in_native");
3341     // frame should already be walkable since we are in native
3342     assert(!current_thread->has_last_Java_frame() ||
3343            current_thread->frame_anchor()->walkable(), "Must be walkable");
3344     current_thread->set_thread_state(_thread_blocked);
3345 
3346     r = rmonitor->raw_wait(millis, true, current_thread);
3347     // restore state, still at a safepoint safe state
3348     current_thread->set_thread_state(state);
3349 
3350 #endif /* PROPER_TRANSITIONS */
3351   } else {
3352     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3353       r = rmonitor->raw_wait(millis, true, thread);
3354     } else {
3355       ShouldNotReachHere();
3356     }
3357   }
3358 
3359   switch (r) {
3360   case ObjectMonitor::OM_INTERRUPTED:
3361     return JVMTI_ERROR_INTERRUPT;
3362   case ObjectMonitor::OM_ILLEGAL_MONITOR_STATE:
3363     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3364   }
3365   assert(r == ObjectMonitor::OM_OK, "raw_wait should have worked");
3366   if (r != ObjectMonitor::OM_OK) {  // robustness
3367     return JVMTI_ERROR_INTERNAL;
3368   }
3369 
3370   return JVMTI_ERROR_NONE;
3371 } /* end RawMonitorWait */
3372 
3373 
3374 // rmonitor - pre-checked for validity
3375 jvmtiError
3376 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3377   int r = 0;
3378   Thread* thread = Thread::current();
3379 
3380   if (thread->is_Java_thread()) {
3381     JavaThread* current_thread = (JavaThread*)thread;
3382     // Not really unknown but ThreadInVMfromNative does more than we want
3383     ThreadInVMfromUnknown __tiv;
3384     r = rmonitor->raw_notify(current_thread);
3385   } else {
3386     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3387       r = rmonitor->raw_notify(thread);
3388     } else {
3389       ShouldNotReachHere();
3390     }
3391   }
3392 
3393   if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3394     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3395   }
3396   assert(r == ObjectMonitor::OM_OK, "raw_notify should have worked");
3397   if (r != ObjectMonitor::OM_OK) {  // robustness
3398     return JVMTI_ERROR_INTERNAL;
3399   }
3400 
3401   return JVMTI_ERROR_NONE;
3402 } /* end RawMonitorNotify */
3403 
3404 
3405 // rmonitor - pre-checked for validity
3406 jvmtiError
3407 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3408   int r = 0;
3409   Thread* thread = Thread::current();
3410 
3411   if (thread->is_Java_thread()) {
3412     JavaThread* current_thread = (JavaThread*)thread;
3413     ThreadInVMfromUnknown __tiv;
3414     r = rmonitor->raw_notifyAll(current_thread);
3415   } else {
3416     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3417       r = rmonitor->raw_notifyAll(thread);
3418     } else {
3419       ShouldNotReachHere();
3420     }
3421   }
3422 
3423   if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3424     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3425   }
3426   assert(r == ObjectMonitor::OM_OK, "raw_notifyAll should have worked");
3427   if (r != ObjectMonitor::OM_OK) {  // robustness
3428     return JVMTI_ERROR_INTERNAL;
3429   }
3430 
3431   return JVMTI_ERROR_NONE;
3432 } /* end RawMonitorNotifyAll */
3433 
3434 
3435   //
3436   // JNI Function Interception functions
3437   //
3438 
3439 
3440 // function_table - pre-checked for NULL
3441 jvmtiError
3442 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3443   // Copy jni function table at safepoint.
3444   VM_JNIFunctionTableCopier copier(function_table);
3445   VMThread::execute(&copier);
3446 
3447   return JVMTI_ERROR_NONE;
3448 } /* end SetJNIFunctionTable */
3449 
3450 
3451 // function_table - pre-checked for NULL
3452 jvmtiError
3453 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3454   *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3455   if (*function_table == NULL)
3456     return JVMTI_ERROR_OUT_OF_MEMORY;
3457   memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3458   return JVMTI_ERROR_NONE;
3459 } /* end GetJNIFunctionTable */
3460 
3461 
3462   //
3463   // Event Management functions
3464   //
3465 
3466 jvmtiError
3467 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3468   // can only generate two event types
3469   if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3470       event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3471     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3472   }
3473 
3474   // for compiled_method_load events we must check that the environment
3475   // has the can_generate_compiled_method_load_events capability.
3476   if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3477     if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3478       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3479     }
3480     return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3481   } else {
3482     return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3483   }
3484 
3485 } /* end GenerateEvents */
3486 
3487 
3488   //
3489   // Extension Mechanism functions
3490   //
3491 
3492 // extension_count_ptr - pre-checked for NULL
3493 // extensions - pre-checked for NULL
3494 jvmtiError
3495 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3496   return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3497 } /* end GetExtensionFunctions */
3498 
3499 
3500 // extension_count_ptr - pre-checked for NULL
3501 // extensions - pre-checked for NULL
3502 jvmtiError
3503 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3504   return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3505 } /* end GetExtensionEvents */
3506 
3507 
3508 // callback - NULL is a valid value, must be checked
3509 jvmtiError
3510 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3511   return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3512 } /* end SetExtensionEventCallback */
3513 
3514   //
3515   // Timers functions
3516   //
3517 
3518 // info_ptr - pre-checked for NULL
3519 jvmtiError
3520 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3521   os::current_thread_cpu_time_info(info_ptr);
3522   return JVMTI_ERROR_NONE;
3523 } /* end GetCurrentThreadCpuTimerInfo */
3524 
3525 
3526 // nanos_ptr - pre-checked for NULL
3527 jvmtiError
3528 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3529   *nanos_ptr = os::current_thread_cpu_time();
3530   return JVMTI_ERROR_NONE;
3531 } /* end GetCurrentThreadCpuTime */
3532 
3533 
3534 // info_ptr - pre-checked for NULL
3535 jvmtiError
3536 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3537   os::thread_cpu_time_info(info_ptr);
3538   return JVMTI_ERROR_NONE;
3539 } /* end GetThreadCpuTimerInfo */
3540 
3541 
3542 // Threads_lock NOT held, java_thread not protected by lock
3543 // java_thread - pre-checked
3544 // nanos_ptr - pre-checked for NULL
3545 jvmtiError
3546 JvmtiEnv::GetThreadCpuTime(JavaThread* java_thread, jlong* nanos_ptr) {
3547   *nanos_ptr = os::thread_cpu_time(java_thread);
3548   return JVMTI_ERROR_NONE;
3549 } /* end GetThreadCpuTime */
3550 
3551 
3552 // info_ptr - pre-checked for NULL
3553 jvmtiError
3554 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3555   os::javaTimeNanos_info(info_ptr);
3556   return JVMTI_ERROR_NONE;
3557 } /* end GetTimerInfo */
3558 
3559 
3560 // nanos_ptr - pre-checked for NULL
3561 jvmtiError
3562 JvmtiEnv::GetTime(jlong* nanos_ptr) {
3563   *nanos_ptr = os::javaTimeNanos();
3564   return JVMTI_ERROR_NONE;
3565 } /* end GetTime */
3566 
3567 
3568 // processor_count_ptr - pre-checked for NULL
3569 jvmtiError
3570 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3571   *processor_count_ptr = os::active_processor_count();
3572   return JVMTI_ERROR_NONE;
3573 } /* end GetAvailableProcessors */
3574 
3575   //
3576   // System Properties functions
3577   //
3578 
3579 // count_ptr - pre-checked for NULL
3580 // property_ptr - pre-checked for NULL
3581 jvmtiError
3582 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3583   jvmtiError err = JVMTI_ERROR_NONE;
3584 
3585   // Get the number of readable properties.
3586   *count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties());
3587 
3588   // Allocate memory to hold the exact number of readable properties.
3589   err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3590   if (err != JVMTI_ERROR_NONE) {
3591     return err;
3592   }
3593   int readable_count = 0;
3594   // Loop through the system properties until all the readable properties are found.
3595   for (SystemProperty* p = Arguments::system_properties(); p != NULL && readable_count < *count_ptr; p = p->next()) {
3596     if (p->is_readable()) {
3597       const char *key = p->key();
3598       char **tmp_value = *property_ptr+readable_count;
3599       readable_count++;
3600       err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3601       if (err == JVMTI_ERROR_NONE) {
3602         strcpy(*tmp_value, key);
3603       } else {
3604         // clean up previously allocated memory.
3605         for (int j=0; j<readable_count; j++) {
3606           Deallocate((unsigned char*)*property_ptr+j);
3607         }
3608         Deallocate((unsigned char*)property_ptr);
3609         break;
3610       }
3611     }
3612   }
3613   assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count");
3614   return err;
3615 } /* end GetSystemProperties */
3616 
3617 
3618 // property - pre-checked for NULL
3619 // value_ptr - pre-checked for NULL
3620 jvmtiError
3621 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3622   jvmtiError err = JVMTI_ERROR_NONE;
3623   const char *value;
3624 
3625   // Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist.
3626   value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property);
3627   if (value == NULL) {
3628     err =  JVMTI_ERROR_NOT_AVAILABLE;
3629   } else {
3630     err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3631     if (err == JVMTI_ERROR_NONE) {
3632       strcpy(*value_ptr, value);
3633     }
3634   }
3635   return err;
3636 } /* end GetSystemProperty */
3637 
3638 
3639 // property - pre-checked for NULL
3640 // value - NULL is a valid value, must be checked
3641 jvmtiError
3642 JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) {
3643   jvmtiError err =JVMTI_ERROR_NOT_AVAILABLE;
3644 
3645   for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
3646     if (strcmp(property, p->key()) == 0) {
3647       if (p->set_writeable_value(value_ptr)) {
3648         err =  JVMTI_ERROR_NONE;
3649       }
3650     }
3651   }
3652   return err;
3653 } /* end SetSystemProperty */
--- EOF ---