src/share/vm/prims/jvmtiEnvBase.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 4673
5ee250974db9
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/systemDictionary.hpp"
aoqi@0 27 #include "jvmtifiles/jvmtiEnv.hpp"
aoqi@0 28 #include "oops/objArrayKlass.hpp"
aoqi@0 29 #include "oops/objArrayOop.hpp"
aoqi@0 30 #include "prims/jvmtiEnvBase.hpp"
aoqi@0 31 #include "prims/jvmtiEventController.inline.hpp"
aoqi@0 32 #include "prims/jvmtiExtensions.hpp"
aoqi@0 33 #include "prims/jvmtiImpl.hpp"
aoqi@0 34 #include "prims/jvmtiManageCapabilities.hpp"
aoqi@0 35 #include "prims/jvmtiTagMap.hpp"
aoqi@0 36 #include "prims/jvmtiThreadState.inline.hpp"
aoqi@0 37 #include "runtime/biasedLocking.hpp"
aoqi@0 38 #include "runtime/deoptimization.hpp"
aoqi@0 39 #include "runtime/interfaceSupport.hpp"
aoqi@0 40 #include "runtime/jfieldIDWorkaround.hpp"
aoqi@0 41 #include "runtime/objectMonitor.hpp"
aoqi@0 42 #include "runtime/objectMonitor.inline.hpp"
aoqi@0 43 #include "runtime/signature.hpp"
aoqi@0 44 #include "runtime/vframe.hpp"
aoqi@0 45 #include "runtime/vframe_hp.hpp"
aoqi@0 46 #include "runtime/vmThread.hpp"
aoqi@0 47 #include "runtime/vm_operations.hpp"
aoqi@0 48
aoqi@0 49 ///////////////////////////////////////////////////////////////
aoqi@0 50 //
aoqi@0 51 // JvmtiEnvBase
aoqi@0 52 //
aoqi@0 53
aoqi@0 54 JvmtiEnvBase* JvmtiEnvBase::_head_environment = NULL;
aoqi@0 55
aoqi@0 56 bool JvmtiEnvBase::_globally_initialized = false;
aoqi@0 57 volatile bool JvmtiEnvBase::_needs_clean_up = false;
aoqi@0 58
aoqi@0 59 jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL;
aoqi@0 60
aoqi@0 61 volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0;
aoqi@0 62
aoqi@0 63 extern jvmtiInterface_1_ jvmti_Interface;
aoqi@0 64 extern jvmtiInterface_1_ jvmtiTrace_Interface;
aoqi@0 65
aoqi@0 66
aoqi@0 67 // perform initializations that must occur before any JVMTI environments
aoqi@0 68 // are released but which should only be initialized once (no matter
aoqi@0 69 // how many environments are created).
aoqi@0 70 void
aoqi@0 71 JvmtiEnvBase::globally_initialize() {
aoqi@0 72 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
aoqi@0 73 assert(_globally_initialized == false, "bad call");
aoqi@0 74
aoqi@0 75 JvmtiManageCapabilities::initialize();
aoqi@0 76
aoqi@0 77 // register extension functions and events
aoqi@0 78 JvmtiExtensions::register_extensions();
aoqi@0 79
aoqi@0 80 #ifdef JVMTI_TRACE
aoqi@0 81 JvmtiTrace::initialize();
aoqi@0 82 #endif
aoqi@0 83
aoqi@0 84 _globally_initialized = true;
aoqi@0 85 }
aoqi@0 86
aoqi@0 87
aoqi@0 88 void
aoqi@0 89 JvmtiEnvBase::initialize() {
aoqi@0 90 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
aoqi@0 91
aoqi@0 92 // Add this environment to the end of the environment list (order is important)
aoqi@0 93 {
aoqi@0 94 // This block of code must not contain any safepoints, as list deallocation
aoqi@0 95 // (which occurs at a safepoint) cannot occur simultaneously with this list
aoqi@0 96 // addition. Note: No_Safepoint_Verifier cannot, currently, be used before
aoqi@0 97 // threads exist.
aoqi@0 98 JvmtiEnvIterator it;
aoqi@0 99 JvmtiEnvBase *previous_env = NULL;
aoqi@0 100 for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
aoqi@0 101 previous_env = env;
aoqi@0 102 }
aoqi@0 103 if (previous_env == NULL) {
aoqi@0 104 _head_environment = this;
aoqi@0 105 } else {
aoqi@0 106 previous_env->set_next_environment(this);
aoqi@0 107 }
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 if (_globally_initialized == false) {
aoqi@0 111 globally_initialize();
aoqi@0 112 }
aoqi@0 113 }
aoqi@0 114
aoqi@0 115
aoqi@0 116 bool
aoqi@0 117 JvmtiEnvBase::is_valid() {
aoqi@0 118 jint value = 0;
aoqi@0 119
aoqi@0 120 // This object might not be a JvmtiEnvBase so we can't assume
aoqi@0 121 // the _magic field is properly aligned. Get the value in a safe
aoqi@0 122 // way and then check against JVMTI_MAGIC.
aoqi@0 123
aoqi@0 124 switch (sizeof(_magic)) {
aoqi@0 125 case 2:
aoqi@0 126 value = Bytes::get_native_u2((address)&_magic);
aoqi@0 127 break;
aoqi@0 128
aoqi@0 129 case 4:
aoqi@0 130 value = Bytes::get_native_u4((address)&_magic);
aoqi@0 131 break;
aoqi@0 132
aoqi@0 133 case 8:
aoqi@0 134 value = Bytes::get_native_u8((address)&_magic);
aoqi@0 135 break;
aoqi@0 136
aoqi@0 137 default:
aoqi@0 138 guarantee(false, "_magic field is an unexpected size");
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 return value == JVMTI_MAGIC;
aoqi@0 142 }
aoqi@0 143
aoqi@0 144
aoqi@0 145 bool
aoqi@0 146 JvmtiEnvBase::use_version_1_0_semantics() {
aoqi@0 147 int major, minor, micro;
aoqi@0 148
aoqi@0 149 JvmtiExport::decode_version_values(_version, &major, &minor, &micro);
aoqi@0 150 return major == 1 && minor == 0; // micro version doesn't matter here
aoqi@0 151 }
aoqi@0 152
aoqi@0 153
aoqi@0 154 bool
aoqi@0 155 JvmtiEnvBase::use_version_1_1_semantics() {
aoqi@0 156 int major, minor, micro;
aoqi@0 157
aoqi@0 158 JvmtiExport::decode_version_values(_version, &major, &minor, &micro);
aoqi@0 159 return major == 1 && minor == 1; // micro version doesn't matter here
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 bool
aoqi@0 163 JvmtiEnvBase::use_version_1_2_semantics() {
aoqi@0 164 int major, minor, micro;
aoqi@0 165
aoqi@0 166 JvmtiExport::decode_version_values(_version, &major, &minor, &micro);
aoqi@0 167 return major == 1 && minor == 2; // micro version doesn't matter here
aoqi@0 168 }
aoqi@0 169
aoqi@0 170
aoqi@0 171 JvmtiEnvBase::JvmtiEnvBase(jint version) : _env_event_enable() {
aoqi@0 172 _version = version;
aoqi@0 173 _env_local_storage = NULL;
aoqi@0 174 _tag_map = NULL;
aoqi@0 175 _native_method_prefix_count = 0;
aoqi@0 176 _native_method_prefixes = NULL;
aoqi@0 177 _next = NULL;
aoqi@0 178 _class_file_load_hook_ever_enabled = false;
aoqi@0 179
aoqi@0 180 // Moot since ClassFileLoadHook not yet enabled.
aoqi@0 181 // But "true" will give a more predictable ClassFileLoadHook behavior
aoqi@0 182 // for environment creation during ClassFileLoadHook.
aoqi@0 183 _is_retransformable = true;
aoqi@0 184
aoqi@0 185 // all callbacks initially NULL
aoqi@0 186 memset(&_event_callbacks,0,sizeof(jvmtiEventCallbacks));
aoqi@0 187
aoqi@0 188 // all capabilities initially off
aoqi@0 189 memset(&_current_capabilities, 0, sizeof(_current_capabilities));
aoqi@0 190
aoqi@0 191 // all prohibited capabilities initially off
aoqi@0 192 memset(&_prohibited_capabilities, 0, sizeof(_prohibited_capabilities));
aoqi@0 193
aoqi@0 194 _magic = JVMTI_MAGIC;
aoqi@0 195
aoqi@0 196 JvmtiEventController::env_initialize((JvmtiEnv*)this);
aoqi@0 197
aoqi@0 198 #ifdef JVMTI_TRACE
aoqi@0 199 _jvmti_external.functions = TraceJVMTI != NULL ? &jvmtiTrace_Interface : &jvmti_Interface;
aoqi@0 200 #else
aoqi@0 201 _jvmti_external.functions = &jvmti_Interface;
aoqi@0 202 #endif
aoqi@0 203 }
aoqi@0 204
aoqi@0 205
aoqi@0 206 void
aoqi@0 207 JvmtiEnvBase::dispose() {
aoqi@0 208
aoqi@0 209 #ifdef JVMTI_TRACE
aoqi@0 210 JvmtiTrace::shutdown();
aoqi@0 211 #endif
aoqi@0 212
aoqi@0 213 // Dispose of event info and let the event controller call us back
aoqi@0 214 // in a locked state (env_dispose, below)
aoqi@0 215 JvmtiEventController::env_dispose(this);
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 void
aoqi@0 219 JvmtiEnvBase::env_dispose() {
aoqi@0 220 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
aoqi@0 221
aoqi@0 222 // We have been entered with all events disabled on this environment.
aoqi@0 223 // A race to re-enable events (by setting callbacks) is prevented by
aoqi@0 224 // checking for a valid environment when setting callbacks (while
aoqi@0 225 // holding the JvmtiThreadState_lock).
aoqi@0 226
aoqi@0 227 // Mark as invalid.
aoqi@0 228 _magic = DISPOSED_MAGIC;
aoqi@0 229
aoqi@0 230 // Relinquish all capabilities.
aoqi@0 231 jvmtiCapabilities *caps = get_capabilities();
aoqi@0 232 JvmtiManageCapabilities::relinquish_capabilities(caps, caps, caps);
aoqi@0 233
aoqi@0 234 // Same situation as with events (see above)
aoqi@0 235 set_native_method_prefixes(0, NULL);
aoqi@0 236
aoqi@0 237 JvmtiTagMap* tag_map_to_deallocate = _tag_map;
aoqi@0 238 set_tag_map(NULL);
aoqi@0 239 // A tag map can be big, deallocate it now
aoqi@0 240 if (tag_map_to_deallocate != NULL) {
aoqi@0 241 delete tag_map_to_deallocate;
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 _needs_clean_up = true;
aoqi@0 245 }
aoqi@0 246
aoqi@0 247
aoqi@0 248 JvmtiEnvBase::~JvmtiEnvBase() {
aoqi@0 249 assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
aoqi@0 250
aoqi@0 251 // There is a small window of time during which the tag map of a
aoqi@0 252 // disposed environment could have been reallocated.
aoqi@0 253 // Make sure it is gone.
aoqi@0 254 JvmtiTagMap* tag_map_to_deallocate = _tag_map;
aoqi@0 255 set_tag_map(NULL);
aoqi@0 256 // A tag map can be big, deallocate it now
aoqi@0 257 if (tag_map_to_deallocate != NULL) {
aoqi@0 258 delete tag_map_to_deallocate;
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 _magic = BAD_MAGIC;
aoqi@0 262 }
aoqi@0 263
aoqi@0 264
aoqi@0 265 void
aoqi@0 266 JvmtiEnvBase::periodic_clean_up() {
aoqi@0 267 assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
aoqi@0 268
aoqi@0 269 // JvmtiEnvBase reference is saved in JvmtiEnvThreadState. So
aoqi@0 270 // clean up JvmtiThreadState before deleting JvmtiEnv pointer.
aoqi@0 271 JvmtiThreadState::periodic_clean_up();
aoqi@0 272
aoqi@0 273 // Unlink all invalid environments from the list of environments
aoqi@0 274 // and deallocate them
aoqi@0 275 JvmtiEnvIterator it;
aoqi@0 276 JvmtiEnvBase* previous_env = NULL;
aoqi@0 277 JvmtiEnvBase* env = it.first();
aoqi@0 278 while (env != NULL) {
aoqi@0 279 if (env->is_valid()) {
aoqi@0 280 previous_env = env;
aoqi@0 281 env = it.next(env);
aoqi@0 282 } else {
aoqi@0 283 // This one isn't valid, remove it from the list and deallocate it
aoqi@0 284 JvmtiEnvBase* defunct_env = env;
aoqi@0 285 env = it.next(env);
aoqi@0 286 if (previous_env == NULL) {
aoqi@0 287 _head_environment = env;
aoqi@0 288 } else {
aoqi@0 289 previous_env->set_next_environment(env);
aoqi@0 290 }
aoqi@0 291 delete defunct_env;
aoqi@0 292 }
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 }
aoqi@0 296
aoqi@0 297
aoqi@0 298 void
aoqi@0 299 JvmtiEnvBase::check_for_periodic_clean_up() {
aoqi@0 300 assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
aoqi@0 301
aoqi@0 302 class ThreadInsideIterationClosure: public ThreadClosure {
aoqi@0 303 private:
aoqi@0 304 bool _inside;
aoqi@0 305 public:
aoqi@0 306 ThreadInsideIterationClosure() : _inside(false) {};
aoqi@0 307
aoqi@0 308 void do_thread(Thread* thread) {
aoqi@0 309 _inside |= thread->is_inside_jvmti_env_iteration();
aoqi@0 310 }
aoqi@0 311
aoqi@0 312 bool is_inside_jvmti_env_iteration() {
aoqi@0 313 return _inside;
aoqi@0 314 }
aoqi@0 315 };
aoqi@0 316
aoqi@0 317 if (_needs_clean_up) {
aoqi@0 318 // Check if we are currently iterating environment,
aoqi@0 319 // deallocation should not occur if we are
aoqi@0 320 ThreadInsideIterationClosure tiic;
aoqi@0 321 Threads::threads_do(&tiic);
aoqi@0 322 if (!tiic.is_inside_jvmti_env_iteration() &&
aoqi@0 323 !is_inside_dying_thread_env_iteration()) {
aoqi@0 324 _needs_clean_up = false;
aoqi@0 325 JvmtiEnvBase::periodic_clean_up();
aoqi@0 326 }
aoqi@0 327 }
aoqi@0 328 }
aoqi@0 329
aoqi@0 330
aoqi@0 331 void
aoqi@0 332 JvmtiEnvBase::record_first_time_class_file_load_hook_enabled() {
aoqi@0 333 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
aoqi@0 334 "sanity check");
aoqi@0 335
aoqi@0 336 if (!_class_file_load_hook_ever_enabled) {
aoqi@0 337 _class_file_load_hook_ever_enabled = true;
aoqi@0 338
aoqi@0 339 if (get_capabilities()->can_retransform_classes) {
aoqi@0 340 _is_retransformable = true;
aoqi@0 341 } else {
aoqi@0 342 _is_retransformable = false;
aoqi@0 343
aoqi@0 344 // cannot add retransform capability after ClassFileLoadHook has been enabled
aoqi@0 345 get_prohibited_capabilities()->can_retransform_classes = 1;
aoqi@0 346 }
aoqi@0 347 }
aoqi@0 348 }
aoqi@0 349
aoqi@0 350
aoqi@0 351 void
aoqi@0 352 JvmtiEnvBase::record_class_file_load_hook_enabled() {
aoqi@0 353 if (!_class_file_load_hook_ever_enabled) {
aoqi@0 354 if (Threads::number_of_threads() == 0) {
aoqi@0 355 record_first_time_class_file_load_hook_enabled();
aoqi@0 356 } else {
aoqi@0 357 MutexLocker mu(JvmtiThreadState_lock);
aoqi@0 358 record_first_time_class_file_load_hook_enabled();
aoqi@0 359 }
aoqi@0 360 }
aoqi@0 361 }
aoqi@0 362
aoqi@0 363
aoqi@0 364 jvmtiError
aoqi@0 365 JvmtiEnvBase::set_native_method_prefixes(jint prefix_count, char** prefixes) {
aoqi@0 366 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
aoqi@0 367 "sanity check");
aoqi@0 368
aoqi@0 369 int old_prefix_count = get_native_method_prefix_count();
aoqi@0 370 char **old_prefixes = get_native_method_prefixes();
aoqi@0 371
aoqi@0 372 // allocate and install the new prefixex
aoqi@0 373 if (prefix_count == 0 || !is_valid()) {
aoqi@0 374 _native_method_prefix_count = 0;
aoqi@0 375 _native_method_prefixes = NULL;
aoqi@0 376 } else {
aoqi@0 377 // there are prefixes, allocate an array to hold them, and fill it
aoqi@0 378 char** new_prefixes = (char**)os::malloc((prefix_count) * sizeof(char*), mtInternal);
aoqi@0 379 if (new_prefixes == NULL) {
aoqi@0 380 return JVMTI_ERROR_OUT_OF_MEMORY;
aoqi@0 381 }
aoqi@0 382 for (int i = 0; i < prefix_count; i++) {
aoqi@0 383 char* prefix = prefixes[i];
aoqi@0 384 if (prefix == NULL) {
aoqi@0 385 for (int j = 0; j < (i-1); j++) {
aoqi@0 386 os::free(new_prefixes[j]);
aoqi@0 387 }
aoqi@0 388 os::free(new_prefixes);
aoqi@0 389 return JVMTI_ERROR_NULL_POINTER;
aoqi@0 390 }
aoqi@0 391 prefix = os::strdup(prefixes[i]);
aoqi@0 392 if (prefix == NULL) {
aoqi@0 393 for (int j = 0; j < (i-1); j++) {
aoqi@0 394 os::free(new_prefixes[j]);
aoqi@0 395 }
aoqi@0 396 os::free(new_prefixes);
aoqi@0 397 return JVMTI_ERROR_OUT_OF_MEMORY;
aoqi@0 398 }
aoqi@0 399 new_prefixes[i] = prefix;
aoqi@0 400 }
aoqi@0 401 _native_method_prefix_count = prefix_count;
aoqi@0 402 _native_method_prefixes = new_prefixes;
aoqi@0 403 }
aoqi@0 404
aoqi@0 405 // now that we know the new prefixes have been successfully installed we can
aoqi@0 406 // safely remove the old ones
aoqi@0 407 if (old_prefix_count != 0) {
aoqi@0 408 for (int i = 0; i < old_prefix_count; i++) {
aoqi@0 409 os::free(old_prefixes[i]);
aoqi@0 410 }
aoqi@0 411 os::free(old_prefixes);
aoqi@0 412 }
aoqi@0 413
aoqi@0 414 return JVMTI_ERROR_NONE;
aoqi@0 415 }
aoqi@0 416
aoqi@0 417
aoqi@0 418 // Collect all the prefixes which have been set in any JVM TI environments
aoqi@0 419 // by the SetNativeMethodPrefix(es) functions. Be sure to maintain the
aoqi@0 420 // order of environments and the order of prefixes within each environment.
aoqi@0 421 // Return in a resource allocated array.
aoqi@0 422 char**
aoqi@0 423 JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) {
aoqi@0 424 assert(Threads::number_of_threads() == 0 ||
aoqi@0 425 SafepointSynchronize::is_at_safepoint() ||
aoqi@0 426 JvmtiThreadState_lock->is_locked(),
aoqi@0 427 "sanity check");
aoqi@0 428
aoqi@0 429 int total_count = 0;
aoqi@0 430 GrowableArray<char*>* prefix_array =new GrowableArray<char*>(5);
aoqi@0 431
aoqi@0 432 JvmtiEnvIterator it;
aoqi@0 433 for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
aoqi@0 434 int prefix_count = env->get_native_method_prefix_count();
aoqi@0 435 char** prefixes = env->get_native_method_prefixes();
aoqi@0 436 for (int j = 0; j < prefix_count; j++) {
aoqi@0 437 // retrieve a prefix and so that it is safe against asynchronous changes
aoqi@0 438 // copy it into the resource area
aoqi@0 439 char* prefix = prefixes[j];
aoqi@0 440 char* prefix_copy = NEW_RESOURCE_ARRAY(char, strlen(prefix)+1);
aoqi@0 441 strcpy(prefix_copy, prefix);
aoqi@0 442 prefix_array->at_put_grow(total_count++, prefix_copy);
aoqi@0 443 }
aoqi@0 444 }
aoqi@0 445
aoqi@0 446 char** all_prefixes = NEW_RESOURCE_ARRAY(char*, total_count);
aoqi@0 447 char** p = all_prefixes;
aoqi@0 448 for (int i = 0; i < total_count; ++i) {
aoqi@0 449 *p++ = prefix_array->at(i);
aoqi@0 450 }
aoqi@0 451 *count_ptr = total_count;
aoqi@0 452 return all_prefixes;
aoqi@0 453 }
aoqi@0 454
aoqi@0 455 void
aoqi@0 456 JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks,
aoqi@0 457 jint size_of_callbacks) {
aoqi@0 458 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
aoqi@0 459
aoqi@0 460 size_t byte_cnt = sizeof(jvmtiEventCallbacks);
aoqi@0 461
aoqi@0 462 // clear in either case to be sure we got any gap between sizes
aoqi@0 463 memset(&_event_callbacks, 0, byte_cnt);
aoqi@0 464
aoqi@0 465 // Now that JvmtiThreadState_lock is held, prevent a possible race condition where events
aoqi@0 466 // are re-enabled by a call to set event callbacks where the DisposeEnvironment
aoqi@0 467 // occurs after the boiler-plate environment check and before the lock is acquired.
aoqi@0 468 if (callbacks != NULL && is_valid()) {
aoqi@0 469 if (size_of_callbacks < (jint)byte_cnt) {
aoqi@0 470 byte_cnt = size_of_callbacks;
aoqi@0 471 }
aoqi@0 472 memcpy(&_event_callbacks, callbacks, byte_cnt);
aoqi@0 473 }
aoqi@0 474 }
aoqi@0 475
aoqi@0 476 // Called from JVMTI entry points which perform stack walking. If the
aoqi@0 477 // associated JavaThread is the current thread, then wait_for_suspend
aoqi@0 478 // is not used. Otherwise, it determines if we should wait for the
aoqi@0 479 // "other" thread to complete external suspension. (NOTE: in future
aoqi@0 480 // releases the suspension mechanism should be reimplemented so this
aoqi@0 481 // is not necessary.)
aoqi@0 482 //
aoqi@0 483 bool
aoqi@0 484 JvmtiEnvBase::is_thread_fully_suspended(JavaThread* thr, bool wait_for_suspend, uint32_t *bits) {
aoqi@0 485 // "other" threads require special handling
aoqi@0 486 if (thr != JavaThread::current()) {
aoqi@0 487 if (wait_for_suspend) {
aoqi@0 488 // We are allowed to wait for the external suspend to complete
aoqi@0 489 // so give the other thread a chance to get suspended.
aoqi@0 490 if (!thr->wait_for_ext_suspend_completion(SuspendRetryCount,
aoqi@0 491 SuspendRetryDelay, bits)) {
aoqi@0 492 // didn't make it so let the caller know
aoqi@0 493 return false;
aoqi@0 494 }
aoqi@0 495 }
aoqi@0 496 // We aren't allowed to wait for the external suspend to complete
aoqi@0 497 // so if the other thread isn't externally suspended we need to
aoqi@0 498 // let the caller know.
aoqi@0 499 else if (!thr->is_ext_suspend_completed_with_lock(bits)) {
aoqi@0 500 return false;
aoqi@0 501 }
aoqi@0 502 }
aoqi@0 503
aoqi@0 504 return true;
aoqi@0 505 }
aoqi@0 506
aoqi@0 507
aoqi@0 508 // In the fullness of time, all users of the method should instead
aoqi@0 509 // directly use allocate, besides being cleaner and faster, this will
aoqi@0 510 // mean much better out of memory handling
aoqi@0 511 unsigned char *
aoqi@0 512 JvmtiEnvBase::jvmtiMalloc(jlong size) {
aoqi@0 513 unsigned char* mem;
aoqi@0 514 jvmtiError result = allocate(size, &mem);
aoqi@0 515 assert(result == JVMTI_ERROR_NONE, "Allocate failed");
aoqi@0 516 return mem;
aoqi@0 517 }
aoqi@0 518
aoqi@0 519
aoqi@0 520 //
aoqi@0 521 // Threads
aoqi@0 522 //
aoqi@0 523
aoqi@0 524 jobject *
aoqi@0 525 JvmtiEnvBase::new_jobjectArray(int length, Handle *handles) {
aoqi@0 526 if (length == 0) {
aoqi@0 527 return NULL;
aoqi@0 528 }
aoqi@0 529
aoqi@0 530 jobject *objArray = (jobject *) jvmtiMalloc(sizeof(jobject) * length);
aoqi@0 531 NULL_CHECK(objArray, NULL);
aoqi@0 532
aoqi@0 533 for (int i=0; i<length; i++) {
aoqi@0 534 objArray[i] = jni_reference(handles[i]);
aoqi@0 535 }
aoqi@0 536 return objArray;
aoqi@0 537 }
aoqi@0 538
aoqi@0 539 jthread *
aoqi@0 540 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
aoqi@0 541 return (jthread *) new_jobjectArray(length,handles);
aoqi@0 542 }
aoqi@0 543
aoqi@0 544 jthreadGroup *
aoqi@0 545 JvmtiEnvBase::new_jthreadGroupArray(int length, Handle *handles) {
aoqi@0 546 return (jthreadGroup *) new_jobjectArray(length,handles);
aoqi@0 547 }
aoqi@0 548
aoqi@0 549
aoqi@0 550 JavaThread *
aoqi@0 551 JvmtiEnvBase::get_JavaThread(jthread jni_thread) {
aoqi@0 552 oop t = JNIHandles::resolve_external_guard(jni_thread);
aoqi@0 553 if (t == NULL || !t->is_a(SystemDictionary::Thread_klass())) {
aoqi@0 554 return NULL;
aoqi@0 555 }
aoqi@0 556 // The following returns NULL if the thread has not yet run or is in
aoqi@0 557 // process of exiting
aoqi@0 558 return java_lang_Thread::thread(t);
aoqi@0 559 }
aoqi@0 560
aoqi@0 561
aoqi@0 562 // return the vframe on the specified thread and depth, NULL if no such frame
aoqi@0 563 vframe*
aoqi@0 564 JvmtiEnvBase::vframeFor(JavaThread* java_thread, jint depth) {
aoqi@0 565 if (!java_thread->has_last_Java_frame()) {
aoqi@0 566 return NULL;
aoqi@0 567 }
aoqi@0 568 RegisterMap reg_map(java_thread);
aoqi@0 569 vframe *vf = java_thread->last_java_vframe(&reg_map);
aoqi@0 570 int d = 0;
aoqi@0 571 while ((vf != NULL) && (d < depth)) {
aoqi@0 572 vf = vf->java_sender();
aoqi@0 573 d++;
aoqi@0 574 }
aoqi@0 575 return vf;
aoqi@0 576 }
aoqi@0 577
aoqi@0 578
aoqi@0 579 //
aoqi@0 580 // utilities: JNI objects
aoqi@0 581 //
aoqi@0 582
aoqi@0 583
aoqi@0 584 jclass
aoqi@0 585 JvmtiEnvBase::get_jni_class_non_null(Klass* k) {
aoqi@0 586 assert(k != NULL, "k != NULL");
aoqi@0 587 return (jclass)jni_reference(k->java_mirror());
aoqi@0 588 }
aoqi@0 589
aoqi@0 590 //
aoqi@0 591 // Field Information
aoqi@0 592 //
aoqi@0 593
aoqi@0 594 bool
aoqi@0 595 JvmtiEnvBase::get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd) {
aoqi@0 596 if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) {
aoqi@0 597 return false;
aoqi@0 598 }
aoqi@0 599 bool found = false;
aoqi@0 600 if (jfieldIDWorkaround::is_static_jfieldID(field)) {
aoqi@0 601 JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field);
aoqi@0 602 found = id->find_local_field(fd);
aoqi@0 603 } else {
aoqi@0 604 // Non-static field. The fieldID is really the offset of the field within the object.
aoqi@0 605 int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field);
aoqi@0 606 found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, fd);
aoqi@0 607 }
aoqi@0 608 return found;
aoqi@0 609 }
aoqi@0 610
aoqi@0 611 //
aoqi@0 612 // Object Monitor Information
aoqi@0 613 //
aoqi@0 614
aoqi@0 615 //
aoqi@0 616 // Count the number of objects for a lightweight monitor. The hobj
aoqi@0 617 // parameter is object that owns the monitor so this routine will
aoqi@0 618 // count the number of times the same object was locked by frames
aoqi@0 619 // in java_thread.
aoqi@0 620 //
aoqi@0 621 jint
aoqi@0 622 JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
aoqi@0 623 jint ret = 0;
aoqi@0 624 if (!java_thread->has_last_Java_frame()) {
aoqi@0 625 return ret; // no Java frames so no monitors
aoqi@0 626 }
aoqi@0 627
aoqi@0 628 ResourceMark rm;
aoqi@0 629 HandleMark hm;
aoqi@0 630 RegisterMap reg_map(java_thread);
aoqi@0 631
aoqi@0 632 for(javaVFrame *jvf=java_thread->last_java_vframe(&reg_map); jvf != NULL;
aoqi@0 633 jvf = jvf->java_sender()) {
aoqi@0 634 GrowableArray<MonitorInfo*>* mons = jvf->monitors();
aoqi@0 635 if (!mons->is_empty()) {
aoqi@0 636 for (int i = 0; i < mons->length(); i++) {
aoqi@0 637 MonitorInfo *mi = mons->at(i);
aoqi@0 638 if (mi->owner_is_scalar_replaced()) continue;
aoqi@0 639
aoqi@0 640 // see if owner of the monitor is our object
aoqi@0 641 if (mi->owner() != NULL && mi->owner() == hobj()) {
aoqi@0 642 ret++;
aoqi@0 643 }
aoqi@0 644 }
aoqi@0 645 }
aoqi@0 646 }
aoqi@0 647 return ret;
aoqi@0 648 }
aoqi@0 649
aoqi@0 650
aoqi@0 651
aoqi@0 652 jvmtiError
aoqi@0 653 JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread, jobject *monitor_ptr) {
aoqi@0 654 #ifdef ASSERT
aoqi@0 655 uint32_t debug_bits = 0;
aoqi@0 656 #endif
aoqi@0 657 assert((SafepointSynchronize::is_at_safepoint() ||
aoqi@0 658 is_thread_fully_suspended(java_thread, false, &debug_bits)),
aoqi@0 659 "at safepoint or target thread is suspended");
aoqi@0 660 oop obj = NULL;
aoqi@0 661 ObjectMonitor *mon = java_thread->current_waiting_monitor();
aoqi@0 662 if (mon == NULL) {
aoqi@0 663 // thread is not doing an Object.wait() call
aoqi@0 664 mon = java_thread->current_pending_monitor();
aoqi@0 665 if (mon != NULL) {
aoqi@0 666 // The thread is trying to enter() or raw_enter() an ObjectMonitor.
aoqi@0 667 obj = (oop)mon->object();
aoqi@0 668 // If obj == NULL, then ObjectMonitor is raw which doesn't count
aoqi@0 669 // as contended for this API
aoqi@0 670 }
aoqi@0 671 // implied else: no contended ObjectMonitor
aoqi@0 672 } else {
aoqi@0 673 // thread is doing an Object.wait() call
aoqi@0 674 obj = (oop)mon->object();
aoqi@0 675 assert(obj != NULL, "Object.wait() should have an object");
aoqi@0 676 }
aoqi@0 677
aoqi@0 678 if (obj == NULL) {
aoqi@0 679 *monitor_ptr = NULL;
aoqi@0 680 } else {
aoqi@0 681 HandleMark hm;
aoqi@0 682 Handle hobj(obj);
aoqi@0 683 *monitor_ptr = jni_reference(calling_thread, hobj);
aoqi@0 684 }
aoqi@0 685 return JVMTI_ERROR_NONE;
aoqi@0 686 }
aoqi@0 687
aoqi@0 688
aoqi@0 689 jvmtiError
aoqi@0 690 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
aoqi@0 691 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
aoqi@0 692 jvmtiError err = JVMTI_ERROR_NONE;
aoqi@0 693 #ifdef ASSERT
aoqi@0 694 uint32_t debug_bits = 0;
aoqi@0 695 #endif
aoqi@0 696 assert((SafepointSynchronize::is_at_safepoint() ||
aoqi@0 697 is_thread_fully_suspended(java_thread, false, &debug_bits)),
aoqi@0 698 "at safepoint or target thread is suspended");
aoqi@0 699
aoqi@0 700 if (java_thread->has_last_Java_frame()) {
aoqi@0 701 ResourceMark rm;
aoqi@0 702 HandleMark hm;
aoqi@0 703 RegisterMap reg_map(java_thread);
aoqi@0 704
aoqi@0 705 int depth = 0;
aoqi@0 706 for (javaVFrame *jvf = java_thread->last_java_vframe(&reg_map); jvf != NULL;
aoqi@0 707 jvf = jvf->java_sender()) {
aoqi@0 708 if (depth++ < MaxJavaStackTraceDepth) { // check for stack too deep
aoqi@0 709 // add locked objects for this frame into list
aoqi@0 710 err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
aoqi@0 711 if (err != JVMTI_ERROR_NONE) {
aoqi@0 712 return err;
aoqi@0 713 }
aoqi@0 714 }
aoqi@0 715 }
aoqi@0 716 }
aoqi@0 717
aoqi@0 718 // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
aoqi@0 719 JvmtiMonitorClosure jmc(java_thread, calling_thread, owned_monitors_list, this);
aoqi@0 720 ObjectSynchronizer::monitors_iterate(&jmc);
aoqi@0 721 err = jmc.error();
aoqi@0 722
aoqi@0 723 return err;
aoqi@0 724 }
aoqi@0 725
aoqi@0 726 // Save JNI local handles for any objects that this frame owns.
aoqi@0 727 jvmtiError
aoqi@0 728 JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread,
aoqi@0 729 javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, int stack_depth) {
aoqi@0 730 jvmtiError err = JVMTI_ERROR_NONE;
aoqi@0 731 ResourceMark rm;
aoqi@0 732
aoqi@0 733 GrowableArray<MonitorInfo*>* mons = jvf->monitors();
aoqi@0 734 if (mons->is_empty()) {
aoqi@0 735 return err; // this javaVFrame holds no monitors
aoqi@0 736 }
aoqi@0 737
aoqi@0 738 HandleMark hm;
aoqi@0 739 oop wait_obj = NULL;
aoqi@0 740 {
aoqi@0 741 // save object of current wait() call (if any) for later comparison
aoqi@0 742 ObjectMonitor *mon = java_thread->current_waiting_monitor();
aoqi@0 743 if (mon != NULL) {
aoqi@0 744 wait_obj = (oop)mon->object();
aoqi@0 745 }
aoqi@0 746 }
aoqi@0 747 oop pending_obj = NULL;
aoqi@0 748 {
aoqi@0 749 // save object of current enter() call (if any) for later comparison
aoqi@0 750 ObjectMonitor *mon = java_thread->current_pending_monitor();
aoqi@0 751 if (mon != NULL) {
aoqi@0 752 pending_obj = (oop)mon->object();
aoqi@0 753 }
aoqi@0 754 }
aoqi@0 755
aoqi@0 756 for (int i = 0; i < mons->length(); i++) {
aoqi@0 757 MonitorInfo *mi = mons->at(i);
aoqi@0 758
aoqi@0 759 if (mi->owner_is_scalar_replaced()) continue;
aoqi@0 760
aoqi@0 761 oop obj = mi->owner();
aoqi@0 762 if (obj == NULL) {
aoqi@0 763 // this monitor doesn't have an owning object so skip it
aoqi@0 764 continue;
aoqi@0 765 }
aoqi@0 766
aoqi@0 767 if (wait_obj == obj) {
aoqi@0 768 // the thread is waiting on this monitor so it isn't really owned
aoqi@0 769 continue;
aoqi@0 770 }
aoqi@0 771
aoqi@0 772 if (pending_obj == obj) {
aoqi@0 773 // the thread is pending on this monitor so it isn't really owned
aoqi@0 774 continue;
aoqi@0 775 }
aoqi@0 776
aoqi@0 777 if (owned_monitors_list->length() > 0) {
aoqi@0 778 // Our list has at least one object on it so we have to check
aoqi@0 779 // for recursive object locking
aoqi@0 780 bool found = false;
aoqi@0 781 for (int j = 0; j < owned_monitors_list->length(); j++) {
aoqi@0 782 jobject jobj = ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(j))->monitor;
aoqi@0 783 oop check = JNIHandles::resolve(jobj);
aoqi@0 784 if (check == obj) {
aoqi@0 785 found = true; // we found the object
aoqi@0 786 break;
aoqi@0 787 }
aoqi@0 788 }
aoqi@0 789
aoqi@0 790 if (found) {
aoqi@0 791 // already have this object so don't include it
aoqi@0 792 continue;
aoqi@0 793 }
aoqi@0 794 }
aoqi@0 795
aoqi@0 796 // add the owning object to our list
aoqi@0 797 jvmtiMonitorStackDepthInfo *jmsdi;
aoqi@0 798 err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
aoqi@0 799 if (err != JVMTI_ERROR_NONE) {
aoqi@0 800 return err;
aoqi@0 801 }
aoqi@0 802 Handle hobj(obj);
aoqi@0 803 jmsdi->monitor = jni_reference(calling_thread, hobj);
aoqi@0 804 jmsdi->stack_depth = stack_depth;
aoqi@0 805 owned_monitors_list->append(jmsdi);
aoqi@0 806 }
aoqi@0 807
aoqi@0 808 return err;
aoqi@0 809 }
aoqi@0 810
aoqi@0 811 jvmtiError
aoqi@0 812 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
aoqi@0 813 jint start_depth, jint max_count,
aoqi@0 814 jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
aoqi@0 815 #ifdef ASSERT
aoqi@0 816 uint32_t debug_bits = 0;
aoqi@0 817 #endif
aoqi@0 818 assert((SafepointSynchronize::is_at_safepoint() ||
aoqi@0 819 is_thread_fully_suspended(java_thread, false, &debug_bits)),
aoqi@0 820 "at safepoint or target thread is suspended");
aoqi@0 821 int count = 0;
aoqi@0 822 if (java_thread->has_last_Java_frame()) {
aoqi@0 823 RegisterMap reg_map(java_thread);
aoqi@0 824 Thread* current_thread = Thread::current();
aoqi@0 825 ResourceMark rm(current_thread);
aoqi@0 826 javaVFrame *jvf = java_thread->last_java_vframe(&reg_map);
aoqi@0 827 HandleMark hm(current_thread);
aoqi@0 828 if (start_depth != 0) {
aoqi@0 829 if (start_depth > 0) {
aoqi@0 830 for (int j = 0; j < start_depth && jvf != NULL; j++) {
aoqi@0 831 jvf = jvf->java_sender();
aoqi@0 832 }
aoqi@0 833 if (jvf == NULL) {
aoqi@0 834 // start_depth is deeper than the stack depth
aoqi@0 835 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
aoqi@0 836 }
aoqi@0 837 } else { // start_depth < 0
aoqi@0 838 // we are referencing the starting depth based on the oldest
aoqi@0 839 // part of the stack.
aoqi@0 840 // optimize to limit the number of times that java_sender() is called
aoqi@0 841 javaVFrame *jvf_cursor = jvf;
aoqi@0 842 javaVFrame *jvf_prev = NULL;
aoqi@0 843 javaVFrame *jvf_prev_prev;
aoqi@0 844 int j = 0;
aoqi@0 845 while (jvf_cursor != NULL) {
aoqi@0 846 jvf_prev_prev = jvf_prev;
aoqi@0 847 jvf_prev = jvf_cursor;
aoqi@0 848 for (j = 0; j > start_depth && jvf_cursor != NULL; j--) {
aoqi@0 849 jvf_cursor = jvf_cursor->java_sender();
aoqi@0 850 }
aoqi@0 851 }
aoqi@0 852 if (j == start_depth) {
aoqi@0 853 // previous pointer is exactly where we want to start
aoqi@0 854 jvf = jvf_prev;
aoqi@0 855 } else {
aoqi@0 856 // we need to back up further to get to the right place
aoqi@0 857 if (jvf_prev_prev == NULL) {
aoqi@0 858 // the -start_depth is greater than the stack depth
aoqi@0 859 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
aoqi@0 860 }
aoqi@0 861 // j now is the number of frames on the stack starting with
aoqi@0 862 // jvf_prev, we start from jvf_prev_prev and move older on
aoqi@0 863 // the stack that many, the result is -start_depth frames
aoqi@0 864 // remaining.
aoqi@0 865 jvf = jvf_prev_prev;
aoqi@0 866 for (; j < 0; j++) {
aoqi@0 867 jvf = jvf->java_sender();
aoqi@0 868 }
aoqi@0 869 }
aoqi@0 870 }
aoqi@0 871 }
aoqi@0 872 for (; count < max_count && jvf != NULL; count++) {
aoqi@0 873 frame_buffer[count].method = jvf->method()->jmethod_id();
aoqi@0 874 frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
aoqi@0 875 jvf = jvf->java_sender();
aoqi@0 876 }
aoqi@0 877 } else {
aoqi@0 878 if (start_depth != 0) {
aoqi@0 879 // no frames and there is a starting depth
aoqi@0 880 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
aoqi@0 881 }
aoqi@0 882 }
aoqi@0 883 *count_ptr = count;
aoqi@0 884 return JVMTI_ERROR_NONE;
aoqi@0 885 }
aoqi@0 886
aoqi@0 887 jvmtiError
aoqi@0 888 JvmtiEnvBase::get_frame_count(JvmtiThreadState *state, jint *count_ptr) {
aoqi@0 889 assert((state != NULL),
aoqi@0 890 "JavaThread should create JvmtiThreadState before calling this method");
aoqi@0 891 *count_ptr = state->count_frames();
aoqi@0 892 return JVMTI_ERROR_NONE;
aoqi@0 893 }
aoqi@0 894
aoqi@0 895 jvmtiError
aoqi@0 896 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
aoqi@0 897 jmethodID* method_ptr, jlocation* location_ptr) {
aoqi@0 898 #ifdef ASSERT
aoqi@0 899 uint32_t debug_bits = 0;
aoqi@0 900 #endif
aoqi@0 901 assert((SafepointSynchronize::is_at_safepoint() ||
aoqi@0 902 is_thread_fully_suspended(java_thread, false, &debug_bits)),
aoqi@0 903 "at safepoint or target thread is suspended");
aoqi@0 904 Thread* current_thread = Thread::current();
aoqi@0 905 ResourceMark rm(current_thread);
aoqi@0 906
aoqi@0 907 vframe *vf = vframeFor(java_thread, depth);
aoqi@0 908 if (vf == NULL) {
aoqi@0 909 return JVMTI_ERROR_NO_MORE_FRAMES;
aoqi@0 910 }
aoqi@0 911
aoqi@0 912 // vframeFor should return a java frame. If it doesn't
aoqi@0 913 // it means we've got an internal error and we return the
aoqi@0 914 // error in product mode. In debug mode we will instead
aoqi@0 915 // attempt to cast the vframe to a javaVFrame and will
aoqi@0 916 // cause an assertion/crash to allow further diagnosis.
aoqi@0 917 #ifdef PRODUCT
aoqi@0 918 if (!vf->is_java_frame()) {
aoqi@0 919 return JVMTI_ERROR_INTERNAL;
aoqi@0 920 }
aoqi@0 921 #endif
aoqi@0 922
aoqi@0 923 HandleMark hm(current_thread);
aoqi@0 924 javaVFrame *jvf = javaVFrame::cast(vf);
aoqi@0 925 Method* method = jvf->method();
aoqi@0 926 if (method->is_native()) {
aoqi@0 927 *location_ptr = -1;
aoqi@0 928 } else {
aoqi@0 929 *location_ptr = jvf->bci();
aoqi@0 930 }
aoqi@0 931 *method_ptr = method->jmethod_id();
aoqi@0 932
aoqi@0 933 return JVMTI_ERROR_NONE;
aoqi@0 934 }
aoqi@0 935
aoqi@0 936
aoqi@0 937 jvmtiError
aoqi@0 938 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
aoqi@0 939 HandleMark hm;
aoqi@0 940 Handle hobj;
aoqi@0 941
aoqi@0 942 bool at_safepoint = SafepointSynchronize::is_at_safepoint();
aoqi@0 943
aoqi@0 944 // Check arguments
aoqi@0 945 {
aoqi@0 946 oop mirror = JNIHandles::resolve_external_guard(object);
aoqi@0 947 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
aoqi@0 948 NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
aoqi@0 949
aoqi@0 950 hobj = Handle(mirror);
aoqi@0 951 }
aoqi@0 952
aoqi@0 953 JavaThread *owning_thread = NULL;
aoqi@0 954 ObjectMonitor *mon = NULL;
aoqi@0 955 jvmtiMonitorUsage ret = {
aoqi@0 956 NULL, 0, 0, NULL, 0, NULL
aoqi@0 957 };
aoqi@0 958
aoqi@0 959 uint32_t debug_bits = 0;
aoqi@0 960 // first derive the object's owner and entry_count (if any)
aoqi@0 961 {
aoqi@0 962 // Revoke any biases before querying the mark word
aoqi@0 963 if (SafepointSynchronize::is_at_safepoint()) {
aoqi@0 964 BiasedLocking::revoke_at_safepoint(hobj);
aoqi@0 965 } else {
aoqi@0 966 BiasedLocking::revoke_and_rebias(hobj, false, calling_thread);
aoqi@0 967 }
aoqi@0 968
aoqi@0 969 address owner = NULL;
aoqi@0 970 {
aoqi@0 971 markOop mark = hobj()->mark();
aoqi@0 972
aoqi@0 973 if (!mark->has_monitor()) {
aoqi@0 974 // this object has a lightweight monitor
aoqi@0 975
aoqi@0 976 if (mark->has_locker()) {
aoqi@0 977 owner = (address)mark->locker(); // save the address of the Lock word
aoqi@0 978 }
aoqi@0 979 // implied else: no owner
aoqi@0 980 } else {
aoqi@0 981 // this object has a heavyweight monitor
aoqi@0 982 mon = mark->monitor();
aoqi@0 983
aoqi@0 984 // The owner field of a heavyweight monitor may be NULL for no
aoqi@0 985 // owner, a JavaThread * or it may still be the address of the
aoqi@0 986 // Lock word in a JavaThread's stack. A monitor can be inflated
aoqi@0 987 // by a non-owning JavaThread, but only the owning JavaThread
aoqi@0 988 // can change the owner field from the Lock word to the
aoqi@0 989 // JavaThread * and it may not have done that yet.
aoqi@0 990 owner = (address)mon->owner();
aoqi@0 991 }
aoqi@0 992 }
aoqi@0 993
aoqi@0 994 if (owner != NULL) {
aoqi@0 995 // This monitor is owned so we have to find the owning JavaThread.
aoqi@0 996 // Since owning_thread_from_monitor_owner() grabs a lock, GC can
aoqi@0 997 // move our object at this point. However, our owner value is safe
aoqi@0 998 // since it is either the Lock word on a stack or a JavaThread *.
aoqi@0 999 owning_thread = Threads::owning_thread_from_monitor_owner(owner, !at_safepoint);
aoqi@0 1000 // Cannot assume (owning_thread != NULL) here because this function
aoqi@0 1001 // may not have been called at a safepoint and the owning_thread
aoqi@0 1002 // might not be suspended.
aoqi@0 1003 if (owning_thread != NULL) {
aoqi@0 1004 // The monitor's owner either has to be the current thread, at safepoint
aoqi@0 1005 // or it has to be suspended. Any of these conditions will prevent both
aoqi@0 1006 // contending and waiting threads from modifying the state of
aoqi@0 1007 // the monitor.
aoqi@0 1008 if (!at_safepoint && !JvmtiEnv::is_thread_fully_suspended(owning_thread, true, &debug_bits)) {
aoqi@0 1009 // Don't worry! This return of JVMTI_ERROR_THREAD_NOT_SUSPENDED
aoqi@0 1010 // will not make it back to the JVM/TI agent. The error code will
aoqi@0 1011 // get intercepted in JvmtiEnv::GetObjectMonitorUsage() which
aoqi@0 1012 // will retry the call via a VM_GetObjectMonitorUsage VM op.
aoqi@0 1013 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
aoqi@0 1014 }
aoqi@0 1015 HandleMark hm;
aoqi@0 1016 Handle th(owning_thread->threadObj());
aoqi@0 1017 ret.owner = (jthread)jni_reference(calling_thread, th);
aoqi@0 1018 }
aoqi@0 1019 // implied else: no owner
aoqi@0 1020 }
aoqi@0 1021
aoqi@0 1022 if (owning_thread != NULL) { // monitor is owned
aoqi@0 1023 if ((address)owning_thread == owner) {
aoqi@0 1024 // the owner field is the JavaThread *
aoqi@0 1025 assert(mon != NULL,
aoqi@0 1026 "must have heavyweight monitor with JavaThread * owner");
aoqi@0 1027 ret.entry_count = mon->recursions() + 1;
aoqi@0 1028 } else {
aoqi@0 1029 // The owner field is the Lock word on the JavaThread's stack
aoqi@0 1030 // so the recursions field is not valid. We have to count the
aoqi@0 1031 // number of recursive monitor entries the hard way. We pass
aoqi@0 1032 // a handle to survive any GCs along the way.
aoqi@0 1033 ResourceMark rm;
aoqi@0 1034 ret.entry_count = count_locked_objects(owning_thread, hobj);
aoqi@0 1035 }
aoqi@0 1036 }
aoqi@0 1037 // implied else: entry_count == 0
aoqi@0 1038 }
aoqi@0 1039
aoqi@0 1040 int nWant,nWait;
aoqi@0 1041 if (mon != NULL) {
aoqi@0 1042 // this object has a heavyweight monitor
aoqi@0 1043 nWant = mon->contentions(); // # of threads contending for monitor
aoqi@0 1044 nWait = mon->waiters(); // # of threads in Object.wait()
aoqi@0 1045 ret.waiter_count = nWant + nWait;
aoqi@0 1046 ret.notify_waiter_count = nWait;
aoqi@0 1047 } else {
aoqi@0 1048 // this object has a lightweight monitor
aoqi@0 1049 ret.waiter_count = 0;
aoqi@0 1050 ret.notify_waiter_count = 0;
aoqi@0 1051 }
aoqi@0 1052
aoqi@0 1053 // Allocate memory for heavyweight and lightweight monitor.
aoqi@0 1054 jvmtiError err;
aoqi@0 1055 err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters);
aoqi@0 1056 if (err != JVMTI_ERROR_NONE) {
aoqi@0 1057 return err;
aoqi@0 1058 }
aoqi@0 1059 err = allocate(ret.notify_waiter_count * sizeof(jthread *),
aoqi@0 1060 (unsigned char**)&ret.notify_waiters);
aoqi@0 1061 if (err != JVMTI_ERROR_NONE) {
aoqi@0 1062 deallocate((unsigned char*)ret.waiters);
aoqi@0 1063 return err;
aoqi@0 1064 }
aoqi@0 1065
aoqi@0 1066 // now derive the rest of the fields
aoqi@0 1067 if (mon != NULL) {
aoqi@0 1068 // this object has a heavyweight monitor
aoqi@0 1069
aoqi@0 1070 // Number of waiters may actually be less than the waiter count.
aoqi@0 1071 // So NULL out memory so that unused memory will be NULL.
aoqi@0 1072 memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
aoqi@0 1073 memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
aoqi@0 1074
aoqi@0 1075 if (ret.waiter_count > 0) {
aoqi@0 1076 // we have contending and/or waiting threads
aoqi@0 1077 HandleMark hm;
aoqi@0 1078 if (nWant > 0) {
aoqi@0 1079 // we have contending threads
aoqi@0 1080 ResourceMark rm;
aoqi@0 1081 // get_pending_threads returns only java thread so we do not need to
aoqi@0 1082 // check for non java threads.
aoqi@0 1083 GrowableArray<JavaThread*>* wantList = Threads::get_pending_threads(
aoqi@0 1084 nWant, (address)mon, !at_safepoint);
aoqi@0 1085 if (wantList->length() < nWant) {
aoqi@0 1086 // robustness: the pending list has gotten smaller
aoqi@0 1087 nWant = wantList->length();
aoqi@0 1088 }
aoqi@0 1089 for (int i = 0; i < nWant; i++) {
aoqi@0 1090 JavaThread *pending_thread = wantList->at(i);
aoqi@0 1091 // If the monitor has no owner, then a non-suspended contending
aoqi@0 1092 // thread could potentially change the state of the monitor by
aoqi@0 1093 // entering it. The JVM/TI spec doesn't allow this.
aoqi@0 1094 if (owning_thread == NULL && !at_safepoint &
aoqi@0 1095 !JvmtiEnv::is_thread_fully_suspended(pending_thread, true, &debug_bits)) {
aoqi@0 1096 if (ret.owner != NULL) {
aoqi@0 1097 destroy_jni_reference(calling_thread, ret.owner);
aoqi@0 1098 }
aoqi@0 1099 for (int j = 0; j < i; j++) {
aoqi@0 1100 destroy_jni_reference(calling_thread, ret.waiters[j]);
aoqi@0 1101 }
aoqi@0 1102 deallocate((unsigned char*)ret.waiters);
aoqi@0 1103 deallocate((unsigned char*)ret.notify_waiters);
aoqi@0 1104 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
aoqi@0 1105 }
aoqi@0 1106 Handle th(pending_thread->threadObj());
aoqi@0 1107 ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
aoqi@0 1108 }
aoqi@0 1109 }
aoqi@0 1110 if (nWait > 0) {
aoqi@0 1111 // we have threads in Object.wait()
aoqi@0 1112 int offset = nWant; // add after any contending threads
aoqi@0 1113 ObjectWaiter *waiter = mon->first_waiter();
aoqi@0 1114 for (int i = 0, j = 0; i < nWait; i++) {
aoqi@0 1115 if (waiter == NULL) {
aoqi@0 1116 // robustness: the waiting list has gotten smaller
aoqi@0 1117 nWait = j;
aoqi@0 1118 break;
aoqi@0 1119 }
aoqi@0 1120 Thread *t = mon->thread_of_waiter(waiter);
aoqi@0 1121 if (t != NULL && t->is_Java_thread()) {
aoqi@0 1122 JavaThread *wjava_thread = (JavaThread *)t;
aoqi@0 1123 // If the thread was found on the ObjectWaiter list, then
aoqi@0 1124 // it has not been notified. This thread can't change the
aoqi@0 1125 // state of the monitor so it doesn't need to be suspended.
aoqi@0 1126 Handle th(wjava_thread->threadObj());
aoqi@0 1127 ret.waiters[offset + j] = (jthread)jni_reference(calling_thread, th);
aoqi@0 1128 ret.notify_waiters[j++] = (jthread)jni_reference(calling_thread, th);
aoqi@0 1129 }
aoqi@0 1130 waiter = mon->next_waiter(waiter);
aoqi@0 1131 }
aoqi@0 1132 }
aoqi@0 1133 }
aoqi@0 1134
aoqi@0 1135 // Adjust count. nWant and nWait count values may be less than original.
aoqi@0 1136 ret.waiter_count = nWant + nWait;
aoqi@0 1137 ret.notify_waiter_count = nWait;
aoqi@0 1138 } else {
aoqi@0 1139 // this object has a lightweight monitor and we have nothing more
aoqi@0 1140 // to do here because the defaults are just fine.
aoqi@0 1141 }
aoqi@0 1142
aoqi@0 1143 // we don't update return parameter unless everything worked
aoqi@0 1144 *info_ptr = ret;
aoqi@0 1145
aoqi@0 1146 return JVMTI_ERROR_NONE;
aoqi@0 1147 }
aoqi@0 1148
aoqi@0 1149 ResourceTracker::ResourceTracker(JvmtiEnv* env) {
aoqi@0 1150 _env = env;
aoqi@0 1151 _allocations = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<unsigned char*>(20, true);
aoqi@0 1152 _failed = false;
aoqi@0 1153 }
aoqi@0 1154 ResourceTracker::~ResourceTracker() {
aoqi@0 1155 if (_failed) {
aoqi@0 1156 for (int i=0; i<_allocations->length(); i++) {
aoqi@0 1157 _env->deallocate(_allocations->at(i));
aoqi@0 1158 }
aoqi@0 1159 }
aoqi@0 1160 delete _allocations;
aoqi@0 1161 }
aoqi@0 1162
aoqi@0 1163 jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) {
aoqi@0 1164 unsigned char *ptr;
aoqi@0 1165 jvmtiError err = _env->allocate(size, &ptr);
aoqi@0 1166 if (err == JVMTI_ERROR_NONE) {
aoqi@0 1167 _allocations->append(ptr);
aoqi@0 1168 *mem_ptr = ptr;
aoqi@0 1169 } else {
aoqi@0 1170 *mem_ptr = NULL;
aoqi@0 1171 _failed = true;
aoqi@0 1172 }
aoqi@0 1173 return err;
aoqi@0 1174 }
aoqi@0 1175
aoqi@0 1176 unsigned char* ResourceTracker::allocate(jlong size) {
aoqi@0 1177 unsigned char* ptr;
aoqi@0 1178 allocate(size, &ptr);
aoqi@0 1179 return ptr;
aoqi@0 1180 }
aoqi@0 1181
aoqi@0 1182 char* ResourceTracker::strdup(const char* str) {
aoqi@0 1183 char *dup_str = (char*)allocate(strlen(str)+1);
aoqi@0 1184 if (dup_str != NULL) {
aoqi@0 1185 strcpy(dup_str, str);
aoqi@0 1186 }
aoqi@0 1187 return dup_str;
aoqi@0 1188 }
aoqi@0 1189
aoqi@0 1190 struct StackInfoNode {
aoqi@0 1191 struct StackInfoNode *next;
aoqi@0 1192 jvmtiStackInfo info;
aoqi@0 1193 };
aoqi@0 1194
aoqi@0 1195 // Create a jvmtiStackInfo inside a linked list node and create a
aoqi@0 1196 // buffer for the frame information, both allocated as resource objects.
aoqi@0 1197 // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo.
aoqi@0 1198 // Note that either or both of thr and thread_oop
aoqi@0 1199 // may be null if the thread is new or has exited.
aoqi@0 1200 void
aoqi@0 1201 VM_GetMultipleStackTraces::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) {
aoqi@0 1202 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
aoqi@0 1203
aoqi@0 1204 jint state = 0;
aoqi@0 1205 struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
aoqi@0 1206 jvmtiStackInfo *infop = &(node->info);
aoqi@0 1207 node->next = head();
aoqi@0 1208 set_head(node);
aoqi@0 1209 infop->frame_count = 0;
aoqi@0 1210 infop->thread = jt;
aoqi@0 1211
aoqi@0 1212 if (thread_oop != NULL) {
aoqi@0 1213 // get most state bits
aoqi@0 1214 state = (jint)java_lang_Thread::get_thread_status(thread_oop);
aoqi@0 1215 }
aoqi@0 1216
aoqi@0 1217 if (thr != NULL) { // add more state bits if there is a JavaThead to query
aoqi@0 1218 // same as is_being_ext_suspended() but without locking
aoqi@0 1219 if (thr->is_ext_suspended() || thr->is_external_suspend()) {
aoqi@0 1220 state |= JVMTI_THREAD_STATE_SUSPENDED;
aoqi@0 1221 }
aoqi@0 1222 JavaThreadState jts = thr->thread_state();
aoqi@0 1223 if (jts == _thread_in_native) {
aoqi@0 1224 state |= JVMTI_THREAD_STATE_IN_NATIVE;
aoqi@0 1225 }
aoqi@0 1226 OSThread* osThread = thr->osthread();
aoqi@0 1227 if (osThread != NULL && osThread->interrupted()) {
aoqi@0 1228 state |= JVMTI_THREAD_STATE_INTERRUPTED;
aoqi@0 1229 }
aoqi@0 1230 }
aoqi@0 1231 infop->state = state;
aoqi@0 1232
aoqi@0 1233 if (thr != NULL || (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
aoqi@0 1234 infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
aoqi@0 1235 env()->get_stack_trace(thr, 0, max_frame_count(),
aoqi@0 1236 infop->frame_buffer, &(infop->frame_count));
aoqi@0 1237 } else {
aoqi@0 1238 infop->frame_buffer = NULL;
aoqi@0 1239 infop->frame_count = 0;
aoqi@0 1240 }
aoqi@0 1241 _frame_count_total += infop->frame_count;
aoqi@0 1242 }
aoqi@0 1243
aoqi@0 1244 // Based on the stack information in the linked list, allocate memory
aoqi@0 1245 // block to return and fill it from the info in the linked list.
aoqi@0 1246 void
aoqi@0 1247 VM_GetMultipleStackTraces::allocate_and_fill_stacks(jint thread_count) {
aoqi@0 1248 // do I need to worry about alignment issues?
aoqi@0 1249 jlong alloc_size = thread_count * sizeof(jvmtiStackInfo)
aoqi@0 1250 + _frame_count_total * sizeof(jvmtiFrameInfo);
aoqi@0 1251 env()->allocate(alloc_size, (unsigned char **)&_stack_info);
aoqi@0 1252
aoqi@0 1253 // pointers to move through the newly allocated space as it is filled in
aoqi@0 1254 jvmtiStackInfo *si = _stack_info + thread_count; // bottom of stack info
aoqi@0 1255 jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si; // is the top of frame info
aoqi@0 1256
aoqi@0 1257 // copy information in resource area into allocated buffer
aoqi@0 1258 // insert stack info backwards since linked list is backwards
aoqi@0 1259 // insert frame info forwards
aoqi@0 1260 // walk the StackInfoNodes
aoqi@0 1261 for (struct StackInfoNode *sin = head(); sin != NULL; sin = sin->next) {
aoqi@0 1262 jint frame_count = sin->info.frame_count;
aoqi@0 1263 size_t frames_size = frame_count * sizeof(jvmtiFrameInfo);
aoqi@0 1264 --si;
aoqi@0 1265 memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
aoqi@0 1266 if (frames_size == 0) {
aoqi@0 1267 si->frame_buffer = NULL;
aoqi@0 1268 } else {
aoqi@0 1269 memcpy(fi, sin->info.frame_buffer, frames_size);
aoqi@0 1270 si->frame_buffer = fi; // point to the new allocated copy of the frames
aoqi@0 1271 fi += frame_count;
aoqi@0 1272 }
aoqi@0 1273 }
aoqi@0 1274 assert(si == _stack_info, "the last copied stack info must be the first record");
aoqi@0 1275 assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size,
aoqi@0 1276 "the last copied frame info must be the last record");
aoqi@0 1277 }
aoqi@0 1278
aoqi@0 1279
aoqi@0 1280 void
aoqi@0 1281 VM_GetThreadListStackTraces::doit() {
aoqi@0 1282 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
aoqi@0 1283
aoqi@0 1284 ResourceMark rm;
aoqi@0 1285 for (int i = 0; i < _thread_count; ++i) {
aoqi@0 1286 jthread jt = _thread_list[i];
aoqi@0 1287 oop thread_oop = JNIHandles::resolve_external_guard(jt);
aoqi@0 1288 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
aoqi@0 1289 set_result(JVMTI_ERROR_INVALID_THREAD);
aoqi@0 1290 return;
aoqi@0 1291 }
aoqi@0 1292 fill_frames(jt, java_lang_Thread::thread(thread_oop), thread_oop);
aoqi@0 1293 }
aoqi@0 1294 allocate_and_fill_stacks(_thread_count);
aoqi@0 1295 }
aoqi@0 1296
aoqi@0 1297 void
aoqi@0 1298 VM_GetAllStackTraces::doit() {
aoqi@0 1299 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
aoqi@0 1300
aoqi@0 1301 ResourceMark rm;
aoqi@0 1302 _final_thread_count = 0;
aoqi@0 1303 for (JavaThread *jt = Threads::first(); jt != NULL; jt = jt->next()) {
aoqi@0 1304 oop thread_oop = jt->threadObj();
aoqi@0 1305 if (thread_oop != NULL &&
aoqi@0 1306 !jt->is_exiting() &&
aoqi@0 1307 java_lang_Thread::is_alive(thread_oop) &&
aoqi@0 1308 !jt->is_hidden_from_external_view()) {
aoqi@0 1309 ++_final_thread_count;
aoqi@0 1310 // Handle block of the calling thread is used to create local refs.
aoqi@0 1311 fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
aoqi@0 1312 jt, thread_oop);
aoqi@0 1313 }
aoqi@0 1314 }
aoqi@0 1315 allocate_and_fill_stacks(_final_thread_count);
aoqi@0 1316 }
aoqi@0 1317
aoqi@0 1318 // Verifies that the top frame is a java frame in an expected state.
aoqi@0 1319 // Deoptimizes frame if needed.
aoqi@0 1320 // Checks that the frame method signature matches the return type (tos).
aoqi@0 1321 // HandleMark must be defined in the caller only.
aoqi@0 1322 // It is to keep a ret_ob_h handle alive after return to the caller.
aoqi@0 1323 jvmtiError
aoqi@0 1324 JvmtiEnvBase::check_top_frame(JavaThread* current_thread, JavaThread* java_thread,
aoqi@0 1325 jvalue value, TosState tos, Handle* ret_ob_h) {
aoqi@0 1326 ResourceMark rm(current_thread);
aoqi@0 1327
aoqi@0 1328 vframe *vf = vframeFor(java_thread, 0);
aoqi@0 1329 NULL_CHECK(vf, JVMTI_ERROR_NO_MORE_FRAMES);
aoqi@0 1330
aoqi@0 1331 javaVFrame *jvf = (javaVFrame*) vf;
aoqi@0 1332 if (!vf->is_java_frame() || jvf->method()->is_native()) {
aoqi@0 1333 return JVMTI_ERROR_OPAQUE_FRAME;
aoqi@0 1334 }
aoqi@0 1335
aoqi@0 1336 // If the frame is a compiled one, need to deoptimize it.
aoqi@0 1337 if (vf->is_compiled_frame()) {
aoqi@0 1338 if (!vf->fr().can_be_deoptimized()) {
aoqi@0 1339 return JVMTI_ERROR_OPAQUE_FRAME;
aoqi@0 1340 }
aoqi@0 1341 Deoptimization::deoptimize_frame(java_thread, jvf->fr().id());
aoqi@0 1342 }
aoqi@0 1343
aoqi@0 1344 // Get information about method return type
aoqi@0 1345 Symbol* signature = jvf->method()->signature();
aoqi@0 1346
aoqi@0 1347 ResultTypeFinder rtf(signature);
aoqi@0 1348 TosState fr_tos = as_TosState(rtf.type());
aoqi@0 1349 if (fr_tos != tos) {
aoqi@0 1350 if (tos != itos || (fr_tos != btos && fr_tos != ctos && fr_tos != stos)) {
aoqi@0 1351 return JVMTI_ERROR_TYPE_MISMATCH;
aoqi@0 1352 }
aoqi@0 1353 }
aoqi@0 1354
aoqi@0 1355 // Check that the jobject class matches the return type signature.
aoqi@0 1356 jobject jobj = value.l;
aoqi@0 1357 if (tos == atos && jobj != NULL) { // NULL reference is allowed
aoqi@0 1358 Handle ob_h = Handle(current_thread, JNIHandles::resolve_external_guard(jobj));
aoqi@0 1359 NULL_CHECK(ob_h, JVMTI_ERROR_INVALID_OBJECT);
aoqi@0 1360 KlassHandle ob_kh = KlassHandle(current_thread, ob_h()->klass());
aoqi@0 1361 NULL_CHECK(ob_kh, JVMTI_ERROR_INVALID_OBJECT);
aoqi@0 1362
aoqi@0 1363 // Method return type signature.
aoqi@0 1364 char* ty_sign = 1 + strchr(signature->as_C_string(), ')');
aoqi@0 1365
aoqi@0 1366 if (!VM_GetOrSetLocal::is_assignable(ty_sign, ob_kh(), current_thread)) {
aoqi@0 1367 return JVMTI_ERROR_TYPE_MISMATCH;
aoqi@0 1368 }
aoqi@0 1369 *ret_ob_h = ob_h;
aoqi@0 1370 }
aoqi@0 1371 return JVMTI_ERROR_NONE;
aoqi@0 1372 } /* end check_top_frame */
aoqi@0 1373
aoqi@0 1374
aoqi@0 1375 // ForceEarlyReturn<type> follows the PopFrame approach in many aspects.
aoqi@0 1376 // Main difference is on the last stage in the interpreter.
aoqi@0 1377 // The PopFrame stops method execution to continue execution
aoqi@0 1378 // from the same method call instruction.
aoqi@0 1379 // The ForceEarlyReturn forces return from method so the execution
aoqi@0 1380 // continues at the bytecode following the method call.
aoqi@0 1381
aoqi@0 1382 // Threads_lock NOT held, java_thread not protected by lock
aoqi@0 1383 // java_thread - pre-checked
aoqi@0 1384
aoqi@0 1385 jvmtiError
aoqi@0 1386 JvmtiEnvBase::force_early_return(JavaThread* java_thread, jvalue value, TosState tos) {
aoqi@0 1387 JavaThread* current_thread = JavaThread::current();
aoqi@0 1388 HandleMark hm(current_thread);
aoqi@0 1389 uint32_t debug_bits = 0;
aoqi@0 1390
aoqi@0 1391 // retrieve or create the state
aoqi@0 1392 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
aoqi@0 1393 if (state == NULL) {
aoqi@0 1394 return JVMTI_ERROR_THREAD_NOT_ALIVE;
aoqi@0 1395 }
aoqi@0 1396
aoqi@0 1397 // Check if java_thread is fully suspended
aoqi@0 1398 if (!is_thread_fully_suspended(java_thread,
aoqi@0 1399 true /* wait for suspend completion */,
aoqi@0 1400 &debug_bits)) {
aoqi@0 1401 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
aoqi@0 1402 }
aoqi@0 1403
aoqi@0 1404 // Check to see if a ForceEarlyReturn was already in progress
aoqi@0 1405 if (state->is_earlyret_pending()) {
aoqi@0 1406 // Probably possible for JVMTI clients to trigger this, but the
aoqi@0 1407 // JPDA backend shouldn't allow this to happen
aoqi@0 1408 return JVMTI_ERROR_INTERNAL;
aoqi@0 1409 }
aoqi@0 1410 {
aoqi@0 1411 // The same as for PopFrame. Workaround bug:
aoqi@0 1412 // 4812902: popFrame hangs if the method is waiting at a synchronize
aoqi@0 1413 // Catch this condition and return an error to avoid hanging.
aoqi@0 1414 // Now JVMTI spec allows an implementation to bail out with an opaque
aoqi@0 1415 // frame error.
aoqi@0 1416 OSThread* osThread = java_thread->osthread();
aoqi@0 1417 if (osThread->get_state() == MONITOR_WAIT) {
aoqi@0 1418 return JVMTI_ERROR_OPAQUE_FRAME;
aoqi@0 1419 }
aoqi@0 1420 }
aoqi@0 1421 Handle ret_ob_h = Handle();
aoqi@0 1422 jvmtiError err = check_top_frame(current_thread, java_thread, value, tos, &ret_ob_h);
aoqi@0 1423 if (err != JVMTI_ERROR_NONE) {
aoqi@0 1424 return err;
aoqi@0 1425 }
aoqi@0 1426 assert(tos != atos || value.l == NULL || ret_ob_h() != NULL,
aoqi@0 1427 "return object oop must not be NULL if jobject is not NULL");
aoqi@0 1428
aoqi@0 1429 // Update the thread state to reflect that the top frame must be
aoqi@0 1430 // forced to return.
aoqi@0 1431 // The current frame will be returned later when the suspended
aoqi@0 1432 // thread is resumed and right before returning from VM to Java.
aoqi@0 1433 // (see call_VM_base() in assembler_<cpu>.cpp).
aoqi@0 1434
aoqi@0 1435 state->set_earlyret_pending();
aoqi@0 1436 state->set_earlyret_oop(ret_ob_h());
aoqi@0 1437 state->set_earlyret_value(value, tos);
aoqi@0 1438
aoqi@0 1439 // Set pending step flag for this early return.
aoqi@0 1440 // It is cleared when next step event is posted.
aoqi@0 1441 state->set_pending_step_for_earlyret();
aoqi@0 1442
aoqi@0 1443 return JVMTI_ERROR_NONE;
aoqi@0 1444 } /* end force_early_return */
aoqi@0 1445
aoqi@0 1446 void
aoqi@0 1447 JvmtiMonitorClosure::do_monitor(ObjectMonitor* mon) {
aoqi@0 1448 if ( _error != JVMTI_ERROR_NONE) {
aoqi@0 1449 // Error occurred in previous iteration so no need to add
aoqi@0 1450 // to the list.
aoqi@0 1451 return;
aoqi@0 1452 }
aoqi@0 1453 if (mon->owner() == _java_thread ) {
aoqi@0 1454 // Filter out on stack monitors collected during stack walk.
aoqi@0 1455 oop obj = (oop)mon->object();
aoqi@0 1456 bool found = false;
aoqi@0 1457 for (int j = 0; j < _owned_monitors_list->length(); j++) {
aoqi@0 1458 jobject jobj = ((jvmtiMonitorStackDepthInfo*)_owned_monitors_list->at(j))->monitor;
aoqi@0 1459 oop check = JNIHandles::resolve(jobj);
aoqi@0 1460 if (check == obj) {
aoqi@0 1461 // On stack monitor already collected during the stack walk.
aoqi@0 1462 found = true;
aoqi@0 1463 break;
aoqi@0 1464 }
aoqi@0 1465 }
aoqi@0 1466 if (found == false) {
aoqi@0 1467 // This is off stack monitor (e.g. acquired via jni MonitorEnter).
aoqi@0 1468 jvmtiError err;
aoqi@0 1469 jvmtiMonitorStackDepthInfo *jmsdi;
aoqi@0 1470 err = _env->allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
aoqi@0 1471 if (err != JVMTI_ERROR_NONE) {
aoqi@0 1472 _error = err;
aoqi@0 1473 return;
aoqi@0 1474 }
aoqi@0 1475 Handle hobj(obj);
aoqi@0 1476 jmsdi->monitor = _env->jni_reference(_calling_thread, hobj);
aoqi@0 1477 // stack depth is unknown for this monitor.
aoqi@0 1478 jmsdi->stack_depth = -1;
aoqi@0 1479 _owned_monitors_list->append(jmsdi);
aoqi@0 1480 }
aoqi@0 1481 }
aoqi@0 1482 }

mercurial