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