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