src/share/vm/prims/jvmtiEnvBase.cpp

Tue, 17 Oct 2017 12:58:25 +0800

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

mercurial