rev 47862 : imported patch 10.07.open.rebase_20171110.dcubed

   1 /*
   2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderExt.hpp"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/stringTable.hpp"
  29 #include "classfile/modules.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "interpreter/bytecodeStream.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvmtifiles/jvmtiEnv.hpp"
  35 #include "logging/log.hpp"
  36 #include "logging/logConfiguration.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "memory/universe.inline.hpp"
  39 #include "oops/instanceKlass.hpp"
  40 #include "oops/objArrayOop.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "prims/jniCheck.hpp"
  43 #include "prims/jvm_misc.hpp"
  44 #include "prims/jvmtiAgentThread.hpp"
  45 #include "prims/jvmtiClassFileReconstituter.hpp"
  46 #include "prims/jvmtiCodeBlobEvents.hpp"
  47 #include "prims/jvmtiExtensions.hpp"
  48 #include "prims/jvmtiGetLoadedClasses.hpp"
  49 #include "prims/jvmtiImpl.hpp"
  50 #include "prims/jvmtiManageCapabilities.hpp"
  51 #include "prims/jvmtiRawMonitor.hpp"
  52 #include "prims/jvmtiRedefineClasses.hpp"
  53 #include "prims/jvmtiTagMap.hpp"
  54 #include "prims/jvmtiThreadState.inline.hpp"
  55 #include "prims/jvmtiUtil.hpp"
  56 #include "runtime/arguments.hpp"
  57 #include "runtime/deoptimization.hpp"
  58 #include "runtime/interfaceSupport.hpp"
  59 #include "runtime/javaCalls.hpp"
  60 #include "runtime/jfieldIDWorkaround.hpp"
  61 #include "runtime/osThread.hpp"
  62 #include "runtime/reflectionUtils.hpp"
  63 #include "runtime/signature.hpp"
  64 #include "runtime/thread.inline.hpp"
  65 #include "runtime/threadSMR.hpp"
  66 #include "runtime/timerTrace.hpp"
  67 #include "runtime/vframe.hpp"
  68 #include "runtime/vmThread.hpp"
  69 #include "services/threadService.hpp"
  70 #include "utilities/exceptions.hpp"
  71 #include "utilities/preserveException.hpp"
  72 
  73 
  74 #define FIXLATER 0 // REMOVE this when completed.
  75 
  76  // FIXLATER: hook into JvmtiTrace
  77 #define TraceJVMTICalls false
  78 
  79 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
  80 }
  81 
  82 JvmtiEnv::~JvmtiEnv() {
  83 }
  84 
  85 JvmtiEnv*
  86 JvmtiEnv::create_a_jvmti(jint version) {
  87   return new JvmtiEnv(version);
  88 }
  89 
  90 // VM operation class to copy jni function table at safepoint.
  91 // More than one java threads or jvmti agents may be reading/
  92 // modifying jni function tables. To reduce the risk of bad
  93 // interaction b/w these threads it is copied at safepoint.
  94 class VM_JNIFunctionTableCopier : public VM_Operation {
  95  private:
  96   const struct JNINativeInterface_ *_function_table;
  97  public:
  98   VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
  99     _function_table = func_tbl;
 100   };
 101 
 102   VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
 103   void doit() {
 104     copy_jni_function_table(_function_table);
 105   };
 106 };
 107 
 108 //
 109 // Do not change the "prefix" marker below, everything above it is copied
 110 // unchanged into the filled stub, everything below is controlled by the
 111 // stub filler (only method bodies are carried forward, and then only for
 112 // functionality still in the spec).
 113 //
 114 // end file prefix
 115 
 116   //
 117   // Memory Management functions
 118   //
 119 
 120 // mem_ptr - pre-checked for NULL
 121 jvmtiError
 122 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
 123   return allocate(size, mem_ptr);
 124 } /* end Allocate */
 125 
 126 
 127 // mem - NULL is a valid value, must be checked
 128 jvmtiError
 129 JvmtiEnv::Deallocate(unsigned char* mem) {
 130   return deallocate(mem);
 131 } /* end Deallocate */
 132 
 133 // Threads_lock NOT held, java_thread not protected by lock
 134 // java_thread - pre-checked
 135 // data - NULL is a valid value, must be checked
 136 jvmtiError
 137 JvmtiEnv::SetThreadLocalStorage(JavaThread* java_thread, const void* data) {
 138   JvmtiThreadState* state = java_thread->jvmti_thread_state();
 139   if (state == NULL) {
 140     if (data == NULL) {
 141       // leaving state unset same as data set to NULL
 142       return JVMTI_ERROR_NONE;
 143     }
 144     // otherwise, create the state
 145     state = JvmtiThreadState::state_for(java_thread);
 146     if (state == NULL) {
 147       return JVMTI_ERROR_THREAD_NOT_ALIVE;
 148     }
 149   }
 150   state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
 151   return JVMTI_ERROR_NONE;
 152 } /* end SetThreadLocalStorage */
 153 
 154 
 155 // Threads_lock NOT held
 156 // thread - NOT pre-checked
 157 // data_ptr - pre-checked for NULL
 158 jvmtiError
 159 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
 160   JavaThread* current_thread = JavaThread::current();
 161   if (thread == NULL) {
 162     JvmtiThreadState* state = current_thread->jvmti_thread_state();
 163     *data_ptr = (state == NULL) ? NULL :
 164       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 165   } else {

 166     // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
 167     // the thread to _thread_in_vm. However, when the TLS for a thread
 168     // other than the current thread is required we need to transition
 169     // from native so as to resolve the jthread.
 170 
 171     ThreadInVMfromNative __tiv(current_thread);
 172     VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
 173     debug_only(VMNativeEntryWrapper __vew;)
 174 
 175     JavaThread* java_thread = NULL;
 176     ThreadsListHandle tlh(current_thread);
 177     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);
 178     if (err != JVMTI_ERROR_NONE) {
 179       return err;
 180     }
 181 






 182     JvmtiThreadState* state = java_thread->jvmti_thread_state();
 183     *data_ptr = (state == NULL) ? NULL :
 184       state->env_thread_state(this)->get_agent_thread_local_storage_data();
 185   }
 186   return JVMTI_ERROR_NONE;
 187 } /* end GetThreadLocalStorage */
 188 
 189   //
 190   // Module functions
 191   //
 192 
 193 // module_count_ptr - pre-checked for NULL
 194 // modules_ptr - pre-checked for NULL
 195 jvmtiError
 196 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
 197     JvmtiModuleClosure jmc;
 198 
 199     return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
 200 } /* end GetAllModules */
 201 
 202 
 203 // class_loader - NULL is a valid value, must be pre-checked
 204 // package_name - pre-checked for NULL
 205 // module_ptr - pre-checked for NULL
 206 jvmtiError
 207 JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
 208   JavaThread* THREAD = JavaThread::current(); // pass to macros
 209   ResourceMark rm(THREAD);
 210 
 211   Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
 212   // Check that loader is a subclass of java.lang.ClassLoader.
 213   if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
 214     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 215   }
 216   jobject module = Modules::get_named_module(h_loader, package_name, THREAD);
 217   if (HAS_PENDING_EXCEPTION) {
 218     CLEAR_PENDING_EXCEPTION;
 219     return JVMTI_ERROR_INTERNAL; // unexpected exception
 220   }
 221   *module_ptr = module;
 222   return JVMTI_ERROR_NONE;
 223 } /* end GetNamedModule */
 224 
 225 
 226 // module - pre-checked for NULL
 227 // to_module - pre-checked for NULL
 228 jvmtiError
 229 JvmtiEnv::AddModuleReads(jobject module, jobject to_module) {
 230   JavaThread* THREAD = JavaThread::current();
 231 
 232   // check module
 233   Handle h_module(THREAD, JNIHandles::resolve(module));
 234   if (!java_lang_Module::is_instance(h_module())) {
 235     return JVMTI_ERROR_INVALID_MODULE;
 236   }
 237   // check to_module
 238   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 239   if (!java_lang_Module::is_instance(h_to_module())) {
 240     return JVMTI_ERROR_INVALID_MODULE;
 241   }
 242   return JvmtiExport::add_module_reads(h_module, h_to_module, THREAD);
 243 } /* end AddModuleReads */
 244 
 245 
 246 // module - pre-checked for NULL
 247 // pkg_name - pre-checked for NULL
 248 // to_module - pre-checked for NULL
 249 jvmtiError
 250 JvmtiEnv::AddModuleExports(jobject module, const char* pkg_name, jobject to_module) {
 251   JavaThread* THREAD = JavaThread::current();
 252   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 253 
 254   // check module
 255   Handle h_module(THREAD, JNIHandles::resolve(module));
 256   if (!java_lang_Module::is_instance(h_module())) {
 257     return JVMTI_ERROR_INVALID_MODULE;
 258   }
 259   // check to_module
 260   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 261   if (!java_lang_Module::is_instance(h_to_module())) {
 262     return JVMTI_ERROR_INVALID_MODULE;
 263   }
 264   return JvmtiExport::add_module_exports(h_module, h_pkg, h_to_module, THREAD);
 265 } /* end AddModuleExports */
 266 
 267 
 268 // module - pre-checked for NULL
 269 // pkg_name - pre-checked for NULL
 270 // to_module - pre-checked for NULL
 271 jvmtiError
 272 JvmtiEnv::AddModuleOpens(jobject module, const char* pkg_name, jobject to_module) {
 273   JavaThread* THREAD = JavaThread::current();
 274   Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
 275 
 276   // check module
 277   Handle h_module(THREAD, JNIHandles::resolve(module));
 278   if (!java_lang_Module::is_instance(h_module())) {
 279     return JVMTI_ERROR_INVALID_MODULE;
 280   }
 281   // check to_module
 282   Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
 283   if (!java_lang_Module::is_instance(h_to_module())) {
 284     return JVMTI_ERROR_INVALID_MODULE;
 285   }
 286   return JvmtiExport::add_module_opens(h_module, h_pkg, h_to_module, THREAD);
 287 } /* end AddModuleOpens */
 288 
 289 
 290 // module - pre-checked for NULL
 291 // service - pre-checked for NULL
 292 jvmtiError
 293 JvmtiEnv::AddModuleUses(jobject module, jclass service) {
 294   JavaThread* THREAD = JavaThread::current();
 295 
 296   // check module
 297   Handle h_module(THREAD, JNIHandles::resolve(module));
 298   if (!java_lang_Module::is_instance(h_module())) {
 299     return JVMTI_ERROR_INVALID_MODULE;
 300   }
 301   // check service
 302   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 303   if (!java_lang_Class::is_instance(h_service()) ||
 304       java_lang_Class::is_primitive(h_service())) {
 305     return JVMTI_ERROR_INVALID_CLASS;
 306   }
 307   return JvmtiExport::add_module_uses(h_module, h_service, THREAD);
 308 } /* end AddModuleUses */
 309 
 310 
 311 // module - pre-checked for NULL
 312 // service - pre-checked for NULL
 313 // impl_class - pre-checked for NULL
 314 jvmtiError
 315 JvmtiEnv::AddModuleProvides(jobject module, jclass service, jclass impl_class) {
 316   JavaThread* THREAD = JavaThread::current();
 317 
 318   // check module
 319   Handle h_module(THREAD, JNIHandles::resolve(module));
 320   if (!java_lang_Module::is_instance(h_module())) {
 321     return JVMTI_ERROR_INVALID_MODULE;
 322   }
 323   // check service
 324   Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
 325   if (!java_lang_Class::is_instance(h_service()) ||
 326       java_lang_Class::is_primitive(h_service())) {
 327     return JVMTI_ERROR_INVALID_CLASS;
 328   }
 329   // check impl_class
 330   Handle h_impl_class(THREAD, JNIHandles::resolve_external_guard(impl_class));
 331   if (!java_lang_Class::is_instance(h_impl_class()) ||
 332       java_lang_Class::is_primitive(h_impl_class())) {
 333     return JVMTI_ERROR_INVALID_CLASS;
 334   }
 335   return JvmtiExport::add_module_provides(h_module, h_service, h_impl_class, THREAD);
 336 } /* end AddModuleProvides */
 337 
 338 // module - pre-checked for NULL
 339 // is_modifiable_class_ptr - pre-checked for NULL
 340 jvmtiError
 341 JvmtiEnv::IsModifiableModule(jobject module, jboolean* is_modifiable_module_ptr) {
 342   JavaThread* THREAD = JavaThread::current();
 343 
 344   // check module
 345   Handle h_module(THREAD, JNIHandles::resolve(module));
 346   if (!java_lang_Module::is_instance(h_module())) {
 347     return JVMTI_ERROR_INVALID_MODULE;
 348   }
 349 
 350   *is_modifiable_module_ptr = JNI_TRUE;
 351   return JVMTI_ERROR_NONE;
 352 } /* end IsModifiableModule */
 353 
 354 
 355   //
 356   // Class functions
 357   //
 358 
 359 // class_count_ptr - pre-checked for NULL
 360 // classes_ptr - pre-checked for NULL
 361 jvmtiError
 362 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
 363   return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
 364 } /* end GetLoadedClasses */
 365 
 366 
 367 // initiating_loader - NULL is a valid value, must be checked
 368 // class_count_ptr - pre-checked for NULL
 369 // classes_ptr - pre-checked for NULL
 370 jvmtiError
 371 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
 372   return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
 373                                                   class_count_ptr, classes_ptr);
 374 } /* end GetClassLoaderClasses */
 375 
 376 // k_mirror - may be primitive, this must be checked
 377 // is_modifiable_class_ptr - pre-checked for NULL
 378 jvmtiError
 379 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
 380   *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
 381                                                        JNI_TRUE : JNI_FALSE;
 382   return JVMTI_ERROR_NONE;
 383 } /* end IsModifiableClass */
 384 
 385 // class_count - pre-checked to be greater than or equal to 0
 386 // classes - pre-checked for NULL
 387 jvmtiError
 388 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
 389 //TODO: add locking
 390 
 391   int index;
 392   JavaThread* current_thread = JavaThread::current();
 393   ResourceMark rm(current_thread);
 394 
 395   jvmtiClassDefinition* class_definitions =
 396                             NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
 397   NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
 398 
 399   for (index = 0; index < class_count; index++) {
 400     HandleMark hm(current_thread);
 401 
 402     jclass jcls = classes[index];
 403     oop k_mirror = JNIHandles::resolve_external_guard(jcls);
 404     if (k_mirror == NULL) {
 405       return JVMTI_ERROR_INVALID_CLASS;
 406     }
 407     if (!k_mirror->is_a(SystemDictionary::Class_klass())) {
 408       return JVMTI_ERROR_INVALID_CLASS;
 409     }
 410 
 411     if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
 412       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
 413     }
 414 
 415     Klass* klass = java_lang_Class::as_Klass(k_mirror);
 416 
 417     jint status = klass->jvmti_class_status();
 418     if (status & (JVMTI_CLASS_STATUS_ERROR)) {
 419       return JVMTI_ERROR_INVALID_CLASS;
 420     }
 421 
 422     InstanceKlass* ik = InstanceKlass::cast(klass);
 423     if (ik->get_cached_class_file_bytes() == NULL) {
 424       // Not cached, we need to reconstitute the class file from the
 425       // VM representation. We don't attach the reconstituted class
 426       // bytes to the InstanceKlass here because they have not been
 427       // validated and we're not at a safepoint.
 428       JvmtiClassFileReconstituter reconstituter(ik);
 429       if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
 430         return reconstituter.get_error();
 431       }
 432 
 433       class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
 434       class_definitions[index].class_bytes      = (unsigned char*)
 435                                                        reconstituter.class_file_bytes();
 436     } else {
 437       // it is cached, get it from the cache
 438       class_definitions[index].class_byte_count = ik->get_cached_class_file_len();
 439       class_definitions[index].class_bytes      = ik->get_cached_class_file_bytes();
 440     }
 441     class_definitions[index].klass              = jcls;
 442   }
 443   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
 444   VMThread::execute(&op);
 445   return (op.check_error());
 446 } /* end RetransformClasses */
 447 
 448 
 449 // class_count - pre-checked to be greater than or equal to 0
 450 // class_definitions - pre-checked for NULL
 451 jvmtiError
 452 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
 453 //TODO: add locking
 454   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
 455   VMThread::execute(&op);
 456   return (op.check_error());
 457 } /* end RedefineClasses */
 458 
 459 
 460   //
 461   // Object functions
 462   //
 463 
 464 // size_ptr - pre-checked for NULL
 465 jvmtiError
 466 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
 467   oop mirror = JNIHandles::resolve_external_guard(object);
 468   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
 469   *size_ptr = (jlong)mirror->size() * wordSize;
 470   return JVMTI_ERROR_NONE;
 471 } /* end GetObjectSize */
 472 
 473   //
 474   // Method functions
 475   //
 476 
 477 // prefix - NULL is a valid value, must be checked
 478 jvmtiError
 479 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
 480   return prefix == NULL?
 481               SetNativeMethodPrefixes(0, NULL) :
 482               SetNativeMethodPrefixes(1, (char**)&prefix);
 483 } /* end SetNativeMethodPrefix */
 484 
 485 
 486 // prefix_count - pre-checked to be greater than or equal to 0
 487 // prefixes - pre-checked for NULL
 488 jvmtiError
 489 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
 490   // Have to grab JVMTI thread state lock to be sure that some thread
 491   // isn't accessing the prefixes at the same time we are setting them.
 492   // No locks during VM bring-up.
 493   if (Threads::number_of_threads() == 0) {
 494     return set_native_method_prefixes(prefix_count, prefixes);
 495   } else {
 496     MutexLocker mu(JvmtiThreadState_lock);
 497     return set_native_method_prefixes(prefix_count, prefixes);
 498   }
 499 } /* end SetNativeMethodPrefixes */
 500 
 501   //
 502   // Event Management functions
 503   //
 504 
 505 // callbacks - NULL is a valid value, must be checked
 506 // size_of_callbacks - pre-checked to be greater than or equal to 0
 507 jvmtiError
 508 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
 509   JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
 510   return JVMTI_ERROR_NONE;
 511 } /* end SetEventCallbacks */
 512 
 513 
 514 // event_thread - NULL is a valid value, must be checked
 515 jvmtiError
 516 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread,   ...) {
 517   if (event_thread == NULL) {
 518     // Can be called at Agent_OnLoad() time with event_thread == NULL
 519     // when Thread::current() does not work yet so we cannot create a
 520     // ThreadsListHandle that is common to both thread-specific and
 521     // global code paths.
 522 
 523     // event_type must be valid
 524     if (!JvmtiEventController::is_valid_event_type(event_type)) {
 525       return JVMTI_ERROR_INVALID_EVENT_TYPE;
 526     }
 527 
 528     bool enabled = (mode == JVMTI_ENABLE);
 529 
 530     // assure that needed capabilities are present
 531     if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 532       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 533     }
 534 
 535     if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 536       record_class_file_load_hook_enabled();
 537     }
 538     JvmtiEventController::set_user_enabled(this, (JavaThread*) NULL, event_type, enabled);
 539   } else {
 540     // We have a specified event_thread.
 541 
 542     JavaThread* java_thread = NULL;
 543     ThreadsListHandle tlh;
 544     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), event_thread, &java_thread, NULL);
 545     if (err != JVMTI_ERROR_NONE) {
 546       return err;
 547     }
 548 
 549     // event_type must be valid
 550     if (!JvmtiEventController::is_valid_event_type(event_type)) {
 551       return JVMTI_ERROR_INVALID_EVENT_TYPE;
 552     }
 553 
 554     // global events cannot be controlled at thread level.
 555     if (JvmtiEventController::is_global_event(event_type)) {
 556       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 557     }
 558 
 559     bool enabled = (mode == JVMTI_ENABLE);
 560 
 561     // assure that needed capabilities are present
 562     if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
 563       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 564     }
 565 
 566     if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
 567       record_class_file_load_hook_enabled();
 568     }
 569     JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);
 570   }
 571 
 572   return JVMTI_ERROR_NONE;
 573 } /* end SetEventNotificationMode */
 574 
 575   //
 576   // Capability functions
 577   //
 578 
 579 // capabilities_ptr - pre-checked for NULL
 580 jvmtiError
 581 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
 582   JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
 583                                                       get_prohibited_capabilities(),
 584                                                       capabilities_ptr);
 585   return JVMTI_ERROR_NONE;
 586 } /* end GetPotentialCapabilities */
 587 
 588 
 589 // capabilities_ptr - pre-checked for NULL
 590 jvmtiError
 591 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 592   return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
 593                                                    get_prohibited_capabilities(),
 594                                                    capabilities_ptr,
 595                                                    get_capabilities());
 596 } /* end AddCapabilities */
 597 
 598 
 599 // capabilities_ptr - pre-checked for NULL
 600 jvmtiError
 601 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
 602   JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
 603   return JVMTI_ERROR_NONE;
 604 } /* end RelinquishCapabilities */
 605 
 606 
 607 // capabilities_ptr - pre-checked for NULL
 608 jvmtiError
 609 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
 610   JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
 611   return JVMTI_ERROR_NONE;
 612 } /* end GetCapabilities */
 613 
 614   //
 615   // Class Loader Search functions
 616   //
 617 
 618 // segment - pre-checked for NULL
 619 jvmtiError
 620 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
 621   jvmtiPhase phase = get_phase();
 622   if (phase == JVMTI_PHASE_ONLOAD) {
 623     Arguments::append_sysclasspath(segment);
 624     return JVMTI_ERROR_NONE;
 625   } else if (use_version_1_0_semantics()) {
 626     // This JvmtiEnv requested version 1.0 semantics and this function
 627     // is only allowed in the ONLOAD phase in version 1.0 so we need to
 628     // return an error here.
 629     return JVMTI_ERROR_WRONG_PHASE;
 630   } else if (phase == JVMTI_PHASE_LIVE) {
 631     // The phase is checked by the wrapper that called this function,
 632     // but this thread could be racing with the thread that is
 633     // terminating the VM so we check one more time.
 634 
 635     // create the zip entry
 636     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, true);
 637     if (zip_entry == NULL) {
 638       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 639     }
 640 
 641     // lock the loader
 642     Thread* thread = Thread::current();
 643     HandleMark hm;
 644     Handle loader_lock = Handle(thread, SystemDictionary::system_loader_lock());
 645 
 646     ObjectLocker ol(loader_lock, thread);
 647 
 648     // add the jar file to the bootclasspath
 649     log_info(class, load)("opened: %s", zip_entry->name());
 650     ClassLoaderExt::append_boot_classpath(zip_entry);
 651     return JVMTI_ERROR_NONE;
 652   } else {
 653     return JVMTI_ERROR_WRONG_PHASE;
 654   }
 655 
 656 } /* end AddToBootstrapClassLoaderSearch */
 657 
 658 
 659 // segment - pre-checked for NULL
 660 jvmtiError
 661 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
 662   jvmtiPhase phase = get_phase();
 663 
 664   if (phase == JVMTI_PHASE_ONLOAD) {
 665     for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
 666       if (strcmp("java.class.path", p->key()) == 0) {
 667         p->append_value(segment);
 668         break;
 669       }
 670     }
 671     return JVMTI_ERROR_NONE;
 672   } else if (phase == JVMTI_PHASE_LIVE) {
 673     // The phase is checked by the wrapper that called this function,
 674     // but this thread could be racing with the thread that is
 675     // terminating the VM so we check one more time.
 676     HandleMark hm;
 677 
 678     // create the zip entry (which will open the zip file and hence
 679     // check that the segment is indeed a zip file).
 680     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false);
 681     if (zip_entry == NULL) {
 682       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 683     }
 684     delete zip_entry;   // no longer needed
 685 
 686     // lock the loader
 687     Thread* THREAD = Thread::current();
 688     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 689 
 690     ObjectLocker ol(loader, THREAD);
 691 
 692     // need the path as java.lang.String
 693     Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
 694     if (HAS_PENDING_EXCEPTION) {
 695       CLEAR_PENDING_EXCEPTION;
 696       return JVMTI_ERROR_INTERNAL;
 697     }
 698 
 699     // Invoke the appendToClassPathForInstrumentation method - if the method
 700     // is not found it means the loader doesn't support adding to the class path
 701     // in the live phase.
 702     {
 703       JavaValue res(T_VOID);
 704       JavaCalls::call_special(&res,
 705                               loader,
 706                               loader->klass(),
 707                               vmSymbols::appendToClassPathForInstrumentation_name(),
 708                               vmSymbols::appendToClassPathForInstrumentation_signature(),
 709                               path,
 710                               THREAD);
 711       if (HAS_PENDING_EXCEPTION) {
 712         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 713         CLEAR_PENDING_EXCEPTION;
 714 
 715         if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
 716           return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
 717         } else {
 718           return JVMTI_ERROR_INTERNAL;
 719         }
 720       }
 721     }
 722 
 723     return JVMTI_ERROR_NONE;
 724   } else {
 725     return JVMTI_ERROR_WRONG_PHASE;
 726   }
 727 } /* end AddToSystemClassLoaderSearch */
 728 
 729   //
 730   // General functions
 731   //
 732 
 733 // phase_ptr - pre-checked for NULL
 734 jvmtiError
 735 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
 736   *phase_ptr = phase();
 737   return JVMTI_ERROR_NONE;
 738 } /* end GetPhase */
 739 
 740 
 741 jvmtiError
 742 JvmtiEnv::DisposeEnvironment() {
 743   dispose();
 744   return JVMTI_ERROR_NONE;
 745 } /* end DisposeEnvironment */
 746 
 747 
 748 // data - NULL is a valid value, must be checked
 749 jvmtiError
 750 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
 751   set_env_local_storage(data);
 752   return JVMTI_ERROR_NONE;
 753 } /* end SetEnvironmentLocalStorage */
 754 
 755 
 756 // data_ptr - pre-checked for NULL
 757 jvmtiError
 758 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
 759   *data_ptr = (void*)get_env_local_storage();
 760   return JVMTI_ERROR_NONE;
 761 } /* end GetEnvironmentLocalStorage */
 762 
 763 // version_ptr - pre-checked for NULL
 764 jvmtiError
 765 JvmtiEnv::GetVersionNumber(jint* version_ptr) {
 766   *version_ptr = JVMTI_VERSION;
 767   return JVMTI_ERROR_NONE;
 768 } /* end GetVersionNumber */
 769 
 770 
 771 // name_ptr - pre-checked for NULL
 772 jvmtiError
 773 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
 774   if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
 775     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 776   }
 777   const char *name = JvmtiUtil::error_name(error);
 778   if (name == NULL) {
 779     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 780   }
 781   size_t len = strlen(name) + 1;
 782   jvmtiError err = allocate(len, (unsigned char**)name_ptr);
 783   if (err == JVMTI_ERROR_NONE) {
 784     memcpy(*name_ptr, name, len);
 785   }
 786   return err;
 787 } /* end GetErrorName */
 788 
 789 
 790 jvmtiError
 791 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
 792   LogLevelType level = value == 0 ? LogLevel::Off : LogLevel::Info;
 793   switch (flag) {
 794   case JVMTI_VERBOSE_OTHER:
 795     // ignore
 796     break;
 797   case JVMTI_VERBOSE_CLASS:
 798     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));
 799     LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));
 800     break;
 801   case JVMTI_VERBOSE_GC:
 802     if (value == 0) {
 803       LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(gc));
 804     } else {
 805       LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(gc));
 806     }
 807     break;
 808   case JVMTI_VERBOSE_JNI:
 809     PrintJNIResolving = value != 0;
 810     break;
 811   default:
 812     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 813   };
 814   return JVMTI_ERROR_NONE;
 815 } /* end SetVerboseFlag */
 816 
 817 
 818 // format_ptr - pre-checked for NULL
 819 jvmtiError
 820 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
 821   *format_ptr = JVMTI_JLOCATION_JVMBCI;
 822   return JVMTI_ERROR_NONE;
 823 } /* end GetJLocationFormat */
 824 
 825   //
 826   // Thread functions
 827   //
 828 
 829 // Threads_lock NOT held
 830 // thread - NOT pre-checked
 831 // thread_state_ptr - pre-checked for NULL
 832 jvmtiError
 833 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
 834   JavaThread* current_thread = JavaThread::current();
 835   JavaThread* java_thread = NULL;
 836   oop thread_oop = NULL;
 837   ThreadsListHandle tlh(current_thread);
 838 
 839   if (thread == NULL) {
 840     java_thread = current_thread;
 841     thread_oop = java_thread->threadObj();


 842 
 843     if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
 844       return JVMTI_ERROR_INVALID_THREAD;
 845     }
 846   } else {
 847     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
 848     if (err != JVMTI_ERROR_NONE) {
 849       // We got an error code so we don't have a JavaThread *, but
 850       // only return an error from here if we didn't get a valid
 851       // thread_oop.
 852       if (thread_oop == NULL) {
 853         return err;
 854       }
 855       // We have a valid thread_oop so we can return some thread state.
 856     }
 857   }
 858 
 859   // get most state bits
 860   jint state = (jint)java_lang_Thread::get_thread_status(thread_oop);
 861 
 862   if (java_thread != NULL) {
 863     // We have a JavaThread* so add more state bits.
 864     JavaThreadState jts = java_thread->thread_state();

 865 
 866     if (java_thread->is_being_ext_suspended()) {
 867       state |= JVMTI_THREAD_STATE_SUSPENDED;
 868     }
 869     if (jts == _thread_in_native) {
 870       state |= JVMTI_THREAD_STATE_IN_NATIVE;
 871     }
 872     OSThread* osThread = java_thread->osthread();
 873     if (osThread != NULL && osThread->interrupted()) {
 874       state |= JVMTI_THREAD_STATE_INTERRUPTED;
 875     }
 876   }
 877 
 878   *thread_state_ptr = state;
 879   return JVMTI_ERROR_NONE;
 880 } /* end GetThreadState */
 881 
 882 
 883 // thread_ptr - pre-checked for NULL
 884 jvmtiError
 885 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
 886   JavaThread* current_thread  = JavaThread::current();
 887   *thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());
 888   return JVMTI_ERROR_NONE;
 889 } /* end GetCurrentThread */
 890 
 891 
 892 // threads_count_ptr - pre-checked for NULL
 893 // threads_ptr - pre-checked for NULL
 894 jvmtiError
 895 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
 896   int nthreads        = 0;
 897   Handle *thread_objs = NULL;
 898   ResourceMark rm;
 899   HandleMark hm;
 900 
 901   // enumerate threads (including agent threads)
 902   ThreadsListEnumerator tle(Thread::current(), true);
 903   nthreads = tle.num_threads();
 904   *threads_count_ptr = nthreads;
 905 
 906   if (nthreads == 0) {
 907     *threads_ptr = NULL;
 908     return JVMTI_ERROR_NONE;
 909   }
 910 
 911   thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
 912   NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
 913 
 914   for (int i=0; i < nthreads; i++) {
 915     thread_objs[i] = Handle(tle.get_threadObj(i));
 916   }
 917 

 918   jthread *jthreads  = new_jthreadArray(nthreads, thread_objs);
 919   NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
 920 
 921   *threads_ptr = jthreads;
 922   return JVMTI_ERROR_NONE;
 923 } /* end GetAllThreads */
 924 
 925 
 926 // Threads_lock NOT held, java_thread not protected by lock
 927 // java_thread - pre-checked
 928 jvmtiError
 929 JvmtiEnv::SuspendThread(JavaThread* java_thread) {
 930   // don't allow hidden thread suspend request.
 931   if (java_thread->is_hidden_from_external_view()) {
 932     return (JVMTI_ERROR_NONE);
 933   }
 934 
 935   {
 936     MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
 937     if (java_thread->is_external_suspend()) {
 938       // don't allow nested external suspend requests.
 939       return (JVMTI_ERROR_THREAD_SUSPENDED);
 940     }
 941     if (java_thread->is_exiting()) { // thread is in the process of exiting
 942       return (JVMTI_ERROR_THREAD_NOT_ALIVE);
 943     }
 944     java_thread->set_external_suspend();
 945   }
 946 
 947   if (!JvmtiSuspendControl::suspend(java_thread)) {
 948     // the thread was in the process of exiting
 949     return (JVMTI_ERROR_THREAD_NOT_ALIVE);
 950   }
 951   return JVMTI_ERROR_NONE;
 952 } /* end SuspendThread */
 953 
 954 
 955 // request_count - pre-checked to be greater than or equal to 0
 956 // request_list - pre-checked for NULL
 957 // results - pre-checked for NULL
 958 jvmtiError
 959 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
 960   int needSafepoint = 0;  // > 0 if we need a safepoint
 961   ThreadsListHandle tlh;
 962   for (int i = 0; i < request_count; i++) {
 963     JavaThread *java_thread = NULL;
 964     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), request_list[i], &java_thread, NULL);
 965     if (err != JVMTI_ERROR_NONE) {
 966       results[i] = err;
 967       continue;
 968     }









 969     // don't allow hidden thread suspend request.
 970     if (java_thread->is_hidden_from_external_view()) {
 971       results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
 972       continue;
 973     }
 974 
 975     {
 976       MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
 977       if (java_thread->is_external_suspend()) {
 978         // don't allow nested external suspend requests.
 979         results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
 980         continue;
 981       }
 982       if (java_thread->is_exiting()) { // thread is in the process of exiting
 983         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 984         continue;
 985       }
 986       java_thread->set_external_suspend();
 987     }
 988     if (java_thread->thread_state() == _thread_in_native) {
 989       // We need to try and suspend native threads here. Threads in
 990       // other states will self-suspend on their next transition.
 991       if (!JvmtiSuspendControl::suspend(java_thread)) {
 992         // The thread was in the process of exiting. Force another
 993         // safepoint to make sure that this thread transitions.
 994         needSafepoint++;
 995         results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
 996         continue;
 997       }
 998     } else {
 999       needSafepoint++;
1000     }
1001     results[i] = JVMTI_ERROR_NONE;  // indicate successful suspend
1002   }
1003   if (needSafepoint > 0) {
1004     VM_ThreadsSuspendJVMTI tsj;
1005     VMThread::execute(&tsj);
1006   }
1007   // per-thread suspend results returned via results parameter
1008   return JVMTI_ERROR_NONE;
1009 } /* end SuspendThreadList */
1010 
1011 
1012 // Threads_lock NOT held, java_thread not protected by lock
1013 // java_thread - pre-checked
1014 jvmtiError
1015 JvmtiEnv::ResumeThread(JavaThread* java_thread) {
1016   // don't allow hidden thread resume request.
1017   if (java_thread->is_hidden_from_external_view()) {
1018     return JVMTI_ERROR_NONE;
1019   }
1020 
1021   if (!java_thread->is_being_ext_suspended()) {
1022     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1023   }
1024 
1025   if (!JvmtiSuspendControl::resume(java_thread)) {
1026     return JVMTI_ERROR_INTERNAL;
1027   }
1028   return JVMTI_ERROR_NONE;
1029 } /* end ResumeThread */
1030 
1031 
1032 // request_count - pre-checked to be greater than or equal to 0
1033 // request_list - pre-checked for NULL
1034 // results - pre-checked for NULL
1035 jvmtiError
1036 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1037   ThreadsListHandle tlh;
1038   for (int i = 0; i < request_count; i++) {
1039     JavaThread* java_thread = NULL;
1040     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), request_list[i], &java_thread, NULL);
1041     if (err != JVMTI_ERROR_NONE) {
1042       results[i] = err;
1043       continue;
1044     }
1045     // don't allow hidden thread resume request.
1046     if (java_thread->is_hidden_from_external_view()) {
1047       results[i] = JVMTI_ERROR_NONE;  // indicate successful resume
1048       continue;
1049     }
1050     if (!java_thread->is_being_ext_suspended()) {
1051       results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1052       continue;
1053     }
1054 
1055     if (!JvmtiSuspendControl::resume(java_thread)) {
1056       results[i] = JVMTI_ERROR_INTERNAL;
1057       continue;
1058     }
1059 
1060     results[i] = JVMTI_ERROR_NONE;  // indicate successful resume
1061   }
1062   // per-thread resume results returned via results parameter
1063   return JVMTI_ERROR_NONE;
1064 } /* end ResumeThreadList */
1065 
1066 
1067 // Threads_lock NOT held, java_thread not protected by lock
1068 // java_thread - pre-checked
1069 jvmtiError
1070 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
1071   oop e = JNIHandles::resolve_external_guard(exception);
1072   NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1073 
1074   JavaThread::send_async_exception(java_thread->threadObj(), e);
1075 
1076   return JVMTI_ERROR_NONE;
1077 
1078 } /* end StopThread */
1079 
1080 
1081 // Threads_lock NOT held
1082 // thread - NOT pre-checked
1083 jvmtiError
1084 JvmtiEnv::InterruptThread(jthread thread) {
1085   // TODO: this is very similar to JVM_Interrupt(); share code in future



1086   JavaThread* current_thread  = JavaThread::current();
1087   JavaThread* java_thread = NULL;
1088   ThreadsListHandle tlh(current_thread);
1089   jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);
1090   if (err != JVMTI_ERROR_NONE) {
1091     return err;
1092   }
1093 









1094   Thread::interrupt(java_thread);
1095 
1096   return JVMTI_ERROR_NONE;
1097 } /* end InterruptThread */
1098 
1099 
1100 // Threads_lock NOT held
1101 // thread - NOT pre-checked
1102 // info_ptr - pre-checked for NULL
1103 jvmtiError
1104 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1105   ResourceMark rm;
1106   HandleMark hm;
1107 
1108   JavaThread* current_thread = JavaThread::current();
1109   ThreadsListHandle tlh(current_thread);
1110 
1111   // if thread is NULL the current thread is used
1112   oop thread_oop = NULL;
1113   if (thread == NULL) {
1114     thread_oop = current_thread->threadObj();
1115     if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
1116       return JVMTI_ERROR_INVALID_THREAD;
1117     }
1118   } else {
1119     JavaThread* java_thread = NULL;
1120     jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1121     if (err != JVMTI_ERROR_NONE) {
1122       // We got an error code so we don't have a JavaThread *, but
1123       // only return an error from here if we didn't get a valid
1124       // thread_oop.
1125       if (thread_oop == NULL) {
1126         return err;
1127       }
1128       // We have a valid thread_oop so we can return some thread info.
1129     }
1130   }
1131 
1132   Handle thread_obj(current_thread, thread_oop);
1133   Handle name;
1134   ThreadPriority priority;
1135   Handle     thread_group;
1136   Handle context_class_loader;
1137   bool          is_daemon;
1138 
1139   { MutexLocker mu(Threads_lock);
1140 
1141     name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1142     priority = java_lang_Thread::priority(thread_obj());
1143     thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1144     is_daemon = java_lang_Thread::is_daemon(thread_obj());
1145 
1146     oop loader = java_lang_Thread::context_class_loader(thread_obj());
1147     context_class_loader = Handle(current_thread, loader);
1148   }
1149   { const char *n;
1150 
1151     if (name() != NULL) {
1152       n = java_lang_String::as_utf8_string(name());
1153     } else {
1154       int utf8_length = 0;
1155       n = UNICODE::as_utf8((jchar*) NULL, utf8_length);
1156     }
1157 
1158     info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1159     if (info_ptr->name == NULL)
1160       return JVMTI_ERROR_OUT_OF_MEMORY;
1161 
1162     strcpy(info_ptr->name, n);
1163   }
1164   info_ptr->is_daemon = is_daemon;
1165   info_ptr->priority  = priority;
1166 
1167   info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
1168                                      jni_reference(context_class_loader);
1169   info_ptr->thread_group = jni_reference(thread_group);
1170 
1171   return JVMTI_ERROR_NONE;
1172 } /* end GetThreadInfo */
1173 
1174 
1175 // Threads_lock NOT held, java_thread not protected by lock
1176 // java_thread - pre-checked
1177 // owned_monitor_count_ptr - pre-checked for NULL
1178 // owned_monitors_ptr - pre-checked for NULL
1179 jvmtiError
1180 JvmtiEnv::GetOwnedMonitorInfo(JavaThread* java_thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1181   jvmtiError err = JVMTI_ERROR_NONE;
1182   JavaThread* calling_thread = JavaThread::current();
1183 
1184   // growable array of jvmti monitors info on the C-heap
1185   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1186       new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1187 
1188   // It is only safe to perform the direct operation on the current
1189   // thread. All other usage needs to use a vm-safepoint-op for safety.
1190   if (java_thread == calling_thread) {
1191     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1192   } else {
1193     // JVMTI get monitors info at safepoint. Do not require target thread to
1194     // be suspended.
1195     VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1196     VMThread::execute(&op);
1197     err = op.result();
1198   }
1199   jint owned_monitor_count = owned_monitors_list->length();
1200   if (err == JVMTI_ERROR_NONE) {
1201     if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1202                       (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1203       // copy into the returned array
1204       for (int i = 0; i < owned_monitor_count; i++) {
1205         (*owned_monitors_ptr)[i] =
1206           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1207       }
1208       *owned_monitor_count_ptr = owned_monitor_count;
1209     }
1210   }
1211   // clean up.
1212   for (int i = 0; i < owned_monitor_count; i++) {
1213     deallocate((unsigned char*)owned_monitors_list->at(i));
1214   }
1215   delete owned_monitors_list;
1216 
1217   return err;
1218 } /* end GetOwnedMonitorInfo */
1219 
1220 
1221 // Threads_lock NOT held, java_thread not protected by lock
1222 // java_thread - pre-checked
1223 // monitor_info_count_ptr - pre-checked for NULL
1224 // monitor_info_ptr - pre-checked for NULL
1225 jvmtiError
1226 JvmtiEnv::GetOwnedMonitorStackDepthInfo(JavaThread* java_thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1227   jvmtiError err = JVMTI_ERROR_NONE;
1228   JavaThread* calling_thread  = JavaThread::current();
1229 
1230   // growable array of jvmti monitors info on the C-heap
1231   GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1232          new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1233 
1234   // It is only safe to perform the direct operation on the current
1235   // thread. All other usage needs to use a vm-safepoint-op for safety.
1236   if (java_thread == calling_thread) {
1237     err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1238   } else {
1239     // JVMTI get owned monitors info at safepoint. Do not require target thread to
1240     // be suspended.
1241     VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1242     VMThread::execute(&op);
1243     err = op.result();
1244   }
1245 
1246   jint owned_monitor_count = owned_monitors_list->length();
1247   if (err == JVMTI_ERROR_NONE) {
1248     if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1249                       (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1250       // copy to output array.
1251       for (int i = 0; i < owned_monitor_count; i++) {
1252         (*monitor_info_ptr)[i].monitor =
1253           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1254         (*monitor_info_ptr)[i].stack_depth =
1255           ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1256       }
1257     }
1258     *monitor_info_count_ptr = owned_monitor_count;
1259   }
1260 
1261   // clean up.
1262   for (int i = 0; i < owned_monitor_count; i++) {
1263     deallocate((unsigned char*)owned_monitors_list->at(i));
1264   }
1265   delete owned_monitors_list;
1266 
1267   return err;
1268 } /* end GetOwnedMonitorStackDepthInfo */
1269 
1270 
1271 // Threads_lock NOT held, java_thread not protected by lock
1272 // java_thread - pre-checked
1273 // monitor_ptr - pre-checked for NULL
1274 jvmtiError
1275 JvmtiEnv::GetCurrentContendedMonitor(JavaThread* java_thread, jobject* monitor_ptr) {
1276   jvmtiError err = JVMTI_ERROR_NONE;
1277   JavaThread* calling_thread  = JavaThread::current();
1278 
1279   // It is only safe to perform the direct operation on the current
1280   // thread. All other usage needs to use a vm-safepoint-op for safety.
1281   if (java_thread == calling_thread) {
1282     err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
1283   } else {
1284     // get contended monitor information at safepoint.
1285     VM_GetCurrentContendedMonitor op(this, calling_thread, java_thread, monitor_ptr);
1286     VMThread::execute(&op);
1287     err = op.result();
1288   }
1289   return err;
1290 } /* end GetCurrentContendedMonitor */
1291 
1292 
1293 // Threads_lock NOT held
1294 // thread - NOT pre-checked
1295 // proc - pre-checked for NULL
1296 // arg - NULL is a valid value, must be checked
1297 jvmtiError
1298 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1299   JavaThread* current_thread = JavaThread::current();
1300 
1301   JavaThread* java_thread = NULL;
1302   oop thread_oop = NULL;
1303   ThreadsListHandle tlh(current_thread);
1304   jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1305   if (err != JVMTI_ERROR_NONE) {
1306     // We got an error code so we don't have a JavaThread *, but
1307     // only return an error from here if we didn't get a valid
1308     // thread_oop.
1309     if (thread_oop == NULL) {
1310       return err;
1311     }
1312     // We have a valid thread_oop.
1313   }
1314 
1315   if (java_thread != NULL) {
1316     // 'thread' refers to an existing JavaThread.
1317     return JVMTI_ERROR_INVALID_THREAD;
1318   }
1319 
1320   if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1321     return JVMTI_ERROR_INVALID_PRIORITY;
1322   }
1323 



1324   Handle thread_hndl(current_thread, thread_oop);
1325   {
1326     MutexLocker mu(Threads_lock); // grab Threads_lock
1327 
1328     JvmtiAgentThread *new_thread = new JvmtiAgentThread(this, proc, arg);
1329 
1330     // At this point it may be possible that no osthread was created for the
1331     // JavaThread due to lack of memory.
1332     if (new_thread == NULL || new_thread->osthread() == NULL) {
1333       if (new_thread != NULL) {
1334         new_thread->smr_delete();
1335       }
1336       return JVMTI_ERROR_OUT_OF_MEMORY;
1337     }
1338 
1339     java_lang_Thread::set_thread(thread_hndl(), new_thread);
1340     java_lang_Thread::set_priority(thread_hndl(), (ThreadPriority)priority);
1341     java_lang_Thread::set_daemon(thread_hndl());
1342 
1343     new_thread->set_threadObj(thread_hndl());
1344     Threads::add(new_thread);
1345     Thread::start(new_thread);
1346   } // unlock Threads_lock
1347 
1348   return JVMTI_ERROR_NONE;
1349 } /* end RunAgentThread */
1350 
1351   //
1352   // Thread Group functions
1353   //
1354 
1355 // group_count_ptr - pre-checked for NULL
1356 // groups_ptr - pre-checked for NULL
1357 jvmtiError
1358 JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
1359   JavaThread* current_thread = JavaThread::current();
1360 
1361   // Only one top level thread group now.
1362   *group_count_ptr = 1;
1363 
1364   // Allocate memory to store global-refs to the thread groups.
1365   // Assume this area is freed by caller.
1366   *groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
1367 
1368   NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
1369 
1370   // Convert oop to Handle, then convert Handle to global-ref.
1371   {
1372     HandleMark hm(current_thread);
1373     Handle system_thread_group(current_thread, Universe::system_thread_group());
1374     *groups_ptr[0] = jni_reference(system_thread_group);
1375   }
1376 
1377   return JVMTI_ERROR_NONE;
1378 } /* end GetTopThreadGroups */
1379 
1380 
1381 // info_ptr - pre-checked for NULL
1382 jvmtiError
1383 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1384   ResourceMark rm;
1385   HandleMark hm;
1386 
1387   JavaThread* current_thread = JavaThread::current();
1388 
1389   Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1390   NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1391 
1392   const char* name;
1393   Handle parent_group;
1394   bool is_daemon;
1395   ThreadPriority max_priority;
1396 
1397   { MutexLocker mu(Threads_lock);
1398 
1399     name         = java_lang_ThreadGroup::name(group_obj());
1400     parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
1401     is_daemon    = java_lang_ThreadGroup::is_daemon(group_obj());
1402     max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1403   }
1404 
1405   info_ptr->is_daemon    = is_daemon;
1406   info_ptr->max_priority = max_priority;
1407   info_ptr->parent       = jni_reference(parent_group);
1408 
1409   if (name != NULL) {
1410     info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1411     NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1412     strcpy(info_ptr->name, name);
1413   } else {
1414     info_ptr->name = NULL;
1415   }
1416 
1417   return JVMTI_ERROR_NONE;
1418 } /* end GetThreadGroupInfo */
1419 
1420 
1421 // thread_count_ptr - pre-checked for NULL
1422 // threads_ptr - pre-checked for NULL
1423 // group_count_ptr - pre-checked for NULL
1424 // groups_ptr - pre-checked for NULL
1425 jvmtiError
1426 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1427   JavaThread* current_thread = JavaThread::current();
1428   oop group_obj = (oop) JNIHandles::resolve_external_guard(group);
1429   NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1430 
1431   Handle *thread_objs = NULL;
1432   Handle *group_objs  = NULL;
1433   int nthreads = 0;
1434   int ngroups = 0;
1435   int hidden_threads = 0;
1436 
1437   ResourceMark rm(current_thread);
1438   HandleMark hm(current_thread);
1439 
1440   Handle group_hdl(current_thread, group_obj);
1441 
1442   { // Cannot allow thread or group counts to change.
1443     MutexLocker mu(Threads_lock);
1444 
1445     nthreads = java_lang_ThreadGroup::nthreads(group_hdl());
1446     ngroups  = java_lang_ThreadGroup::ngroups(group_hdl());
1447 
1448     if (nthreads > 0) {
1449       ThreadsListHandle tlh(current_thread);
1450       objArrayOop threads = java_lang_ThreadGroup::threads(group_hdl());
1451       assert(nthreads <= threads->length(), "too many threads");
1452       thread_objs = NEW_RESOURCE_ARRAY(Handle,nthreads);
1453       for (int i=0, j=0; i<nthreads; i++) {
1454         oop thread_obj = threads->obj_at(i);
1455         assert(thread_obj != NULL, "thread_obj is NULL");
1456         JavaThread *java_thread = NULL;
1457         jvmtiError err = JvmtiExport::cv_oop_to_JavaThread(tlh.list(), thread_obj, &java_thread);
1458         if (err == JVMTI_ERROR_NONE) {
1459           // Have a valid JavaThread*.
1460           if (java_thread->is_hidden_from_external_view()) {
1461             // Filter out hidden java threads.

1462             hidden_threads++;
1463             continue;
1464           }
1465         } else {
1466           // We couldn't convert thread_obj into a JavaThread*.
1467           if (err == JVMTI_ERROR_INVALID_THREAD) {
1468             // The thread_obj does not refer to a java.lang.Thread object
1469             // so skip it.
1470             hidden_threads++;
1471             continue;
1472           }
1473           // We have a valid thread_obj, but no JavaThread*; the caller
1474           // can still have limited use for the thread_obj.
1475         }
1476         thread_objs[j++] = Handle(current_thread, thread_obj);
1477       }
1478       nthreads -= hidden_threads;
1479     } // ThreadsListHandle is destroyed here.
1480 
1481     if (ngroups > 0) {
1482       objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
1483       assert(ngroups <= groups->length(), "too many groups");
1484       group_objs = NEW_RESOURCE_ARRAY(Handle,ngroups);
1485       for (int i=0; i<ngroups; i++) {
1486         oop group_obj = groups->obj_at(i);
1487         assert(group_obj != NULL, "group_obj != NULL");
1488         group_objs[i] = Handle(current_thread, group_obj);
1489       }
1490     }
1491   }
1492 
1493   // have to make global handles outside of Threads_lock
1494   *group_count_ptr  = ngroups;
1495   *thread_count_ptr = nthreads;
1496   *threads_ptr     = new_jthreadArray(nthreads, thread_objs);
1497   *groups_ptr      = new_jthreadGroupArray(ngroups, group_objs);
1498   if ((nthreads > 0) && (*threads_ptr == NULL)) {
1499     return JVMTI_ERROR_OUT_OF_MEMORY;
1500   }
1501   if ((ngroups > 0) && (*groups_ptr == NULL)) {
1502     return JVMTI_ERROR_OUT_OF_MEMORY;
1503   }
1504 
1505   return JVMTI_ERROR_NONE;
1506 } /* end GetThreadGroupChildren */
1507 
1508 
1509   //
1510   // Stack Frame functions
1511   //
1512 
1513 // Threads_lock NOT held, java_thread not protected by lock
1514 // java_thread - pre-checked
1515 // max_frame_count - pre-checked to be greater than or equal to 0
1516 // frame_buffer - pre-checked for NULL
1517 // count_ptr - pre-checked for NULL
1518 jvmtiError
1519 JvmtiEnv::GetStackTrace(JavaThread* java_thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1520   jvmtiError err = JVMTI_ERROR_NONE;
1521 
1522   // It is only safe to perform the direct operation on the current
1523   // thread. All other usage needs to use a vm-safepoint-op for safety.
1524   if (java_thread == JavaThread::current()) {
1525     err = get_stack_trace(java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1526   } else {
1527     // JVMTI get stack trace at safepoint. Do not require target thread to
1528     // be suspended.
1529     VM_GetStackTrace op(this, java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1530     VMThread::execute(&op);
1531     err = op.result();
1532   }
1533 
1534   return err;
1535 } /* end GetStackTrace */
1536 
1537 
1538 // max_frame_count - pre-checked to be greater than or equal to 0
1539 // stack_info_ptr - pre-checked for NULL
1540 // thread_count_ptr - pre-checked for NULL
1541 jvmtiError
1542 JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
1543   jvmtiError err = JVMTI_ERROR_NONE;
1544   JavaThread* calling_thread = JavaThread::current();
1545 
1546   // JVMTI get stack traces at safepoint.
1547   VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1548   VMThread::execute(&op);
1549   *thread_count_ptr = op.final_thread_count();
1550   *stack_info_ptr = op.stack_info();
1551   err = op.result();
1552   return err;
1553 } /* end GetAllStackTraces */
1554 
1555 
1556 // thread_count - pre-checked to be greater than or equal to 0
1557 // thread_list - pre-checked for NULL
1558 // max_frame_count - pre-checked to be greater than or equal to 0
1559 // stack_info_ptr - pre-checked for NULL
1560 jvmtiError
1561 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1562   jvmtiError err = JVMTI_ERROR_NONE;
1563   // JVMTI get stack traces at safepoint.
1564   VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1565   VMThread::execute(&op);
1566   err = op.result();
1567   if (err == JVMTI_ERROR_NONE) {
1568     *stack_info_ptr = op.stack_info();
1569   }
1570   return err;
1571 } /* end GetThreadListStackTraces */
1572 
1573 
1574 // Threads_lock NOT held, java_thread not protected by lock
1575 // java_thread - pre-checked
1576 // count_ptr - pre-checked for NULL
1577 jvmtiError
1578 JvmtiEnv::GetFrameCount(JavaThread* java_thread, jint* count_ptr) {
1579   jvmtiError err = JVMTI_ERROR_NONE;
1580 
1581   // retrieve or create JvmtiThreadState.
1582   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1583   if (state == NULL) {
1584     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1585   }
1586 
1587   // It is only safe to perform the direct operation on the current
1588   // thread. All other usage needs to use a vm-safepoint-op for safety.
1589   if (java_thread == JavaThread::current()) {
1590     err = get_frame_count(state, count_ptr);
1591   } else {
1592     // get java stack frame count at safepoint.
1593     VM_GetFrameCount op(this, state, count_ptr);
1594     VMThread::execute(&op);
1595     err = op.result();
1596   }
1597   return err;
1598 } /* end GetFrameCount */
1599 
1600 
1601 // Threads_lock NOT held, java_thread not protected by lock
1602 // java_thread - pre-checked
1603 jvmtiError
1604 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1605   JavaThread* current_thread  = JavaThread::current();
1606   HandleMark hm(current_thread);
1607   uint32_t debug_bits = 0;
1608 
1609   // retrieve or create the state
1610   JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1611   if (state == NULL) {
1612     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1613   }
1614 
1615   // Check if java_thread is fully suspended
1616   if (!java_thread->is_thread_fully_suspended(true /* wait for suspend completion */, &debug_bits)) {
1617     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1618   }
1619   // Check to see if a PopFrame was already in progress
1620   if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
1621     // Probably possible for JVMTI clients to trigger this, but the
1622     // JPDA backend shouldn't allow this to happen
1623     return JVMTI_ERROR_INTERNAL;
1624   }
1625 
1626   {
1627     // Was workaround bug
1628     //    4812902: popFrame hangs if the method is waiting at a synchronize
1629     // Catch this condition and return an error to avoid hanging.
1630     // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
1631     OSThread* osThread = java_thread->osthread();
1632     if (osThread->get_state() == MONITOR_WAIT) {
1633       return JVMTI_ERROR_OPAQUE_FRAME;
1634     }
1635   }
1636 
1637   {
1638     ResourceMark rm(current_thread);
1639     // Check if there are more than one Java frame in this thread, that the top two frames
1640     // are Java (not native) frames, and that there is no intervening VM frame
1641     int frame_count = 0;
1642     bool is_interpreted[2];
1643     intptr_t *frame_sp[2];
1644     // The 2-nd arg of constructor is needed to stop iterating at java entry frame.
1645     for (vframeStream vfs(java_thread, true); !vfs.at_end(); vfs.next()) {
1646       methodHandle mh(current_thread, vfs.method());
1647       if (mh->is_native()) return(JVMTI_ERROR_OPAQUE_FRAME);
1648       is_interpreted[frame_count] = vfs.is_interpreted_frame();
1649       frame_sp[frame_count] = vfs.frame_id();
1650       if (++frame_count > 1) break;
1651     }
1652     if (frame_count < 2)  {
1653       // We haven't found two adjacent non-native Java frames on the top.
1654       // There can be two situations here:
1655       //  1. There are no more java frames
1656       //  2. Two top java frames are separated by non-java native frames
1657       if(vframeFor(java_thread, 1) == NULL) {
1658         return JVMTI_ERROR_NO_MORE_FRAMES;
1659       } else {
1660         // Intervening non-java native or VM frames separate java frames.
1661         // Current implementation does not support this. See bug #5031735.
1662         // In theory it is possible to pop frames in such cases.
1663         return JVMTI_ERROR_OPAQUE_FRAME;
1664       }
1665     }
1666 
1667     // If any of the top 2 frames is a compiled one, need to deoptimize it
1668     for (int i = 0; i < 2; i++) {
1669       if (!is_interpreted[i]) {
1670         Deoptimization::deoptimize_frame(java_thread, frame_sp[i]);
1671       }
1672     }
1673 
1674     // Update the thread state to reflect that the top frame is popped
1675     // so that cur_stack_depth is maintained properly and all frameIDs
1676     // are invalidated.
1677     // The current frame will be popped later when the suspended thread
1678     // is resumed and right before returning from VM to Java.
1679     // (see call_VM_base() in assembler_<cpu>.cpp).
1680 
1681     // It's fine to update the thread state here because no JVMTI events
1682     // shall be posted for this PopFrame.
1683 
1684     // It is only safe to perform the direct operation on the current
1685     // thread. All other usage needs to use a vm-safepoint-op for safety.
1686     if (java_thread == JavaThread::current()) {
1687       state->update_for_pop_top_frame();
1688     } else {
1689       VM_UpdateForPopTopFrame op(state);
1690       VMThread::execute(&op);
1691       jvmtiError err = op.result();
1692       if (err != JVMTI_ERROR_NONE) {
1693         return err;
1694       }
1695     }
1696 
1697     java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
1698     // Set pending step flag for this popframe and it is cleared when next
1699     // step event is posted.
1700     state->set_pending_step_for_popframe();
1701   }
1702 
1703   return JVMTI_ERROR_NONE;
1704 } /* end PopFrame */
1705 
1706 
1707 // Threads_lock NOT held, java_thread not protected by lock
1708 // java_thread - pre-checked
1709 // java_thread - unchecked
1710 // depth - pre-checked as non-negative
1711 // method_ptr - pre-checked for NULL
1712 // location_ptr - pre-checked for NULL
1713 jvmtiError
1714 JvmtiEnv::GetFrameLocation(JavaThread* java_thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1715   jvmtiError err = JVMTI_ERROR_NONE;
1716 
1717   // It is only safe to perform the direct operation on the current
1718   // thread. All other usage needs to use a vm-safepoint-op for safety.
1719   if (java_thread == JavaThread::current()) {
1720     err = get_frame_location(java_thread, depth, method_ptr, location_ptr);
1721   } else {
1722     // JVMTI get java stack frame location at safepoint.
1723     VM_GetFrameLocation op(this, java_thread, depth, method_ptr, location_ptr);
1724     VMThread::execute(&op);
1725     err = op.result();
1726   }
1727   return err;
1728 } /* end GetFrameLocation */
1729 
1730 
1731 // Threads_lock NOT held, java_thread not protected by lock
1732 // java_thread - pre-checked
1733 // java_thread - unchecked
1734 // depth - pre-checked as non-negative
1735 jvmtiError
1736 JvmtiEnv::NotifyFramePop(JavaThread* java_thread, jint depth) {
1737   jvmtiError err = JVMTI_ERROR_NONE;
1738   ResourceMark rm;
1739   uint32_t debug_bits = 0;
1740 
1741   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
1742   if (state == NULL) {
1743     return JVMTI_ERROR_THREAD_NOT_ALIVE;
1744   }
1745 
1746   if (!java_thread->is_thread_fully_suspended(true, &debug_bits)) {
1747     return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1748   }
1749 
1750   if (TraceJVMTICalls) {
1751     JvmtiSuspendControl::print();
1752   }
1753 
1754   vframe *vf = vframeFor(java_thread, depth);
1755   if (vf == NULL) {
1756     return JVMTI_ERROR_NO_MORE_FRAMES;
1757   }
1758 
1759   if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
1760     return JVMTI_ERROR_OPAQUE_FRAME;
1761   }
1762 
1763   assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
1764 
1765   // It is only safe to perform the direct operation on the current
1766   // thread. All other usage needs to use a vm-safepoint-op for safety.
1767   if (java_thread == JavaThread::current()) {
1768     int frame_number = state->count_frames() - depth;
1769     state->env_thread_state(this)->set_frame_pop(frame_number);
1770   } else {
1771     VM_SetFramePop op(this, state, depth);
1772     VMThread::execute(&op);
1773     err = op.result();
1774   }
1775   return err;
1776 } /* end NotifyFramePop */
1777 
1778 
1779   //
1780   // Force Early Return functions
1781   //
1782 
1783 // Threads_lock NOT held, java_thread not protected by lock
1784 // java_thread - pre-checked
1785 jvmtiError
1786 JvmtiEnv::ForceEarlyReturnObject(JavaThread* java_thread, jobject value) {
1787   jvalue val;
1788   val.l = value;
1789   return force_early_return(java_thread, val, atos);
1790 } /* end ForceEarlyReturnObject */
1791 
1792 
1793 // Threads_lock NOT held, java_thread not protected by lock
1794 // java_thread - pre-checked
1795 jvmtiError
1796 JvmtiEnv::ForceEarlyReturnInt(JavaThread* java_thread, jint value) {
1797   jvalue val;
1798   val.i = value;
1799   return force_early_return(java_thread, val, itos);
1800 } /* end ForceEarlyReturnInt */
1801 
1802 
1803 // Threads_lock NOT held, java_thread not protected by lock
1804 // java_thread - pre-checked
1805 jvmtiError
1806 JvmtiEnv::ForceEarlyReturnLong(JavaThread* java_thread, jlong value) {
1807   jvalue val;
1808   val.j = value;
1809   return force_early_return(java_thread, val, ltos);
1810 } /* end ForceEarlyReturnLong */
1811 
1812 
1813 // Threads_lock NOT held, java_thread not protected by lock
1814 // java_thread - pre-checked
1815 jvmtiError
1816 JvmtiEnv::ForceEarlyReturnFloat(JavaThread* java_thread, jfloat value) {
1817   jvalue val;
1818   val.f = value;
1819   return force_early_return(java_thread, val, ftos);
1820 } /* end ForceEarlyReturnFloat */
1821 
1822 
1823 // Threads_lock NOT held, java_thread not protected by lock
1824 // java_thread - pre-checked
1825 jvmtiError
1826 JvmtiEnv::ForceEarlyReturnDouble(JavaThread* java_thread, jdouble value) {
1827   jvalue val;
1828   val.d = value;
1829   return force_early_return(java_thread, val, dtos);
1830 } /* end ForceEarlyReturnDouble */
1831 
1832 
1833 // Threads_lock NOT held, java_thread not protected by lock
1834 // java_thread - pre-checked
1835 jvmtiError
1836 JvmtiEnv::ForceEarlyReturnVoid(JavaThread* java_thread) {
1837   jvalue val;
1838   val.j = 0L;
1839   return force_early_return(java_thread, val, vtos);
1840 } /* end ForceEarlyReturnVoid */
1841 
1842 
1843   //
1844   // Heap functions
1845   //
1846 
1847 // klass - NULL is a valid value, must be checked
1848 // initial_object - NULL is a valid value, must be checked
1849 // callbacks - pre-checked for NULL
1850 // user_data - NULL is a valid value, must be checked
1851 jvmtiError
1852 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1853   // check klass if provided
1854   Klass* k = NULL;
1855   if (klass != NULL) {
1856     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1857     if (k_mirror == NULL) {
1858       return JVMTI_ERROR_INVALID_CLASS;
1859     }
1860     if (java_lang_Class::is_primitive(k_mirror)) {
1861       return JVMTI_ERROR_NONE;
1862     }
1863     k = java_lang_Class::as_Klass(k_mirror);
1864     if (klass == NULL) {
1865       return JVMTI_ERROR_INVALID_CLASS;
1866     }
1867   }
1868 
1869   if (initial_object != NULL) {
1870     oop init_obj = JNIHandles::resolve_external_guard(initial_object);
1871     if (init_obj == NULL) {
1872       return JVMTI_ERROR_INVALID_OBJECT;
1873     }
1874   }
1875 
1876   Thread *thread = Thread::current();
1877   HandleMark hm(thread);
1878 
1879   TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
1880   JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, k, initial_object, callbacks, user_data);
1881   return JVMTI_ERROR_NONE;
1882 } /* end FollowReferences */
1883 
1884 
1885 // klass - NULL is a valid value, must be checked
1886 // callbacks - pre-checked for NULL
1887 // user_data - NULL is a valid value, must be checked
1888 jvmtiError
1889 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1890   // check klass if provided
1891   Klass* k = NULL;
1892   if (klass != NULL) {
1893     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1894     if (k_mirror == NULL) {
1895       return JVMTI_ERROR_INVALID_CLASS;
1896     }
1897     if (java_lang_Class::is_primitive(k_mirror)) {
1898       return JVMTI_ERROR_NONE;
1899     }
1900     k = java_lang_Class::as_Klass(k_mirror);
1901     if (k == NULL) {
1902       return JVMTI_ERROR_INVALID_CLASS;
1903     }
1904   }
1905 
1906   TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1907   JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, k, callbacks, user_data);
1908   return JVMTI_ERROR_NONE;
1909 } /* end IterateThroughHeap */
1910 
1911 
1912 // tag_ptr - pre-checked for NULL
1913 jvmtiError
1914 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1915   oop o = JNIHandles::resolve_external_guard(object);
1916   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1917   *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1918   return JVMTI_ERROR_NONE;
1919 } /* end GetTag */
1920 
1921 
1922 jvmtiError
1923 JvmtiEnv::SetTag(jobject object, jlong tag) {
1924   oop o = JNIHandles::resolve_external_guard(object);
1925   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1926   JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1927   return JVMTI_ERROR_NONE;
1928 } /* end SetTag */
1929 
1930 
1931 // tag_count - pre-checked to be greater than or equal to 0
1932 // tags - pre-checked for NULL
1933 // count_ptr - pre-checked for NULL
1934 // object_result_ptr - NULL is a valid value, must be checked
1935 // tag_result_ptr - NULL is a valid value, must be checked
1936 jvmtiError
1937 JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
1938   TraceTime t("GetObjectsWithTags", TRACETIME_LOG(Debug, jvmti, objecttagging));
1939   return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
1940 } /* end GetObjectsWithTags */
1941 
1942 
1943 jvmtiError
1944 JvmtiEnv::ForceGarbageCollection() {
1945   Universe::heap()->collect(GCCause::_jvmti_force_gc);
1946   return JVMTI_ERROR_NONE;
1947 } /* end ForceGarbageCollection */
1948 
1949 
1950   //
1951   // Heap (1.0) functions
1952   //
1953 
1954 // object_reference_callback - pre-checked for NULL
1955 // user_data - NULL is a valid value, must be checked
1956 jvmtiError
1957 JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
1958   oop o = JNIHandles::resolve_external_guard(object);
1959   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1960   JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
1961   return JVMTI_ERROR_NONE;
1962 } /* end IterateOverObjectsReachableFromObject */
1963 
1964 
1965 // heap_root_callback - NULL is a valid value, must be checked
1966 // stack_ref_callback - NULL is a valid value, must be checked
1967 // object_ref_callback - NULL is a valid value, must be checked
1968 // user_data - NULL is a valid value, must be checked
1969 jvmtiError
1970 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
1971   TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
1972   JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
1973   return JVMTI_ERROR_NONE;
1974 } /* end IterateOverReachableObjects */
1975 
1976 
1977 // heap_object_callback - pre-checked for NULL
1978 // user_data - NULL is a valid value, must be checked
1979 jvmtiError
1980 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1981   TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1982   Thread *thread = Thread::current();
1983   HandleMark hm(thread);
1984   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, NULL, heap_object_callback, user_data);
1985   return JVMTI_ERROR_NONE;
1986 } /* end IterateOverHeap */
1987 
1988 
1989 // k_mirror - may be primitive, this must be checked
1990 // heap_object_callback - pre-checked for NULL
1991 // user_data - NULL is a valid value, must be checked
1992 jvmtiError
1993 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1994   if (java_lang_Class::is_primitive(k_mirror)) {
1995     // DO PRIMITIVE CLASS PROCESSING
1996     return JVMTI_ERROR_NONE;
1997   }
1998   Klass* klass = java_lang_Class::as_Klass(k_mirror);
1999   if (klass == NULL) {
2000     return JVMTI_ERROR_INVALID_CLASS;
2001   }
2002   TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
2003   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
2004   return JVMTI_ERROR_NONE;
2005 } /* end IterateOverInstancesOfClass */
2006 
2007 
2008   //
2009   // Local Variable functions
2010   //
2011 
2012 // Threads_lock NOT held, java_thread not protected by lock
2013 // java_thread - pre-checked
2014 // java_thread - unchecked
2015 // depth - pre-checked as non-negative
2016 // value_ptr - pre-checked for NULL
2017 jvmtiError
2018 JvmtiEnv::GetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject* value_ptr) {
2019   JavaThread* current_thread = JavaThread::current();
2020   // rm object is created to clean up the javaVFrame created in
2021   // doit_prologue(), but after doit() is finished with it.
2022   ResourceMark rm(current_thread);
2023 
2024   VM_GetOrSetLocal op(java_thread, current_thread, depth, slot);
2025   VMThread::execute(&op);
2026   jvmtiError err = op.result();
2027   if (err != JVMTI_ERROR_NONE) {
2028     return err;
2029   } else {
2030     *value_ptr = op.value().l;
2031     return JVMTI_ERROR_NONE;
2032   }
2033 } /* end GetLocalObject */
2034 
2035 // Threads_lock NOT held, java_thread not protected by lock
2036 // java_thread - pre-checked
2037 // java_thread - unchecked
2038 // depth - pre-checked as non-negative
2039 // value - pre-checked for NULL
2040 jvmtiError
2041 JvmtiEnv::GetLocalInstance(JavaThread* java_thread, jint depth, jobject* value_ptr){
2042   JavaThread* current_thread = JavaThread::current();
2043   // rm object is created to clean up the javaVFrame created in
2044   // doit_prologue(), but after doit() is finished with it.
2045   ResourceMark rm(current_thread);
2046 
2047   VM_GetReceiver op(java_thread, current_thread, depth);
2048   VMThread::execute(&op);
2049   jvmtiError err = op.result();
2050   if (err != JVMTI_ERROR_NONE) {
2051     return err;
2052   } else {
2053     *value_ptr = op.value().l;
2054     return JVMTI_ERROR_NONE;
2055   }
2056 } /* end GetLocalInstance */
2057 
2058 
2059 // Threads_lock NOT held, java_thread not protected by lock
2060 // java_thread - pre-checked
2061 // java_thread - unchecked
2062 // depth - pre-checked as non-negative
2063 // value_ptr - pre-checked for NULL
2064 jvmtiError
2065 JvmtiEnv::GetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint* value_ptr) {
2066   // rm object is created to clean up the javaVFrame created in
2067   // doit_prologue(), but after doit() is finished with it.
2068   ResourceMark rm;
2069 
2070   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT);
2071   VMThread::execute(&op);
2072   *value_ptr = op.value().i;
2073   return op.result();
2074 } /* end GetLocalInt */
2075 
2076 
2077 // Threads_lock NOT held, java_thread not protected by lock
2078 // java_thread - pre-checked
2079 // java_thread - unchecked
2080 // depth - pre-checked as non-negative
2081 // value_ptr - pre-checked for NULL
2082 jvmtiError
2083 JvmtiEnv::GetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong* value_ptr) {
2084   // rm object is created to clean up the javaVFrame created in
2085   // doit_prologue(), but after doit() is finished with it.
2086   ResourceMark rm;
2087 
2088   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG);
2089   VMThread::execute(&op);
2090   *value_ptr = op.value().j;
2091   return op.result();
2092 } /* end GetLocalLong */
2093 
2094 
2095 // Threads_lock NOT held, java_thread not protected by lock
2096 // java_thread - pre-checked
2097 // java_thread - unchecked
2098 // depth - pre-checked as non-negative
2099 // value_ptr - pre-checked for NULL
2100 jvmtiError
2101 JvmtiEnv::GetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat* value_ptr) {
2102   // rm object is created to clean up the javaVFrame created in
2103   // doit_prologue(), but after doit() is finished with it.
2104   ResourceMark rm;
2105 
2106   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT);
2107   VMThread::execute(&op);
2108   *value_ptr = op.value().f;
2109   return op.result();
2110 } /* end GetLocalFloat */
2111 
2112 
2113 // Threads_lock NOT held, java_thread not protected by lock
2114 // java_thread - pre-checked
2115 // java_thread - unchecked
2116 // depth - pre-checked as non-negative
2117 // value_ptr - pre-checked for NULL
2118 jvmtiError
2119 JvmtiEnv::GetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble* value_ptr) {
2120   // rm object is created to clean up the javaVFrame created in
2121   // doit_prologue(), but after doit() is finished with it.
2122   ResourceMark rm;
2123 
2124   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE);
2125   VMThread::execute(&op);
2126   *value_ptr = op.value().d;
2127   return op.result();
2128 } /* end GetLocalDouble */
2129 
2130 
2131 // Threads_lock NOT held, java_thread not protected by lock
2132 // java_thread - pre-checked
2133 // java_thread - unchecked
2134 // depth - pre-checked as non-negative
2135 jvmtiError
2136 JvmtiEnv::SetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject value) {
2137   // rm object is created to clean up the javaVFrame created in
2138   // doit_prologue(), but after doit() is finished with it.
2139   ResourceMark rm;
2140   jvalue val;
2141   val.l = value;
2142   VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val);
2143   VMThread::execute(&op);
2144   return op.result();
2145 } /* end SetLocalObject */
2146 
2147 
2148 // Threads_lock NOT held, java_thread not protected by lock
2149 // java_thread - pre-checked
2150 // java_thread - unchecked
2151 // depth - pre-checked as non-negative
2152 jvmtiError
2153 JvmtiEnv::SetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint value) {
2154   // rm object is created to clean up the javaVFrame created in
2155   // doit_prologue(), but after doit() is finished with it.
2156   ResourceMark rm;
2157   jvalue val;
2158   val.i = value;
2159   VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val);
2160   VMThread::execute(&op);
2161   return op.result();
2162 } /* end SetLocalInt */
2163 
2164 
2165 // Threads_lock NOT held, java_thread not protected by lock
2166 // java_thread - pre-checked
2167 // java_thread - unchecked
2168 // depth - pre-checked as non-negative
2169 jvmtiError
2170 JvmtiEnv::SetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong value) {
2171   // rm object is created to clean up the javaVFrame created in
2172   // doit_prologue(), but after doit() is finished with it.
2173   ResourceMark rm;
2174   jvalue val;
2175   val.j = value;
2176   VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val);
2177   VMThread::execute(&op);
2178   return op.result();
2179 } /* end SetLocalLong */
2180 
2181 
2182 // Threads_lock NOT held, java_thread not protected by lock
2183 // java_thread - pre-checked
2184 // java_thread - unchecked
2185 // depth - pre-checked as non-negative
2186 jvmtiError
2187 JvmtiEnv::SetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat value) {
2188   // rm object is created to clean up the javaVFrame created in
2189   // doit_prologue(), but after doit() is finished with it.
2190   ResourceMark rm;
2191   jvalue val;
2192   val.f = value;
2193   VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val);
2194   VMThread::execute(&op);
2195   return op.result();
2196 } /* end SetLocalFloat */
2197 
2198 
2199 // Threads_lock NOT held, java_thread not protected by lock
2200 // java_thread - pre-checked
2201 // java_thread - unchecked
2202 // depth - pre-checked as non-negative
2203 jvmtiError
2204 JvmtiEnv::SetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble value) {
2205   // rm object is created to clean up the javaVFrame created in
2206   // doit_prologue(), but after doit() is finished with it.
2207   ResourceMark rm;
2208   jvalue val;
2209   val.d = value;
2210   VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val);
2211   VMThread::execute(&op);
2212   return op.result();
2213 } /* end SetLocalDouble */
2214 
2215 
2216   //
2217   // Breakpoint functions
2218   //
2219 
2220 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2221 jvmtiError
2222 JvmtiEnv::SetBreakpoint(Method* method_oop, jlocation location) {
2223   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2224   if (location < 0) {   // simple invalid location check first
2225     return JVMTI_ERROR_INVALID_LOCATION;
2226   }
2227   // verify that the breakpoint is not past the end of the method
2228   if (location >= (jlocation) method_oop->code_size()) {
2229     return JVMTI_ERROR_INVALID_LOCATION;
2230   }
2231 
2232   ResourceMark rm;
2233   JvmtiBreakpoint bp(method_oop, location);
2234   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2235   if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
2236     return JVMTI_ERROR_DUPLICATE;
2237 
2238   if (TraceJVMTICalls) {
2239     jvmti_breakpoints.print();
2240   }
2241 
2242   return JVMTI_ERROR_NONE;
2243 } /* end SetBreakpoint */
2244 
2245 
2246 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2247 jvmtiError
2248 JvmtiEnv::ClearBreakpoint(Method* method_oop, jlocation location) {
2249   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2250 
2251   if (location < 0) {   // simple invalid location check first
2252     return JVMTI_ERROR_INVALID_LOCATION;
2253   }
2254 
2255   // verify that the breakpoint is not past the end of the method
2256   if (location >= (jlocation) method_oop->code_size()) {
2257     return JVMTI_ERROR_INVALID_LOCATION;
2258   }
2259 
2260   JvmtiBreakpoint bp(method_oop, location);
2261 
2262   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2263   if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
2264     return JVMTI_ERROR_NOT_FOUND;
2265 
2266   if (TraceJVMTICalls) {
2267     jvmti_breakpoints.print();
2268   }
2269 
2270   return JVMTI_ERROR_NONE;
2271 } /* end ClearBreakpoint */
2272 
2273 
2274   //
2275   // Watched Field functions
2276   //
2277 
2278 jvmtiError
2279 JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2280   // make sure we haven't set this watch before
2281   if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
2282   fdesc_ptr->set_is_field_access_watched(true);
2283 
2284   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
2285 
2286   return JVMTI_ERROR_NONE;
2287 } /* end SetFieldAccessWatch */
2288 
2289 
2290 jvmtiError
2291 JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2292   // make sure we have a watch to clear
2293   if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
2294   fdesc_ptr->set_is_field_access_watched(false);
2295 
2296   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
2297 
2298   return JVMTI_ERROR_NONE;
2299 } /* end ClearFieldAccessWatch */
2300 
2301 
2302 jvmtiError
2303 JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2304   // make sure we haven't set this watch before
2305   if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
2306   fdesc_ptr->set_is_field_modification_watched(true);
2307 
2308   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
2309 
2310   return JVMTI_ERROR_NONE;
2311 } /* end SetFieldModificationWatch */
2312 
2313 
2314 jvmtiError
2315 JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2316    // make sure we have a watch to clear
2317   if (!fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_NOT_FOUND;
2318   fdesc_ptr->set_is_field_modification_watched(false);
2319 
2320   JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, false);
2321 
2322   return JVMTI_ERROR_NONE;
2323 } /* end ClearFieldModificationWatch */
2324 
2325   //
2326   // Class functions
2327   //
2328 
2329 
2330 // k_mirror - may be primitive, this must be checked
2331 // signature_ptr - NULL is a valid value, must be checked
2332 // generic_ptr - NULL is a valid value, must be checked
2333 jvmtiError
2334 JvmtiEnv::GetClassSignature(oop k_mirror, char** signature_ptr, char** generic_ptr) {
2335   ResourceMark rm;
2336   bool isPrimitive = java_lang_Class::is_primitive(k_mirror);
2337   Klass* k = NULL;
2338   if (!isPrimitive) {
2339     k = java_lang_Class::as_Klass(k_mirror);
2340     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2341   }
2342   if (signature_ptr != NULL) {
2343     char* result = NULL;
2344     if (isPrimitive) {
2345       char tchar = type2char(java_lang_Class::primitive_type(k_mirror));
2346       result = (char*) jvmtiMalloc(2);
2347       result[0] = tchar;
2348       result[1] = '\0';
2349     } else {
2350       const char* class_sig = k->signature_name();
2351       result = (char *) jvmtiMalloc(strlen(class_sig)+1);
2352       strcpy(result, class_sig);
2353     }
2354     *signature_ptr = result;
2355   }
2356   if (generic_ptr != NULL) {
2357     *generic_ptr = NULL;
2358     if (!isPrimitive && k->is_instance_klass()) {
2359       Symbol* soo = InstanceKlass::cast(k)->generic_signature();
2360       if (soo != NULL) {
2361         const char *gen_sig = soo->as_C_string();
2362         if (gen_sig != NULL) {
2363           char* gen_result;
2364           jvmtiError err = allocate(strlen(gen_sig) + 1,
2365                                     (unsigned char **)&gen_result);
2366           if (err != JVMTI_ERROR_NONE) {
2367             return err;
2368           }
2369           strcpy(gen_result, gen_sig);
2370           *generic_ptr = gen_result;
2371         }
2372       }
2373     }
2374   }
2375   return JVMTI_ERROR_NONE;
2376 } /* end GetClassSignature */
2377 
2378 
2379 // k_mirror - may be primitive, this must be checked
2380 // status_ptr - pre-checked for NULL
2381 jvmtiError
2382 JvmtiEnv::GetClassStatus(oop k_mirror, jint* status_ptr) {
2383   jint result = 0;
2384   if (java_lang_Class::is_primitive(k_mirror)) {
2385     result |= JVMTI_CLASS_STATUS_PRIMITIVE;
2386   } else {
2387     Klass* k = java_lang_Class::as_Klass(k_mirror);
2388     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2389     result = k->jvmti_class_status();
2390   }
2391   *status_ptr = result;
2392 
2393   return JVMTI_ERROR_NONE;
2394 } /* end GetClassStatus */
2395 
2396 
2397 // k_mirror - may be primitive, this must be checked
2398 // source_name_ptr - pre-checked for NULL
2399 jvmtiError
2400 JvmtiEnv::GetSourceFileName(oop k_mirror, char** source_name_ptr) {
2401   if (java_lang_Class::is_primitive(k_mirror)) {
2402      return JVMTI_ERROR_ABSENT_INFORMATION;
2403   }
2404   Klass* k_klass = java_lang_Class::as_Klass(k_mirror);
2405   NULL_CHECK(k_klass, JVMTI_ERROR_INVALID_CLASS);
2406 
2407   if (!k_klass->is_instance_klass()) {
2408     return JVMTI_ERROR_ABSENT_INFORMATION;
2409   }
2410 
2411   Symbol* sfnOop = InstanceKlass::cast(k_klass)->source_file_name();
2412   NULL_CHECK(sfnOop, JVMTI_ERROR_ABSENT_INFORMATION);
2413   {
2414     JavaThread* current_thread  = JavaThread::current();
2415     ResourceMark rm(current_thread);
2416     const char* sfncp = (const char*) sfnOop->as_C_string();
2417     *source_name_ptr = (char *) jvmtiMalloc(strlen(sfncp)+1);
2418     strcpy(*source_name_ptr, sfncp);
2419   }
2420 
2421   return JVMTI_ERROR_NONE;
2422 } /* end GetSourceFileName */
2423 
2424 
2425 // k_mirror - may be primitive, this must be checked
2426 // modifiers_ptr - pre-checked for NULL
2427 jvmtiError
2428 JvmtiEnv::GetClassModifiers(oop k_mirror, jint* modifiers_ptr) {
2429   JavaThread* current_thread  = JavaThread::current();
2430   jint result = 0;
2431   if (!java_lang_Class::is_primitive(k_mirror)) {
2432     Klass* k = java_lang_Class::as_Klass(k_mirror);
2433     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2434     result = k->compute_modifier_flags(current_thread);
2435     JavaThread* THREAD = current_thread; // pass to macros
2436     if (HAS_PENDING_EXCEPTION) {
2437       CLEAR_PENDING_EXCEPTION;
2438       return JVMTI_ERROR_INTERNAL;
2439     };
2440 
2441     // Reset the deleted  ACC_SUPER bit ( deleted in compute_modifier_flags()).
2442     if(k->is_super()) {
2443       result |= JVM_ACC_SUPER;
2444     }
2445   } else {
2446     result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
2447   }
2448   *modifiers_ptr = result;
2449 
2450   return JVMTI_ERROR_NONE;
2451 } /* end GetClassModifiers */
2452 
2453 
2454 // k_mirror - may be primitive, this must be checked
2455 // method_count_ptr - pre-checked for NULL
2456 // methods_ptr - pre-checked for NULL
2457 jvmtiError
2458 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
2459   JavaThread* current_thread  = JavaThread::current();
2460   HandleMark hm(current_thread);
2461 
2462   if (java_lang_Class::is_primitive(k_mirror)) {
2463     *method_count_ptr = 0;
2464     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2465     return JVMTI_ERROR_NONE;
2466   }
2467   Klass* k = java_lang_Class::as_Klass(k_mirror);
2468   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2469 
2470   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2471   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2472     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2473   }
2474 
2475   if (!k->is_instance_klass()) {
2476     *method_count_ptr = 0;
2477     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2478     return JVMTI_ERROR_NONE;
2479   }
2480   InstanceKlass* ik = InstanceKlass::cast(k);
2481   // Allocate the result and fill it in
2482   int result_length = ik->methods()->length();
2483   jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2484   int index;
2485   bool jmethodids_found = true;
2486 
2487   if (JvmtiExport::can_maintain_original_method_order()) {
2488     // Use the original method ordering indices stored in the class, so we can emit
2489     // jmethodIDs in the order they appeared in the class file
2490     for (index = 0; index < result_length; index++) {
2491       Method* m = ik->methods()->at(index);
2492       int original_index = ik->method_ordering()->at(index);
2493       assert(original_index >= 0 && original_index < result_length, "invalid original method index");
2494       jmethodID id;
2495       if (jmethodids_found) {
2496         id = m->find_jmethod_id_or_null();
2497         if (id == NULL) {
2498           // If we find an uninitialized value, make sure there is
2499           // enough space for all the uninitialized values we might
2500           // find.
2501           ik->ensure_space_for_methodids(index);
2502           jmethodids_found = false;
2503           id = m->jmethod_id();
2504         }
2505       } else {
2506         id = m->jmethod_id();
2507       }
2508       result_list[original_index] = id;
2509     }
2510   } else {
2511     // otherwise just copy in any order
2512     for (index = 0; index < result_length; index++) {
2513       Method* m = ik->methods()->at(index);
2514       jmethodID id;
2515       if (jmethodids_found) {
2516         id = m->find_jmethod_id_or_null();
2517         if (id == NULL) {
2518           // If we find an uninitialized value, make sure there is
2519           // enough space for all the uninitialized values we might
2520           // find.
2521           ik->ensure_space_for_methodids(index);
2522           jmethodids_found = false;
2523           id = m->jmethod_id();
2524         }
2525       } else {
2526         id = m->jmethod_id();
2527       }
2528       result_list[index] = id;
2529     }
2530   }
2531   // Fill in return value.
2532   *method_count_ptr = result_length;
2533   *methods_ptr = result_list;
2534 
2535   return JVMTI_ERROR_NONE;
2536 } /* end GetClassMethods */
2537 
2538 
2539 // k_mirror - may be primitive, this must be checked
2540 // field_count_ptr - pre-checked for NULL
2541 // fields_ptr - pre-checked for NULL
2542 jvmtiError
2543 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
2544   if (java_lang_Class::is_primitive(k_mirror)) {
2545     *field_count_ptr = 0;
2546     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2547     return JVMTI_ERROR_NONE;
2548   }
2549   JavaThread* current_thread = JavaThread::current();
2550   HandleMark hm(current_thread);
2551   Klass* k = java_lang_Class::as_Klass(k_mirror);
2552   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2553 
2554   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2555   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2556     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2557   }
2558 
2559   if (!k->is_instance_klass()) {
2560     *field_count_ptr = 0;
2561     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2562     return JVMTI_ERROR_NONE;
2563   }
2564 
2565 
2566   InstanceKlass* ik = InstanceKlass::cast(k);
2567 
2568   int result_count = 0;
2569   // First, count the fields.
2570   FilteredFieldStream flds(ik, true, true);
2571   result_count = flds.field_count();
2572 
2573   // Allocate the result and fill it in
2574   jfieldID* result_list = (jfieldID*) jvmtiMalloc(result_count * sizeof(jfieldID));
2575   // The JVMTI spec requires fields in the order they occur in the class file,
2576   // this is the reverse order of what FieldStream hands out.
2577   int id_index = (result_count - 1);
2578 
2579   for (FilteredFieldStream src_st(ik, true, true); !src_st.eos(); src_st.next()) {
2580     result_list[id_index--] = jfieldIDWorkaround::to_jfieldID(
2581                                             ik, src_st.offset(),
2582                                             src_st.access_flags().is_static());
2583   }
2584   assert(id_index == -1, "just checking");
2585   // Fill in the results
2586   *field_count_ptr = result_count;
2587   *fields_ptr = result_list;
2588 
2589   return JVMTI_ERROR_NONE;
2590 } /* end GetClassFields */
2591 
2592 
2593 // k_mirror - may be primitive, this must be checked
2594 // interface_count_ptr - pre-checked for NULL
2595 // interfaces_ptr - pre-checked for NULL
2596 jvmtiError
2597 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2598   {
2599     if (java_lang_Class::is_primitive(k_mirror)) {
2600       *interface_count_ptr = 0;
2601       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2602       return JVMTI_ERROR_NONE;
2603     }
2604     JavaThread* current_thread = JavaThread::current();
2605     HandleMark hm(current_thread);
2606     Klass* k = java_lang_Class::as_Klass(k_mirror);
2607     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2608 
2609     // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2610     if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2611       return JVMTI_ERROR_CLASS_NOT_PREPARED;
2612 
2613     if (!k->is_instance_klass()) {
2614       *interface_count_ptr = 0;
2615       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2616       return JVMTI_ERROR_NONE;
2617     }
2618 
2619     Array<Klass*>* interface_list = InstanceKlass::cast(k)->local_interfaces();
2620     const int result_length = (interface_list == NULL ? 0 : interface_list->length());
2621     jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2622     for (int i_index = 0; i_index < result_length; i_index += 1) {
2623       Klass* klass_at = interface_list->at(i_index);
2624       assert(klass_at->is_klass(), "interfaces must be Klass*s");
2625       assert(klass_at->is_interface(), "interfaces must be interfaces");
2626       oop mirror_at = klass_at->java_mirror();
2627       Handle handle_at = Handle(current_thread, mirror_at);
2628       result_list[i_index] = (jclass) jni_reference(handle_at);
2629     }
2630     *interface_count_ptr = result_length;
2631     *interfaces_ptr = result_list;
2632   }
2633 
2634   return JVMTI_ERROR_NONE;
2635 } /* end GetImplementedInterfaces */
2636 
2637 
2638 // k_mirror - may be primitive, this must be checked
2639 // minor_version_ptr - pre-checked for NULL
2640 // major_version_ptr - pre-checked for NULL
2641 jvmtiError
2642 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2643   if (java_lang_Class::is_primitive(k_mirror)) {
2644     return JVMTI_ERROR_ABSENT_INFORMATION;
2645   }
2646   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2647 
2648   jint status = klass->jvmti_class_status();
2649   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2650     return JVMTI_ERROR_INVALID_CLASS;
2651   }
2652   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2653     return JVMTI_ERROR_ABSENT_INFORMATION;
2654   }
2655 
2656   InstanceKlass* ik = InstanceKlass::cast(klass);
2657   *minor_version_ptr = ik->minor_version();
2658   *major_version_ptr = ik->major_version();
2659 
2660   return JVMTI_ERROR_NONE;
2661 } /* end GetClassVersionNumbers */
2662 
2663 
2664 // k_mirror - may be primitive, this must be checked
2665 // constant_pool_count_ptr - pre-checked for NULL
2666 // constant_pool_byte_count_ptr - pre-checked for NULL
2667 // constant_pool_bytes_ptr - pre-checked for NULL
2668 jvmtiError
2669 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2670   if (java_lang_Class::is_primitive(k_mirror)) {
2671     return JVMTI_ERROR_ABSENT_INFORMATION;
2672   }
2673 
2674   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2675   Thread *thread = Thread::current();
2676   ResourceMark rm(thread);
2677 
2678   jint status = klass->jvmti_class_status();
2679   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2680     return JVMTI_ERROR_INVALID_CLASS;
2681   }
2682   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2683     return JVMTI_ERROR_ABSENT_INFORMATION;
2684   }
2685 
2686   InstanceKlass* ik = InstanceKlass::cast(klass);
2687   JvmtiConstantPoolReconstituter reconstituter(ik);
2688   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2689     return reconstituter.get_error();
2690   }
2691 
2692   unsigned char *cpool_bytes;
2693   int cpool_size = reconstituter.cpool_size();
2694   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2695     return reconstituter.get_error();
2696   }
2697   jvmtiError res = allocate(cpool_size, &cpool_bytes);
2698   if (res != JVMTI_ERROR_NONE) {
2699     return res;
2700   }
2701   reconstituter.copy_cpool_bytes(cpool_bytes);
2702   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2703     return reconstituter.get_error();
2704   }
2705 
2706   constantPoolHandle  constants(thread, ik->constants());
2707   *constant_pool_count_ptr      = constants->length();
2708   *constant_pool_byte_count_ptr = cpool_size;
2709   *constant_pool_bytes_ptr      = cpool_bytes;
2710 
2711   return JVMTI_ERROR_NONE;
2712 } /* end GetConstantPool */
2713 
2714 
2715 // k_mirror - may be primitive, this must be checked
2716 // is_interface_ptr - pre-checked for NULL
2717 jvmtiError
2718 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2719   {
2720     bool result = false;
2721     if (!java_lang_Class::is_primitive(k_mirror)) {
2722       Klass* k = java_lang_Class::as_Klass(k_mirror);
2723       if (k != NULL && k->is_interface()) {
2724         result = true;
2725       }
2726     }
2727     *is_interface_ptr = result;
2728   }
2729 
2730   return JVMTI_ERROR_NONE;
2731 } /* end IsInterface */
2732 
2733 
2734 // k_mirror - may be primitive, this must be checked
2735 // is_array_class_ptr - pre-checked for NULL
2736 jvmtiError
2737 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
2738   {
2739     bool result = false;
2740     if (!java_lang_Class::is_primitive(k_mirror)) {
2741       Klass* k = java_lang_Class::as_Klass(k_mirror);
2742       if (k != NULL && k->is_array_klass()) {
2743         result = true;
2744       }
2745     }
2746     *is_array_class_ptr = result;
2747   }
2748 
2749   return JVMTI_ERROR_NONE;
2750 } /* end IsArrayClass */
2751 
2752 
2753 // k_mirror - may be primitive, this must be checked
2754 // classloader_ptr - pre-checked for NULL
2755 jvmtiError
2756 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
2757   {
2758     if (java_lang_Class::is_primitive(k_mirror)) {
2759       *classloader_ptr = (jclass) jni_reference(Handle());
2760       return JVMTI_ERROR_NONE;
2761     }
2762     JavaThread* current_thread = JavaThread::current();
2763     HandleMark hm(current_thread);
2764     Klass* k = java_lang_Class::as_Klass(k_mirror);
2765     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2766 
2767     oop result_oop = k->class_loader();
2768     if (result_oop == NULL) {
2769       *classloader_ptr = (jclass) jni_reference(Handle());
2770       return JVMTI_ERROR_NONE;
2771     }
2772     Handle result_handle = Handle(current_thread, result_oop);
2773     jclass result_jnihandle = (jclass) jni_reference(result_handle);
2774     *classloader_ptr = result_jnihandle;
2775   }
2776   return JVMTI_ERROR_NONE;
2777 } /* end GetClassLoader */
2778 
2779 
2780 // k_mirror - may be primitive, this must be checked
2781 // source_debug_extension_ptr - pre-checked for NULL
2782 jvmtiError
2783 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
2784   {
2785     if (java_lang_Class::is_primitive(k_mirror)) {
2786       return JVMTI_ERROR_ABSENT_INFORMATION;
2787     }
2788     Klass* k = java_lang_Class::as_Klass(k_mirror);
2789     NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2790     if (!k->is_instance_klass()) {
2791       return JVMTI_ERROR_ABSENT_INFORMATION;
2792     }
2793     const char* sde = InstanceKlass::cast(k)->source_debug_extension();
2794     NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION);
2795 
2796     {
2797       *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1);
2798       strcpy(*source_debug_extension_ptr, sde);
2799     }
2800   }
2801 
2802   return JVMTI_ERROR_NONE;
2803 } /* end GetSourceDebugExtension */
2804 
2805   //
2806   // Object functions
2807   //
2808 
2809 // hash_code_ptr - pre-checked for NULL
2810 jvmtiError
2811 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
2812   oop mirror = JNIHandles::resolve_external_guard(object);
2813   NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
2814   NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
2815 
2816   {
2817     jint result = (jint) mirror->identity_hash();
2818     *hash_code_ptr = result;
2819   }
2820   return JVMTI_ERROR_NONE;
2821 } /* end GetObjectHashCode */
2822 
2823 
2824 // info_ptr - pre-checked for NULL
2825 jvmtiError
2826 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
2827   JavaThread* calling_thread = JavaThread::current();
2828   jvmtiError err = get_object_monitor_usage(calling_thread, object, info_ptr);
2829   if (err == JVMTI_ERROR_THREAD_NOT_SUSPENDED) {
2830     // Some of the critical threads were not suspended. go to a safepoint and try again
2831     VM_GetObjectMonitorUsage op(this, calling_thread, object, info_ptr);
2832     VMThread::execute(&op);
2833     err = op.result();
2834   }
2835   return err;
2836 } /* end GetObjectMonitorUsage */
2837 
2838 
2839   //
2840   // Field functions
2841   //
2842 
2843 // name_ptr - NULL is a valid value, must be checked
2844 // signature_ptr - NULL is a valid value, must be checked
2845 // generic_ptr - NULL is a valid value, must be checked
2846 jvmtiError
2847 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2848   JavaThread* current_thread  = JavaThread::current();
2849   ResourceMark rm(current_thread);
2850   if (name_ptr == NULL) {
2851     // just don't return the name
2852   } else {
2853     const char* fieldName = fdesc_ptr->name()->as_C_string();
2854     *name_ptr =  (char*) jvmtiMalloc(strlen(fieldName) + 1);
2855     if (*name_ptr == NULL)
2856       return JVMTI_ERROR_OUT_OF_MEMORY;
2857     strcpy(*name_ptr, fieldName);
2858   }
2859   if (signature_ptr== NULL) {
2860     // just don't return the signature
2861   } else {
2862     const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
2863     *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
2864     if (*signature_ptr == NULL)
2865       return JVMTI_ERROR_OUT_OF_MEMORY;
2866     strcpy(*signature_ptr, fieldSignature);
2867   }
2868   if (generic_ptr != NULL) {
2869     *generic_ptr = NULL;
2870     Symbol* soop = fdesc_ptr->generic_signature();
2871     if (soop != NULL) {
2872       const char* gen_sig = soop->as_C_string();
2873       if (gen_sig != NULL) {
2874         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2875         if (err != JVMTI_ERROR_NONE) {
2876           return err;
2877         }
2878         strcpy(*generic_ptr, gen_sig);
2879       }
2880     }
2881   }
2882   return JVMTI_ERROR_NONE;
2883 } /* end GetFieldName */
2884 
2885 
2886 // declaring_class_ptr - pre-checked for NULL
2887 jvmtiError
2888 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
2889 
2890   *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
2891   return JVMTI_ERROR_NONE;
2892 } /* end GetFieldDeclaringClass */
2893 
2894 
2895 // modifiers_ptr - pre-checked for NULL
2896 jvmtiError
2897 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
2898 
2899   AccessFlags resultFlags = fdesc_ptr->access_flags();
2900   jint result = resultFlags.as_int();
2901   *modifiers_ptr = result;
2902 
2903   return JVMTI_ERROR_NONE;
2904 } /* end GetFieldModifiers */
2905 
2906 
2907 // is_synthetic_ptr - pre-checked for NULL
2908 jvmtiError
2909 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
2910   *is_synthetic_ptr = fdesc_ptr->is_synthetic();
2911   return JVMTI_ERROR_NONE;
2912 } /* end IsFieldSynthetic */
2913 
2914 
2915   //
2916   // Method functions
2917   //
2918 
2919 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2920 // name_ptr - NULL is a valid value, must be checked
2921 // signature_ptr - NULL is a valid value, must be checked
2922 // generic_ptr - NULL is a valid value, must be checked
2923 jvmtiError
2924 JvmtiEnv::GetMethodName(Method* method_oop, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2925   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2926   JavaThread* current_thread  = JavaThread::current();
2927 
2928   ResourceMark rm(current_thread); // get the utf8 name and signature
2929   if (name_ptr == NULL) {
2930     // just don't return the name
2931   } else {
2932     const char* utf8_name = (const char *) method_oop->name()->as_utf8();
2933     *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
2934     strcpy(*name_ptr, utf8_name);
2935   }
2936   if (signature_ptr == NULL) {
2937     // just don't return the signature
2938   } else {
2939     const char* utf8_signature = (const char *) method_oop->signature()->as_utf8();
2940     *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
2941     strcpy(*signature_ptr, utf8_signature);
2942   }
2943 
2944   if (generic_ptr != NULL) {
2945     *generic_ptr = NULL;
2946     Symbol* soop = method_oop->generic_signature();
2947     if (soop != NULL) {
2948       const char* gen_sig = soop->as_C_string();
2949       if (gen_sig != NULL) {
2950         jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2951         if (err != JVMTI_ERROR_NONE) {
2952           return err;
2953         }
2954         strcpy(*generic_ptr, gen_sig);
2955       }
2956     }
2957   }
2958   return JVMTI_ERROR_NONE;
2959 } /* end GetMethodName */
2960 
2961 
2962 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2963 // declaring_class_ptr - pre-checked for NULL
2964 jvmtiError
2965 JvmtiEnv::GetMethodDeclaringClass(Method* method_oop, jclass* declaring_class_ptr) {
2966   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2967   (*declaring_class_ptr) = get_jni_class_non_null(method_oop->method_holder());
2968   return JVMTI_ERROR_NONE;
2969 } /* end GetMethodDeclaringClass */
2970 
2971 
2972 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2973 // modifiers_ptr - pre-checked for NULL
2974 jvmtiError
2975 JvmtiEnv::GetMethodModifiers(Method* method_oop, jint* modifiers_ptr) {
2976   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2977   (*modifiers_ptr) = method_oop->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2978   return JVMTI_ERROR_NONE;
2979 } /* end GetMethodModifiers */
2980 
2981 
2982 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2983 // max_ptr - pre-checked for NULL
2984 jvmtiError
2985 JvmtiEnv::GetMaxLocals(Method* method_oop, jint* max_ptr) {
2986   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2987   // get max stack
2988   (*max_ptr) = method_oop->max_locals();
2989   return JVMTI_ERROR_NONE;
2990 } /* end GetMaxLocals */
2991 
2992 
2993 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2994 // size_ptr - pre-checked for NULL
2995 jvmtiError
2996 JvmtiEnv::GetArgumentsSize(Method* method_oop, jint* size_ptr) {
2997   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2998   // get size of arguments
2999 
3000   (*size_ptr) = method_oop->size_of_parameters();
3001   return JVMTI_ERROR_NONE;
3002 } /* end GetArgumentsSize */
3003 
3004 
3005 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3006 // entry_count_ptr - pre-checked for NULL
3007 // table_ptr - pre-checked for NULL
3008 jvmtiError
3009 JvmtiEnv::GetLineNumberTable(Method* method_oop, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
3010   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3011   if (!method_oop->has_linenumber_table()) {
3012     return (JVMTI_ERROR_ABSENT_INFORMATION);
3013   }
3014 
3015   // The line number table is compressed so we don't know how big it is until decompressed.
3016   // Decompression is really fast so we just do it twice.
3017 
3018   // Compute size of table
3019   jint num_entries = 0;
3020   CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
3021   while (stream.read_pair()) {
3022     num_entries++;
3023   }
3024   jvmtiLineNumberEntry *jvmti_table =
3025             (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
3026 
3027   // Fill jvmti table
3028   if (num_entries > 0) {
3029     int index = 0;
3030     CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
3031     while (stream.read_pair()) {
3032       jvmti_table[index].start_location = (jlocation) stream.bci();
3033       jvmti_table[index].line_number = (jint) stream.line();
3034       index++;
3035     }
3036     assert(index == num_entries, "sanity check");
3037   }
3038 
3039   // Set up results
3040   (*entry_count_ptr) = num_entries;
3041   (*table_ptr) = jvmti_table;
3042 
3043   return JVMTI_ERROR_NONE;
3044 } /* end GetLineNumberTable */
3045 
3046 
3047 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3048 // start_location_ptr - pre-checked for NULL
3049 // end_location_ptr - pre-checked for NULL
3050 jvmtiError
3051 JvmtiEnv::GetMethodLocation(Method* method_oop, jlocation* start_location_ptr, jlocation* end_location_ptr) {
3052 
3053   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3054   // get start and end location
3055   (*end_location_ptr) = (jlocation) (method_oop->code_size() - 1);
3056   if (method_oop->code_size() == 0) {
3057     // there is no code so there is no start location
3058     (*start_location_ptr) = (jlocation)(-1);
3059   } else {
3060     (*start_location_ptr) = (jlocation)(0);
3061   }
3062 
3063   return JVMTI_ERROR_NONE;
3064 } /* end GetMethodLocation */
3065 
3066 
3067 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3068 // entry_count_ptr - pre-checked for NULL
3069 // table_ptr - pre-checked for NULL
3070 jvmtiError
3071 JvmtiEnv::GetLocalVariableTable(Method* method_oop, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
3072 
3073   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3074   JavaThread* current_thread  = JavaThread::current();
3075 
3076   // does the klass have any local variable information?
3077   InstanceKlass* ik = method_oop->method_holder();
3078   if (!ik->access_flags().has_localvariable_table()) {
3079     return (JVMTI_ERROR_ABSENT_INFORMATION);
3080   }
3081 
3082   ConstantPool* constants = method_oop->constants();
3083   NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
3084 
3085   // in the vm localvariable table representation, 6 consecutive elements in the table
3086   // represent a 6-tuple of shorts
3087   // [start_pc, length, name_index, descriptor_index, signature_index, index]
3088   jint num_entries = method_oop->localvariable_table_length();
3089   jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
3090                 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
3091 
3092   if (num_entries > 0) {
3093     LocalVariableTableElement* table = method_oop->localvariable_table_start();
3094     for (int i = 0; i < num_entries; i++) {
3095       // get the 5 tuple information from the vm table
3096       jlocation start_location = (jlocation) table[i].start_bci;
3097       jint length = (jint) table[i].length;
3098       int name_index = (int) table[i].name_cp_index;
3099       int signature_index = (int) table[i].descriptor_cp_index;
3100       int generic_signature_index = (int) table[i].signature_cp_index;
3101       jint slot = (jint) table[i].slot;
3102 
3103       // get utf8 name and signature
3104       char *name_buf = NULL;
3105       char *sig_buf = NULL;
3106       char *gen_sig_buf = NULL;
3107       {
3108         ResourceMark rm(current_thread);
3109 
3110         const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
3111         name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3112         strcpy(name_buf, utf8_name);
3113 
3114         const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
3115         sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
3116         strcpy(sig_buf, utf8_signature);
3117 
3118         if (generic_signature_index > 0) {
3119           const char *utf8_gen_sign = (const char *)
3120                                        constants->symbol_at(generic_signature_index)->as_utf8();
3121           gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
3122           strcpy(gen_sig_buf, utf8_gen_sign);
3123         }
3124       }
3125 
3126       // fill in the jvmti local variable table
3127       jvmti_table[i].start_location = start_location;
3128       jvmti_table[i].length = length;
3129       jvmti_table[i].name = name_buf;
3130       jvmti_table[i].signature = sig_buf;
3131       jvmti_table[i].generic_signature = gen_sig_buf;
3132       jvmti_table[i].slot = slot;
3133     }
3134   }
3135 
3136   // set results
3137   (*entry_count_ptr) = num_entries;
3138   (*table_ptr) = jvmti_table;
3139 
3140   return JVMTI_ERROR_NONE;
3141 } /* end GetLocalVariableTable */
3142 
3143 
3144 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3145 // bytecode_count_ptr - pre-checked for NULL
3146 // bytecodes_ptr - pre-checked for NULL
3147 jvmtiError
3148 JvmtiEnv::GetBytecodes(Method* method_oop, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
3149   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3150 
3151   HandleMark hm;
3152   methodHandle method(method_oop);
3153   jint size = (jint)method->code_size();
3154   jvmtiError err = allocate(size, bytecodes_ptr);
3155   if (err != JVMTI_ERROR_NONE) {
3156     return err;
3157   }
3158 
3159   (*bytecode_count_ptr) = size;
3160   // get byte codes
3161   JvmtiClassFileReconstituter::copy_bytecodes(method, *bytecodes_ptr);
3162 
3163   return JVMTI_ERROR_NONE;
3164 } /* end GetBytecodes */
3165 
3166 
3167 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3168 // is_native_ptr - pre-checked for NULL
3169 jvmtiError
3170 JvmtiEnv::IsMethodNative(Method* method_oop, jboolean* is_native_ptr) {
3171   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3172   (*is_native_ptr) = method_oop->is_native();
3173   return JVMTI_ERROR_NONE;
3174 } /* end IsMethodNative */
3175 
3176 
3177 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3178 // is_synthetic_ptr - pre-checked for NULL
3179 jvmtiError
3180 JvmtiEnv::IsMethodSynthetic(Method* method_oop, jboolean* is_synthetic_ptr) {
3181   NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3182   (*is_synthetic_ptr) = method_oop->is_synthetic();
3183   return JVMTI_ERROR_NONE;
3184 } /* end IsMethodSynthetic */
3185 
3186 
3187 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3188 // is_obsolete_ptr - pre-checked for NULL
3189 jvmtiError
3190 JvmtiEnv::IsMethodObsolete(Method* method_oop, jboolean* is_obsolete_ptr) {
3191   if (use_version_1_0_semantics() &&
3192       get_capabilities()->can_redefine_classes == 0) {
3193     // This JvmtiEnv requested version 1.0 semantics and this function
3194     // requires the can_redefine_classes capability in version 1.0 so
3195     // we need to return an error here.
3196     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3197   }
3198 
3199   if (method_oop == NULL || method_oop->is_obsolete()) {
3200     *is_obsolete_ptr = true;
3201   } else {
3202     *is_obsolete_ptr = false;
3203   }
3204   return JVMTI_ERROR_NONE;
3205 } /* end IsMethodObsolete */
3206 
3207   //
3208   // Raw Monitor functions
3209   //
3210 
3211 // name - pre-checked for NULL
3212 // monitor_ptr - pre-checked for NULL
3213 jvmtiError
3214 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
3215   JvmtiRawMonitor* rmonitor = new JvmtiRawMonitor(name);
3216   NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
3217 
3218   *monitor_ptr = (jrawMonitorID)rmonitor;
3219 
3220   return JVMTI_ERROR_NONE;
3221 } /* end CreateRawMonitor */
3222 
3223 
3224 // rmonitor - pre-checked for validity
3225 jvmtiError
3226 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
3227   if (Threads::number_of_threads() == 0) {
3228     // Remove this  monitor from pending raw monitors list
3229     // if it has entered in onload or start phase.
3230     JvmtiPendingMonitors::destroy(rmonitor);
3231   } else {
3232     Thread* thread  = Thread::current();
3233     if (rmonitor->is_entered(thread)) {
3234       // The caller owns this monitor which we are about to destroy.
3235       // We exit the underlying synchronization object so that the
3236       // "delete monitor" call below can work without an assertion
3237       // failure on systems that don't like destroying synchronization
3238       // objects that are locked.
3239       int r;
3240       intptr_t recursion = rmonitor->recursions();
3241       for (intptr_t i=0; i <= recursion; i++) {
3242         r = rmonitor->raw_exit(thread);
3243         assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
3244         if (r != ObjectMonitor::OM_OK) {  // robustness
3245           return JVMTI_ERROR_INTERNAL;
3246         }
3247       }
3248     }
3249     if (rmonitor->owner() != NULL) {
3250       // The caller is trying to destroy a monitor that is locked by
3251       // someone else. While this is not forbidden by the JVMTI
3252       // spec, it will cause an assertion failure on systems that don't
3253       // like destroying synchronization objects that are locked.
3254       // We indicate a problem with the error return (and leak the
3255       // monitor's memory).
3256       return JVMTI_ERROR_NOT_MONITOR_OWNER;
3257     }
3258   }
3259 
3260   delete rmonitor;
3261 
3262   return JVMTI_ERROR_NONE;
3263 } /* end DestroyRawMonitor */
3264 
3265 
3266 // rmonitor - pre-checked for validity
3267 jvmtiError
3268 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
3269   if (Threads::number_of_threads() == 0) {
3270     // No JavaThreads exist so ObjectMonitor enter cannot be
3271     // used, add this raw monitor to the pending list.
3272     // The pending monitors will be actually entered when
3273     // the VM is setup.
3274     // See transition_pending_raw_monitors in create_vm()
3275     // in thread.cpp.
3276     JvmtiPendingMonitors::enter(rmonitor);
3277   } else {
3278     int r = 0;
3279     Thread* thread = Thread::current();
3280 
3281     if (thread->is_Java_thread()) {
3282       JavaThread* current_thread = (JavaThread*)thread;
3283 
3284 #ifdef PROPER_TRANSITIONS
3285       // Not really unknown but ThreadInVMfromNative does more than we want
3286       ThreadInVMfromUnknown __tiv;
3287       {
3288         ThreadBlockInVM __tbivm(current_thread);
3289         r = rmonitor->raw_enter(current_thread);
3290       }
3291 #else
3292       /* Transition to thread_blocked without entering vm state          */
3293       /* This is really evil. Normally you can't undo _thread_blocked    */
3294       /* transitions like this because it would cause us to miss a       */
3295       /* safepoint but since the thread was already in _thread_in_native */
3296       /* the thread is not leaving a safepoint safe state and it will    */
3297       /* block when it tries to return from native. We can't safepoint   */
3298       /* block in here because we could deadlock the vmthread. Blech.    */
3299 
3300       JavaThreadState state = current_thread->thread_state();
3301       assert(state == _thread_in_native, "Must be _thread_in_native");
3302       // frame should already be walkable since we are in native
3303       assert(!current_thread->has_last_Java_frame() ||
3304              current_thread->frame_anchor()->walkable(), "Must be walkable");
3305       current_thread->set_thread_state(_thread_blocked);
3306 
3307       r = rmonitor->raw_enter(current_thread);
3308       // restore state, still at a safepoint safe state
3309       current_thread->set_thread_state(state);
3310 
3311 #endif /* PROPER_TRANSITIONS */
3312       assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked");
3313     } else {
3314       if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3315         r = rmonitor->raw_enter(thread);
3316       } else {
3317         ShouldNotReachHere();
3318       }
3319     }
3320 
3321     if (r != ObjectMonitor::OM_OK) {  // robustness
3322       return JVMTI_ERROR_INTERNAL;
3323     }
3324   }
3325   return JVMTI_ERROR_NONE;
3326 } /* end RawMonitorEnter */
3327 
3328 
3329 // rmonitor - pre-checked for validity
3330 jvmtiError
3331 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3332   jvmtiError err = JVMTI_ERROR_NONE;
3333 
3334   if (Threads::number_of_threads() == 0) {
3335     // No JavaThreads exist so just remove this monitor from the pending list.
3336     // Bool value from exit is false if rmonitor is not in the list.
3337     if (!JvmtiPendingMonitors::exit(rmonitor)) {
3338       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3339     }
3340   } else {
3341     int r = 0;
3342     Thread* thread = Thread::current();
3343 
3344     if (thread->is_Java_thread()) {
3345       JavaThread* current_thread = (JavaThread*)thread;
3346 #ifdef PROPER_TRANSITIONS
3347       // Not really unknown but ThreadInVMfromNative does more than we want
3348       ThreadInVMfromUnknown __tiv;
3349 #endif /* PROPER_TRANSITIONS */
3350       r = rmonitor->raw_exit(current_thread);
3351     } else {
3352       if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3353         r = rmonitor->raw_exit(thread);
3354       } else {
3355         ShouldNotReachHere();
3356       }
3357     }
3358 
3359     if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3360       err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3361     } else {
3362       assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
3363       if (r != ObjectMonitor::OM_OK) {  // robustness
3364         err = JVMTI_ERROR_INTERNAL;
3365       }
3366     }
3367   }
3368   return err;
3369 } /* end RawMonitorExit */
3370 
3371 
3372 // rmonitor - pre-checked for validity
3373 jvmtiError
3374 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3375   int r = 0;
3376   Thread* thread = Thread::current();
3377 
3378   if (thread->is_Java_thread()) {
3379     JavaThread* current_thread = (JavaThread*)thread;
3380 #ifdef PROPER_TRANSITIONS
3381     // Not really unknown but ThreadInVMfromNative does more than we want
3382     ThreadInVMfromUnknown __tiv;
3383     {
3384       ThreadBlockInVM __tbivm(current_thread);
3385       r = rmonitor->raw_wait(millis, true, current_thread);
3386     }
3387 #else
3388     /* Transition to thread_blocked without entering vm state          */
3389     /* This is really evil. Normally you can't undo _thread_blocked    */
3390     /* transitions like this because it would cause us to miss a       */
3391     /* safepoint but since the thread was already in _thread_in_native */
3392     /* the thread is not leaving a safepoint safe state and it will    */
3393     /* block when it tries to return from native. We can't safepoint   */
3394     /* block in here because we could deadlock the vmthread. Blech.    */
3395 
3396     JavaThreadState state = current_thread->thread_state();
3397     assert(state == _thread_in_native, "Must be _thread_in_native");
3398     // frame should already be walkable since we are in native
3399     assert(!current_thread->has_last_Java_frame() ||
3400            current_thread->frame_anchor()->walkable(), "Must be walkable");
3401     current_thread->set_thread_state(_thread_blocked);
3402 
3403     r = rmonitor->raw_wait(millis, true, current_thread);
3404     // restore state, still at a safepoint safe state
3405     current_thread->set_thread_state(state);
3406 
3407 #endif /* PROPER_TRANSITIONS */
3408   } else {
3409     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3410       r = rmonitor->raw_wait(millis, true, thread);
3411     } else {
3412       ShouldNotReachHere();
3413     }
3414   }
3415 
3416   switch (r) {
3417   case ObjectMonitor::OM_INTERRUPTED:
3418     return JVMTI_ERROR_INTERRUPT;
3419   case ObjectMonitor::OM_ILLEGAL_MONITOR_STATE:
3420     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3421   }
3422   assert(r == ObjectMonitor::OM_OK, "raw_wait should have worked");
3423   if (r != ObjectMonitor::OM_OK) {  // robustness
3424     return JVMTI_ERROR_INTERNAL;
3425   }
3426 
3427   return JVMTI_ERROR_NONE;
3428 } /* end RawMonitorWait */
3429 
3430 
3431 // rmonitor - pre-checked for validity
3432 jvmtiError
3433 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3434   int r = 0;
3435   Thread* thread = Thread::current();
3436 
3437   if (thread->is_Java_thread()) {
3438     JavaThread* current_thread = (JavaThread*)thread;
3439     // Not really unknown but ThreadInVMfromNative does more than we want
3440     ThreadInVMfromUnknown __tiv;
3441     r = rmonitor->raw_notify(current_thread);
3442   } else {
3443     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3444       r = rmonitor->raw_notify(thread);
3445     } else {
3446       ShouldNotReachHere();
3447     }
3448   }
3449 
3450   if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3451     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3452   }
3453   assert(r == ObjectMonitor::OM_OK, "raw_notify should have worked");
3454   if (r != ObjectMonitor::OM_OK) {  // robustness
3455     return JVMTI_ERROR_INTERNAL;
3456   }
3457 
3458   return JVMTI_ERROR_NONE;
3459 } /* end RawMonitorNotify */
3460 
3461 
3462 // rmonitor - pre-checked for validity
3463 jvmtiError
3464 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3465   int r = 0;
3466   Thread* thread = Thread::current();
3467 
3468   if (thread->is_Java_thread()) {
3469     JavaThread* current_thread = (JavaThread*)thread;
3470     ThreadInVMfromUnknown __tiv;
3471     r = rmonitor->raw_notifyAll(current_thread);
3472   } else {
3473     if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3474       r = rmonitor->raw_notifyAll(thread);
3475     } else {
3476       ShouldNotReachHere();
3477     }
3478   }
3479 
3480   if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3481     return JVMTI_ERROR_NOT_MONITOR_OWNER;
3482   }
3483   assert(r == ObjectMonitor::OM_OK, "raw_notifyAll should have worked");
3484   if (r != ObjectMonitor::OM_OK) {  // robustness
3485     return JVMTI_ERROR_INTERNAL;
3486   }
3487 
3488   return JVMTI_ERROR_NONE;
3489 } /* end RawMonitorNotifyAll */
3490 
3491 
3492   //
3493   // JNI Function Interception functions
3494   //
3495 
3496 
3497 // function_table - pre-checked for NULL
3498 jvmtiError
3499 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3500   // Copy jni function table at safepoint.
3501   VM_JNIFunctionTableCopier copier(function_table);
3502   VMThread::execute(&copier);
3503 
3504   return JVMTI_ERROR_NONE;
3505 } /* end SetJNIFunctionTable */
3506 
3507 
3508 // function_table - pre-checked for NULL
3509 jvmtiError
3510 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3511   *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3512   if (*function_table == NULL)
3513     return JVMTI_ERROR_OUT_OF_MEMORY;
3514   memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3515   return JVMTI_ERROR_NONE;
3516 } /* end GetJNIFunctionTable */
3517 
3518 
3519   //
3520   // Event Management functions
3521   //
3522 
3523 jvmtiError
3524 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3525   // can only generate two event types
3526   if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3527       event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3528     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3529   }
3530 
3531   // for compiled_method_load events we must check that the environment
3532   // has the can_generate_compiled_method_load_events capability.
3533   if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3534     if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3535       return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3536     }
3537     return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3538   } else {
3539     return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3540   }
3541 
3542 } /* end GenerateEvents */
3543 
3544 
3545   //
3546   // Extension Mechanism functions
3547   //
3548 
3549 // extension_count_ptr - pre-checked for NULL
3550 // extensions - pre-checked for NULL
3551 jvmtiError
3552 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3553   return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3554 } /* end GetExtensionFunctions */
3555 
3556 
3557 // extension_count_ptr - pre-checked for NULL
3558 // extensions - pre-checked for NULL
3559 jvmtiError
3560 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3561   return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3562 } /* end GetExtensionEvents */
3563 
3564 
3565 // callback - NULL is a valid value, must be checked
3566 jvmtiError
3567 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3568   return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3569 } /* end SetExtensionEventCallback */
3570 
3571   //
3572   // Timers functions
3573   //
3574 
3575 // info_ptr - pre-checked for NULL
3576 jvmtiError
3577 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3578   os::current_thread_cpu_time_info(info_ptr);
3579   return JVMTI_ERROR_NONE;
3580 } /* end GetCurrentThreadCpuTimerInfo */
3581 
3582 
3583 // nanos_ptr - pre-checked for NULL
3584 jvmtiError
3585 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3586   *nanos_ptr = os::current_thread_cpu_time();
3587   return JVMTI_ERROR_NONE;
3588 } /* end GetCurrentThreadCpuTime */
3589 
3590 
3591 // info_ptr - pre-checked for NULL
3592 jvmtiError
3593 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3594   os::thread_cpu_time_info(info_ptr);
3595   return JVMTI_ERROR_NONE;
3596 } /* end GetThreadCpuTimerInfo */
3597 
3598 
3599 // Threads_lock NOT held, java_thread not protected by lock
3600 // java_thread - pre-checked
3601 // nanos_ptr - pre-checked for NULL
3602 jvmtiError
3603 JvmtiEnv::GetThreadCpuTime(JavaThread* java_thread, jlong* nanos_ptr) {
3604   *nanos_ptr = os::thread_cpu_time(java_thread);
3605   return JVMTI_ERROR_NONE;
3606 } /* end GetThreadCpuTime */
3607 
3608 
3609 // info_ptr - pre-checked for NULL
3610 jvmtiError
3611 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3612   os::javaTimeNanos_info(info_ptr);
3613   return JVMTI_ERROR_NONE;
3614 } /* end GetTimerInfo */
3615 
3616 
3617 // nanos_ptr - pre-checked for NULL
3618 jvmtiError
3619 JvmtiEnv::GetTime(jlong* nanos_ptr) {
3620   *nanos_ptr = os::javaTimeNanos();
3621   return JVMTI_ERROR_NONE;
3622 } /* end GetTime */
3623 
3624 
3625 // processor_count_ptr - pre-checked for NULL
3626 jvmtiError
3627 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3628   *processor_count_ptr = os::active_processor_count();
3629   return JVMTI_ERROR_NONE;
3630 } /* end GetAvailableProcessors */
3631 
3632   //
3633   // System Properties functions
3634   //
3635 
3636 // count_ptr - pre-checked for NULL
3637 // property_ptr - pre-checked for NULL
3638 jvmtiError
3639 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3640   jvmtiError err = JVMTI_ERROR_NONE;
3641 
3642   // Get the number of readable properties.
3643   *count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties());
3644 
3645   // Allocate memory to hold the exact number of readable properties.
3646   err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3647   if (err != JVMTI_ERROR_NONE) {
3648     return err;
3649   }
3650   int readable_count = 0;
3651   // Loop through the system properties until all the readable properties are found.
3652   for (SystemProperty* p = Arguments::system_properties(); p != NULL && readable_count < *count_ptr; p = p->next()) {
3653     if (p->is_readable()) {
3654       const char *key = p->key();
3655       char **tmp_value = *property_ptr+readable_count;
3656       readable_count++;
3657       err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3658       if (err == JVMTI_ERROR_NONE) {
3659         strcpy(*tmp_value, key);
3660       } else {
3661         // clean up previously allocated memory.
3662         for (int j=0; j<readable_count; j++) {
3663           Deallocate((unsigned char*)*property_ptr+j);
3664         }
3665         Deallocate((unsigned char*)property_ptr);
3666         break;
3667       }
3668     }
3669   }
3670   assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count");
3671   return err;
3672 } /* end GetSystemProperties */
3673 
3674 
3675 // property - pre-checked for NULL
3676 // value_ptr - pre-checked for NULL
3677 jvmtiError
3678 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3679   jvmtiError err = JVMTI_ERROR_NONE;
3680   const char *value;
3681 
3682   // Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist.
3683   value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property);
3684   if (value == NULL) {
3685     err =  JVMTI_ERROR_NOT_AVAILABLE;
3686   } else {
3687     err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3688     if (err == JVMTI_ERROR_NONE) {
3689       strcpy(*value_ptr, value);
3690     }
3691   }
3692   return err;
3693 } /* end GetSystemProperty */
3694 
3695 
3696 // property - pre-checked for NULL
3697 // value - NULL is a valid value, must be checked
3698 jvmtiError
3699 JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) {
3700   jvmtiError err =JVMTI_ERROR_NOT_AVAILABLE;
3701 
3702   for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
3703     if (strcmp(property, p->key()) == 0) {
3704       if (p->set_writeable_value(value_ptr)) {
3705         err =  JVMTI_ERROR_NONE;
3706       }
3707     }
3708   }
3709   return err;
3710 } /* end SetSystemProperty */
--- EOF ---