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