-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathheapDumper.cpp
2143 lines (1867 loc) · 70.1 KB
/
heapDumper.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) 2005, 2019, 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 "jvm.h"
#include "classfile/classLoaderData.inline.hpp"
#include "classfile/classLoaderDataGraph.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "gc/shared/gcLocker.hpp"
#include "gc/shared/gcVMOperations.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/objArrayKlass.hpp"
#include "oops/objArrayOop.inline.hpp"
#include "oops/oop.inline.hpp"
#include "oops/typeArrayOop.inline.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/jniHandles.hpp"
#include "runtime/os.inline.hpp"
#include "runtime/reflectionUtils.hpp"
#include "runtime/thread.inline.hpp"
#include "runtime/threadSMR.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vmThread.hpp"
#include "runtime/vmOperations.hpp"
#include "services/heapDumper.hpp"
#include "services/threadService.hpp"
#include "utilities/macros.hpp"
#include "utilities/ostream.hpp"
/*
* HPROF binary format - description copied from:
* src/share/demo/jvmti/hprof/hprof_io.c
*
*
* header "JAVA PROFILE 1.0.2" (0-terminated)
*
* u4 size of identifiers. Identifiers are used to represent
* UTF8 strings, objects, stack traces, etc. They usually
* have the same size as host pointers. For example, on
* Solaris and Win32, the size is 4.
* u4 high word
* u4 low word number of milliseconds since 0:00 GMT, 1/1/70
* [record]* a sequence of records.
*
*
* Record format:
*
* u1 a TAG denoting the type of the record
* u4 number of *microseconds* since the time stamp in the
* header. (wraps around in a little more than an hour)
* u4 number of bytes *remaining* in the record. Note that
* this number excludes the tag and the length field itself.
* [u1]* BODY of the record (a sequence of bytes)
*
*
* The following TAGs are supported:
*
* TAG BODY notes
*----------------------------------------------------------
* HPROF_UTF8 a UTF8-encoded name
*
* id name ID
* [u1]* UTF8 characters (no trailing zero)
*
* HPROF_LOAD_CLASS a newly loaded class
*
* u4 class serial number (> 0)
* id class object ID
* u4 stack trace serial number
* id class name ID
*
* HPROF_UNLOAD_CLASS an unloading class
*
* u4 class serial_number
*
* HPROF_FRAME a Java stack frame
*
* id stack frame ID
* id method name ID
* id method signature ID
* id source file name ID
* u4 class serial number
* i4 line number. >0: normal
* -1: unknown
* -2: compiled method
* -3: native method
*
* HPROF_TRACE a Java stack trace
*
* u4 stack trace serial number
* u4 thread serial number
* u4 number of frames
* [id]* stack frame IDs
*
*
* HPROF_ALLOC_SITES a set of heap allocation sites, obtained after GC
*
* u2 flags 0x0001: incremental vs. complete
* 0x0002: sorted by allocation vs. live
* 0x0004: whether to force a GC
* u4 cutoff ratio
* u4 total live bytes
* u4 total live instances
* u8 total bytes allocated
* u8 total instances allocated
* u4 number of sites that follow
* [u1 is_array: 0: normal object
* 2: object array
* 4: boolean array
* 5: char array
* 6: float array
* 7: double array
* 8: byte array
* 9: short array
* 10: int array
* 11: long array
* u4 class serial number (may be zero during startup)
* u4 stack trace serial number
* u4 number of bytes alive
* u4 number of instances alive
* u4 number of bytes allocated
* u4]* number of instance allocated
*
* HPROF_START_THREAD a newly started thread.
*
* u4 thread serial number (> 0)
* id thread object ID
* u4 stack trace serial number
* id thread name ID
* id thread group name ID
* id thread group parent name ID
*
* HPROF_END_THREAD a terminating thread.
*
* u4 thread serial number
*
* HPROF_HEAP_SUMMARY heap summary
*
* u4 total live bytes
* u4 total live instances
* u8 total bytes allocated
* u8 total instances allocated
*
* HPROF_HEAP_DUMP denote a heap dump
*
* [heap dump sub-records]*
*
* There are four kinds of heap dump sub-records:
*
* u1 sub-record type
*
* HPROF_GC_ROOT_UNKNOWN unknown root
*
* id object ID
*
* HPROF_GC_ROOT_THREAD_OBJ thread object
*
* id thread object ID (may be 0 for a
* thread newly attached through JNI)
* u4 thread sequence number
* u4 stack trace sequence number
*
* HPROF_GC_ROOT_JNI_GLOBAL JNI global ref root
*
* id object ID
* id JNI global ref ID
*
* HPROF_GC_ROOT_JNI_LOCAL JNI local ref
*
* id object ID
* u4 thread serial number
* u4 frame # in stack trace (-1 for empty)
*
* HPROF_GC_ROOT_JAVA_FRAME Java stack frame
*
* id object ID
* u4 thread serial number
* u4 frame # in stack trace (-1 for empty)
*
* HPROF_GC_ROOT_NATIVE_STACK Native stack
*
* id object ID
* u4 thread serial number
*
* HPROF_GC_ROOT_STICKY_CLASS System class
*
* id object ID
*
* HPROF_GC_ROOT_THREAD_BLOCK Reference from thread block
*
* id object ID
* u4 thread serial number
*
* HPROF_GC_ROOT_MONITOR_USED Busy monitor
*
* id object ID
*
* HPROF_GC_CLASS_DUMP dump of a class object
*
* id class object ID
* u4 stack trace serial number
* id super class object ID
* id class loader object ID
* id signers object ID
* id protection domain object ID
* id reserved
* id reserved
*
* u4 instance size (in bytes)
*
* u2 size of constant pool
* [u2, constant pool index,
* ty, type
* 2: object
* 4: boolean
* 5: char
* 6: float
* 7: double
* 8: byte
* 9: short
* 10: int
* 11: long
* vl]* and value
*
* u2 number of static fields
* [id, static field name,
* ty, type,
* vl]* and value
*
* u2 number of inst. fields (not inc. super)
* [id, instance field name,
* ty]* type
*
* HPROF_GC_INSTANCE_DUMP dump of a normal object
*
* id object ID
* u4 stack trace serial number
* id class object ID
* u4 number of bytes that follow
* [vl]* instance field values (class, followed
* by super, super's super ...)
*
* HPROF_GC_OBJ_ARRAY_DUMP dump of an object array
*
* id array object ID
* u4 stack trace serial number
* u4 number of elements
* id array class ID
* [id]* elements
*
* HPROF_GC_PRIM_ARRAY_DUMP dump of a primitive array
*
* id array object ID
* u4 stack trace serial number
* u4 number of elements
* u1 element type
* 4: boolean array
* 5: char array
* 6: float array
* 7: double array
* 8: byte array
* 9: short array
* 10: int array
* 11: long array
* [u1]* elements
*
* HPROF_CPU_SAMPLES a set of sample traces of running threads
*
* u4 total number of samples
* u4 # of traces
* [u4 # of samples
* u4]* stack trace serial number
*
* HPROF_CONTROL_SETTINGS the settings of on/off switches
*
* u4 0x00000001: alloc traces on/off
* 0x00000002: cpu sampling on/off
* u2 stack trace depth
*
*
* When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally
* be generated as a sequence of heap dump segments. This sequence is
* terminated by an end record. The additional tags allowed by format
* "JAVA PROFILE 1.0.2" are:
*
* HPROF_HEAP_DUMP_SEGMENT denote a heap dump segment
*
* [heap dump sub-records]*
* The same sub-record types allowed by HPROF_HEAP_DUMP
*
* HPROF_HEAP_DUMP_END denotes the end of a heap dump
*
*/
// HPROF tags
typedef enum {
// top-level records
HPROF_UTF8 = 0x01,
HPROF_LOAD_CLASS = 0x02,
HPROF_UNLOAD_CLASS = 0x03,
HPROF_FRAME = 0x04,
HPROF_TRACE = 0x05,
HPROF_ALLOC_SITES = 0x06,
HPROF_HEAP_SUMMARY = 0x07,
HPROF_START_THREAD = 0x0A,
HPROF_END_THREAD = 0x0B,
HPROF_HEAP_DUMP = 0x0C,
HPROF_CPU_SAMPLES = 0x0D,
HPROF_CONTROL_SETTINGS = 0x0E,
// 1.0.2 record types
HPROF_HEAP_DUMP_SEGMENT = 0x1C,
HPROF_HEAP_DUMP_END = 0x2C,
// field types
HPROF_ARRAY_OBJECT = 0x01,
HPROF_NORMAL_OBJECT = 0x02,
HPROF_BOOLEAN = 0x04,
HPROF_CHAR = 0x05,
HPROF_FLOAT = 0x06,
HPROF_DOUBLE = 0x07,
HPROF_BYTE = 0x08,
HPROF_SHORT = 0x09,
HPROF_INT = 0x0A,
HPROF_LONG = 0x0B,
// data-dump sub-records
HPROF_GC_ROOT_UNKNOWN = 0xFF,
HPROF_GC_ROOT_JNI_GLOBAL = 0x01,
HPROF_GC_ROOT_JNI_LOCAL = 0x02,
HPROF_GC_ROOT_JAVA_FRAME = 0x03,
HPROF_GC_ROOT_NATIVE_STACK = 0x04,
HPROF_GC_ROOT_STICKY_CLASS = 0x05,
HPROF_GC_ROOT_THREAD_BLOCK = 0x06,
HPROF_GC_ROOT_MONITOR_USED = 0x07,
HPROF_GC_ROOT_THREAD_OBJ = 0x08,
HPROF_GC_CLASS_DUMP = 0x20,
HPROF_GC_INSTANCE_DUMP = 0x21,
HPROF_GC_OBJ_ARRAY_DUMP = 0x22,
HPROF_GC_PRIM_ARRAY_DUMP = 0x23
} hprofTag;
// Default stack trace ID (used for dummy HPROF_TRACE record)
enum {
STACK_TRACE_ID = 1,
INITIAL_CLASS_COUNT = 200
};
// Supports I/O operations on a dump file
class DumpWriter : public StackObj {
private:
enum {
io_buffer_size = 8*M
};
int _fd; // file descriptor (-1 if dump file not open)
julong _bytes_written; // number of byte written to dump file
char* _buffer; // internal buffer
size_t _size;
size_t _pos;
jlong _dump_start;
char* _error; // error message when I/O fails
void set_file_descriptor(int fd) { _fd = fd; }
int file_descriptor() const { return _fd; }
char* buffer() const { return _buffer; }
size_t buffer_size() const { return _size; }
size_t position() const { return _pos; }
void set_position(size_t pos) { _pos = pos; }
void set_error(const char* error) { _error = (char*)os::strdup(error); }
// all I/O go through this function
void write_internal(void* s, size_t len);
public:
DumpWriter(const char* path);
~DumpWriter();
void close();
bool is_open() const { return file_descriptor() >= 0; }
void flush();
jlong dump_start() const { return _dump_start; }
void set_dump_start(jlong pos);
julong current_record_length();
// total number of bytes written to the disk
julong bytes_written() const { return _bytes_written; }
// adjust the number of bytes written to disk (used to keep the count
// of the number of bytes written in case of rewrites)
void adjust_bytes_written(jlong n) { _bytes_written += n; }
// number of (buffered) bytes as yet unwritten to the dump file
size_t bytes_unwritten() const { return position(); }
char* error() const { return _error; }
jlong current_offset();
void seek_to_offset(jlong pos);
// writer functions
void write_raw(void* s, size_t len);
void write_u1(u1 x) { write_raw((void*)&x, 1); }
void write_u2(u2 x);
void write_u4(u4 x);
void write_u8(u8 x);
void write_objectID(oop o);
void write_symbolID(Symbol* o);
void write_classID(Klass* k);
void write_id(u4 x);
};
DumpWriter::DumpWriter(const char* path) {
// try to allocate an I/O buffer of io_buffer_size. If there isn't
// sufficient memory then reduce size until we can allocate something.
_size = io_buffer_size;
do {
_buffer = (char*)os::malloc(_size, mtInternal);
if (_buffer == NULL) {
_size = _size >> 1;
}
} while (_buffer == NULL && _size > 0);
assert((_size > 0 && _buffer != NULL) || (_size == 0 && _buffer == NULL), "sanity check");
_pos = 0;
_error = NULL;
_bytes_written = 0L;
_dump_start = (jlong)-1;
_fd = os::create_binary_file(path, false); // don't replace existing file
// if the open failed we record the error
if (_fd < 0) {
_error = (char*)os::strdup(os::strerror(errno));
}
}
DumpWriter::~DumpWriter() {
// flush and close dump file
if (is_open()) {
close();
}
if (_buffer != NULL) os::free(_buffer);
if (_error != NULL) os::free(_error);
}
// closes dump file (if open)
void DumpWriter::close() {
// flush and close dump file
if (is_open()) {
flush();
os::close(file_descriptor());
set_file_descriptor(-1);
}
}
// sets the dump starting position
void DumpWriter::set_dump_start(jlong pos) {
_dump_start = pos;
}
julong DumpWriter::current_record_length() {
if (is_open()) {
// calculate the size of the dump record
julong dump_end = bytes_written() + bytes_unwritten();
assert(dump_end == (size_t)current_offset(), "checking");
julong dump_len = dump_end - dump_start() - 4;
return dump_len;
}
return 0;
}
// write directly to the file
void DumpWriter::write_internal(void* s, size_t len) {
if (is_open()) {
const char* pos = (char*)s;
ssize_t n = 0;
while (len > 0) {
uint tmp = (uint)MIN2(len, (size_t)UINT_MAX);
n = os::write(file_descriptor(), pos, tmp);
if (n < 0) {
// EINTR cannot happen here, os::write will take care of that
set_error(os::strerror(errno));
os::close(file_descriptor());
set_file_descriptor(-1);
return;
}
_bytes_written += n;
pos += n;
len -= n;
}
}
}
// write raw bytes
void DumpWriter::write_raw(void* s, size_t len) {
if (is_open()) {
// flush buffer to make room
if ((position() + len) >= buffer_size()) {
flush();
}
// buffer not available or too big to buffer it
if ((buffer() == NULL) || (len >= buffer_size())) {
write_internal(s, len);
} else {
// Should optimize this for u1/u2/u4/u8 sizes.
memcpy(buffer() + position(), s, len);
set_position(position() + len);
}
}
}
// flush any buffered bytes to the file
void DumpWriter::flush() {
if (is_open() && position() > 0) {
write_internal(buffer(), position());
set_position(0);
}
}
jlong DumpWriter::current_offset() {
if (is_open()) {
// the offset is the file offset plus whatever we have buffered
jlong offset = os::current_file_offset(file_descriptor());
assert(offset >= 0, "lseek failed");
return offset + position();
} else {
return (jlong)-1;
}
}
void DumpWriter::seek_to_offset(jlong off) {
assert(off >= 0, "bad offset");
// need to flush before seeking
flush();
// may be closed due to I/O error
if (is_open()) {
jlong n = os::seek_to_file_offset(file_descriptor(), off);
assert(n >= 0, "lseek failed");
}
}
void DumpWriter::write_u2(u2 x) {
u2 v;
Bytes::put_Java_u2((address)&v, x);
write_raw((void*)&v, 2);
}
void DumpWriter::write_u4(u4 x) {
u4 v;
Bytes::put_Java_u4((address)&v, x);
write_raw((void*)&v, 4);
}
void DumpWriter::write_u8(u8 x) {
u8 v;
Bytes::put_Java_u8((address)&v, x);
write_raw((void*)&v, 8);
}
void DumpWriter::write_objectID(oop o) {
address a = (address)o;
#ifdef _LP64
write_u8((u8)a);
#else
write_u4((u4)a);
#endif
}
void DumpWriter::write_symbolID(Symbol* s) {
address a = (address)((uintptr_t)s);
#ifdef _LP64
write_u8((u8)a);
#else
write_u4((u4)a);
#endif
}
void DumpWriter::write_id(u4 x) {
#ifdef _LP64
write_u8((u8) x);
#else
write_u4(x);
#endif
}
// We use java mirror as the class ID
void DumpWriter::write_classID(Klass* k) {
write_objectID(k->java_mirror());
}
// Support class with a collection of functions used when dumping the heap
class DumperSupport : AllStatic {
public:
// write a header of the given type
static void write_header(DumpWriter* writer, hprofTag tag, u4 len);
// returns hprof tag for the given type signature
static hprofTag sig2tag(Symbol* sig);
// returns hprof tag for the given basic type
static hprofTag type2tag(BasicType type);
// returns the size of the instance of the given class
static u4 instance_size(Klass* k);
// dump a jfloat
static void dump_float(DumpWriter* writer, jfloat f);
// dump a jdouble
static void dump_double(DumpWriter* writer, jdouble d);
// dumps the raw value of the given field
static void dump_field_value(DumpWriter* writer, char type, oop obj, int offset);
// dumps static fields of the given class
static void dump_static_fields(DumpWriter* writer, Klass* k);
// dump the raw values of the instance fields of the given object
static void dump_instance_fields(DumpWriter* writer, oop o);
// dumps the definition of the instance fields for a given class
static void dump_instance_field_descriptors(DumpWriter* writer, Klass* k);
// creates HPROF_GC_INSTANCE_DUMP record for the given object
static void dump_instance(DumpWriter* writer, oop o);
// creates HPROF_GC_CLASS_DUMP record for the given class and each of its
// array classes
static void dump_class_and_array_classes(DumpWriter* writer, Klass* k);
// creates HPROF_GC_CLASS_DUMP record for a given primitive array
// class (and each multi-dimensional array class too)
static void dump_basic_type_array_class(DumpWriter* writer, Klass* k);
// creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
static void dump_object_array(DumpWriter* writer, objArrayOop array);
// creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
static void dump_prim_array(DumpWriter* writer, typeArrayOop array);
// create HPROF_FRAME record for the given method and bci
static void dump_stack_frame(DumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
// check if we need to truncate an array
static int calculate_array_max_length(DumpWriter* writer, arrayOop array, short header_size);
// writes a HPROF_HEAP_DUMP_SEGMENT record
static void write_dump_header(DumpWriter* writer);
// fixes up the length of the current dump record
static void write_current_dump_record_length(DumpWriter* writer);
// fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
static void end_of_dump(DumpWriter* writer);
static oop mask_dormant_archived_object(oop o) {
if (o != NULL && o->klass()->java_mirror() == NULL) {
// Ignore this object since the corresponding java mirror is not loaded.
// Might be a dormant archive object.
return NULL;
} else {
return o;
}
}
};
// write a header of the given type
void DumperSupport:: write_header(DumpWriter* writer, hprofTag tag, u4 len) {
writer->write_u1((u1)tag);
writer->write_u4(0); // current ticks
writer->write_u4(len);
}
// returns hprof tag for the given type signature
hprofTag DumperSupport::sig2tag(Symbol* sig) {
switch (sig->char_at(0)) {
case JVM_SIGNATURE_CLASS : return HPROF_NORMAL_OBJECT;
case JVM_SIGNATURE_ARRAY : return HPROF_NORMAL_OBJECT;
case JVM_SIGNATURE_BYTE : return HPROF_BYTE;
case JVM_SIGNATURE_CHAR : return HPROF_CHAR;
case JVM_SIGNATURE_FLOAT : return HPROF_FLOAT;
case JVM_SIGNATURE_DOUBLE : return HPROF_DOUBLE;
case JVM_SIGNATURE_INT : return HPROF_INT;
case JVM_SIGNATURE_LONG : return HPROF_LONG;
case JVM_SIGNATURE_SHORT : return HPROF_SHORT;
case JVM_SIGNATURE_BOOLEAN : return HPROF_BOOLEAN;
default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
}
}
hprofTag DumperSupport::type2tag(BasicType type) {
switch (type) {
case T_BYTE : return HPROF_BYTE;
case T_CHAR : return HPROF_CHAR;
case T_FLOAT : return HPROF_FLOAT;
case T_DOUBLE : return HPROF_DOUBLE;
case T_INT : return HPROF_INT;
case T_LONG : return HPROF_LONG;
case T_SHORT : return HPROF_SHORT;
case T_BOOLEAN : return HPROF_BOOLEAN;
default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
}
}
// dump a jfloat
void DumperSupport::dump_float(DumpWriter* writer, jfloat f) {
if (g_isnan(f)) {
writer->write_u4(0x7fc00000); // collapsing NaNs
} else {
union {
int i;
float f;
} u;
u.f = (float)f;
writer->write_u4((u4)u.i);
}
}
// dump a jdouble
void DumperSupport::dump_double(DumpWriter* writer, jdouble d) {
union {
jlong l;
double d;
} u;
if (g_isnan(d)) { // collapsing NaNs
u.l = (jlong)(0x7ff80000);
u.l = (u.l << 32);
} else {
u.d = (double)d;
}
writer->write_u8((u8)u.l);
}
// dumps the raw value of the given field
void DumperSupport::dump_field_value(DumpWriter* writer, char type, oop obj, int offset) {
switch (type) {
case JVM_SIGNATURE_CLASS :
case JVM_SIGNATURE_ARRAY : {
oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) {
ResourceMark rm;
log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
p2i(o), o->klass()->external_name(),
p2i(obj), obj->klass()->external_name());
}
o = mask_dormant_archived_object(o);
assert(oopDesc::is_oop_or_null(o), "Expected an oop or NULL at " PTR_FORMAT, p2i(o));
writer->write_objectID(o);
break;
}
case JVM_SIGNATURE_BYTE : {
jbyte b = obj->byte_field(offset);
writer->write_u1((u1)b);
break;
}
case JVM_SIGNATURE_CHAR : {
jchar c = obj->char_field(offset);
writer->write_u2((u2)c);
break;
}
case JVM_SIGNATURE_SHORT : {
jshort s = obj->short_field(offset);
writer->write_u2((u2)s);
break;
}
case JVM_SIGNATURE_FLOAT : {
jfloat f = obj->float_field(offset);
dump_float(writer, f);
break;
}
case JVM_SIGNATURE_DOUBLE : {
jdouble d = obj->double_field(offset);
dump_double(writer, d);
break;
}
case JVM_SIGNATURE_INT : {
jint i = obj->int_field(offset);
writer->write_u4((u4)i);
break;
}
case JVM_SIGNATURE_LONG : {
jlong l = obj->long_field(offset);
writer->write_u8((u8)l);
break;
}
case JVM_SIGNATURE_BOOLEAN : {
jboolean b = obj->bool_field(offset);
writer->write_u1((u1)b);
break;
}
default : {
ShouldNotReachHere();
break;
}
}
}
// returns the size of the instance of the given class
u4 DumperSupport::instance_size(Klass* k) {
HandleMark hm;
InstanceKlass* ik = InstanceKlass::cast(k);
u4 size = 0;
for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
if (!fld.access_flags().is_static()) {
Symbol* sig = fld.signature();
switch (sig->char_at(0)) {
case JVM_SIGNATURE_CLASS :
case JVM_SIGNATURE_ARRAY : size += oopSize; break;
case JVM_SIGNATURE_BYTE :
case JVM_SIGNATURE_BOOLEAN : size += 1; break;
case JVM_SIGNATURE_CHAR :
case JVM_SIGNATURE_SHORT : size += 2; break;
case JVM_SIGNATURE_INT :
case JVM_SIGNATURE_FLOAT : size += 4; break;
case JVM_SIGNATURE_LONG :
case JVM_SIGNATURE_DOUBLE : size += 8; break;
default : ShouldNotReachHere();
}
}
}
return size;
}
// dumps static fields of the given class
void DumperSupport::dump_static_fields(DumpWriter* writer, Klass* k) {
HandleMark hm;
InstanceKlass* ik = InstanceKlass::cast(k);
// pass 1 - count the static fields
u2 field_count = 0;
for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
if (fldc.access_flags().is_static()) field_count++;
}
// Add in resolved_references which is referenced by the cpCache
// The resolved_references is an array per InstanceKlass holding the
// strings and other oops resolved from the constant pool.
oop resolved_references = ik->constants()->resolved_references_or_null();
if (resolved_references != NULL) {
field_count++;
// Add in the resolved_references of the used previous versions of the class
// in the case of RedefineClasses
InstanceKlass* prev = ik->previous_versions();
while (prev != NULL && prev->constants()->resolved_references_or_null() != NULL) {
field_count++;
prev = prev->previous_versions();
}
}
// Also provide a pointer to the init_lock if present, so there aren't unreferenced int[0]
// arrays.
oop init_lock = ik->init_lock();
if (init_lock != NULL) {
field_count++;
}
writer->write_u2(field_count);
// pass 2 - dump the field descriptors and raw values
for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
if (fld.access_flags().is_static()) {
Symbol* sig = fld.signature();
writer->write_symbolID(fld.name()); // name
writer->write_u1(sig2tag(sig)); // type
// value
dump_field_value(writer, sig->char_at(0), ik->java_mirror(), fld.offset());
}
}
// Add resolved_references for each class that has them
if (resolved_references != NULL) {
writer->write_symbolID(vmSymbols::resolved_references_name()); // name
writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
writer->write_objectID(resolved_references);
// Also write any previous versions
InstanceKlass* prev = ik->previous_versions();
while (prev != NULL && prev->constants()->resolved_references_or_null() != NULL) {
writer->write_symbolID(vmSymbols::resolved_references_name()); // name
writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
writer->write_objectID(prev->constants()->resolved_references());
prev = prev->previous_versions();
}
}
// Add init lock to the end if the class is not yet initialized
if (init_lock != NULL) {
writer->write_symbolID(vmSymbols::init_lock_name()); // name
writer->write_u1(sig2tag(vmSymbols::int_array_signature())); // type
writer->write_objectID(init_lock);
}
}
// dump the raw values of the instance fields of the given object
void DumperSupport::dump_instance_fields(DumpWriter* writer, oop o) {
HandleMark hm;
InstanceKlass* ik = InstanceKlass::cast(o->klass());
for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
if (!fld.access_flags().is_static()) {
Symbol* sig = fld.signature();
dump_field_value(writer, sig->char_at(0), o, fld.offset());
}
}
}
// dumps the definition of the instance fields for a given class
void DumperSupport::dump_instance_field_descriptors(DumpWriter* writer, Klass* k) {
HandleMark hm;
InstanceKlass* ik = InstanceKlass::cast(k);
// pass 1 - count the instance fields
u2 field_count = 0;
for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
if (!fldc.access_flags().is_static()) field_count++;
}
writer->write_u2(field_count);
// pass 2 - dump the field descriptors
for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
if (!fld.access_flags().is_static()) {
Symbol* sig = fld.signature();
writer->write_symbolID(fld.name()); // name
writer->write_u1(sig2tag(sig)); // type
}
}
}
// creates HPROF_GC_INSTANCE_DUMP record for the given object
void DumperSupport::dump_instance(DumpWriter* writer, oop o) {
Klass* k = o->klass();
writer->write_u1(HPROF_GC_INSTANCE_DUMP);
writer->write_objectID(o);
writer->write_u4(STACK_TRACE_ID);
// class ID
writer->write_classID(k);
// number of bytes that follow
writer->write_u4(instance_size(k) );
// field values
dump_instance_fields(writer, o);
}
// creates HPROF_GC_CLASS_DUMP record for the given class and each of
// its array classes
void DumperSupport::dump_class_and_array_classes(DumpWriter* writer, Klass* k) {
InstanceKlass* ik = InstanceKlass::cast(k);
// We can safepoint and do a heap dump at a point where we have a Klass,
// but no java mirror class has been setup for it. So we need to check
// that the class is at least loaded, to avoid crash from a null mirror.