-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathheapdump.cpp
2506 lines (2123 loc) · 84.2 KB
/
heapdump.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 IBM Corp. and others 2003
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
/* Includes */
#include <string.h>
#include "j9protos.h"
#include "j2sever.h"
#include "HeapIteratorAPI.h"
#include "j9dmpnls.h"
#include "FileStream.hpp"
#include "ut_j9dmp.h"
/* Function prototypes */
extern "C" {
void writeClassicHeapdump(const char *label, J9RASdumpContext *context, J9RASdumpAgent *agent);
}
static jvmtiIterationControl binaryHeapDumpHeapIteratorCallback (J9JavaVM* vm, J9MM_IterateHeapDescriptor* heapDescriptor, void* userData);
static jvmtiIterationControl binaryHeapDumpSpaceIteratorCallback (J9JavaVM* vm, J9MM_IterateSpaceDescriptor* spaceDescriptor, void* userData);
static jvmtiIterationControl binaryHeapDumpRegionIteratorCallback (J9JavaVM* vm, J9MM_IterateRegionDescriptor* regionDescription, void* userData);
static jvmtiIterationControl binaryHeapDumpObjectIteratorCallback (J9JavaVM* vm, J9MM_IterateObjectDescriptor* objectDescriptor, void* userData);
static jvmtiIterationControl binaryHeapDumpObjectReferenceIteratorTraitsCallback(J9JavaVM* virtualMachine, J9MM_IterateObjectDescriptor* objectDescriptor, J9MM_IterateObjectRefDescriptor* referenceDescriptor, void* userData);
static jvmtiIterationControl binaryHeapDumpObjectReferenceIteratorWriterCallback(J9JavaVM* virtualMachine, J9MM_IterateObjectDescriptor* objectDescriptor, J9MM_IterateObjectRefDescriptor* referenceDescriptor, void* userData);
#define allClassesStartDo(vm, state, loader) \
vm->internalVMFunctions->allClassesStartDo(state, vm, loader)
#define allClassesNextDo(vm, state) \
vm->internalVMFunctions->allClassesNextDo(state)
#define allClassesEndDo(vm, state) \
vm->internalVMFunctions->allClassesEndDo(state)
/* Function prototypes for performance measurement
void startTimer();
void stopTimer();
*/
/**************************************************************************************************/
/* */
/* Classes for manipulating strings */
/* */
/* This really ought to be a template but there's a certain lack of confidence in the compilers */
/* despite templates being used in the product already. In the mean time it doesn't actually */
/* have to be a template */
/* */
/**************************************************************************************************/
/* template<class DataType, class LengthType>class Strings */
class Strings
{
public :
/* Declare the 'template' parameters */
typedef char DataType; typedef UDATA LengthType;
/* Constructors... */
/* Pseudo default constructor */
inline Strings(J9PortLibrary* portLibrary) :
_PortLibrary(portLibrary),
_Buffer(0)
{
}
/* Destructor */
inline ~Strings()
{
deleteBuffer(_Buffer);
_Buffer = 0;
}
/* Assignment operators... */
/* Assignment methods... */
/* Append operators... */
/* Operator for appending another string */
inline Strings& operator+=(const Strings& source)
{
appendInternal(source.data(), source.length());
return *this;
}
/* Operator for appending from a pointer to a zero terminated string of items */
inline Strings& operator+=(const DataType* data)
{
appendInternal(data, length(data));
return *this;
}
/* Operator for appending a single item */
inline Strings& operator+=(const DataType& source)
{
appendInternal(&source, 1);
return *this;
}
/* Append methods... */
/* Method for appending from a substring of another string */
inline Strings& append(const Strings& data, LengthType position, LengthType length)
{
/* Check the parameters */
LengthType sourceLength = data.length();
if (position >= sourceLength) {
return *this;
}
/* Limit length to not exceed the end of the source data */
length = limitLength(sourceLength, position, length);
appendInternal(data.data() + position, length);
return *this;
}
/* Method for appending the remainder of another string */
inline Strings& append(const Strings& data, LengthType position)
{
/* Check the parameters */
LengthType sourceLength = data.length();
if (position >= sourceLength) {
return *this;
}
/* Limit length to not exceed the end of the source data */
LengthType length = sourceLength - position;
appendInternal(data.data() + position, length);
return *this;
}
/* Method for appending an array of items */
inline Strings& append(const DataType* source, LengthType length)
{
appendInternal(source, length);
return *this;
}
/* Method for clearing/emptying the string completely */
inline void clear(void)
{
deleteBuffer(_Buffer);
_Buffer = 0;
}
/* Method for getting the length of the data */
inline LengthType length(void) const
{
if (_Buffer != 0) {
return _Buffer->iLength;
}
return 0;
}
/* Method for getting a pointer to the data */
inline const DataType* data(void) const
{
if (_Buffer != 0) {
return _Buffer->data();
}
/* Use the pointer itself to act as a null */
/* NB : This makes assumptions about the data type */
return (DataType*)&_Buffer;
}
/* Method for locating the first occurrence of a zero terminated substring in a string */
inline LengthType find(const DataType* targetString, LengthType position = 0) const
{
/* Determine the length of the target string */
LengthType targetStringLength = length(targetString);
/* Limit the position parameter to the bounds of the string */
LengthType stringLength = length();
if (position > (stringLength - 1)) {
position = stringLength - 1;
}
/* Compare the given string with each of the string's sub strings */
const DataType* stringStart = data();
const DataType* stringEnd = stringStart + stringLength;
const DataType* currentString = stringStart + position;
while (currentString < stringEnd) {
/* Compare the first characters of the two strings */
if (*currentString == *targetString) {
/* If there is target string is longer than the current substring, they can't match */
if (unsigned(stringEnd - currentString) < targetStringLength) {
return (UDATA) -1;
}
/* Compare the subsequent characters of the strings */
bool match = true;
for (LengthType i = 0; i < targetStringLength; i++) {
if (currentString[i] != targetString[i]) {
match = false;
break;
}
}
if (match) {
return currentString - stringStart;
}
}
currentString++;
}
return (UDATA) -1;
}
/* Static method for determining the length of a zero terminated array of items */
inline static LengthType length(const DataType* source)
{
/* Make a null pointer equivalent to a zero length string */
if (source == 0) {
return 0;
}
/* Count the items in the string */
LengthType count = 0;
for (const DataType* current = source; *current != 0; current++) {
count++;
}
return count;
}
protected :
/* Declare the private, nested string buffer class */
class Buffer
{
public :
/* Declared data */
LengthType iCapacity;
LengthType iLength;
DataType* iDebugData; /* Only required for debugging */
/* Internal method for accessing the data associated with a header */
inline DataType* data(void)
{
return(DataType*)((char*)this + sizeof(Buffer));
}
/* Internal method for accessing the data associated with a header */
inline const DataType* data(void) const
{
return(DataType*)((char*)this + sizeof(Buffer));
}
private :
/* Prevent use of the copy constructor and assignment operator */
Buffer(const Buffer& source);
Buffer& operator=(const Buffer& source);
};
/* Internal Method for getting a non const pointer to the data */
inline DataType* dataPointer(void)
{
if (_Buffer != 0) {
return _Buffer->data();
}
/* Use the pointer itself to act as a null */
/* NB : This makes assumptions about the data type */
return (DataType*)&_Buffer;
}
/* Internal method for appending a new value to a String object */
inline Strings& appendInternal(
const DataType* appendData,
LengthType numberToAppend
)
{
/* If the append length is zero, there's nothing to do */
/* NB : Interpret a null pointer as an empty string */
if ((appendData == 0) || (numberToAppend == 0)) {
return *this;
}
/* Establish the lengths */
LengthType originalLength = length();
LengthType newLength = originalLength + numberToAppend;
/* Establish the source and target buffers */
DataType* sourceBufferPointer = 0;
DataType* targetBufferPointer = 0;
Buffer* bufferPointerForUnlinking = 0;
if (_Buffer != 0) {
/* Get a pointer to the source buffer */
sourceBufferPointer = _Buffer->data();
/* If the capacity is insufficient, a new buffer is needed */
if (_Buffer->iCapacity < newLength) {
/* A new buffer needed is - prepare the old buffer unlinking */
bufferPointerForUnlinking = _Buffer;
/* Create a new remote buffer */
_Buffer = createBuffer(newLength);
}
/* Otherwise use the existing buffer */
} else {
/* Create a new remote buffer */
_Buffer = createBuffer(newLength);
}
targetBufferPointer = _Buffer->data();
_Buffer->iLength = newLength;
/* Copy the existing data */
if (sourceBufferPointer && targetBufferPointer != sourceBufferPointer) {
copyBuffer(targetBufferPointer, sourceBufferPointer, originalLength, true);
}
/* Copy in the append data */
copyBuffer(targetBufferPointer + originalLength, appendData, numberToAppend, true);
/* If a new remote buffer was created, release the original */
if (bufferPointerForUnlinking) {
deleteBuffer(bufferPointerForUnlinking);
}
return *this;
}
/* Internal method for creating a new buffer */
inline Buffer* createBuffer(LengthType requestedLength)
{
/* NB : This is never called with requestedLength == 0 */
/* Granularize the buffer length. This helps performance when strings are */
/* altered in situ (e.g. append) and also means that heap allocations will */
/* tend to be for the same sized block (i.e. the minimum) */
LengthType newBufferLength = sizeof(Buffer) + sizeof(DataType) * (requestedLength + 1);
LengthType granularNewBufferLength = ((newBufferLength / 32) + 1) * 32;
PORT_ACCESS_FROM_PORT(_PortLibrary);
/* Create a new buffer */
Buffer* newBufferPointer = (Buffer*)j9mem_allocate_memory(granularNewBufferLength, OMRMEM_CATEGORY_VM);
/* Initialize it */
newBufferPointer->iCapacity = granularNewBufferLength - sizeof(Buffer) - sizeof(DataType);
newBufferPointer->iLength = 0;
newBufferPointer->iDebugData = (DataType*)((char*)newBufferPointer + sizeof(Buffer));
return newBufferPointer;
}
/* Internal method for copying buffer contents */
inline void copyBuffer(DataType* target, const DataType* source, LengthType length, bool terminated)
{
/* Copy element by element */
DataType* targetEnd = target + length;
while (target < targetEnd) {
*target++ = *source++;
}
/* If required, terminate the string */
if (terminated) {
*target = DataType(0);
}
}
/* Internal method for deleting a buffer */
inline void deleteBuffer(Buffer* buffer)
{
PORT_ACCESS_FROM_PORT(_PortLibrary);
if (buffer != 0) {
j9mem_free_memory(buffer);
}
}
/* Method to limit the requested number of elements to the end of the available string */
inline static LengthType limitLength(LengthType sourceLength, LengthType startPosition, LengthType requestedLength)
{
if (startPosition < sourceLength) {
/* Reduce the source length by the offset */
sourceLength -= startPosition;
/* Return the minimum of the source length and the requested length */
return sourceLength < requestedLength ? sourceLength : requestedLength;
} else {
/* Start position beyond length */
return 0;
}
}
/* Declared data */
J9PortLibrary* _PortLibrary;
Buffer* _Buffer;
private :
/* Prevent use of the unimplemented copy constructor and assignment operator */
inline Strings& operator=(const Strings& source);
inline Strings(const Strings& source);
};
/* String class methods that aren't needed at the moment */
/* Copy constructor
inline Strings(const Strings& source) :
_PortLibrary(source._PortLibrary),
_Buffer(0)
{
assign(source);
}
*/
/* Constructor from a sub-string of a String
inline Strings(const Strings& source, LengthType position, LengthType length) :
iBuffer(0)
{
assign(source, position, length);
}
*/
/* Constructor from the remainder of a String
inline Strings(const Strings& source, LengthType position) :
iBuffer(0)
{
assign(source, position);
}
*/
/* Constructor from a null terminated string
inline Strings(const DataType* data) :
iBuffer(0)
{
assignInternal(data, length(data));
}
*/
/* Operator for assigning from another String object
inline Strings& operator=(const Strings& source)
{
assign(source);
return *this;
}
*/
/* Method for assigning from another string
inline Strings& assign(const Strings& source) {
If it's a self assignment, do nothing
if (&source == this) {
return *this;
}
Dispose of the existing buffer
if (iBuffer != 0) {
unlinkFromBuffer(iBuffer);
iBuffer = 0;
}
Create a new buffer which is a copy of the original
assignInternal(source.data(), source.length());
return *this;
}
*/
/* Method for assigning from a substring of another string
inline Strings& assign(const Strings& data, LengthType position, LengthType length) {
Check the parameters
LengthType sourceLength = data.length();
if (position >= sourceLength) {
return *this;
}
Limit length to not exceed the end of the source data
length = limitLength(sourceLength, position, length);
assignInternal(data.data() + position, length);
return *this;
}
*/
/* Method for assigning from the remainder of a string
inline Strings& assign(const Strings& data, LengthType position) {
Check the parameters
LengthType sourceLength = data.length();
if (position >= sourceLength) {
return *this;
}
Limit length to not exceed the end of the source data
LengthType length = sourceLength - position;
assignInternal(data.data() + position, length);
return *this;
}
*/
/* Operator for accessing the items
inline DataType& operator[](LengthType index)
{
Detect the case where the index is out of range
LengthType len = length();
if ((index < 0) || (index > len)) {
return *(dataPointer() + len);
}
return *(dataPointer() + index);
}
*/
/* Internal method for assigning a new value to a String object
inline void assignInternal(const DataType* newData, LengthType newLength)
{
If the result of this operation is a null string, call clear() and return
if (newData == 0 || newLength == 0) {
clear();
return;
}
Create a new buffer that's exactly the right size
Buffer* newBufferPointer = createBuffer(newLength);
newBufferPointer->iLength = newLength;
Copy in the new data
copyBuffer(newBufferPointer->data(), newData, newLength, true);
Finalize the buffers
deleteBuffer(iBuffer);
iBuffer = newBufferPointer;
}
*/
/*typedef Strings<char, UDATA>CharacterStringBase;*/
typedef Strings CharacterStringBase;
class CharacterString : public CharacterStringBase
{
public :
/* Constructor */
CharacterString(J9PortLibrary* portLibrary) :
CharacterStringBase(portLibrary)
{
}
/* Append methods... */
/* Method for appending from a character rendition of an unsigned integer. */
inline CharacterString& appendAsCharacters(UDATA data, UDATA radix)
{
PORT_ACCESS_FROM_PORT(_PortLibrary);
char buffer[1 + (sizeof(UDATA) * 8)];
switch (radix) {
case 16:
j9str_printf(buffer, sizeof(buffer), "%.*zX", sizeof(UDATA) * 2, data);
break;
default:
j9str_printf(buffer, sizeof(buffer), "%zu", data);
break;
}
append(buffer, strlen(buffer));
return *this;
}
};
/**************************************************************************************************/
/* */
/* Class for walking a class constant pool to extract class references. See CMVC 177688. */
/* Note: adapted from GC_ConstantPoolClassSlotIterator in Modron/gc_structs */
/* */
/**************************************************************************************************/
class ConstantPoolClassIterator
{
J9Object **_cpEntry;
U_32 _cpEntryCount;
U_32 _cpEntryTotal;
U_32 *_cpDescriptionSlots;
U_32 _cpDescription;
UDATA _cpDescriptionIndex;
public:
/* Constructor */
ConstantPoolClassIterator(J9Class *clazz) :
_cpEntry((J9Object **)J9_CP_FROM_CLASS(clazz)),
_cpEntryCount((U_32)((J9ROMClass *)clazz->romClass)->ramConstantPoolCount),
_cpDescriptionSlots(NULL),
_cpDescription(0)
{
_cpEntryTotal = _cpEntryCount;
if (_cpEntryCount) {
_cpDescriptionSlots = SRP_PTR_GET(&((J9ROMClass *)clazz->romClass)->cpShapeDescription, U_32 *);
_cpDescriptionIndex = 0;
}
}
/* Method to return the next Class reference from the constant pool, or NULL if no more references. */
j9object_t nextClassObject(void) {
U_32 slotType;
J9Object **slotPtr;
while(_cpEntryCount) {
if (0 == _cpDescriptionIndex) {
_cpDescription = (U_32)*_cpDescriptionSlots;
_cpDescriptionSlots += 1;
_cpDescriptionIndex = J9_CP_DESCRIPTIONS_PER_U32;
}
slotType = _cpDescription & J9_CP_DESCRIPTION_MASK;
slotPtr = _cpEntry;
/* Adjust the CP slot and description information */
_cpEntry = (J9Object **)( ((U_8 *)_cpEntry) + sizeof(J9RAMConstantPoolItem) );
_cpEntryCount -= 1;
_cpDescription >>= J9_CP_BITS_PER_DESCRIPTION;
_cpDescriptionIndex -= 1;
/* We are only interested if the slot is a class reference */
if (slotType == J9CPTYPE_CLASS) {
/* PHD needs the address of the on-heap java/lang/Class object (2.4 VM and later) */
J9Class *clazz = (((J9RAMClassRef *) slotPtr)->value);
if (NULL != clazz) {
return clazz->classObject;
}
}
}
return NULL;
}
};
/**************************************************************************************************/
/* */
/* Class for walking the superclass references from a class. See CMVC 177688. */
/* */
/**************************************************************************************************/
class SuperclassesIterator
{
UDATA _classDepth;
IDATA _index;
J9Class **_superclassPtr;
public:
/* Constructor */
SuperclassesIterator(J9Class *clazz) :
_classDepth(J9CLASS_DEPTH(clazz)),
_index(-1),
_superclassPtr((J9Class **)clazz->superclasses)
{
}
/* Method to return a reference to the next superclass, or NULL if no more superclasses */
j9object_t nextClassObject(void) {
J9Class **slotPtr;
if(0 == _classDepth) {
return NULL;
}
_index += 1;
_classDepth -= 1;
slotPtr = _superclassPtr++;
/* PHD needs the address of the on-heap java/lang/Class object (2.4 VM and later) */
J9Class *clazz = (((J9RAMClassRef *) slotPtr)->value);
if (NULL != clazz) {
return clazz->classObject;
} else {
return NULL;
}
}
};
/**************************************************************************************************/
/* */
/* Class for walking the interface references from a class. See CMVC 177688. */
/* Note: adapted from GC_ClassLocalInterfaceIterator in Modron/gc_structs */
/* */
/**************************************************************************************************/
class InterfacesIterator
{
J9ITable *_iTable;
J9ITable *_superclassITable;
public:
/* Constructor */
InterfacesIterator(J9Class *clazz) :
_iTable((J9ITable *)clazz->iTable)
{
J9Class *superclass = (J9Class *) ((struct J9Class**)clazz->superclasses[J9CLASS_DEPTH(clazz) - 1]);
if(superclass) {
_superclassITable = (J9ITable *)superclass->iTable;
} else {
_superclassITable = NULL;
}
}
/* Method to return a reference to the next interface, or NULL if no more interfaces */
j9object_t nextClassObject(void) {
J9Class **slotPtr;
if(_iTable == _superclassITable) {
return NULL;
}
slotPtr = &_iTable->interfaceClass;
_iTable = (J9ITable *)_iTable->next;
/* PHD needs the address of the on-heap java/lang/Class object (2.4 VM and later) */
J9Class *clazz = (((J9RAMClassRef *) slotPtr)->value);
if (NULL != clazz) {
return clazz->classObject;
} else {
return NULL;
}
}
};
/**************************************************************************************************/
/* */
/* Class for writing binary portable heap dump files */
/* */
/**************************************************************************************************/
class BinaryHeapDumpWriter
{
public :
/* Constructor */
BinaryHeapDumpWriter(const char* fileName, J9RASdumpContext* context, J9RASdumpAgent* agent);
/* Destructor */
~BinaryHeapDumpWriter();
UDATA _Id;
char* _RegionStart;
char* _RegionEnd;
private :
/* Prevent use of the copy constructor and assignment operator */
BinaryHeapDumpWriter(const BinaryHeapDumpWriter& source);
BinaryHeapDumpWriter& operator=(const BinaryHeapDumpWriter& source);
/* Allow the callback functions access */
friend jvmtiIterationControl binaryHeapDumpSpaceIteratorCallback (J9JavaVM* virtualMachine, J9MM_IterateSpaceDescriptor* spaceDescriptor, void* userData);
friend jvmtiIterationControl binaryHeapDumpObjectIteratorCallback (J9JavaVM* virtualMachine, J9MM_IterateObjectDescriptor* objectDescriptor, void* userData);
friend jvmtiIterationControl binaryHeapDumpObjectReferenceIteratorTraitsCallback(J9JavaVM* virtualMachine, J9MM_IterateObjectDescriptor* objectDescriptor, J9MM_IterateObjectRefDescriptor* referenceDescriptor, void* userData);
friend jvmtiIterationControl binaryHeapDumpObjectReferenceIteratorWriterCallback(J9JavaVM* virtualMachine, J9MM_IterateObjectDescriptor* objectDescriptor, J9MM_IterateObjectRefDescriptor* referenceDescriptor, void* userData);
friend jvmtiIterationControl binaryHeapDumpHeapIteratorCallback(J9JavaVM* virtualMachine, J9MM_IterateHeapDescriptor* heapDescriptor, void* userData);
friend jvmtiIterationControl binaryHeapDumpRegionIteratorCallback(J9JavaVM* virtualMachine, J9MM_IterateRegionDescriptor* regionDescription, void* userData);
/* Nested class for determining the characteristics of the references */
class ReferenceTraits
{
public :
/* Constructor */
ReferenceTraits(BinaryHeapDumpWriter* heapDumpWriter, j9object_t objectAddress);
/* Method for adding a reference to the 'list' of those analysed */
void addReference(j9object_t objectReference);
/* Methods for getting the object's attributes */
IDATA maximumOffset (void) const;
UDATA count (void) const;
IDATA offset (UDATA index) const;
BinaryHeapDumpWriter* _HeapDumpWriter;
private :
/* Prevent use of the copy constructor and assignment operator */
ReferenceTraits(const ReferenceTraits& source);
ReferenceTraits& operator=(const ReferenceTraits& source);
/* Declared data */
j9object_t _ObjectAddress;
IDATA _MaximumOffset;
IDATA _MinimumOffset;
UDATA _Count;
IDATA _Offsets[8];
};
/* Nested class for writing a reference to the file */
class ReferenceWriter
{
public :
/* Constructor */
ReferenceWriter(BinaryHeapDumpWriter* heapDumpWriter, j9object_t objectAddress, UDATA count, int size);
/* Method for writing a reference to the dump file */
void writeNumber(j9object_t objectAddress);
BinaryHeapDumpWriter* _HeapDumpWriter;
private :
/* Prevent use of the copy constructor and assignment operator */
ReferenceWriter(const ReferenceWriter& source);
ReferenceWriter& operator=(const ReferenceWriter& source);
/* Declared data */
j9object_t _ObjectAddress;
UDATA _Count;
int _Size;
};
/* Nested class for managing the class cache */
class ClassCache
{
public :
/* Constructor */
ClassCache();
/* Method for adding a class */
void add(const void* clazz);
/* Method for finding a class */
int find(const void* clazz);
/* Methods for getting the object's attributes */
int index(void) const;
/* Method for setting the object back to its initial state (i.e. empty) */
void clear(void);
private :
/* Prevent use of the copy constructor and assignment operator */
ClassCache(const ClassCache& source);
ClassCache& operator=(const ClassCache& source);
/* Declared data */
const void* _Cache[4];
int _Index;
};
friend class ReferenceTraits;
friend class ReferenceWriter;
/* Internal methods */
void openNewDumpFile(J9MM_IterateSpaceDescriptor* spaceDesriptor);
void writeDumpFileHeader(void);
void writeDumpFileTrailer(void);
void writeFullVersionRecord(void);
void writeObjectRecord(J9MM_IterateObjectDescriptor* objectDescriptor);
void writeNormalObjectRecord(J9MM_IterateObjectDescriptor* objectDescriptor);
void writeArrayObjectRecord(J9MM_IterateObjectDescriptor* objectDescriptor);
void writeClassRecord(J9Class* clazz);
static int numberSize(IDATA number);
int getObjectHashCode(j9object_t object);
static int numberSizeEncoding(int numberSize);
static int wordSize(void);
void checkForIOError(void);
/* Methods for writing data to output file (proxies to _OutputStream */
void writeCharacters (const char* data, IDATA length);
void writeCharacters (const char* data);
void writeNumber (IDATA data, int length);
/* Declared data */
/* NB : The initialization order is not guaranteed on all C++ compilers */
J9RASdumpContext* _Context;
J9RASdumpAgent* _Agent;
J9JavaVM* _VirtualMachine;
J9PortLibrary* _PortLibrary;
CharacterString _FileName;
FileStream _OutputStream;
void* _CurrentObject;
ClassCache _ClassCache;
bool _FileMode;
bool _Error;
/* Static methods returning constant values */
inline static const char* identifierField(void) {return "portable heap dump";}
inline static char versionField(void) {return 0x06;}
#if defined(J9VM_OPT_NEW_OBJECT_HASH)
inline static char primaryFlagsField(void)
{
#ifdef J9VM_ENV_DATA64
return 0x05;
#else
return 0x04;
#endif
}
#else /* defined(J9VM_OPT_NEW_OBJECT_HASH) */
inline static char primaryFlagsField(void)
{
#ifdef J9VM_ENV_DATA64
return 0x07;
#else
return 0x06;
#endif
}
#endif /* defined(J9VM_OPT_NEW_OBJECT_HASH) */
inline static char headerStartField(void) {return 0x01;}
inline static char fullVersionRecordField(void) {return 0x04;}
inline static char headerEndField(void) {return 0x02;}
inline static char dumpStartField(void) {return 0x02;}
inline static char longObjectRecordField(void) {return 0x04;}
inline static char longPrimitiveArrayRecordField(void) {return 0x07;}
inline static char arrayObjectRecordField(void) {return 0x08;}
inline static char classObjectRecordField(void) {return 0x06;}
inline static char dumpEndField(void) {return 0x03;}
};
/**************************************************************************************************/
/* */
/* BinaryHeapDumpWriter::ReferenceTraits::ReferenceTraits() method implementation */
/* */
/**************************************************************************************************/
BinaryHeapDumpWriter::ReferenceTraits::ReferenceTraits(
BinaryHeapDumpWriter* heapDumpWriter,
j9object_t objectAddress
) :
_HeapDumpWriter(heapDumpWriter),
_ObjectAddress(objectAddress),
_MaximumOffset(0),
_MinimumOffset(0),
_Count(0)
{
}
/**************************************************************************************************/
/* */
/* BinaryHeapDumpWriter::ReferenceTraits::addReference() method implementation */
/* */
/**************************************************************************************************/
void
BinaryHeapDumpWriter::ReferenceTraits::addReference(j9object_t objectReference)
{
/* Ignore null references */
if (objectReference == 0) {
return;
}
/* Calculate the offset (gap) from the current object */
IDATA offset = ((char*)(objectReference) - (char*)_ObjectAddress);
/* Remember the biggest offsets */
if (offset > _MaximumOffset) {
_MaximumOffset = offset;
}
if (offset < _MinimumOffset) {
_MinimumOffset = offset;
}
/* Remember the first few offsets */
if (_Count < 8) {
_Offsets[_Count] = offset;
}
/* Increment the count of valid references */
_Count++;
}
/**************************************************************************************************/
/* */
/* BinaryHeapDumpWriter::ReferenceTraits::maximumOffset() method implementation */
/* */
/**************************************************************************************************/
IDATA
BinaryHeapDumpWriter::ReferenceTraits::maximumOffset(void) const
{
/* Calculate the biggest difference as a positive number */
/* NB : A byte has a range of -128 to +127 but this will say -128 needs 2 bytes */
IDATA result = -_MinimumOffset > _MaximumOffset ? -_MinimumOffset : _MaximumOffset;
return result;
}
/**************************************************************************************************/
/* */
/* BinaryHeapDumpWriter::ReferenceTraits::count() method implementation */
/* */
/**************************************************************************************************/
UDATA
BinaryHeapDumpWriter::ReferenceTraits::count(void) const
{
return _Count;
}
/**************************************************************************************************/
/* */
/* BinaryHeapDumpWriter::ReferenceTraits::offset() method implementation */
/* */
/**************************************************************************************************/
IDATA
BinaryHeapDumpWriter::ReferenceTraits::offset(UDATA index) const
{
if (index > 8) {
return 0;
}