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