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