/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "classfile/classLoaderDataGraph.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/vmClasses.hpp"
#include "classfile/vmSymbols.hpp"
#include "gc/shared/collectedHeap.hpp"
#include "jvmtifiles/jvmtiEnv.hpp"
#include "logging/log.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/access.inline.hpp"
#include "oops/arrayOop.hpp"
#include "oops/constantPool.inline.hpp"
#include "oops/instanceMirrorKlass.hpp"
#include "oops/klass.inline.hpp"
#include "oops/objArrayKlass.hpp"
#include "oops/objArrayOop.inline.hpp"
#include "oops/oop.inline.hpp"
#include "oops/typeArrayOop.inline.hpp"
#include "prims/jvmtiEventController.hpp"
#include "prims/jvmtiEventController.inline.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/jvmtiImpl.hpp"
#include "prims/jvmtiTagMap.hpp"
#include "prims/jvmtiTagMapTable.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/javaThread.inline.hpp"
#include "runtime/jniHandles.inline.hpp"
#include "runtime/mutex.hpp"
#include "runtime/mutexLocker.hpp"
#include "runtime/reflectionUtils.hpp"
#include "runtime/safepoint.hpp"
#include "runtime/timerTrace.hpp"
#include "runtime/threadSMR.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vmThread.hpp"
#include "runtime/vmOperations.hpp"
#include "utilities/objectBitSet.inline.hpp"
#include "utilities/macros.hpp"
typedef ObjectBitSet<mtServiceability> JVMTIBitSet;
bool JvmtiTagMap::_has_object_free_events = false;
// create a JvmtiTagMap
JvmtiTagMap::JvmtiTagMap(JvmtiEnv* env) :
_env(env),
_lock(Mutex::nosafepoint, "JvmtiTagMap_lock"),
_needs_cleaning(false),
_posting_events(false) {
assert(JvmtiThreadState_lock->is_locked(), "sanity check");
assert(((JvmtiEnvBase *)env)->tag_map() == NULL, "tag map already exists for environment");
_hashmap = new JvmtiTagMapTable();
// finally add us to the environment
((JvmtiEnvBase *)env)->release_set_tag_map(this);
}
// destroy a JvmtiTagMap
JvmtiTagMap::~JvmtiTagMap() {
// no lock acquired as we assume the enclosing environment is
// also being destroyed.
((JvmtiEnvBase *)_env)->set_tag_map(NULL);
// finally destroy the hashmap
delete _hashmap;
_hashmap = NULL;
}
// Called by env_dispose() to reclaim memory before deallocation.
// Remove all the entries but keep the empty table intact.
// This needs the table lock.
void JvmtiTagMap::clear() {
MutexLocker ml(lock(), Mutex::_no_safepoint_check_flag);
_hashmap->clear();
}
// returns the tag map for the given environments. If the tag map
// doesn't exist then it is created.
JvmtiTagMap* JvmtiTagMap::tag_map_for(JvmtiEnv* env) {
JvmtiTagMap* tag_map = ((JvmtiEnvBase*)env)->tag_map_acquire();
if (tag_map == NULL) {
MutexLocker mu(JvmtiThreadState_lock);
tag_map = ((JvmtiEnvBase*)env)->tag_map();
if (tag_map == NULL) {
tag_map = new JvmtiTagMap(env);
}
} else {
DEBUG_ONLY(JavaThread::current()->check_possible_safepoint());
}
return tag_map;
}
// iterate over all entries in the tag map.
void JvmtiTagMap::entry_iterate(JvmtiTagMapEntryClosure* closure) {
hashmap()->entry_iterate(closure);
}
// returns true if the hashmaps are empty
bool JvmtiTagMap::is_empty() {
assert(SafepointSynchronize::is_at_safepoint() || is_locked(), "checking");
return hashmap()->is_empty();
}
// This checks for posting before operations that use
// this tagmap table.
void JvmtiTagMap::check_hashmap(GrowableArray<jlong>* objects) {
assert(is_locked(), "checking");
if (is_empty()) { return; }
if (_needs_cleaning &&
objects != NULL &&
env()->is_enabled(JVMTI_EVENT_OBJECT_FREE)) {
remove_dead_entries_locked(objects);
}
}
// This checks for posting and is called from the heap walks.
void JvmtiTagMap::check_hashmaps_for_heapwalk(GrowableArray<jlong>* objects) {
assert(SafepointSynchronize::is_at_safepoint(), "called from safepoints");
// Verify that the tag map tables are valid and unconditionally post events
// that are expected to be posted before gc_notification.
JvmtiEnvIterator it;
for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
JvmtiTagMap* tag_map = env->tag_map_acquire();
if (tag_map != NULL) {
// The ZDriver may be walking the hashmaps concurrently so this lock is needed.
MutexLocker ml(tag_map->lock(), Mutex::_no_safepoint_check_flag);
tag_map->check_hashmap(objects);
}
}
}
// Return the tag value for an object, or 0 if the object is
// not tagged
//
static inline jlong tag_for(JvmtiTagMap* tag_map, oop o) {
JvmtiTagMapEntry* entry = tag_map->hashmap()->find(o);
if (entry == NULL) {
return 0;
} else {
jlong tag = entry->tag();
assert(tag != 0, "should not be zero");
return entry->tag();
}
}
// A CallbackWrapper is a support class for querying and tagging an object
// around a callback to a profiler. The constructor does pre-callback
// work to get the tag value, klass tag value, ... and the destructor
// does the post-callback work of tagging or untagging the object.
//
// {
// CallbackWrapper wrapper(tag_map, o);
//
// (*callback)(wrapper.klass_tag(), wrapper.obj_size(), wrapper.obj_tag_p(), ...)
//
// } // wrapper goes out of scope here which results in the destructor
// checking to see if the object has been tagged, untagged, or the
// tag value has changed.
//
class CallbackWrapper : public StackObj {
private:
JvmtiTagMap* _tag_map;
JvmtiTagMapTable* _hashmap;
JvmtiTagMapEntry* _entry;
oop _o;
jlong _obj_size;
jlong _obj_tag;
jlong _klass_tag;
protected:
JvmtiTagMap* tag_map() const { return _tag_map; }
// invoked post-callback to tag, untag, or update the tag of an object
void inline post_callback_tag_update(oop o, JvmtiTagMapTable* hashmap,
JvmtiTagMapEntry* entry, jlong obj_tag);
public:
CallbackWrapper(JvmtiTagMap* tag_map, oop o) {
assert(Thread::current()->is_VM_thread() || tag_map->is_locked(),
"MT unsafe or must be VM thread");
// object to tag
_o = o;
// object size
_obj_size = (jlong)_o->size() * wordSize;
// record the context
_tag_map = tag_map;
_hashmap = tag_map->hashmap();
_entry = _hashmap->find(_o);
// get object tag
_obj_tag = (_entry == NULL) ? 0 : _entry->tag();
// get the class and the class's tag value
assert(vmClasses::Class_klass()->is_mirror_instance_klass(), "Is not?");
_klass_tag = tag_for(tag_map, _o->klass()->java_mirror());
}
~CallbackWrapper() {
post_callback_tag_update(_o, _hashmap, _entry, _obj_tag);
}
inline jlong* obj_tag_p() { return &_obj_tag; }
inline jlong obj_size() const { return _obj_size; }
inline jlong obj_tag() const { return _obj_tag; }
inline jlong klass_tag() const { return _klass_tag; }
};
// callback post-callback to tag, untag, or update the tag of an object
void inline CallbackWrapper::post_callback_tag_update(oop o,
JvmtiTagMapTable* hashmap,
JvmtiTagMapEntry* entry,
jlong obj_tag) {
if (entry == NULL) {
if (obj_tag != 0) {
// callback has tagged the object
assert(Thread::current()->is_VM_thread(), "must be VMThread");
hashmap->add(o, obj_tag);
}
} else {
// object was previously tagged - the callback may have untagged
// the object or changed the tag value
if (obj_tag == 0) {
hashmap->remove(o);
} else {
if (obj_tag != entry->tag()) {
entry->set_tag(obj_tag);
}
}
}
}
// An extended CallbackWrapper used when reporting an object reference
// to the agent.
//
// {
// TwoOopCallbackWrapper wrapper(tag_map, referrer, o);
//
// (*callback)(wrapper.klass_tag(),
// wrapper.obj_size(),
// wrapper.obj_tag_p()
// wrapper.referrer_tag_p(), ...)
//
// } // wrapper goes out of scope here which results in the destructor
// checking to see if the referrer object has been tagged, untagged,
// or the tag value has changed.
//
class TwoOopCallbackWrapper : public CallbackWrapper {
private:
bool _is_reference_to_self;
JvmtiTagMapTable* _referrer_hashmap;
JvmtiTagMapEntry* _referrer_entry;
oop _referrer;
jlong _referrer_obj_tag;
jlong _referrer_klass_tag;
jlong* _referrer_tag_p;
bool is_reference_to_self() const { return _is_reference_to_self; }
public:
TwoOopCallbackWrapper(JvmtiTagMap* tag_map, oop referrer, oop o) :
CallbackWrapper(tag_map, o)
{
// self reference needs to be handled in a special way
_is_reference_to_self = (referrer == o);
if (_is_reference_to_self) {
_referrer_klass_tag = klass_tag();
_referrer_tag_p = obj_tag_p();
} else {
_referrer = referrer;
// record the context
_referrer_hashmap = tag_map->hashmap();
_referrer_entry = _referrer_hashmap->find(_referrer);
// get object tag
_referrer_obj_tag = (_referrer_entry == NULL) ? 0 : _referrer_entry->tag();
_referrer_tag_p = &_referrer_obj_tag;
// get referrer class tag.
_referrer_klass_tag = tag_for(tag_map, _referrer->klass()->java_mirror());
}
}
~TwoOopCallbackWrapper() {
if (!is_reference_to_self()){
post_callback_tag_update(_referrer,
_referrer_hashmap,
_referrer_entry,
_referrer_obj_tag);
}
}
// address of referrer tag
// (for a self reference this will return the same thing as obj_tag_p())
inline jlong* referrer_tag_p() { return _referrer_tag_p; }
// referrer's class tag
inline jlong referrer_klass_tag() { return _referrer_klass_tag; }
};
// tag an object
//
// This function is performance critical. If many threads attempt to tag objects
// around the same time then it's possible that the Mutex associated with the
// tag map will be a hot lock.
void JvmtiTagMap::set_tag(jobject object, jlong tag) {
MutexLocker ml(lock(), Mutex::_no_safepoint_check_flag);
// SetTag should not post events because the JavaThread has to
// transition to native for the callback and this cannot stop for
// safepoints with the hashmap lock held.
check_hashmap(NULL); /* don't collect dead objects */
// resolve the object
oop o = JNIHandles::resolve_non_null(object);
// see if the object is already tagged
JvmtiTagMapTable* hashmap = _hashmap;
JvmtiTagMapEntry* entry = hashmap->find(o);
// if the object is not already tagged then we tag it
if (entry == NULL) {
if (tag != 0) {
hashmap->add(o, tag);
} else {
// no-op
}
} else {
// if the object is already tagged then we either update
// the tag (if a new tag value has been provided)
// or remove the object if the new tag value is 0.
if (tag == 0) {
hashmap->remove(o);
} else {
entry->set_tag(tag);
}
}
}
// get the tag for an object
jlong JvmtiTagMap::get_tag(jobject object) {
MutexLocker ml(lock(), Mutex::_no_safepoint_check_flag);
// GetTag should not post events because the JavaThread has to
// transition to native for the callback and this cannot stop for
// safepoints with the hashmap lock held.
check_hashmap(NULL); /* don't collect dead objects */
// resolve the object
oop o = JNIHandles::resolve_non_null(object);
return tag_for(this, o);
}
// Helper class used to describe the static or instance fields of a class.
// For each field it holds the field index (as defined by the JVMTI specification),
// the field type, and the offset.
class ClassFieldDescriptor: public CHeapObj<mtInternal> {
private:
int _field_index;
int _field_offset;
char _field_type;
public:
ClassFieldDescriptor(int index, char type, int offset) :
_field_index(index), _field_offset(offset), _field_type(type) {
}
int field_index() const { return _field_index; }
char field_type() const { return _field_type; }
int field_offset() const { return _field_offset; }
};
class ClassFieldMap: public CHeapObj<mtInternal> {
private:
enum {
initial_field_count = 5
};
// list of field descriptors
GrowableArray<ClassFieldDescriptor*>* _fields;
// constructor
ClassFieldMap();
// add a field
void add(int index, char type, int offset);
public:
~ClassFieldMap();
// access
int field_count() { return _fields->length(); }
ClassFieldDescriptor* field_at(int i) { return _fields->at(i); }
// functions to create maps of static or instance fields
static ClassFieldMap* create_map_of_static_fields(Klass* k);
static ClassFieldMap* create_map_of_instance_fields(oop obj);
};
ClassFieldMap::ClassFieldMap() {
_fields = new (mtServiceability)
GrowableArray<ClassFieldDescriptor*>(initial_field_count, mtServiceability);
}
ClassFieldMap::~ClassFieldMap() {
for (int i=0; i<_fields->length(); i++) {
delete _fields->at(i);
}
delete _fields;
}
void ClassFieldMap::add(int index, char type, int offset) {
ClassFieldDescriptor* field = new ClassFieldDescriptor(index, type, offset);
_fields->append(field);
}
// Returns a heap allocated ClassFieldMap to describe the static fields
// of the given class.
//
ClassFieldMap* ClassFieldMap::create_map_of_static_fields(Klass* k) {
InstanceKlass* ik = InstanceKlass::cast(k);
// create the field map
ClassFieldMap* field_map = new ClassFieldMap();
FilteredFieldStream f(ik, false, false);
int max_field_index = f.field_count()-1;
int index = 0;
for (FilteredFieldStream fld(ik, true, true); !fld.eos(); fld.next(), index++) {
// ignore instance fields
if (!fld.access_flags().is_static()) {
continue;
}
field_map->add(max_field_index - index, fld.signature()->char_at(0), fld.offset());
}
return field_map;
}
// Returns a heap allocated ClassFieldMap to describe the instance fields
// of the given class. All instance fields are included (this means public
// and private fields declared in superclasses and superinterfaces too).
//
ClassFieldMap* ClassFieldMap::create_map_of_instance_fields(oop obj) {
InstanceKlass* ik = InstanceKlass::cast(obj->klass());
// create the field map
ClassFieldMap* field_map = new ClassFieldMap();
FilteredFieldStream f(ik, false, false);
int max_field_index = f.field_count()-1;
int index = 0;
for (FilteredFieldStream fld(ik, false, false); !fld.eos(); fld.next(), index++) {
// ignore static fields
if (fld.access_flags().is_static()) {
continue;
}
field_map->add(max_field_index - index, fld.signature()->char_at(0), fld.offset());
}
return field_map;
}
// Helper class used to cache a ClassFileMap for the instance fields of
// a cache. A JvmtiCachedClassFieldMap can be cached by an InstanceKlass during
// heap iteration and avoid creating a field map for each object in the heap
// (only need to create the map when the first instance of a class is encountered).
//
class JvmtiCachedClassFieldMap : public CHeapObj<mtInternal> {
private:
enum {
initial_class_count = 200
};
ClassFieldMap* _field_map;
ClassFieldMap* field_map() const { return _field_map; }
JvmtiCachedClassFieldMap(ClassFieldMap* field_map);
~JvmtiCachedClassFieldMap();
static GrowableArray<InstanceKlass*>* _class_list;
static void add_to_class_list(InstanceKlass* ik);
public:
// returns the field map for a given object (returning map cached
// by InstanceKlass if possible
static ClassFieldMap* get_map_of_instance_fields(oop obj);
// removes the field map from all instanceKlasses - should be
// called before VM operation completes
static void clear_cache();
// returns the number of ClassFieldMap cached by instanceKlasses
static int cached_field_map_count();
};
GrowableArray<InstanceKlass*>* JvmtiCachedClassFieldMap::_class_list;
JvmtiCachedClassFieldMap::JvmtiCachedClassFieldMap(ClassFieldMap* field_map) {
_field_map = field_map;
}
JvmtiCachedClassFieldMap::~JvmtiCachedClassFieldMap() {
if (_field_map != NULL) {
delete _field_map;
}
}
// Marker class to ensure that the class file map cache is only used in a defined
// scope.
class ClassFieldMapCacheMark : public StackObj {
private:
static bool _is_active;
public:
ClassFieldMapCacheMark() {
assert(Thread::current()->is_VM_thread(), "must be VMThread");
assert(JvmtiCachedClassFieldMap::cached_field_map_count() == 0, "cache not empty");
assert(!_is_active, "ClassFieldMapCacheMark cannot be nested");
_is_active = true;
}
~ClassFieldMapCacheMark() {
JvmtiCachedClassFieldMap::clear_cache();
_is_active = false;
}
static bool is_active() { return _is_active; }
};
bool ClassFieldMapCacheMark::_is_active;
// record that the given InstanceKlass is caching a field map
void JvmtiCachedClassFieldMap::add_to_class_list(InstanceKlass* ik) {
if (_class_list == NULL) {
_class_list = new (mtServiceability)
GrowableArray<InstanceKlass*>(initial_class_count, mtServiceability);
}
_class_list->push(ik);
}
// returns the instance field map for the given object
// (returns field map cached by the InstanceKlass if possible)
ClassFieldMap* JvmtiCachedClassFieldMap::get_map_of_instance_fields(oop obj) {
assert(Thread::current()->is_VM_thread(), "must be VMThread");
assert(ClassFieldMapCacheMark::is_active(), "ClassFieldMapCacheMark not active");
Klass* k = obj->klass();
InstanceKlass* ik = InstanceKlass::cast(k);
// return cached map if possible
JvmtiCachedClassFieldMap* cached_map = ik->jvmti_cached_class_field_map();
if (cached_map != NULL) {
assert(cached_map->field_map() != NULL, "missing field list");
return cached_map->field_map();
} else {
ClassFieldMap* field_map = ClassFieldMap::create_map_of_instance_fields(obj);
cached_map = new JvmtiCachedClassFieldMap(field_map);
ik->set_jvmti_cached_class_field_map(cached_map);
add_to_class_list(ik);
return field_map;
}
}
// remove the fields maps cached from all instanceKlasses
void JvmtiCachedClassFieldMap::clear_cache() {
assert(Thread::current()->is_VM_thread(), "must be VMThread");
if (_class_list != NULL) {
for (int i = 0; i < _class_list->length(); i++) {
InstanceKlass* ik = _class_list->at(i);
JvmtiCachedClassFieldMap* cached_map = ik->jvmti_cached_class_field_map();
assert(cached_map != NULL, "should not be NULL");
ik->set_jvmti_cached_class_field_map(NULL);
delete cached_map; // deletes the encapsulated field map
}
delete _class_list;
_class_list = NULL;
}
}
// returns the number of ClassFieldMap cached by instanceKlasses
int JvmtiCachedClassFieldMap::cached_field_map_count() {
return (_class_list == NULL) ? 0 : _class_list->length();
}
// helper function to indicate if an object is filtered by its tag or class tag
static inline bool is_filtered_by_heap_filter(jlong obj_tag,
jlong klass_tag,
int heap_filter) {
// apply the heap filter
if (obj_tag != 0) {
// filter out tagged objects
if (heap_filter & JVMTI_HEAP_FILTER_TAGGED) return true;
} else {
// filter out untagged objects
if (heap_filter & JVMTI_HEAP_FILTER_UNTAGGED) return true;
}
if (klass_tag != 0) {
// filter out objects with tagged classes
if (heap_filter & JVMTI_HEAP_FILTER_CLASS_TAGGED) return true;
} else {
// filter out objects with untagged classes.
if (heap_filter & JVMTI_HEAP_FILTER_CLASS_UNTAGGED) return true;
}
return false;
}
// helper function to indicate if an object is filtered by a klass filter
static inline bool is_filtered_by_klass_filter(oop obj, Klass* klass_filter) {
if (klass_filter != NULL) {
if (obj->klass() != klass_filter) {
return true;
}
}
return false;
}
// helper function to tell if a field is a primitive field or not
static inline bool is_primitive_field_type(char type) {
return (type != JVM_SIGNATURE_CLASS && type != JVM_SIGNATURE_ARRAY);
}
// helper function to copy the value from location addr to jvalue.
static inline void copy_to_jvalue(jvalue *v, address addr, jvmtiPrimitiveType value_type) {
switch (value_type) {
case JVMTI_PRIMITIVE_TYPE_BOOLEAN : { v->z = *(jboolean*)addr; break; }
case JVMTI_PRIMITIVE_TYPE_BYTE : { v->b = *(jbyte*)addr; break; }
case JVMTI_PRIMITIVE_TYPE_CHAR : { v->c = *(jchar*)addr; break; }
case JVMTI_PRIMITIVE_TYPE_SHORT : { v->s = *(jshort*)addr; break; }
case JVMTI_PRIMITIVE_TYPE_INT : { v->i = *(jint*)addr; break; }
case JVMTI_PRIMITIVE_TYPE_LONG : { v->j = *(jlong*)addr; break; }
case JVMTI_PRIMITIVE_TYPE_FLOAT : { v->f = *(jfloat*)addr; break; }
case JVMTI_PRIMITIVE_TYPE_DOUBLE : { v->d = *(jdouble*)addr; break; }
default: ShouldNotReachHere();
}
}
// helper function to invoke string primitive value callback
// returns visit control flags
static jint invoke_string_value_callback(jvmtiStringPrimitiveValueCallback cb,
CallbackWrapper* wrapper,
oop str,
void* user_data)
{
assert(str->klass() == vmClasses::String_klass(), "not a string");
typeArrayOop s_value = java_lang_String::value(str);
// JDK-6584008: the value field may be null if a String instance is
// partially constructed.
if (s_value == NULL) {
return 0;
}
// get the string value and length
// (string value may be offset from the base)
int s_len = java_lang_String::length(str);
bool is_latin1 = java_lang_String::is_latin1(str);
jchar* value;
if (s_len > 0) {
if (!is_latin1) {
value = s_value->char_at_addr(0);
} else {
// Inflate latin1 encoded string to UTF16
jchar* buf = NEW_C_HEAP_ARRAY(jchar, s_len, mtInternal);
for (int i = 0; i < s_len; i++) {
buf[i] = ((jchar) s_value->byte_at(i)) & 0xff;
}
value = &buf[0];
}
} else {
// Don't use char_at_addr(0) if length is 0
value = (jchar*) s_value->base(T_CHAR);
}
// invoke the callback
jint res = (*cb)(wrapper->klass_tag(),
wrapper->obj_size(),
wrapper->obj_tag_p(),
value,
(jint)s_len,
user_data);
if (is_latin1 && s_len > 0) {
FREE_C_HEAP_ARRAY(jchar, value);
}
return res;
}
// helper function to invoke string primitive value callback
// returns visit control flags
static jint invoke_array_primitive_value_callback(jvmtiArrayPrimitiveValueCallback cb,
CallbackWrapper* wrapper,
oop obj,
void* user_data)
{
assert(obj->is_typeArray(), "not a primitive array");
// get base address of first element
typeArrayOop array = typeArrayOop(obj);
BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
void* elements = array->base(type);
// jvmtiPrimitiveType is defined so this mapping is always correct
jvmtiPrimitiveType elem_type = (jvmtiPrimitiveType)type2char(type);
return (*cb)(wrapper->klass_tag(),
wrapper->obj_size(),
wrapper->obj_tag_p(),
(jint)array->length(),
elem_type,
elements,
user_data);
}
// helper function to invoke the primitive field callback for all static fields
// of a given class
static jint invoke_primitive_field_callback_for_static_fields
(CallbackWrapper* wrapper,
oop obj,
jvmtiPrimitiveFieldCallback cb,
void* user_data)
{
// for static fields only the index will be set
static jvmtiHeapReferenceInfo reference_info = { 0 };
assert(obj->klass() == vmClasses::Class_klass(), "not a class");
if (java_lang_Class::is_primitive(obj)) {
return 0;
}
Klass* klass = java_lang_Class::as_Klass(obj);
// ignore classes for object and type arrays
if (!klass->is_instance_klass()) {
return 0;
}
// ignore classes which aren't linked yet
InstanceKlass* ik = InstanceKlass::cast(klass);
if (!ik->is_linked()) {
return 0;
}
// get the field map
ClassFieldMap* field_map = ClassFieldMap::create_map_of_static_fields(klass);
// invoke the callback for each static primitive field
for (int i=0; i<field_map->field_count(); i++) {
ClassFieldDescriptor* field = field_map->field_at(i);
// ignore non-primitive fields
char type = field->field_type();
if (!is_primitive_field_type(type)) {
continue;
}
// one-to-one mapping
jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type;
// get offset and field value
int offset = field->field_offset();
address addr = cast_from_oop<address>(klass->java_mirror()) + offset;
jvalue value;
copy_to_jvalue(&value, addr, value_type);
// field index
reference_info.field.index = field->field_index();
// invoke the callback
jint res = (*cb)(JVMTI_HEAP_REFERENCE_STATIC_FIELD,
&reference_info,
wrapper->klass_tag(),
wrapper->obj_tag_p(),
value,
value_type,
user_data);
if (res & JVMTI_VISIT_ABORT) {
delete field_map;
return res;
}
}
delete field_map;
return 0;
}
// helper function to invoke the primitive field callback for all instance fields
// of a given object
static jint invoke_primitive_field_callback_for_instance_fields(
CallbackWrapper* wrapper,
oop obj,
jvmtiPrimitiveFieldCallback cb,
void* user_data)
{
// for instance fields only the index will be set
static jvmtiHeapReferenceInfo reference_info = { 0 };
// get the map of the instance fields
ClassFieldMap* fields = JvmtiCachedClassFieldMap::get_map_of_instance_fields(obj);
// invoke the callback for each instance primitive field
for (int i=0; i<fields->field_count(); i++) {
ClassFieldDescriptor* field = fields->field_at(i);
// ignore non-primitive fields
char type = field->field_type();
if (!is_primitive_field_type(type)) {
continue;
}
// one-to-one mapping
jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type;
// get offset and field value
int offset = field->field_offset();
address addr = cast_from_oop<address>(obj) + offset;
jvalue value;
copy_to_jvalue(&value, addr, value_type);
// field index
reference_info.field.index = field->field_index();
// invoke the callback
jint res = (*cb)(JVMTI_HEAP_REFERENCE_FIELD,
&reference_info,
wrapper->klass_tag(),
wrapper->obj_tag_p(),
value,
value_type,
user_data);
if (res & JVMTI_VISIT_ABORT) {
return res;
}
}
return 0;
}
// VM operation to iterate over all objects in the heap (both reachable
// and unreachable)
class VM_HeapIterateOperation: public VM_Operation {
private:
ObjectClosure* _blk;
GrowableArray<jlong>* const _dead_objects;
public:
VM_HeapIterateOperation(ObjectClosure* blk, GrowableArray<jlong>* objects) :
_blk(blk), _dead_objects(objects) { }
VMOp_Type type() const { return VMOp_HeapIterateOperation; }
void doit() {
// allows class files maps to be cached during iteration
ClassFieldMapCacheMark cm;
JvmtiTagMap::check_hashmaps_for_heapwalk(_dead_objects);
// make sure that heap is parsable (fills TLABs with filler objects)
Universe::heap()->ensure_parsability(false); // no need to retire TLABs
// Verify heap before iteration - if the heap gets corrupted then
// JVMTI's IterateOverHeap will crash.
if (VerifyBeforeIteration) {
Universe::verify();
}
// do the iteration
Universe::heap()->object_iterate(_blk);
}
};
// An ObjectClosure used to support the deprecated IterateOverHeap and
// IterateOverInstancesOfClass functions
class IterateOverHeapObjectClosure: public ObjectClosure {
private:
JvmtiTagMap* _tag_map;
Klass* _klass;
jvmtiHeapObjectFilter _object_filter;
jvmtiHeapObjectCallback _heap_object_callback;
const void* _user_data;
// accessors
JvmtiTagMap* tag_map() const { return _tag_map; }
jvmtiHeapObjectFilter object_filter() const { return _object_filter; }
jvmtiHeapObjectCallback object_callback() const { return _heap_object_callback; }
Klass* klass() const { return _klass; }
const void* user_data() const { return _user_data; }
// indicates if iteration has been aborted
bool _iteration_aborted;
bool is_iteration_aborted() const { return _iteration_aborted; }
void set_iteration_aborted(bool aborted) { _iteration_aborted = aborted; }
public:
IterateOverHeapObjectClosure(JvmtiTagMap* tag_map,
Klass* klass,
jvmtiHeapObjectFilter object_filter,
jvmtiHeapObjectCallback heap_object_callback,
const void* user_data) :
_tag_map(tag_map),
_klass(klass),
_object_filter(object_filter),
_heap_object_callback(heap_object_callback),
_user_data(user_data),
_iteration_aborted(false)
{
}
void do_object(oop o);
};
// invoked for each object in the heap
void IterateOverHeapObjectClosure::do_object(oop o) {
// check if iteration has been halted
if (is_iteration_aborted()) return;
// instanceof check when filtering by klass
if (klass() != NULL && !o->is_a(klass())) {
return;
}
// skip if object is a dormant shared object whose mirror hasn't been loaded
if (o != NULL && o->klass()->java_mirror() == NULL) {
log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)", p2i(o),
o->klass()->external_name());
return;
}
// prepare for the calllback
CallbackWrapper wrapper(tag_map(), o);
// if the object is tagged and we're only interested in untagged objects
// then don't invoke the callback. Similarly, if the object is untagged
// and we're only interested in tagged objects we skip the callback.
if (wrapper.obj_tag() != 0) {
if (object_filter() == JVMTI_HEAP_OBJECT_UNTAGGED) return;
} else {
if (object_filter() == JVMTI_HEAP_OBJECT_TAGGED) return;
}
// invoke the agent's callback
jvmtiIterationControl control = (*object_callback())(wrapper.klass_tag(),
wrapper.obj_size(),
wrapper.obj_tag_p(),
(void*)user_data());
if (control == JVMTI_ITERATION_ABORT) {
set_iteration_aborted(true);
}
}
// An ObjectClosure used to support the IterateThroughHeap function
class IterateThroughHeapObjectClosure: public ObjectClosure {
private:
JvmtiTagMap* _tag_map;
Klass* _klass;
int _heap_filter;
const jvmtiHeapCallbacks* _callbacks;
const void* _user_data;
// accessor functions
JvmtiTagMap* tag_map() const { return _tag_map; }
int heap_filter() const { return _heap_filter; }
const jvmtiHeapCallbacks* callbacks() const { return _callbacks; }
Klass* klass() const { return _klass; }
const void* user_data() const { return _user_data; }
// indicates if the iteration has been aborted
bool _iteration_aborted;
bool is_iteration_aborted() const { return _iteration_aborted; }
// used to check the visit control flags. If the abort flag is set
// then we set the iteration aborted flag so that the iteration completes
// without processing any further objects
bool check_flags_for_abort(jint flags) {
bool is_abort = (flags & JVMTI_VISIT_ABORT) != 0;
if (is_abort) {
_iteration_aborted = true;
}
return is_abort;
}
public:
IterateThroughHeapObjectClosure(JvmtiTagMap* tag_map,
Klass* klass,
int heap_filter,
const jvmtiHeapCallbacks* heap_callbacks,
const void* user_data) :
_tag_map(tag_map),
_klass(klass),
_heap_filter(heap_filter),
_callbacks(heap_callbacks),
_user_data(user_data),
_iteration_aborted(false)
{
}
void do_object(oop o);
};
// invoked for each object in the heap
void IterateThroughHeapObjectClosure::do_object(oop obj) {
// check if iteration has been halted
if (is_iteration_aborted()) return;
// apply class filter
if (is_filtered_by_klass_filter(obj, klass())) return;
// skip if object is a dormant shared object whose mirror hasn't been loaded
if (obj != NULL && obj->klass()->java_mirror() == NULL) {
log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)", p2i(obj),
obj->klass()->external_name());
return;
}
// prepare for callback
CallbackWrapper wrapper(tag_map(), obj);
// check if filtered by the heap filter
if (is_filtered_by_heap_filter(wrapper.obj_tag(), wrapper.klass_tag(), heap_filter())) {
return;
}
// for arrays we need the length, otherwise -1
bool is_array = obj->is_array();
int len = is_array ? arrayOop(obj)->length() : -1;
// invoke the object callback (if callback is provided)
if (callbacks()->heap_iteration_callback != NULL) {
jvmtiHeapIterationCallback cb = callbacks()->heap_iteration_callback;
jint res = (*cb)(wrapper.klass_tag(),
wrapper.obj_size(),
wrapper.obj_tag_p(),
(jint)len,
(void*)user_data());
if (check_flags_for_abort(res)) return;
}
// for objects and classes we report primitive fields if callback provided
if (callbacks()->primitive_field_callback != NULL && obj->is_instance()) {
jint res;
jvmtiPrimitiveFieldCallback cb = callbacks()->primitive_field_callback;
if (obj->klass() == vmClasses::Class_klass()) {
res = invoke_primitive_field_callback_for_static_fields(&wrapper,
obj,
cb,
(void*)user_data());
} else {
res = invoke_primitive_field_callback_for_instance_fields(&wrapper,
obj,
cb,
(void*)user_data());
}
if (check_flags_for_abort(res)) return;
}
// string callback
if (!is_array &&
callbacks()->string_primitive_value_callback != NULL &&
obj->klass() == vmClasses::String_klass()) {
jint res = invoke_string_value_callback(
callbacks()->string_primitive_value_callback,
&wrapper,
obj,
(void*)user_data() );
if (check_flags_for_abort(res)) return;
}
// array callback
if (is_array &&
callbacks()->array_primitive_value_callback != NULL &&
obj->is_typeArray()) {
jint res = invoke_array_primitive_value_callback(
callbacks()->array_primitive_value_callback,
&wrapper,
obj,
(void*)user_data() );
if (check_flags_for_abort(res)) return;
}
};
// Deprecated function to iterate over all objects in the heap
void JvmtiTagMap::iterate_over_heap(jvmtiHeapObjectFilter object_filter,
Klass* klass,
jvmtiHeapObjectCallback heap_object_callback,
const void* user_data)
{
// EA based optimizations on tagged objects are already reverted.
EscapeBarrier eb(object_filter == JVMTI_HEAP_OBJECT_UNTAGGED ||
object_filter == JVMTI_HEAP_OBJECT_EITHER,
JavaThread::current());
eb.deoptimize_objects_all_threads();
Arena dead_object_arena(mtServiceability);
GrowableArray <jlong> dead_objects(&dead_object_arena, 10, 0, 0);
{
MutexLocker ml(Heap_lock);
IterateOverHeapObjectClosure blk(this,
klass,
object_filter,
heap_object_callback,
user_data);
VM_HeapIterateOperation op(&blk, &dead_objects);
VMThread::execute(&op);
}
// Post events outside of Heap_lock
post_dead_objects(&dead_objects);
}
// Iterates over all objects in the heap
void JvmtiTagMap::iterate_through_heap(jint heap_filter,
Klass* klass,
const jvmtiHeapCallbacks* callbacks,
const void* user_data)
{
// EA based optimizations on tagged objects are already reverted.
// disabled if vritual threads are enabled with --enable-preview
EscapeBarrier eb(!(heap_filter & JVMTI_HEAP_FILTER_UNTAGGED), JavaThread::current());
eb.deoptimize_objects_all_threads();
Arena dead_object_arena(mtServiceability);
GrowableArray<jlong> dead_objects(&dead_object_arena, 10, 0, 0);
{
MutexLocker ml(Heap_lock);
IterateThroughHeapObjectClosure blk(this,
klass,
heap_filter,
callbacks,
user_data);
VM_HeapIterateOperation op(&blk, &dead_objects);
VMThread::execute(&op);
}
// Post events outside of Heap_lock
post_dead_objects(&dead_objects);
}
void JvmtiTagMap::remove_dead_entries_locked(GrowableArray<jlong>* objects) {
assert(is_locked(), "precondition");
if (_needs_cleaning) {
// Recheck whether to post object free events under the lock.
if (!env()->is_enabled(JVMTI_EVENT_OBJECT_FREE)) {
objects = NULL;
}
log_info(jvmti, table)("TagMap table needs cleaning%s",
((objects != NULL) ? " and posting" : ""));
hashmap()->remove_dead_entries(objects);
_needs_cleaning = false;
}
}
void JvmtiTagMap::remove_dead_entries(GrowableArray<jlong>* objects) {
MutexLocker ml(lock(), Mutex::_no_safepoint_check_flag);
remove_dead_entries_locked(objects);
}
void JvmtiTagMap::post_dead_objects(GrowableArray<jlong>* const objects) {
assert(Thread::current()->is_Java_thread(), "Must post from JavaThread");
if (objects != NULL && objects->length() > 0) {
JvmtiExport::post_object_free(env(), objects);
log_info(jvmti)("%d free object posted", objects->length());
}
}
void JvmtiTagMap::remove_and_post_dead_objects() {
ResourceMark rm;
GrowableArray<jlong> objects;
remove_dead_entries(&objects);
post_dead_objects(&objects);
}
void JvmtiTagMap::flush_object_free_events() {
assert_not_at_safepoint();
if (env()->is_enabled(JVMTI_EVENT_OBJECT_FREE)) {
{
MonitorLocker ml(lock(), Mutex::_no_safepoint_check_flag);
// If another thread is posting events, let it finish
while (_posting_events) {
ml.wait();
}
if (!_needs_cleaning || is_empty()) {
_needs_cleaning = false;
return;
}
_posting_events = true;
} // Drop the lock so we can do the cleaning on the VM thread.
// Needs both cleaning and event posting (up to some other thread
// getting there first after we dropped the lock).
remove_and_post_dead_objects();
{
MonitorLocker ml(lock(), Mutex::_no_safepoint_check_flag);
_posting_events = false;
ml.notify_all();
}
} else {
remove_dead_entries(NULL);
}
}
// support class for get_objects_with_tags
class TagObjectCollector : public JvmtiTagMapEntryClosure {
private:
JvmtiEnv* _env;
JavaThread* _thread;
jlong* _tags;
jint _tag_count;
bool _some_dead_found;
GrowableArray<jobject>* _object_results; // collected objects (JNI weak refs)
GrowableArray<uint64_t>* _tag_results; // collected tags
public:
TagObjectCollector(JvmtiEnv* env, const jlong* tags, jint tag_count) :
_env(env),
_thread(JavaThread::current()),
_tags((jlong*)tags),
_tag_count(tag_count),
_some_dead_found(false),
_object_results(new (mtServiceability) GrowableArray<jobject>(1, mtServiceability)),
_tag_results(new (mtServiceability) GrowableArray<uint64_t>(1, mtServiceability)) { }
~TagObjectCollector() {
delete _object_results;
delete _tag_results;
}
bool some_dead_found() const { return _some_dead_found; }
// for each tagged object check if the tag value matches
// - if it matches then we create a JNI local reference to the object
// and record the reference and tag value.
//
void do_entry(JvmtiTagMapEntry* entry) {
for (int i=0; i<_tag_count; i++) {
if (_tags[i] == entry->tag()) {
// The reference in this tag map could be the only (implicitly weak)
// reference to that object. If we hand it out, we need to keep it live wrt
// SATB marking similar to other j.l.ref.Reference referents. This is
// achieved by using a phantom load in the object() accessor.
oop o = entry->object();
if (o == NULL) {
_some_dead_found = true;
// skip this whole entry
return;
}
assert(o != NULL && Universe::heap()->is_in(o), "sanity check");
jobject ref = JNIHandles::make_local(_thread, o);
_object_results->append(ref);
_tag_results->append((uint64_t)entry->tag());
}
}
}
// return the results from the collection
//
jvmtiError result(jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
jvmtiError error;
int count = _object_results->length();
assert(count >= 0, "sanity check");
// if object_result_ptr is not NULL then allocate the result and copy
// in the object references.
if (object_result_ptr != NULL) {
error = _env->Allocate(count * sizeof(jobject), (unsigned char**)object_result_ptr);
if (error != JVMTI_ERROR_NONE) {
return error;
}
for (int i=0; i<count; i++) {
(*object_result_ptr)[i] = _object_results->at(i);
}
}
// if tag_result_ptr is not NULL then allocate the result and copy
// in the tag values.
if (tag_result_ptr != NULL) {
error = _env->Allocate(count * sizeof(jlong), (unsigned char**)tag_result_ptr);
if (error != JVMTI_ERROR_NONE) {
if (object_result_ptr != NULL) {
_env->Deallocate((unsigned char*)object_result_ptr);
}
return error;
}
for (int i=0; i<count; i++) {
(*tag_result_ptr)[i] = (jlong)_tag_results->at(i);
}
}
*count_ptr = count;
return JVMTI_ERROR_NONE;
}
};
// return the list of objects with the specified tags
jvmtiError JvmtiTagMap::get_objects_with_tags(const jlong* tags,
jint count, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
TagObjectCollector collector(env(), tags, count);
{
// iterate over all tagged objects
MutexLocker ml(lock(), Mutex::_no_safepoint_check_flag);
// Can't post ObjectFree events here from a JavaThread, so this
// will race with the gc_notification thread in the tiny
// window where the object is not marked but hasn't been notified that
// it is collected yet.
entry_iterate(&collector);
}
return collector.result(count_ptr, object_result_ptr, tag_result_ptr);
}
// helper to map a jvmtiHeapReferenceKind to an old style jvmtiHeapRootKind
// (not performance critical as only used for roots)
static jvmtiHeapRootKind toJvmtiHeapRootKind(jvmtiHeapReferenceKind kind) {
switch (kind) {
case JVMTI_HEAP_REFERENCE_JNI_GLOBAL: return JVMTI_HEAP_ROOT_JNI_GLOBAL;
case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS: return JVMTI_HEAP_ROOT_SYSTEM_CLASS;
case JVMTI_HEAP_REFERENCE_STACK_LOCAL: return JVMTI_HEAP_ROOT_STACK_LOCAL;
case JVMTI_HEAP_REFERENCE_JNI_LOCAL: return JVMTI_HEAP_ROOT_JNI_LOCAL;
case JVMTI_HEAP_REFERENCE_THREAD: return JVMTI_HEAP_ROOT_THREAD;
case JVMTI_HEAP_REFERENCE_OTHER: return JVMTI_HEAP_ROOT_OTHER;
default: ShouldNotReachHere(); return JVMTI_HEAP_ROOT_OTHER;
}
}
// Base class for all heap walk contexts. The base class maintains a flag
// to indicate if the context is valid or not.
class HeapWalkContext {
private:
bool _valid;
public:
HeapWalkContext(bool valid) { _valid = valid; }
void invalidate() { _valid = false; }
bool is_valid() const { return _valid; }
};
// A basic heap walk context for the deprecated heap walking functions.
// The context for a basic heap walk are the callbacks and fields used by
// the referrer caching scheme.
class BasicHeapWalkContext: public HeapWalkContext {
private:
jvmtiHeapRootCallback _heap_root_callback;
jvmtiStackReferenceCallback _stack_ref_callback;
jvmtiObjectReferenceCallback _object_ref_callback;
// used for caching
oop _last_referrer;
jlong _last_referrer_tag;
public:
BasicHeapWalkContext() : HeapWalkContext(false) { }
BasicHeapWalkContext(jvmtiHeapRootCallback heap_root_callback,
jvmtiStackReferenceCallback stack_ref_callback,
jvmtiObjectReferenceCallback object_ref_callback) :
HeapWalkContext(true),
_heap_root_callback(heap_root_callback),
_stack_ref_callback(stack_ref_callback),
_object_ref_callback(object_ref_callback),
_last_referrer(NULL),
_last_referrer_tag(0) {
}
// accessors
jvmtiHeapRootCallback heap_root_callback() const { return _heap_root_callback; }
jvmtiStackReferenceCallback stack_ref_callback() const { return _stack_ref_callback; }
jvmtiObjectReferenceCallback object_ref_callback() const { return _object_ref_callback; }
oop last_referrer() const { return _last_referrer; }
void set_last_referrer(oop referrer) { _last_referrer = referrer; }
jlong last_referrer_tag() const { return _last_referrer_tag; }
void set_last_referrer_tag(jlong value) { _last_referrer_tag = value; }
};
// The advanced heap walk context for the FollowReferences functions.
// The context is the callbacks, and the fields used for filtering.
class AdvancedHeapWalkContext: public HeapWalkContext {
private:
jint _heap_filter;
Klass* _klass_filter;
const jvmtiHeapCallbacks* _heap_callbacks;
public:
AdvancedHeapWalkContext() : HeapWalkContext(false) { }
AdvancedHeapWalkContext(jint heap_filter,
Klass* klass_filter,
const jvmtiHeapCallbacks* heap_callbacks) :
HeapWalkContext(true),
_heap_filter(heap_filter),
_klass_filter(klass_filter),
_heap_callbacks(heap_callbacks) {
}
// accessors
jint heap_filter() const { return _heap_filter; }
Klass* klass_filter() const { return _klass_filter; }
const jvmtiHeapReferenceCallback heap_reference_callback() const {
return _heap_callbacks->heap_reference_callback;
};
const jvmtiPrimitiveFieldCallback primitive_field_callback() const {
return _heap_callbacks->primitive_field_callback;
}
const jvmtiArrayPrimitiveValueCallback array_primitive_value_callback() const {
return _heap_callbacks->array_primitive_value_callback;
}
const jvmtiStringPrimitiveValueCallback string_primitive_value_callback() const {
return _heap_callbacks->string_primitive_value_callback;
}
};
// The CallbackInvoker is a class with static functions that the heap walk can call
// into to invoke callbacks. It works in one of two modes. The "basic" mode is
// used for the deprecated IterateOverReachableObjects functions. The "advanced"
// mode is for the newer FollowReferences function which supports a lot of
// additional callbacks.
class CallbackInvoker : AllStatic {
private:
// heap walk styles
enum { basic, advanced };
static int _heap_walk_type;
static bool is_basic_heap_walk() { return _heap_walk_type == basic; }
static bool is_advanced_heap_walk() { return _heap_walk_type == advanced; }
// context for basic style heap walk
static BasicHeapWalkContext _basic_context;
static BasicHeapWalkContext* basic_context() {
assert(_basic_context.is_valid(), "invalid");
return &_basic_context;
}
// context for advanced style heap walk
static AdvancedHeapWalkContext _advanced_context;
static AdvancedHeapWalkContext* advanced_context() {
assert(_advanced_context.is_valid(), "invalid");
return &_advanced_context;
}
// context needed for all heap walks
static JvmtiTagMap* _tag_map;
static const void* _user_data;
static GrowableArray<oop>* _visit_stack;
static JVMTIBitSet* _bitset;
// accessors
static JvmtiTagMap* tag_map() { return _tag_map; }
static const void* user_data() { return _user_data; }
static GrowableArray<oop>* visit_stack() { return _visit_stack; }
// if the object hasn't been visited then push it onto the visit stack
// so that it will be visited later
static inline bool check_for_visit(oop obj) {
if (!_bitset->is_marked(obj)) visit_stack()->push(obj);
return true;
}
// invoke basic style callbacks
static inline bool invoke_basic_heap_root_callback
(jvmtiHeapRootKind root_kind, oop obj);
static inline bool invoke_basic_stack_ref_callback
(jvmtiHeapRootKind root_kind, jlong thread_tag, jint depth, jmethodID method,
int slot, oop obj);
static inline bool invoke_basic_object_reference_callback
(jvmtiObjectReferenceKind ref_kind, oop referrer, oop referree, jint index);
// invoke advanced style callbacks
static inline bool invoke_advanced_heap_root_callback
(jvmtiHeapReferenceKind ref_kind, oop obj);
static inline bool invoke_advanced_stack_ref_callback
(jvmtiHeapReferenceKind ref_kind, jlong thread_tag, jlong tid, int depth,
jmethodID method, jlocation bci, jint slot, oop obj);
static inline bool invoke_advanced_object_reference_callback
(jvmtiHeapReferenceKind ref_kind, oop referrer, oop referree, jint index);
// used to report the value of primitive fields
static inline bool report_primitive_field
(jvmtiHeapReferenceKind ref_kind, oop obj, jint index, address addr, char type);
public:
// initialize for basic mode
static void initialize_for_basic_heap_walk(JvmtiTagMap* tag_map,
GrowableArray<oop>* visit_stack,
const void* user_data,
BasicHeapWalkContext context,
JVMTIBitSet* bitset);
// initialize for advanced mode
static void initialize_for_advanced_heap_walk(JvmtiTagMap* tag_map,
GrowableArray<oop>* visit_stack,
const void* user_data,
AdvancedHeapWalkContext context,
JVMTIBitSet* bitset);
// functions to report roots
static inline bool report_simple_root(jvmtiHeapReferenceKind kind, oop o);
static inline bool report_jni_local_root(jlong thread_tag, jlong tid, jint depth,
jmethodID m, oop o);
static inline bool report_stack_ref_root(jlong thread_tag, jlong tid, jint depth,
jmethodID method, jlocation bci, jint slot, oop o);
// functions to report references
static inline bool report_array_element_reference(oop referrer, oop referree, jint index);
static inline bool report_class_reference(oop referrer, oop referree);
static inline bool report_class_loader_reference(oop referrer, oop referree);
static inline bool report_signers_reference(oop referrer, oop referree);
static inline bool report_protection_domain_reference(oop referrer, oop referree);
static inline bool report_superclass_reference(oop referrer, oop referree);
static inline bool report_interface_reference(oop referrer, oop referree);
static inline bool report_static_field_reference(oop referrer, oop referree, jint slot);
static inline bool report_field_reference(oop referrer, oop referree, jint slot);
static inline bool report_constant_pool_reference(oop referrer, oop referree, jint index);
static inline bool report_primitive_array_values(oop array);
static inline bool report_string_value(oop str);
static inline bool report_primitive_instance_field(oop o, jint index, address value, char type);
static inline bool report_primitive_static_field(oop o, jint index, address value, char type);
};
// statics
int CallbackInvoker::_heap_walk_type;
BasicHeapWalkContext CallbackInvoker::_basic_context;
AdvancedHeapWalkContext CallbackInvoker::_advanced_context;
JvmtiTagMap* CallbackInvoker::_tag_map;
const void* CallbackInvoker::_user_data;
GrowableArray<oop>* CallbackInvoker::_visit_stack;
JVMTIBitSet* CallbackInvoker::_bitset;
// initialize for basic heap walk (IterateOverReachableObjects et al)
void CallbackInvoker::initialize_for_basic_heap_walk(JvmtiTagMap* tag_map,
GrowableArray<oop>* visit_stack,
const void* user_data,
BasicHeapWalkContext context,
JVMTIBitSet* bitset) {
_tag_map = tag_map;
_visit_stack = visit_stack;
_user_data = user_data;
_basic_context = context;
_advanced_context.invalidate(); // will trigger assertion if used
_heap_walk_type = basic;
_bitset = bitset;
}
// initialize for advanced heap walk (FollowReferences)
void CallbackInvoker::initialize_for_advanced_heap_walk(JvmtiTagMap* tag_map,
GrowableArray<oop>* visit_stack,
const void* user_data,
AdvancedHeapWalkContext context,
JVMTIBitSet* bitset) {
_tag_map = tag_map;
_visit_stack = visit_stack;
_user_data = user_data;
_advanced_context = context;
_basic_context.invalidate(); // will trigger assertion if used
_heap_walk_type = advanced;
_bitset = bitset;
}
// invoke basic style heap root callback
inline bool CallbackInvoker::invoke_basic_heap_root_callback(jvmtiHeapRootKind root_kind, oop obj) {
// if we heap roots should be reported
jvmtiHeapRootCallback cb = basic_context()->heap_root_callback();
if (cb == NULL) {
return check_for_visit(obj);
}
CallbackWrapper wrapper(tag_map(), obj);
jvmtiIterationControl control = (*cb)(root_kind,
wrapper.klass_tag(),
wrapper.obj_size(),
wrapper.obj_tag_p(),
(void*)user_data());
// push root to visit stack when following references
if (control == JVMTI_ITERATION_CONTINUE &&
basic_context()->object_ref_callback() != NULL) {
visit_stack()->push(obj);
}
return control != JVMTI_ITERATION_ABORT;
}
// invoke basic style stack ref callback
inline bool CallbackInvoker::invoke_basic_stack_ref_callback(jvmtiHeapRootKind root_kind,
jlong thread_tag,
jint depth,
jmethodID method,
int slot,
oop obj) {
// if we stack refs should be reported
jvmtiStackReferenceCallback cb = basic_context()->stack_ref_callback();
if (cb == NULL) {
return check_for_visit(obj);
}
CallbackWrapper wrapper(tag_map(), obj);
jvmtiIterationControl control = (*cb)(root_kind,
wrapper.klass_tag(),
wrapper.obj_size(),
wrapper.obj_tag_p(),
thread_tag,
depth,
method,
slot,
(void*)user_data());
// push root to visit stack when following references
if (control == JVMTI_ITERATION_CONTINUE &&
basic_context()->object_ref_callback() != NULL) {
visit_stack()->push(obj);
}
return control != JVMTI_ITERATION_ABORT;
}
// invoke basic style object reference callback
inline bool CallbackInvoker::invoke_basic_object_reference_callback(jvmtiObjectReferenceKind ref_kind,
oop referrer,
oop referree,
jint index) {
BasicHeapWalkContext* context = basic_context();
// callback requires the referrer's tag. If it's the same referrer
// as the last call then we use the cached value.
jlong referrer_tag;
if (referrer == context->last_referrer()) {
referrer_tag = context->last_referrer_tag();
} else {
referrer_tag = tag_for(tag_map(), referrer);
}
// do the callback
CallbackWrapper wrapper(tag_map(), referree);
jvmtiObjectReferenceCallback cb = context->object_ref_callback();
jvmtiIterationControl control = (*cb)(ref_kind,
wrapper.klass_tag(),
wrapper.obj_size(),
wrapper.obj_tag_p(),
referrer_tag,
index,
(void*)user_data());
// record referrer and referrer tag. For self-references record the
// tag value from the callback as this might differ from referrer_tag.
context->set_last_referrer(referrer);
if (referrer == referree) {
context->set_last_referrer_tag(*wrapper.obj_tag_p());
} else {
context->set_last_referrer_tag(referrer_tag);
}
if (control == JVMTI_ITERATION_CONTINUE) {
return check_for_visit(referree);
} else {
return control != JVMTI_ITERATION_ABORT;
}
}
// invoke advanced style heap root callback
inline bool CallbackInvoker::invoke_advanced_heap_root_callback(jvmtiHeapReferenceKind ref_kind,
oop obj) {
AdvancedHeapWalkContext* context = advanced_context();
// check that callback is provided
jvmtiHeapReferenceCallback cb = context->heap_reference_callback();
if (cb == NULL) {
return check_for_visit(obj);
}
// apply class filter
if (is_filtered_by_klass_filter(obj, context->klass_filter())) {
return check_for_visit(obj);
}
// setup the callback wrapper
CallbackWrapper wrapper(tag_map(), obj);
// apply tag filter
if (is_filtered_by_heap_filter(wrapper.obj_tag(),
wrapper.klass_tag(),
context->heap_filter())) {
return check_for_visit(obj);
}
// for arrays we need the length, otherwise -1
jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1);
// invoke the callback
jint res = (*cb)(ref_kind,
NULL, // referrer info
wrapper.klass_tag(),
0, // referrer_class_tag is 0 for heap root
wrapper.obj_size(),
wrapper.obj_tag_p(),
NULL, // referrer_tag_p
len,
(void*)user_data());
if (res & JVMTI_VISIT_ABORT) {
return false;// referrer class tag
}
if (res & JVMTI_VISIT_OBJECTS) {
check_for_visit(obj);
}
return true;
}
// report a reference from a thread stack to an object
inline bool CallbackInvoker::invoke_advanced_stack_ref_callback(jvmtiHeapReferenceKind ref_kind,
jlong thread_tag,
jlong tid,
int depth,
jmethodID method,
jlocation bci,
jint slot,
oop obj) {
AdvancedHeapWalkContext* context = advanced_context();
// check that callback is provider
jvmtiHeapReferenceCallback cb = context->heap_reference_callback();
if (cb == NULL) {
return check_for_visit(obj);
}
// apply class filter
if (is_filtered_by_klass_filter(obj, context->klass_filter())) {
return check_for_visit(obj);
}
// setup the callback wrapper
CallbackWrapper wrapper(tag_map(), obj);
// apply tag filter
if (is_filtered_by_heap_filter(wrapper.obj_tag(),
wrapper.klass_tag(),
context->heap_filter())) {
return check_for_visit(obj);
}
// setup the referrer info
jvmtiHeapReferenceInfo reference_info;
reference_info.stack_local.thread_tag = thread_tag;
reference_info.stack_local.thread_id = tid;
reference_info.stack_local.depth = depth;
reference_info.stack_local.method = method;
reference_info.stack_local.location = bci;
reference_info.stack_local.slot = slot;
// for arrays we need the length, otherwise -1
jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1);
// call into the agent
int res = (*cb)(ref_kind,
&reference_info,
wrapper.klass_tag(),
0, // referrer_class_tag is 0 for heap root (stack)
wrapper.obj_size(),
wrapper.obj_tag_p(),
NULL, // referrer_tag is 0 for root
len,
(void*)user_data());
if (res & JVMTI_VISIT_ABORT) {
return false;
}
if (res & JVMTI_VISIT_OBJECTS) {
check_for_visit(obj);
}
return true;
}
// This mask is used to pass reference_info to a jvmtiHeapReferenceCallback
// only for ref_kinds defined by the JVM TI spec. Otherwise, NULL is passed.
#define REF_INFO_MASK ((1 << JVMTI_HEAP_REFERENCE_FIELD) \
| (1 << JVMTI_HEAP_REFERENCE_STATIC_FIELD) \
| (1 << JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT) \
| (1 << JVMTI_HEAP_REFERENCE_CONSTANT_POOL) \
| (1 << JVMTI_HEAP_REFERENCE_STACK_LOCAL) \
| (1 << JVMTI_HEAP_REFERENCE_JNI_LOCAL))
// invoke the object reference callback to report a reference
inline bool CallbackInvoker::invoke_advanced_object_reference_callback(jvmtiHeapReferenceKind ref_kind,
oop referrer,
oop obj,
jint index)
{
// field index is only valid field in reference_info
static jvmtiHeapReferenceInfo reference_info = { 0 };
--> --------------------
--> maximum size reached
--> --------------------
[ Verzeichnis aufwärts0.81unsichere Verbindung
Übersetzung europäischer Sprachen durch Browser
]
|