src/share/vm/prims/jvmtiThreadState.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2012, 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 "jvmtifiles/jvmtiEnv.hpp"
aoqi@0 27 #include "memory/gcLocker.hpp"
aoqi@0 28 #include "memory/resourceArea.hpp"
aoqi@0 29 #include "prims/jvmtiEventController.inline.hpp"
aoqi@0 30 #include "prims/jvmtiImpl.hpp"
aoqi@0 31 #include "prims/jvmtiThreadState.inline.hpp"
aoqi@0 32 #include "runtime/vframe.hpp"
aoqi@0 33
aoqi@0 34 // marker for when the stack depth has been reset and is now unknown.
aoqi@0 35 // any negative number would work but small ones might obscure an
aoqi@0 36 // underrun error.
aoqi@0 37 static const int UNKNOWN_STACK_DEPTH = -99;
aoqi@0 38
aoqi@0 39 ///////////////////////////////////////////////////////////////
aoqi@0 40 //
aoqi@0 41 // class JvmtiThreadState
aoqi@0 42 //
aoqi@0 43 // Instances of JvmtiThreadState hang off of each thread.
aoqi@0 44 // Thread local storage for JVMTI.
aoqi@0 45 //
aoqi@0 46
aoqi@0 47 JvmtiThreadState *JvmtiThreadState::_head = NULL;
aoqi@0 48
aoqi@0 49 JvmtiThreadState::JvmtiThreadState(JavaThread* thread)
aoqi@0 50 : _thread_event_enable() {
aoqi@0 51 assert(JvmtiThreadState_lock->is_locked(), "sanity check");
aoqi@0 52 _thread = thread;
aoqi@0 53 _exception_detected = false;
aoqi@0 54 _exception_caught = false;
aoqi@0 55 _debuggable = true;
aoqi@0 56 _hide_single_stepping = false;
aoqi@0 57 _hide_level = 0;
aoqi@0 58 _pending_step_for_popframe = false;
aoqi@0 59 _class_being_redefined = NULL;
aoqi@0 60 _class_load_kind = jvmti_class_load_kind_load;
aoqi@0 61 _head_env_thread_state = NULL;
aoqi@0 62 _dynamic_code_event_collector = NULL;
aoqi@0 63 _vm_object_alloc_event_collector = NULL;
aoqi@0 64 _the_class_for_redefinition_verification = NULL;
aoqi@0 65 _scratch_class_for_redefinition_verification = NULL;
aoqi@0 66
aoqi@0 67 // JVMTI ForceEarlyReturn support
aoqi@0 68 _pending_step_for_earlyret = false;
aoqi@0 69 _earlyret_state = earlyret_inactive;
aoqi@0 70 _earlyret_tos = ilgl;
aoqi@0 71 _earlyret_value.j = 0L;
aoqi@0 72 _earlyret_oop = NULL;
aoqi@0 73
aoqi@0 74 // add all the JvmtiEnvThreadState to the new JvmtiThreadState
aoqi@0 75 {
aoqi@0 76 JvmtiEnvIterator it;
aoqi@0 77 for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
aoqi@0 78 if (env->is_valid()) {
aoqi@0 79 add_env(env);
aoqi@0 80 }
aoqi@0 81 }
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 // link us into the list
aoqi@0 85 {
aoqi@0 86 // The thread state list manipulation code must not have safepoints.
aoqi@0 87 // See periodic_clean_up().
aoqi@0 88 debug_only(No_Safepoint_Verifier nosafepoint;)
aoqi@0 89
aoqi@0 90 _prev = NULL;
aoqi@0 91 _next = _head;
aoqi@0 92 if (_head != NULL) {
aoqi@0 93 _head->_prev = this;
aoqi@0 94 }
aoqi@0 95 _head = this;
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 // set this as the state for the thread
aoqi@0 99 thread->set_jvmti_thread_state(this);
aoqi@0 100 }
aoqi@0 101
aoqi@0 102
aoqi@0 103 JvmtiThreadState::~JvmtiThreadState() {
aoqi@0 104 assert(JvmtiThreadState_lock->is_locked(), "sanity check");
aoqi@0 105
aoqi@0 106 // clear this as the state for the thread
aoqi@0 107 get_thread()->set_jvmti_thread_state(NULL);
aoqi@0 108
aoqi@0 109 // zap our env thread states
aoqi@0 110 {
aoqi@0 111 JvmtiEnvBase::entering_dying_thread_env_iteration();
aoqi@0 112 JvmtiEnvThreadStateIterator it(this);
aoqi@0 113 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ) {
aoqi@0 114 JvmtiEnvThreadState* zap = ets;
aoqi@0 115 ets = it.next(ets);
aoqi@0 116 delete zap;
aoqi@0 117 }
aoqi@0 118 JvmtiEnvBase::leaving_dying_thread_env_iteration();
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 // remove us from the list
aoqi@0 122 {
aoqi@0 123 // The thread state list manipulation code must not have safepoints.
aoqi@0 124 // See periodic_clean_up().
aoqi@0 125 debug_only(No_Safepoint_Verifier nosafepoint;)
aoqi@0 126
aoqi@0 127 if (_prev == NULL) {
aoqi@0 128 assert(_head == this, "sanity check");
aoqi@0 129 _head = _next;
aoqi@0 130 } else {
aoqi@0 131 assert(_head != this, "sanity check");
aoqi@0 132 _prev->_next = _next;
aoqi@0 133 }
aoqi@0 134 if (_next != NULL) {
aoqi@0 135 _next->_prev = _prev;
aoqi@0 136 }
aoqi@0 137 _next = NULL;
aoqi@0 138 _prev = NULL;
aoqi@0 139 }
aoqi@0 140 }
aoqi@0 141
aoqi@0 142
aoqi@0 143 void
aoqi@0 144 JvmtiThreadState::periodic_clean_up() {
aoqi@0 145 assert(SafepointSynchronize::is_at_safepoint(), "at safepoint");
aoqi@0 146
aoqi@0 147 // This iteration is initialized with "_head" instead of "JvmtiThreadState::first()"
aoqi@0 148 // because the latter requires the JvmtiThreadState_lock.
aoqi@0 149 // This iteration is safe at a safepoint as well, see the No_Safepoint_Verifier
aoqi@0 150 // asserts at all list manipulation sites.
aoqi@0 151 for (JvmtiThreadState *state = _head; state != NULL; state = state->next()) {
aoqi@0 152 // For each environment thread state corresponding to an invalid environment
aoqi@0 153 // unlink it from the list and deallocate it.
aoqi@0 154 JvmtiEnvThreadStateIterator it(state);
aoqi@0 155 JvmtiEnvThreadState* previous_ets = NULL;
aoqi@0 156 JvmtiEnvThreadState* ets = it.first();
aoqi@0 157 while (ets != NULL) {
aoqi@0 158 if (ets->get_env()->is_valid()) {
aoqi@0 159 previous_ets = ets;
aoqi@0 160 ets = it.next(ets);
aoqi@0 161 } else {
aoqi@0 162 // This one isn't valid, remove it from the list and deallocate it
aoqi@0 163 JvmtiEnvThreadState* defunct_ets = ets;
aoqi@0 164 ets = ets->next();
aoqi@0 165 if (previous_ets == NULL) {
aoqi@0 166 assert(state->head_env_thread_state() == defunct_ets, "sanity check");
aoqi@0 167 state->set_head_env_thread_state(ets);
aoqi@0 168 } else {
aoqi@0 169 previous_ets->set_next(ets);
aoqi@0 170 }
aoqi@0 171 delete defunct_ets;
aoqi@0 172 }
aoqi@0 173 }
aoqi@0 174 }
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 void JvmtiThreadState::add_env(JvmtiEnvBase *env) {
aoqi@0 178 assert(JvmtiThreadState_lock->is_locked(), "sanity check");
aoqi@0 179
aoqi@0 180 JvmtiEnvThreadState *new_ets = new JvmtiEnvThreadState(_thread, env);
aoqi@0 181 // add this environment thread state to the end of the list (order is important)
aoqi@0 182 {
aoqi@0 183 // list deallocation (which occurs at a safepoint) cannot occur simultaneously
aoqi@0 184 debug_only(No_Safepoint_Verifier nosafepoint;)
aoqi@0 185
aoqi@0 186 JvmtiEnvThreadStateIterator it(this);
aoqi@0 187 JvmtiEnvThreadState* previous_ets = NULL;
aoqi@0 188 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
aoqi@0 189 previous_ets = ets;
aoqi@0 190 }
aoqi@0 191 if (previous_ets == NULL) {
aoqi@0 192 set_head_env_thread_state(new_ets);
aoqi@0 193 } else {
aoqi@0 194 previous_ets->set_next(new_ets);
aoqi@0 195 }
aoqi@0 196 }
aoqi@0 197 }
aoqi@0 198
aoqi@0 199
aoqi@0 200
aoqi@0 201
aoqi@0 202 void JvmtiThreadState::enter_interp_only_mode() {
aoqi@0 203 assert(_thread->get_interp_only_mode() == 0, "entering interp only when mode not zero");
aoqi@0 204 _thread->increment_interp_only_mode();
aoqi@0 205 }
aoqi@0 206
aoqi@0 207
aoqi@0 208 void JvmtiThreadState::leave_interp_only_mode() {
aoqi@0 209 assert(_thread->get_interp_only_mode() == 1, "leaving interp only when mode not one");
aoqi@0 210 _thread->decrement_interp_only_mode();
aoqi@0 211 }
aoqi@0 212
aoqi@0 213
aoqi@0 214 // Helper routine used in several places
aoqi@0 215 int JvmtiThreadState::count_frames() {
aoqi@0 216 #ifdef ASSERT
aoqi@0 217 uint32_t debug_bits = 0;
aoqi@0 218 #endif
aoqi@0 219 assert(SafepointSynchronize::is_at_safepoint() ||
aoqi@0 220 JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
aoqi@0 221 "at safepoint or must be suspended");
aoqi@0 222
aoqi@0 223 if (!get_thread()->has_last_Java_frame()) return 0; // no Java frames
aoqi@0 224
aoqi@0 225 ResourceMark rm;
aoqi@0 226 RegisterMap reg_map(get_thread());
aoqi@0 227 javaVFrame *jvf = get_thread()->last_java_vframe(&reg_map);
aoqi@0 228 int n = 0;
aoqi@0 229 // tty->print_cr("CSD: counting frames on %s ...",
aoqi@0 230 // JvmtiTrace::safe_get_thread_name(get_thread()));
aoqi@0 231 while (jvf != NULL) {
aoqi@0 232 Method* method = jvf->method();
aoqi@0 233 // tty->print_cr("CSD: frame - method %s.%s - loc %d",
aoqi@0 234 // method->klass_name()->as_C_string(),
aoqi@0 235 // method->name()->as_C_string(),
aoqi@0 236 // jvf->bci() );
aoqi@0 237 jvf = jvf->java_sender();
aoqi@0 238 n++;
aoqi@0 239 }
aoqi@0 240 // tty->print_cr("CSD: frame count: %d", n);
aoqi@0 241 return n;
aoqi@0 242 }
aoqi@0 243
aoqi@0 244
aoqi@0 245 void JvmtiThreadState::invalidate_cur_stack_depth() {
aoqi@0 246 Thread *cur = Thread::current();
aoqi@0 247 uint32_t debug_bits = 0;
aoqi@0 248
aoqi@0 249 // The caller can be the VMThread at a safepoint, the current thread
aoqi@0 250 // or the target thread must be suspended.
aoqi@0 251 guarantee((cur->is_VM_thread() && SafepointSynchronize::is_at_safepoint()) ||
aoqi@0 252 (JavaThread *)cur == get_thread() ||
aoqi@0 253 JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
aoqi@0 254 "sanity check");
aoqi@0 255
aoqi@0 256 _cur_stack_depth = UNKNOWN_STACK_DEPTH;
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 void JvmtiThreadState::incr_cur_stack_depth() {
aoqi@0 260 guarantee(JavaThread::current() == get_thread(), "must be current thread");
aoqi@0 261
aoqi@0 262 if (!is_interp_only_mode()) {
aoqi@0 263 _cur_stack_depth = UNKNOWN_STACK_DEPTH;
aoqi@0 264 }
aoqi@0 265 if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
aoqi@0 266 ++_cur_stack_depth;
aoqi@0 267 }
aoqi@0 268 }
aoqi@0 269
aoqi@0 270 void JvmtiThreadState::decr_cur_stack_depth() {
aoqi@0 271 guarantee(JavaThread::current() == get_thread(), "must be current thread");
aoqi@0 272
aoqi@0 273 if (!is_interp_only_mode()) {
aoqi@0 274 _cur_stack_depth = UNKNOWN_STACK_DEPTH;
aoqi@0 275 }
aoqi@0 276 if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
aoqi@0 277 --_cur_stack_depth;
aoqi@0 278 assert(_cur_stack_depth >= 0, "incr/decr_cur_stack_depth mismatch");
aoqi@0 279 }
aoqi@0 280 }
aoqi@0 281
aoqi@0 282 int JvmtiThreadState::cur_stack_depth() {
aoqi@0 283 uint32_t debug_bits = 0;
aoqi@0 284 guarantee(JavaThread::current() == get_thread() ||
aoqi@0 285 JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
aoqi@0 286 "must be current thread or suspended");
aoqi@0 287
aoqi@0 288 if (!is_interp_only_mode() || _cur_stack_depth == UNKNOWN_STACK_DEPTH) {
aoqi@0 289 _cur_stack_depth = count_frames();
aoqi@0 290 } else {
aoqi@0 291 // heavy weight assert
aoqi@0 292 assert(_cur_stack_depth == count_frames(),
aoqi@0 293 "cur_stack_depth out of sync");
aoqi@0 294 }
aoqi@0 295 return _cur_stack_depth;
aoqi@0 296 }
aoqi@0 297
aoqi@0 298 bool JvmtiThreadState::may_be_walked() {
aoqi@0 299 return (get_thread()->is_being_ext_suspended() || (JavaThread::current() == get_thread()));
aoqi@0 300 }
aoqi@0 301
aoqi@0 302
aoqi@0 303 void JvmtiThreadState::process_pending_step_for_popframe() {
aoqi@0 304 // We are single stepping as the last part of the PopFrame() dance
aoqi@0 305 // so we have some house keeping to do.
aoqi@0 306
aoqi@0 307 JavaThread *thr = get_thread();
aoqi@0 308 if (thr->popframe_condition() != JavaThread::popframe_inactive) {
aoqi@0 309 // If the popframe_condition field is not popframe_inactive, then
aoqi@0 310 // we missed all of the popframe_field cleanup points:
aoqi@0 311 //
aoqi@0 312 // - unpack_frames() was not called (nothing to deopt)
aoqi@0 313 // - remove_activation_preserving_args_entry() was not called
aoqi@0 314 // (did not get suspended in a call_vm() family call and did
aoqi@0 315 // not complete a call_vm() family call on the way here)
aoqi@0 316 thr->clear_popframe_condition();
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 // clearing the flag indicates we are done with the PopFrame() dance
aoqi@0 320 clr_pending_step_for_popframe();
aoqi@0 321
aoqi@0 322 // If exception was thrown in this frame, need to reset jvmti thread state.
aoqi@0 323 // Single stepping may not get enabled correctly by the agent since
aoqi@0 324 // exception state is passed in MethodExit event which may be sent at some
aoqi@0 325 // time in the future. JDWP agent ignores MethodExit events if caused by
aoqi@0 326 // an exception.
aoqi@0 327 //
aoqi@0 328 if (is_exception_detected()) {
aoqi@0 329 clear_exception_detected();
aoqi@0 330 }
aoqi@0 331 // If step is pending for popframe then it may not be
aoqi@0 332 // a repeat step. The new_bci and method_id is same as current_bci
aoqi@0 333 // and current method_id after pop and step for recursive calls.
aoqi@0 334 // Force the step by clearing the last location.
aoqi@0 335 JvmtiEnvThreadStateIterator it(this);
aoqi@0 336 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
aoqi@0 337 ets->clear_current_location();
aoqi@0 338 }
aoqi@0 339 }
aoqi@0 340
aoqi@0 341
aoqi@0 342 // Class: JvmtiThreadState
aoqi@0 343 // Function: update_for_pop_top_frame
aoqi@0 344 // Description:
aoqi@0 345 // This function removes any frame pop notification request for
aoqi@0 346 // the top frame and invalidates both the current stack depth and
aoqi@0 347 // all cached frameIDs.
aoqi@0 348 //
aoqi@0 349 // Called by: PopFrame
aoqi@0 350 //
aoqi@0 351 void JvmtiThreadState::update_for_pop_top_frame() {
aoqi@0 352 if (is_interp_only_mode()) {
aoqi@0 353 // remove any frame pop notification request for the top frame
aoqi@0 354 // in any environment
aoqi@0 355 int popframe_number = cur_stack_depth();
aoqi@0 356 {
aoqi@0 357 JvmtiEnvThreadStateIterator it(this);
aoqi@0 358 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
aoqi@0 359 if (ets->is_frame_pop(popframe_number)) {
aoqi@0 360 ets->clear_frame_pop(popframe_number);
aoqi@0 361 }
aoqi@0 362 }
aoqi@0 363 }
aoqi@0 364 // force stack depth to be recalculated
aoqi@0 365 invalidate_cur_stack_depth();
aoqi@0 366 } else {
aoqi@0 367 assert(!is_enabled(JVMTI_EVENT_FRAME_POP), "Must have no framepops set");
aoqi@0 368 }
aoqi@0 369 }
aoqi@0 370
aoqi@0 371
aoqi@0 372 void JvmtiThreadState::process_pending_step_for_earlyret() {
aoqi@0 373 // We are single stepping as the last part of the ForceEarlyReturn
aoqi@0 374 // dance so we have some house keeping to do.
aoqi@0 375
aoqi@0 376 if (is_earlyret_pending()) {
aoqi@0 377 // If the earlyret_state field is not earlyret_inactive, then
aoqi@0 378 // we missed all of the earlyret_field cleanup points:
aoqi@0 379 //
aoqi@0 380 // - remove_activation() was not called
aoqi@0 381 // (did not get suspended in a call_vm() family call and did
aoqi@0 382 // not complete a call_vm() family call on the way here)
aoqi@0 383 //
aoqi@0 384 // One legitimate way for us to miss all the cleanup points is
aoqi@0 385 // if we got here right after handling a compiled return. If that
aoqi@0 386 // is the case, then we consider our return from compiled code to
aoqi@0 387 // complete the ForceEarlyReturn request and we clear the condition.
aoqi@0 388 clr_earlyret_pending();
aoqi@0 389 set_earlyret_oop(NULL);
aoqi@0 390 clr_earlyret_value();
aoqi@0 391 }
aoqi@0 392
aoqi@0 393 // clearing the flag indicates we are done with
aoqi@0 394 // the ForceEarlyReturn() dance
aoqi@0 395 clr_pending_step_for_earlyret();
aoqi@0 396
aoqi@0 397 // If exception was thrown in this frame, need to reset jvmti thread state.
aoqi@0 398 // Single stepping may not get enabled correctly by the agent since
aoqi@0 399 // exception state is passed in MethodExit event which may be sent at some
aoqi@0 400 // time in the future. JDWP agent ignores MethodExit events if caused by
aoqi@0 401 // an exception.
aoqi@0 402 //
aoqi@0 403 if (is_exception_detected()) {
aoqi@0 404 clear_exception_detected();
aoqi@0 405 }
aoqi@0 406 // If step is pending for earlyret then it may not be a repeat step.
aoqi@0 407 // The new_bci and method_id is same as current_bci and current
aoqi@0 408 // method_id after earlyret and step for recursive calls.
aoqi@0 409 // Force the step by clearing the last location.
aoqi@0 410 JvmtiEnvThreadStateIterator it(this);
aoqi@0 411 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
aoqi@0 412 ets->clear_current_location();
aoqi@0 413 }
aoqi@0 414 }
aoqi@0 415
aoqi@0 416 void JvmtiThreadState::oops_do(OopClosure* f) {
aoqi@0 417 f->do_oop((oop*) &_earlyret_oop);
aoqi@0 418 }

mercurial