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