src/share/vm/prims/jvmtiEnvThreadState.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/jvmtiEnvThreadState.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,335 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2014, 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 "classfile/systemDictionary.hpp"
    1.30 +#include "interpreter/interpreter.hpp"
    1.31 +#include "jvmtifiles/jvmtiEnv.hpp"
    1.32 +#include "memory/resourceArea.hpp"
    1.33 +#include "prims/jvmtiEnvThreadState.hpp"
    1.34 +#include "prims/jvmtiEventController.inline.hpp"
    1.35 +#include "prims/jvmtiImpl.hpp"
    1.36 +#include "runtime/handles.hpp"
    1.37 +#include "runtime/handles.inline.hpp"
    1.38 +#include "runtime/interfaceSupport.hpp"
    1.39 +#include "runtime/javaCalls.hpp"
    1.40 +#include "runtime/signature.hpp"
    1.41 +#include "runtime/vframe.hpp"
    1.42 +#include "runtime/vm_operations.hpp"
    1.43 +
    1.44 +
    1.45 +///////////////////////////////////////////////////////////////
    1.46 +//
    1.47 +// class JvmtiFramePop
    1.48 +//
    1.49 +
    1.50 +#ifndef PRODUCT
    1.51 +void JvmtiFramePop::print() {
    1.52 +  tty->print_cr("_frame_number=%d", _frame_number);
    1.53 +}
    1.54 +#endif
    1.55 +
    1.56 +
    1.57 +///////////////////////////////////////////////////////////////
    1.58 +//
    1.59 +// class JvmtiFramePops - private methods
    1.60 +//
    1.61 +
    1.62 +void
    1.63 +JvmtiFramePops::set(JvmtiFramePop& fp) {
    1.64 +  if (_pops->find(fp.frame_number()) < 0) {
    1.65 +    _pops->append(fp.frame_number());
    1.66 +  }
    1.67 +}
    1.68 +
    1.69 +
    1.70 +void
    1.71 +JvmtiFramePops::clear(JvmtiFramePop& fp) {
    1.72 +  assert(_pops->length() > 0, "No more frame pops");
    1.73 +
    1.74 +  _pops->remove(fp.frame_number());
    1.75 +}
    1.76 +
    1.77 +
    1.78 +int
    1.79 +JvmtiFramePops::clear_to(JvmtiFramePop& fp) {
    1.80 +  int cleared = 0;
    1.81 +  int index = 0;
    1.82 +  while (index < _pops->length()) {
    1.83 +    JvmtiFramePop pop = JvmtiFramePop(_pops->at(index));
    1.84 +    if (pop.above_on_stack(fp)) {
    1.85 +      _pops->remove_at(index);
    1.86 +      ++cleared;
    1.87 +    } else {
    1.88 +      ++index;
    1.89 +    }
    1.90 +  }
    1.91 +  return cleared;
    1.92 +}
    1.93 +
    1.94 +
    1.95 +///////////////////////////////////////////////////////////////
    1.96 +//
    1.97 +// class JvmtiFramePops - public methods
    1.98 +//
    1.99 +
   1.100 +JvmtiFramePops::JvmtiFramePops() {
   1.101 +  _pops = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int> (2, true);
   1.102 +}
   1.103 +
   1.104 +JvmtiFramePops::~JvmtiFramePops() {
   1.105 +  // return memory to c_heap.
   1.106 +  delete _pops;
   1.107 +}
   1.108 +
   1.109 +
   1.110 +#ifndef PRODUCT
   1.111 +void JvmtiFramePops::print() {
   1.112 +  ResourceMark rm;
   1.113 +
   1.114 +  int n = _pops->length();
   1.115 +  for (int i=0; i<n; i++) {
   1.116 +    JvmtiFramePop fp = JvmtiFramePop(_pops->at(i));
   1.117 +    tty->print("%d: ", i);
   1.118 +    fp.print();
   1.119 +    tty->cr();
   1.120 +  }
   1.121 +}
   1.122 +#endif
   1.123 +
   1.124 +///////////////////////////////////////////////////////////////
   1.125 +//
   1.126 +// class JvmtiEnvThreadState
   1.127 +//
   1.128 +// Instances of JvmtiEnvThreadState hang off of each JvmtiThreadState,
   1.129 +// one per JvmtiEnv.
   1.130 +//
   1.131 +
   1.132 +JvmtiEnvThreadState::JvmtiEnvThreadState(JavaThread *thread, JvmtiEnvBase *env) :
   1.133 +  _event_enable() {
   1.134 +  _thread                 = thread;
   1.135 +  _env                    = (JvmtiEnv*)env;
   1.136 +  _next                   = NULL;
   1.137 +  _frame_pops             = NULL;
   1.138 +  _current_bci            = 0;
   1.139 +  _current_method_id      = NULL;
   1.140 +  _breakpoint_posted      = false;
   1.141 +  _single_stepping_posted = false;
   1.142 +  _agent_thread_local_storage_data = NULL;
   1.143 +}
   1.144 +
   1.145 +JvmtiEnvThreadState::~JvmtiEnvThreadState()   {
   1.146 +  delete _frame_pops;
   1.147 +  _frame_pops = NULL;
   1.148 +}
   1.149 +
   1.150 +// Given that a new (potential) event has come in,
   1.151 +// maintain the current JVMTI location on a per-thread per-env basis
   1.152 +// and use it to filter out duplicate events:
   1.153 +// - instruction rewrites
   1.154 +// - breakpoint followed by single step
   1.155 +// - single step at a breakpoint
   1.156 +void JvmtiEnvThreadState::compare_and_set_current_location(Method* new_method,
   1.157 +                                                           address new_location, jvmtiEvent event) {
   1.158 +
   1.159 +  int new_bci = new_location - new_method->code_base();
   1.160 +
   1.161 +  // The method is identified and stored as a jmethodID which is safe in this
   1.162 +  // case because the class cannot be unloaded while a method is executing.
   1.163 +  jmethodID new_method_id = new_method->jmethod_id();
   1.164 +
   1.165 +  // the last breakpoint or single step was at this same location
   1.166 +  if (_current_bci == new_bci && _current_method_id == new_method_id) {
   1.167 +    switch (event) {
   1.168 +    case JVMTI_EVENT_BREAKPOINT:
   1.169 +      // Repeat breakpoint is complicated. If we previously posted a breakpoint
   1.170 +      // event at this location and if we also single stepped at this location
   1.171 +      // then we skip the duplicate breakpoint.
   1.172 +      _breakpoint_posted = _breakpoint_posted && _single_stepping_posted;
   1.173 +      break;
   1.174 +    case JVMTI_EVENT_SINGLE_STEP:
   1.175 +      // Repeat single step is easy: just don't post it again.
   1.176 +      // If step is pending for popframe then it may not be
   1.177 +      // a repeat step. The new_bci and method_id is same as current_bci
   1.178 +      // and current method_id after pop and step for recursive calls.
   1.179 +      // This has been handled by clearing the location
   1.180 +      _single_stepping_posted = true;
   1.181 +      break;
   1.182 +    default:
   1.183 +      assert(false, "invalid event value passed");
   1.184 +      break;
   1.185 +    }
   1.186 +    return;
   1.187 +  }
   1.188 +
   1.189 +  set_current_location(new_method_id, new_bci);
   1.190 +  _breakpoint_posted = false;
   1.191 +  _single_stepping_posted = false;
   1.192 +}
   1.193 +
   1.194 +
   1.195 +JvmtiFramePops* JvmtiEnvThreadState::get_frame_pops() {
   1.196 +#ifdef ASSERT
   1.197 +  uint32_t debug_bits = 0;
   1.198 +#endif
   1.199 +  assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
   1.200 +         "frame pop data only accessible from same thread or while suspended");
   1.201 +
   1.202 +  if (_frame_pops == NULL) {
   1.203 +    _frame_pops = new JvmtiFramePops();
   1.204 +    assert(_frame_pops != NULL, "_frame_pops != NULL");
   1.205 +  }
   1.206 +  return _frame_pops;
   1.207 +}
   1.208 +
   1.209 +
   1.210 +bool JvmtiEnvThreadState::has_frame_pops() {
   1.211 +  return _frame_pops == NULL? false : (_frame_pops->length() > 0);
   1.212 +}
   1.213 +
   1.214 +void JvmtiEnvThreadState::set_frame_pop(int frame_number) {
   1.215 +#ifdef ASSERT
   1.216 +  uint32_t debug_bits = 0;
   1.217 +#endif
   1.218 +  assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
   1.219 +         "frame pop data only accessible from same thread or while suspended");
   1.220 +  JvmtiFramePop fpop(frame_number);
   1.221 +  JvmtiEventController::set_frame_pop(this, fpop);
   1.222 +}
   1.223 +
   1.224 +
   1.225 +void JvmtiEnvThreadState::clear_frame_pop(int frame_number) {
   1.226 +#ifdef ASSERT
   1.227 +  uint32_t debug_bits = 0;
   1.228 +#endif
   1.229 +  assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
   1.230 +         "frame pop data only accessible from same thread or while suspended");
   1.231 +  JvmtiFramePop fpop(frame_number);
   1.232 +  JvmtiEventController::clear_frame_pop(this, fpop);
   1.233 +}
   1.234 +
   1.235 +
   1.236 +void JvmtiEnvThreadState::clear_to_frame_pop(int frame_number)  {
   1.237 +#ifdef ASSERT
   1.238 +  uint32_t debug_bits = 0;
   1.239 +#endif
   1.240 +  assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
   1.241 +         "frame pop data only accessible from same thread or while suspended");
   1.242 +  JvmtiFramePop fpop(frame_number);
   1.243 +  JvmtiEventController::clear_to_frame_pop(this, fpop);
   1.244 +}
   1.245 +
   1.246 +
   1.247 +bool JvmtiEnvThreadState::is_frame_pop(int cur_frame_number) {
   1.248 +#ifdef ASSERT
   1.249 +  uint32_t debug_bits = 0;
   1.250 +#endif
   1.251 +  assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
   1.252 +         "frame pop data only accessible from same thread or while suspended");
   1.253 +  if (!get_thread()->is_interp_only_mode() || _frame_pops == NULL) {
   1.254 +    return false;
   1.255 +  }
   1.256 +  JvmtiFramePop fp(cur_frame_number);
   1.257 +  return get_frame_pops()->contains(fp);
   1.258 +}
   1.259 +
   1.260 +
   1.261 +class VM_GetCurrentLocation : public VM_Operation {
   1.262 + private:
   1.263 +   JavaThread *_thread;
   1.264 +   jmethodID _method_id;
   1.265 +   int _bci;
   1.266 +
   1.267 + public:
   1.268 +  VM_GetCurrentLocation(JavaThread *thread) {
   1.269 +     _thread = thread;
   1.270 +   }
   1.271 +  VMOp_Type type() const { return VMOp_GetCurrentLocation; }
   1.272 +  void doit() {
   1.273 +    ResourceMark rmark; // _thread != Thread::current()
   1.274 +    RegisterMap rm(_thread, false);
   1.275 +    // There can be a race condition between a VM_Operation reaching a safepoint
   1.276 +    // and the target thread exiting from Java execution.
   1.277 +    // We must recheck the last Java frame still exists.
   1.278 +    if (!_thread->is_exiting() && _thread->has_last_Java_frame()) {
   1.279 +      javaVFrame* vf = _thread->last_java_vframe(&rm);
   1.280 +      assert(vf != NULL, "must have last java frame");
   1.281 +      Method* method = vf->method();
   1.282 +      _method_id = method->jmethod_id();
   1.283 +      _bci = vf->bci();
   1.284 +    } else {
   1.285 +      // Clear current location as the target thread has no Java frames anymore.
   1.286 +      _method_id = (jmethodID)NULL;
   1.287 +      _bci = 0;
   1.288 +    }
   1.289 +  }
   1.290 +  void get_current_location(jmethodID *method_id, int *bci) {
   1.291 +    *method_id = _method_id;
   1.292 +    *bci = _bci;
   1.293 +  }
   1.294 +};
   1.295 +
   1.296 +void JvmtiEnvThreadState::reset_current_location(jvmtiEvent event_type, bool enabled) {
   1.297 +  assert(event_type == JVMTI_EVENT_SINGLE_STEP || event_type == JVMTI_EVENT_BREAKPOINT,
   1.298 +         "must be single-step or breakpoint event");
   1.299 +
   1.300 +  // Current location is used to detect the following:
   1.301 +  // 1) a breakpoint event followed by single-stepping to the same bci
   1.302 +  // 2) single-step to a bytecode that will be transformed to a fast version
   1.303 +  // We skip to avoid posting the duplicate single-stepping event.
   1.304 +
   1.305 +  // If single-stepping is disabled, clear current location so that
   1.306 +  // single-stepping to the same method and bcp at a later time will be
   1.307 +  // detected if single-stepping is enabled at that time (see 4388912).
   1.308 +
   1.309 +  // If single-stepping is enabled, set the current location to the
   1.310 +  // current method and bcp. This covers the following type of case,
   1.311 +  // e.g., the debugger stepi command:
   1.312 +  // - bytecode single stepped
   1.313 +  // - SINGLE_STEP event posted and SINGLE_STEP event disabled
   1.314 +  // - SINGLE_STEP event reenabled
   1.315 +  // - bytecode rewritten to fast version
   1.316 +
   1.317 +  // If breakpoint event is disabled, clear current location only if
   1.318 +  // single-stepping is not enabled.  Otherwise, keep the thread location
   1.319 +  // to detect any duplicate events.
   1.320 +
   1.321 +  if (enabled) {
   1.322 +    // If enabling breakpoint, no need to reset.
   1.323 +    // Can't do anything if empty stack.
   1.324 +    if (event_type == JVMTI_EVENT_SINGLE_STEP && _thread->has_last_Java_frame()) {
   1.325 +      jmethodID method_id;
   1.326 +      int bci;
   1.327 +      // The java thread stack may not be walkable for a running thread
   1.328 +      // so get current location at safepoint.
   1.329 +      VM_GetCurrentLocation op(_thread);
   1.330 +      VMThread::execute(&op);
   1.331 +      op.get_current_location(&method_id, &bci);
   1.332 +      set_current_location(method_id, bci);
   1.333 +    }
   1.334 +  } else if (event_type == JVMTI_EVENT_SINGLE_STEP || !is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
   1.335 +    // If this is to disable breakpoint, also check if single-step is not enabled
   1.336 +    clear_current_location();
   1.337 +  }
   1.338 +}

mercurial