jvmtiEnvThreadState.cpp revision 3863
0N/A/*
0N/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 *
873N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A *
0N/A */
0N/A
5037N/A#include "precompiled.hpp"
5708N/A#include "classfile/systemDictionary.hpp"
0N/A#include "interpreter/interpreter.hpp"
0N/A#include "jvmtifiles/jvmtiEnv.hpp"
0N/A#include "memory/resourceArea.hpp"
0N/A#include "prims/jvmtiEnvThreadState.hpp"
5422N/A#include "prims/jvmtiEventController.inline.hpp"
0N/A#include "prims/jvmtiImpl.hpp"
0N/A#include "runtime/handles.hpp"
0N/A#include "runtime/handles.inline.hpp"
3012N/A#include "runtime/interfaceSupport.hpp"
5040N/A#include "runtime/javaCalls.hpp"
3381N/A#include "runtime/signature.hpp"
3349N/A#include "runtime/vframe.hpp"
3349N/A#include "runtime/vm_operations.hpp"
3012N/A
1083N/A
1083N/A///////////////////////////////////////////////////////////////
1083N/A//
1083N/A// class JvmtiFramePop
4156N/A//
1083N/A
1083N/A#ifndef PRODUCT
1083N/Avoid JvmtiFramePop::print() {
1083N/A tty->print_cr("_frame_number=%d", _frame_number);
3090N/A}
3090N/A#endif
3214N/A
5481N/A
1083N/A///////////////////////////////////////////////////////////////
1083N/A//
1083N/A// class JvmtiFramePops - private methods
1083N/A//
233N/A
712N/Avoid
1177N/AJvmtiFramePops::set(JvmtiFramePop& fp) {
564N/A if (_pops->find(fp.frame_number()) < 0) {
0N/A _pops->append(fp.frame_number());
0N/A }
0N/A}
0N/A
712N/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());
233N/A}
233N/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)) {
567N/A _pops->remove_at(index);
567N/A ++cleared;
567N/A } else {
567N/A ++index;
2046N/A }
2046N/A }
1210N/A return cleared;
2033N/A}
712N/A
5614N/A
2046N/A///////////////////////////////////////////////////////////////
712N/A//
675N/A// class JvmtiFramePops - public methods
5058N/A//
712N/A
675N/AJvmtiFramePops::JvmtiFramePops() {
567N/A _pops = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int> (2, true);
0N/A}
5058N/A
712N/AJvmtiFramePops::~JvmtiFramePops() {
422N/A // return memory to c_heap.
0N/A delete _pops;
422N/A}
0N/A
422N/A
0N/A#ifndef PRODUCT
0N/Avoid JvmtiFramePops::print() {
0N/A ResourceMark rm;
0N/A
902N/A int n = _pops->length();
902N/A for (int i=0; i<n; i++) {
902N/A JvmtiFramePop fp = JvmtiFramePop(_pops->at(i));
902N/A tty->print("%d: ", i);
0N/A fp.print();
0N/A tty->print_cr("");
0N/A }
0N/A}
233N/A#endif
233N/A
233N/A///////////////////////////////////////////////////////////////
233N/A//
730N/A// class JvmtiEnvThreadState
730N/A//
730N/A// Instances of JvmtiEnvThreadState hang off of each JvmtiThreadState,
0N/A// one per JvmtiEnv.
729N/A//
729N/A
729N/AJvmtiEnvThreadState::JvmtiEnvThreadState(JavaThread *thread, JvmtiEnvBase *env) :
0N/A _event_enable() {
0N/A _thread = thread;
0N/A _env = (JvmtiEnv*)env;
110N/A _next = NULL;
110N/A _frame_pops = NULL;
121N/A _current_bci = 0;
699N/A _current_method_id = NULL;
0N/A _breakpoint_posted = false;
0N/A _single_stepping_posted = false;
1008N/A _agent_thread_local_storage_data = NULL;
1008N/A}
1008N/A
1008N/AJvmtiEnvThreadState::~JvmtiEnvThreadState() {
3214N/A delete _frame_pops;
3381N/A _frame_pops = NULL;
2086N/A}
3646N/A
3646N/A// Given that a new (potential) event has come in,
3646N/A// maintain the current JVMTI location on a per-thread per-env basis
2086N/A// and use it to filter out duplicate events:
3646N/A// - instruction rewrites
1177N/A// - breakpoint followed by single step
3980N/A// - single step at a breakpoint
3980N/Avoid JvmtiEnvThreadState::compare_and_set_current_location(methodOop new_method,
5058N/A address new_location, jvmtiEvent event) {
3980N/A
3980N/A int new_bci = new_location - new_method->code_base();
3349N/A
3349N/A // The method is identified and stored as a jmethodID which is safe in this
3349N/A // case because the class cannot be unloaded while a method is executing.
3349N/A jmethodID new_method_id = new_method->jmethod_id();
4686N/A
3349N/A // the last breakpoint or single step was at this same location
3349N/A if (_current_bci == new_bci && _current_method_id == new_method_id) {
3381N/A switch (event) {
3381N/A case JVMTI_EVENT_BREAKPOINT:
3349N/A // Repeat breakpoint is complicated. If we previously posted a breakpoint
5713N/A // event at this location and if we also single stepped at this location
5713N/A // then we skip the duplicate breakpoint.
5713N/A _breakpoint_posted = _breakpoint_posted && _single_stepping_posted;
5713N/A break;
5713N/A case JVMTI_EVENT_SINGLE_STEP:
5713N/A // Repeat single step is easy: just don't post it again.
3381N/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;
3381N/A break;
5058N/A default:
5058N/A assert(false, "invalid event value passed");
5058N/A break;
3381N/A }
0N/A return;
5058N/A }
5896N/A
1177N/A set_current_location(new_method_id, new_bci);
1177N/A _breakpoint_posted = false;
0N/A _single_stepping_posted = false;
0N/A}
0N/A
3853N/A
1177N/AJvmtiFramePops* JvmtiEnvThreadState::get_frame_pops() {
1177N/A#ifdef ASSERT
0N/A uint32_t debug_bits = 0;
3853N/A#endif
3853N/A assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
3853N/A "frame pop data only accessible from same thread or while suspended");
3853N/A
3853N/A if (_frame_pops == NULL) {
0N/A _frame_pops = new JvmtiFramePops();
3853N/A assert(_frame_pops != NULL, "_frame_pops != NULL");
3853N/A }
5896N/A return _frame_pops;
3853N/A}
3853N/A
0N/A
0N/Abool JvmtiEnvThreadState::has_frame_pops() {
0N/A return _frame_pops == NULL? false : (_frame_pops->length() > 0);
0N/A}
1177N/A
1177N/Avoid JvmtiEnvThreadState::set_frame_pop(int frame_number) {
0N/A#ifdef ASSERT
0N/A uint32_t debug_bits = 0;
3381N/A#endif
3381N/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);
5896N/A JvmtiEventController::set_frame_pop(this, fpop);
1177N/A}
1177N/A
0N/A
0N/Avoid JvmtiEnvThreadState::clear_frame_pop(int frame_number) {
2086N/A#ifdef ASSERT
2086N/A uint32_t debug_bits = 0;
2086N/A#endif
2086N/A assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
2086N/A "frame pop data only accessible from same thread or while suspended");
2086N/A JvmtiFramePop fpop(frame_number);
2086N/A JvmtiEventController::clear_frame_pop(this, fpop);
2086N/A}
2086N/A
2086N/A
2086N/Avoid JvmtiEnvThreadState::clear_to_frame_pop(int frame_number) {
2086N/A#ifdef ASSERT
2086N/A uint32_t debug_bits = 0;
2086N/A#endif
2086N/A assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
2086N/A "frame pop data only accessible from same thread or while suspended");
2086N/A JvmtiFramePop fpop(frame_number);
2086N/A JvmtiEventController::clear_to_frame_pop(this, fpop);
2086N/A}
2086N/A
2086N/A
2086N/Abool JvmtiEnvThreadState::is_frame_pop(int cur_frame_number) {
2086N/A#ifdef ASSERT
2086N/A uint32_t debug_bits = 0;
2086N/A#endif
2086N/A assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
2086N/A "frame pop data only accessible from same thread or while suspended");
2086N/A if (!get_thread()->is_interp_only_mode() || _frame_pops == NULL) {
2086N/A return false;
2086N/A }
2086N/A JvmtiFramePop fp(cur_frame_number);
2086N/A return get_frame_pops()->contains(fp);
2284N/A}
2284N/A
2284N/A
2086N/Aclass VM_GetCurrentLocation : public VM_Operation {
2086N/A private:
2086N/A JavaThread *_thread;
2086N/A jmethodID _method_id;
2086N/A int _bci;
2086N/A
2086N/A public:
2086N/A VM_GetCurrentLocation(JavaThread *thread) {
2086N/A _thread = thread;
2086N/A }
2086N/A VMOp_Type type() const { return VMOp_GetCurrentLocation; }
2086N/A void doit() {
2086N/A ResourceMark rmark; // _thread != Thread::current()
2086N/A RegisterMap rm(_thread, false);
2086N/A javaVFrame* vf = _thread->last_java_vframe(&rm);
2086N/A assert(vf != NULL, "must have last java frame");
2086N/A methodOop method = vf->method();
2086N/A _method_id = method->jmethod_id();
2086N/A _bci = vf->bci();
2086N/A }
2086N/A void get_current_location(jmethodID *method_id, int *bci) {
2086N/A *method_id = _method_id;
2086N/A *bci = _bci;
2086N/A }
2086N/A};
2086N/A
2086N/Avoid JvmtiEnvThreadState::reset_current_location(jvmtiEvent event_type, bool enabled) {
2086N/A assert(event_type == JVMTI_EVENT_SINGLE_STEP || event_type == JVMTI_EVENT_BREAKPOINT,
2086N/A "must be single-step or breakpoint event");
2086N/A
2086N/A // Current location is used to detect the following:
2086N/A // 1) a breakpoint event followed by single-stepping to the same bci
2086N/A // 2) single-step to a bytecode that will be transformed to a fast version
2086N/A // We skip to avoid posting the duplicate single-stepping event.
2086N/A
2086N/A // If single-stepping is disabled, clear current location so that
3034N/A // single-stepping to the same method and bcp at a later time will be
3034N/A // detected if single-stepping is enabled at that time (see 4388912).
3034N/A
4156N/A // If single-stepping is enabled, set the current location to the
4156N/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
3853N/A // - SINGLE_STEP event posted and SINGLE_STEP event disabled
0N/A // - SINGLE_STEP event reenabled
0N/A // - bytecode rewritten to fast version
2342N/A
1008N/A // If breakpoint event is disabled, clear current location only if
1008N/A // single-stepping is not enabled. Otherwise, keep the thread location
1008N/A // to detect any duplicate events.
1008N/A
0N/A if (enabled) {
0N/A // If enabling breakpoint, no need to reset.
0N/A // Can't do anything if empty stack.
1177N/A if (event_type == JVMTI_EVENT_SINGLE_STEP && _thread->has_last_Java_frame()) {
121N/A jmethodID method_id;
1177N/A int bci;
1177N/A // The java thread stack may not be walkable for a running thread
1177N/A // so get current location at safepoint.
0N/A VM_GetCurrentLocation op(_thread);
0N/A VMThread::execute(&op);
1273N/A op.get_current_location(&method_id, &bci);
1273N/A set_current_location(method_id, bci);
1273N/A }
1273N/A } else if (event_type == JVMTI_EVENT_SINGLE_STEP || !is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
5178N/A // If this is to disable breakpoint, also check if single-step is not enabled
1273N/A clear_current_location();
5038N/A }
1273N/A}
1273N/A