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