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