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