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