src/share/vm/prims/jvmtiEnvThreadState.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, 2014, 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 "classfile/systemDictionary.hpp"
aoqi@0 27 #include "interpreter/interpreter.hpp"
aoqi@0 28 #include "jvmtifiles/jvmtiEnv.hpp"
aoqi@0 29 #include "memory/resourceArea.hpp"
aoqi@0 30 #include "prims/jvmtiEnvThreadState.hpp"
aoqi@0 31 #include "prims/jvmtiEventController.inline.hpp"
aoqi@0 32 #include "prims/jvmtiImpl.hpp"
aoqi@0 33 #include "runtime/handles.hpp"
aoqi@0 34 #include "runtime/handles.inline.hpp"
aoqi@0 35 #include "runtime/interfaceSupport.hpp"
aoqi@0 36 #include "runtime/javaCalls.hpp"
aoqi@0 37 #include "runtime/signature.hpp"
aoqi@0 38 #include "runtime/vframe.hpp"
aoqi@0 39 #include "runtime/vm_operations.hpp"
aoqi@0 40
aoqi@0 41
aoqi@0 42 ///////////////////////////////////////////////////////////////
aoqi@0 43 //
aoqi@0 44 // class JvmtiFramePop
aoqi@0 45 //
aoqi@0 46
aoqi@0 47 #ifndef PRODUCT
aoqi@0 48 void JvmtiFramePop::print() {
aoqi@0 49 tty->print_cr("_frame_number=%d", _frame_number);
aoqi@0 50 }
aoqi@0 51 #endif
aoqi@0 52
aoqi@0 53
aoqi@0 54 ///////////////////////////////////////////////////////////////
aoqi@0 55 //
aoqi@0 56 // class JvmtiFramePops - private methods
aoqi@0 57 //
aoqi@0 58
aoqi@0 59 void
aoqi@0 60 JvmtiFramePops::set(JvmtiFramePop& fp) {
aoqi@0 61 if (_pops->find(fp.frame_number()) < 0) {
aoqi@0 62 _pops->append(fp.frame_number());
aoqi@0 63 }
aoqi@0 64 }
aoqi@0 65
aoqi@0 66
aoqi@0 67 void
aoqi@0 68 JvmtiFramePops::clear(JvmtiFramePop& fp) {
aoqi@0 69 assert(_pops->length() > 0, "No more frame pops");
aoqi@0 70
aoqi@0 71 _pops->remove(fp.frame_number());
aoqi@0 72 }
aoqi@0 73
aoqi@0 74
aoqi@0 75 int
aoqi@0 76 JvmtiFramePops::clear_to(JvmtiFramePop& fp) {
aoqi@0 77 int cleared = 0;
aoqi@0 78 int index = 0;
aoqi@0 79 while (index < _pops->length()) {
aoqi@0 80 JvmtiFramePop pop = JvmtiFramePop(_pops->at(index));
aoqi@0 81 if (pop.above_on_stack(fp)) {
aoqi@0 82 _pops->remove_at(index);
aoqi@0 83 ++cleared;
aoqi@0 84 } else {
aoqi@0 85 ++index;
aoqi@0 86 }
aoqi@0 87 }
aoqi@0 88 return cleared;
aoqi@0 89 }
aoqi@0 90
aoqi@0 91
aoqi@0 92 ///////////////////////////////////////////////////////////////
aoqi@0 93 //
aoqi@0 94 // class JvmtiFramePops - public methods
aoqi@0 95 //
aoqi@0 96
aoqi@0 97 JvmtiFramePops::JvmtiFramePops() {
aoqi@0 98 _pops = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int> (2, true);
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 JvmtiFramePops::~JvmtiFramePops() {
aoqi@0 102 // return memory to c_heap.
aoqi@0 103 delete _pops;
aoqi@0 104 }
aoqi@0 105
aoqi@0 106
aoqi@0 107 #ifndef PRODUCT
aoqi@0 108 void JvmtiFramePops::print() {
aoqi@0 109 ResourceMark rm;
aoqi@0 110
aoqi@0 111 int n = _pops->length();
aoqi@0 112 for (int i=0; i<n; i++) {
aoqi@0 113 JvmtiFramePop fp = JvmtiFramePop(_pops->at(i));
aoqi@0 114 tty->print("%d: ", i);
aoqi@0 115 fp.print();
aoqi@0 116 tty->cr();
aoqi@0 117 }
aoqi@0 118 }
aoqi@0 119 #endif
aoqi@0 120
aoqi@0 121 ///////////////////////////////////////////////////////////////
aoqi@0 122 //
aoqi@0 123 // class JvmtiEnvThreadState
aoqi@0 124 //
aoqi@0 125 // Instances of JvmtiEnvThreadState hang off of each JvmtiThreadState,
aoqi@0 126 // one per JvmtiEnv.
aoqi@0 127 //
aoqi@0 128
aoqi@0 129 JvmtiEnvThreadState::JvmtiEnvThreadState(JavaThread *thread, JvmtiEnvBase *env) :
aoqi@0 130 _event_enable() {
aoqi@0 131 _thread = thread;
aoqi@0 132 _env = (JvmtiEnv*)env;
aoqi@0 133 _next = NULL;
aoqi@0 134 _frame_pops = NULL;
aoqi@0 135 _current_bci = 0;
aoqi@0 136 _current_method_id = NULL;
aoqi@0 137 _breakpoint_posted = false;
aoqi@0 138 _single_stepping_posted = false;
aoqi@0 139 _agent_thread_local_storage_data = NULL;
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 JvmtiEnvThreadState::~JvmtiEnvThreadState() {
aoqi@0 143 delete _frame_pops;
aoqi@0 144 _frame_pops = NULL;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 // Given that a new (potential) event has come in,
aoqi@0 148 // maintain the current JVMTI location on a per-thread per-env basis
aoqi@0 149 // and use it to filter out duplicate events:
aoqi@0 150 // - instruction rewrites
aoqi@0 151 // - breakpoint followed by single step
aoqi@0 152 // - single step at a breakpoint
aoqi@0 153 void JvmtiEnvThreadState::compare_and_set_current_location(Method* new_method,
aoqi@0 154 address new_location, jvmtiEvent event) {
aoqi@0 155
aoqi@0 156 int new_bci = new_location - new_method->code_base();
aoqi@0 157
aoqi@0 158 // The method is identified and stored as a jmethodID which is safe in this
aoqi@0 159 // case because the class cannot be unloaded while a method is executing.
aoqi@0 160 jmethodID new_method_id = new_method->jmethod_id();
aoqi@0 161
aoqi@0 162 // the last breakpoint or single step was at this same location
aoqi@0 163 if (_current_bci == new_bci && _current_method_id == new_method_id) {
aoqi@0 164 switch (event) {
aoqi@0 165 case JVMTI_EVENT_BREAKPOINT:
aoqi@0 166 // Repeat breakpoint is complicated. If we previously posted a breakpoint
aoqi@0 167 // event at this location and if we also single stepped at this location
aoqi@0 168 // then we skip the duplicate breakpoint.
aoqi@0 169 _breakpoint_posted = _breakpoint_posted && _single_stepping_posted;
aoqi@0 170 break;
aoqi@0 171 case JVMTI_EVENT_SINGLE_STEP:
aoqi@0 172 // Repeat single step is easy: just don't post it again.
aoqi@0 173 // If step is pending for popframe then it may not be
aoqi@0 174 // a repeat step. The new_bci and method_id is same as current_bci
aoqi@0 175 // and current method_id after pop and step for recursive calls.
aoqi@0 176 // This has been handled by clearing the location
aoqi@0 177 _single_stepping_posted = true;
aoqi@0 178 break;
aoqi@0 179 default:
aoqi@0 180 assert(false, "invalid event value passed");
aoqi@0 181 break;
aoqi@0 182 }
aoqi@0 183 return;
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 set_current_location(new_method_id, new_bci);
aoqi@0 187 _breakpoint_posted = false;
aoqi@0 188 _single_stepping_posted = false;
aoqi@0 189 }
aoqi@0 190
aoqi@0 191
aoqi@0 192 JvmtiFramePops* JvmtiEnvThreadState::get_frame_pops() {
aoqi@0 193 #ifdef ASSERT
aoqi@0 194 uint32_t debug_bits = 0;
aoqi@0 195 #endif
aoqi@0 196 assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
aoqi@0 197 "frame pop data only accessible from same thread or while suspended");
aoqi@0 198
aoqi@0 199 if (_frame_pops == NULL) {
aoqi@0 200 _frame_pops = new JvmtiFramePops();
aoqi@0 201 assert(_frame_pops != NULL, "_frame_pops != NULL");
aoqi@0 202 }
aoqi@0 203 return _frame_pops;
aoqi@0 204 }
aoqi@0 205
aoqi@0 206
aoqi@0 207 bool JvmtiEnvThreadState::has_frame_pops() {
aoqi@0 208 return _frame_pops == NULL? false : (_frame_pops->length() > 0);
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 void JvmtiEnvThreadState::set_frame_pop(int frame_number) {
aoqi@0 212 #ifdef ASSERT
aoqi@0 213 uint32_t debug_bits = 0;
aoqi@0 214 #endif
aoqi@0 215 assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
aoqi@0 216 "frame pop data only accessible from same thread or while suspended");
aoqi@0 217 JvmtiFramePop fpop(frame_number);
aoqi@0 218 JvmtiEventController::set_frame_pop(this, fpop);
aoqi@0 219 }
aoqi@0 220
aoqi@0 221
aoqi@0 222 void JvmtiEnvThreadState::clear_frame_pop(int frame_number) {
aoqi@0 223 #ifdef ASSERT
aoqi@0 224 uint32_t debug_bits = 0;
aoqi@0 225 #endif
aoqi@0 226 assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
aoqi@0 227 "frame pop data only accessible from same thread or while suspended");
aoqi@0 228 JvmtiFramePop fpop(frame_number);
aoqi@0 229 JvmtiEventController::clear_frame_pop(this, fpop);
aoqi@0 230 }
aoqi@0 231
aoqi@0 232
aoqi@0 233 void JvmtiEnvThreadState::clear_to_frame_pop(int frame_number) {
aoqi@0 234 #ifdef ASSERT
aoqi@0 235 uint32_t debug_bits = 0;
aoqi@0 236 #endif
aoqi@0 237 assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
aoqi@0 238 "frame pop data only accessible from same thread or while suspended");
aoqi@0 239 JvmtiFramePop fpop(frame_number);
aoqi@0 240 JvmtiEventController::clear_to_frame_pop(this, fpop);
aoqi@0 241 }
aoqi@0 242
aoqi@0 243
aoqi@0 244 bool JvmtiEnvThreadState::is_frame_pop(int cur_frame_number) {
aoqi@0 245 #ifdef ASSERT
aoqi@0 246 uint32_t debug_bits = 0;
aoqi@0 247 #endif
aoqi@0 248 assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
aoqi@0 249 "frame pop data only accessible from same thread or while suspended");
aoqi@0 250 if (!get_thread()->is_interp_only_mode() || _frame_pops == NULL) {
aoqi@0 251 return false;
aoqi@0 252 }
aoqi@0 253 JvmtiFramePop fp(cur_frame_number);
aoqi@0 254 return get_frame_pops()->contains(fp);
aoqi@0 255 }
aoqi@0 256
aoqi@0 257
aoqi@0 258 class VM_GetCurrentLocation : public VM_Operation {
aoqi@0 259 private:
aoqi@0 260 JavaThread *_thread;
aoqi@0 261 jmethodID _method_id;
aoqi@0 262 int _bci;
aoqi@0 263
aoqi@0 264 public:
aoqi@0 265 VM_GetCurrentLocation(JavaThread *thread) {
aoqi@0 266 _thread = thread;
aoqi@0 267 }
aoqi@0 268 VMOp_Type type() const { return VMOp_GetCurrentLocation; }
aoqi@0 269 void doit() {
aoqi@0 270 ResourceMark rmark; // _thread != Thread::current()
aoqi@0 271 RegisterMap rm(_thread, false);
aoqi@0 272 // There can be a race condition between a VM_Operation reaching a safepoint
aoqi@0 273 // and the target thread exiting from Java execution.
aoqi@0 274 // We must recheck the last Java frame still exists.
aoqi@0 275 if (!_thread->is_exiting() && _thread->has_last_Java_frame()) {
aoqi@0 276 javaVFrame* vf = _thread->last_java_vframe(&rm);
aoqi@0 277 assert(vf != NULL, "must have last java frame");
aoqi@0 278 Method* method = vf->method();
aoqi@0 279 _method_id = method->jmethod_id();
aoqi@0 280 _bci = vf->bci();
aoqi@0 281 } else {
aoqi@0 282 // Clear current location as the target thread has no Java frames anymore.
aoqi@0 283 _method_id = (jmethodID)NULL;
aoqi@0 284 _bci = 0;
aoqi@0 285 }
aoqi@0 286 }
aoqi@0 287 void get_current_location(jmethodID *method_id, int *bci) {
aoqi@0 288 *method_id = _method_id;
aoqi@0 289 *bci = _bci;
aoqi@0 290 }
aoqi@0 291 };
aoqi@0 292
aoqi@0 293 void JvmtiEnvThreadState::reset_current_location(jvmtiEvent event_type, bool enabled) {
aoqi@0 294 assert(event_type == JVMTI_EVENT_SINGLE_STEP || event_type == JVMTI_EVENT_BREAKPOINT,
aoqi@0 295 "must be single-step or breakpoint event");
aoqi@0 296
aoqi@0 297 // Current location is used to detect the following:
aoqi@0 298 // 1) a breakpoint event followed by single-stepping to the same bci
aoqi@0 299 // 2) single-step to a bytecode that will be transformed to a fast version
aoqi@0 300 // We skip to avoid posting the duplicate single-stepping event.
aoqi@0 301
aoqi@0 302 // If single-stepping is disabled, clear current location so that
aoqi@0 303 // single-stepping to the same method and bcp at a later time will be
aoqi@0 304 // detected if single-stepping is enabled at that time (see 4388912).
aoqi@0 305
aoqi@0 306 // If single-stepping is enabled, set the current location to the
aoqi@0 307 // current method and bcp. This covers the following type of case,
aoqi@0 308 // e.g., the debugger stepi command:
aoqi@0 309 // - bytecode single stepped
aoqi@0 310 // - SINGLE_STEP event posted and SINGLE_STEP event disabled
aoqi@0 311 // - SINGLE_STEP event reenabled
aoqi@0 312 // - bytecode rewritten to fast version
aoqi@0 313
aoqi@0 314 // If breakpoint event is disabled, clear current location only if
aoqi@0 315 // single-stepping is not enabled. Otherwise, keep the thread location
aoqi@0 316 // to detect any duplicate events.
aoqi@0 317
aoqi@0 318 if (enabled) {
aoqi@0 319 // If enabling breakpoint, no need to reset.
aoqi@0 320 // Can't do anything if empty stack.
aoqi@0 321 if (event_type == JVMTI_EVENT_SINGLE_STEP && _thread->has_last_Java_frame()) {
aoqi@0 322 jmethodID method_id;
aoqi@0 323 int bci;
aoqi@0 324 // The java thread stack may not be walkable for a running thread
aoqi@0 325 // so get current location at safepoint.
aoqi@0 326 VM_GetCurrentLocation op(_thread);
aoqi@0 327 VMThread::execute(&op);
aoqi@0 328 op.get_current_location(&method_id, &bci);
aoqi@0 329 set_current_location(method_id, bci);
aoqi@0 330 }
aoqi@0 331 } else if (event_type == JVMTI_EVENT_SINGLE_STEP || !is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
aoqi@0 332 // If this is to disable breakpoint, also check if single-step is not enabled
aoqi@0 333 clear_current_location();
aoqi@0 334 }
aoqi@0 335 }

mercurial