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