src/share/vm/prims/jvmtiThreadState.cpp

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

mercurial