aoqi@0: /* aoqi@0: * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "interpreter/interpreter.hpp" aoqi@0: #include "jvmtifiles/jvmtiEnv.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "prims/jvmtiEnvThreadState.hpp" aoqi@0: #include "prims/jvmtiEventController.inline.hpp" aoqi@0: #include "prims/jvmtiImpl.hpp" aoqi@0: #include "runtime/handles.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/interfaceSupport.hpp" aoqi@0: #include "runtime/javaCalls.hpp" aoqi@0: #include "runtime/signature.hpp" aoqi@0: #include "runtime/vframe.hpp" aoqi@0: #include "runtime/vm_operations.hpp" aoqi@0: aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////// aoqi@0: // aoqi@0: // class JvmtiFramePop aoqi@0: // aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void JvmtiFramePop::print() { aoqi@0: tty->print_cr("_frame_number=%d", _frame_number); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////// aoqi@0: // aoqi@0: // class JvmtiFramePops - private methods aoqi@0: // aoqi@0: aoqi@0: void aoqi@0: JvmtiFramePops::set(JvmtiFramePop& fp) { aoqi@0: if (_pops->find(fp.frame_number()) < 0) { aoqi@0: _pops->append(fp.frame_number()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void aoqi@0: JvmtiFramePops::clear(JvmtiFramePop& fp) { aoqi@0: assert(_pops->length() > 0, "No more frame pops"); aoqi@0: aoqi@0: _pops->remove(fp.frame_number()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int aoqi@0: JvmtiFramePops::clear_to(JvmtiFramePop& fp) { aoqi@0: int cleared = 0; aoqi@0: int index = 0; aoqi@0: while (index < _pops->length()) { aoqi@0: JvmtiFramePop pop = JvmtiFramePop(_pops->at(index)); aoqi@0: if (pop.above_on_stack(fp)) { aoqi@0: _pops->remove_at(index); aoqi@0: ++cleared; aoqi@0: } else { aoqi@0: ++index; aoqi@0: } aoqi@0: } aoqi@0: return cleared; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////// aoqi@0: // aoqi@0: // class JvmtiFramePops - public methods aoqi@0: // aoqi@0: aoqi@0: JvmtiFramePops::JvmtiFramePops() { aoqi@0: _pops = new (ResourceObj::C_HEAP, mtInternal) GrowableArray (2, true); aoqi@0: } aoqi@0: aoqi@0: JvmtiFramePops::~JvmtiFramePops() { aoqi@0: // return memory to c_heap. aoqi@0: delete _pops; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void JvmtiFramePops::print() { aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: int n = _pops->length(); aoqi@0: for (int i=0; iat(i)); aoqi@0: tty->print("%d: ", i); aoqi@0: fp.print(); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: /////////////////////////////////////////////////////////////// aoqi@0: // aoqi@0: // class JvmtiEnvThreadState aoqi@0: // aoqi@0: // Instances of JvmtiEnvThreadState hang off of each JvmtiThreadState, aoqi@0: // one per JvmtiEnv. aoqi@0: // aoqi@0: aoqi@0: JvmtiEnvThreadState::JvmtiEnvThreadState(JavaThread *thread, JvmtiEnvBase *env) : aoqi@0: _event_enable() { aoqi@0: _thread = thread; aoqi@0: _env = (JvmtiEnv*)env; aoqi@0: _next = NULL; aoqi@0: _frame_pops = NULL; aoqi@0: _current_bci = 0; aoqi@0: _current_method_id = NULL; aoqi@0: _breakpoint_posted = false; aoqi@0: _single_stepping_posted = false; aoqi@0: _agent_thread_local_storage_data = NULL; aoqi@0: } aoqi@0: aoqi@0: JvmtiEnvThreadState::~JvmtiEnvThreadState() { aoqi@0: delete _frame_pops; aoqi@0: _frame_pops = NULL; aoqi@0: } aoqi@0: aoqi@0: // Given that a new (potential) event has come in, aoqi@0: // maintain the current JVMTI location on a per-thread per-env basis aoqi@0: // and use it to filter out duplicate events: aoqi@0: // - instruction rewrites aoqi@0: // - breakpoint followed by single step aoqi@0: // - single step at a breakpoint aoqi@0: void JvmtiEnvThreadState::compare_and_set_current_location(Method* new_method, aoqi@0: address new_location, jvmtiEvent event) { aoqi@0: aoqi@0: int new_bci = new_location - new_method->code_base(); aoqi@0: aoqi@0: // The method is identified and stored as a jmethodID which is safe in this aoqi@0: // case because the class cannot be unloaded while a method is executing. aoqi@0: jmethodID new_method_id = new_method->jmethod_id(); aoqi@0: aoqi@0: // the last breakpoint or single step was at this same location aoqi@0: if (_current_bci == new_bci && _current_method_id == new_method_id) { aoqi@0: switch (event) { aoqi@0: case JVMTI_EVENT_BREAKPOINT: aoqi@0: // Repeat breakpoint is complicated. If we previously posted a breakpoint aoqi@0: // event at this location and if we also single stepped at this location aoqi@0: // then we skip the duplicate breakpoint. aoqi@0: _breakpoint_posted = _breakpoint_posted && _single_stepping_posted; aoqi@0: break; aoqi@0: case JVMTI_EVENT_SINGLE_STEP: aoqi@0: // Repeat single step is easy: just don't post it again. aoqi@0: // If step is pending for popframe then it may not be aoqi@0: // a repeat step. The new_bci and method_id is same as current_bci aoqi@0: // and current method_id after pop and step for recursive calls. aoqi@0: // This has been handled by clearing the location aoqi@0: _single_stepping_posted = true; aoqi@0: break; aoqi@0: default: aoqi@0: assert(false, "invalid event value passed"); aoqi@0: break; aoqi@0: } aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: set_current_location(new_method_id, new_bci); aoqi@0: _breakpoint_posted = false; aoqi@0: _single_stepping_posted = false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: JvmtiFramePops* JvmtiEnvThreadState::get_frame_pops() { aoqi@0: #ifdef ASSERT aoqi@0: uint32_t debug_bits = 0; aoqi@0: #endif aoqi@0: assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), aoqi@0: "frame pop data only accessible from same thread or while suspended"); aoqi@0: aoqi@0: if (_frame_pops == NULL) { aoqi@0: _frame_pops = new JvmtiFramePops(); aoqi@0: assert(_frame_pops != NULL, "_frame_pops != NULL"); aoqi@0: } aoqi@0: return _frame_pops; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool JvmtiEnvThreadState::has_frame_pops() { aoqi@0: return _frame_pops == NULL? false : (_frame_pops->length() > 0); aoqi@0: } aoqi@0: aoqi@0: void JvmtiEnvThreadState::set_frame_pop(int frame_number) { aoqi@0: #ifdef ASSERT aoqi@0: uint32_t debug_bits = 0; aoqi@0: #endif aoqi@0: assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), aoqi@0: "frame pop data only accessible from same thread or while suspended"); aoqi@0: JvmtiFramePop fpop(frame_number); aoqi@0: JvmtiEventController::set_frame_pop(this, fpop); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiEnvThreadState::clear_frame_pop(int frame_number) { aoqi@0: #ifdef ASSERT aoqi@0: uint32_t debug_bits = 0; aoqi@0: #endif aoqi@0: assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), aoqi@0: "frame pop data only accessible from same thread or while suspended"); aoqi@0: JvmtiFramePop fpop(frame_number); aoqi@0: JvmtiEventController::clear_frame_pop(this, fpop); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JvmtiEnvThreadState::clear_to_frame_pop(int frame_number) { aoqi@0: #ifdef ASSERT aoqi@0: uint32_t debug_bits = 0; aoqi@0: #endif aoqi@0: assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), aoqi@0: "frame pop data only accessible from same thread or while suspended"); aoqi@0: JvmtiFramePop fpop(frame_number); aoqi@0: JvmtiEventController::clear_to_frame_pop(this, fpop); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool JvmtiEnvThreadState::is_frame_pop(int cur_frame_number) { aoqi@0: #ifdef ASSERT aoqi@0: uint32_t debug_bits = 0; aoqi@0: #endif aoqi@0: assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), aoqi@0: "frame pop data only accessible from same thread or while suspended"); aoqi@0: if (!get_thread()->is_interp_only_mode() || _frame_pops == NULL) { aoqi@0: return false; aoqi@0: } aoqi@0: JvmtiFramePop fp(cur_frame_number); aoqi@0: return get_frame_pops()->contains(fp); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: class VM_GetCurrentLocation : public VM_Operation { aoqi@0: private: aoqi@0: JavaThread *_thread; aoqi@0: jmethodID _method_id; aoqi@0: int _bci; aoqi@0: aoqi@0: public: aoqi@0: VM_GetCurrentLocation(JavaThread *thread) { aoqi@0: _thread = thread; aoqi@0: } aoqi@0: VMOp_Type type() const { return VMOp_GetCurrentLocation; } aoqi@0: void doit() { aoqi@0: ResourceMark rmark; // _thread != Thread::current() aoqi@0: RegisterMap rm(_thread, false); aoqi@0: // There can be a race condition between a VM_Operation reaching a safepoint aoqi@0: // and the target thread exiting from Java execution. aoqi@0: // We must recheck the last Java frame still exists. aoqi@0: if (!_thread->is_exiting() && _thread->has_last_Java_frame()) { aoqi@0: javaVFrame* vf = _thread->last_java_vframe(&rm); aoqi@0: assert(vf != NULL, "must have last java frame"); aoqi@0: Method* method = vf->method(); aoqi@0: _method_id = method->jmethod_id(); aoqi@0: _bci = vf->bci(); aoqi@0: } else { aoqi@0: // Clear current location as the target thread has no Java frames anymore. aoqi@0: _method_id = (jmethodID)NULL; aoqi@0: _bci = 0; aoqi@0: } aoqi@0: } aoqi@0: void get_current_location(jmethodID *method_id, int *bci) { aoqi@0: *method_id = _method_id; aoqi@0: *bci = _bci; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: void JvmtiEnvThreadState::reset_current_location(jvmtiEvent event_type, bool enabled) { aoqi@0: assert(event_type == JVMTI_EVENT_SINGLE_STEP || event_type == JVMTI_EVENT_BREAKPOINT, aoqi@0: "must be single-step or breakpoint event"); aoqi@0: aoqi@0: // Current location is used to detect the following: aoqi@0: // 1) a breakpoint event followed by single-stepping to the same bci aoqi@0: // 2) single-step to a bytecode that will be transformed to a fast version aoqi@0: // We skip to avoid posting the duplicate single-stepping event. aoqi@0: aoqi@0: // If single-stepping is disabled, clear current location so that aoqi@0: // single-stepping to the same method and bcp at a later time will be aoqi@0: // detected if single-stepping is enabled at that time (see 4388912). aoqi@0: aoqi@0: // If single-stepping is enabled, set the current location to the aoqi@0: // current method and bcp. This covers the following type of case, aoqi@0: // e.g., the debugger stepi command: aoqi@0: // - bytecode single stepped aoqi@0: // - SINGLE_STEP event posted and SINGLE_STEP event disabled aoqi@0: // - SINGLE_STEP event reenabled aoqi@0: // - bytecode rewritten to fast version aoqi@0: aoqi@0: // If breakpoint event is disabled, clear current location only if aoqi@0: // single-stepping is not enabled. Otherwise, keep the thread location aoqi@0: // to detect any duplicate events. aoqi@0: aoqi@0: if (enabled) { aoqi@0: // If enabling breakpoint, no need to reset. aoqi@0: // Can't do anything if empty stack. aoqi@0: if (event_type == JVMTI_EVENT_SINGLE_STEP && _thread->has_last_Java_frame()) { aoqi@0: jmethodID method_id; aoqi@0: int bci; aoqi@0: // The java thread stack may not be walkable for a running thread aoqi@0: // so get current location at safepoint. aoqi@0: VM_GetCurrentLocation op(_thread); aoqi@0: VMThread::execute(&op); aoqi@0: op.get_current_location(&method_id, &bci); aoqi@0: set_current_location(method_id, bci); aoqi@0: } aoqi@0: } else if (event_type == JVMTI_EVENT_SINGLE_STEP || !is_enabled(JVMTI_EVENT_SINGLE_STEP)) { aoqi@0: // If this is to disable breakpoint, also check if single-step is not enabled aoqi@0: clear_current_location(); aoqi@0: } aoqi@0: }