forked from openjdk/lilliput
/
jvmtiTagMap.cpp
3096 lines (2664 loc) · 107 KB
/
jvmtiTagMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2003, 2021, 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/biasedLocking.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/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/thread.inline.hpp"
#include "runtime/threadSMR.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vmThread.hpp"
#include "runtime/vmOperations.hpp"
#include "utilities/macros.hpp"
bool JvmtiTagMap::_has_object_free_events = false;
// create a JvmtiTagMap
JvmtiTagMap::JvmtiTagMap(JvmtiEnv* env) :
_env(env),
_lock(Mutex::nonleaf+1, "JvmtiTagMap_lock", Mutex::_allow_vm_block_flag,
Mutex::_safepoint_check_never),
_needs_rehashing(false),
_needs_cleaning(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 and rehashing before operations that
// this tagmap table. The calls from a JavaThread only rehash, posting is
// only done before heap walks.
void JvmtiTagMap::check_hashmap(bool post_events) {
assert(!post_events || SafepointSynchronize::is_at_safepoint(), "precondition");
assert(is_locked(), "checking");
if (is_empty()) { return; }
if (_needs_cleaning &&
post_events &&
env()->is_enabled(JVMTI_EVENT_OBJECT_FREE)) {
remove_dead_entries_locked(true /* post_object_free */);
}
if (_needs_rehashing) {
log_info(jvmti, table)("TagMap table needs rehashing");
hashmap()->rehash();
_needs_rehashing = false;
}
}
// This checks for posting and rehashing and is called from the heap walks.
void JvmtiTagMap::check_hashmaps_for_heapwalk() {
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(/*post_events*/ true);
}
}
}
// 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(/*post_events*/ false);
// 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(/*post_events*/ false);
// 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 (ResourceObj::C_HEAP, 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 (ResourceObj::C_HEAP, 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;
public:
VM_HeapIterateOperation(ObjectClosure* blk) { _blk = blk; }
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();
// 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. Similiarly, 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;