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