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