1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderExt.hpp"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/stringTable.hpp"
  29 #include "classfile/modules.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "interpreter/bytecodeStream.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvmtifiles/jvmtiEnv.hpp"
  35 #include "logging/log.hpp"
  36 #include "logging/logConfiguration.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "memory/universe.inline.hpp"
  39 #include "oops/instanceKlass.hpp"
  40 #include "oops/objArrayOop.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "prims/jniCheck.hpp"
  43 #include "prims/jvm_misc.hpp"
  44 #include "prims/jvmtiAgentThread.hpp"
  45 #include "prims/jvmtiClassFileReconstituter.hpp"
  46 #include "prims/jvmtiCodeBlobEvents.hpp"
  47 #include "prims/jvmtiExtensions.hpp"
  48 #include "prims/jvmtiGetLoadedClasses.hpp"
  49 #include "prims/jvmtiImpl.hpp"
  50 #include "prims/jvmtiManageCapabilities.hpp"
  51 #include "prims/jvmtiRawMonitor.hpp"
  52 #include "prims/jvmtiRedefineClasses.hpp"
  53 #include "prims/jvmtiTagMap.hpp"
  54 #include "prims/jvmtiThreadState.inline.hpp"
  55 #include "prims/jvmtiUtil.hpp"
  56 #include "runtime/arguments.hpp"
  57 #include "runtime/deoptimization.hpp"
  58 #include "runtime/interfaceSupport.hpp"
  59 #include "runtime/javaCalls.hpp"
  60 #include "runtime/jfieldIDWorkaround.hpp"
  61 #include "runtime/osThread.hpp"
  62 #include "runtime/reflectionUtils.hpp"
  63 #include "runtime/signature.hpp"
  64 #include "runtime/thread.inline.hpp"
  65 #include "runtime/timerTrace.hpp"
  66 #include "runtime/vframe.hpp"
  67 #include "runtime/vmThread.hpp"
  68 #include "services/threadService.hpp"
  69 #include "utilities/exceptions.hpp"
  70 #include "utilities/preserveException.hpp"
  71 
  72 
  73 #define FIXLATER 0 // REMOVE this when completed.
  74 
  75  // FIXLATER: hook into JvmtiTrace
  76 #define TraceJVMTICalls false
  77 
  78 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
  79 }
  80 
  81 JvmtiEnv::~JvmtiEnv() {
  82 }
  83 
  84 JvmtiEnv*
  85 JvmtiEnv::create_a_jvmti(jint version) {
  86   return new JvmtiEnv(version);
  87 }
  88 
  89 // VM operation class to copy jni function table at safepoint.
  90 // More than one java threads or jvmti agents may be reading/
  91 // modifying jni function tables. To reduce the risk of bad
  92 // interaction b/w these threads it is copied at safepoint.
  93 class VM_JNIFunctionTableCopier : public VM_Operation {
  94  private:
  95   const struct JNINativeInterface_ *_function_table;
  96  public:
  97   VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
  98     _function_table = func_tbl;
  99   };
 100 
 101   VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
 102   void doit() {
 103     copy_jni_function_table(_function_table);
 104   };
 105 };
 106 
 107 //
 108 // Do not change the "prefix" marker below, everything above it is copied
 109 // unchanged into the filled stub, everything below is controlled by the
 110 // stub filler (only method bodies are carried forward, and then only for
 111 // functionality still in the spec).
 112 //
 113 // end file prefix
 114 
 115   //
 116   // Memory Management functions
 117   //
 118 
 119 // mem_ptr - pre-checked for NULL
 120 jvmtiError
 121 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
 122   return allocate(size, mem_ptr);
 123 } /* end Allocate */
 124 
 125 
 126 // mem - NULL is a valid value, must be checked
 127 jvmtiError
 128 JvmtiEnv::Deallocate(unsigned char* mem) {
 129   return deallocate(mem);
 130 } /* end Deallocate */
 131 
 132 // Threads_lock NOT held, java_thread not protected by lock
 133 // java_thread - pre-checked
 134 // data - NULL is a valid value, must be checked
 135 jvmtiError
 136 JvmtiEnv::SetThreadLocalStorage(JavaThread* java_thread, const void* data) {
 137   JvmtiThreadState* state = java_thread->jvmti_thread_state();
 138   if (state == NULL) {
 139     if (data == NULL) {
 140       // leaving state unset same as data set to NULL
 141       return JVMTI_ERROR_NONE;
 142     }
 143     // otherwise, create the state
 144     state = JvmtiThreadState::state_for(java_thread);
 145     if (state == NULL) {
 146       return JVMTI_ERROR_THREAD_NOT_ALIVE;
 147     }
 148   }
 149   state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
 150   return JVMTI_ERROR_NONE;
 151 } /* end SetThreadLocalStorage */
 152 
 153 
 154 // Threads_lock NOT held
 155 // thread - NOT pre-checked
 156 // data_ptr - pre-checked for NULL
 157 jvmtiError
 158 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
 159   JavaThread* current_thread = JavaThread::current();
 160   if (thread == NULL) {
 161     JvmtiThreadState* state = current_thread->jvmti_thread_state();
 162     *data_ptr = (state == NULL) ? NULL :
 163       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 164   } else {
 165 
 166     // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
 167     // the thread to _thread_in_vm. However, when the TLS for a thread
 168     // other than the current thread is required we need to transition
 169     // from native so as to resolve the jthread.
 170 
 171     ThreadInVMfromNative __tiv(current_thread);
 172     VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
 173     debug_only(VMNativeEntryWrapper __vew;)
 174 
 175     oop thread_oop = JNIHandles::resolve_external_guard(thread);
 176     if (thread_oop == NULL) {
 177       return JVMTI_ERROR_INVALID_THREAD;
 178     }
 179     if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
 180       return JVMTI_ERROR_INVALID_THREAD;
 181     }
 182     JavaThread* java_thread = java_lang_Thread::thread(thread_oop);
 183     if (java_thread == NULL) {
 184       return JVMTI_ERROR_THREAD_NOT_ALIVE;
 185     }
 186     JvmtiThreadState* state = java_thread->jvmti_thread_state();
 187     *data_ptr = (state == NULL) ? NULL :
 188       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 189   }
 190   return JVMTI_ERROR_NONE;
 191 } /* end GetThreadLocalStorage */
 192 
 193   //
 194   // Module functions
 195   //
 196 
 197 // module_count_ptr - pre-checked for NULL
 198 // modules_ptr - pre-checked for NULL
 199 jvmtiError
 200 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
 201     JvmtiModuleClosure jmc;
 202 
 203     return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
 204 } /* end GetAllModules */
 205 
 206 
 207 // class_loader - NULL is a valid value, must be pre-checked
 208 // package_name - pre-checked for NULL
 209 // module_ptr - pre-checked for NULL
 210 jvmtiError
 211 JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
 212   JavaThread* THREAD = JavaThread::current(); // pass to macros
 213   ResourceMark rm(THREAD);
 214 
 215   Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
 216   // Check that loader is a subclass of java.lang.ClassLoader.
 217   if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 218     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 219   }
 220   jobject module = Modules::get_named_module(h_loader, package_name, THREAD);
 221   if (HAS_PENDING_EXCEPTION) {
 222     CLEAR_PENDING_EXCEPTION;
 223     return JVMTI_ERROR_INTERNAL; // unexpected exception
 224   }
 225   *module_ptr = module;
 226   return JVMTI_ERROR_NONE;
 227 } /* end GetNamedModule */
 228 
 229 
 230 // module - pre-checked for NULL
 231 // to_module - pre-checked for NULL
 232 jvmtiError
 233 JvmtiEnv::AddModuleReads(jobject module, jobject to_module) {
 234   JavaThread* THREAD = JavaThread::current();
 235 
 236   // check module
 237   Handle h_module(THREAD, JNIHandles::resolve(module));
 238   if (!java_lang_reflect_Module::is_instance(h_module())) {
 239     return JVMTI_ERROR_INVALID_MODULE;
 240   }
 241   // check to_module
 242   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 243   if (!java_lang_reflect_Module::is_instance(h_to_module())) {
 244     return JVMTI_ERROR_INVALID_MODULE;
 245   }
 246   return JvmtiExport::add_module_reads(h_module, h_to_module, THREAD);
 247 } /* end AddModuleReads */
 248 
 249 
 250 // module - pre-checked for NULL
 251 // pkg_name - pre-checked for NULL
 252 // to_module - pre-checked for NULL
 253 jvmtiError
 254 JvmtiEnv::AddModuleExports(jobject module, const char* pkg_name, jobject to_module) {
 255   JavaThread* THREAD = JavaThread::current();
 256   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 257 
 258   // check module
 259   Handle h_module(THREAD, JNIHandles::resolve(module));
 260   if (!java_lang_reflect_Module::is_instance(h_module())) {
 261     return JVMTI_ERROR_INVALID_MODULE;
 262   }
 263   // check to_module
 264   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 265   if (!java_lang_reflect_Module::is_instance(h_to_module())) {
 266     return JVMTI_ERROR_INVALID_MODULE;
 267   }
 268   return JvmtiExport::add_module_exports(h_module, h_pkg, h_to_module, THREAD);
 269 } /* end AddModuleExports */
 270 
 271 
 272 // module - pre-checked for NULL
 273 // pkg_name - pre-checked for NULL
 274 // to_module - pre-checked for NULL
 275 jvmtiError
 276 JvmtiEnv::AddModuleOpens(jobject module, const char* pkg_name, jobject to_module) {
 277   JavaThread* THREAD = JavaThread::current();
 278   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 279 
 280   // check module
 281   Handle h_module(THREAD, JNIHandles::resolve(module));
 282   if (!java_lang_reflect_Module::is_instance(h_module())) {
 283     return JVMTI_ERROR_INVALID_MODULE;
 284   }
 285   // check to_module
 286   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 287   if (!java_lang_reflect_Module::is_instance(h_to_module())) {
 288     return JVMTI_ERROR_INVALID_MODULE;
 289   }
 290   return JvmtiExport::add_module_opens(h_module, h_pkg, h_to_module, THREAD);
 291 } /* end AddModuleOpens */
 292 
 293 
 294 // module - pre-checked for NULL
 295 // service - pre-checked for NULL
 296 jvmtiError
 297 JvmtiEnv::AddModuleUses(jobject module, jclass service) {
 298   JavaThread* THREAD = JavaThread::current();
 299 
 300   // check module
 301   Handle h_module(THREAD, JNIHandles::resolve(module));
 302   if (!java_lang_reflect_Module::is_instance(h_module())) {
 303     return JVMTI_ERROR_INVALID_MODULE;
 304   }
 305   // check service
 306   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 307   if (!java_lang_Class::is_instance(h_service()) ||
 308       java_lang_Class::is_primitive(h_service())) {
 309     return JVMTI_ERROR_INVALID_CLASS;
 310   }
 311   return JvmtiExport::add_module_uses(h_module, h_service, THREAD);
 312 } /* end AddModuleUses */
 313 
 314 
 315 // module - pre-checked for NULL
 316 // service - pre-checked for NULL
 317 // impl_class - pre-checked for NULL
 318 jvmtiError
 319 JvmtiEnv::AddModuleProvides(jobject module, jclass service, jclass impl_class) {
 320   JavaThread* THREAD = JavaThread::current();
 321 
 322   // check module
 323   Handle h_module(THREAD, JNIHandles::resolve(module));
 324   if (!java_lang_reflect_Module::is_instance(h_module())) {
 325     return JVMTI_ERROR_INVALID_MODULE;
 326   }
 327   // check service
 328   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 329   if (!java_lang_Class::is_instance(h_service()) ||
 330       java_lang_Class::is_primitive(h_service())) {
 331     return JVMTI_ERROR_INVALID_CLASS;
 332   }
 333   // check impl_class
 334   Handle h_impl_class(THREAD, JNIHandles::resolve_external_guard(impl_class));
 335   if (!java_lang_Class::is_instance(h_impl_class()) ||
 336       java_lang_Class::is_primitive(h_impl_class())) {
 337     return JVMTI_ERROR_INVALID_CLASS;
 338   }
 339   return JvmtiExport::add_module_provides(h_module, h_service, h_impl_class, THREAD);
 340 } /* end AddModuleProvides */
 341 
 342 
 343   //
 344   // Class functions
 345   //
 346 
 347 // class_count_ptr - pre-checked for NULL
 348 // classes_ptr - pre-checked for NULL
 349 jvmtiError
 350 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
 351   return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
 352 } /* end GetLoadedClasses */
 353 
 354 
 355 // initiating_loader - NULL is a valid value, must be checked
 356 // class_count_ptr - pre-checked for NULL
 357 // classes_ptr - pre-checked for NULL
 358 jvmtiError
 359 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
 360   return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
 361                                                   class_count_ptr, classes_ptr);
 362 } /* end GetClassLoaderClasses */
 363 
 364 // k_mirror - may be primitive, this must be checked
 365 // is_modifiable_class_ptr - pre-checked for NULL
 366 jvmtiError
 367 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
 368   *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
 369                                                        JNI_TRUE : JNI_FALSE;
 370   return JVMTI_ERROR_NONE;
 371 } /* end IsModifiableClass */
 372 
 373 // class_count - pre-checked to be greater than or equal to 0
 374 // classes - pre-checked for NULL
 375 jvmtiError
 376 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
 377 //TODO: add locking
 378 
 379   int index;
 380   JavaThread* current_thread = JavaThread::current();
 381   ResourceMark rm(current_thread);
 382 
 383   jvmtiClassDefinition* class_definitions =
 384                             NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
 385   NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
 386 
 387   for (index = 0; index < class_count; index++) {
 388     HandleMark hm(current_thread);
 389 
 390     jclass jcls = classes[index];
 391     oop k_mirror = JNIHandles::resolve_external_guard(jcls);
 392     if (k_mirror == NULL) {
 393       return JVMTI_ERROR_INVALID_CLASS;
 394     }
 395     if (!k_mirror->is_a(SystemDictionary::Class_klass())) {
 396       return JVMTI_ERROR_INVALID_CLASS;
 397     }
 398 
 399     if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
 400       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
 401     }
 402 
 403     Klass* klass = java_lang_Class::as_Klass(k_mirror);

 404 
 405     jint status = klass->jvmti_class_status();
 406     if (status & (JVMTI_CLASS_STATUS_ERROR)) {
 407       return JVMTI_ERROR_INVALID_CLASS;
 408     }
 409 
 410     InstanceKlass* ik = InstanceKlass::cast(klass);
 411     if (ik->get_cached_class_file_bytes() == NULL) {
 412       // Not cached, we need to reconstitute the class file from the
 413       // VM representation. We don't attach the reconstituted class
 414       // bytes to the InstanceKlass here because they have not been
 415       // validated and we're not at a safepoint.
 416       JvmtiClassFileReconstituter reconstituter(ik);
 417       if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
 418         return reconstituter.get_error();
 419       }
 420 
 421       class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
 422       class_definitions[index].class_bytes      = (unsigned char*)
 423                                                        reconstituter.class_file_bytes();
 424     } else {
 425       // it is cached, get it from the cache
 426       class_definitions[index].class_byte_count = ik->get_cached_class_file_len();
 427       class_definitions[index].class_bytes      = ik->get_cached_class_file_bytes();
 428     }
 429     class_definitions[index].klass              = jcls;
 430   }
 431   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
 432   VMThread::execute(&op);
 433   return (op.check_error());
 434 } /* end RetransformClasses */
 435 
 436 
 437 // class_count - pre-checked to be greater than or equal to 0
 438 // class_definitions - pre-checked for NULL
 439 jvmtiError
 440 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
 441 //TODO: add locking
 442   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
 443   VMThread::execute(&op);
 444   return (op.check_error());
 445 } /* end RedefineClasses */
 446 
 447 
 448   //
 449   // Object functions
 450   //
 451 
 452 // size_ptr - pre-checked for NULL
 453 jvmtiError
 454 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
 455   oop mirror = JNIHandles::resolve_external_guard(object);
 456   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
 457   *size_ptr = (jlong)mirror->size() * wordSize;
 458   return JVMTI_ERROR_NONE;
 459 } /* end GetObjectSize */
 460 
 461   //
 462   // Method functions
 463   //
 464 
 465 // prefix - NULL is a valid value, must be checked
 466 jvmtiError
 467 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
 468   return prefix == NULL?
 469               SetNativeMethodPrefixes(0, NULL) :
 470               SetNativeMethodPrefixes(1, (char**)&prefix);
 471 } /* end SetNativeMethodPrefix */
 472 
 473 
 474 // prefix_count - pre-checked to be greater than or equal to 0
 475 // prefixes - pre-checked for NULL
 476 jvmtiError
 477 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
 478   // Have to grab JVMTI thread state lock to be sure that some thread
 479   // isn't accessing the prefixes at the same time we are setting them.
 480   // No locks during VM bring-up.
 481   if (Threads::number_of_threads() == 0) {
 482     return set_native_method_prefixes(prefix_count, prefixes);
 483   } else {
 484     MutexLocker mu(JvmtiThreadState_lock);
 485     return set_native_method_prefixes(prefix_count, prefixes);
 486   }
 487 } /* end SetNativeMethodPrefixes */
 488 
 489   //
 490   // Event Management functions
 491   //
 492 
 493 // callbacks - NULL is a valid value, must be checked
 494 // size_of_callbacks - pre-checked to be greater than or equal to 0
 495 jvmtiError
 496 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
 497   JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
 498   return JVMTI_ERROR_NONE;
 499 } /* end SetEventCallbacks */
 500 
 501 
 502 // event_thread - NULL is a valid value, must be checked
 503 jvmtiError
 504 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread,   ...) {
 505   JavaThread* java_thread = NULL;
 506   if (event_thread != NULL) {
 507     oop thread_oop = JNIHandles::resolve_external_guard(event_thread);
 508     if (thread_oop == NULL) {
 509       return JVMTI_ERROR_INVALID_THREAD;
 510     }
 511     if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
 512       return JVMTI_ERROR_INVALID_THREAD;
 513     }
 514     java_thread = java_lang_Thread::thread(thread_oop);
 515     if (java_thread == NULL) {
 516       return JVMTI_ERROR_THREAD_NOT_ALIVE;
 517     }
 518   }
 519 
 520   // event_type must be valid
 521   if (!JvmtiEventController::is_valid_event_type(event_type)) {
 522     return JVMTI_ERROR_INVALID_EVENT_TYPE;
 523   }
 524 
 525   // global events cannot be controlled at thread level.
 526   if (java_thread != NULL && JvmtiEventController::is_global_event(event_type)) {
 527     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 528   }
 529 
 530   bool enabled = (mode == JVMTI_ENABLE);
 531 
 532   // assure that needed capabilities are present
 533   if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 534     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 535   }
 536 
 537   if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 538     record_class_file_load_hook_enabled();
 539   }
 540   JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);
 541 
 542   return JVMTI_ERROR_NONE;
 543 } /* end SetEventNotificationMode */
 544 
 545   //
 546   // Capability functions
 547   //
 548 
 549 // capabilities_ptr - pre-checked for NULL
 550 jvmtiError
 551 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
 552   JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
 553                                                       get_prohibited_capabilities(),
 554                                                       capabilities_ptr);
 555   return JVMTI_ERROR_NONE;
 556 } /* end GetPotentialCapabilities */
 557 
 558 
 559 // capabilities_ptr - pre-checked for NULL
 560 jvmtiError
 561 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 562   return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
 563                                                    get_prohibited_capabilities(),
 564                                                    capabilities_ptr,
 565                                                    get_capabilities());
 566 } /* end AddCapabilities */
 567 
 568 
 569 // capabilities_ptr - pre-checked for NULL
 570 jvmtiError
 571 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 572   JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
 573   return JVMTI_ERROR_NONE;
 574 } /* end RelinquishCapabilities */
 575 
 576 
 577 // capabilities_ptr - pre-checked for NULL
 578 jvmtiError
 579 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
 580   JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
 581   return JVMTI_ERROR_NONE;
 582 } /* end GetCapabilities */
 583 
 584   //
 585   // Class Loader Search functions
 586   //
 587 
 588 // segment - pre-checked for NULL
 589 jvmtiError
 590 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
 591   jvmtiPhase phase = get_phase();
 592   if (phase == JVMTI_PHASE_ONLOAD) {
 593     Arguments::append_sysclasspath(segment);
 594     return JVMTI_ERROR_NONE;
 595   } else if (use_version_1_0_semantics()) {
 596     // This JvmtiEnv requested version 1.0 semantics and this function
 597     // is only allowed in the ONLOAD phase in version 1.0 so we need to
 598     // return an error here.
 599     return JVMTI_ERROR_WRONG_PHASE;
 600   } else if (phase == JVMTI_PHASE_LIVE) {
 601     // The phase is checked by the wrapper that called this function,
 602     // but this thread could be racing with the thread that is
 603     // terminating the VM so we check one more time.
 604 
 605     // create the zip entry
 606     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, true);
 607     if (zip_entry == NULL) {
 608       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 609     }
 610 
 611     // lock the loader
 612     Thread* thread = Thread::current();
 613     HandleMark hm;
 614     Handle loader_lock = Handle(thread, SystemDictionary::system_loader_lock());
 615 
 616     ObjectLocker ol(loader_lock, thread);
 617 
 618     // add the jar file to the bootclasspath
 619     log_info(class, load)("opened: %s", zip_entry->name());
 620     ClassLoaderExt::append_boot_classpath(zip_entry);
 621     return JVMTI_ERROR_NONE;
 622   } else {
 623     return JVMTI_ERROR_WRONG_PHASE;
 624   }
 625 
 626 } /* end AddToBootstrapClassLoaderSearch */
 627 
 628 
 629 // segment - pre-checked for NULL
 630 jvmtiError
 631 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
 632   jvmtiPhase phase = get_phase();
 633 
 634   if (phase == JVMTI_PHASE_ONLOAD) {
 635     for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
 636       if (strcmp("java.class.path", p->key()) == 0) {
 637         p->append_value(segment);
 638         break;
 639       }
 640     }
 641     return JVMTI_ERROR_NONE;
 642   } else if (phase == JVMTI_PHASE_LIVE) {
 643     // The phase is checked by the wrapper that called this function,
 644     // but this thread could be racing with the thread that is
 645     // terminating the VM so we check one more time.
 646     HandleMark hm;
 647 
 648     // create the zip entry (which will open the zip file and hence
 649     // check that the segment is indeed a zip file).
 650     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false);
 651     if (zip_entry == NULL) {
 652       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 653     }
 654     delete zip_entry;   // no longer needed
 655 
 656     // lock the loader
 657     Thread* THREAD = Thread::current();
 658     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 659 
 660     ObjectLocker ol(loader, THREAD);
 661 
 662     // need the path as java.lang.String
 663     Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
 664     if (HAS_PENDING_EXCEPTION) {
 665       CLEAR_PENDING_EXCEPTION;
 666       return JVMTI_ERROR_INTERNAL;
 667     }
 668 


 669     // Invoke the appendToClassPathForInstrumentation method - if the method
 670     // is not found it means the loader doesn't support adding to the class path
 671     // in the live phase.
 672     {
 673       JavaValue res(T_VOID);
 674       JavaCalls::call_special(&res,
 675                               loader,
 676                               loader->klass(),
 677                               vmSymbols::appendToClassPathForInstrumentation_name(),
 678                               vmSymbols::appendToClassPathForInstrumentation_signature(),
 679                               path,
 680                               THREAD);
 681       if (HAS_PENDING_EXCEPTION) {
 682         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 683         CLEAR_PENDING_EXCEPTION;
 684 
 685         if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
 686           return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
 687         } else {
 688           return JVMTI_ERROR_INTERNAL;
 689         }
 690       }
 691     }
 692 
 693     return JVMTI_ERROR_NONE;
 694   } else {
 695     return JVMTI_ERROR_WRONG_PHASE;
 696   }
 697 } /* end AddToSystemClassLoaderSearch */
 698 
 699   //
 700   // General functions
 701   //
 702 
 703 // phase_ptr - pre-checked for NULL
 704 jvmtiError
 705 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
 706   *phase_ptr = phase();
 707   return JVMTI_ERROR_NONE;
 708 } /* end GetPhase */
 709 
 710 
 711 jvmtiError
 712 JvmtiEnv::DisposeEnvironment() {
 713   dispose();
 714   return JVMTI_ERROR_NONE;
 715 } /* end DisposeEnvironment */
 716 
 717 
 718 // data - NULL is a valid value, must be checked
 719 jvmtiError
 720 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
 721   set_env_local_storage(data);
 722   return JVMTI_ERROR_NONE;
 723 } /* end SetEnvironmentLocalStorage */
 724 
 725 
 726 // data_ptr - pre-checked for NULL
 727 jvmtiError
 728 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
 729   *data_ptr = (void*)get_env_local_storage();
 730   return JVMTI_ERROR_NONE;
 731 } /* end GetEnvironmentLocalStorage */
 732 
 733 // version_ptr - pre-checked for NULL
 734 jvmtiError
 735 JvmtiEnv::GetVersionNumber(jint* version_ptr) {
 736   *version_ptr = JVMTI_VERSION;
 737   return JVMTI_ERROR_NONE;
 738 } /* end GetVersionNumber */
 739 
 740 
 741 // name_ptr - pre-checked for NULL
 742 jvmtiError
 743 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
 744   if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
 745     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 746   }
 747   const char *name = JvmtiUtil::error_name(error);
 748   if (name == NULL) {
 749     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 750   }
 751   size_t len = strlen(name) + 1;
 752   jvmtiError err = allocate(len, (unsigned char**)name_ptr);
 753   if (err == JVMTI_ERROR_NONE) {
 754     memcpy(*name_ptr, name, len);
 755   }
 756   return err;
 757 } /* end GetErrorName */
 758 
 759 
 760 jvmtiError
 761 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
 762   LogLevelType level = value == 0 ? LogLevel::Off : LogLevel::Info;
 763   switch (flag) {
 764   case JVMTI_VERBOSE_OTHER:
 765     // ignore
 766     break;
 767   case JVMTI_VERBOSE_CLASS:
 768     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));
 769     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));
 770     break;
 771   case JVMTI_VERBOSE_GC:
 772     if (value == 0) {
 773       LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(gc));
 774     } else {
 775       LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(gc));
 776     }
 777     break;
 778   case JVMTI_VERBOSE_JNI:
 779     PrintJNIResolving = value != 0;
 780     break;
 781   default:
 782     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 783   };
 784   return JVMTI_ERROR_NONE;
 785 } /* end SetVerboseFlag */
 786 
 787 
 788 // format_ptr - pre-checked for NULL
 789 jvmtiError
 790 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
 791   *format_ptr = JVMTI_JLOCATION_JVMBCI;
 792   return JVMTI_ERROR_NONE;
 793 } /* end GetJLocationFormat */
 794 
 795   //
 796   // Thread functions
 797   //
 798 
 799 // Threads_lock NOT held
 800 // thread - NOT pre-checked
 801 // thread_state_ptr - pre-checked for NULL
 802 jvmtiError
 803 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
 804   jint state;
 805   oop thread_oop;
 806   JavaThread* thr;
 807 
 808   if (thread == NULL) {
 809     thread_oop = JavaThread::current()->threadObj();
 810   } else {
 811     thread_oop = JNIHandles::resolve_external_guard(thread);
 812   }
 813 
 814   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
 815     return JVMTI_ERROR_INVALID_THREAD;
 816   }
 817 
 818   // get most state bits
 819   state = (jint)java_lang_Thread::get_thread_status(thread_oop);
 820 
 821   // add more state bits
 822   thr = java_lang_Thread::thread(thread_oop);
 823   if (thr != NULL) {
 824     JavaThreadState jts = thr->thread_state();
 825 
 826     if (thr->is_being_ext_suspended()) {
 827       state |= JVMTI_THREAD_STATE_SUSPENDED;
 828     }
 829     if (jts == _thread_in_native) {
 830       state |= JVMTI_THREAD_STATE_IN_NATIVE;
 831     }
 832     OSThread* osThread = thr->osthread();
 833     if (osThread != NULL && osThread->interrupted()) {
 834       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 835     }
 836   }
 837 
 838   *thread_state_ptr = state;
 839   return JVMTI_ERROR_NONE;
 840 } /* end GetThreadState */
 841 
 842 
 843 // thread_ptr - pre-checked for NULL
 844 jvmtiError
 845 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
 846   JavaThread* current_thread  = JavaThread::current();
 847   *thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());
 848   return JVMTI_ERROR_NONE;
 849 } /* end GetCurrentThread */
 850 
 851 
 852 // threads_count_ptr - pre-checked for NULL
 853 // threads_ptr - pre-checked for NULL
 854 jvmtiError
 855 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
 856   int nthreads        = 0;
 857   Handle *thread_objs = NULL;
 858   ResourceMark rm;
 859   HandleMark hm;
 860 
 861   // enumerate threads (including agent threads)
 862   ThreadsListEnumerator tle(Thread::current(), true);
 863   nthreads = tle.num_threads();
 864   *threads_count_ptr = nthreads;
 865 
 866   if (nthreads == 0) {
 867     *threads_ptr = NULL;
 868     return JVMTI_ERROR_NONE;
 869   }
 870 
 871   thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
 872   NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 873 
 874   for (int i=0; i < nthreads; i++) {
 875     thread_objs[i] = Handle(tle.get_threadObj(i));
 876   }
 877 
 878   // have to make global handles outside of Threads_lock
 879   jthread *jthreads  = new_jthreadArray(nthreads, thread_objs);
 880   NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
 881 
 882   *threads_ptr = jthreads;
 883   return JVMTI_ERROR_NONE;
 884 } /* end GetAllThreads */
 885 
 886 
 887 // Threads_lock NOT held, java_thread not protected by lock
 888 // java_thread - pre-checked
 889 jvmtiError
 890 JvmtiEnv::SuspendThread(JavaThread* java_thread) {
 891   // don't allow hidden thread suspend request.
 892   if (java_thread->is_hidden_from_external_view()) {
 893     return (JVMTI_ERROR_NONE);
 894   }
 895 
 896   {
 897     MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
 898     if (java_thread->is_external_suspend()) {
 899       // don't allow nested external suspend requests.
 900       return (JVMTI_ERROR_THREAD_SUSPENDED);
 901     }
 902     if (java_thread->is_exiting()) { // thread is in the process of exiting
 903       return (JVMTI_ERROR_THREAD_NOT_ALIVE);
 904     }
 905     java_thread->set_external_suspend();
 906   }
 907 
 908   if (!JvmtiSuspendControl::suspend(java_thread)) {
 909     // the thread was in the process of exiting
 910     return (JVMTI_ERROR_THREAD_NOT_ALIVE);
 911   }
 912   return JVMTI_ERROR_NONE;
 913 } /* end SuspendThread */
 914 
 915 
 916 // request_count - pre-checked to be greater than or equal to 0
 917 // request_list - pre-checked for NULL
 918 // results - pre-checked for NULL
 919 jvmtiError
 920 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
 921   int needSafepoint = 0;  // > 0 if we need a safepoint
 922   for (int i = 0; i < request_count; i++) {
 923     JavaThread *java_thread = get_JavaThread(request_list[i]);
 924     if (java_thread == NULL) {
 925       results[i] = JVMTI_ERROR_INVALID_THREAD;
 926       continue;
 927     }
 928     // the thread has not yet run or has exited (not on threads list)
 929     if (java_thread->threadObj() == NULL) {
 930       results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 931       continue;
 932     }
 933     if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) {
 934       results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 935       continue;
 936     }
 937     // don't allow hidden thread suspend request.
 938     if (java_thread->is_hidden_from_external_view()) {
 939       results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
 940       continue;
 941     }
 942 
 943     {
 944       MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
 945       if (java_thread->is_external_suspend()) {
 946         // don't allow nested external suspend requests.
 947         results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
 948         continue;
 949       }
 950       if (java_thread->is_exiting()) { // thread is in the process of exiting
 951         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 952         continue;
 953       }
 954       java_thread->set_external_suspend();
 955     }
 956     if (java_thread->thread_state() == _thread_in_native) {
 957       // We need to try and suspend native threads here. Threads in
 958       // other states will self-suspend on their next transition.
 959       if (!JvmtiSuspendControl::suspend(java_thread)) {
 960         // The thread was in the process of exiting. Force another
 961         // safepoint to make sure that this thread transitions.
 962         needSafepoint++;
 963         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 964         continue;
 965       }
 966     } else {
 967       needSafepoint++;
 968     }
 969     results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
 970   }
 971   if (needSafepoint > 0) {
 972     VM_ForceSafepoint vfs;
 973     VMThread::execute(&vfs);
 974   }
 975   // per-thread suspend results returned via results parameter
 976   return JVMTI_ERROR_NONE;
 977 } /* end SuspendThreadList */
 978 
 979 
 980 // Threads_lock NOT held, java_thread not protected by lock
 981 // java_thread - pre-checked
 982 jvmtiError
 983 JvmtiEnv::ResumeThread(JavaThread* java_thread) {
 984   // don't allow hidden thread resume request.
 985   if (java_thread->is_hidden_from_external_view()) {
 986     return JVMTI_ERROR_NONE;
 987   }
 988 
 989   if (!java_thread->is_being_ext_suspended()) {
 990     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
 991   }
 992 
 993   if (!JvmtiSuspendControl::resume(java_thread)) {
 994     return JVMTI_ERROR_INTERNAL;
 995   }
 996   return JVMTI_ERROR_NONE;
 997 } /* end ResumeThread */
 998 
 999 
1000 // request_count - pre-checked to be greater than or equal to 0
1001 // request_list - pre-checked for NULL
1002 // results - pre-checked for NULL
1003 jvmtiError
1004 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1005   for (int i = 0; i < request_count; i++) {
1006     JavaThread *java_thread = get_JavaThread(request_list[i]);
1007     if (java_thread == NULL) {
1008       results[i] = JVMTI_ERROR_INVALID_THREAD;
1009       continue;
1010     }
1011     // don't allow hidden thread resume request.
1012     if (java_thread->is_hidden_from_external_view()) {
1013       results[i] = JVMTI_ERROR_NONE;  // indicate successful resume
1014       continue;
1015     }
1016     if (!java_thread->is_being_ext_suspended()) {
1017       results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1018       continue;
1019     }
1020 
1021     if (!JvmtiSuspendControl::resume(java_thread)) {
1022       results[i] = JVMTI_ERROR_INTERNAL;
1023       continue;
1024     }
1025 
1026     results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
1027   }
1028   // per-thread resume results returned via results parameter
1029   return JVMTI_ERROR_NONE;
1030 } /* end ResumeThreadList */
1031 
1032 
1033 // Threads_lock NOT held, java_thread not protected by lock
1034 // java_thread - pre-checked
1035 jvmtiError
1036 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
1037   oop e = JNIHandles::resolve_external_guard(exception);
1038   NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1039 
1040   JavaThread::send_async_exception(java_thread->threadObj(), e);
1041 
1042   return JVMTI_ERROR_NONE;
1043 
1044 } /* end StopThread */
1045 
1046 
1047 // Threads_lock NOT held
1048 // thread - NOT pre-checked
1049 jvmtiError
1050 JvmtiEnv::InterruptThread(jthread thread) {
1051   oop thread_oop = JNIHandles::resolve_external_guard(thread);
1052   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass()))
1053     return JVMTI_ERROR_INVALID_THREAD;
1054 
1055   JavaThread* current_thread  = JavaThread::current();
1056 
1057   // Todo: this is a duplicate of JVM_Interrupt; share code in future
1058   // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
1059   MutexLockerEx ml(current_thread->threadObj() == thread_oop ? NULL : Threads_lock);
1060   // We need to re-resolve the java_thread, since a GC might have happened during the
1061   // acquire of the lock
1062 
1063   JavaThread* java_thread = java_lang_Thread::thread(JNIHandles::resolve_external_guard(thread));
1064   NULL_CHECK(java_thread, JVMTI_ERROR_THREAD_NOT_ALIVE);
1065 
1066   Thread::interrupt(java_thread);
1067 
1068   return JVMTI_ERROR_NONE;
1069 } /* end InterruptThread */
1070 
1071 
1072 // Threads_lock NOT held
1073 // thread - NOT pre-checked
1074 // info_ptr - pre-checked for NULL
1075 jvmtiError
1076 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1077   ResourceMark rm;
1078   HandleMark hm;
1079 
1080   JavaThread* current_thread = JavaThread::current();
1081 
1082   // if thread is NULL the current thread is used
1083   oop thread_oop;
1084   if (thread == NULL) {
1085     thread_oop = current_thread->threadObj();
1086   } else {
1087     thread_oop = JNIHandles::resolve_external_guard(thread);
1088   }
1089   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass()))
1090     return JVMTI_ERROR_INVALID_THREAD;
1091 
1092   Handle thread_obj(current_thread, thread_oop);
1093   Handle name;
1094   ThreadPriority priority;
1095   Handle     thread_group;
1096   Handle context_class_loader;
1097   bool          is_daemon;
1098 
1099   { MutexLocker mu(Threads_lock);
1100 
1101     name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1102     priority = java_lang_Thread::priority(thread_obj());
1103     thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1104     is_daemon = java_lang_Thread::is_daemon(thread_obj());
1105 
1106     oop loader = java_lang_Thread::context_class_loader(thread_obj());
1107     context_class_loader = Handle(current_thread, loader);
1108   }
1109   { const char *n;
1110 
1111     if (name() != NULL) {
1112       n = java_lang_String::as_utf8_string(name());
1113     } else {
1114       int utf8_length = 0;
1115       n = UNICODE::as_utf8((jchar*) NULL, utf8_length);
1116     }
1117 
1118     info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1119     if (info_ptr->name == NULL)
1120       return JVMTI_ERROR_OUT_OF_MEMORY;
1121 
1122     strcpy(info_ptr->name, n);
1123   }
1124   info_ptr->is_daemon = is_daemon;
1125   info_ptr->priority  = priority;
1126 
1127   info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
1128                                      jni_reference(context_class_loader);
1129   info_ptr->thread_group = jni_reference(thread_group);
1130 
1131   return JVMTI_ERROR_NONE;
1132 } /* end GetThreadInfo */
1133 
1134 
1135 // Threads_lock NOT held, java_thread not protected by lock
1136 // java_thread - pre-checked
1137 // owned_monitor_count_ptr - pre-checked for NULL
1138 // owned_monitors_ptr - pre-checked for NULL
1139 jvmtiError
1140 JvmtiEnv::GetOwnedMonitorInfo(JavaThread* java_thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1141   jvmtiError err = JVMTI_ERROR_NONE;
1142   JavaThread* calling_thread = JavaThread::current();
1143 
1144   // growable array of jvmti monitors info on the C-heap
1145   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1146       new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1147 
1148   // It is only safe to perform the direct operation on the current
1149   // thread. All other usage needs to use a vm-safepoint-op for safety.
1150   if (java_thread == calling_thread) {
1151     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1152   } else {
1153     // JVMTI get monitors info at safepoint. Do not require target thread to
1154     // be suspended.
1155     VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1156     VMThread::execute(&op);
1157     err = op.result();
1158   }
1159   jint owned_monitor_count = owned_monitors_list->length();
1160   if (err == JVMTI_ERROR_NONE) {
1161     if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1162                       (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1163       // copy into the returned array
1164       for (int i = 0; i < owned_monitor_count; i++) {
1165         (*owned_monitors_ptr)[i] =
1166           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1167       }
1168       *owned_monitor_count_ptr = owned_monitor_count;
1169     }
1170   }
1171   // clean up.
1172   for (int i = 0; i < owned_monitor_count; i++) {
1173     deallocate((unsigned char*)owned_monitors_list->at(i));
1174   }
1175   delete owned_monitors_list;
1176 
1177   return err;
1178 } /* end GetOwnedMonitorInfo */
1179 
1180 
1181 // Threads_lock NOT held, java_thread not protected by lock
1182 // java_thread - pre-checked
1183 // monitor_info_count_ptr - pre-checked for NULL
1184 // monitor_info_ptr - pre-checked for NULL
1185 jvmtiError
1186 JvmtiEnv::GetOwnedMonitorStackDepthInfo(JavaThread* java_thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1187   jvmtiError err = JVMTI_ERROR_NONE;
1188   JavaThread* calling_thread  = JavaThread::current();
1189 
1190   // growable array of jvmti monitors info on the C-heap
1191   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1192          new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1193 
1194   // It is only safe to perform the direct operation on the current
1195   // thread. All other usage needs to use a vm-safepoint-op for safety.
1196   if (java_thread == calling_thread) {
1197     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1198   } else {
1199     // JVMTI get owned monitors info at safepoint. Do not require target thread to
1200     // be suspended.
1201     VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1202     VMThread::execute(&op);
1203     err = op.result();
1204   }
1205 
1206   jint owned_monitor_count = owned_monitors_list->length();
1207   if (err == JVMTI_ERROR_NONE) {
1208     if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1209                       (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1210       // copy to output array.
1211       for (int i = 0; i < owned_monitor_count; i++) {
1212         (*monitor_info_ptr)[i].monitor =
1213           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1214         (*monitor_info_ptr)[i].stack_depth =
1215           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1216       }
1217     }
1218     *monitor_info_count_ptr = owned_monitor_count;
1219   }
1220 
1221   // clean up.
1222   for (int i = 0; i < owned_monitor_count; i++) {
1223     deallocate((unsigned char*)owned_monitors_list->at(i));
1224   }
1225   delete owned_monitors_list;
1226 
1227   return err;
1228 } /* end GetOwnedMonitorStackDepthInfo */
1229 
1230 
1231 // Threads_lock NOT held, java_thread not protected by lock
1232 // java_thread - pre-checked
1233 // monitor_ptr - pre-checked for NULL
1234 jvmtiError
1235 JvmtiEnv::GetCurrentContendedMonitor(JavaThread* java_thread, jobject* monitor_ptr) {
1236   jvmtiError err = JVMTI_ERROR_NONE;
1237   JavaThread* calling_thread  = JavaThread::current();
1238 
1239   // It is only safe to perform the direct operation on the current
1240   // thread. All other usage needs to use a vm-safepoint-op for safety.
1241   if (java_thread == calling_thread) {
1242     err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
1243   } else {
1244     // get contended monitor information at safepoint.
1245     VM_GetCurrentContendedMonitor op(this, calling_thread, java_thread, monitor_ptr);
1246     VMThread::execute(&op);
1247     err = op.result();
1248   }
1249   return err;
1250 } /* end GetCurrentContendedMonitor */
1251 
1252 
1253 // Threads_lock NOT held
1254 // thread - NOT pre-checked
1255 // proc - pre-checked for NULL
1256 // arg - NULL is a valid value, must be checked
1257 jvmtiError
1258 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1259   oop thread_oop = JNIHandles::resolve_external_guard(thread);
1260   if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
1261     return JVMTI_ERROR_INVALID_THREAD;
1262   }
1263   if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1264     return JVMTI_ERROR_INVALID_PRIORITY;
1265   }
1266 
1267   //Thread-self
1268   JavaThread* current_thread = JavaThread::current();
1269 
1270   Handle thread_hndl(current_thread, thread_oop);
1271   {
1272     MutexLocker mu(Threads_lock); // grab Threads_lock
1273 
1274     JvmtiAgentThread *new_thread = new JvmtiAgentThread(this, proc, arg);
1275 
1276     // At this point it may be possible that no osthread was created for the
1277     // JavaThread due to lack of memory.
1278     if (new_thread == NULL || new_thread->osthread() == NULL) {
1279       if (new_thread) delete new_thread;
1280       return JVMTI_ERROR_OUT_OF_MEMORY;
1281     }
1282 
1283     java_lang_Thread::set_thread(thread_hndl(), new_thread);
1284     java_lang_Thread::set_priority(thread_hndl(), (ThreadPriority)priority);
1285     java_lang_Thread::set_daemon(thread_hndl());
1286 
1287     new_thread->set_threadObj(thread_hndl());
1288     Threads::add(new_thread);
1289     Thread::start(new_thread);
1290   } // unlock Threads_lock
1291 
1292   return JVMTI_ERROR_NONE;
1293 } /* end RunAgentThread */
1294 
1295   //
1296   // Thread Group functions
1297   //
1298 
1299 // group_count_ptr - pre-checked for NULL
1300 // groups_ptr - pre-checked for NULL
1301 jvmtiError
1302 JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
1303   JavaThread* current_thread = JavaThread::current();
1304 
1305   // Only one top level thread group now.
1306   *group_count_ptr = 1;
1307 
1308   // Allocate memory to store global-refs to the thread groups.
1309   // Assume this area is freed by caller.
1310   *groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
1311 
1312   NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
1313 
1314   // Convert oop to Handle, then convert Handle to global-ref.
1315   {
1316     HandleMark hm(current_thread);
1317     Handle system_thread_group(current_thread, Universe::system_thread_group());
1318     *groups_ptr[0] = jni_reference(system_thread_group);
1319   }
1320 
1321   return JVMTI_ERROR_NONE;
1322 } /* end GetTopThreadGroups */
1323 
1324 
1325 // info_ptr - pre-checked for NULL
1326 jvmtiError
1327 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1328   ResourceMark rm;
1329   HandleMark hm;
1330 
1331   JavaThread* current_thread = JavaThread::current();
1332 
1333   Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1334   NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1335 
1336   const char* name;
1337   Handle parent_group;
1338   bool is_daemon;
1339   ThreadPriority max_priority;
1340 
1341   { MutexLocker mu(Threads_lock);
1342 
1343     name         = java_lang_ThreadGroup::name(group_obj());
1344     parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
1345     is_daemon    = java_lang_ThreadGroup::is_daemon(group_obj());
1346     max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1347   }
1348 
1349   info_ptr->is_daemon    = is_daemon;
1350   info_ptr->max_priority = max_priority;
1351   info_ptr->parent       = jni_reference(parent_group);
1352 
1353   if (name != NULL) {
1354     info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1355     NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1356     strcpy(info_ptr->name, name);
1357   } else {
1358     info_ptr->name = NULL;
1359   }
1360 
1361   return JVMTI_ERROR_NONE;
1362 } /* end GetThreadGroupInfo */
1363 
1364 
1365 // thread_count_ptr - pre-checked for NULL
1366 // threads_ptr - pre-checked for NULL
1367 // group_count_ptr - pre-checked for NULL
1368 // groups_ptr - pre-checked for NULL
1369 jvmtiError
1370 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1371   JavaThread* current_thread = JavaThread::current();
1372   oop group_obj = (oop) JNIHandles::resolve_external_guard(group);
1373   NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1374 
1375   Handle *thread_objs = NULL;
1376   Handle *group_objs  = NULL;
1377   int nthreads = 0;
1378   int ngroups = 0;
1379   int hidden_threads = 0;
1380 
1381   ResourceMark rm;
1382   HandleMark hm;
1383 
1384   Handle group_hdl(current_thread, group_obj);
1385 
1386   { MutexLocker mu(Threads_lock);
1387 
1388     nthreads = java_lang_ThreadGroup::nthreads(group_hdl());
1389     ngroups  = java_lang_ThreadGroup::ngroups(group_hdl());
1390 
1391     if (nthreads > 0) {
1392       objArrayOop threads = java_lang_ThreadGroup::threads(group_hdl());
1393       assert(nthreads <= threads->length(), "too many threads");
1394       thread_objs = NEW_RESOURCE_ARRAY(Handle,nthreads);
1395       for (int i=0, j=0; i<nthreads; i++) {
1396         oop thread_obj = threads->obj_at(i);
1397         assert(thread_obj != NULL, "thread_obj is NULL");
1398         JavaThread *javathread = java_lang_Thread::thread(thread_obj);
1399         // Filter out hidden java threads.
1400         if (javathread != NULL && javathread->is_hidden_from_external_view()) {
1401           hidden_threads++;
1402           continue;
1403         }
1404         thread_objs[j++] = Handle(current_thread, thread_obj);
1405       }
1406       nthreads -= hidden_threads;
1407     }
1408     if (ngroups > 0) {
1409       objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
1410       assert(ngroups <= groups->length(), "too many threads");
1411       group_objs = NEW_RESOURCE_ARRAY(Handle,ngroups);
1412       for (int i=0; i<ngroups; i++) {
1413         oop group_obj = groups->obj_at(i);
1414         assert(group_obj != NULL, "group_obj != NULL");
1415         group_objs[i] = Handle(current_thread, group_obj);
1416       }
1417     }
1418   }
1419 
1420   // have to make global handles outside of Threads_lock
1421   *group_count_ptr  = ngroups;
1422   *thread_count_ptr = nthreads;
1423   *threads_ptr     = new_jthreadArray(nthreads, thread_objs);
1424   *groups_ptr      = new_jthreadGroupArray(ngroups, group_objs);
1425   if ((nthreads > 0) && (*threads_ptr == NULL)) {
1426     return JVMTI_ERROR_OUT_OF_MEMORY;
1427   }
1428   if ((ngroups > 0) && (*groups_ptr == NULL)) {
1429     return JVMTI_ERROR_OUT_OF_MEMORY;
1430   }
1431 
1432   return JVMTI_ERROR_NONE;
1433 } /* end GetThreadGroupChildren */
1434 
1435 
1436   //
1437   // Stack Frame functions
1438   //
1439 
1440 // Threads_lock NOT held, java_thread not protected by lock
1441 // java_thread - pre-checked
1442 // max_frame_count - pre-checked to be greater than or equal to 0
1443 // frame_buffer - pre-checked for NULL
1444 // count_ptr - pre-checked for NULL
1445 jvmtiError
1446 JvmtiEnv::GetStackTrace(JavaThread* java_thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1447   jvmtiError err = JVMTI_ERROR_NONE;
1448 
1449   // It is only safe to perform the direct operation on the current
1450   // thread. All other usage needs to use a vm-safepoint-op for safety.
1451   if (java_thread == JavaThread::current()) {
1452     err = get_stack_trace(java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1453   } else {
1454     // JVMTI get stack trace at safepoint. Do not require target thread to
1455     // be suspended.
1456     VM_GetStackTrace op(this, java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1457     VMThread::execute(&op);
1458     err = op.result();
1459   }
1460 
1461   return err;
1462 } /* end GetStackTrace */
1463 
1464 
1465 // max_frame_count - pre-checked to be greater than or equal to 0
1466 // stack_info_ptr - pre-checked for NULL
1467 // thread_count_ptr - pre-checked for NULL
1468 jvmtiError
1469 JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
1470   jvmtiError err = JVMTI_ERROR_NONE;
1471   JavaThread* calling_thread = JavaThread::current();
1472 
1473   // JVMTI get stack traces at safepoint.
1474   VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1475   VMThread::execute(&op);
1476   *thread_count_ptr = op.final_thread_count();
1477   *stack_info_ptr = op.stack_info();
1478   err = op.result();
1479   return err;
1480 } /* end GetAllStackTraces */
1481 
1482 
1483 // thread_count - pre-checked to be greater than or equal to 0
1484 // thread_list - pre-checked for NULL
1485 // max_frame_count - pre-checked to be greater than or equal to 0
1486 // stack_info_ptr - pre-checked for NULL
1487 jvmtiError
1488 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1489   jvmtiError err = JVMTI_ERROR_NONE;
1490   // JVMTI get stack traces at safepoint.
1491   VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1492   VMThread::execute(&op);
1493   err = op.result();
1494   if (err == JVMTI_ERROR_NONE) {
1495     *stack_info_ptr = op.stack_info();
1496   }
1497   return err;
1498 } /* end GetThreadListStackTraces */
1499 
1500 
1501 // Threads_lock NOT held, java_thread not protected by lock
1502 // java_thread - pre-checked
1503 // count_ptr - pre-checked for NULL
1504 jvmtiError
1505 JvmtiEnv::GetFrameCount(JavaThread* java_thread, jint* count_ptr) {
1506   jvmtiError err = JVMTI_ERROR_NONE;
1507 
1508   // retrieve or create JvmtiThreadState.
1509   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1510   if (state == NULL) {
1511     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1512   }
1513 
1514   // It is only safe to perform the direct operation on the current
1515   // thread. All other usage needs to use a vm-safepoint-op for safety.
1516   if (java_thread == JavaThread::current()) {
1517     err = get_frame_count(state, count_ptr);
1518   } else {
1519     // get java stack frame count at safepoint.
1520     VM_GetFrameCount op(this, state, count_ptr);
1521     VMThread::execute(&op);
1522     err = op.result();
1523   }
1524   return err;
1525 } /* end GetFrameCount */
1526 
1527 
1528 // Threads_lock NOT held, java_thread not protected by lock
1529 // java_thread - pre-checked
1530 jvmtiError
1531 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1532   JavaThread* current_thread  = JavaThread::current();
1533   HandleMark hm(current_thread);
1534   uint32_t debug_bits = 0;
1535 
1536   // retrieve or create the state
1537   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1538   if (state == NULL) {
1539     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1540   }
1541 
1542   // Check if java_thread is fully suspended
1543   if (!is_thread_fully_suspended(java_thread, true /* wait for suspend completion */, &debug_bits)) {
1544     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1545   }
1546   // Check to see if a PopFrame was already in progress
1547   if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
1548     // Probably possible for JVMTI clients to trigger this, but the
1549     // JPDA backend shouldn't allow this to happen
1550     return JVMTI_ERROR_INTERNAL;
1551   }
1552 
1553   {
1554     // Was workaround bug
1555     //    4812902: popFrame hangs if the method is waiting at a synchronize
1556     // Catch this condition and return an error to avoid hanging.
1557     // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
1558     OSThread* osThread = java_thread->osthread();
1559     if (osThread->get_state() == MONITOR_WAIT) {
1560       return JVMTI_ERROR_OPAQUE_FRAME;
1561     }
1562   }
1563 
1564   {
1565     ResourceMark rm(current_thread);
1566     // Check if there are more than one Java frame in this thread, that the top two frames
1567     // are Java (not native) frames, and that there is no intervening VM frame
1568     int frame_count = 0;
1569     bool is_interpreted[2];
1570     intptr_t *frame_sp[2];
1571     // The 2-nd arg of constructor is needed to stop iterating at java entry frame.
1572     for (vframeStream vfs(java_thread, true); !vfs.at_end(); vfs.next()) {
1573       methodHandle mh(current_thread, vfs.method());
1574       if (mh->is_native()) return(JVMTI_ERROR_OPAQUE_FRAME);
1575       is_interpreted[frame_count] = vfs.is_interpreted_frame();
1576       frame_sp[frame_count] = vfs.frame_id();
1577       if (++frame_count > 1) break;
1578     }
1579     if (frame_count < 2)  {
1580       // We haven't found two adjacent non-native Java frames on the top.
1581       // There can be two situations here:
1582       //  1. There are no more java frames
1583       //  2. Two top java frames are separated by non-java native frames
1584       if(vframeFor(java_thread, 1) == NULL) {
1585         return JVMTI_ERROR_NO_MORE_FRAMES;
1586       } else {
1587         // Intervening non-java native or VM frames separate java frames.
1588         // Current implementation does not support this. See bug #5031735.
1589         // In theory it is possible to pop frames in such cases.
1590         return JVMTI_ERROR_OPAQUE_FRAME;
1591       }
1592     }
1593 
1594     // If any of the top 2 frames is a compiled one, need to deoptimize it
1595     for (int i = 0; i < 2; i++) {
1596       if (!is_interpreted[i]) {
1597         Deoptimization::deoptimize_frame(java_thread, frame_sp[i]);
1598       }
1599     }
1600 
1601     // Update the thread state to reflect that the top frame is popped
1602     // so that cur_stack_depth is maintained properly and all frameIDs
1603     // are invalidated.
1604     // The current frame will be popped later when the suspended thread
1605     // is resumed and right before returning from VM to Java.
1606     // (see call_VM_base() in assembler_<cpu>.cpp).
1607 
1608     // It's fine to update the thread state here because no JVMTI events
1609     // shall be posted for this PopFrame.
1610 
1611     // It is only safe to perform the direct operation on the current
1612     // thread. All other usage needs to use a vm-safepoint-op for safety.
1613     if (java_thread == JavaThread::current()) {
1614       state->update_for_pop_top_frame();
1615     } else {
1616       VM_UpdateForPopTopFrame op(state);
1617       VMThread::execute(&op);
1618       jvmtiError err = op.result();
1619       if (err != JVMTI_ERROR_NONE) {
1620         return err;
1621       }
1622     }
1623 
1624     java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
1625     // Set pending step flag for this popframe and it is cleared when next
1626     // step event is posted.
1627     state->set_pending_step_for_popframe();
1628   }
1629 
1630   return JVMTI_ERROR_NONE;
1631 } /* end PopFrame */
1632 
1633 
1634 // Threads_lock NOT held, java_thread not protected by lock
1635 // java_thread - pre-checked
1636 // java_thread - unchecked
1637 // depth - pre-checked as non-negative
1638 // method_ptr - pre-checked for NULL
1639 // location_ptr - pre-checked for NULL
1640 jvmtiError
1641 JvmtiEnv::GetFrameLocation(JavaThread* java_thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1642   jvmtiError err = JVMTI_ERROR_NONE;
1643 
1644   // It is only safe to perform the direct operation on the current
1645   // thread. All other usage needs to use a vm-safepoint-op for safety.
1646   if (java_thread == JavaThread::current()) {
1647     err = get_frame_location(java_thread, depth, method_ptr, location_ptr);
1648   } else {
1649     // JVMTI get java stack frame location at safepoint.
1650     VM_GetFrameLocation op(this, java_thread, depth, method_ptr, location_ptr);
1651     VMThread::execute(&op);
1652     err = op.result();
1653   }
1654   return err;
1655 } /* end GetFrameLocation */
1656 
1657 
1658 // Threads_lock NOT held, java_thread not protected by lock
1659 // java_thread - pre-checked
1660 // java_thread - unchecked
1661 // depth - pre-checked as non-negative
1662 jvmtiError
1663 JvmtiEnv::NotifyFramePop(JavaThread* java_thread, jint depth) {
1664   jvmtiError err = JVMTI_ERROR_NONE;
1665   ResourceMark rm;
1666   uint32_t debug_bits = 0;
1667 
1668   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
1669   if (state == NULL) {
1670     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1671   }
1672 
1673   if (!JvmtiEnv::is_thread_fully_suspended(java_thread, true, &debug_bits)) {
1674       return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1675   }
1676 
1677   if (TraceJVMTICalls) {
1678     JvmtiSuspendControl::print();
1679   }
1680 
1681   vframe *vf = vframeFor(java_thread, depth);
1682   if (vf == NULL) {
1683     return JVMTI_ERROR_NO_MORE_FRAMES;
1684   }
1685 
1686   if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
1687     return JVMTI_ERROR_OPAQUE_FRAME;
1688   }
1689 
1690   assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
1691 
1692   // It is only safe to perform the direct operation on the current
1693   // thread. All other usage needs to use a vm-safepoint-op for safety.
1694   if (java_thread == JavaThread::current()) {
1695     int frame_number = state->count_frames() - depth;
1696     state->env_thread_state(this)->set_frame_pop(frame_number);
1697   } else {
1698     VM_SetFramePop op(this, state, depth);
1699     VMThread::execute(&op);
1700     err = op.result();
1701   }
1702   return err;
1703 } /* end NotifyFramePop */
1704 
1705 
1706   //
1707   // Force Early Return functions
1708   //
1709 
1710 // Threads_lock NOT held, java_thread not protected by lock
1711 // java_thread - pre-checked
1712 jvmtiError
1713 JvmtiEnv::ForceEarlyReturnObject(JavaThread* java_thread, jobject value) {
1714   jvalue val;
1715   val.l = value;
1716   return force_early_return(java_thread, val, atos);
1717 } /* end ForceEarlyReturnObject */
1718 
1719 
1720 // Threads_lock NOT held, java_thread not protected by lock
1721 // java_thread - pre-checked
1722 jvmtiError
1723 JvmtiEnv::ForceEarlyReturnInt(JavaThread* java_thread, jint value) {
1724   jvalue val;
1725   val.i = value;
1726   return force_early_return(java_thread, val, itos);
1727 } /* end ForceEarlyReturnInt */
1728 
1729 
1730 // Threads_lock NOT held, java_thread not protected by lock
1731 // java_thread - pre-checked
1732 jvmtiError
1733 JvmtiEnv::ForceEarlyReturnLong(JavaThread* java_thread, jlong value) {
1734   jvalue val;
1735   val.j = value;
1736   return force_early_return(java_thread, val, ltos);
1737 } /* end ForceEarlyReturnLong */
1738 
1739 
1740 // Threads_lock NOT held, java_thread not protected by lock
1741 // java_thread - pre-checked
1742 jvmtiError
1743 JvmtiEnv::ForceEarlyReturnFloat(JavaThread* java_thread, jfloat value) {
1744   jvalue val;
1745   val.f = value;
1746   return force_early_return(java_thread, val, ftos);
1747 } /* end ForceEarlyReturnFloat */
1748 
1749 
1750 // Threads_lock NOT held, java_thread not protected by lock
1751 // java_thread - pre-checked
1752 jvmtiError
1753 JvmtiEnv::ForceEarlyReturnDouble(JavaThread* java_thread, jdouble value) {
1754   jvalue val;
1755   val.d = value;
1756   return force_early_return(java_thread, val, dtos);
1757 } /* end ForceEarlyReturnDouble */
1758 
1759 
1760 // Threads_lock NOT held, java_thread not protected by lock
1761 // java_thread - pre-checked
1762 jvmtiError
1763 JvmtiEnv::ForceEarlyReturnVoid(JavaThread* java_thread) {
1764   jvalue val;
1765   val.j = 0L;
1766   return force_early_return(java_thread, val, vtos);
1767 } /* end ForceEarlyReturnVoid */
1768 
1769 
1770   //
1771   // Heap functions
1772   //
1773 
1774 // klass - NULL is a valid value, must be checked
1775 // initial_object - NULL is a valid value, must be checked
1776 // callbacks - pre-checked for NULL
1777 // user_data - NULL is a valid value, must be checked
1778 jvmtiError
1779 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1780   // check klass if provided
1781   Klass* k = NULL;
1782   if (klass != NULL) {
1783     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1784     if (k_mirror == NULL) {
1785       return JVMTI_ERROR_INVALID_CLASS;
1786     }
1787     if (java_lang_Class::is_primitive(k_mirror)) {
1788       return JVMTI_ERROR_NONE;
1789     }
1790     k = java_lang_Class::as_Klass(k_mirror);
1791     if (klass == NULL) {
1792       return JVMTI_ERROR_INVALID_CLASS;
1793     }
1794   }
1795 
1796   Thread *thread = Thread::current();
1797   HandleMark hm(thread);

1798 
1799   TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
1800   JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, k, initial_object, callbacks, user_data);
1801   return JVMTI_ERROR_NONE;
1802 } /* end FollowReferences */
1803 
1804 
1805 // klass - NULL is a valid value, must be checked
1806 // callbacks - pre-checked for NULL
1807 // user_data - NULL is a valid value, must be checked
1808 jvmtiError
1809 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1810   // check klass if provided
1811   Klass* k = NULL;
1812   if (klass != NULL) {
1813     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1814     if (k_mirror == NULL) {
1815       return JVMTI_ERROR_INVALID_CLASS;
1816     }
1817     if (java_lang_Class::is_primitive(k_mirror)) {
1818       return JVMTI_ERROR_NONE;
1819     }
1820     k = java_lang_Class::as_Klass(k_mirror);
1821     if (k == NULL) {
1822       return JVMTI_ERROR_INVALID_CLASS;
1823     }
1824   }
1825 




1826   TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1827   JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, k, callbacks, user_data);
1828   return JVMTI_ERROR_NONE;
1829 } /* end IterateThroughHeap */
1830 
1831 
1832 // tag_ptr - pre-checked for NULL
1833 jvmtiError
1834 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1835   oop o = JNIHandles::resolve_external_guard(object);
1836   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1837   *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1838   return JVMTI_ERROR_NONE;
1839 } /* end GetTag */
1840 
1841 
1842 jvmtiError
1843 JvmtiEnv::SetTag(jobject object, jlong tag) {
1844   oop o = JNIHandles::resolve_external_guard(object);
1845   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1846   JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1847   return JVMTI_ERROR_NONE;
1848 } /* end SetTag */
1849 
1850 
1851 // tag_count - pre-checked to be greater than or equal to 0
1852 // tags - pre-checked for NULL
1853 // count_ptr - pre-checked for NULL
1854 // object_result_ptr - NULL is a valid value, must be checked
1855 // tag_result_ptr - NULL is a valid value, must be checked
1856 jvmtiError
1857 JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
1858   TraceTime t("GetObjectsWithTags", TRACETIME_LOG(Debug, jvmti, objecttagging));
1859   return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
1860 } /* end GetObjectsWithTags */
1861 
1862 
1863 jvmtiError
1864 JvmtiEnv::ForceGarbageCollection() {
1865   Universe::heap()->collect(GCCause::_jvmti_force_gc);
1866   return JVMTI_ERROR_NONE;
1867 } /* end ForceGarbageCollection */
1868 
1869 
1870   //
1871   // Heap (1.0) functions
1872   //
1873 
1874 // object_reference_callback - pre-checked for NULL
1875 // user_data - NULL is a valid value, must be checked
1876 jvmtiError
1877 JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
1878   oop o = JNIHandles::resolve_external_guard(object);
1879   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1880   JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
1881   return JVMTI_ERROR_NONE;
1882 } /* end IterateOverObjectsReachableFromObject */
1883 
1884 
1885 // heap_root_callback - NULL is a valid value, must be checked
1886 // stack_ref_callback - NULL is a valid value, must be checked
1887 // object_ref_callback - NULL is a valid value, must be checked
1888 // user_data - NULL is a valid value, must be checked
1889 jvmtiError
1890 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
1891   TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
1892   JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
1893   return JVMTI_ERROR_NONE;
1894 } /* end IterateOverReachableObjects */
1895 
1896 
1897 // heap_object_callback - pre-checked for NULL
1898 // user_data - NULL is a valid value, must be checked
1899 jvmtiError
1900 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1901   TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1902   Thread *thread = Thread::current();
1903   HandleMark hm(thread);
1904   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, NULL, heap_object_callback, user_data);
1905   return JVMTI_ERROR_NONE;
1906 } /* end IterateOverHeap */
1907 
1908 
1909 // k_mirror - may be primitive, this must be checked
1910 // heap_object_callback - pre-checked for NULL
1911 // user_data - NULL is a valid value, must be checked
1912 jvmtiError
1913 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1914   if (java_lang_Class::is_primitive(k_mirror)) {
1915     // DO PRIMITIVE CLASS PROCESSING
1916     return JVMTI_ERROR_NONE;
1917   }
1918   Klass* klass = java_lang_Class::as_Klass(k_mirror);
1919   if (klass == NULL) {
1920     return JVMTI_ERROR_INVALID_CLASS;
1921   }



1922   TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
1923   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
1924   return JVMTI_ERROR_NONE;
1925 } /* end IterateOverInstancesOfClass */
1926 
1927 
1928   //
1929   // Local Variable functions
1930   //
1931 
1932 // Threads_lock NOT held, java_thread not protected by lock
1933 // java_thread - pre-checked
1934 // java_thread - unchecked
1935 // depth - pre-checked as non-negative
1936 // value_ptr - pre-checked for NULL
1937 jvmtiError
1938 JvmtiEnv::GetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject* value_ptr) {
1939   JavaThread* current_thread = JavaThread::current();
1940   // rm object is created to clean up the javaVFrame created in
1941   // doit_prologue(), but after doit() is finished with it.
1942   ResourceMark rm(current_thread);
1943 
1944   VM_GetOrSetLocal op(java_thread, current_thread, depth, slot);
1945   VMThread::execute(&op);
1946   jvmtiError err = op.result();
1947   if (err != JVMTI_ERROR_NONE) {
1948     return err;
1949   } else {
1950     *value_ptr = op.value().l;
1951     return JVMTI_ERROR_NONE;
1952   }
1953 } /* end GetLocalObject */
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 - pre-checked for NULL
1960 jvmtiError
1961 JvmtiEnv::GetLocalInstance(JavaThread* java_thread, jint depth, 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_GetReceiver op(java_thread, current_thread, depth);
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 GetLocalInstance */
1977 
1978 
1979 // Threads_lock NOT held, java_thread not protected by lock
1980 // java_thread - pre-checked
1981 // java_thread - unchecked
1982 // depth - pre-checked as non-negative
1983 // value_ptr - pre-checked for NULL
1984 jvmtiError
1985 JvmtiEnv::GetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint* value_ptr) {
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;
1989 
1990   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT);
1991   VMThread::execute(&op);
1992   *value_ptr = op.value().i;
1993   return op.result();
1994 } /* end GetLocalInt */
1995 
1996 
1997 // Threads_lock NOT held, java_thread not protected by lock
1998 // java_thread - pre-checked
1999 // java_thread - unchecked
2000 // depth - pre-checked as non-negative
2001 // value_ptr - pre-checked for NULL
2002 jvmtiError
2003 JvmtiEnv::GetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong* value_ptr) {
2004   // rm object is created to clean up the javaVFrame created in
2005   // doit_prologue(), but after doit() is finished with it.
2006   ResourceMark rm;
2007 
2008   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG);
2009   VMThread::execute(&op);
2010   *value_ptr = op.value().j;
2011   return op.result();
2012 } /* end GetLocalLong */
2013 
2014 
2015 // Threads_lock NOT held, java_thread not protected by lock
2016 // java_thread - pre-checked
2017 // java_thread - unchecked
2018 // depth - pre-checked as non-negative
2019 // value_ptr - pre-checked for NULL
2020 jvmtiError
2021 JvmtiEnv::GetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat* value_ptr) {
2022   // rm object is created to clean up the javaVFrame created in
2023   // doit_prologue(), but after doit() is finished with it.
2024   ResourceMark rm;
2025 
2026   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT);
2027   VMThread::execute(&op);
2028   *value_ptr = op.value().f;
2029   return op.result();
2030 } /* end GetLocalFloat */
2031 
2032 
2033 // Threads_lock NOT held, java_thread not protected by lock
2034 // java_thread - pre-checked
2035 // java_thread - unchecked
2036 // depth - pre-checked as non-negative
2037 // value_ptr - pre-checked for NULL
2038 jvmtiError
2039 JvmtiEnv::GetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble* value_ptr) {
2040   // rm object is created to clean up the javaVFrame created in
2041   // doit_prologue(), but after doit() is finished with it.
2042   ResourceMark rm;
2043 
2044   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE);
2045   VMThread::execute(&op);
2046   *value_ptr = op.value().d;
2047   return op.result();
2048 } /* end GetLocalDouble */
2049 
2050 
2051 // Threads_lock NOT held, java_thread not protected by lock
2052 // java_thread - pre-checked
2053 // java_thread - unchecked
2054 // depth - pre-checked as non-negative
2055 jvmtiError
2056 JvmtiEnv::SetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject value) {
2057   // rm object is created to clean up the javaVFrame created in
2058   // doit_prologue(), but after doit() is finished with it.
2059   ResourceMark rm;
2060   jvalue val;
2061   val.l = value;
2062   VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val);
2063   VMThread::execute(&op);
2064   return op.result();
2065 } /* end SetLocalObject */
2066 
2067 
2068 // Threads_lock NOT held, java_thread not protected by lock
2069 // java_thread - pre-checked
2070 // java_thread - unchecked
2071 // depth - pre-checked as non-negative
2072 jvmtiError
2073 JvmtiEnv::SetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint value) {
2074   // rm object is created to clean up the javaVFrame created in
2075   // doit_prologue(), but after doit() is finished with it.
2076   ResourceMark rm;
2077   jvalue val;
2078   val.i = value;
2079   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val);
2080   VMThread::execute(&op);
2081   return op.result();
2082 } /* end SetLocalInt */
2083 
2084 
2085 // Threads_lock NOT held, java_thread not protected by lock
2086 // java_thread - pre-checked
2087 // java_thread - unchecked
2088 // depth - pre-checked as non-negative
2089 jvmtiError
2090 JvmtiEnv::SetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong value) {
2091   // rm object is created to clean up the javaVFrame created in
2092   // doit_prologue(), but after doit() is finished with it.
2093   ResourceMark rm;
2094   jvalue val;
2095   val.j = value;
2096   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val);
2097   VMThread::execute(&op);
2098   return op.result();
2099 } /* end SetLocalLong */
2100 
2101 
2102 // Threads_lock NOT held, java_thread not protected by lock
2103 // java_thread - pre-checked
2104 // java_thread - unchecked
2105 // depth - pre-checked as non-negative
2106 jvmtiError
2107 JvmtiEnv::SetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat value) {
2108   // rm object is created to clean up the javaVFrame created in
2109   // doit_prologue(), but after doit() is finished with it.
2110   ResourceMark rm;
2111   jvalue val;
2112   val.f = value;
2113   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val);
2114   VMThread::execute(&op);
2115   return op.result();
2116 } /* end SetLocalFloat */
2117 
2118 
2119 // Threads_lock NOT held, java_thread not protected by lock
2120 // java_thread - pre-checked
2121 // java_thread - unchecked
2122 // depth - pre-checked as non-negative
2123 jvmtiError
2124 JvmtiEnv::SetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble value) {
2125   // rm object is created to clean up the javaVFrame created in
2126   // doit_prologue(), but after doit() is finished with it.
2127   ResourceMark rm;
2128   jvalue val;
2129   val.d = value;
2130   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val);
2131   VMThread::execute(&op);
2132   return op.result();
2133 } /* end SetLocalDouble */
2134 
2135 
2136   //
2137   // Breakpoint functions
2138   //
2139 
2140 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2141 jvmtiError
2142 JvmtiEnv::SetBreakpoint(Method* method_oop, jlocation location) {
2143   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2144   if (location < 0) {   // simple invalid location check first
2145     return JVMTI_ERROR_INVALID_LOCATION;
2146   }
2147   // verify that the breakpoint is not past the end of the method
2148   if (location >= (jlocation) method_oop->code_size()) {
2149     return JVMTI_ERROR_INVALID_LOCATION;
2150   }
2151 
2152   ResourceMark rm;
2153   JvmtiBreakpoint bp(method_oop, location);
2154   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2155   if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
2156     return JVMTI_ERROR_DUPLICATE;
2157 
2158   if (TraceJVMTICalls) {
2159     jvmti_breakpoints.print();
2160   }
2161 
2162   return JVMTI_ERROR_NONE;
2163 } /* end SetBreakpoint */
2164 
2165 
2166 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2167 jvmtiError
2168 JvmtiEnv::ClearBreakpoint(Method* method_oop, jlocation location) {
2169   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2170 
2171   if (location < 0) {   // simple invalid location check first
2172     return JVMTI_ERROR_INVALID_LOCATION;
2173   }
2174 
2175   // verify that the breakpoint is not past the end of the method
2176   if (location >= (jlocation) method_oop->code_size()) {
2177     return JVMTI_ERROR_INVALID_LOCATION;
2178   }
2179 
2180   JvmtiBreakpoint bp(method_oop, location);
2181 
2182   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2183   if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
2184     return JVMTI_ERROR_NOT_FOUND;
2185 
2186   if (TraceJVMTICalls) {
2187     jvmti_breakpoints.print();
2188   }
2189 
2190   return JVMTI_ERROR_NONE;
2191 } /* end ClearBreakpoint */
2192 
2193 
2194   //
2195   // Watched Field functions
2196   //
2197 
2198 jvmtiError
2199 JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2200   // make sure we haven't set this watch before
2201   if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
2202   fdesc_ptr->set_is_field_access_watched(true);
2203 
2204   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
2205 
2206   return JVMTI_ERROR_NONE;
2207 } /* end SetFieldAccessWatch */
2208 
2209 
2210 jvmtiError
2211 JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2212   // make sure we have a watch to clear
2213   if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
2214   fdesc_ptr->set_is_field_access_watched(false);
2215 
2216   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
2217 
2218   return JVMTI_ERROR_NONE;
2219 } /* end ClearFieldAccessWatch */
2220 
2221 
2222 jvmtiError
2223 JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2224   // make sure we haven't set this watch before
2225   if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
2226   fdesc_ptr->set_is_field_modification_watched(true);
2227 
2228   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
2229 
2230   return JVMTI_ERROR_NONE;
2231 } /* end SetFieldModificationWatch */
2232 
2233 
2234 jvmtiError
2235 JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2236    // make sure we have a watch to clear
2237   if (!fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_NOT_FOUND;
2238   fdesc_ptr->set_is_field_modification_watched(false);
2239 
2240   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, false);
2241 
2242   return JVMTI_ERROR_NONE;
2243 } /* end ClearFieldModificationWatch */
2244 
2245   //
2246   // Class functions
2247   //
2248 
2249 
2250 // k_mirror - may be primitive, this must be checked
2251 // signature_ptr - NULL is a valid value, must be checked
2252 // generic_ptr - NULL is a valid value, must be checked
2253 jvmtiError
2254 JvmtiEnv::GetClassSignature(oop k_mirror, char** signature_ptr, char** generic_ptr) {
2255   ResourceMark rm;
2256   bool isPrimitive = java_lang_Class::is_primitive(k_mirror);
2257   Klass* k = NULL;
2258   if (!isPrimitive) {
2259     k = java_lang_Class::as_Klass(k_mirror);
2260     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2261   }
2262   if (signature_ptr != NULL) {
2263     char* result = NULL;
2264     if (isPrimitive) {
2265       char tchar = type2char(java_lang_Class::primitive_type(k_mirror));
2266       result = (char*) jvmtiMalloc(2);
2267       result[0] = tchar;
2268       result[1] = '\0';
2269     } else {
2270       const char* class_sig = k->signature_name();
2271       result = (char *) jvmtiMalloc(strlen(class_sig)+1);
2272       strcpy(result, class_sig);
2273     }
2274     *signature_ptr = result;
2275   }
2276   if (generic_ptr != NULL) {
2277     *generic_ptr = NULL;
2278     if (!isPrimitive && k->is_instance_klass()) {
2279       Symbol* soo = InstanceKlass::cast(k)->generic_signature();
2280       if (soo != NULL) {
2281         const char *gen_sig = soo->as_C_string();
2282         if (gen_sig != NULL) {
2283           char* gen_result;
2284           jvmtiError err = allocate(strlen(gen_sig) + 1,
2285                                     (unsigned char **)&gen_result);
2286           if (err != JVMTI_ERROR_NONE) {
2287             return err;
2288           }
2289           strcpy(gen_result, gen_sig);
2290           *generic_ptr = gen_result;
2291         }
2292       }
2293     }
2294   }
2295   return JVMTI_ERROR_NONE;
2296 } /* end GetClassSignature */
2297 
2298 
2299 // k_mirror - may be primitive, this must be checked
2300 // status_ptr - pre-checked for NULL
2301 jvmtiError
2302 JvmtiEnv::GetClassStatus(oop k_mirror, jint* status_ptr) {
2303   jint result = 0;
2304   if (java_lang_Class::is_primitive(k_mirror)) {
2305     result |= JVMTI_CLASS_STATUS_PRIMITIVE;
2306   } else {
2307     Klass* k = java_lang_Class::as_Klass(k_mirror);
2308     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2309     result = k->jvmti_class_status();
2310   }
2311   *status_ptr = result;
2312 
2313   return JVMTI_ERROR_NONE;
2314 } /* end GetClassStatus */
2315 
2316 
2317 // k_mirror - may be primitive, this must be checked
2318 // source_name_ptr - pre-checked for NULL
2319 jvmtiError
2320 JvmtiEnv::GetSourceFileName(oop k_mirror, char** source_name_ptr) {
2321   if (java_lang_Class::is_primitive(k_mirror)) {
2322      return JVMTI_ERROR_ABSENT_INFORMATION;
2323   }
2324   Klass* k_klass = java_lang_Class::as_Klass(k_mirror);
2325   NULL_CHECK(k_klass, JVMTI_ERROR_INVALID_CLASS);
2326 
2327   if (!k_klass->is_instance_klass()) {
2328     return JVMTI_ERROR_ABSENT_INFORMATION;
2329   }
2330 
2331   Symbol* sfnOop = InstanceKlass::cast(k_klass)->source_file_name();
2332   NULL_CHECK(sfnOop, JVMTI_ERROR_ABSENT_INFORMATION);
2333   {
2334     JavaThread* current_thread  = JavaThread::current();
2335     ResourceMark rm(current_thread);
2336     const char* sfncp = (const char*) sfnOop->as_C_string();
2337     *source_name_ptr = (char *) jvmtiMalloc(strlen(sfncp)+1);
2338     strcpy(*source_name_ptr, sfncp);
2339   }
2340 
2341   return JVMTI_ERROR_NONE;
2342 } /* end GetSourceFileName */
2343 
2344 
2345 // k_mirror - may be primitive, this must be checked
2346 // modifiers_ptr - pre-checked for NULL
2347 jvmtiError
2348 JvmtiEnv::GetClassModifiers(oop k_mirror, jint* modifiers_ptr) {
2349   JavaThread* current_thread  = JavaThread::current();
2350   jint result = 0;
2351   if (!java_lang_Class::is_primitive(k_mirror)) {
2352     Klass* k = java_lang_Class::as_Klass(k_mirror);
2353     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2354     result = k->compute_modifier_flags(current_thread);
2355     JavaThread* THREAD = current_thread; // pass to macros
2356     if (HAS_PENDING_EXCEPTION) {
2357       CLEAR_PENDING_EXCEPTION;
2358       return JVMTI_ERROR_INTERNAL;
2359     };
2360 
2361     // Reset the deleted  ACC_SUPER bit ( deleted in compute_modifier_flags()).
2362     if(k->is_super()) {
2363       result |= JVM_ACC_SUPER;
2364     }
2365   } else {
2366     result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
2367   }
2368   *modifiers_ptr = result;
2369 
2370   return JVMTI_ERROR_NONE;
2371 } /* end GetClassModifiers */
2372 
2373 
2374 // k_mirror - may be primitive, this must be checked
2375 // method_count_ptr - pre-checked for NULL
2376 // methods_ptr - pre-checked for NULL
2377 jvmtiError
2378 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
2379   JavaThread* current_thread  = JavaThread::current();
2380   HandleMark hm(current_thread);
2381 
2382   if (java_lang_Class::is_primitive(k_mirror)) {
2383     *method_count_ptr = 0;
2384     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2385     return JVMTI_ERROR_NONE;
2386   }
2387   Klass* k = java_lang_Class::as_Klass(k_mirror);
2388   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2389 
2390   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2391   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2392     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2393   }
2394 
2395   if (!k->is_instance_klass()) {
2396     *method_count_ptr = 0;
2397     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2398     return JVMTI_ERROR_NONE;
2399   }
2400   InstanceKlass* ik = InstanceKlass::cast(k);
2401   // Allocate the result and fill it in
2402   int result_length = ik->methods()->length();
2403   jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2404   int index;
2405   bool jmethodids_found = true;
2406 
2407   if (JvmtiExport::can_maintain_original_method_order()) {
2408     // Use the original method ordering indices stored in the class, so we can emit
2409     // jmethodIDs in the order they appeared in the class file
2410     for (index = 0; index < result_length; index++) {
2411       Method* m = ik->methods()->at(index);
2412       int original_index = ik->method_ordering()->at(index);
2413       assert(original_index >= 0 && original_index < result_length, "invalid original method index");
2414       jmethodID id;
2415       if (jmethodids_found) {
2416         id = m->find_jmethod_id_or_null();
2417         if (id == NULL) {
2418           // If we find an uninitialized value, make sure there is
2419           // enough space for all the uninitialized values we might
2420           // find.
2421           ik->ensure_space_for_methodids(index);
2422           jmethodids_found = false;
2423           id = m->jmethod_id();
2424         }
2425       } else {
2426         id = m->jmethod_id();
2427       }
2428       result_list[original_index] = id;
2429     }
2430   } else {
2431     // otherwise just copy in any order
2432     for (index = 0; index < result_length; index++) {
2433       Method* m = ik->methods()->at(index);
2434       jmethodID id;
2435       if (jmethodids_found) {
2436         id = m->find_jmethod_id_or_null();
2437         if (id == NULL) {
2438           // If we find an uninitialized value, make sure there is
2439           // enough space for all the uninitialized values we might
2440           // find.
2441           ik->ensure_space_for_methodids(index);
2442           jmethodids_found = false;
2443           id = m->jmethod_id();
2444         }
2445       } else {
2446         id = m->jmethod_id();
2447       }
2448       result_list[index] = id;
2449     }
2450   }
2451   // Fill in return value.
2452   *method_count_ptr = result_length;
2453   *methods_ptr = result_list;
2454 
2455   return JVMTI_ERROR_NONE;
2456 } /* end GetClassMethods */
2457 
2458 
2459 // k_mirror - may be primitive, this must be checked
2460 // field_count_ptr - pre-checked for NULL
2461 // fields_ptr - pre-checked for NULL
2462 jvmtiError
2463 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
2464   if (java_lang_Class::is_primitive(k_mirror)) {
2465     *field_count_ptr = 0;
2466     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2467     return JVMTI_ERROR_NONE;
2468   }
2469   JavaThread* current_thread = JavaThread::current();
2470   HandleMark hm(current_thread);
2471   Klass* k = java_lang_Class::as_Klass(k_mirror);
2472   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2473 
2474   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2475   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2476     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2477   }
2478 
2479   if (!k->is_instance_klass()) {
2480     *field_count_ptr = 0;
2481     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2482     return JVMTI_ERROR_NONE;
2483   }
2484 
2485 
2486   InstanceKlass* ik = InstanceKlass::cast(k);
2487 
2488   int result_count = 0;
2489   // First, count the fields.
2490   FilteredFieldStream flds(ik, true, true);
2491   result_count = flds.field_count();
2492 
2493   // Allocate the result and fill it in
2494   jfieldID* result_list = (jfieldID*) jvmtiMalloc(result_count * sizeof(jfieldID));
2495   // The JVMTI spec requires fields in the order they occur in the class file,
2496   // this is the reverse order of what FieldStream hands out.
2497   int id_index = (result_count - 1);
2498 
2499   for (FilteredFieldStream src_st(ik, true, true); !src_st.eos(); src_st.next()) {
2500     result_list[id_index--] = jfieldIDWorkaround::to_jfieldID(
2501                                             ik, src_st.offset(),
2502                                             src_st.access_flags().is_static());
2503   }
2504   assert(id_index == -1, "just checking");
2505   // Fill in the results
2506   *field_count_ptr = result_count;
2507   *fields_ptr = result_list;
2508 
2509   return JVMTI_ERROR_NONE;
2510 } /* end GetClassFields */
2511 
2512 
2513 // k_mirror - may be primitive, this must be checked
2514 // interface_count_ptr - pre-checked for NULL
2515 // interfaces_ptr - pre-checked for NULL
2516 jvmtiError
2517 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2518   {
2519     if (java_lang_Class::is_primitive(k_mirror)) {
2520       *interface_count_ptr = 0;
2521       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2522       return JVMTI_ERROR_NONE;
2523     }
2524     JavaThread* current_thread = JavaThread::current();
2525     HandleMark hm(current_thread);
2526     Klass* k = java_lang_Class::as_Klass(k_mirror);
2527     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2528 
2529     // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2530     if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2531       return JVMTI_ERROR_CLASS_NOT_PREPARED;
2532 
2533     if (!k->is_instance_klass()) {
2534       *interface_count_ptr = 0;
2535       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2536       return JVMTI_ERROR_NONE;
2537     }
2538 
2539     Array<Klass*>* interface_list = InstanceKlass::cast(k)->local_interfaces();
2540     const int result_length = (interface_list == NULL ? 0 : interface_list->length());
2541     jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2542     for (int i_index = 0; i_index < result_length; i_index += 1) {
2543       Klass* klass_at = interface_list->at(i_index);
2544       assert(klass_at->is_klass(), "interfaces must be Klass*s");
2545       assert(klass_at->is_interface(), "interfaces must be interfaces");
2546       oop mirror_at = klass_at->java_mirror();
2547       Handle handle_at = Handle(current_thread, mirror_at);
2548       result_list[i_index] = (jclass) jni_reference(handle_at);
2549     }
2550     *interface_count_ptr = result_length;
2551     *interfaces_ptr = result_list;
2552   }
2553 
2554   return JVMTI_ERROR_NONE;
2555 } /* end GetImplementedInterfaces */
2556 
2557 
2558 // k_mirror - may be primitive, this must be checked
2559 // minor_version_ptr - pre-checked for NULL
2560 // major_version_ptr - pre-checked for NULL
2561 jvmtiError
2562 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2563   if (java_lang_Class::is_primitive(k_mirror)) {
2564     return JVMTI_ERROR_ABSENT_INFORMATION;
2565   }
2566   Klass* klass = java_lang_Class::as_Klass(k_mirror);



2567 
2568   jint status = klass->jvmti_class_status();
2569   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2570     return JVMTI_ERROR_INVALID_CLASS;
2571   }
2572   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2573     return JVMTI_ERROR_ABSENT_INFORMATION;
2574   }
2575 
2576   InstanceKlass* ik = InstanceKlass::cast(klass);
2577   *minor_version_ptr = ik->minor_version();
2578   *major_version_ptr = ik->major_version();
2579 
2580   return JVMTI_ERROR_NONE;
2581 } /* end GetClassVersionNumbers */
2582 
2583 
2584 // k_mirror - may be primitive, this must be checked
2585 // constant_pool_count_ptr - pre-checked for NULL
2586 // constant_pool_byte_count_ptr - pre-checked for NULL
2587 // constant_pool_bytes_ptr - pre-checked for NULL
2588 jvmtiError
2589 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2590   if (java_lang_Class::is_primitive(k_mirror)) {
2591     return JVMTI_ERROR_ABSENT_INFORMATION;
2592   }
2593 
2594   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2595   Thread *thread = Thread::current();

2596   ResourceMark rm(thread);

2597 
2598   jint status = klass->jvmti_class_status();
2599   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2600     return JVMTI_ERROR_INVALID_CLASS;
2601   }
2602   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2603     return JVMTI_ERROR_ABSENT_INFORMATION;
2604   }
2605 
2606   InstanceKlass* ik = InstanceKlass::cast(klass);
2607   JvmtiConstantPoolReconstituter reconstituter(ik);
2608   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2609     return reconstituter.get_error();
2610   }
2611 
2612   unsigned char *cpool_bytes;
2613   int cpool_size = reconstituter.cpool_size();
2614   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2615     return reconstituter.get_error();
2616   }
2617   jvmtiError res = allocate(cpool_size, &cpool_bytes);
2618   if (res != JVMTI_ERROR_NONE) {
2619     return res;
2620   }
2621   reconstituter.copy_cpool_bytes(cpool_bytes);
2622   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2623     return reconstituter.get_error();
2624   }
2625 
2626   constantPoolHandle  constants(thread, ik->constants());
2627   *constant_pool_count_ptr      = constants->length();
2628   *constant_pool_byte_count_ptr = cpool_size;
2629   *constant_pool_bytes_ptr      = cpool_bytes;
2630 
2631   return JVMTI_ERROR_NONE;
2632 } /* end GetConstantPool */
2633 
2634 
2635 // k_mirror - may be primitive, this must be checked
2636 // is_interface_ptr - pre-checked for NULL
2637 jvmtiError
2638 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2639   {
2640     bool result = false;
2641     if (!java_lang_Class::is_primitive(k_mirror)) {
2642       Klass* k = java_lang_Class::as_Klass(k_mirror);
2643       if (k != NULL && k->is_interface()) {
2644         result = true;
2645       }
2646     }
2647     *is_interface_ptr = result;
2648   }
2649 
2650   return JVMTI_ERROR_NONE;
2651 } /* end IsInterface */
2652 
2653 
2654 // k_mirror - may be primitive, this must be checked
2655 // is_array_class_ptr - pre-checked for NULL
2656 jvmtiError
2657 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
2658   {
2659     bool result = false;
2660     if (!java_lang_Class::is_primitive(k_mirror)) {
2661       Klass* k = java_lang_Class::as_Klass(k_mirror);
2662       if (k != NULL && k->is_array_klass()) {
2663         result = true;
2664       }
2665     }
2666     *is_array_class_ptr = result;
2667   }
2668 
2669   return JVMTI_ERROR_NONE;
2670 } /* end IsArrayClass */
2671 
2672 
2673 // k_mirror - may be primitive, this must be checked
2674 // classloader_ptr - pre-checked for NULL
2675 jvmtiError
2676 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
2677   {
2678     if (java_lang_Class::is_primitive(k_mirror)) {
2679       *classloader_ptr = (jclass) jni_reference(Handle());
2680       return JVMTI_ERROR_NONE;
2681     }
2682     JavaThread* current_thread = JavaThread::current();
2683     HandleMark hm(current_thread);
2684     Klass* k = java_lang_Class::as_Klass(k_mirror);
2685     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2686 
2687     oop result_oop = k->class_loader();
2688     if (result_oop == NULL) {
2689       *classloader_ptr = (jclass) jni_reference(Handle());
2690       return JVMTI_ERROR_NONE;
2691     }
2692     Handle result_handle = Handle(current_thread, result_oop);
2693     jclass result_jnihandle = (jclass) jni_reference(result_handle);
2694     *classloader_ptr = result_jnihandle;
2695   }
2696   return JVMTI_ERROR_NONE;
2697 } /* end GetClassLoader */
2698 
2699 
2700 // k_mirror - may be primitive, this must be checked
2701 // source_debug_extension_ptr - pre-checked for NULL
2702 jvmtiError
2703 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
2704   {
2705     if (java_lang_Class::is_primitive(k_mirror)) {
2706       return JVMTI_ERROR_ABSENT_INFORMATION;
2707     }
2708     Klass* k = java_lang_Class::as_Klass(k_mirror);
2709     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2710     if (!k->is_instance_klass()) {
2711       return JVMTI_ERROR_ABSENT_INFORMATION;
2712     }
2713     const char* sde = InstanceKlass::cast(k)->source_debug_extension();
2714     NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION);
2715 
2716     {
2717       *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1);
2718       strcpy(*source_debug_extension_ptr, sde);
2719     }
2720   }
2721 
2722   return JVMTI_ERROR_NONE;
2723 } /* end GetSourceDebugExtension */
2724 
2725   //
2726   // Object functions
2727   //
2728 
2729 // hash_code_ptr - pre-checked for NULL
2730 jvmtiError
2731 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
2732   oop mirror = JNIHandles::resolve_external_guard(object);
2733   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
2734   NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
2735 
2736   {
2737     jint result = (jint) mirror->identity_hash();
2738     *hash_code_ptr = result;
2739   }
2740   return JVMTI_ERROR_NONE;
2741 } /* end GetObjectHashCode */
2742 
2743 
2744 // info_ptr - pre-checked for NULL
2745 jvmtiError
2746 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
2747   JavaThread* calling_thread = JavaThread::current();
2748   jvmtiError err = get_object_monitor_usage(calling_thread, object, info_ptr);
2749   if (err == JVMTI_ERROR_THREAD_NOT_SUSPENDED) {
2750     // Some of the critical threads were not suspended. go to a safepoint and try again
2751     VM_GetObjectMonitorUsage op(this, calling_thread, object, info_ptr);
2752     VMThread::execute(&op);
2753     err = op.result();
2754   }
2755   return err;
2756 } /* end GetObjectMonitorUsage */
2757 
2758 
2759   //
2760   // Field functions
2761   //
2762 
2763 // name_ptr - NULL is a valid value, must be checked
2764 // signature_ptr - NULL is a valid value, must be checked
2765 // generic_ptr - NULL is a valid value, must be checked
2766 jvmtiError
2767 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2768   JavaThread* current_thread  = JavaThread::current();
2769   ResourceMark rm(current_thread);
2770   if (name_ptr == NULL) {
2771     // just don't return the name
2772   } else {
2773     const char* fieldName = fdesc_ptr->name()->as_C_string();
2774     *name_ptr =  (char*) jvmtiMalloc(strlen(fieldName) + 1);
2775     if (*name_ptr == NULL)
2776       return JVMTI_ERROR_OUT_OF_MEMORY;
2777     strcpy(*name_ptr, fieldName);
2778   }
2779   if (signature_ptr== NULL) {
2780     // just don't return the signature
2781   } else {
2782     const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
2783     *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
2784     if (*signature_ptr == NULL)
2785       return JVMTI_ERROR_OUT_OF_MEMORY;
2786     strcpy(*signature_ptr, fieldSignature);
2787   }
2788   if (generic_ptr != NULL) {
2789     *generic_ptr = NULL;
2790     Symbol* soop = fdesc_ptr->generic_signature();
2791     if (soop != NULL) {
2792       const char* gen_sig = soop->as_C_string();
2793       if (gen_sig != NULL) {
2794         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2795         if (err != JVMTI_ERROR_NONE) {
2796           return err;
2797         }
2798         strcpy(*generic_ptr, gen_sig);
2799       }
2800     }
2801   }
2802   return JVMTI_ERROR_NONE;
2803 } /* end GetFieldName */
2804 
2805 
2806 // declaring_class_ptr - pre-checked for NULL
2807 jvmtiError
2808 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
2809 
2810   *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
2811   return JVMTI_ERROR_NONE;
2812 } /* end GetFieldDeclaringClass */
2813 
2814 
2815 // modifiers_ptr - pre-checked for NULL
2816 jvmtiError
2817 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
2818 
2819   AccessFlags resultFlags = fdesc_ptr->access_flags();
2820   jint result = resultFlags.as_int();
2821   *modifiers_ptr = result;
2822 
2823   return JVMTI_ERROR_NONE;
2824 } /* end GetFieldModifiers */
2825 
2826 
2827 // is_synthetic_ptr - pre-checked for NULL
2828 jvmtiError
2829 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
2830   *is_synthetic_ptr = fdesc_ptr->is_synthetic();
2831   return JVMTI_ERROR_NONE;
2832 } /* end IsFieldSynthetic */
2833 
2834 
2835   //
2836   // Method functions
2837   //
2838 
2839 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2840 // name_ptr - NULL is a valid value, must be checked
2841 // signature_ptr - NULL is a valid value, must be checked
2842 // generic_ptr - NULL is a valid value, must be checked
2843 jvmtiError
2844 JvmtiEnv::GetMethodName(Method* method_oop, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2845   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2846   JavaThread* current_thread  = JavaThread::current();
2847 
2848   ResourceMark rm(current_thread); // get the utf8 name and signature
2849   if (name_ptr == NULL) {
2850     // just don't return the name
2851   } else {
2852     const char* utf8_name = (const char *) method_oop->name()->as_utf8();
2853     *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
2854     strcpy(*name_ptr, utf8_name);
2855   }
2856   if (signature_ptr == NULL) {
2857     // just don't return the signature
2858   } else {
2859     const char* utf8_signature = (const char *) method_oop->signature()->as_utf8();
2860     *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
2861     strcpy(*signature_ptr, utf8_signature);
2862   }
2863 
2864   if (generic_ptr != NULL) {
2865     *generic_ptr = NULL;
2866     Symbol* soop = method_oop->generic_signature();
2867     if (soop != NULL) {
2868       const char* gen_sig = soop->as_C_string();
2869       if (gen_sig != NULL) {
2870         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2871         if (err != JVMTI_ERROR_NONE) {
2872           return err;
2873         }
2874         strcpy(*generic_ptr, gen_sig);
2875       }
2876     }
2877   }
2878   return JVMTI_ERROR_NONE;
2879 } /* end GetMethodName */
2880 
2881 
2882 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2883 // declaring_class_ptr - pre-checked for NULL
2884 jvmtiError
2885 JvmtiEnv::GetMethodDeclaringClass(Method* method_oop, jclass* declaring_class_ptr) {
2886   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2887   (*declaring_class_ptr) = get_jni_class_non_null(method_oop->method_holder());
2888   return JVMTI_ERROR_NONE;
2889 } /* end GetMethodDeclaringClass */
2890 
2891 
2892 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2893 // modifiers_ptr - pre-checked for NULL
2894 jvmtiError
2895 JvmtiEnv::GetMethodModifiers(Method* method_oop, jint* modifiers_ptr) {
2896   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2897   (*modifiers_ptr) = method_oop->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2898   return JVMTI_ERROR_NONE;
2899 } /* end GetMethodModifiers */
2900 
2901 
2902 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2903 // max_ptr - pre-checked for NULL
2904 jvmtiError
2905 JvmtiEnv::GetMaxLocals(Method* method_oop, jint* max_ptr) {
2906   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2907   // get max stack
2908   (*max_ptr) = method_oop->max_locals();
2909   return JVMTI_ERROR_NONE;
2910 } /* end GetMaxLocals */
2911 
2912 
2913 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2914 // size_ptr - pre-checked for NULL
2915 jvmtiError
2916 JvmtiEnv::GetArgumentsSize(Method* method_oop, jint* size_ptr) {
2917   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2918   // get size of arguments
2919 
2920   (*size_ptr) = method_oop->size_of_parameters();
2921   return JVMTI_ERROR_NONE;
2922 } /* end GetArgumentsSize */
2923 
2924 
2925 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2926 // entry_count_ptr - pre-checked for NULL
2927 // table_ptr - pre-checked for NULL
2928 jvmtiError
2929 JvmtiEnv::GetLineNumberTable(Method* method_oop, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
2930   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2931   if (!method_oop->has_linenumber_table()) {
2932     return (JVMTI_ERROR_ABSENT_INFORMATION);
2933   }
2934 
2935   // The line number table is compressed so we don't know how big it is until decompressed.
2936   // Decompression is really fast so we just do it twice.
2937 
2938   // Compute size of table
2939   jint num_entries = 0;
2940   CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
2941   while (stream.read_pair()) {
2942     num_entries++;
2943   }
2944   jvmtiLineNumberEntry *jvmti_table =
2945             (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
2946 
2947   // Fill jvmti table
2948   if (num_entries > 0) {
2949     int index = 0;
2950     CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
2951     while (stream.read_pair()) {
2952       jvmti_table[index].start_location = (jlocation) stream.bci();
2953       jvmti_table[index].line_number = (jint) stream.line();
2954       index++;
2955     }
2956     assert(index == num_entries, "sanity check");
2957   }
2958 
2959   // Set up results
2960   (*entry_count_ptr) = num_entries;
2961   (*table_ptr) = jvmti_table;
2962 
2963   return JVMTI_ERROR_NONE;
2964 } /* end GetLineNumberTable */
2965 
2966 
2967 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2968 // start_location_ptr - pre-checked for NULL
2969 // end_location_ptr - pre-checked for NULL
2970 jvmtiError
2971 JvmtiEnv::GetMethodLocation(Method* method_oop, jlocation* start_location_ptr, jlocation* end_location_ptr) {
2972 
2973   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2974   // get start and end location
2975   (*end_location_ptr) = (jlocation) (method_oop->code_size() - 1);
2976   if (method_oop->code_size() == 0) {
2977     // there is no code so there is no start location
2978     (*start_location_ptr) = (jlocation)(-1);
2979   } else {
2980     (*start_location_ptr) = (jlocation)(0);
2981   }
2982 
2983   return JVMTI_ERROR_NONE;
2984 } /* end GetMethodLocation */
2985 
2986 
2987 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2988 // entry_count_ptr - pre-checked for NULL
2989 // table_ptr - pre-checked for NULL
2990 jvmtiError
2991 JvmtiEnv::GetLocalVariableTable(Method* method_oop, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
2992 
2993   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2994   JavaThread* current_thread  = JavaThread::current();
2995 
2996   // does the klass have any local variable information?
2997   InstanceKlass* ik = method_oop->method_holder();
2998   if (!ik->access_flags().has_localvariable_table()) {
2999     return (JVMTI_ERROR_ABSENT_INFORMATION);
3000   }
3001 
3002   ConstantPool* constants = method_oop->constants();
3003   NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
3004 
3005   // in the vm localvariable table representation, 6 consecutive elements in the table
3006   // represent a 6-tuple of shorts
3007   // [start_pc, length, name_index, descriptor_index, signature_index, index]
3008   jint num_entries = method_oop->localvariable_table_length();
3009   jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
3010                 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
3011 
3012   if (num_entries > 0) {
3013     LocalVariableTableElement* table = method_oop->localvariable_table_start();
3014     for (int i = 0; i < num_entries; i++) {
3015       // get the 5 tuple information from the vm table
3016       jlocation start_location = (jlocation) table[i].start_bci;
3017       jint length = (jint) table[i].length;
3018       int name_index = (int) table[i].name_cp_index;
3019       int signature_index = (int) table[i].descriptor_cp_index;
3020       int generic_signature_index = (int) table[i].signature_cp_index;
3021       jint slot = (jint) table[i].slot;
3022 
3023       // get utf8 name and signature
3024       char *name_buf = NULL;
3025       char *sig_buf = NULL;
3026       char *gen_sig_buf = NULL;
3027       {
3028         ResourceMark rm(current_thread);
3029 
3030         const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
3031         name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3032         strcpy(name_buf, utf8_name);
3033 
3034         const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
3035         sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
3036         strcpy(sig_buf, utf8_signature);
3037 
3038         if (generic_signature_index > 0) {
3039           const char *utf8_gen_sign = (const char *)
3040                                        constants->symbol_at(generic_signature_index)->as_utf8();
3041           gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
3042           strcpy(gen_sig_buf, utf8_gen_sign);
3043         }
3044       }
3045 
3046       // fill in the jvmti local variable table
3047       jvmti_table[i].start_location = start_location;
3048       jvmti_table[i].length = length;
3049       jvmti_table[i].name = name_buf;
3050       jvmti_table[i].signature = sig_buf;
3051       jvmti_table[i].generic_signature = gen_sig_buf;
3052       jvmti_table[i].slot = slot;
3053     }
3054   }
3055 
3056   // set results
3057   (*entry_count_ptr) = num_entries;
3058   (*table_ptr) = jvmti_table;
3059 
3060   return JVMTI_ERROR_NONE;
3061 } /* end GetLocalVariableTable */
3062 
3063 
3064 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3065 // bytecode_count_ptr - pre-checked for NULL
3066 // bytecodes_ptr - pre-checked for NULL
3067 jvmtiError
3068 JvmtiEnv::GetBytecodes(Method* method_oop, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
3069   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3070 
3071   HandleMark hm;
3072   methodHandle method(method_oop);
3073   jint size = (jint)method->code_size();
3074   jvmtiError err = allocate(size, bytecodes_ptr);
3075   if (err != JVMTI_ERROR_NONE) {
3076     return err;
3077   }
3078 
3079   (*bytecode_count_ptr) = size;
3080   // get byte codes
3081   JvmtiClassFileReconstituter::copy_bytecodes(method, *bytecodes_ptr);
3082 
3083   return JVMTI_ERROR_NONE;
3084 } /* end GetBytecodes */
3085 
3086 
3087 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3088 // is_native_ptr - pre-checked for NULL
3089 jvmtiError
3090 JvmtiEnv::IsMethodNative(Method* method_oop, jboolean* is_native_ptr) {
3091   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3092   (*is_native_ptr) = method_oop->is_native();
3093   return JVMTI_ERROR_NONE;
3094 } /* end IsMethodNative */
3095 
3096 
3097 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3098 // is_synthetic_ptr - pre-checked for NULL
3099 jvmtiError
3100 JvmtiEnv::IsMethodSynthetic(Method* method_oop, jboolean* is_synthetic_ptr) {
3101   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3102   (*is_synthetic_ptr) = method_oop->is_synthetic();
3103   return JVMTI_ERROR_NONE;
3104 } /* end IsMethodSynthetic */
3105 
3106 
3107 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3108 // is_obsolete_ptr - pre-checked for NULL
3109 jvmtiError
3110 JvmtiEnv::IsMethodObsolete(Method* method_oop, jboolean* is_obsolete_ptr) {
3111   if (use_version_1_0_semantics() &&
3112       get_capabilities()->can_redefine_classes == 0) {
3113     // This JvmtiEnv requested version 1.0 semantics and this function
3114     // requires the can_redefine_classes capability in version 1.0 so
3115     // we need to return an error here.
3116     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3117   }
3118 
3119   if (method_oop == NULL || method_oop->is_obsolete()) {
3120     *is_obsolete_ptr = true;
3121   } else {
3122     *is_obsolete_ptr = false;
3123   }
3124   return JVMTI_ERROR_NONE;
3125 } /* end IsMethodObsolete */
3126 
3127   //
3128   // Raw Monitor functions
3129   //
3130 
3131 // name - pre-checked for NULL
3132 // monitor_ptr - pre-checked for NULL
3133 jvmtiError
3134 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
3135   JvmtiRawMonitor* rmonitor = new JvmtiRawMonitor(name);
3136   NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
3137 
3138   *monitor_ptr = (jrawMonitorID)rmonitor;
3139 
3140   return JVMTI_ERROR_NONE;
3141 } /* end CreateRawMonitor */
3142 
3143 
3144 // rmonitor - pre-checked for validity
3145 jvmtiError
3146 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
3147   if (Threads::number_of_threads() == 0) {
3148     // Remove this  monitor from pending raw monitors list
3149     // if it has entered in onload or start phase.
3150     JvmtiPendingMonitors::destroy(rmonitor);
3151   } else {
3152     Thread* thread  = Thread::current();
3153     if (rmonitor->is_entered(thread)) {
3154       // The caller owns this monitor which we are about to destroy.
3155       // We exit the underlying synchronization object so that the
3156       // "delete monitor" call below can work without an assertion
3157       // failure on systems that don't like destroying synchronization
3158       // objects that are locked.
3159       int r;
3160       intptr_t recursion = rmonitor->recursions();
3161       for (intptr_t i=0; i <= recursion; i++) {
3162         r = rmonitor->raw_exit(thread);
3163         assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
3164         if (r != ObjectMonitor::OM_OK) {  // robustness
3165           return JVMTI_ERROR_INTERNAL;
3166         }
3167       }
3168     }
3169     if (rmonitor->owner() != NULL) {
3170       // The caller is trying to destroy a monitor that is locked by
3171       // someone else. While this is not forbidden by the JVMTI
3172       // spec, it will cause an assertion failure on systems that don't
3173       // like destroying synchronization objects that are locked.
3174       // We indicate a problem with the error return (and leak the
3175       // monitor's memory).
3176       return JVMTI_ERROR_NOT_MONITOR_OWNER;
3177     }
3178   }
3179 
3180   delete rmonitor;
3181 
3182   return JVMTI_ERROR_NONE;
3183 } /* end DestroyRawMonitor */
3184 
3185 
3186 // rmonitor - pre-checked for validity
3187 jvmtiError
3188 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
3189   if (Threads::number_of_threads() == 0) {
3190     // No JavaThreads exist so ObjectMonitor enter cannot be
3191     // used, add this raw monitor to the pending list.
3192     // The pending monitors will be actually entered when
3193     // the VM is setup.
3194     // See transition_pending_raw_monitors in create_vm()
3195     // in thread.cpp.
3196     JvmtiPendingMonitors::enter(rmonitor);
3197   } else {
3198     int r = 0;
3199     Thread* thread = Thread::current();
3200 
3201     if (thread->is_Java_thread()) {
3202       JavaThread* current_thread = (JavaThread*)thread;
3203 
3204 #ifdef PROPER_TRANSITIONS
3205       // Not really unknown but ThreadInVMfromNative does more than we want
3206       ThreadInVMfromUnknown __tiv;
3207       {
3208         ThreadBlockInVM __tbivm(current_thread);
3209         r = rmonitor->raw_enter(current_thread);
3210       }
3211 #else
3212       /* Transition to thread_blocked without entering vm state          */
3213       /* This is really evil. Normally you can't undo _thread_blocked    */
3214       /* transitions like this because it would cause us to miss a       */
3215       /* safepoint but since the thread was already in _thread_in_native */
3216       /* the thread is not leaving a safepoint safe state and it will    */
3217       /* block when it tries to return from native. We can't safepoint   */
3218       /* block in here because we could deadlock the vmthread. Blech.    */
3219 
3220       JavaThreadState state = current_thread->thread_state();
3221       assert(state == _thread_in_native, "Must be _thread_in_native");
3222       // frame should already be walkable since we are in native
3223       assert(!current_thread->has_last_Java_frame() ||
3224              current_thread->frame_anchor()->walkable(), "Must be walkable");
3225       current_thread->set_thread_state(_thread_blocked);
3226 
3227       r = rmonitor->raw_enter(current_thread);
3228       // restore state, still at a safepoint safe state
3229       current_thread->set_thread_state(state);
3230 
3231 #endif /* PROPER_TRANSITIONS */
3232       assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked");
3233     } else {
3234       if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3235         r = rmonitor->raw_enter(thread);
3236       } else {
3237         ShouldNotReachHere();
3238       }
3239     }
3240 
3241     if (r != ObjectMonitor::OM_OK) {  // robustness
3242       return JVMTI_ERROR_INTERNAL;
3243     }
3244   }
3245   return JVMTI_ERROR_NONE;
3246 } /* end RawMonitorEnter */
3247 
3248 
3249 // rmonitor - pre-checked for validity
3250 jvmtiError
3251 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3252   jvmtiError err = JVMTI_ERROR_NONE;
3253 
3254   if (Threads::number_of_threads() == 0) {
3255     // No JavaThreads exist so just remove this monitor from the pending list.
3256     // Bool value from exit is false if rmonitor is not in the list.
3257     if (!JvmtiPendingMonitors::exit(rmonitor)) {
3258       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3259     }
3260   } else {
3261     int r = 0;
3262     Thread* thread = Thread::current();
3263 
3264     if (thread->is_Java_thread()) {
3265       JavaThread* current_thread = (JavaThread*)thread;
3266 #ifdef PROPER_TRANSITIONS
3267       // Not really unknown but ThreadInVMfromNative does more than we want
3268       ThreadInVMfromUnknown __tiv;
3269 #endif /* PROPER_TRANSITIONS */
3270       r = rmonitor->raw_exit(current_thread);
3271     } else {
3272       if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3273         r = rmonitor->raw_exit(thread);
3274       } else {
3275         ShouldNotReachHere();
3276       }
3277     }
3278 
3279     if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3280       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3281     } else {
3282       assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
3283       if (r != ObjectMonitor::OM_OK) {  // robustness
3284         err = JVMTI_ERROR_INTERNAL;
3285       }
3286     }
3287   }
3288   return err;
3289 } /* end RawMonitorExit */
3290 
3291 
3292 // rmonitor - pre-checked for validity
3293 jvmtiError
3294 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3295   int r = 0;
3296   Thread* thread = Thread::current();
3297 
3298   if (thread->is_Java_thread()) {
3299     JavaThread* current_thread = (JavaThread*)thread;
3300 #ifdef PROPER_TRANSITIONS
3301     // Not really unknown but ThreadInVMfromNative does more than we want
3302     ThreadInVMfromUnknown __tiv;
3303     {
3304       ThreadBlockInVM __tbivm(current_thread);
3305       r = rmonitor->raw_wait(millis, true, current_thread);
3306     }
3307 #else
3308     /* Transition to thread_blocked without entering vm state          */
3309     /* This is really evil. Normally you can't undo _thread_blocked    */
3310     /* transitions like this because it would cause us to miss a       */
3311     /* safepoint but since the thread was already in _thread_in_native */
3312     /* the thread is not leaving a safepoint safe state and it will    */
3313     /* block when it tries to return from native. We can't safepoint   */
3314     /* block in here because we could deadlock the vmthread. Blech.    */
3315 
3316     JavaThreadState state = current_thread->thread_state();
3317     assert(state == _thread_in_native, "Must be _thread_in_native");
3318     // frame should already be walkable since we are in native
3319     assert(!current_thread->has_last_Java_frame() ||
3320            current_thread->frame_anchor()->walkable(), "Must be walkable");
3321     current_thread->set_thread_state(_thread_blocked);
3322 
3323     r = rmonitor->raw_wait(millis, true, current_thread);
3324     // restore state, still at a safepoint safe state
3325     current_thread->set_thread_state(state);
3326 
3327 #endif /* PROPER_TRANSITIONS */
3328   } else {
3329     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3330       r = rmonitor->raw_wait(millis, true, thread);
3331     } else {
3332       ShouldNotReachHere();
3333     }
3334   }
3335 
3336   switch (r) {
3337   case ObjectMonitor::OM_INTERRUPTED:
3338     return JVMTI_ERROR_INTERRUPT;
3339   case ObjectMonitor::OM_ILLEGAL_MONITOR_STATE:
3340     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3341   }
3342   assert(r == ObjectMonitor::OM_OK, "raw_wait should have worked");
3343   if (r != ObjectMonitor::OM_OK) {  // robustness
3344     return JVMTI_ERROR_INTERNAL;
3345   }
3346 
3347   return JVMTI_ERROR_NONE;
3348 } /* end RawMonitorWait */
3349 
3350 
3351 // rmonitor - pre-checked for validity
3352 jvmtiError
3353 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3354   int r = 0;
3355   Thread* thread = Thread::current();
3356 
3357   if (thread->is_Java_thread()) {
3358     JavaThread* current_thread = (JavaThread*)thread;
3359     // Not really unknown but ThreadInVMfromNative does more than we want
3360     ThreadInVMfromUnknown __tiv;
3361     r = rmonitor->raw_notify(current_thread);
3362   } else {
3363     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3364       r = rmonitor->raw_notify(thread);
3365     } else {
3366       ShouldNotReachHere();
3367     }
3368   }
3369 
3370   if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3371     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3372   }
3373   assert(r == ObjectMonitor::OM_OK, "raw_notify should have worked");
3374   if (r != ObjectMonitor::OM_OK) {  // robustness
3375     return JVMTI_ERROR_INTERNAL;
3376   }
3377 
3378   return JVMTI_ERROR_NONE;
3379 } /* end RawMonitorNotify */
3380 
3381 
3382 // rmonitor - pre-checked for validity
3383 jvmtiError
3384 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3385   int r = 0;
3386   Thread* thread = Thread::current();
3387 
3388   if (thread->is_Java_thread()) {
3389     JavaThread* current_thread = (JavaThread*)thread;
3390     ThreadInVMfromUnknown __tiv;
3391     r = rmonitor->raw_notifyAll(current_thread);
3392   } else {
3393     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3394       r = rmonitor->raw_notifyAll(thread);
3395     } else {
3396       ShouldNotReachHere();
3397     }
3398   }
3399 
3400   if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3401     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3402   }
3403   assert(r == ObjectMonitor::OM_OK, "raw_notifyAll should have worked");
3404   if (r != ObjectMonitor::OM_OK) {  // robustness
3405     return JVMTI_ERROR_INTERNAL;
3406   }
3407 
3408   return JVMTI_ERROR_NONE;
3409 } /* end RawMonitorNotifyAll */
3410 
3411 
3412   //
3413   // JNI Function Interception functions
3414   //
3415 
3416 
3417 // function_table - pre-checked for NULL
3418 jvmtiError
3419 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3420   // Copy jni function table at safepoint.
3421   VM_JNIFunctionTableCopier copier(function_table);
3422   VMThread::execute(&copier);
3423 
3424   return JVMTI_ERROR_NONE;
3425 } /* end SetJNIFunctionTable */
3426 
3427 
3428 // function_table - pre-checked for NULL
3429 jvmtiError
3430 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3431   *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3432   if (*function_table == NULL)
3433     return JVMTI_ERROR_OUT_OF_MEMORY;
3434   memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3435   return JVMTI_ERROR_NONE;
3436 } /* end GetJNIFunctionTable */
3437 
3438 
3439   //
3440   // Event Management functions
3441   //
3442 
3443 jvmtiError
3444 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3445   // can only generate two event types
3446   if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3447       event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3448     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3449   }
3450 
3451   // for compiled_method_load events we must check that the environment
3452   // has the can_generate_compiled_method_load_events capability.
3453   if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3454     if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3455       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3456     }
3457     return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3458   } else {
3459     return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3460   }
3461 
3462 } /* end GenerateEvents */
3463 
3464 
3465   //
3466   // Extension Mechanism functions
3467   //
3468 
3469 // extension_count_ptr - pre-checked for NULL
3470 // extensions - pre-checked for NULL
3471 jvmtiError
3472 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3473   return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3474 } /* end GetExtensionFunctions */
3475 
3476 
3477 // extension_count_ptr - pre-checked for NULL
3478 // extensions - pre-checked for NULL
3479 jvmtiError
3480 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3481   return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3482 } /* end GetExtensionEvents */
3483 
3484 
3485 // callback - NULL is a valid value, must be checked
3486 jvmtiError
3487 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3488   return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3489 } /* end SetExtensionEventCallback */
3490 
3491   //
3492   // Timers functions
3493   //
3494 
3495 // info_ptr - pre-checked for NULL
3496 jvmtiError
3497 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3498   os::current_thread_cpu_time_info(info_ptr);
3499   return JVMTI_ERROR_NONE;
3500 } /* end GetCurrentThreadCpuTimerInfo */
3501 
3502 
3503 // nanos_ptr - pre-checked for NULL
3504 jvmtiError
3505 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3506   *nanos_ptr = os::current_thread_cpu_time();
3507   return JVMTI_ERROR_NONE;
3508 } /* end GetCurrentThreadCpuTime */
3509 
3510 
3511 // info_ptr - pre-checked for NULL
3512 jvmtiError
3513 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3514   os::thread_cpu_time_info(info_ptr);
3515   return JVMTI_ERROR_NONE;
3516 } /* end GetThreadCpuTimerInfo */
3517 
3518 
3519 // Threads_lock NOT held, java_thread not protected by lock
3520 // java_thread - pre-checked
3521 // nanos_ptr - pre-checked for NULL
3522 jvmtiError
3523 JvmtiEnv::GetThreadCpuTime(JavaThread* java_thread, jlong* nanos_ptr) {
3524   *nanos_ptr = os::thread_cpu_time(java_thread);
3525   return JVMTI_ERROR_NONE;
3526 } /* end GetThreadCpuTime */
3527 
3528 
3529 // info_ptr - pre-checked for NULL
3530 jvmtiError
3531 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3532   os::javaTimeNanos_info(info_ptr);
3533   return JVMTI_ERROR_NONE;
3534 } /* end GetTimerInfo */
3535 
3536 
3537 // nanos_ptr - pre-checked for NULL
3538 jvmtiError
3539 JvmtiEnv::GetTime(jlong* nanos_ptr) {
3540   *nanos_ptr = os::javaTimeNanos();
3541   return JVMTI_ERROR_NONE;
3542 } /* end GetTime */
3543 
3544 
3545 // processor_count_ptr - pre-checked for NULL
3546 jvmtiError
3547 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3548   *processor_count_ptr = os::active_processor_count();
3549   return JVMTI_ERROR_NONE;
3550 } /* end GetAvailableProcessors */
3551 
3552   //
3553   // System Properties functions
3554   //
3555 
3556 // count_ptr - pre-checked for NULL
3557 // property_ptr - pre-checked for NULL
3558 jvmtiError
3559 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3560   jvmtiError err = JVMTI_ERROR_NONE;
3561 
3562   // Get the number of readable properties.
3563   *count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties());
3564 
3565   // Allocate memory to hold the exact number of readable properties.
3566   err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3567   if (err != JVMTI_ERROR_NONE) {
3568     return err;
3569   }
3570   int readable_count = 0;
3571   // Loop through the system properties until all the readable properties are found.
3572   for (SystemProperty* p = Arguments::system_properties(); p != NULL && readable_count < *count_ptr; p = p->next()) {
3573     if (p->is_readable()) {
3574       const char *key = p->key();
3575       char **tmp_value = *property_ptr+readable_count;
3576       readable_count++;
3577       err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3578       if (err == JVMTI_ERROR_NONE) {
3579         strcpy(*tmp_value, key);
3580       } else {
3581         // clean up previously allocated memory.
3582         for (int j=0; j<readable_count; j++) {
3583           Deallocate((unsigned char*)*property_ptr+j);
3584         }
3585         Deallocate((unsigned char*)property_ptr);
3586         break;
3587       }
3588     }
3589   }
3590   assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count");
3591   return err;
3592 } /* end GetSystemProperties */
3593 
3594 
3595 // property - pre-checked for NULL
3596 // value_ptr - pre-checked for NULL
3597 jvmtiError
3598 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3599   jvmtiError err = JVMTI_ERROR_NONE;
3600   const char *value;
3601 
3602   // Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist.
3603   value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property);
3604   if (value == NULL) {
3605     err =  JVMTI_ERROR_NOT_AVAILABLE;
3606   } else {
3607     err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3608     if (err == JVMTI_ERROR_NONE) {
3609       strcpy(*value_ptr, value);
3610     }
3611   }
3612   return err;
3613 } /* end GetSystemProperty */
3614 
3615 
3616 // property - pre-checked for NULL
3617 // value - NULL is a valid value, must be checked
3618 jvmtiError
3619 JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) {
3620   jvmtiError err =JVMTI_ERROR_NOT_AVAILABLE;
3621 
3622   for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
3623     if (strcmp(property, p->key()) == 0) {
3624       if (p->set_writeable_value(value_ptr)) {
3625         err =  JVMTI_ERROR_NONE;
3626       }
3627     }
3628   }
3629   return err;
3630 } /* end SetSystemProperty */
--- EOF ---