1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/prims/jvmtiThreadState.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,394 @@ 1.4 +/* 1.5 + * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_jvmtiThreadState.cpp.incl" 1.30 + 1.31 +// marker for when the stack depth has been reset and is now unknown. 1.32 +// any negative number would work but small ones might obscure an 1.33 +// underrun error. 1.34 +static const int UNKNOWN_STACK_DEPTH = -99; 1.35 + 1.36 +/////////////////////////////////////////////////////////////// 1.37 +// 1.38 +// class JvmtiThreadState 1.39 +// 1.40 +// Instances of JvmtiThreadState hang off of each thread. 1.41 +// Thread local storage for JVMTI. 1.42 +// 1.43 + 1.44 +JvmtiThreadState *JvmtiThreadState::_head = NULL; 1.45 + 1.46 +JvmtiThreadState::JvmtiThreadState(JavaThread* thread) 1.47 + : _thread_event_enable() { 1.48 + assert(JvmtiThreadState_lock->is_locked(), "sanity check"); 1.49 + _thread = thread; 1.50 + _exception_detected = false; 1.51 + _exception_caught = false; 1.52 + _debuggable = true; 1.53 + _hide_single_stepping = false; 1.54 + _hide_level = 0; 1.55 + _pending_step_for_popframe = false; 1.56 + _class_being_redefined = NULL; 1.57 + _class_load_kind = jvmti_class_load_kind_load; 1.58 + _head_env_thread_state = NULL; 1.59 + _dynamic_code_event_collector = NULL; 1.60 + _vm_object_alloc_event_collector = NULL; 1.61 + _the_class_for_redefinition_verification = NULL; 1.62 + _scratch_class_for_redefinition_verification = NULL; 1.63 + 1.64 + // JVMTI ForceEarlyReturn support 1.65 + _pending_step_for_earlyret = false; 1.66 + _earlyret_state = earlyret_inactive; 1.67 + _earlyret_tos = ilgl; 1.68 + _earlyret_value.j = 0L; 1.69 + _earlyret_oop = NULL; 1.70 + 1.71 + // add all the JvmtiEnvThreadState to the new JvmtiThreadState 1.72 + { 1.73 + JvmtiEnvIterator it; 1.74 + for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) { 1.75 + if (env->is_valid()) { 1.76 + add_env(env); 1.77 + } 1.78 + } 1.79 + } 1.80 + 1.81 + // link us into the list 1.82 + { 1.83 + // The thread state list manipulation code must not have safepoints. 1.84 + // See periodic_clean_up(). 1.85 + debug_only(No_Safepoint_Verifier nosafepoint;) 1.86 + 1.87 + _prev = NULL; 1.88 + _next = _head; 1.89 + if (_head != NULL) { 1.90 + _head->_prev = this; 1.91 + } 1.92 + _head = this; 1.93 + } 1.94 + 1.95 + // set this as the state for the thread 1.96 + thread->set_jvmti_thread_state(this); 1.97 +} 1.98 + 1.99 + 1.100 +JvmtiThreadState::~JvmtiThreadState() { 1.101 + assert(JvmtiThreadState_lock->is_locked(), "sanity check"); 1.102 + 1.103 + // clear this as the state for the thread 1.104 + get_thread()->set_jvmti_thread_state(NULL); 1.105 + 1.106 + // zap our env thread states 1.107 + { 1.108 + JvmtiEnvBase::entering_dying_thread_env_iteration(); 1.109 + JvmtiEnvThreadStateIterator it(this); 1.110 + for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ) { 1.111 + JvmtiEnvThreadState* zap = ets; 1.112 + ets = it.next(ets); 1.113 + delete zap; 1.114 + } 1.115 + JvmtiEnvBase::leaving_dying_thread_env_iteration(); 1.116 + } 1.117 + 1.118 + // remove us from the list 1.119 + { 1.120 + // The thread state list manipulation code must not have safepoints. 1.121 + // See periodic_clean_up(). 1.122 + debug_only(No_Safepoint_Verifier nosafepoint;) 1.123 + 1.124 + if (_prev == NULL) { 1.125 + assert(_head == this, "sanity check"); 1.126 + _head = _next; 1.127 + } else { 1.128 + assert(_head != this, "sanity check"); 1.129 + _prev->_next = _next; 1.130 + } 1.131 + if (_next != NULL) { 1.132 + _next->_prev = _prev; 1.133 + } 1.134 + _next = NULL; 1.135 + _prev = NULL; 1.136 + } 1.137 +} 1.138 + 1.139 + 1.140 +void 1.141 +JvmtiThreadState::periodic_clean_up() { 1.142 + assert(SafepointSynchronize::is_at_safepoint(), "at safepoint"); 1.143 + 1.144 + // This iteration is initialized with "_head" instead of "JvmtiThreadState::first()" 1.145 + // because the latter requires the JvmtiThreadState_lock. 1.146 + // This iteration is safe at a safepoint as well, see the No_Safepoint_Verifier 1.147 + // asserts at all list manipulation sites. 1.148 + for (JvmtiThreadState *state = _head; state != NULL; state = state->next()) { 1.149 + // For each environment thread state corresponding to an invalid environment 1.150 + // unlink it from the list and deallocate it. 1.151 + JvmtiEnvThreadStateIterator it(state); 1.152 + JvmtiEnvThreadState* previous_ets = NULL; 1.153 + JvmtiEnvThreadState* ets = it.first(); 1.154 + while (ets != NULL) { 1.155 + if (ets->get_env()->is_valid()) { 1.156 + previous_ets = ets; 1.157 + ets = it.next(ets); 1.158 + } else { 1.159 + // This one isn't valid, remove it from the list and deallocate it 1.160 + JvmtiEnvThreadState* defunct_ets = ets; 1.161 + ets = ets->next(); 1.162 + if (previous_ets == NULL) { 1.163 + assert(state->head_env_thread_state() == defunct_ets, "sanity check"); 1.164 + state->set_head_env_thread_state(ets); 1.165 + } else { 1.166 + previous_ets->set_next(ets); 1.167 + } 1.168 + delete defunct_ets; 1.169 + } 1.170 + } 1.171 + } 1.172 +} 1.173 + 1.174 +void JvmtiThreadState::add_env(JvmtiEnvBase *env) { 1.175 + assert(JvmtiThreadState_lock->is_locked(), "sanity check"); 1.176 + 1.177 + JvmtiEnvThreadState *new_ets = new JvmtiEnvThreadState(_thread, env); 1.178 + // add this environment thread state to the end of the list (order is important) 1.179 + { 1.180 + // list deallocation (which occurs at a safepoint) cannot occur simultaneously 1.181 + debug_only(No_Safepoint_Verifier nosafepoint;) 1.182 + 1.183 + JvmtiEnvThreadStateIterator it(this); 1.184 + JvmtiEnvThreadState* previous_ets = NULL; 1.185 + for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1.186 + previous_ets = ets; 1.187 + } 1.188 + if (previous_ets == NULL) { 1.189 + set_head_env_thread_state(new_ets); 1.190 + } else { 1.191 + previous_ets->set_next(new_ets); 1.192 + } 1.193 + } 1.194 +} 1.195 + 1.196 + 1.197 + 1.198 + 1.199 +void JvmtiThreadState::enter_interp_only_mode() { 1.200 + assert(_thread->get_interp_only_mode() == 0, "entering interp only when mode not zero"); 1.201 + _thread->increment_interp_only_mode(); 1.202 +} 1.203 + 1.204 + 1.205 +void JvmtiThreadState::leave_interp_only_mode() { 1.206 + assert(_thread->get_interp_only_mode() == 1, "leaving interp only when mode not one"); 1.207 + _thread->decrement_interp_only_mode(); 1.208 +} 1.209 + 1.210 + 1.211 +// Helper routine used in several places 1.212 +int JvmtiThreadState::count_frames() { 1.213 +#ifdef ASSERT 1.214 + uint32_t debug_bits = 0; 1.215 +#endif 1.216 + assert(SafepointSynchronize::is_at_safepoint() || 1.217 + JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), 1.218 + "at safepoint or must be suspended"); 1.219 + 1.220 + if (!get_thread()->has_last_Java_frame()) return 0; // no Java frames 1.221 + 1.222 + ResourceMark rm; 1.223 + RegisterMap reg_map(get_thread()); 1.224 + javaVFrame *jvf = get_thread()->last_java_vframe(®_map); 1.225 + int n = 0; 1.226 + // tty->print_cr("CSD: counting frames on %s ...", 1.227 + // JvmtiTrace::safe_get_thread_name(get_thread())); 1.228 + while (jvf != NULL) { 1.229 + methodOop method = jvf->method(); 1.230 + // tty->print_cr("CSD: frame - method %s.%s - loc %d", 1.231 + // method->klass_name()->as_C_string(), 1.232 + // method->name()->as_C_string(), 1.233 + // jvf->bci() ); 1.234 + jvf = jvf->java_sender(); 1.235 + n++; 1.236 + } 1.237 + // tty->print_cr("CSD: frame count: %d", n); 1.238 + return n; 1.239 +} 1.240 + 1.241 + 1.242 +void JvmtiThreadState::invalidate_cur_stack_depth() { 1.243 + Thread *cur = Thread::current(); 1.244 + uint32_t debug_bits = 0; 1.245 + 1.246 + // The caller can be the VMThread at a safepoint, the current thread 1.247 + // or the target thread must be suspended. 1.248 + guarantee((cur->is_VM_thread() && SafepointSynchronize::is_at_safepoint()) || 1.249 + (JavaThread *)cur == get_thread() || 1.250 + JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), 1.251 + "sanity check"); 1.252 + 1.253 + _cur_stack_depth = UNKNOWN_STACK_DEPTH; 1.254 +} 1.255 + 1.256 +void JvmtiThreadState::incr_cur_stack_depth() { 1.257 + guarantee(JavaThread::current() == get_thread(), "must be current thread"); 1.258 + 1.259 + if (!is_interp_only_mode()) { 1.260 + _cur_stack_depth = UNKNOWN_STACK_DEPTH; 1.261 + } 1.262 + if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) { 1.263 + ++_cur_stack_depth; 1.264 + } 1.265 +} 1.266 + 1.267 +void JvmtiThreadState::decr_cur_stack_depth() { 1.268 + guarantee(JavaThread::current() == get_thread(), "must be current thread"); 1.269 + 1.270 + if (!is_interp_only_mode()) { 1.271 + _cur_stack_depth = UNKNOWN_STACK_DEPTH; 1.272 + } 1.273 + if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) { 1.274 + --_cur_stack_depth; 1.275 + assert(_cur_stack_depth >= 0, "incr/decr_cur_stack_depth mismatch"); 1.276 + } 1.277 +} 1.278 + 1.279 +int JvmtiThreadState::cur_stack_depth() { 1.280 + uint32_t debug_bits = 0; 1.281 + guarantee(JavaThread::current() == get_thread() || 1.282 + JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), 1.283 + "must be current thread or suspended"); 1.284 + 1.285 + if (!is_interp_only_mode() || _cur_stack_depth == UNKNOWN_STACK_DEPTH) { 1.286 + _cur_stack_depth = count_frames(); 1.287 + } else { 1.288 + // heavy weight assert 1.289 + assert(_cur_stack_depth == count_frames(), 1.290 + "cur_stack_depth out of sync"); 1.291 + } 1.292 + return _cur_stack_depth; 1.293 +} 1.294 + 1.295 +bool JvmtiThreadState::may_be_walked() { 1.296 + return (get_thread()->is_being_ext_suspended() || (JavaThread::current() == get_thread())); 1.297 +} 1.298 + 1.299 + 1.300 +void JvmtiThreadState::process_pending_step_for_popframe() { 1.301 + // We are single stepping as the last part of the PopFrame() dance 1.302 + // so we have some house keeping to do. 1.303 + 1.304 + JavaThread *thr = get_thread(); 1.305 + if (thr->popframe_condition() != JavaThread::popframe_inactive) { 1.306 + // If the popframe_condition field is not popframe_inactive, then 1.307 + // we missed all of the popframe_field cleanup points: 1.308 + // 1.309 + // - unpack_frames() was not called (nothing to deopt) 1.310 + // - remove_activation_preserving_args_entry() was not called 1.311 + // (did not get suspended in a call_vm() family call and did 1.312 + // not complete a call_vm() family call on the way here) 1.313 + thr->clear_popframe_condition(); 1.314 + } 1.315 + 1.316 + // clearing the flag indicates we are done with the PopFrame() dance 1.317 + clr_pending_step_for_popframe(); 1.318 + 1.319 + // If step is pending for popframe then it may not be 1.320 + // a repeat step. The new_bci and method_id is same as current_bci 1.321 + // and current method_id after pop and step for recursive calls. 1.322 + // Force the step by clearing the last location. 1.323 + JvmtiEnvThreadStateIterator it(this); 1.324 + for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1.325 + ets->clear_current_location(); 1.326 + } 1.327 +} 1.328 + 1.329 + 1.330 +// Class: JvmtiThreadState 1.331 +// Function: update_for_pop_top_frame 1.332 +// Description: 1.333 +// This function removes any frame pop notification request for 1.334 +// the top frame and invalidates both the current stack depth and 1.335 +// all cached frameIDs. 1.336 +// 1.337 +// Called by: PopFrame 1.338 +// 1.339 +void JvmtiThreadState::update_for_pop_top_frame() { 1.340 + if (is_interp_only_mode()) { 1.341 + // remove any frame pop notification request for the top frame 1.342 + // in any environment 1.343 + int popframe_number = cur_stack_depth(); 1.344 + { 1.345 + JvmtiEnvThreadStateIterator it(this); 1.346 + for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1.347 + if (ets->is_frame_pop(popframe_number)) { 1.348 + ets->clear_frame_pop(popframe_number); 1.349 + } 1.350 + } 1.351 + } 1.352 + // force stack depth to be recalculated 1.353 + invalidate_cur_stack_depth(); 1.354 + } else { 1.355 + assert(!is_enabled(JVMTI_EVENT_FRAME_POP), "Must have no framepops set"); 1.356 + } 1.357 +} 1.358 + 1.359 + 1.360 +void JvmtiThreadState::process_pending_step_for_earlyret() { 1.361 + // We are single stepping as the last part of the ForceEarlyReturn 1.362 + // dance so we have some house keeping to do. 1.363 + 1.364 + if (is_earlyret_pending()) { 1.365 + // If the earlyret_state field is not earlyret_inactive, then 1.366 + // we missed all of the earlyret_field cleanup points: 1.367 + // 1.368 + // - remove_activation() was not called 1.369 + // (did not get suspended in a call_vm() family call and did 1.370 + // not complete a call_vm() family call on the way here) 1.371 + // 1.372 + // One legitimate way for us to miss all the cleanup points is 1.373 + // if we got here right after handling a compiled return. If that 1.374 + // is the case, then we consider our return from compiled code to 1.375 + // complete the ForceEarlyReturn request and we clear the condition. 1.376 + clr_earlyret_pending(); 1.377 + set_earlyret_oop(NULL); 1.378 + clr_earlyret_value(); 1.379 + } 1.380 + 1.381 + // clearing the flag indicates we are done with 1.382 + // the ForceEarlyReturn() dance 1.383 + clr_pending_step_for_earlyret(); 1.384 + 1.385 + // If step is pending for earlyret then it may not be a repeat step. 1.386 + // The new_bci and method_id is same as current_bci and current 1.387 + // method_id after earlyret and step for recursive calls. 1.388 + // Force the step by clearing the last location. 1.389 + JvmtiEnvThreadStateIterator it(this); 1.390 + for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { 1.391 + ets->clear_current_location(); 1.392 + } 1.393 +} 1.394 + 1.395 +void JvmtiThreadState::oops_do(OopClosure* f) { 1.396 + f->do_oop((oop*) &_earlyret_oop); 1.397 +}