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