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