src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/HeapHprofBinWriter.java
author stefank
Tue, 19 Feb 2019 10:04:44 +0100
changeset 53816 f3e0c8ca50d9
parent 52810 a2500cf11ee5
child 53912 91f06b86c0da
permissions -rw-r--r--
8218970: SA: Enable HeapHprofBinWriter for ZGC Reviewed-by: eosterlund, ysuenaga
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
     2
 * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 670
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 670
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 670
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.utilities;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import java.nio.channels.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.memory.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
import sun.jvm.hotspot.oops.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
import sun.jvm.hotspot.runtime.*;
42889
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
    34
import sun.jvm.hotspot.classfile.*;
52810
a2500cf11ee5 8213323: sa/TestJmapCoreMetaspace.java and sa/TestJmapCore.java fail with ZGC
jgeorge
parents: 50929
diff changeset
    35
import sun.jvm.hotspot.gc.z.ZCollectedHeap;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
 * This class writes Java heap in hprof binary format. This format is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
 * used by Heap Analysis Tool (HAT). The class is heavily influenced
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
 * by 'hprof_io.c' of 1.5 new hprof implementation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
/* hprof binary format: (result either written to a file or sent over
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
 * the network).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
 * WARNING: This format is still under development, and is subject to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
 * change without notice.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
 *
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
    49
 * header     "JAVA PROFILE 1.0.2" (0-terminated)
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
    50
 * u4         size of identifiers. Identifiers are used to represent
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
 *            UTF8 strings, objects, stack traces, etc. They usually
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
 *            have the same size as host pointers. For example, on
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
 *            Solaris and Win32, the size is 4.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
 * u4         high word
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
 * u4         low word    number of milliseconds since 0:00 GMT, 1/1/70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
 * [record]*  a sequence of records.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
 * Record format:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
 * u1         a TAG denoting the type of the record
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
 * u4         number of *microseconds* since the time stamp in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
 *            header. (wraps around in a little more than an hour)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
 * u4         number of bytes *remaining* in the record. Note that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
 *            this number excludes the tag and the length field itself.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
 * [u1]*      BODY of the record (a sequence of bytes)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
 * The following TAGs are supported:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
 * TAG           BODY       notes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
 *----------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
 * HPROF_UTF8               a UTF8-encoded name
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
 *               id         name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
 *               [u1]*      UTF8 characters (no trailing zero)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
 * HPROF_LOAD_CLASS         a newly loaded class
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
 *                u4        class serial number (> 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
 *                id        class object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
 *                u4        stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
 *                id        class name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
 * HPROF_UNLOAD_CLASS       an unloading class
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
 *                u4        class serial_number
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
 * HPROF_FRAME              a Java stack frame
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
 *                id        stack frame ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
 *                id        method name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
 *                id        method signature ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
 *                id        source file name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
 *                u4        class serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
 *                i4        line number. >0: normal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
 *                                       -1: unknown
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
 *                                       -2: compiled method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
 *                                       -3: native method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
 * HPROF_TRACE              a Java stack trace
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
 *               u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
 *               u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
 *               u4         number of frames
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
 *               [id]*      stack frame IDs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
 * HPROF_ALLOC_SITES        a set of heap allocation sites, obtained after GC
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
 *               u2         flags 0x0001: incremental vs. complete
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
 *                                0x0002: sorted by allocation vs. live
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
 *                                0x0004: whether to force a GC
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
 *               u4         cutoff ratio
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
 *               u4         total live bytes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
 *               u4         total live instances
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
 *               u8         total bytes allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
 *               u8         total instances allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
 *               u4         number of sites that follow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
 *               [u1        is_array: 0:  normal object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
 *                                    2:  object array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
 *                                    4:  boolean array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
 *                                    5:  char array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
 *                                    6:  float array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
 *                                    7:  double array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
 *                                    8:  byte array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
 *                                    9:  short array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
 *                                    10: int array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
 *                                    11: long array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
 *                u4        class serial number (may be zero during startup)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
 *                u4        stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
 *                u4        number of bytes alive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
 *                u4        number of instances alive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
 *                u4        number of bytes allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
 *                u4]*      number of instance allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
 * HPROF_START_THREAD       a newly started thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
 *               u4         thread serial number (> 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
 *               id         thread object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
 *               u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
 *               id         thread name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
 *               id         thread group name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
 *               id         thread group parent name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
 * HPROF_END_THREAD         a terminating thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
 *               u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
 * HPROF_HEAP_SUMMARY       heap summary
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
 *               u4         total live bytes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
 *               u4         total live instances
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
 *               u8         total bytes allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
 *               u8         total instances allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
 * HPROF_HEAP_DUMP          denote a heap dump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
 *               [heap dump sub-records]*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
 *                          There are four kinds of heap dump sub-records:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
 *               u1         sub-record type
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
 *               HPROF_GC_ROOT_UNKNOWN         unknown root
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
 *               HPROF_GC_ROOT_THREAD_OBJ      thread object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
 *                          id         thread object ID  (may be 0 for a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
 *                                     thread newly attached through JNI)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
 *                          u4         thread sequence number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
 *                          u4         stack trace sequence number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
 *               HPROF_GC_ROOT_JNI_GLOBAL      JNI global ref root
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
 *                          id         JNI global ref ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
 *               HPROF_GC_ROOT_JNI_LOCAL       JNI local ref
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
 *                          u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
 *                          u4         frame # in stack trace (-1 for empty)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
 *               HPROF_GC_ROOT_JAVA_FRAME      Java stack frame
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
 *                          u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
 *                          u4         frame # in stack trace (-1 for empty)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
 *               HPROF_GC_ROOT_NATIVE_STACK    Native stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
 *                          u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
 *               HPROF_GC_ROOT_STICKY_CLASS    System class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
 *               HPROF_GC_ROOT_THREAD_BLOCK    Reference from thread block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
 *                          u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
 *               HPROF_GC_ROOT_MONITOR_USED    Busy monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
 *               HPROF_GC_CLASS_DUMP           dump of a class object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
 *                          id         class object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
 *                          u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
 *                          id         super class object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
 *                          id         class loader object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
 *                          id         signers object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
 *                          id         protection domain object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
 *                          id         reserved
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
 *                          id         reserved
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
 *                          u4         instance size (in bytes)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
 *                          u2         size of constant pool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
 *                          [u2,       constant pool index,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
 *                           ty,       type
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
 *                                     2:  object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
 *                                     4:  boolean
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
 *                                     5:  char
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
 *                                     6:  float
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
 *                                     7:  double
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
 *                                     8:  byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
 *                                     9:  short
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
 *                                     10: int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
 *                                     11: long
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
 *                           vl]*      and value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
 *                          u2         number of static fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
 *                          [id,       static field name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
 *                           ty,       type,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
 *                           vl]*      and value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
 *                          u2         number of inst. fields (not inc. super)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
 *                          [id,       instance field name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
 *                           ty]*      type
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
 *               HPROF_GC_INSTANCE_DUMP        dump of a normal object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
 *                          u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
 *                          id         class object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
 *                          u4         number of bytes that follow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
 *                          [vl]*      instance field values (class, followed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
 *                                     by super, super's super ...)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
 *               HPROF_GC_OBJ_ARRAY_DUMP       dump of an object array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
 *                          id         array object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
 *                          u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
 *                          u4         number of elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
 *                          id         array class ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
 *                          [id]*      elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
 *               HPROF_GC_PRIM_ARRAY_DUMP      dump of a primitive array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
 *                          id         array object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
 *                          u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
 *                          u4         number of elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
 *                          u1         element type
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
 *                                     4:  boolean array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
 *                                     5:  char array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
 *                                     6:  float array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
 *                                     7:  double array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
 *                                     8:  byte array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
 *                                     9:  short array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
 *                                     10: int array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
 *                                     11: long array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
 *                          [u1]*      elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
 * HPROF_CPU_SAMPLES        a set of sample traces of running threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
 *                u4        total number of samples
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
 *                u4        # of traces
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
 *               [u4        # of samples
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
 *                u4]*      stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
 * HPROF_CONTROL_SETTINGS   the settings of on/off switches
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
 *                u4        0x00000001: alloc traces on/off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
 *                          0x00000002: cpu sampling on/off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
 *                u2        stack trace depth
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
 *
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   297
 *
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   298
 * A heap dump can optionally be generated as a sequence of heap dump
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   299
 * segments. This sequence is terminated by an end record. The additional
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   300
 * tags allowed by format "JAVA PROFILE 1.0.2" are:
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   301
 *
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   302
 * HPROF_HEAP_DUMP_SEGMENT  denote a heap dump segment
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   303
 *
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   304
 *               [heap dump sub-records]*
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   305
 *               The same sub-record types allowed by HPROF_HEAP_DUMP
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   306
 *
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   307
 * HPROF_HEAP_DUMP_END      denotes the end of a heap dump
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   308
 *
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
public class HeapHprofBinWriter extends AbstractHeapGraphWriter {
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   312
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   313
    // Record which Symbol names have been dumped already.
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   314
    private HashSet<Symbol> names;
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   315
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   316
    private static final long HPROF_SEGMENTED_HEAP_DUMP_THRESHOLD = 2L * 0x40000000;
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   317
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   318
    // The approximate size of a heap segment. Used to calculate when to create
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   319
    // a new segment.
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   320
    private static final long HPROF_SEGMENTED_HEAP_DUMP_SEGMENT_SIZE = 1L * 0x40000000;
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   321
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
    // hprof binary file header
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   323
    private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2";
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    // constants in enum HprofTag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
    private static final int HPROF_UTF8             = 0x01;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    private static final int HPROF_LOAD_CLASS       = 0x02;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
    private static final int HPROF_UNLOAD_CLASS     = 0x03;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    private static final int HPROF_FRAME            = 0x04;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    private static final int HPROF_TRACE            = 0x05;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    private static final int HPROF_ALLOC_SITES      = 0x06;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    private static final int HPROF_HEAP_SUMMARY     = 0x07;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    private static final int HPROF_START_THREAD     = 0x0A;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    private static final int HPROF_END_THREAD       = 0x0B;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    private static final int HPROF_HEAP_DUMP        = 0x0C;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    private static final int HPROF_CPU_SAMPLES      = 0x0D;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    private static final int HPROF_CONTROL_SETTINGS = 0x0E;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   339
    // 1.0.2 record types
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   340
    private static final int HPROF_HEAP_DUMP_SEGMENT = 0x1C;
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   341
    private static final int HPROF_HEAP_DUMP_END     = 0x2C;
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   342
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    // Heap dump constants
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
    // constants in enum HprofGcTag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    private static final int HPROF_GC_ROOT_UNKNOWN       = 0xFF;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
    private static final int HPROF_GC_ROOT_JNI_GLOBAL    = 0x01;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
    private static final int HPROF_GC_ROOT_JNI_LOCAL     = 0x02;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    private static final int HPROF_GC_ROOT_JAVA_FRAME    = 0x03;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
    private static final int HPROF_GC_ROOT_NATIVE_STACK  = 0x04;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    private static final int HPROF_GC_ROOT_STICKY_CLASS  = 0x05;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    private static final int HPROF_GC_ROOT_THREAD_BLOCK  = 0x06;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
    private static final int HPROF_GC_ROOT_MONITOR_USED  = 0x07;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    private static final int HPROF_GC_ROOT_THREAD_OBJ    = 0x08;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
    private static final int HPROF_GC_CLASS_DUMP         = 0x20;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
    private static final int HPROF_GC_INSTANCE_DUMP      = 0x21;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    private static final int HPROF_GC_OBJ_ARRAY_DUMP     = 0x22;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    private static final int HPROF_GC_PRIM_ARRAY_DUMP    = 0x23;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    // constants in enum HprofType
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    private static final int HPROF_ARRAY_OBJECT  = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    private static final int HPROF_NORMAL_OBJECT = 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
    private static final int HPROF_BOOLEAN       = 4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    private static final int HPROF_CHAR          = 5;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    private static final int HPROF_FLOAT         = 6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
    private static final int HPROF_DOUBLE        = 7;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
    private static final int HPROF_BYTE          = 8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    private static final int HPROF_SHORT         = 9;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
    private static final int HPROF_INT           = 10;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    private static final int HPROF_LONG          = 11;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
    // Java type codes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
    private static final int JVM_SIGNATURE_BOOLEAN = 'Z';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
    private static final int JVM_SIGNATURE_CHAR    = 'C';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
    private static final int JVM_SIGNATURE_BYTE    = 'B';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    private static final int JVM_SIGNATURE_SHORT   = 'S';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    private static final int JVM_SIGNATURE_INT     = 'I';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    private static final int JVM_SIGNATURE_LONG    = 'J';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
    private static final int JVM_SIGNATURE_FLOAT   = 'F';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    private static final int JVM_SIGNATURE_DOUBLE  = 'D';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    private static final int JVM_SIGNATURE_ARRAY   = '[';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    private static final int JVM_SIGNATURE_CLASS   = 'L';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   383
    private static final long MAX_U4_VALUE = 0xFFFFFFFFL;
42889
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   384
    int serialNum = 1;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   385
45371
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   386
    public HeapHprofBinWriter() {
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   387
        this.KlassMap = new ArrayList<Klass>();
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   388
        this.names = new HashSet<Symbol>();
45371
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   389
    }
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   390
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    public synchronized void write(String fileName) throws IOException {
52810
a2500cf11ee5 8213323: sa/TestJmapCoreMetaspace.java and sa/TestJmapCore.java fail with ZGC
jgeorge
parents: 50929
diff changeset
   392
        VM vm = VM.getVM();
a2500cf11ee5 8213323: sa/TestJmapCoreMetaspace.java and sa/TestJmapCore.java fail with ZGC
jgeorge
parents: 50929
diff changeset
   393
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
        // open file stream and create buffered data output stream
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   395
        fos = new FileOutputStream(fileName);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
        out = new DataOutputStream(new BufferedOutputStream(fos));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
        dbg = vm.getDebugger();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
        objectHeap = vm.getObjectHeap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
        OBJ_ID_SIZE = (int) vm.getOopSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
        BOOLEAN_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_BOOLEAN);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
        BYTE_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_BYTE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
        CHAR_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_CHAR);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
        SHORT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_SHORT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
        INT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_INT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
        LONG_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_LONG);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
        FLOAT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_FLOAT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
        DOUBLE_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_DOUBLE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
        OBJECT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_OBJECT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
        BOOLEAN_SIZE = objectHeap.getBooleanSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
        BYTE_SIZE = objectHeap.getByteSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
        CHAR_SIZE = objectHeap.getCharSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
        SHORT_SIZE = objectHeap.getShortSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
        INT_SIZE = objectHeap.getIntSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
        LONG_SIZE = objectHeap.getLongSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
        FLOAT_SIZE = objectHeap.getFloatSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
        DOUBLE_SIZE = objectHeap.getDoubleSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   422
        // Check weather we should dump the heap as segments
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   423
        useSegmentedHeapDump = vm.getUniverse().heap().used() > HPROF_SEGMENTED_HEAP_DUMP_THRESHOLD;
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   424
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
        // hprof bin format header
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
        writeFileHeader();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
        // dummy stack trace without any frames so that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
        // HAT can be run without -stack false option
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
        writeDummyTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
        // hprof UTF-8 symbols section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
        writeSymbols();
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   434
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
        // HPROF_LOAD_CLASS records for all classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
        writeClasses();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
45371
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   438
        // write HPROF_FRAME and HPROF_TRACE records
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   439
        dumpStackTraces();
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   440
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
        // write CLASS_DUMP records
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
        writeClassDumpRecords();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
        // this will write heap data into the buffer stream
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
        super.write();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   447
        // flush buffer stream.
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   448
        out.flush();
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   449
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   450
        // Fill in final length
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   451
        fillInHeapRecordLength();
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   452
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   453
        if (useSegmentedHeapDump) {
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   454
            // Write heap segment-end record
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   455
            out.writeByte((byte) HPROF_HEAP_DUMP_END);
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   456
            out.writeInt(0);
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   457
            out.writeInt(0);
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   458
        }
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   459
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
        // flush buffer stream and throw it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
        out.flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
        out = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   464
        // close the file stream
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   465
        fos.close();
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   466
    }
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   467
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   468
    @Override
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   469
    protected void writeHeapRecordPrologue() throws IOException {
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   470
        if (currentSegmentStart == 0) {
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   471
            // write heap data header, depending on heap size use segmented heap
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   472
            // format
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   473
            out.writeByte((byte) (useSegmentedHeapDump ? HPROF_HEAP_DUMP_SEGMENT
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   474
                    : HPROF_HEAP_DUMP));
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   475
            out.writeInt(0);
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   476
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   477
            // remember position of dump length, we will fixup
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   478
            // length later - hprof format requires length.
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   479
            out.flush();
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   480
            currentSegmentStart = fos.getChannel().position();
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   481
            // write dummy length of 0 and we'll fix it later.
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   482
            out.writeInt(0);
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   483
        }
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   484
    }
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   485
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   486
    @Override
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   487
    protected void writeHeapRecordEpilogue() throws IOException {
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   488
        if (useSegmentedHeapDump) {
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   489
            out.flush();
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   490
            if ((fos.getChannel().position() - currentSegmentStart - 4L) >= HPROF_SEGMENTED_HEAP_DUMP_SEGMENT_SIZE) {
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   491
                fillInHeapRecordLength();
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   492
                currentSegmentStart = 0;
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   493
            }
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   494
        }
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   495
    }
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   496
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   497
    private void fillInHeapRecordLength() throws IOException {
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   498
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   499
        // now get the current position to calculate length
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   500
        long dumpEnd = fos.getChannel().position();
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   501
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   502
        // calculate the length of heap data
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   503
        long dumpLenLong = (dumpEnd - currentSegmentStart - 4L);
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   504
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   505
        // Check length boundary, overflow could happen but is _very_ unlikely
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   506
        if (dumpLenLong >= (4L * 0x40000000)) {
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   507
            throw new RuntimeException("Heap segment size overflow.");
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   508
        }
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   509
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   510
        // Save the current position
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   511
        long currentPosition = fos.getChannel().position();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
        // seek the position to write length
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   514
        fos.getChannel().position(currentSegmentStart);
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   515
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   516
        int dumpLen = (int) dumpLenLong;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
        // write length as integer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
        fos.write((dumpLen >>> 24) & 0xFF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
        fos.write((dumpLen >>> 16) & 0xFF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
        fos.write((dumpLen >>> 8) & 0xFF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
        fos.write((dumpLen >>> 0) & 0xFF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   524
        //Reset to previous current position
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
   525
        fos.getChannel().position(currentPosition);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   528
    // get the size in bytes for the requested type
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   529
    private long getSizeForType(int type) throws IOException {
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   530
        switch (type) {
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   531
            case TypeArrayKlass.T_BOOLEAN:
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   532
                return BOOLEAN_SIZE;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   533
            case TypeArrayKlass.T_INT:
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   534
                return INT_SIZE;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   535
            case TypeArrayKlass.T_CHAR:
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   536
                return CHAR_SIZE;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   537
            case TypeArrayKlass.T_SHORT:
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   538
                return SHORT_SIZE;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   539
            case TypeArrayKlass.T_BYTE:
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   540
                return BYTE_SIZE;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   541
            case TypeArrayKlass.T_LONG:
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   542
                return LONG_SIZE;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   543
            case TypeArrayKlass.T_FLOAT:
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   544
                return FLOAT_SIZE;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   545
            case TypeArrayKlass.T_DOUBLE:
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   546
                return DOUBLE_SIZE;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   547
            default:
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   548
                throw new RuntimeException(
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   549
                    "Should not reach here: Unknown type: " + type);
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   550
         }
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   551
    }
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   552
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   553
    private int getArrayHeaderSize(boolean isObjectAarray) {
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   554
        return isObjectAarray?
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   555
            ((int) BYTE_SIZE + 2 * (int) INT_SIZE + 2 * (int) OBJ_ID_SIZE):
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   556
            (2 * (int) BYTE_SIZE + 2 * (int) INT_SIZE + (int) OBJ_ID_SIZE);
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   557
    }
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   558
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   559
    // Check if we need to truncate an array
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   560
    private int calculateArrayMaxLength(long originalArrayLength,
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   561
                                        int headerSize,
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   562
                                        long typeSize,
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   563
                                        String typeName) throws IOException {
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   564
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   565
        long length = originalArrayLength;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   566
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   567
        // now get the current position to calculate length
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   568
        long dumpEnd = fos.getChannel().position();
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   569
        long originalLengthInBytes = originalArrayLength * typeSize;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   570
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   571
        // calculate the length of heap data
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   572
        long currentRecordLength = (dumpEnd - currentSegmentStart - 4L);
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   573
        if (currentRecordLength > 0 &&
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   574
            (currentRecordLength + headerSize + originalLengthInBytes) > MAX_U4_VALUE) {
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   575
            fillInHeapRecordLength();
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   576
            currentSegmentStart = 0;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   577
            writeHeapRecordPrologue();
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   578
            currentRecordLength = 0;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   579
        }
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   580
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   581
        // Calculate the max bytes we can use.
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   582
        long maxBytes = (MAX_U4_VALUE - (headerSize + currentRecordLength));
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   583
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   584
        if (originalLengthInBytes > maxBytes) {
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   585
            length = maxBytes/typeSize;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   586
            System.err.println("WARNING: Cannot dump array of type " + typeName
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   587
                               + " with length " + originalArrayLength
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   588
                               + "; truncating to length " + length);
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   589
        }
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   590
        return (int) length;
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   591
    }
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   592
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
    private void writeClassDumpRecords() throws IOException {
42889
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   594
        ClassLoaderDataGraph cldGraph = VM.getVM().getClassLoaderDataGraph();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
        try {
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 45371
diff changeset
   596
             cldGraph.classesDo(new ClassLoaderDataGraph.ClassVisitor() {
42889
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   597
                            public void visit(Klass k) {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   598
                                try {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   599
                                    writeHeapRecordPrologue();
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   600
                                    writeClassDumpRecord(k);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   601
                                    writeHeapRecordEpilogue();
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   602
                                } catch (IOException e) {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   603
                                    throw new RuntimeException(e);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   604
                                }
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   605
                            }
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
   606
                        });
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
        } catch (RuntimeException re) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
            handleRuntimeException(re);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
    protected void writeClass(Instance instance) throws IOException {
8878
a6283814032c 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 8729
diff changeset
   613
        Klass reflectedKlass = java_lang_Class.asKlass(instance);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
        // dump instance record only for primitive type Class objects.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
        // all other Class objects are covered by writeClassDumpRecords.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
        if (reflectedKlass == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
            writeInstance(instance);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
    private void writeClassDumpRecord(Klass k) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
        out.writeByte((byte)HPROF_GC_CLASS_DUMP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
        writeObjectID(k.getJavaMirror());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
        out.writeInt(DUMMY_STACK_TRACE_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
        Klass superKlass = k.getJavaSuper();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
        if (superKlass != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
            writeObjectID(superKlass.getJavaMirror());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
            writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
        if (k instanceof InstanceKlass) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
            InstanceKlass ik = (InstanceKlass) k;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
            writeObjectID(ik.getClassLoader());
17826
9ad5cd464a75 8003421: NPG: Move oops out of InstanceKlass into mirror
coleenp
parents: 13728
diff changeset
   635
            writeObjectID(null);  // ik.getJavaMirror().getSigners());
9ad5cd464a75 8003421: NPG: Move oops out of InstanceKlass into mirror
coleenp
parents: 13728
diff changeset
   636
            writeObjectID(null);  // ik.getJavaMirror().getProtectionDomain());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
            // two reserved id fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
            writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
            writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
            List fields = getInstanceFields(ik);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
            int instSize = getSizeForFields(fields);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
            classDataCache.put(ik, new ClassData(instSize, fields));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
            out.writeInt(instSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
            // For now, ignore constant pool - HAT ignores too!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
            // output number of cp entries as zero.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
            out.writeShort((short) 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
            List declaredFields = ik.getImmediateFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
            List staticFields = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
            List instanceFields = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
            Iterator itr = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
            for (itr = declaredFields.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
                Field field = (Field) itr.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
                if (field.isStatic()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
                    staticFields.add(field);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
                    instanceFields.add(field);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
            // dump static field descriptors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
            writeFieldDescriptors(staticFields, ik);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
            // dump instance field descriptors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
            writeFieldDescriptors(instanceFields, null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
            if (k instanceof ObjArrayKlass) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
                ObjArrayKlass oak = (ObjArrayKlass) k;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
                Klass bottomKlass = oak.getBottomKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
                if (bottomKlass instanceof InstanceKlass) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
                    InstanceKlass ik = (InstanceKlass) bottomKlass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
                    writeObjectID(ik.getClassLoader());
17826
9ad5cd464a75 8003421: NPG: Move oops out of InstanceKlass into mirror
coleenp
parents: 13728
diff changeset
   674
                    writeObjectID(null); // ik.getJavaMirror().getSigners());
9ad5cd464a75 8003421: NPG: Move oops out of InstanceKlass into mirror
coleenp
parents: 13728
diff changeset
   675
                    writeObjectID(null); // ik.getJavaMirror().getProtectionDomain());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
                    writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
                    writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
                    writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
                writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
                writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
                writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
            // two reserved id fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
            writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
            writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
            // write zero instance size -- as instance size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
            // is variable for arrays.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
            out.writeInt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
            // no constant pool for array klasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
            out.writeShort((short) 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
            // no static fields for array klasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
            out.writeShort((short) 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
            // no instance fields for array klasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
            out.writeShort((short) 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
45371
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   701
    private void dumpStackTraces() throws IOException {
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   702
        // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   703
        writeHeader(HPROF_TRACE, 3 * (int)INT_SIZE );
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   704
        out.writeInt(DUMMY_STACK_TRACE_ID);
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   705
        out.writeInt(0);                    // thread number
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   706
        out.writeInt(0);                    // frame count
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   707
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   708
        int frameSerialNum = 0;
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   709
        int numThreads = 0;
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   710
        Threads threads = VM.getVM().getThreads();
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   711
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   712
        for (JavaThread thread = threads.first(); thread != null; thread = thread.next()) {
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   713
            Oop threadObj = thread.getThreadObj();
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   714
            if (threadObj != null && !thread.isExiting() && !thread.isHiddenFromExternalView()) {
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   715
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   716
                // dump thread stack trace
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   717
                ThreadStackTrace st = new ThreadStackTrace(thread);
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   718
                st.dumpStack(-1);
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   719
                numThreads++;
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   720
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   721
                // write HPROF_FRAME records for this thread's stack trace
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   722
                int depth = st.getStackDepth();
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   723
                int threadFrameStart = frameSerialNum;
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   724
                for (int j=0; j < depth; j++) {
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   725
                    StackFrameInfo frame = st.stackFrameAt(j);
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   726
                    Method m = frame.getMethod();
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   727
                    int classSerialNum = KlassMap.indexOf(m.getMethodHolder()) + 1;
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   728
                    // the class serial number starts from 1
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   729
                    assert classSerialNum > 0:"class not found";
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   730
                    dumpStackFrame(++frameSerialNum, classSerialNum, m, frame.getBCI());
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   731
                }
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   732
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   733
                // write HPROF_TRACE record for one thread
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   734
                writeHeader(HPROF_TRACE, 3 * (int)INT_SIZE + depth * (int)VM.getVM().getOopSize());
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   735
                int stackSerialNum = numThreads + DUMMY_STACK_TRACE_ID;
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   736
                out.writeInt(stackSerialNum);      // stack trace serial number
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   737
                out.writeInt(numThreads);          // thread serial number
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   738
                out.writeInt(depth);               // frame count
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   739
                for (int j=1; j <= depth; j++) {
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   740
                    writeObjectID(threadFrameStart + j);
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   741
                }
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   742
            }
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   743
        }
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   744
    }
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   745
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   746
    private void dumpStackFrame(int frameSN, int classSN, Method m, int bci) throws IOException {
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   747
        int lineNumber;
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   748
        if (m.isNative()) {
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   749
            lineNumber = -3; // native frame
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   750
        } else {
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   751
            lineNumber = m.getLineNumberFromBCI(bci);
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   752
        }
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   753
        // First dump UTF8 if needed
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   754
        writeSymbol(m.getName());                              // method's name
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   755
        writeSymbol(m.getSignature());                         // method's signature
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   756
        writeSymbol(m.getMethodHolder().getSourceFileName());  // source file name
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   757
        // Then write FRAME descriptor
45371
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   758
        writeHeader(HPROF_FRAME, 4 * (int)VM.getVM().getOopSize() + 2 * (int)INT_SIZE);
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   759
        writeObjectID(frameSN);                                  // frame serial number
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   760
        writeSymbolID(m.getName());                              // method's name
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   761
        writeSymbolID(m.getSignature());                         // method's signature
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   762
        writeSymbolID(m.getMethodHolder().getSourceFileName());  // source file name
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   763
        out.writeInt(classSN);                                   // class serial number
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   764
        out.writeInt(lineNumber);                                // line number
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   765
    }
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
   766
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
    protected void writeJavaThread(JavaThread jt, int index) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
        out.writeByte((byte) HPROF_GC_ROOT_THREAD_OBJ);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
        writeObjectID(jt.getThreadObj());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
        out.writeInt(index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
        out.writeInt(DUMMY_STACK_TRACE_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
        writeLocalJNIHandles(jt, index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
    protected void writeLocalJNIHandles(JavaThread jt, int index) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
        final int threadIndex = index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
        JNIHandleBlock blk = jt.activeHandles();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
        if (blk != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
            try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
                blk.oopsDo(new AddressVisitor() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
                           public void visitAddress(Address handleAddr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
                               try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
                                   if (handleAddr != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
                                       OopHandle oopHandle = handleAddr.getOopHandleAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
                                       Oop oop = objectHeap.newOop(oopHandle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
                                       // exclude JNI handles hotspot internal objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
                                       if (oop != null && isJavaVisible(oop)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
                                           out.writeByte((byte) HPROF_GC_ROOT_JNI_LOCAL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
                                           writeObjectID(oop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
                                           out.writeInt(threadIndex);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
                                           out.writeInt(EMPTY_FRAME_DEPTH);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
                                       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
                                   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
                               } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
                                   throw new RuntimeException(exp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
                               }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
                           }
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   798
                           public void visitCompOopAddress(Address handleAddr) {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   799
                             throw new RuntimeException(
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   800
                                   " Should not reach here. JNIHandles are not compressed \n");
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   801
                           }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
                       });
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
            } catch (RuntimeException re) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
                handleRuntimeException(re);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
    protected void writeGlobalJNIHandle(Address handleAddr) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
        OopHandle oopHandle = handleAddr.getOopHandleAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
        Oop oop = objectHeap.newOop(oopHandle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
        // exclude JNI handles of hotspot internal objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
        if (oop != null && isJavaVisible(oop)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
            out.writeByte((byte) HPROF_GC_ROOT_JNI_GLOBAL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
            writeObjectID(oop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
            // use JNIHandle address as ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
            writeObjectID(getAddressValue(handleAddr));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
    protected void writeObjectArray(ObjArray array) throws IOException {
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   822
        int headerSize = getArrayHeaderSize(true);
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   823
        final int length = calculateArrayMaxLength(array.getLength(),
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   824
                                                   headerSize,
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   825
                                                   OBJ_ID_SIZE,
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   826
                                                   "Object");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
        out.writeByte((byte) HPROF_GC_OBJ_ARRAY_DUMP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
        writeObjectID(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
        out.writeInt(DUMMY_STACK_TRACE_ID);
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   830
        out.writeInt(length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
        writeObjectID(array.getKlass().getJavaMirror());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
        for (int index = 0; index < length; index++) {
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   833
            OopHandle handle = array.getOopHandleAt(index);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
            writeObjectID(getAddressValue(handle));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
    protected void writePrimitiveArray(TypeArray array) throws IOException {
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   839
        int headerSize = getArrayHeaderSize(false);
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   840
        TypeArrayKlass tak = (TypeArrayKlass) array.getKlass();
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   841
        final int type = (int) tak.getElementType();
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   842
        final String typeName = tak.getElementTypeName();
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   843
        final long typeSize = getSizeForType(type);
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   844
        final int length = calculateArrayMaxLength(array.getLength(),
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   845
                                                   headerSize,
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   846
                                                   typeSize,
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   847
                                                   typeName);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
        out.writeByte((byte) HPROF_GC_PRIM_ARRAY_DUMP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
        writeObjectID(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
        out.writeInt(DUMMY_STACK_TRACE_ID);
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   851
        out.writeInt(length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
        out.writeByte((byte) type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
        switch (type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
            case TypeArrayKlass.T_BOOLEAN:
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   855
                writeBooleanArray(array, length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
            case TypeArrayKlass.T_CHAR:
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   858
                writeCharArray(array, length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
            case TypeArrayKlass.T_FLOAT:
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   861
                writeFloatArray(array, length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
            case TypeArrayKlass.T_DOUBLE:
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   864
                writeDoubleArray(array, length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
            case TypeArrayKlass.T_BYTE:
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   867
                writeByteArray(array, length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
            case TypeArrayKlass.T_SHORT:
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   870
                writeShortArray(array, length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
            case TypeArrayKlass.T_INT:
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   873
                writeIntArray(array, length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
            case TypeArrayKlass.T_LONG:
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   876
                writeLongArray(array, length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
            default:
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   879
                throw new RuntimeException(
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   880
                    "Should not reach here: Unknown type: " + type);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   884
    private void writeBooleanArray(TypeArray array, int length) throws IOException {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
             long offset = BOOLEAN_BASE_OFFSET + index * BOOLEAN_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
             out.writeBoolean(array.getHandle().getJBooleanAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   891
    private void writeByteArray(TypeArray array, int length) throws IOException {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
             long offset = BYTE_BASE_OFFSET + index * BYTE_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
             out.writeByte(array.getHandle().getJByteAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   898
    private void writeShortArray(TypeArray array, int length) throws IOException {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
             long offset = SHORT_BASE_OFFSET + index * SHORT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
             out.writeShort(array.getHandle().getJShortAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   905
    private void writeIntArray(TypeArray array, int length) throws IOException {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
             long offset = INT_BASE_OFFSET + index * INT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
             out.writeInt(array.getHandle().getJIntAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   912
    private void writeLongArray(TypeArray array, int length) throws IOException {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
             long offset = LONG_BASE_OFFSET + index * LONG_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
             out.writeLong(array.getHandle().getJLongAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   919
    private void writeCharArray(TypeArray array, int length) throws IOException {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
             long offset = CHAR_BASE_OFFSET + index * CHAR_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
             out.writeChar(array.getHandle().getJCharAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   926
    private void writeFloatArray(TypeArray array, int length) throws IOException {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
             long offset = FLOAT_BASE_OFFSET + index * FLOAT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
             out.writeFloat(array.getHandle().getJFloatAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
   933
    private void writeDoubleArray(TypeArray array, int length) throws IOException {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
             long offset = DOUBLE_BASE_OFFSET + index * DOUBLE_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
             out.writeDouble(array.getHandle().getJDoubleAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
    protected void writeInstance(Instance instance) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
        out.writeByte((byte) HPROF_GC_INSTANCE_DUMP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
        writeObjectID(instance);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
        out.writeInt(DUMMY_STACK_TRACE_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
        Klass klass = instance.getKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
        writeObjectID(klass.getJavaMirror());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
        ClassData cd = (ClassData) classDataCache.get(klass);
30235
000cc9f42b61 8044416: serviceability/sa/jmap-hashcode/Test8028623.java fails with AssertionFailure: can not get class data for java/lang/UNIXProcess$Platform$$Lambda
dsamersoff
parents: 22234
diff changeset
   948
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
        if (Assert.ASSERTS_ENABLED) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 8878
diff changeset
   950
            Assert.that(cd != null, "can not get class data for " + klass.getName().asString() + klass.getAddress());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
        List fields = cd.fields;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
        int size = cd.instSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
        out.writeInt(size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
        for (Iterator itr = fields.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
            writeField((Field) itr.next(), instance);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
    //-- Internals only below this point
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
    private void writeFieldDescriptors(List fields, InstanceKlass ik)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
        throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
        // ik == null for instance fields.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
        out.writeShort((short) fields.size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
        for (Iterator itr = fields.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
            Field field = (Field) itr.next();
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
   968
            Symbol name = field.getName();
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   969
            writeSymbolID(name);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
            char typeCode = (char) field.getSignature().getByteAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
            int kind = signatureToHprofKind(typeCode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
            out.writeByte((byte)kind);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
            if (ik != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
                // static field
8729
cdb7525d39cb 7030300: more nightly failures after statics in Class changes
never
parents: 8076
diff changeset
   975
                writeField(field, ik.getJavaMirror());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
    public static int signatureToHprofKind(char ch) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
        switch (ch) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
        case JVM_SIGNATURE_CLASS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
        case JVM_SIGNATURE_ARRAY:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
            return HPROF_NORMAL_OBJECT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
        case JVM_SIGNATURE_BOOLEAN:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
            return HPROF_BOOLEAN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
        case JVM_SIGNATURE_CHAR:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
            return HPROF_CHAR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
        case JVM_SIGNATURE_FLOAT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
            return HPROF_FLOAT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
        case JVM_SIGNATURE_DOUBLE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
            return HPROF_DOUBLE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
        case JVM_SIGNATURE_BYTE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
            return HPROF_BYTE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
        case JVM_SIGNATURE_SHORT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
            return HPROF_SHORT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
        case JVM_SIGNATURE_INT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
            return HPROF_INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
        case JVM_SIGNATURE_LONG:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
            return HPROF_LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
        default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
            throw new RuntimeException("should not reach here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
    private void writeField(Field field, Oop oop) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
        char typeCode = (char) field.getSignature().getByteAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1008
        switch (typeCode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
        case JVM_SIGNATURE_BOOLEAN:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
            out.writeBoolean(((BooleanField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
        case JVM_SIGNATURE_CHAR:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
            out.writeChar(((CharField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
        case JVM_SIGNATURE_BYTE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
            out.writeByte(((ByteField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
        case JVM_SIGNATURE_SHORT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
            out.writeShort(((ShortField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1021
        case JVM_SIGNATURE_INT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1022
            out.writeInt(((IntField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1023
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1024
        case JVM_SIGNATURE_LONG:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
            out.writeLong(((LongField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
        case JVM_SIGNATURE_FLOAT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
            out.writeFloat(((FloatField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
        case JVM_SIGNATURE_DOUBLE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
            out.writeDouble(((DoubleField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
        case JVM_SIGNATURE_CLASS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
        case JVM_SIGNATURE_ARRAY: {
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
  1035
            if (VM.getVM().isCompressedOopsEnabled()) {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
  1036
              OopHandle handle = ((NarrowOopField)field).getValueAsOopHandle(oop);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
  1037
              writeObjectID(getAddressValue(handle));
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
  1038
            } else {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
  1039
              OopHandle handle = ((OopField)field).getValueAsOopHandle(oop);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
  1040
              writeObjectID(getAddressValue(handle));
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
  1041
            }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1042
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1043
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
        default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
            throw new RuntimeException("should not reach here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
    private void writeHeader(int tag, int len) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
        out.writeByte((byte)tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1051
        out.writeInt(0); // current ticks
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1052
        out.writeInt(len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1053
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1054
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1055
    private void writeDummyTrace() throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1056
        writeHeader(HPROF_TRACE, 3 * 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1057
        out.writeInt(DUMMY_STACK_TRACE_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1058
        out.writeInt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1059
        out.writeInt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1060
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1061
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1062
    private void writeClassSymbols(Klass k) throws IOException {
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1063
        writeSymbol(k.getName());
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1064
        if (k instanceof InstanceKlass) {
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1065
            InstanceKlass ik = (InstanceKlass) k;
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1066
            List declaredFields = ik.getImmediateFields();
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1067
            for (Iterator itr = declaredFields.iterator(); itr.hasNext();) {
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1068
                Field field = (Field) itr.next();
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1069
                writeSymbol(field.getName());
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1070
            }
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1071
        }
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1072
    }
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1073
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1074
    private void writeSymbols() throws IOException {
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1075
        // Write all the symbols that are used by the classes
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1076
        ClassLoaderDataGraph cldGraph = VM.getVM().getClassLoaderDataGraph();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1077
        try {
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1078
             cldGraph.classesDo(new ClassLoaderDataGraph.ClassVisitor() {
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1079
                            public void visit(Klass k) {
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1080
                                try {
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1081
                                    writeClassSymbols(k);
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1082
                                } catch (IOException e) {
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1083
                                    throw new RuntimeException(e);
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1084
                                }
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1085
                            }
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1086
                        });
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1087
        } catch (RuntimeException re) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1088
            handleRuntimeException(re);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1089
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1090
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1091
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1092
    private void writeSymbol(Symbol sym) throws IOException {
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1093
        // If name is already written don't write it again.
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1094
        if (names.add(sym)) {
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1095
            byte[] buf = sym.asString().getBytes("UTF-8");
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1096
            writeHeader(HPROF_UTF8, buf.length + OBJ_ID_SIZE);
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1097
            writeSymbolID(sym);
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1098
            out.write(buf);
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1099
        }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1100
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1101
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1102
    private void writeClasses() throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1103
        // write class list (id, name) association
42889
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents: 35217
diff changeset
  1104
        ClassLoaderDataGraph cldGraph = VM.getVM().getClassLoaderDataGraph();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1105
        try {
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 45371
diff changeset
  1106
            cldGraph.classesDo(new ClassLoaderDataGraph.ClassVisitor() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1107
                public void visit(Klass k) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1108
                    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1109
                        Instance clazz = k.getJavaMirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1110
                        writeHeader(HPROF_LOAD_CLASS, 2 * (OBJ_ID_SIZE + 4));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1111
                        out.writeInt(serialNum);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1112
                        writeObjectID(clazz);
45371
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
  1113
                        KlassMap.add(serialNum - 1, k);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1114
                        out.writeInt(DUMMY_STACK_TRACE_ID);
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
  1115
                        writeSymbolID(k.getName());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1116
                        serialNum++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1117
                    } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1118
                        throw new RuntimeException(exp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1119
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1120
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1121
            });
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1122
        } catch (RuntimeException re) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1123
            handleRuntimeException(re);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1124
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1125
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1126
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1127
    // writes hprof binary file header
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1128
    private void writeFileHeader() throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1129
        // version string
43671
a152f2d3320e 8171084: heapdump/JMapHeapCore fails with java.lang.RuntimeException: Heap segment size overflow
jgeorge
parents: 42889
diff changeset
  1130
        out.writeBytes(HPROF_HEADER_1_0_2);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1131
        out.writeByte((byte)'\0');
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1132
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1133
        // write identifier size. we use pointers as identifiers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1134
        out.writeInt(OBJ_ID_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1135
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1136
        // timestamp -- file creation time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1137
        out.writeLong(System.currentTimeMillis());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1138
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1139
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1140
    // writes unique ID for an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1141
    private void writeObjectID(Oop oop) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1142
        OopHandle handle = (oop != null)? oop.getHandle() : null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1143
        long address = getAddressValue(handle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1144
        writeObjectID(address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1145
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1146
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
  1147
    private void writeSymbolID(Symbol sym) throws IOException {
50929
ef57cfcd22ff 8205534: Remove SymbolTable dependency from serviceability agent
coleenp
parents: 47216
diff changeset
  1148
        assert names.contains(sym);
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
  1149
        writeObjectID(getAddressValue(sym.getAddress()));
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
  1150
    }
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
  1151
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1152
    private void writeObjectID(long address) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1153
        if (OBJ_ID_SIZE == 4) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1154
            out.writeInt((int) address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1155
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1156
            out.writeLong(address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1157
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1158
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1159
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1160
    private long getAddressValue(Address addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1161
        return (addr == null)? 0L : dbg.getAddressValue(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1162
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1163
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1164
    // get all declared as well as inherited (directly/indirectly) fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1165
    private static List/*<Field>*/ getInstanceFields(InstanceKlass ik) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1166
        InstanceKlass klass = ik;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1167
        List res = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1168
        while (klass != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1169
            List curFields = klass.getImmediateFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1170
            for (Iterator itr = curFields.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1171
                Field f = (Field) itr.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1172
                if (! f.isStatic()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1173
                    res.add(f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1174
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1175
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1176
            klass = (InstanceKlass) klass.getSuper();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1177
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1178
        return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1179
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1180
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1181
    // get size in bytes (in stream) required for given fields.  Note
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1182
    // that this is not the same as object size in heap. The size in
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1183
    // heap will include size of padding/alignment bytes as well.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1184
    private int getSizeForFields(List fields) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1185
        int size = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1186
        for (Iterator itr = fields.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1187
            Field field = (Field) itr.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1188
            char typeCode = (char) field.getSignature().getByteAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1189
            switch (typeCode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1190
            case JVM_SIGNATURE_BOOLEAN:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1191
            case JVM_SIGNATURE_BYTE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1192
                size++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1193
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1194
            case JVM_SIGNATURE_CHAR:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1195
            case JVM_SIGNATURE_SHORT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1196
                size += 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1197
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1198
            case JVM_SIGNATURE_INT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1199
            case JVM_SIGNATURE_FLOAT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1200
                size += 4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1201
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1202
            case JVM_SIGNATURE_CLASS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1203
            case JVM_SIGNATURE_ARRAY:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1204
                size += OBJ_ID_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1205
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1206
            case JVM_SIGNATURE_LONG:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1207
            case JVM_SIGNATURE_DOUBLE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1208
                size += 8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1209
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1210
            default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1211
                throw new RuntimeException("should not reach here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1212
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1213
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1214
        return size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1215
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1216
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1217
    // We don't have allocation site info. We write a dummy
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1218
    // stack trace with this id.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1219
    private static final int DUMMY_STACK_TRACE_ID = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1220
    private static final int EMPTY_FRAME_DEPTH = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1221
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1222
    private DataOutputStream out;
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
  1223
    private FileOutputStream fos;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1224
    private Debugger dbg;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1225
    private ObjectHeap objectHeap;
45371
5d0b68ea07c3 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 43671
diff changeset
  1226
    private ArrayList<Klass> KlassMap;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1227
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1228
    // oopSize of the debuggee
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1229
    private int OBJ_ID_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1230
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
  1231
    // Added for hprof file format 1.0.2 support
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
  1232
    private boolean useSegmentedHeapDump;
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
  1233
    private long currentSegmentStart;
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
  1234
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1235
    private long BOOLEAN_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1236
    private long BYTE_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1237
    private long CHAR_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1238
    private long SHORT_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1239
    private long INT_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1240
    private long LONG_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1241
    private long FLOAT_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1242
    private long DOUBLE_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1243
    private long OBJECT_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1244
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1245
    private long BOOLEAN_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1246
    private long BYTE_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1247
    private long CHAR_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1248
    private long SHORT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1249
    private long INT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
    private long LONG_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1251
    private long FLOAT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1252
    private long DOUBLE_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1253
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1254
    private static class ClassData {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1255
        int instSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1256
        List fields;
20388
2cf7b26682dc 6313383: SA: Update jmap to support HPROF binary format "JAVA PROFILE 1.0.2"
sla
parents: 17826
diff changeset
  1257
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1258
        ClassData(int instSize, List fields) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1259
            this.instSize = instSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1260
            this.fields = fields;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1261
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1262
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
    private Map classDataCache = new HashMap(); // <InstanceKlass, ClassData>
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1265
}