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