forked from RedlineResearch/elephant-tracks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathETCallBackHandler.cpp
More file actions
2591 lines (2198 loc) · 88.6 KB
/
ETCallBackHandler.cpp
File metadata and controls
2591 lines (2198 loc) · 88.6 KB
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
#include <agent_util.h>
#include <java_crw_demo.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <algorithm>
#include <GraphAlgorithms.h>
#include <assert.h>
#include <sstream>
#include <limits>
#include <cstddef>
#include "ETCallBackHandler.h"
#include "FlatTraceOutputter.h"
#include "main.h"
#include "RootRecord.h"
//#include <boost/graph/graph_traits.hpp>
//#include <boost/graph/adjacency_list.hpp>
//#include <boost/graph/topological_sort.hpp>
#include "TagTimeMap.h"
using namespace std;
//using namespace boost;
#define ATOMICALLY(x) enter_critical_section(theJVMTI); x exit_critical_section(theJVMTI);
//#define TRACE_NEW_OBJECTS 1
//#define TRACE_AGENT_RECORDING 1
#define ACC_STATIC 8 // from JVM Spec
#define SYNTHETIC_ID_BASE 0x10000000 // allows for 256 million classes/fields
static jvmtiEnv *theJVMTI;
jniNativeInterface *origJNIEnv; // the original vector of JNI functions
jniNativeInterface *instJNIEnv; // our replacement, allowing instrumentation
static int useFollowReferences = 0;
// information for getting something things from Java java/lang/reflect/Field objects
bool haveFieldDeclaringClassId = false;
jfieldID FieldDeclaringClassId;
bool haveFieldNameId = false;
jfieldID FieldNameId;
bool haveFieldTypeId = false;
jfieldID FieldTypeId;
static void JNICALL cleanerStaticWrapper (jvmtiEnv *jvmti, JNIEnv* jni, void* data) {
// if we are doing IterateThroughHeap, don't use the weak ref cleaner
if (useFollowReferences) {
ETCallBackHandler* etCallBackHandler = (ETCallBackHandler*)data;
etCallBackHandler->weakRefCleanerThreadStart(jvmti, jni);
}
}
static tbb::atomic<jlong> nextTag; //The next object tag to use, we skip 0 since we use that to identify null
ETCallBackHandler::ETCallBackHandler (jvmtiEnv *jvmti,
JavaVM*,
FlatTraceOutputter* traceOutputter)
{
timeStampTag_time = 0;
computeDeathTimes_time = 0;
fakeAGC_time = 0;
deadFinder_time = 0;
GCEnd_time = 0;
computeReachable_time = 0;
deadSetAdjustment_time = 0;
doGCEnd_time = 0;
nativeVarEvent_time = 0;
nextTag = 1; // we skip 0, since they means "null"
this->traceOutputter = traceOutputter;
this->aJVMTI = jvmti;
theJVMTI = jvmti;
this->currentTime = 0;
totalHeapReads = 0;
totalHeapWrites = 0;
totalHeapRefReads = 0;
totalHeapRefWrites = 0;
totalInstCount = 0;
classId2slots[0] = 0; // superclass of Object has size 0
staticSlots = 0; // may not be necessary, but ...
nextSyntheticId = SYNTHETIC_ID_BASE;
jvmti->CreateRawMonitor("heapLock" , &heapLock );
jvmti->CreateRawMonitor("deadSetLock" , &deadSetLock );
jvmti->CreateRawMonitor("clockLock" , &clockLock );
jvmti->CreateRawMonitor("tagLock" , &tagLock );
jvmti->CreateRawMonitor("countsLock" , &countsLock );
jvmti->CreateRawMonitor("weakRefMon" , &weakRefMon );
// jvmti->CreateRawMonitor("tag2TimeStampLock", &tag2TimeStampLock);
// jvmti->CreateRawMonitor("arrayShadowLock" , &arrayShadowLock );
GCsToProcess = 0;
weakRefCleaner = new WeakRefCleaner(jvmti, weakRefMon, this);
}
void ETCallBackHandler::lockOn (jrawMonitorID lock) {
jvmtiError error = aJVMTI->RawMonitorEnter(lock);
check_jvmti_error(aJVMTI, error, "ETCallBackHandler::lockOn: Cannot enter with raw monitor");
}
void ETCallBackHandler::unlockOn (jrawMonitorID lock) {
jvmtiError error = aJVMTI->RawMonitorExit(lock);
check_jvmti_error(aJVMTI, error, "ETCallBackHandler::unlockOn:Cannot exit with raw monitor");
}
jthread ETCallBackHandler::getCurrentThread () {
jthread currentThread;
jvmtiError err = aJVMTI->GetCurrentThread(¤tThread);
check_jvmti_error(aJVMTI, err, "getCurrentThread: could not get current thread");
return currentThread;
}
void ETCallBackHandler::nativeMethodEntry (JNIEnv *jni,
jclass,
jobject thread,
jint methodId,
jobject receiver) {
jthread currentThread = (jthread)thread;
jlong newTime = incTime();
jlong receiverTag = getTagAndTagIfNeeded(jni, receiver);
jlong threadTag = (currentThread == 0 ? 0 : getTagAndTagIfNeeded(jni, currentThread));
this->traceOutputter->doMethodEntry(newTime, methodId, receiverTag, threadTag);
}
void ETCallBackHandler::nativeMethodExit (JNIEnv *jni, jclass klass, jobject thread, jobject exception,
jint methodId, jobject receiver) {
jthread currentThread = (jthread)thread;
this->nativeVarEvent(jni, klass, 2, receiver, exception, NULL, NULL, NULL);
jlong newTime = incTime();
jlong receiverTag = getTagAndTagIfNeeded(jni, receiver );
jlong threadTag = getTagAndTagIfNeeded(jni, currentThread);
jlong excTag = getTagAndTagIfNeeded(jni, exception );
this->traceOutputter->doMethodExit(newTime, methodId, receiverTag, excTag, threadTag);
}
void ETCallBackHandler::nativeException (JNIEnv *jni, jclass klass, jobject thread, jobject exception,
jint methodId, jobject receiver, ExceptKind kind) {
jthread currentThread = (jthread)thread;
jlong newTime = incTime();
jlong receiverTag = getTagAndTagIfNeeded(jni, receiver );
jlong threadTag = getTagAndTagIfNeeded(jni, currentThread);
jlong excTag = getTagAndTagIfNeeded(jni, exception );
traceOutputter->doException(newTime, methodId, receiverTag, excTag, threadTag, kind);
this->nativeVarEvent(jni, klass, 2, receiver, exception, NULL, NULL, NULL);
}
void ETCallBackHandler::nativeExceptionThrow (JNIEnv *jni, jclass klass, jobject thread, jobject exception,
jint methodId, jobject receiver) {
nativeException(jni, klass, thread, exception, methodId, receiver, ExceptThrow);
}
void ETCallBackHandler::nativeExceptionHandle (JNIEnv *jni, jclass klass, jobject thread, jobject exception,
jint methodId, jobject receiver) {
nativeException(jni, klass, thread, exception, methodId, receiver, ExceptHandle);
}
bool vmDead () {
return false;
}
ETCallBackHandler::~ETCallBackHandler () { }
jlong ETCallBackHandler::readNextTag () {
return nextTag; // useful for debug messages, etc.
}
jlong ETCallBackHandler::tagObject (jvmtiEnv *jvmti, jobject o, bool *newTag) {
jlong thisTag;
if (nextTag == 0) {
verbose_println("!! tagObject: nextTag is 0.");
}
bool isNew = false;
// EBM: This should be done under a lock and should check first that the
// tag is not yet set. Even with the lock, it is best to use atomic access to nextTag.
lockOn(tagLock);
jvmtiError err = jvmti->GetTag(o, &thisTag);
check_jvmti_error(this->aJVMTI, err, "ETCallBackHandler::tagObject: Could not get tag.");
if (thisTag == 0) {
err = jvmti->SetTag(o, (thisTag = nextTag++));
check_jvmti_error(this->aJVMTI, err, "ETCallBackHandler::tagObject: Could not set tag.");
isNew = true;
}
unlockOn(tagLock);
#ifdef ET_DEBUG
{
static set<jlong> tagsUsed;
assert(tagsUsed.count(thisTag) == 0);
tagsUsed.insert(thisTag);
//Check that this tag is used for only one object.
//It is only possible to call GetObjectsWithTags
//in the live phase, so we cannot do this check
//if we are in another phase.
jvmtiPhase phase;
err = jvmti->GetPhase(&phase);
check_jvmti_error(jvmti, err, "ETCallBackHandler::tagObject: could not get phase.");
if (phase == JVMTI_PHASE_LIVE) {
jlong tags[1];
tags[0] = thisTag;
jint getTagCount = 0;
err = jvmti->GetObjectsWithTags(1, tags, &getTagCount, NULL, NULL);
check_jvmti_error(this->aJVMTI, err,
"ETCallBackHandler::tagObject: Could not get objects with tag: " + thisTag);
assert(getTagCount == 1);
}
}
#endif
if (newTag != NULL) {
*newTag = isNew;
}
return thisTag;
}
jlong ETCallBackHandler::getNextTag () {
// sync should be enough for this (should not also need a lock)
lockOn(tagLock);
jlong result = nextTag++;
unlockOn(tagLock);
return result;
}
void ETCallBackHandler::addObjectToHeap (jlong tag, int slots) {
heap.addNode(tag, slots);
}
void ETCallBackHandler::nativeNewObj (JNIEnv *jni, jclass klass,
jobject thread, jobject obj, bool isVM, jint site) {
verbose_println("ETCallBackHandler::nativeNewObj called.");
if (vmDead()) {
return;
}
// tag thread first for more consistent order of P and A records in the output
jlong threadTag = getTagAndTagIfNeeded(jni, thread);
assert(threadTag != 0);
// get class now so that we can check for arrays and threads
jclass classOfObj = jni->GetObjectClass(obj);
char *classSig;
jvmtiError err = aJVMTI->GetClassSignature(classOfObj, &classSig, NULL);
check_jvmti_error(aJVMTI, err,
"ETCallBackHandler::nativeNewObj: could not get class signature.");
AllocKind kind = (isVM ? AllocVM : AllocArray);
this->nativeNewObj(jni, klass, thread, obj, classSig, kind, site);
#ifdef TRACE_NEW_OBJECT
jlong size;
err = aJVMTI->GetObjectSize(obj, &size);
check_jvmti_error(this->aJVMTI, err, "ETCallBackHandler::nativeNewObj: could not get object size.");
ATOMICALLY(
cerr << (char)kind << " " << tag << " " << size << " " << classSig << " " << threadTag << endl;
)
#endif
#ifdef DEBUG_MULTINODE
if (strcmp(classSig, "LtestPrograms/MultiNode;") == 0) {
ATOMICALLY(
cerr << hex << "NewObj: Added thing to multiNodes: 0x" << tag << endl;
)
multiNodes.push_back(tag);
}
#endif
aJVMTI->Deallocate((unsigned char *)classSig);
}
void ETCallBackHandler::nativeNewObj (JNIEnv *jni,
jclass,
jobject thread,
jobject obj,
const char *className,
AllocKind kind,
jint site) {
jlong threadTag = getTagAndTagIfNeeded(jni, thread);
jlong objTag = 0;
jvmtiError err = aJVMTI->GetTag(obj, &objTag);
check_jvmti_error(aJVMTI, err, "ETCallBackHandler::nativeNewObject: could not get object's tag..");
if (objTag == 0) {
bool isNew;
objTag = tagObject(aJVMTI, obj, &isNew);
jlong size;
err = aJVMTI->GetObjectSize(obj, &size);
//the lenght field has no meaning for things that are not arrays
int length = -1;
if (className[0] == '[') {
length = jni->GetArrayLength((jarray)obj);
}
this->traceOutputter->doAllocation(currentTime, objTag, size, length, className, threadTag, kind, site);
if (isNew) {
jclass objClass = jni->GetObjectClass(obj);
jlong classTag = getTagAndTagIfNeeded(jni, objClass);
int slots = (length >= 0 ? length : getSlotsForClass(jni, objClass, classTag));
addObjectToHeap(objTag, slots);
}
}
timeStampTag(objTag, threadTag);
}
void ETCallBackHandler::nativeNewObj (JNIEnv *jni,
jclass klass,
jobject thread,
jstring className,
jint slot,
jint site) {
// obtain the object, and the className as a char *, and pass the buck
jobject obj;
jvmtiError err = aJVMTI->GetLocalObject((jthread)thread, (jint)2, slot, &obj);
check_jvmti_error(aJVMTI, err,
"ETCallBackHandler::nativeNewObj: Could not GetLocalObject");
const char *cname = jni->GetStringUTFChars(className, NULL);
this->nativeNewObj(jni, klass, thread, obj, cname, AllocNew, site);
jni->ReleaseStringUTFChars(className, cname);
}
jlong ETCallBackHandler::getTagAndTagIfNeeded (JNIEnv* jni, jobject o) {
if (jni->IsSameObject(o, NULL)) {
//if the object is null, return 0 as its tag
return 0;
}
jlong tag;
jvmtiError err = aJVMTI->GetTag(o, &tag);
check_jvmti_error(aJVMTI, err,
"ETCallBackHandler::getTagAndTagIfNeeded: Could not get tag from object");
if (tag == 0) {
bool isNew;
tag = tagObject(this->aJVMTI, o, &isNew);
if (tag == 0) {
verbose_println("!! ETCallBackHandler::GetTagAndTagIfNeeded: got a zero tag.");
}
#ifdef TRACE_NEW_OBJECTS
ATOMICALLY(
cerr << hex << "tagged object " << tag << endl;
)
#endif
if (isNew) {
jlong size = 0;
err = aJVMTI->GetObjectSize(o, &size);
check_jvmti_error(this->aJVMTI, err, "ETCallBackHandler::getTagAndTagIfNeeded: could not get object size.");
jclass classOfo = jni->GetObjectClass(o);
char *classSig;
err = aJVMTI->GetClassSignature(classOfo, &classSig, NULL);
check_jvmti_error(this->aJVMTI, err,
"ETCallBackhandler::getTagAndTagIfNeeded: could not get class signature.");
jthread currentThread;
err = aJVMTI->GetCurrentThread(¤tThread);
check_jvmti_error(aJVMTI, err, "getTagAndTagIfNeeded: could not get current thread");
jlong threadTag;
err = aJVMTI->GetTag(currentThread, &threadTag);
check_jvmti_error(aJVMTI, err,
"ETCallBackHander::getTagAndTagIfNeeded: Could not get tag from thread");
#ifdef TRACE_NEW_OBJECT
ATOMICALLY(
cerr << "P " << tag << " " << size << " " << classSig << " " << threadTag << endl;
)
#endif
int length = -1;
if (classSig[0] == '[') {
length = jni->GetArrayLength((jarray)o);
}
this->traceOutputter->doAllocation(currentTime, tag, size, length, classSig,
threadTag, AllocPreexisting, 0);
timeStampTag(tag, threadTag);
jlong classTag = getTagAndTagIfNeeded(jni, classOfo);
int slots = (length >= 0 ? length : getSlotsForClass(jni, classOfo, classTag));
addObjectToHeap(tag, slots);
aJVMTI->Deallocate((unsigned char*)classSig);
}
}
return tag;
}
void ETCallBackHandler::timeStampTag (jlong tag, jlong thread_tag) {
HL::Timer time;
IF_TIMING(time.start());
///Time stamping null doesn't make sense, ignore it.
// Maybe we should tell the caller not to do this?
if (tag != 0) {
tagTimeMap[tag] = TimeStamp(this->currentTime, thread_tag);
}
IF_TIMING( \
time.stop(); \
timeStampTag_time += time.getElapsedTime(); \
);
}
void ETCallBackHandler::ObjectFree (jvmtiEnv*, jlong tag) {
#ifdef ET_DEBUG
{ //Check that we don't get ObjectFree calls twice for the same object.
static set<jlong> seenObjects;
ATOMICALLY(
cerr << hex << seenObjects.count(tag) << " Object ID: 0x" << tag
<< "Time stamp: " << tagTimeMap[tag] << endl;
)
assert(seenObjects.count(tag) == 0);
seenObjects.insert(tag);
}
#endif
#ifdef DEBUG_MULTINODE
if (isMultiNode(tag)) {
ATOMICALLY(
cerr << hex << "ObjectFree: MultiNode Free 0x" << tag << endl;
)
}
#endif
if (tag == 0) {
// cerr << "ObjectFree: Got Zero Tag" << endl;
} else {
deadSet.insert(tag);
}
}
void ETCallBackHandler::pointerSlotUpdated (JNIEnv *jni,
jobject thread,
jobject origin,
jlong originTag,
jobject newTarget,
jlong newTargetTag,
int fieldId,
int slot,
bool isArray) {
jlong thread_tag = getTagAndTagIfNeeded(jni, thread);
timeStampTag(originTag, thread_tag);
jlong oldTargetTag = updateHeapAtAndStamp(originTag, slot, newTargetTag, thread_tag);
timeStampTag(oldTargetTag, thread_tag);
this->traceOutputter->doPointerUpdate(currentTime,
oldTargetTag,
originTag,
newTargetTag,
fieldId,
getTagAndTagIfNeeded(jni, thread));
if ((!isArray) && (fieldId == referentFieldNumber)) {
weakRefCleaner->updateReferent(theJVMTI, jni, origin, originTag, newTarget, newTargetTag);
}
}
void ETCallBackHandler::nativePointerUpdated (JNIEnv *jni,
jclass,
jobject thread,
jobject origin,
jobject newTarget,
jint fieldId) {
// A null origin indicates this is an update of a static field
jlong originTag = getTagAndTagIfNeeded(jni, origin );
jlong newTargetTag = getTagAndTagIfNeeded(jni, newTarget);
int slot = fieldId2slot[fieldId];
pointerSlotUpdated(jni, thread, origin, originTag, newTarget, newTargetTag, fieldId, slot, false);
}
void ETCallBackHandler::nativePointerUpdated (JNIEnv *jni,
jclass,
jobject thread,
jobject origin,
jobject newTarget,
jlong offset) {
jlong originTag = getTagAndTagIfNeeded(jni, origin );
jlong newTargetTag = getTagAndTagIfNeeded(jni, newTarget);
#ifdef TRACE_AGENT_RECORDING
cerr << "nativePointerUpdated called: origin 0x" << hex << originTag
<< " newTarget 0x" << hex << newTargetTag
<< " offset " << dec << offset
<< endl;
#endif
// is this a static or not?
TagAndOffset2FieldIdMap::const_iterator fieldIdIter =
tagAndOffset2fieldId.find(make_pair(originTag, offset));
if (fieldIdIter != tagAndOffset2fieldId.end()) {
// static field
int fieldId = fieldIdIter->second;
int slot = fieldId2slot[fieldId];
#ifdef TRACE_AGENT_RECORDING
cerr << "nativePointerUpdated: static via offset: origin 0x" << hex << originTag
<< " offset " << dec << offset
<< " fieldId 0x" << hex << fieldId
<< " slot " << dec << slot
<< endl;
#endif
pointerSlotUpdated(jni, thread, 0, 0, newTarget, newTargetTag, fieldId, slot, false);
return;
}
// instance; sanity check for null
if (origin == NULL) {
// TODO: emit warning message
return;
}
// non-null instance: examine its class
jclass originClass = jni->GetObjectClass(origin);
jlong classTag = getTagAndTagIfNeeded(jni, originClass);
// is this an array?
jboolean isArrayClass;
jvmtiError err = theJVMTI->IsArrayClass(originClass, &isArrayClass);
check_jvmti_error(theJVMTI,err,"ETCallBackHandler::nativePointerUpdate: Could not determine IsArrayClass");
if (isArrayClass) {
ArrayClassTag2IntMap::const_iterator it = arrayClassTag2baseOffset.find(classTag);
if (it == arrayClassTag2baseOffset.end()) {
}
int base = it->second;
it = arrayClassTag2indexScale.find(classTag);
if (it == arrayClassTag2indexScale.end()) {
}
int scale = it->second;
int index = (offset - base) / scale;
int size = jni->GetArrayLength((jarray)origin);
#ifdef TRACE_AGENT_RECORDING
cerr << "nativePointerUpdated: array via offset: origin 0x" << hex << originTag
<< " offset " << dec << offset
<< " base " << dec << base
<< " scale " << dec << scale
<< " size " << dec << size
<< endl;
#endif
if (index < 0 || index >= size) {
// index out of range; nothing to do
return;
}
pointerSlotUpdated(jni, thread, origin, originTag, newTarget, newTargetTag, index, index, true);
return;
}
// instance, but not an array: field may have been declared in a superclass
jclass tryClass = originClass;
int fieldId;
while (true) {
fieldIdIter = tagAndOffset2fieldId.find(make_pair(classTag, offset));
if (fieldIdIter != tagAndOffset2fieldId.end()) {
fieldId = fieldIdIter->second;
if (tryClass != originClass) {
// cache the mapping we found
tagAndOffset2fieldId[make_pair(classTag, offset)] = fieldId;
}
break;
}
tryClass = jni->GetSuperclass(tryClass);
if (tryClass == NULL) {
// could not find field
// TODO: add warning message
return;
}
getTagAndTagIfNeeded(jni, tryClass);
}
int slot = fieldId2slot[fieldId];
#ifdef TRACE_AGENT_RECORDING
cerr << "nativePointerUpdated: instance via offset: origin 0x" << hex << originTag
<< " offset " << dec << offset
<< " fieldId 0x" << hex << fieldId
<< " slot " << dec << slot
<< endl;
#endif
pointerSlotUpdated(jni, thread, origin, originTag, newTarget, newTargetTag, fieldId, slot, false);
}
static set<jlong> staticsScannedClassTags; // also include interfaces
static jclass classClass = NULL;
void ETCallBackHandler::scanStaticsOf (jvmtiEnv *jvmti, JNIEnv *jni, queue<jclass> toScan) {
jvmtiError err;
while (!toScan.empty()) {
jclass cls = toScan.front();
toScan.pop();
jlong clsTag = getTag(jni, cls);
// push superclass, if necessary
jclass super = jni->GetSuperclass(cls);
if (super != NULL) {
jlong superTag = getTag(jni, super);
if (staticsScannedClassTags.count(superTag) == 0) {
staticsScannedClassTags.insert(superTag);
toScan.push(super);
}
}
jint status;
err = jvmti->GetClassStatus(cls, &status);
check_jvmti_error(jvmti, err, "scanStaticsOf: could not get class status");
if ((status & JVMTI_CLASS_STATUS_ARRAY) != 0) {
// array classes have no static fields
continue;
} else if ((status & JVMTI_CLASS_STATUS_PRIMITIVE) != 0) {
// not sure if int.class, etc., have static fields:
// do nothing (fall through)
} else if ((status & JVMTI_CLASS_STATUS_PREPARED) == 0) {
// if a class it not prepared, we cannot get its methods, implemented
// interfaces, etc., so we must skip it
continue;
}
// push implemented interfaces, as needed
jint numIntfs;
jclass *intfs;
err = jvmti->GetImplementedInterfaces(cls, &numIntfs, &intfs);
check_jvmti_error(jvmti, err, "scanStaticsOf: Could not get implemented interfaces");
for (int i = 0; i < numIntfs; ++i) {
jclass intf = intfs[i];
jlong intfTag = getTag(jni, intf);
if (staticsScannedClassTags.count(intfTag) == 0) {
staticsScannedClassTags.insert(intfTag);
toScan.push(intf);
}
}
err = jvmti->Deallocate((unsigned char *)intfs);
check_jvmti_error(jvmti, err, "scanStaticsOf: Could not deallocate interfaces");
// now scan fields of this class/interface
jint numFields;
jfieldID *fields;
err = jvmti->GetClassFields(cls, &numFields, &fields);
check_jvmti_error(jvmti, err, "scanStaticsOf: Could not get class fields");
string className = getClassName(jni, cls, clsTag);
int classId = className2classId[className];
FieldsInfo finfo = classId2fieldsInfo[classId];
for (int i = 0; i < numFields; ++i) {
jfieldID fid = fields[i];
jint mods;
err = jvmti->GetFieldModifiers(cls, fid, &mods);
check_jvmti_error(jvmti, err, "scanStaticsOf: Could not get field modifiers");
if ((mods & ACC_STATIC) == 0) {
continue;
}
char *fname;
char *fsig;
err = jvmti->GetFieldName(cls, fid, &fname, &fsig, NULL);
check_jvmti_error(jvmti, err, "scanStaticsOf: Could not get field name");
int agentFieldId = -1;
char kind = fsig[0];
if (kind == 'L' || kind == '[') {
string fieldName((const char *)fname);
for (FieldsInfo::const_iterator fiit = finfo.begin(); fiit != finfo.end(); ++fiit) {
if (fiit->first == fieldName) {
agentFieldId = -fiit->second;
break;
}
}
}
err = jvmti->Deallocate((unsigned char *)fname);
check_jvmti_error(jvmti, err, "scanStaticsOf: Could not deallocate field name");
err = jvmti->Deallocate((unsigned char *)fsig);
check_jvmti_error(jvmti, err, "scanStaticsOf: Could not deallocate field signature");
if (agentFieldId < 0) {
continue;
}
FieldId2slotMap::const_iterator slotIt = fieldId2slot.find(agentFieldId);
if (slotIt == fieldId2slot.end()) {
continue;
}
int slot = fieldId2slot[agentFieldId];
jobject newTarget = jni->GetStaticObjectField(cls, fid);
jlong newTargetTag = 0;
if (newTarget != NULL) {
err = jvmti->GetTag(newTarget, &newTargetTag);
check_jvmti_error(jvmti, err, "scanStaticsOf: Could not get netTarget tag");
}
jlong oldTargetTag = this->updateHeapAtAndStamp(0,
slot,
newTargetTag,
getTagAndTagIfNeeded(jni, getCurrentThread()));
if (newTargetTag != oldTargetTag) {
this->traceOutputter->doSyntheticUpdate(0, oldTargetTag, 0, newTargetTag, agentFieldId);
}
}
err = jvmti->Deallocate((unsigned char *)fields);
check_jvmti_error(jvmti, err, "scanStaticsOf: Could not deallocate fields");
}
}
void ETCallBackHandler::handleInitialObject (jvmtiEnv *jvmti, JNIEnv *jni, jobject obj, jlong tag) {
if (obj == NULL) {
return; // shouldn't happen, but for safety ...
}
jclass cls = jni->GetObjectClass(obj);
jlong classTag = getTag(jni, cls);
jlong size;
jvmtiError err = jvmti->GetObjectSize(obj, &size);
check_jvmti_error(jvmti, err, "handleInitialObject: could not get object size");
string type = getClassSignature(jni, cls, classTag);
if (type[0] == '[') {
jobjectArray array = (jobjectArray)obj;
jsize length = jni->GetArrayLength(array);
this->traceOutputter->doAllocation(0, tag, size, length, type, 0, AllocInitialHeap, 0);
char kind = type[1];
if (kind != '[' && kind != 'L') {
// array of primitive: no fields
this->addObjectToHeap(tag, 0);
return;
}
// array of references
this->addObjectToHeap(tag, length);
for (int i = 0; i < length; ++i) {
jobject element = jni->GetObjectArrayElement(array, i);
jlong elementTag = getTag(jni, element);
if (element != NULL) {
jlong oldElementTag = this->updateHeapAt(tag, i, elementTag);
if (oldElementTag != elementTag) {
this->traceOutputter->doSyntheticUpdate(0, oldElementTag, tag, elementTag, i);
}
}
}
return;
}
if (classClass == NULL) {
jclass cc = jni->FindClass("java/lang/Class");
classClass = (jclass)jni->NewGlobalRef(cc);
}
// for Class objects, scan statics
if (jni->IsInstanceOf(obj, classClass)) {
if (staticsScannedClassTags.count(tag) == 0) {
staticsScannedClassTags.insert(tag);
queue<jclass> staticsToProcess; // will hold classes and interfaces whose statics need processing
staticsToProcess.push((jclass)obj);
scanStaticsOf(jvmti, jni, staticsToProcess);
}
}
// scalar object case
int slots = getSlotsForClass(jni, cls, classTag); // insures tables are filled in :-)
this->addObjectToHeap(tag, slots);
this->traceOutputter->doAllocation(0, tag, size, 0, type, 0, AllocInitialHeap, 0);
for (jclass currClass = cls; currClass != NULL; currClass = jni->GetSuperclass(currClass)) {
// process instance fields
jlong clsTag = getTag(jni, currClass);
int numFields;
jfieldID *fields;
err = jvmti->GetClassFields(currClass, &numFields, &fields);
check_jvmti_error(jvmti, err, "handleInitialObject: could not get class fields");
string className = getClassName(jni, currClass, clsTag);
int classId = className2classId[className];
FieldsInfo finfo = classId2fieldsInfo[classId];
for (int i = 0; i < numFields; ++i) {
jfieldID fid = fields[i];
jint mods;
err = jvmti->GetFieldModifiers(currClass, fid, &mods);
check_jvmti_error(jvmti, err, "handleInitialObject: could not get field modifiers");
if ((mods & ACC_STATIC) != 0) {
continue;
}
int agentFieldId = -1;
pair<jlong,jfieldID> classAndJFieldID = make_pair(clsTag, fid);
TagAndJFieldID2AgentId::iterator fit =
tagAndJFieldID2AgentId.find(classAndJFieldID);
if (fit != tagAndJFieldID2AgentId.end()) {
agentFieldId = fit->second;
} else {
char *fname;
char *fsig;
err = jvmti->GetFieldName(currClass, fid, &fname, &fsig, NULL);
check_jvmti_error(jvmti, err, "handleInitialObject: could not get field name and signature");
string fieldName((const char *)fname);
for (FieldsInfo::const_iterator fiit = finfo.begin(); fiit != finfo.end(); ++fiit) {
if (fiit->first == fieldName) {
agentFieldId = fiit->second;
break;
}
}
tagAndJFieldID2AgentId[classAndJFieldID] = agentFieldId;
err = jvmti->Deallocate((unsigned char *)fname);
check_jvmti_error(jvmti, err, "handleInitialObject: could not deallocate field name");
err = jvmti->Deallocate((unsigned char *)fsig);
check_jvmti_error(jvmti, err, "handleInitialObject: could not deallocate field signature");
}
if (agentFieldId < 0) {
continue;
}
FieldId2slotMap::const_iterator slotIt = fieldId2slot.find(agentFieldId);
if (slotIt == fieldId2slot.end()) {
continue;
}
int slot = fieldId2slot[agentFieldId];
jobject newTarget = jni->GetObjectField(obj, fid);
jlong newTargetTag = getTag(jni, newTarget);
jlong oldTargetTag = this->updateHeapAt(tag, slot, newTargetTag);
if (newTargetTag != oldTargetTag) {
this->traceOutputter->doSyntheticUpdate(0, oldTargetTag, tag, newTargetTag, agentFieldId);
}
}
}
}
void ETCallBackHandler::JNIPointerUpdated (JNIEnv *jni, jobject obj, jclass klass,
jfieldID fieldId, jobject newVal) {
// NOTE: if obj is NULL then klass should be non-null
// and this is an update of a static field of klass;
// if obj is non-null, klass is ignored.
jthread thread = getCurrentThread();
jclass cls;
jclass declClass;
if (obj == NULL) {
cls = klass;
declClass = klass;
} else {
cls = jni->GetObjectClass(obj);
jvmtiError err = theJVMTI->GetFieldDeclaringClass(cls, fieldId, &declClass);
check_jvmti_error(theJVMTI, err, "ETCallBackHandler:: Could not GetFieldDeclaringClass");
}
jlong classTag = getTagAndTagIfNeeded(jni, cls);
if (obj != NULL) {
getSlotsForClass(jni, cls, classTag); // insures tables are filled in :-)
}
pair<jlong,jfieldID> classAndJFieldID = make_pair(classTag, fieldId);
int fid = -1;
TagAndJFieldID2AgentId::const_iterator fit =
tagAndJFieldID2AgentId.find(classAndJFieldID);
if (fit != tagAndJFieldID2AgentId.end()) {
fid = fit->second;
} else {
string className = getAgentClassName(jni, declClass);
int classId = className2classId[className];
FieldsInfo finfo = classId2fieldsInfo[classId];
jobject fldObj = jni->ToReflectedField(declClass, fieldId, false);
string fieldName = getFieldName(jni, fldObj);
for (FieldsInfo::const_iterator it = finfo.begin(); it != finfo.end(); ++it) {
if (it->first == fieldName) {
fid = it->second;
tagAndJFieldID2AgentId[classAndJFieldID] = fid;
#ifdef TRACE_AGENT_RECORDING
cerr << "nativePointerUpdated from JNI SetObjectField; field id = 0x" << hex << fid << endl;
#endif
break;
}
}
}
assert(fid != -1);
nativePointerUpdated(jni, cls, thread, obj, newVal, fid);
}
/*A pointer update, based only on tags;
Used by the WeakRefCleaner, who isn't so sure about objects */
void ETCallBackHandler::syntheticPointerUpdated (JNIEnv* jni,
jlong originTag,
jlong newTargetTag,
jlong time) {
int fieldId = referentFieldNumber;
int referentSlot = fieldId2slot[fieldId];
jlong oldTargetTag;
if (time == 0) {
oldTargetTag = updateHeapAt(originTag, referentSlot, newTargetTag);
// consider this to be a present action;
// otherwise, it is in the past and we should not update time stamps
timeStampTag(originTag, getTagAndTagIfNeeded(jni, getCurrentThread()));
time = currentTime;
} else {
oldTargetTag = updateHeapAtAndStamp(originTag,
referentSlot,
newTargetTag,
getTagAndTagIfNeeded(jni, getCurrentThread()));
}
if (useFollowReferences) {
this->traceOutputter->doPointerUpdate(time, oldTargetTag, originTag, newTargetTag, fieldId, 0);
} else {
this->traceOutputter->doSyntheticUpdate(time, oldTargetTag, originTag, newTargetTag, fieldId);
}
}
void ETCallBackHandler::nativeUninitPutfield (JNIEnv *jni, jclass klass, jobject thread,
jobject newTarget, jint objSlot, jint fieldId) {
jobject obj;
jvmtiError err = aJVMTI->GetLocalObject((jthread)thread, (jint)2, objSlot, &obj);
check_jvmti_error(aJVMTI, err, "ETCallBackHandler::nativeUninitPutfield: Could not getLocalObject");
this->nativePointerUpdated (jni, klass, thread, obj, newTarget, fieldId);
}
//TODO: Break this into two methods
jlong ETCallBackHandler::updateHeapAtAndStamp (jlong originTag,
int slot,
jlong newTargetTag,
jlong threadTag) {
jlong result = heap.updateEdge(originTag, slot, newTargetTag);
timeStampTag(result, threadTag);
return result;
}
jlong ETCallBackHandler::updateHeapAt (jlong originTag,
int slot,
jlong newTargetTag) {
jlong result = heap.updateEdge(originTag, slot, newTargetTag);
return result;
}
void ETCallBackHandler::nativeStampThis (JNIEnv *jni, jclass, jstring) {
jthread thread;
jvmtiError err = aJVMTI->GetCurrentThread(&thread);
check_jvmti_error(aJVMTI, err, "nativeStampThis: could not get current thread");
// jlong threadTag = getTagAndTagIfNeeded(jni, thread);
jobject obj;
err = aJVMTI->GetLocalObject((jthread)thread, (jint)2, (jint)0, &obj);
check_jvmti_error(aJVMTI, err, "ETCallBackHandler::nativeStampThis: could not GetLocalObject");
jlong objTag = 0;
err = aJVMTI->GetTag(obj, &objTag);
check_jvmti_error(aJVMTI, err, "ETCallBackHandler::nativeStampThis: could not GetTag");
if (objTag == 0) {
objTag = getTagAndTagIfNeeded(jni, obj); // outputs a P trace entry, if necessary, and adds to heap
}
timeStampTag(objTag, getTagAndTagIfNeeded(jni, thread));
}
void ETCallBackHandler::nativeVarEvent (JNIEnv *jni,
jclass, jint numObjs,
jobject o0,
jobject o1,
jobject o2,
jobject o3,
jobject o4) {